SUBROUTINE GPENDTR( &
 ! --- INPUT ------------------------------------------------------------------
 & KST,KEND,LDADNMI,PDT,&
 & PGFLT1,&
 & PUT0 ,PVT0  ,PTT0 ,PSPT0,&
 & PSLB2USI,PSLB2VSI ,PSLB2TSI,PSLB2SPSI,PSLB2PDSI,PSLB2VDSI,&
 ! --- OUTPUT -----------------------------------------------------------------
 & PUT1    ,PVT1     ,PTT1,&
 & PSPD1   ,PSVD1    ,PSPT1,&
 ! --- INPUT/OUTPUT -----------------------------------------------------------
 & PGFL)

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

USE YOMDIM   , ONLY : NFLEVG   ,NPROMA  
USE YOMCT0   , ONLY : LSPRT    ,LNHDYN 
USE YOMCST   , ONLY : RD
USE YOM_YGFL , ONLY : YGFL     ,YQ, YL, YI, YR, YS, YG

!**** *GPENDTR - Final memory transfers in CPGLAG

!     Purpose.

!**   Interface.
!     ----------
!        *CALL* *GPENDTR(...)*

!        Explicit arguments : see CPGLAG, all names the same
!        -------------------- except for DOCTOR norm modifications

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

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

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

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

!     Author.
!     -------
!        Mats Hamrud  *ECMWF*
!     --------

