SUBROUTINE SUMPIOH(KFLG,KPROC,KPRIO,KFLD,KFLDOFF)

!**** *SUMPIOH*  - SET-UP MESSAGE PASSING FOR I/O HANDLINGS

!     PURPOSE.
!     --------
!        To setup in a contigus way the distribution of fields among I/O processors

!**   INTERFACE.
!     ----------
!       *CALL* *SUMPIOH()*

!        EXPLICIT ARGUMENTS
!        ------------------
!           KFLG     : number of fields to distribute
!           KPROC    : total number of processors
!           KPRIO    : number of processors for I/O
!           KFLD     : number of fields for each processor
!           KFLDOFF  : fields offset after distribution among KPRIO procs.

!        IMPLICIT ARGUMENTS
!        ------------------
!         See modules above

!     METHOD.
!     -------
!        SEE DOCUMENTATION

!     EXTERNALS.
!     ----------

!     REFERENCE.
!     ----------
!        ECMWF Research Department documentation of the IFS
!        Documentation about FULL-POS.

!     AUTHOR.
!     -------
!        RYAD EL KHATIB *METEO-FRANCE*

!     MODIFICATIONS.
!     --------------
!        ORIGINAL : 99-02-02
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!     ------------------------------------------------------------------

USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK

IMPLICIT NONE
INTEGER(KIND=JPIM),INTENT(IN)    :: KPROC 
INTEGER(KIND=JPIM),INTENT(IN)    :: KFLG 
INTEGER(KIND=JPIM),INTENT(IN)    :: KPRIO 
INTEGER(KIND=JPIM),INTENT(OUT)   :: KFLD(KPROC) 
INTEGER(KIND=JPIM),INTENT(OUT)   :: KFLDOFF(KPROC) 
!     IPROC  : processor number each field belongs

INTEGER(KIND=JPIM) :: IPROC(KFLG)

INTEGER(KIND=JPIM) :: JF, JROC
REAL(KIND=JPRB) :: ZHOOK_HANDLE

!     ------------------------------------------------------------------

!*      1. SETUP CONTIGUS DISTRIBUTION AMONG "I/O" PROCESSORS

IF (LHOOK) CALL DR_HOOK('SUMPIOH',0,ZHOOK_HANDLE)
DO JF=1,KFLG
  IPROC(JF)=MOD(JF-1,KPRIO)+1
ENDDO
DO JROC=1,KPROC
  KFLD(JROC)=COUNT(IPROC == JROC)
ENDDO
KFLDOFF(1)=0
DO JROC=2,KPROC
  KFLDOFF(JROC)=KFLDOFF(JROC-1)+KFLD(JROC-1)
ENDDO

IF (LHOOK) CALL DR_HOOK('SUMPIOH',1,ZHOOK_HANDLE)
END SUBROUTINE SUMPIOH
