SUBROUTINE LATTEX5(&
 ! --- INPUT --------------------------------------
 & KPROMA,KSTART,KPROF,KFLEV,KFLSA,KFLEN,KWLAG,KTLAG,&
 & PDTS2,PBT,PBDT,PXI,KSTEP,LDTWOTL,LDSETTLS,LDADVF,LD2TLFF,&
 & PESGP,PESGM,PRCORI,POMVRL,POMVRM,PCMSLP0,&
 & POROG,POROGL,POROGM,PTSUR,PRD,PCORDIH,PCORDIF,&
 & PGAGT5L,PGAGT5M,PTOD5,PSGRT5L,PSGRT5M,&
 & PKAP5,PVVEL5,PRDELP5,PEVEL5,&
 & PUT5,PVT5,PTT5,PGFLT5,&
 ! --- INPUT-OUTPUT AND OUTPUT --------------------
 & PUNLT95,PVNLT95,PTNLT95,&
 & PUL05,PVL05,PTL05,&
 & PUL95,PVL95,PTL95,PGFLL95,&
 & PUT15,PVT15)

!**** *LATTEX5* Semi-Lagrangian scheme. Trajectory version of LATTEX.
!                Computation of the t and t-dt useful quantities
!                 at grid-points. Equations for tri-dimensional
!                 variables.

!     Purpose.
!     --------
!       Trajectory version of LATTEX: see LATTEX for more information.
!       Remarks:
!        - This routine is called only in the TL model when NTRSLTYPE=1
!          but this option has never worked (uncomplete code).
!        - The following options are currently not coded in this routine:
!          * SL3TL.
!          * K[X]LAG = 2
!          * LVERCOR=T
!          * NH model.
!          * LPC_FULL.
!          * LSLPHY.

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

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

!        INPUT:
!          KPROMA  - horizontal dimension.
!          KSTART  - first element of work.
!          KPROF   - depth of work.
!          KFLEV   - number of layers.
!          KFLSA   - lower number of layer for arrays with extra-layers.
!          KFLEN   - upper number of layer for arrays with extra-layers.
!          KWLAG   - type of SL discretisation for momentum equation.
!          KTLAG   - type of SL discretisation for temperature equation.
!          PDTS2   - 0.5*time step for the first time-integration step of
!                    a leap-frog scheme or all time-integration steps of
!                    a two-time level scheme; time step for the following
!                    time-integration steps of a leap-frog scheme.
!          PBT     - PDTS2*BETADT (BETADT is in YOMDYN) or zero 
!                    according to the value of CDCONF.
!          PBDT    - PBT if semi-implicit scheme with unreduced
!                    divergence, PBT*(c**2/GM**2) if semi-implicit
!                    scheme with reduced divergence.
!          PXI     - coeff. for "alternative averaging" in 2TL scheme.
!          KSTEP   - Number of the current time-step.
!          LDTWOTL - .T./.F.: Two-time level/Three-time level SL scheme.
!          LDSETTLS- .T./.F.: Stable/Conventional extrapolations for SL2TL.
!          LDADVF  - .T./.F.: (Rochas implicit)/(No Rochas implicit)
!                    formulation for Coriolis term.
!          LD2TLFF - Recalculation of origin point for term 2 Omega wedge r.
!          PESGP   - (1 + uncentering factor).
!          PESGM   - (1 - uncentering factor).
!          PRCORI  - Coriolis parameter "f = 2 Omega sin(theta)".
!          POMVRL  - zonal component of "2 Omega vec a k".
!          POMVRM  - meridian component of "2 Omega vec a k".
!          PCMSLP0 - coefficient for Tanguay/Ritchie modification.
!          POROG   - surface orography.
!          POROGL  - zonal component of the surface orography gradient.
!          POROGM  - meridian component of the surface orography gradient.
!          PTSUR   - Reference surface temperature (standard atmosphere).
!          PRD     - Dry air constant.
!          PCORDIH - Values for the new thermodynamic eq. at half levels
!          PCORDIF - Values for the new thermodynamic eq. at full levels
! ---------------------- trajectory variables -------------------------
!          PGAGT5L - semi-implicit term at time t for U-wind equation.
!          PGAGT5M - semi-implicit term at time t for V-wind equation.
!          PTOD5   - semi-implicit term at time t for temperature equation.
!          PSGRT5L - zonal component of the pressure force term.
!          PSGRT5M - meridian component of the pressure force term.
!          PKAP5   - "R/cp" on layers at t.
!          PVVEL5  - "omega/prehyd" on layers a t.
!          PRDELP5 - "1/(pressure depth of layers)" at t.
!          PEVEL5  - "etadot (d prehyd/d eta)" on interlayers at t.
!          PUT5    - U-component of the wind on layers at t.
!          PVT5    - V-component of the wind on layers at t.
!          PTT5    - temperature on layers at t.
!          PGFLT5  - GFL on layers at t.
! ---------------------------------------------------------------------

