SUBROUTINE GPTF2(&
 ! --- INPUT ---------------------------------------------------------
 & KST,KEN,LDFSTEP,&
 ! --- INPUT-OUTPUT --------------------------------------------------
 & PGMV,PGMVS,PGFL)

!**** *GPTF2* - Timefilter part 2

!     Purpose.
!     --------
!           Performs part 2 of the time-filtering.
!           - leap frog + ldfstep=true : pxt9. = pxt0.
!           - leap frog + ldfstep=false: pxt9. = pxt9. + eps2*pxt0.
!           - sl2tl     + ldfstep=true : put9=put0 and pvt9=pvt0 only.
!           - sl2tl     + ldfstep=false: nothing.

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

!        Explicit arguments :
!        --------------------

!        INPUT:
!         KST          : start of work
!         KEN          : depth of work
!         LDLSTEP      : check on the last time step.

!        INPUT/OUTPUT:
!         PGMV         : "t" and "t-dt" upper air GMV variables.
!         PGMVS        : "t" and "t-dt" surface GMV variables.
!         PGFL         : "t" and "t-dt" GFL variables.

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

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

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

!     Reference.
!     ----------
!        ECMWF Research Department documentation of the IFS

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

! Modifications.
! --------------
!   Original : 92-02-01
!   Modified 94-06-20 by M.Hamrud : upper air grid-point file
!   Modified 93-11-10 by R. EL Khatib (Non hydrostatic variables)
!   Modified 95-03-01 by M.Hamrud (F90 re-write)
!   Modified 96-03-07 by C. Temperton (no time-filter if LTWOTL)
!   Modified 96-02-20 by L.Isaksen (Adapted for Message Passing)
!   Modified 96-03 by A. Untch : grid-point prognostic ozone (EC)
!   Modified 98-01-07 by E.Holm (Included spectral ozone switch)
!   Modified 99-08-16 by C. Temperton (removed redundant RKROMA)
!   Modified 02-09-30 by P. Smolikova (variable d4 in NH)
!   Modified 13-11-02 K. YESSAD : cleanings + improve vectorization.
!   Modified 2003-07-17 C. Fischer - psvdauxt0* come from pgmv
!   01-Oct-2003 M. Hamrud  CY28 Cleaning
!   08-Jun-2004 J. Masek   NH cleaning (LVSLWBC)
!   Modified Nov 2007 N. Wedi: bug correction
!   N. Wedi and K. Yessad (Jan 2008): different dev for NH model and PC scheme
!   K. Yessad (Dec 2008): remove dummy CDLOCK
! End Modifications
!------------------------------------------------------------------

USE PARKIND1 , ONLY : JPIM     ,JPRB
USE YOMCT0   , ONLY : LTWOTL   ,LNHDYN
USE YOMDIM   , ONLY : NFLEVG   ,NFTHER   ,NPROMA
USE YOMDYN   , ONLY : REPS2    ,REPSM2
USE YOMDYNA  , ONLY : NVDVAR
USE YOMHOOK  , ONLY : LHOOK    ,DR_HOOK
USE YOM_YGFL , ONLY : YGFL     ,YGFLC