!     Modifications.
!     --------------
!      Original : 95-03-02 (From CPGLAG)
!      Modified 96-03 by A. Untch : grid-point prognostic ozone (EC)
!      Modified 98-01-09 by E. Holm : changed order of ozone and 
!                                     included spectral switch
!      Modified 15-10-01 D.Salmond FULLIMP mods
!      Modified 30-09-02 P.Smolikova : variable d4 in NH
!      Modified 12-10-2002 by J. Masek: PC bugfix.
!      Modified 08-2002 C. Smith : use "w" as prognostic variable in the
!       semi-lag advection of vertical divergence in the NH model.
!      Modified 10-2002 K. YESSAD: cleanings for better vectorization.
!      M.Hamrud      01-Oct-2003 CY28 Cleaning
!      Modified 04-02-06: Y. Seity : new arguments to GPRCP for AROME
!      K. Yessad (Dec 2008): remove dummy CDLOCK + cleanings
!     ------------------------------------------------------------------

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KST 
INTEGER(KIND=JPIM),INTENT(IN)    :: KEND 
LOGICAL           ,INTENT(IN)    :: LDADNMI 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDT 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGFLT1(NPROMA,NFLEVG,YGFL%NDIM1) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PUT0(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVT0(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTT0(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSPT0(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSLB2USI(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSLB2VSI(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSLB2TSI(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSLB2SPSI(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSLB2PDSI(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSLB2VDSI(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PUT1(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PVT1(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTT1(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PSPD1(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PSVD1(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PSPT1(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGFL(NPROMA,NFLEVG,YGFL%NDIM) 

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

!   --- ZR1,ZR0 useful when LSPRT=T only ---
REAL(KIND=JPRB) :: ZR1 (NPROMA,NFLEVG)
REAL(KIND=JPRB) :: ZR0 (NPROMA,NFLEVG)

REAL(KIND=JPRB)    :: ZRD_R

REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

#include "gprcp.intfb.h"

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

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

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

!*       1.  TRANSFER FROM BUFFER TO T+DT ARRAYS (USUAL CASE)
!            ------------------------------------------------

ZRD_R = 1.0_JPRB/RD
IF (.NOT.LDADNMI ) THEN

  PUT1(KST:KEND,1:NFLEVG)=PUT1(KST:KEND,1:NFLEVG)+PSLB2USI(KST:KEND,1:NFLEVG)
  PVT1(KST:KEND,1:NFLEVG)=PVT1(KST:KEND,1:NFLEVG)+PSLB2VSI(KST:KEND,1:NFLEVG)
  PTT1(KST:KEND,1:NFLEVG)=PTT1(KST:KEND,1:NFLEVG)+PSLB2TSI(KST:KEND,1:NFLEVG)
  IF(LNHDYN) THEN
    PSPD1(KST:KEND,1:NFLEVG)=PSPD1(KST:KEND,1:NFLEVG)+PSLB2PDSI(KST:KEND,1:NFLEVG)
    PSVD1(KST:KEND,1:NFLEVG)=PSVD1(KST:KEND,1:NFLEVG)+PSLB2VDSI(KST:KEND,1:NFLEVG)
  ENDIF
  
  PSPT1(KST:KEND)=PSPT1(KST:KEND)+PSLB2SPSI(KST:KEND)
  
  IF (LSPRT) THEN
    CALL GPRCP(NPROMA,KST,KEND,NFLEVG,PR=ZR1,PGFL=PGFLT1,KGFLTYP=1)
    PTT1(KST:KEND,1:NFLEVG)=ZR1(KST:KEND,1:NFLEVG)*PTT1(KST:KEND,1:NFLEVG)*ZRD_R
  ENDIF

!!$  DO JGFL=1,YGFL%NUMFLDS
!!$    IF(YGFLC(JGFL)%LT1) THEN
!!$      PGFL(KST:KEND,:,YGFLC(JGFL)%MP) = PGFLT1(KST:KEND,:,YGFLC(JGFL)%MP1)
!!$    ENDIF
!!$  ENDDO
  
ELSEIF (.NOT.LSPRT) THEN
  
!*       2.0  TRANSFER FROM BUFFER TO T+DT ARRAYS AND DIVIDE BY TIME-STEP
!            (NNMI WITH NOT SPECTRAL RT, TENDENCIES REQUESTED)

  PUT1(KST:KEND,1:NFLEVG)=PUT1(KST:KEND,1:NFLEVG)/PDT
  PVT1(KST:KEND,1:NFLEVG)=PVT1(KST:KEND,1:NFLEVG)/PDT
  PTT1(KST:KEND,1:NFLEVG)=PTT1(KST:KEND,1:NFLEVG)/PDT
  IF(LNHDYN) THEN
    PSPD1(KST:KEND,1:NFLEVG)=0.0_JPRB
    PSVD1(KST:KEND,1:NFLEVG)=0.0_JPRB
  ENDIF
  PSPT1(KST:KEND)=PSPT1(KST:KEND)/PDT

ELSE

!*       3.0  TRANSFER FROM BUFFERS TO T+DT ARRAYS
!            (NNMI WITH  SPECTRAL RT, TENDENCIES REQUESTED)

  CALL GPRCP(NPROMA,KST,KEND,NFLEVG,PR=ZR1,PGFL=PGFLT1,KGFLTYP=1)
  CALL GPRCP(NPROMA,KST,KEND,NFLEVG,PR=ZR0,PGFL=PGFL)

  PUT1(KST:KEND,1:NFLEVG)=(PUT1(KST:KEND,1:NFLEVG)-PUT0(KST:KEND,1:NFLEVG))/PDT
  PVT1(KST:KEND,1:NFLEVG)=(PVT1(KST:KEND,1:NFLEVG)-PVT0(KST:KEND,1:NFLEVG))/PDT
  PTT1(KST:KEND,1:NFLEVG)=(ZR1(KST:KEND,1:NFLEVG)*PTT1(KST:KEND,1:NFLEVG)/RD &
   & -ZR0(KST:KEND,1:NFLEVG)*PTT0(KST:KEND,1:NFLEVG)/RD)/PDT  
  IF(LNHDYN) THEN
    PSPD1(KST:KEND,1:NFLEVG) = 0.0_JPRB
    PSVD1(KST:KEND,1:NFLEVG) = 0.0_JPRB
  ENDIF

  PSPT1(KST:KEND) = (PSPT1(KST:KEND)-PSPT0(KST:KEND))/PDT

ENDIF

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

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