SUBROUTINE SUPROCFP(KPROCFP)

!**** *SUPROCFP*  - Setup processor number - Fullpos

!     PURPOSE.
!     --------
!        To initialize the array telling for each output point which 
!        processor treats this point, in DM-distribution linked to
!        the arrival geometry.

!**   INTERFACE.
!     ----------
!       *CALL* *SUPROCFP*

!        EXPLICIT ARGUMENTS
!        --------------------
!         OUTPUT : 
!          KPROCFP  : processor number each point belongs

!        IMPLICIT ARGUMENTS
!        --------------------
!        See module below

!     METHOD.
!     -------
!        Associate to a post-processing point a processor, in the DM-distribution
!         linked to arrival geometry. This is simpler than in the DM-distribution
!         linked to departure geometry, because, in a DM-global array dimensioned
!         with NFPRGPG, the ordering is already done according the arrival
!         geometry: that means that the processor repartition will respect the
!         above ordering and point contiguity.
!         Processor repartition for the NFPRGRG points:

!         ..........**********..........**********..........  etc
!         1st proc  2nd proc  3rd proc  4th proc  5th proc

!         The number of points for each processor is as close as possible
!         to NFPRGRG/NPROC (NFPRGPL of each processor).
!         If IR=NFPRGPG-NPROC*(NFPRGPG/NPROC), processors 1 to IR treat
!          NFPRGPLX points, processors IR+1 to NPROC treat NFPRGPLX-1 points.

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

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

!     AUTHOR.
!     -------
!        RYAD EL KHATIB *METEO-FRANCE* (old SUPROCFP)
!        K. Yessad (renaming old SUPROCFP into SUPROCFP_DEP,
!         creating this new SUPROCFP).

!     MODIFICATIONS.
!     --------------
!        Original : 98-10-05 from sufpg
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!        K.Yessad : 27-Feb-2007: adapt to DM-arrival geometry, clean.
!     ------------------------------------------------------------------

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

USE YOMCT0   , ONLY : NPROC
USE YOMFPC   , ONLY : NFPDOM
USE YOMFPD   , ONLY : NFPSIZEG
USE YOMFPG   , ONLY : NFPRGPG

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

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(OUT)   :: KPROCFP(NFPRGPG) 

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

INTEGER(KIND=JPIM) :: IR, IRA, JROC, IST, IEND
INTEGER(KIND=JPIM) :: IOFF, JDOM

REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

IF (LHOOK) CALL DR_HOOK('SUPROCFP',0,ZHOOK_HANDLE)

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

!*     1. PRELIMINARY COMPUTATIONS
!         ------------------------

! k.y.: this piece of code (currently commented)
!       assumes that we don't split the different
!       subdomains before distributing.
!       (ex for NFPDOM=2, NPROC=3):
!       ..................********* *********..................
!       1st proc 1st proc 2nd proc  2nd proc 3rd proc 3rd proc
!       dom1     dom1     dom1      dom2     dom2     dom2

!       Is it the right way for NFPSURFEX>1?

!! IR = NFPRGPG - NPROC*(NFPRGPG/NPROC)
!! IRA = INT(NFPRGPG/NPROC)

!! DO JROC=1,IR
!!   IST=1+(JROC-1)*(IRA+1)
!!   IEND=JROC*(IRA+1)
!!   KPROCFP(IST:IEND)=JROC
!! ENDDO

!! DO JROC=IR+1,NPROC
!!   IST=1+IR*(IRA+1)+(JROC-IR-1)*IRA
!!   IEND=IR*(IRA+1)+(JROC-IR)*IRA
!!   KPROCFP(IST:IEND)=JROC
!! ENDDO

! k.y.: alternative:
!       split the different subdomains before distributing.
!       Processor repartition for the NFPRGRG points:
!       (ex for NFPDOM=2, NPROC=3):
!       .........*********......... .........*********.........
!       1st proc 2nd proc 3rd proc  1st proc 2nd proc 3rd proc
!       dom1     dom1     dom1      dom2     dom2     dom2

IOFF=0
DO JDOM=1,NFPDOM

  IR = NFPSIZEG(JDOM) - NPROC*(NFPSIZEG(JDOM)/NPROC)
  IRA = INT(NFPSIZEG(JDOM)/NPROC)

  DO JROC=1,IR
    IST=1+(JROC-1)*(IRA+1)
    IEND=JROC*(IRA+1)
    KPROCFP(IOFF+IST:IOFF+IEND)=JROC
  ENDDO

  DO JROC=IR+1,NPROC
    IST=1+IR*(IRA+1)+(JROC-IR-1)*IRA
    IEND=IR*(IRA+1)+(JROC-IR)*IRA
    KPROCFP(IOFF+IST:IOFF+IEND)=JROC
  ENDDO

  IOFF=IOFF+NFPSIZEG(JDOM)

ENDDO

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

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