SUBROUTINE SUPROCFP_DEP(LDFPBUF,PLA,PLO,KPROCFP)

!**** *SUPROCFP_DEP*  - 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 departure geometry.

!**   INTERFACE.
!     ----------
!       *CALL* *SUPROCFP_DEP*

!        EXPLICIT ARGUMENTS
!        --------------------
!         INPUT : 
!          LDFPBUF  : .FALSE. if model and fullpos buffer have the same
!                     characteristics
!          PLA, PLO : global latitudes, longitudes of all output points
!         OUTPUT : 
!          KPROCFP  : processor number each point belongs

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

!     METHOD.
!     -------
!         ** Associate to interpolation point a model grid point, the rule is
!            the following:
!            - if the Gaussian/Lobatto latitude of the north-western model
!              grid-point is between 1 and NDGLG, THEN take it.
!            - if the Gaussian/Lobatto latitude of the north-western model
!              grid-point is zero (Northern pole or high resolution pole),
!              add 1 to latitude (SW point instead of NW point).
!            Remarks:
!            - LELAM=.T. coded according to similar relations used in
!              the routines SUEHOW1 and ELASCAW (compute IFPLANW).
!              the differences to Arpege come from the simplified geometry.
!            - a more complicated rule has to be studied later to minimize the
!              inter-processor variance of number of interpolation points
!              => the trick would be to define a larger halo so each processor
!                 could treat the same number of points (in that case, several
!                 points would be interpolated without the core!
!                 To be tried ... but the distribution of the output point will
!                 be difficult and the communication may increase ?? (Ryad. El Khatib)
!                 Then define the processor treating the interpolation point 
!                 (KPROCFP) as the processor treating the associated model 
!                 grid point (IPROCGP).
!        SEE DOCUMENTATION

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

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

!     AUTHOR.
!     -------
!        RYAD EL KHATIB *METEO-FRANCE* (SUPROCFP)
!        K. Yessad (renaming into SUPROCFP_DEP and adapting/cleaning)

!     MODIFICATIONS.
!     --------------
!        Original : 98-10-05 from sufpg
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!        K.Yessad : 27-Feb-2007: rename into SUPROCFP_DEP, adapt, clean.
!     ------------------------------------------------------------------

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

USE YOMCT0   , ONLY : LELAM
USE YOMDIM   , ONLY : NDGLG    ,NDGUNG   ,NDLUNG   ,NDLUXG
USE YOMFPG   , ONLY : NFPRGPG
USE YOMGEM   , ONLY : NLOENG   ,R4JP     ,NGPTOTG
USE YOMCST   , ONLY : RPI
USE YOMLEG   , ONLY : RLATIG
USE YEMGEO   , ONLY : EDELX    ,EDELY

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

IMPLICIT NONE

LOGICAL           ,INTENT(IN)    :: LDFPBUF 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PLA(NFPRGPG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PLO(NFPRGPG) 
INTEGER(KIND=JPIM),INTENT(OUT)   :: KPROCFP(NFPRGPG) 

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

INTEGER(KIND=JPIM) :: IASGPLAG, IASGPLOG, IAXNW, IFPLANW, ILAG,&
 & IROF, IZLATG, JGLGLO, JJPG  
INTEGER(KIND=JPIM) :: ISTAGPG(NDGLG),IPROCGP(NGPTOTG)

REAL(KIND=JPRB) :: ZFRL, ZLOO, ZLSDEPI
REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

#include "suprocgp.intfb.h"

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

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

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

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

!     1.1 Starting adress of each global latitude

IROF=1
DO JGLGLO=1,NDGLG
  ISTAGPG(JGLGLO)=IROF
  IROF=IROF+NLOENG(JGLGLO)
ENDDO

!     1.2 Processor number each model point belongs

CALL SUPROCGP(IPROCGP)

!*     2. COMPUTE KPROCFP
!         ---------------

IF (LDFPBUF) THEN

  IF (LELAM) THEN
    DO JJPG=1,NFPRGPG

      ZFRL =PLA(JJPG)/EDELY
      ILAG =INT(ZFRL)+NDGUNG
      ! * IFPLANW: DM-global regular y-coordinate number situated 
      !   immediately south of the interpolation point.
      IFPLANW=ILAG
      ! * IASGPLAG: DM-global regular y-coordinate number of the
      !   model grid-point which is associated to the interpolation point.
      IASGPLAG=MAX(1,MIN(NDGLG,IFPLANW))
      ! * IASGPLOG: x-coordinate number of the point of y-coord. number 
      !   IASGPLAG, immediately west to the interpolation point.
      ZFRL  =PLO(JJPG)/EDELX
      IAXNW =INT(ZFRL)+NDLUNG
      IASGPLOG=MAX(NDLUNG,MIN(IAXNW,NDLUXG))

      KPROCFP(JJPG)=IPROCGP(ISTAGPG(IASGPLAG)+IASGPLOG-1)

    ENDDO
  ELSE
    DO JJPG=1,NFPRGPG

      IZLATG=INT(R4JP*(0.5_JPRB*RPI-PLA(JJPG))+0.75_JPRB)
      ILAG  =INT(R4JP*(0.5_JPRB*RPI-PLA(JJPG))+0.75_JPRB)&
       & +NINT(SIGN(0.5_JPRB,RLATIG(IZLATG)-PLA(JJPG))-1.5_JPRB)  
      ! * IFPLANW: DM-global Gaussian/Lobatto latitude number situated 
      !   immediately north of the interpolation point.
      IFPLANW=ILAG+1
      ! * IASGPLAG: DM-global Gaussian/Lobatto latitude number of the
      !   model grid-point which is associated to the interpolation point.
      IASGPLAG=MAX(1,MIN(NDGLG,IFPLANW))
      ! * IASGPLOG: Longitude number of the point of latitude number 
      !   IASGPLAG, immediately west to the interpolation point.
      ZLOO = MOD(PLO(JJPG),2.0_JPRB*RPI)
      ZLSDEPI=REAL(NLOENG(IASGPLAG),JPRB)/(2.0_JPRB*RPI)
      IASGPLOG=INT(ZLOO*ZLSDEPI)
      IASGPLOG=IASGPLOG+NLOENG(IASGPLAG)*MAX(0,1-ABS(IASGPLOG))

      KPROCFP(JJPG)=IPROCGP(ISTAGPG(IASGPLAG)+IASGPLOG-1)

    ENDDO
  ENDIF

ELSE

  KPROCFP(:)=IPROCGP(:)

ENDIF

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

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