!        INPUT/OUTPUT:
! ---------------------- trajectory variables -------------------------
!          PUNLT95 - nonlinear term in U-equn at t-dt.
!          PVNLT95 - nonlinear term in V-equn at t-dt.
!          PTNLT95 - nonlinear term in T-equn at t-dt.
!          PUL05   - second SL quantity to be
!                    interpolated (linearly) at O if KWLAG=3,
!                    for the U-wind equation.
!          PVL05   - second SL quantity to be
!                    interpolated (linearly) at O if KWLAG=3,
!                    for the V-wind equation.
!          PTL05   - second SL quantity to be
!                    interpolated (linearly) at O if KTLAG=3,
!                    for the temperature equation.
!          PUL95   - SL quantity to be interpolated at O for U-wind eqn.
!          PVL95   - SL quantity to be interpolated at O for V-wind eqn.
!          PTL95   - SL quantity to be interpolated at O for "T" equation.
!          PGFLL95 - SL quantity to be interpolated at O 
!          PUT15   - t+dt term and other final point terms for U-wind eqn.
!          PVT15   - t+dt term and other final point terms for V-wind eqn.
! ---------------------------------------------------------------------

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

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

!     Externals.
!     ----------
!           none
!           Called by LACDYNTL.

!     Reference.
!     ----------
!             Arpege documentation about semi-Lagrangian scheme.

!     Author.
!     -------
!        C. Temperton (ECMWF), code in LATTEXTL (until 2007)
!        K. Yessad (isolate trajectory code in LATTEXTL, 2007) 

!     Modifications.
!     --------------
!        Original : 28 Mar 2007.
!        K. Yessad Nov 2008: rationalisation of dummy argument interfaces
!        P. Bechtold+A. Untch 26-10-2008: add LEGWWMS switch for non-orogr. GWD
!     ------------------------------------------------------------------

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