USE YOMGMV

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

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KST 
INTEGER(KIND=JPIM),INTENT(IN)    :: KEN 
LOGICAL           ,INTENT(IN)    :: LDFSTEP 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PGMV(NPROMA,NFLEVG,NDIMGMV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PGMVS(NPROMA,NDIMGMVS) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PGFL(NPROMA,NFLEVG,YGFL%NDIM) 

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

INTEGER(KIND=JPIM) :: JL,JGFL,JK
REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

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

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

!*       1. PERFORM TIME FILTER (PART 2)
!           ----------------------------

!*        1.1   TWO TIME LEVEL

IF (LTWOTL) THEN

  IF(LDFSTEP) THEN
    DO JK=1,NFLEVG
      DO JL=KST,KEN
        PGMV(JL,JK,YT9%MU) = PGMV(JL,JK,YT0%MU)
        PGMV(JL,JK,YT9%MV) = PGMV(JL,JK,YT0%MV)
      ENDDO
    ENDDO
  ENDIF

ELSE

!*        1.2   THREE TIME LEVEL

  IF(LDFSTEP) THEN
    DO JK=1,NFLEVG
      DO JL=KST,KEN
        PGMV(JL,JK,YT9%MU)   = PGMV(JL,JK,YT0%MU)
        PGMV(JL,JK,YT9%MV)   = PGMV(JL,JK,YT0%MV)
        PGMV(JL,JK,YT9%MDIV) = PGMV(JL,JK,YT0%MDIV)
      ENDDO
      IF(NFTHER >= 1) THEN
        DO JL=KST,KEN
          PGMV(JL,JK,YT9%MT)  = PGMV(JL,JK,YT0%MT)
          PGMV(JL,JK,YT9%MTL) = PGMV(JL,JK,YT0%MTL)
          PGMV(JL,JK,YT9%MTM) = PGMV(JL,JK,YT0%MTM)
        ENDDO
      ENDIF
    ENDDO

    DO JGFL=1,YGFL%NUMFLDS
      IF(YGFLC(JGFL)%LT9) THEN
        DO JK=1,NFLEVG
          DO JL=KST,KEN
            PGFL(JL,JK,YGFLC(JGFL)%MP9) = PGFL(JL,JK,YGFLC(JGFL)%MP)
          ENDDO
        ENDDO
      ENDIF
    ENDDO

    IF(LNHDYN) THEN
      DO JK=1,NFLEVG
        DO JL=KST,KEN
          PGMV(JL,JK,YT9%MSPD) = PGMV(JL,JK,YT0%MSPD)
          PGMV(JL,JK,YT9%MSVD) = PGMV(JL,JK,YT0%MSVD)
          PGMV(JL,JK,YT9%MSPDL) = PGMV(JL,JK,YT0%MSPDL)
          PGMV(JL,JK,YT9%MSVDL) = PGMV(JL,JK,YT0%MSVDL)
          PGMV(JL,JK,YT9%MSPDM) = PGMV(JL,JK,YT0%MSPDM)
          PGMV(JL,JK,YT9%MSVDM) = PGMV(JL,JK,YT0%MSVDM)
        ENDDO
      ENDDO

      IF( NVDVAR==4 ) THEN
        DO JK=1,NFLEVG
          DO JL=KST,KEN
            PGMV(JL,JK,YT9%MNHX) = PGMV(JL,JK,YT0%MNHX)
          ENDDO
        ENDDO
      ENDIF

    ENDIF

    DO JL=KST,KEN
      PGMVS(JL,YT9%MSP)  = PGMVS(JL,YT0%MSP)
      PGMVS(JL,YT9%MSPL) = PGMVS(JL,YT0%MSPL)
      PGMVS(JL,YT9%MSPM) = PGMVS(JL,YT0%MSPM)
    ENDDO

  ELSE

    DO JK=1,NFLEVG
      DO JL=KST,KEN
        PGMV(JL,JK,YT9%MU)   = PGMV(JL,JK,YT9%MU)  +REPS2*PGMV(JL,JK,YT0%MU)
        PGMV(JL,JK,YT9%MV)   = PGMV(JL,JK,YT9%MV)  +REPS2*PGMV(JL,JK,YT0%MV)
        PGMV(JL,JK,YT9%MDIV) = PGMV(JL,JK,YT9%MDIV)+REPS2*PGMV(JL,JK,YT0%MDIV)
      ENDDO
      IF(NFTHER >= 1) THEN
        DO JL=KST,KEN
          PGMV(JL,JK,YT9%MT)  = PGMV(JL,JK,YT9%MT)  +REPS2*PGMV(JL,JK,YT0%MT)
          PGMV(JL,JK,YT9%MTL) = PGMV(JL,JK,YT9%MTL) +REPS2*PGMV(JL,JK,YT0%MTL)
          PGMV(JL,JK,YT9%MTM) = PGMV(JL,JK,YT9%MTM) +REPS2*PGMV(JL,JK,YT0%MTM)
        ENDDO
      ENDIF
    ENDDO

    DO JGFL=1,YGFL%NUMFLDS
      IF(YGFLC(JGFL)%LT9) THEN
        DO JK=1,NFLEVG
          DO JL=KST,KEN
            PGFL(JL,JK,YGFLC(JGFL)%MP9) = PGFL(JL,JK,YGFLC(JGFL)%MP9)+&
             & REPSM2*PGFL(JL,JK,YGFLC(JGFL)%MP)  
          ENDDO
        ENDDO
      ENDIF
    ENDDO

    IF(LNHDYN) THEN
      DO JK=1,NFLEVG
        DO JL=KST,KEN
          PGMV(JL,JK,YT9%MSPD)  = &
           & PGMV(JL,JK,YT9%MSPD)+REPS2*PGMV(JL,JK,YT0%MSPD)
          PGMV(JL,JK,YT9%MSVD)  = &
           & PGMV(JL,JK,YT9%MSVD)+REPS2*PGMV(JL,JK,YT0%MSVD)
          PGMV(JL,JK,YT9%MSPDL) = &
           & PGMV(JL,JK,YT9%MSPDL)+REPS2*PGMV(JL,JK,YT0%MSPDL)
          PGMV(JL,JK,YT9%MSVDL) = &
           & PGMV(JL,JK,YT9%MSVDL)+REPS2*PGMV(JL,JK,YT0%MSVDL)
          PGMV(JL,JK,YT9%MSPDM) = &
           & PGMV(JL,JK,YT9%MSPDM)+REPS2*PGMV(JL,JK,YT0%MSPDM)
          PGMV(JL,JK,YT9%MSVDM) = &
           & PGMV(JL,JK,YT9%MSVDM)+REPS2*PGMV(JL,JK,YT0%MSVDM)
        ENDDO
      ENDDO
    ENDIF

    DO JL=KST,KEN
      PGMVS(JL,YT9%MSP)  = PGMVS(JL,YT9%MSP) +REPS2*PGMVS(JL,YT0%MSP)
      PGMVS(JL,YT9%MSPL) = PGMVS(JL,YT9%MSPL)+REPS2*PGMVS(JL,YT0%MSPL)
      PGMVS(JL,YT9%MSPM) = PGMVS(JL,YT9%MSPM)+REPS2*PGMVS(JL,YT0%MSPM)
    ENDDO

  ENDIF

ENDIF

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

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