SUBROUTINE DIGP(KDIR,KGPP,PGFLGP)

USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK
USE YOMDIM   , ONLY : NFLEVG   ,NPROMA
USE YOMGEM   , ONLY : NGPTOT
USE YOM_YGFL , ONLY : YGFL     ,YGFLC
USE YOMGFL   , ONLY : GFL


!**** *DIGP*  - Save upper air grid point field

!     Purpose.
!     --------
!        Store or restore grid-point fields (GFL) in a buffer           

!**   Interface.
!     ----------
!        *CALL* *DIGP*

!        Explicit arguments :  
!        ------------------
!             KDIR direction of copy 1 GFLGP  ==> buffer 
!                                   -1 buffer ==> GFLGP
!             PGFLGP : Buffer for GFLGP fields
!             KGPP   : Size of the buffer

!        Implicit arguments :
!        ------------------

!     Method.
!     -------
!        See documentation

!     Externals.
!     ----------

!     Reference.
!     ----------

!     Author.
!     -------
!        *Meteo-France*

!     Modifications.
!     --------------
!        Original : 01-April-2006 Y. Bouteloup

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


IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KGPP
INTEGER(KIND=JPIM),INTENT(IN)    :: KDIR
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PGFLGP(NGPTOT,KGPP)

#include "abor1.intfb.h"

INTEGER(KIND=JPIM) :: JGFL, IGFL, JLEV, IOFF, JKGLO, IBL, IST, IEND, JROF

REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

!        1.    Save GFL to buffer
!              ------------------------

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

IF (KDIR == 1) THEN
   DO JKGLO=1,NGPTOT,NPROMA
     IBL=(JKGLO-1)/NPROMA+1
     IST=1
     IEND=MIN(NPROMA,NGPTOT-JKGLO+1)
     IGFL=0
     DO JGFL=1,YGFL%NUMFLDS
       SELECT CASE (YGFLC(JGFL)%LGP)
       CASE(.TRUE.)
         IGFL=IGFL+1
         IOFF=(IGFL-1)*NFLEVG
         DO JLEV=1,NFLEVG
           DO JROF=IST,IEND
             PGFLGP(JROF+JKGLO-1,IOFF+JLEV)=GFL(JROF,JLEV,YGFLC(JGFL)%MP,IBL)
           ENDDO
         ENDDO
       END SELECT
     ENDDO
   ENDDO
ELSEIF (KDIR == -1) THEN
   DO JKGLO=1,NGPTOT,NPROMA
     IBL=(JKGLO-1)/NPROMA+1
     IST=1
     IEND=MIN(NPROMA,NGPTOT-JKGLO+1)
     IGFL=0
     DO JGFL=1,YGFL%NUMFLDS
       SELECT CASE (YGFLC(JGFL)%LGP)
       CASE(.TRUE.)
         IGFL=IGFL+1
         IOFF=(IGFL-1)*NFLEVG
         DO JLEV=1,NFLEVG
           DO JROF=IST,IEND
             GFL(JROF,JLEV,YGFLC(JGFL)%MP,IBL)=PGFLGP(JROF+JKGLO-1,IOFF+JLEV)
           ENDDO
         ENDDO
       END SELECT
     ENDDO
   ENDDO
ELSE
   CALL ABOR1(' DIGP : Value not expected for kdir')
ENDIF

IF (LHOOK) CALL DR_HOOK('DIGP',1,ZHOOK_HANDLE)

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

END SUBROUTINE DIGP