USE YOMCVER  , ONLY : LVERTFE
USE YOM_YGFL , ONLY : YGFL    ,YGFLC
USE YOMCT0   , ONLY : LRFRIC
USE YOMDYN   , ONLY : RKRF
USE YOEPHY   , ONLY : LEGWWMS

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

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KPROMA 
INTEGER(KIND=JPIM),INTENT(IN)    :: KFLEV 
INTEGER(KIND=JPIM),INTENT(IN)    :: KFLSA 
INTEGER(KIND=JPIM),INTENT(IN)    :: KFLEN 
INTEGER(KIND=JPIM),INTENT(IN)    :: KSTART 
INTEGER(KIND=JPIM),INTENT(IN)    :: KPROF 
INTEGER(KIND=JPIM),INTENT(IN)    :: KWLAG 
INTEGER(KIND=JPIM),INTENT(IN)    :: KTLAG 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDTS2 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PBT 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PBDT(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PXI 
INTEGER(KIND=JPIM),INTENT(IN)    :: KSTEP 
LOGICAL           ,INTENT(IN)    :: LDTWOTL 
LOGICAL           ,INTENT(IN)    :: LDSETTLS 
LOGICAL           ,INTENT(IN)    :: LDADVF 
LOGICAL           ,INTENT(IN)    :: LD2TLFF 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PESGP 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PESGM 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRCORI(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: POMVRL(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: POMVRM(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCMSLP0 
REAL(KIND=JPRB)   ,INTENT(IN)    :: POROG(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: POROGL(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: POROGM(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTSUR 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRD 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCORDIH(0:KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCORDIF(KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGAGT5L(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGAGT5M(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTOD5(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSGRT5L(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSGRT5M(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PKAP5(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVVEL5(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRDELP5(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PEVEL5(KPROMA,0:KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PUT5(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVT5(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTT5(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGFLT5(KPROMA,KFLEV,YGFL%NDIM5) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PUNLT95(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PVNLT95(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTNLT95(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PUL05(KPROMA,KFLSA:KFLEN) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PVL05(KPROMA,KFLSA:KFLEN) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTL05(KPROMA,KFLSA:KFLEN) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PUL95(KPROMA,KFLSA:KFLEN) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PVL95(KPROMA,KFLSA:KFLEN) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTL95(KPROMA,KFLSA:KFLEN) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PGFLL95(KPROMA,KFLSA:KFLEN,YGFL%NUMFLDS_SL1) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PUT15(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PVT15(KPROMA,KFLEV) 

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

REAL(KIND=JPRB) :: ZMOY1U5(KPROMA,KFLEV)
REAL(KIND=JPRB) :: ZMOY1V5(KPROMA,KFLEV)
REAL(KIND=JPRB) :: ZMOY1T5(KPROMA,KFLEV)
REAL(KIND=JPRB) :: ZWT5(KPROMA)
REAL(KIND=JPRB) :: ZUSI5(KPROMA,KFLEV)
REAL(KIND=JPRB) :: ZVSI5(KPROMA,KFLEV)
REAL(KIND=JPRB) :: ZTSI5(KPROMA,KFLEV)
REAL(KIND=JPRB) :: ZTT15_DUM(KPROMA,KFLEV) 

INTEGER(KIND=JPIM) :: JLEV,JGFL,JROF

LOGICAL :: LLINCRXT1,LLSETTLSW

REAL(KIND=JPRB) :: ZCMSLP, ZXIGM, ZXIGP
REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

#include "lattex_dnt5.intfb.h"

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

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

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

!*      1.  PRELIMINARY INITIALISATIONS.
!       --------------------------------

ZCMSLP=PCMSLP0/(PRD*PTSUR)
IF (KSTEP <= 0) THEN
  ZXIGM=(1.0_JPRB-PXI)
  ZXIGP=(1.0_JPRB+PXI)
ELSE
  ZXIGM=(1.0_JPRB+PXI)
  ZXIGP=(1.0_JPRB+PXI)
ENDIF

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

!*      2.  TREATMENT OF GMV VARIABLES.
!       -------------------------------

!*       2.1   Momentum equation.

! * LDSETTLS is replaced by LLSETTLSW=.FALSE. for wind-eqn if VESL>0 because
!   stable extrapolation deteriorates scores without improving stability.
LLSETTLSW=LDSETTLS.AND.(.NOT.(PESGP > PESGM))

DO JLEV=1,KFLEV

  ! * U
  ZUSI5(KSTART:KPROF,JLEV)=PBT*PGAGT5L(KSTART:KPROF,JLEV)
  IF (LDADVF) THEN
    ZMOY1U5(KSTART:KPROF,JLEV) = -PDTS2*PSGRT5L(KSTART:KPROF,JLEV)
  ELSE
    DO JROF=KSTART,KPROF 
      ZMOY1U5(JROF,JLEV)=&
       & PDTS2*(PRCORI(JROF)*PVT5(JROF,JLEV)-PSGRT5L(JROF,JLEV))
    ENDDO
  ENDIF
  IF (LRFRIC .AND. .NOT.LEGWWMS) THEN
    DO JROF=KSTART,KPROF 
      ZMOY1U5(JROF,JLEV)=ZMOY1U5(JROF,JLEV)-PDTS2*RKRF(JLEV)*PUT5(JROF,JLEV)
    ENDDO
  ENDIF

  ! * V
  ZVSI5(KSTART:KPROF,JLEV)=PBT*PGAGT5M(KSTART:KPROF,JLEV)
  IF (LDADVF) THEN
    ZMOY1V5(KSTART:KPROF,JLEV) = -PDTS2*PSGRT5M(KSTART:KPROF,JLEV)
  ELSE
    DO JROF=KSTART,KPROF 
      ZMOY1V5(JROF,JLEV)=&
       & -PDTS2*(PRCORI(JROF)*PUT5(JROF,JLEV)+PSGRT5M(JROF,JLEV))
    ENDDO
  ENDIF

ENDDO

IF (LDTWOTL) THEN

  LLINCRXT1=LD2TLFF

  CALL LATTEX_DNT5(KPROMA,KSTART,KPROF,KFLEV,KFLSA,KFLEN,KSTEP,&
   & LLSETTLSW,LLINCRXT1,KWLAG,PESGP,PESGM,PXI,ZXIGP,ZXIGM,PUT5,&
   & ZMOY1U5,ZUSI5,PUNLT95,PUT15,PUL05,PUL95)

  CALL LATTEX_DNT5(KPROMA,KSTART,KPROF,KFLEV,KFLSA,KFLEN,KSTEP,&
   & LLSETTLSW,LLINCRXT1,KWLAG,PESGP,PESGM,PXI,ZXIGP,ZXIGM,PVT5,&
   & ZMOY1V5,ZVSI5,PVNLT95,PVT15,PVL05,PVL95)

ELSE         !!! T/L OF THREE-TIME-LEVEL SCHEME NOT DONE !!!

  ! (two calls to LATTEX_TNT5)

ENDIF

IF(LD2TLFF.AND.LDADVF) THEN
  DO JLEV=1,KFLEV
    PUT15(KSTART:KPROF,JLEV)=PUT15(KSTART:KPROF,JLEV)-POMVRL(KSTART:KPROF)
    PVT15(KSTART:KPROF,JLEV)=PVT15(KSTART:KPROF,JLEV)-POMVRM(KSTART:KPROF)
  ENDDO
ENDIF

!*       2.2   Temperature equation.

DO JLEV=1,KFLEV

  DO JROF=KSTART,KPROF 
    ZTSI5(JROF,JLEV)=PBDT(JROF)*PTOD5(JROF,JLEV)
  ENDDO

  IF(LVERTFE) THEN
    DO JROF=KSTART,KPROF 
      ZWT5(JROF)=PEVEL5(JROF,JLEV)*PRDELP5(JROF,JLEV)
    ENDDO
  ELSE
    DO JROF=KSTART,KPROF 
      ZWT5(JROF)=0.5_JPRB*(PEVEL5(JROF,JLEV)&
       & +PEVEL5(JROF,JLEV-1))*PRDELP5(JROF,JLEV)  
    ENDDO
  ENDIF

  DO JROF=KSTART,KPROF 
    ZMOY1T5(JROF,JLEV)=&
     & PDTS2*PKAP5(JROF,JLEV)*PTT5(JROF,JLEV)*PVVEL5(JROF,JLEV)&
     & +PDTS2*ZCMSLP*PCORDIF(JLEV)*PUT5(JROF,JLEV)*POROGL(JROF)&
     & +PDTS2*ZCMSLP*PCORDIF(JLEV)*PVT5(JROF,JLEV)*POROGM(JROF)&
     & +PDTS2*ZCMSLP*(PCORDIH(JLEV)-PCORDIH(JLEV-1))*ZWT5(JROF)*POROG(JROF)  
  ENDDO

ENDDO

IF (LDTWOTL) THEN

  LLINCRXT1=.FALSE.

  CALL LATTEX_DNT5(KPROMA,KSTART,KPROF,KFLEV,KFLSA,KFLEN,KSTEP,&
   & LDSETTLS,LLINCRXT1,KTLAG,PESGP,PESGM,PXI,ZXIGP,ZXIGM,PTT5,&
   & ZMOY1T5,ZTSI5,PTNLT95,ZTT15_DUM,PTL05,PTL95)

ELSE         !!! T/L OF THREE-TIME-LEVEL SCHEME NOT DONE !!!

  ! (one call to LATTEX_TNT5)

ENDIF

DO JLEV=1,KFLEV
  DO JROF=KSTART,KPROF
    PTL95(JROF,JLEV)=PTL95(JROF,JLEV)+PCORDIF(JLEV)*ZCMSLP*POROG(JROF)  
  ENDDO
ENDDO

!*       2.3   Anhydrostatic variables equations: "pressure departure".

!NYC-NH IF (LDNHDYN) THEN
!NYC-NH   TL not yet coded.
!NYC-NH ENDIF

!*       2.4a  Anhydrostatic variables equations: "vertical divergence".

!NYC-NH IF (LDNHDYN.AND.(.NOT.LGWADV)) THEN
!NYC-NH   TL not yet coded.
!NYC-NH ENDIF

!*       2.4b  Anhydrostatic variables equations: "gw".

!NYC-NH IF (LDNHDYN.AND.LGWADV) THEN
!NYC-NH   TL not yet coded.
!NYC-NH ENDIF

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

!*      3.  TREATMENT OF GFL VARIABLES.
!       --------------------------------

IF (LDTWOTL) THEN

  DO JGFL=1,YGFL%NUMFLDS
    IF(YGFLC(JGFL)%LADV) THEN
      PGFLL95(KSTART:KPROF,1:KFLEV,YGFLC(JGFL)%MP_SL1) = &
       & PGFLL95(KSTART:KPROF,1:KFLEV,YGFLC(JGFL)%MP_SL1) +&
       & PGFLT5(KSTART:KPROF,1:KFLEV,YGFLC(JGFL)%MP5)  
    ENDIF
  ENDDO

ELSE    !!! T/L OF THREE-TIME-LEVEL SCHEME NOT DONE !!!

ENDIF

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

!*      4.  DEALLOCATIONS.
!       ------------------

! Empty for the time being.

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

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

