SUBROUTINE LATTEX( &
 ! --- INPUT --------------------------------------------------
 & KST,KPROF,PDTS2,PBT,PBDT,PESGP,PESGM,&
 & PGNORDL,PGNORDM,PRCORI,PRCORIC,POMVRL,POMVRM,POROG,POROGL,POROGM,&
 & PVCRSSA0F,PVCASRS0F,PVCRSSA9F,PRDLR0,PRDLR9,PVCW0F,PWDLW0F,&
 & PGAGT0L,PGAGT0M,PTOD0,PSPDS0,PLPD0,PDPD0,&
 & PGAGT9L,PGAGT9M,PTOD9,PSPDS9,PLPD9,PDPD9,&
 & PSGRTL,PSGRTM,PKAP,PVVEL,PRDELP,PEVEL,P3DIVG,PTNDPD,PTNDVD,PTNDGW,&
 & PNHXT0,PGWT0,PNHXT9,PGWT9,PGFL,&
 ! --- INPUT/OUTPUT -------------------------------------------
 & PGMV,PGMVT1,PB1, &
 ! --- OUTPUT -------------------------------------------------
 & PB2)

!**** *LATTEX*   Semi-Lagrangian scheme.
!                Computation of the t and t-dt useful quantities
!                 at grid-points. Equations for tri-dimensional
!                 variables.
!                Abbreviation "vwv" stands for "vertical wind variable".

!     Purpose.
!     --------
!        * This subroutine computes the equation quantities to be
!          interpolated at each grid-point of the colocation grid
!          (Gauss grid at all levels).
!          The terms considered here are the explicit terms and
!          the explicit part of the semi-implicit scheme (terms
!          previously computed in LASSIE or LANHSI).
!          Equations considered here are equations for tri-dimensional
!          variables: momentum, temperature, NH variables, GFL.
!        * Remark 1: when an alternate averaging is used for linear terms
!          in the 2TL SL scheme, the first timestep is treated differently
!          (first order uncentering), no uncentering is applied to the
!          total term ("cursive A") and the term saved in PGMV(1,1,YT9%M[X]NL)
!          is [ (Delta t/2) ("cursive A" - (1 + xidt) beta "cursive B") ]
!          instead of [ (Delta t/2) ("cursive A" - beta "cursive B") ].
!        * Remark 2: for lsettls=true, uncentering is applied to
!          the 'stable' extrapolation if vesl > 0 to avoid instability
!          in the momentum equation.
!        * Remark 3: for lnh_pdvd.and.lgwadv=true in the NH model, variable
!          "gw" is advected instead of vertical divergence; it is advected at
!          half levels if lvfe_gw=f, full levels if lvfe_gw=t.
!          That means that PDPD0,PDPD9,PTNDGW,PGWT0,PGWT9 contain:
!          - half level values 0 to nflevg-1 if LVFE_GW=F.
!          - full level values 1 to nflevg if LVFE_GW=T.
!        * Remark 4: for PC schemes:
!          - this routine is called for nsiter=0.
!          - this routine is called for the predictor of lpc_old.
!          - this routine is called for the predictor of lpc_full.
!          - this routine is called for the corrector of lpc_full.

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

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

!        INPUT:
!          KST         - first element of work.
!          KPROF       - depth of work.
!          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).
!          PBDT        - PBT if semi-implicit scheme with unreduced
!                        divergence, PBT*(c**2/GM**2) if semi-implicit
!                        scheme with reduced divergence.
!          PESGP       - (1 + uncentering factor).
!          PESGM       - (1 - uncentering factor).
!          PGNORDL     - zonal component ("Gnordl") of the unit vector
!                        directed towards the true North pole.
!          PGNORDM     - meridian component ("Gnordm") of the unit vector
!                        directed towards the true North pole.
!          PRCORI      - Coriolis parameter "f = 2 Omega sin(theta)".
!          PRCORIC     - 2 Omega cos(theta).
!          POMVRL      - zonal component of "2 Omega vec a k".
!          POMVRM      - meridian component of "2 Omega vec a k".
!          POROG       - surface orography.
!          POROGL      - zonal component of "rssavnabla(surf orography)"
!          POROGM      - meridian component of "rssavnabla(surf orography)"
!          PVCRSSA0F   - "rs/a" at full levels at t.
!          PVCASRS0F   - "a/rs" at full levels at t.
!          PVCRSSA9F   - "rs/a" at full levels at t-dt.
!          PRDLR0      - array containing "r/a", "a/r", "grad(r/a)" at t.
!          PRDLR9      - array containing "r/a", "a/r" at t-dt.
!          PVCW0F      - pseudo-vertical velocity "W = Drs/Dt" at full lev at t.
!          PWDLW0F     - vertical velocity "w = Dr/Dt" at full levels at t.
!          PGAGT0L     - semi-implicit term at time t for U-wind equation.
!          PGAGT0M     - semi-implicit term at time t for V-wind equation.
!          PTOD0       - semi-implicit term at time t for temperature equation.
!          PSPDS0      - semi-implicit term at time t for press dep equation.
!          PLPD0       - semi-implicit term at time t for vert div equation.
!          PDPD0       - semi-implicit term at time t for "gw" equation.
!          PGAGT9L     - semi-implicit term at time t-dt for U-wind equation.
!          PGAGT9M     - semi-implicit term at time t-dt for V-wind equation.
!          PTOD9       - semi-implicit term at time t-dt for temperature eqn.
!          PSPDS9      - semi-implicit term at time t-dt for press dep equation.
!          PLPD9       - semi-implicit term at time t-dt for vert div equation.
!          PDPD9       - semi-implicit term at time t-dt for "gw" equation.
!          PSGRTL      - semi-reduced zonal compon. of the pressure force grad.
!          PSGRTM      - semi-reduced merid compon. of the pressure force grad.
!          PKAP        - "R/cp" at full levels at t.
!          PVVEL       - "omega/prehyd" at full levels a t.
!          PRDELP      - "1/(pressure depth of layers)" at t.
!          PEVEL       - "etadot (d prehyd/d eta)" at half levels at t.
!          P3DIVG      - true 3D divergence at full levels at t.
!          PTNDPD      - adiabatic term in RHS of press dep equation.
!          PTNDVD      - adiabatic term in RHS of vert div equation.
!          PTNDGW      - adiabatic term in RHS of "gw" equation.
!          PNHXT0      - "X" at full levels at t, diagnosed in CPG_GP.
!          PGWT0       - "gw" at t (LGWADV=T only; layers: see in CPG_GP).
!          PNHXT9      - "X" at full levels at t-dt, diagnosed in CPG_GP.
!          PGWT9       - "gw" at t-dt (LGWADV=T only; layers: see in CPG_GP).
!          PGFL        - unified_treatment grid-point fields

!        INPUT/OUTPUT:
!          PGMV        - GMV variables at t-dt and t.
!          PGMVT1      - GMV variables at t+dt.
!          PB1         - "SLBUF1" buffer for interpolations.

!        OUTPUT:
!          PB2         - "SLBUF2" buffer.

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

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

!     Externals.
!     ----------
!           none
!           Called by LACDYN.

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

!     Author.
!     -------
!        K. YESSAD (METEO FRANCE/CNRM/GMAP) after old part 3.2 of LACDYN. 
!        DO loops have been updated according to F90 norms.

! Modifications
! -------------
!   Original : AUGUST 1995.
!   K. YESSAD (MARCH 1996): add option LDCORAN.
!   K. YESSAD, R. BUBNOVA and M. JANOUSEK (APRIL 1996):
!     - Non hydrostatic scheme.
!   Clive Temperton 96-03-06 : removal of time-filtering in 2TL scheme
!   A. Untch 96-03 : grid-point prognostic ozone (EC)
!   Clive Temperton 96-04-16 : "alternative averaging" in 2TL scheme
!   Clive Temperton/M. Hortal 96-10-23 : analytic treatment of
!                   Coriolis term in 2TL semi-Lagrangian scheme
!     for hydrostatic model.
!   Modified OCT 1996 by K. YESSAD: removal of variable uncentering,
!     arrays PESGP, PESGM, PESGA, PESGP, PESGM, PESGA become scalar.
!   C. Temperton 97-02-24: modified first timestep when XIDT>0
!   M. Charron (June 1998): LSETTLS for NH variables
!   K. YESSAD (July 1998): cleaning
!    - removal of options NODEC=2, L2TLAW=T, L2TLAT=T
!    - merge of VESLA and VESL into VESL
!    - clearer writing of options LSETTLS=T/F
!    - clearer writing of options XIDT>0/VESL>0
!    - add uncentering in the stable extrapolation (under LSETTLS)
!      when VESL>0 to avoid instability in the momentum equation.
!    - rewrite code in a more vectorisable way.
!   E. Holm 98-05-26: Order of ozone changed in call. 
!   Modified 99-03-31 by P. SMOLIKOVA: option LRDAORG (double averaging).
!   K. YESSAD 14-10-1999: inhibit stable extrapolation in wind-eqn
!    if LSETTLS=T and VESL>0 (because deteriorates scores).
!   C. Temperton 00-03-24: repair LSETTLS (case XIDT=0 and VESL=0)
!   K. YESSAD 15-11-1999: rewrite SL3TL like SL2TL.
!   M. SZCZECH 00-02-24: ozone - LDGPO3 -> LDADVO3.
!   Modified 01-06-22 by K. YESSAD:
!    - relaxation of thin layer hypothesis.
!    - correct false or ambiguous comments.
!   Modified 01-08-30 by K. YESSAD: pruning and some other cleanings.
!   J.Vivoda (03-2002) - rationalization, PC schemes for NH dynamics
!                        (LPC_XXXX keys)
!                      - introduction of LATTEX_DNT and LATTEX_TNT routines
!   Modified 02-09-30 P. Smolikova : variable d4 in NH
!   Modified 08-2002 C. Smith : use "w" as prognostic variable in the
!    semi-lag advection of vertical divergence in the NH model.
!   Modified 03-01-31 P. Smolikova : variable d4 in 2tl PC NH
!   A. Untch 01-08-10: transforms to b-spline space for cubic spline 
!                      interpolation in the vertical
!   Modified 2003-03-16 M.Hamrud/M.Hortal - Revised data flow (GFL)
!   01-Oct-2003 M. Hamrud  CY28 Cleaning
!   M.Hortal 01-Dec-2003 Introduce the possibility of extra-fields coming
!    from the dynamics
!   09-Jun-2004 J. Masek   NH cleaning (LPC_NOTR, LFULLIMP)
!   01-Jul-2004 K. Yessad  Make clearer the tests for PC scheme + optimization.
!   A.Untch June 2005: Rayleigh friction
!   12-Jan-2005 K. Yessad  Move calc. of (ZXM,ZX0,PNHXT9) from LACDYN to LATTEX
!   23-Feb-2005 J. Vivoda  GWADV scheme for 2TL PC NESC scheme
!   11-Jul-2005 R. Brozkova cleaning d4 subvariants
!   14-Oct-2005 Y. Bouteloup SL computation of CVGQ for French physics
!   K. Yessad 07-02-2007: Splitting alti/surf for (gw) in NH+LGWADV.
!   K. Yessad 07-03-2007: Remove useless (gw)_surf interpolations in NH+LGWADV.
!   K. Yessad 22-08-2007: Simplify data flux for NVDVAR=4
!   K. Yessad 22-08-2007: Bug correction (NH, NVDVAR=4, LSETTLS, corrector).
!   K. Yessad 09-10-2007: NH: fix calculation of ZMOY1T for VFE.
!   N. Wedi and K. Yessad (Nov 2007): ND4SYS=2 to improve NH model stability.
!   N. Wedi and K. Yessad (Jan 2008): different dev for NH model and PC scheme
!   K. Yessad Aug 2008: rationalisation of dummy argument interfaces
!   P. Bechtold+A. Untch 26-10-2008: add LEGWWMS switch for non-orogr. GWD
! End Modifications
!------------------------------------------------------------------------------

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

USE YOMCST   , ONLY : RD
USE YOMCT0   , ONLY : LPC_FULL ,LPC_NESC ,LPC_CHEAP,LRFRIC   ,LVERCOR  , &
 & LNHDYN   ,LTWOTL
USE YOMCT3   , ONLY : NSTEP
USE YOMCVER  , ONLY : LVERTFE
USE YOMDIM   , ONLY : NPROMA   ,NPROMVC  ,NPROMDLW ,NPROMDLR ,NPROMNH  , &
 & NFLEVG   ,NFLSA    ,NFLEN
USE YOMDYN   , ONLY : NWLAG    ,NTLAG    ,NSPDLAG  ,NSVDLAG  ,NCURRENT_ITER, &
 & RKRF     ,LSETTLS  ,LADVF    ,LADVFW   ,RCMSLP0  ,RCORDIH  ,RCORDIF
USE YOMDYNA  , ONLY : LGWADV   ,NPDVAR   ,NVDVAR   ,ND4SYS   ,LNH_PDVD , &
 & LNH_GEOGW,LRWSDLW  ,LRWSDLR
USE YOMGEM   , ONLY : VDELB    ,VBH      ,VBF 
USE YOMGMV   , ONLY : NDIMGMV  ,YT0      ,YT1      ,YT9
USE YOMSC2   , ONLY : NFLDSLB1 ,NFLDSLB2
USE YOMSTA   , ONLY : RTSUR
USE YOM_YGFL , ONLY : YGFL     ,YGFLC,YCVGQ,YQ
USE PTRSLB1  , ONLY : MSLB1U0  ,MSLB1V0  ,MSLB1T0  ,MSLB1PD0 ,MSLB1VD0 , &
 & MSLB1U9  ,MSLB1V9  ,MSLB1T9  ,MSLB1GFL9,MSLB1GFLSP9,MSLB1PD9,MSLB1VD9, &
 & MSLB1NHX9,MSLB1RSSA9
USE PTRSLB2  , ONLY : MSLB2USI ,MSLB2VSI ,MSLB2TSI ,MSLB2PDSI,MSLB2VDSI
USE PAR_RDLR , ONLY : JP_ASRF  ,JP_RSAF  ,JP_DIMR0 ,JP_DIMR9
USE YOEPHY   , ONLY : LEGWWMS

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

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KST 
INTEGER(KIND=JPIM),INTENT(IN)    :: KPROF 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDTS2 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PBT 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PBDT(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PESGP 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PESGM 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGNORDL(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGNORDM(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRCORI(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRCORIC(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: POMVRL(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: POMVRM(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: POROG(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: POROGL(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: POROGM(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVCRSSA0F(NPROMVC,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVCASRS0F(NPROMVC,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVCRSSA9F(NPROMVC,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRDLR0(NPROMDLR,0:NFLEVG,JP_DIMR0)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRDLR9(NPROMDLR,0:NFLEVG,JP_DIMR9)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVCW0F(NPROMVC,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PWDLW0F(NPROMDLW,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGAGT0L(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGAGT0M(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTOD0(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSPDS0(NPROMNH,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PLPD0(NPROMNH,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDPD0(NPROMNH,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGAGT9L(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGAGT9M(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTOD9(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSPDS9(NPROMNH,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PLPD9(NPROMNH,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDPD9(NPROMNH,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSGRTL(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSGRTM(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PKAP(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVVEL(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRDELP(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PEVEL(NPROMA,0:NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: P3DIVG(NPROMNH,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTNDPD(NPROMNH,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTNDVD(NPROMNH,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTNDGW(NPROMNH,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PNHXT0(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGWT0(NPROMNH,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PNHXT9(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGWT9(NPROMNH,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGFL(NPROMA,NFLEVG,YGFL%NDIM) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PGMV(NPROMA,NFLEVG,NDIMGMV)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PGMVT1(NPROMA,NFLEVG,YT1%NDIM)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PB1(NPROMA,NFLDSLB1)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PB2(NPROMA,NFLDSLB2)

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

REAL(KIND=JPRB)               :: ZWT0(NPROMA)
REAL(KIND=JPRB) , ALLOCATABLE :: ZUSI9(:,:)
REAL(KIND=JPRB) , ALLOCATABLE :: ZVSI9(:,:)
REAL(KIND=JPRB) , ALLOCATABLE :: ZTSI9(:,:)
REAL(KIND=JPRB) , ALLOCATABLE :: ZSPDSI9(:,:)
REAL(KIND=JPRB) , ALLOCATABLE :: ZVWVSI9(:,:)
REAL(KIND=JPRB)               :: ZMBF(NFLEVG)
REAL(KIND=JPRB)               :: ZMDELB(NFLEVG)
REAL(KIND=JPRB)               :: ZMOY1U(NPROMA,NFLEVG)
REAL(KIND=JPRB)               :: ZMOY1V(NPROMA,NFLEVG)
REAL(KIND=JPRB)               :: ZMOY1T(NPROMA,NFLEVG)
REAL(KIND=JPRB)               :: ZMOY1SPD(NPROMNH,NFLEVG)
REAL(KIND=JPRB)               :: ZMOY1VWV(NPROMNH,NFLEVG)

INTEGER(KIND=JPIM) :: IPX, IPXSP, IPQ
INTEGER(KIND=JPIM) :: JLEV, JGFL, JROF

LOGICAL :: LLSETTLSW, LLCT, LLCTC

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

REAL(KIND=JPRB) :: ZCMSLP
REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

#include "abor1.intfb.h"
#include "lattex_dnt.intfb.h"
#include "lattex_tnt.intfb.h"
#include "vspltrans.intfb.h"

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

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

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

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

!       1.1  Allocate arrays for 3TL scheme:

IF(.NOT.LTWOTL)THEN
  ALLOCATE( ZUSI9(NPROMA,NFLEVG))
  ALLOCATE( ZVSI9(NPROMA,NFLEVG))
  ALLOCATE( ZTSI9(NPROMA,NFLEVG))
  IF( LNHDYN )THEN
    ALLOCATE( ZSPDSI9(NPROMNH,NFLEVG))
    ALLOCATE( ZVWVSI9(NPROMNH,NFLEVG))
  ENDIF
ENDIF

!       1.2  Scalar initialisations:

LLCT =LPC_FULL .AND. NCURRENT_ITER > 0  ! corrector step
LLCTC=LPC_CHEAP .AND. NCURRENT_ITER > 0

ZCMSLP=RCMSLP0/(RD*RTSUR)

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

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

!*       2.1   Momentum equation.

! * LSETTLS is replaced by LLSETTLSW=.FALSE. for wind-eqn if VESL>0 because
!   stable extrapolation deteriorates scores without improving stability.

IF (PESGP > PESGM) THEN
  LLSETTLSW=.FALSE.
ELSE
  LLSETTLSW=LSETTLS
ENDIF

DO JLEV=1,NFLEVG

  DO JROF=KST,KPROF
    PB2(JROF,MSLB2USI+JLEV-1)=PBT*PGAGT0L(JROF,JLEV)
    PB2(JROF,MSLB2VSI+JLEV-1)=PBT*PGAGT0M(JROF,JLEV)
  ENDDO
  ! * Add pressure gradient term in the wind equation.
  !   Convert semi-reduced pressure gradient term into geographical pressure
  !   gradient term before adding by multiplying it by "a/rs".
  IF (LVERCOR) THEN
    DO JROF=KST,KPROF
      ZMOY1U(JROF,JLEV) = -PDTS2*PVCASRS0F(JROF,JLEV)*PSGRTL(JROF,JLEV)
      ZMOY1V(JROF,JLEV) = -PDTS2*PVCASRS0F(JROF,JLEV)*PSGRTM(JROF,JLEV)
    ENDDO
  ELSEIF (LRWSDLR) THEN
    DO JROF=KST,KPROF
      ZMOY1U(JROF,JLEV) = -PDTS2*PRDLR0(JROF,JLEV,JP_ASRF)*PSGRTL(JROF,JLEV)
      ZMOY1V(JROF,JLEV) = -PDTS2*PRDLR0(JROF,JLEV,JP_ASRF)*PSGRTM(JROF,JLEV)
    ENDDO
  ELSE
    DO JROF=KST,KPROF
      ZMOY1U(JROF,JLEV) = -PDTS2*PSGRTL(JROF,JLEV)
      ZMOY1V(JROF,JLEV) = -PDTS2*PSGRTM(JROF,JLEV)
    ENDDO
  ENDIF
  ! * Add Rayleigh friction
  IF(LRFRIC .AND. .NOT.LEGWWMS) THEN
    DO JROF=KST,KPROF
      ZMOY1U(JROF,JLEV) = ZMOY1U(JROF,JLEV) &
       & -PDTS2*RKRF(JLEV)*PGMV(JROF,JLEV,YT0%MU)
    ENDDO
  ENDIF
  ! * Add "- 2 Omega vec V" explicit contribution when required.
  IF (.NOT.LADVF) THEN
    DO JROF=KST,KPROF
      ZMOY1U(JROF,JLEV)=ZMOY1U(JROF,JLEV) &
       & +PDTS2*PRCORI(JROF)*PGMV(JROF,JLEV,YT0%MV)
      ZMOY1V(JROF,JLEV)=ZMOY1V(JROF,JLEV) &
       & -PDTS2*PRCORI(JROF)*PGMV(JROF,JLEV,YT0%MU)
    ENDDO
  ENDIF
  ! * Add "- 2 Omega vec W k" explicit contribution when required.
  IF (LVERCOR.AND.(.NOT.LADVFW)) THEN
    DO JROF=KST,KPROF
      ZMOY1U(JROF,JLEV)=ZMOY1U(JROF,JLEV)-PDTS2* &
       & PGNORDM(JROF)*PRCORIC(JROF)*PVCW0F(JROF,JLEV)
      ZMOY1V(JROF,JLEV)=ZMOY1V(JROF,JLEV)+PDTS2* &
       & PGNORDL(JROF)*PRCORIC(JROF)*PVCW0F(JROF,JLEV)
    ENDDO
  ELSEIF (LRWSDLW.AND.(.NOT.LADVFW)) THEN
    DO JROF=KST,KPROF
      ZMOY1U(JROF,JLEV)=ZMOY1U(JROF,JLEV)-PDTS2* &
       & PGNORDM(JROF)*PRCORIC(JROF)*PWDLW0F(JROF,JLEV)
      ZMOY1V(JROF,JLEV)=ZMOY1V(JROF,JLEV)+PDTS2* &
       & PGNORDL(JROF)*PRCORIC(JROF)*PWDLW0F(JROF,JLEV)
    ENDDO
  ENDIF
  ! * Add "- 2 Omega vec (1-a/rs) V" residual explicit contrib when required.
  IF (LVERCOR.AND.LADVF.AND.(.NOT.LADVFW)) THEN
    DO JROF=KST,KPROF
      ZMOY1U(JROF,JLEV)=ZMOY1U(JROF,JLEV)+PDTS2* &
       & PRCORI(JROF)*PGMV(JROF,JLEV,YT0%MV)*(1.0_JPRB-PVCASRS0F(JROF,JLEV))
      ZMOY1V(JROF,JLEV)=ZMOY1V(JROF,JLEV)-PDTS2* &
       & PRCORI(JROF)*PGMV(JROF,JLEV,YT0%MU)*(1.0_JPRB-PVCASRS0F(JROF,JLEV))
    ENDDO
  ENDIF
  ! * Add "- 2 Omega vec (1-a/r) V" residual explicit contrib when required.
  IF (LRWSDLW.AND.LRWSDLR.AND.LADVF.AND.(.NOT.LADVFW)) THEN
    DO JROF=KST,KPROF
      ZMOY1U(JROF,JLEV)=ZMOY1U(JROF,JLEV) &
       & +PDTS2*PRCORI(JROF)*PGMV(JROF,JLEV,YT0%MV) &
       & *(1.0_JPRB-PRDLR0(JROF,JLEV,JP_ASRF))
      ZMOY1V(JROF,JLEV)=ZMOY1V(JROF,JLEV) &
       & -PDTS2*PRCORI(JROF)*PGMV(JROF,JLEV,YT0%MU) &
       & *(1.0_JPRB-PRDLR0(JROF,JLEV,JP_ASRF))
    ENDDO
  ENDIF
  ! * Add "- 2 Omega vec (a/rs-1) V" residual explicit contrib when required.
  IF (LVERCOR.AND.(.NOT.LADVF).AND.LADVFW) THEN
    DO JROF=KST,KPROF
      ZMOY1U(JROF,JLEV)=ZMOY1U(JROF,JLEV)+PDTS2* &
       & PRCORI(JROF)*PGMV(JROF,JLEV,YT0%MV)*(PVCASRS0F(JROF,JLEV)-1.0_JPRB)
      ZMOY1V(JROF,JLEV)=ZMOY1V(JROF,JLEV)-PDTS2* &
       & PRCORI(JROF)*PGMV(JROF,JLEV,YT0%MU)*(PVCASRS0F(JROF,JLEV)-1.0_JPRB)
    ENDDO
  ENDIF
  ! * Add "- 2 Omega vec (a/r-1) V" residual explicit contrib when required.
  IF (LRWSDLW.AND.LRWSDLR.AND.(.NOT.LADVF).AND.LADVFW) THEN
    DO JROF=KST,KPROF
      ZMOY1U(JROF,JLEV)=ZMOY1U(JROF,JLEV) &
       & +PDTS2*PRCORI(JROF)*PGMV(JROF,JLEV,YT0%MV) &
       & *(PRDLR0(JROF,JLEV,JP_ASRF)-1.0_JPRB)
      ZMOY1V(JROF,JLEV)=ZMOY1V(JROF,JLEV) &
       & -PDTS2*PRCORI(JROF)*PGMV(JROF,JLEV,YT0%MU) &
       & *(PRDLR0(JROF,JLEV,JP_ASRF)-1.0_JPRB)
    ENDDO
  ENDIF

ENDDO

IF (LTWOTL) THEN
  
  CALL LATTEX_DNT(KST,KPROF,LLSETTLSW,NWLAG,PESGP,PESGM,&
   & PGMV(1,1,YT0%MU),PGMV(1,1,YT9%MU),ZMOY1U,&
   & PB2(1,MSLB2USI),PGMV(1,1,YT9%MUNL),PGMVT1(1,1,YT1%MU),&
   & PB1(1,MSLB1U0),PB1(1,MSLB1U9),PGMV(1,1,YT9%MCUNL))
  
  CALL LATTEX_DNT(KST,KPROF,LLSETTLSW,NWLAG,PESGP,PESGM,&
   & PGMV(1,1,YT0%MV),PGMV(1,1,YT9%MV),ZMOY1V, &
   & PB2(1,MSLB2VSI),PGMV(1,1,YT9%MVNL),PGMVT1(1,1,YT1%MV),&
   & PB1(1,MSLB1V0),PB1(1,MSLB1V9),PGMV(1,1,YT9%MCVNL))
  
  IF(LVERCOR.AND.LADVFW.AND.(.NOT.LLCTC)) THEN
    ! need to interpolate "rs/a" (instant t) at O.
    DO JLEV=1,NFLEVG
      DO JROF=KST,KPROF
        PB1(JROF,MSLB1RSSA9+JLEV-NFLSA)=PB1(JROF,MSLB1RSSA9+JLEV-NFLSA) &
         & +PVCRSSA0F(JROF,JLEV)
      ENDDO
    ENDDO
  ENDIF
  IF (LRWSDLW.AND.LRWSDLR.AND.LADVFW.AND.(.NOT.LLCTC)) THEN
    ! need to interpolate "r/a" (instant t) at O.
    DO JLEV=1,NFLEVG
      DO JROF=KST,KPROF
        PB1(JROF,MSLB1RSSA9+JLEV-NFLSA)=PB1(JROF,MSLB1RSSA9+JLEV-NFLSA) &
         & +PRDLR0(JROF,JLEV,JP_RSAF)
      ENDDO
    ENDDO
  ENDIF
  
ELSE
  
  DO JLEV=1,NFLEVG
    DO JROF=KST,KPROF
      ZUSI9(JROF,JLEV)=PBT*PGAGT9L(JROF,JLEV)
      ZVSI9(JROF,JLEV)=PBT*PGAGT9M(JROF,JLEV)
    ENDDO
  ENDDO
  
  CALL LATTEX_TNT(KST,KPROF,NWLAG,PESGP,PESGM,PGMV(1,1,YT9%MU),ZMOY1U,ZUSI9,&
   & PB2(1,MSLB2USI),PGMVT1(1,1,YT1%MU),PB1(1,MSLB1U0),PB1(1,MSLB1U9))
  CALL LATTEX_TNT(KST,KPROF,NWLAG,PESGP,PESGM,PGMV(1,1,YT9%MV),ZMOY1V,ZVSI9,&
   & PB2(1,MSLB2VSI),PGMVT1(1,1,YT1%MV),PB1(1,MSLB1V0),PB1(1,MSLB1V9))
  
  IF(LVERCOR.AND.LADVFW) THEN
    ! need to interpolate "rs/a" (instant t-dt) at O.
    DO JLEV=1,NFLEVG
      DO JROF=KST,KPROF
        PB1(JROF,MSLB1RSSA9+JLEV-NFLSA)=PB1(JROF,MSLB1RSSA9+JLEV-NFLSA) &
         & +PVCRSSA9F(JROF,JLEV)
      ENDDO
    ENDDO
  ENDIF
  IF (LRWSDLW.AND.LRWSDLR.AND.LADVFW.AND.(.NOT.LLCTC)) THEN
    ! need to interpolate "r/a" (instant t-dt) at O.
    DO JLEV=1,NFLEVG
      DO JROF=KST,KPROF
        PB1(JROF,MSLB1RSSA9+JLEV-NFLSA)=PB1(JROF,MSLB1RSSA9+JLEV-NFLSA) &
         & +PRDLR9(JROF,JLEV,JP_RSAF)
      ENDDO
    ENDDO
  ENDIF

ENDIF
  
IF(LADVF.AND.(.NOT.LADVFW)) THEN
  DO JLEV=1,NFLEVG
    DO JROF=KST,KPROF
      PGMVT1(JROF,JLEV,YT1%MU)=PGMVT1(JROF,JLEV,YT1%MU)-POMVRL(JROF)
      PGMVT1(JROF,JLEV,YT1%MV)=PGMVT1(JROF,JLEV,YT1%MV)-POMVRM(JROF)
    ENDDO
  ENDDO
ENDIF

!*       2.2   Temperature equation.

DO JLEV=1,NFLEVG

  DO JROF=KST,KPROF
    PB2(JROF,MSLB2TSI+JLEV-1)=PBDT(JROF)*PTOD0(JROF,JLEV)
  ENDDO

  ! * compute ZMOY1T
  IF(LVERTFE) THEN
    DO JROF=KST,KPROF
      ZWT0(JROF)=PEVEL(JROF,JLEV)*PRDELP(JROF,JLEV)
    ENDDO
  ELSE
    DO JROF=KST,KPROF
      ZWT0(JROF)=0.5_JPRB*(PEVEL(JROF,JLEV)&
       & +PEVEL(JROF,JLEV-1))*PRDELP(JROF,JLEV)
    ENDDO
  ENDIF
  DO JROF=KST,KPROF
    ZMOY1T(JROF,JLEV)=&
     & PDTS2*ZCMSLP*RCORDIF(JLEV)*PGMV(JROF,JLEV,YT0%MU)*POROGL(JROF)&
     & +PDTS2*ZCMSLP*RCORDIF(JLEV)*PGMV(JROF,JLEV,YT0%MV)*POROGM(JROF)
  ENDDO
  IF(LVERCOR) THEN
    ! take account of the fact that (POROGL,POROGM) contain (rs/a)*nabla(orog)
    ! and that we need to use nabla(orog).
    DO JROF=KST,KPROF
      ZMOY1T(JROF,JLEV)=ZMOY1T(JROF,JLEV)*PVCASRS0F(JROF,JLEV)
    ENDDO
  ENDIF
  IF(LRWSDLR) THEN
    ! take account of the fact that (POROGL,POROGM) contain (r/a)*nabla(orog)
    ! and that we need to use nabla(orog).
    DO JROF=KST,KPROF
      ZMOY1T(JROF,JLEV)=ZMOY1T(JROF,JLEV)*PRDLR0(JROF,JLEV,JP_ASRF)
    ENDDO
  ENDIF
  IF (LNHDYN) THEN
    DO JROF=KST,KPROF
      ZMOY1T(JROF,JLEV)=ZMOY1T(JROF,JLEV)&
       & +PDTS2*(1.0_JPRB-( 1.0_JPRB/(1.0_JPRB-PKAP(JROF,JLEV)) ))&
       & *PGMV(JROF,JLEV,YT0%MT)*P3DIVG(JROF,JLEV) &
       & +PDTS2*ZCMSLP*(RCORDIH(JLEV)-RCORDIH(JLEV-1))*ZWT0(JROF)*POROG(JROF)
    ENDDO
  ELSE
    DO JROF=KST,KPROF
      ZMOY1T(JROF,JLEV)=ZMOY1T(JROF,JLEV)&
       & +PDTS2*PKAP(JROF,JLEV)*PGMV(JROF,JLEV,YT0%MT)*PVVEL(JROF,JLEV)&
       & +PDTS2*ZCMSLP*(RCORDIH(JLEV)-RCORDIH(JLEV-1))*ZWT0(JROF)*POROG(JROF)
    ENDDO
  ENDIF

ENDDO

IF (LTWOTL) THEN
  
  CALL LATTEX_DNT(KST,KPROF,LSETTLS,NTLAG,PESGP,PESGM, &
   & PGMV(1,1,YT0%MT),PGMV(1,1,YT9%MT),ZMOY1T,&
   & PB2(1,MSLB2TSI),PGMV(1,1,YT9%MTNL),PGMVT1(1,1,YT1%MT), &
   & PB1(1,MSLB1T0),PB1(1,MSLB1T9),PGMV(1,1,YT9%MCTNL))
  
ELSE
  
  DO JLEV=1,NFLEVG
    DO JROF=KST,KPROF
      ZTSI9(JROF,JLEV)=PBDT(JROF)*PTOD9(JROF,JLEV)
    ENDDO
  ENDDO
  
  CALL LATTEX_TNT(KST,KPROF,NTLAG,PESGP,PESGM,PGMV(1,1,YT9%MT),ZMOY1T,ZTSI9,&
   & PB2(1,MSLB2TSI),PGMVT1(1,1,YT1%MT),PB1(1,MSLB1T0),PB1(1,MSLB1T9))
  
ENDIF

DO JLEV=1,NFLEVG
  DO JROF=KST,KPROF
    PGMVT1(JROF,JLEV,YT1%MT)=PGMVT1(JROF,JLEV,YT1%MT) &
     & -RCORDIF(JLEV)*ZCMSLP*POROG(JROF)
  ENDDO
  IF (.NOT.LLCTC) THEN
    DO JROF=KST,KPROF
      PB1(JROF,MSLB1T9+JLEV-NFLSA)=PB1(JROF,MSLB1T9+JLEV-NFLSA) &
       & +RCORDIF(JLEV)*ZCMSLP*POROG(JROF)
    ENDDO
  ENDIF
ENDDO

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

IF (LNHDYN) THEN

  DO JLEV=1,NFLEVG

    DO JROF=KST,KPROF
      PB2(JROF,MSLB2PDSI+JLEV-1)=PBDT(JROF)*PSPDS0(JROF,JLEV)
      ZMOY1SPD(JROF,JLEV)=PDTS2*PTNDPD(JROF,JLEV)
    ENDDO

    IF (LNH_GEOGW .AND. NPDVAR==4) THEN
      ! Apply a Tanguay-Ritchie correction like we do for temperature
      !  equation: advect (Phi - B Phi_surf) instead of Phi in order
      !  to advect a variable less sensitive to orography.
      IF(LVERTFE) THEN
        DO JROF=KST,KPROF
          ZWT0(JROF)=PEVEL(JROF,JLEV)*PRDELP(JROF,JLEV)
        ENDDO
        ZMBF(JLEV)=-VBF(JLEV)
      ELSE
        DO JROF=KST,KPROF
          ZWT0(JROF)=0.5_JPRB*(PEVEL(JROF,JLEV)&
           & +PEVEL(JROF,JLEV-1))*PRDELP(JROF,JLEV)
        ENDDO
        ZMBF(JLEV)=-0.5_JPRB*(VBH(JLEV)+VBH(JLEV-1))
      ENDIF
      ZMDELB(JLEV)=-VDELB(JLEV)
      ! Probably something to add later here for deep-layer equations.
      DO JROF=KST,KPROF
        ZMOY1SPD(JROF,JLEV)=ZMOY1SPD(JROF,JLEV)+&
         & PDTS2*ZCMSLP*ZMBF(JLEV)*PGMV(JROF,JLEV,YT0%MU)*POROGL(JROF)&
         & +PDTS2*ZCMSLP*ZMBF(JLEV)*PGMV(JROF,JLEV,YT0%MV)*POROGM(JROF)
      ENDDO
      DO JROF=KST,KPROF
        ZMOY1SPD(JROF,JLEV)=ZMOY1SPD(JROF,JLEV) &
         & +PDTS2*ZCMSLP*ZMDELB(JLEV)*ZWT0(JROF)*POROG(JROF)
      ENDDO
    ENDIF

  ENDDO

  IF (LTWOTL) THEN
  
    CALL LATTEX_DNT(KST,KPROF,LSETTLS,NSPDLAG,PESGP,PESGM,&
     & PGMV(1,1,YT0%MSPD),PGMV(1,1,YT9%MSPD),ZMOY1SPD,&
     & PB2(1,MSLB2PDSI),PGMV(1,1,YT9%MSPDNL),PGMVT1(1,1,YT1%MSPD), &
     & PB1(1,MSLB1PD0),PB1(1,MSLB1PD9),PGMV(1,1,YT9%MCSPDNL))
  ELSE
  
    DO JLEV=1,NFLEVG
      DO JROF=KST,KPROF
        ZSPDSI9(JROF,JLEV)=PBDT(JROF)*PSPDS9(JROF,JLEV)
      ENDDO
    ENDDO
  
    CALL LATTEX_TNT(KST,KPROF,NSPDLAG,PESGP,PESGM,PGMV(1,1,YT9%MSPD),&
     & ZMOY1SPD,ZSPDSI9,&
     & PB2(1,MSLB2PDSI),PGMVT1(1,1,YT1%MSPD),PB1(1,MSLB1PD0),PB1(1,MSLB1PD9))
  ENDIF

  IF (LNH_GEOGW .AND. NPDVAR==4) THEN
    ! Tanguay-Ritchie correction (cont'd). 
    DO JLEV=1,NFLEVG
      DO JROF=KST,KPROF
        PGMVT1(JROF,JLEV,YT1%MSPD)=PGMVT1(JROF,JLEV,YT1%MSPD) &
         & -ZMBF(JLEV)*ZCMSLP*POROG(JROF)
      ENDDO
      IF (.NOT. LLCTC) THEN
        DO JROF=KST,KPROF
          PB1(JROF,MSLB1PD9+JLEV-NFLSA)=PB1(JROF,MSLB1PD9+JLEV-NFLSA) &
           & +ZMBF(JLEV)*ZCMSLP*POROG(JROF)
        ENDDO
      ENDIF
    ENDDO
  ENDIF

ENDIF

!*       2.4a  Anhydrostatic variables equations:
!              "vertical divergence" if LNH_PDVD.
!              "gw" if LNH_GEOGW.

IF (LNHDYN.AND.( (LNH_PDVD.AND.(.NOT.LGWADV)).OR.LNH_GEOGW )) THEN

  IF ((NVDVAR==5 .OR.NVDVAR==6).AND.(.NOT.LVERTFE)) THEN
    ! not coded (the following code only works if prognostic variable
    ! is at full levels).
    CALL ABOR1('LATTEX: NVDVAR==5 or 6 .AND. (.NOT.LVERTFE) not yet coded')
  ENDIF

  DO JLEV=1,NFLEVG
    DO JROF=KST,KPROF
      PB2(JROF,MSLB2VDSI+JLEV-1)=PBT*PLPD0(JROF,JLEV)
      ZMOY1VWV(JROF,JLEV)=PDTS2*PTNDVD(JROF,JLEV)
    ENDDO
  ENDDO

  IF (LTWOTL) THEN

    CALL LATTEX_DNT(KST,KPROF,LSETTLS,NSVDLAG,PESGP,PESGM,&
     & PGMV(1,1,YT0%MSVD),PGMV(1,1,YT9%MSVD),ZMOY1VWV,&
     & PB2(1,MSLB2VDSI),PGMV(1,1,YT9%MVWVNL),PGMVT1(1,1,YT1%MSVD),&
     & PB1(1,MSLB1VD0),PB1(1,MSLB1VD9),PGMV(1,1,YT9%MCVWVNL))

    IF (NVDVAR==4 .AND. ND4SYS==1) THEN

      IF (.NOT.LLCT) THEN
        ! * predictor step for LPC_FULL or normal SI step.
        IF((LPC_FULL.AND.LPC_NESC).OR.(NSTEP <= 0)) THEN
          ! * LPC_NESC=T predictor or NSTEP<=0.
          DO JLEV=1,NFLEVG
            DO JROF=KST,KPROF
              PGMVT1(JROF,JLEV,YT1%MSVD)=PGMVT1(JROF,JLEV,YT1%MSVD) &
               & +PNHXT0(JROF,JLEV)
              PGMVT1(JROF,JLEV,YT1%MNHX)=PNHXT0(JROF,JLEV)
              PB1(JROF,MSLB1VD9+JLEV-NFLSA)=PB1(JROF,MSLB1VD9+JLEV-NFLSA) &
               & -PNHXT0(JROF,JLEV)
              PB1(JROF,MSLB1NHX9+JLEV-NFLSA)=0.0_JPRB
            ENDDO
          ENDDO
        ELSEIF (.NOT.(LPC_FULL.AND.LPC_NESC).AND.(NSTEP > 0).AND.LSETTLS) THEN
          ! * LSETTLS=T predictor if NSTEP>0.
          DO JLEV=1,NFLEVG
            DO JROF=KST,KPROF
              PGMVT1(JROF,JLEV,YT1%MSVD)=PGMVT1(JROF,JLEV,YT1%MSVD) &
               & +PNHXT0(JROF,JLEV)
              PGMVT1(JROF,JLEV,YT1%MNHX)=PNHXT0(JROF,JLEV)
              PB1(JROF,MSLB1VD9+JLEV-NFLSA)=PB1(JROF,MSLB1VD9+JLEV-NFLSA) &
               & -PGMV(JROF,JLEV,YT9%MNHX)
              PB1(JROF,MSLB1NHX9+JLEV-NFLSA)=PNHXT0(JROF,JLEV) &
               & -PGMV(JROF,JLEV,YT9%MNHX)
            ENDDO
          ENDDO
        ELSE
          ! * predictor (LPC_FULL.AND.LPC_NESC)=F, NSTEP>0, LSETTLS=F.
          DO JLEV=1,NFLEVG
            DO JROF=KST,KPROF
              PGMVT1(JROF,JLEV,YT1%MSVD)=PGMVT1(JROF,JLEV,YT1%MSVD) &
               & +1.5_JPRB*PNHXT0(JROF,JLEV)-0.5_JPRB*PGMV(JROF,JLEV,YT9%MNHX)
              PGMVT1(JROF,JLEV,YT1%MNHX)= &
               & 1.5_JPRB*PNHXT0(JROF,JLEV)-0.5_JPRB*PGMV(JROF,JLEV,YT9%MNHX)
              PB1(JROF,MSLB1VD9+JLEV-NFLSA)=PB1(JROF,MSLB1VD9+JLEV-NFLSA) &
               & -0.5_JPRB*PNHXT0(JROF,JLEV)-0.5_JPRB*PGMV(JROF,JLEV,YT9%MNHX)
              PB1(JROF,MSLB1NHX9+JLEV-NFLSA)= &
               & 0.5_JPRB*(PNHXT0(JROF,JLEV)-PGMV(JROF,JLEV,YT9%MNHX))
            ENDDO
          ENDDO
        ENDIF
        PGMV(KST:KPROF,1:NFLEVG,YT9%MNHX)=PNHXT0(KST:KPROF,1:NFLEVG)
      ELSE
        ! * corrector step for LPC_FULL.
        DO JLEV=1,NFLEVG
          DO JROF=KST,KPROF
            PGMVT1(JROF,JLEV,YT1%MSVD)=PGMVT1(JROF,JLEV,YT1%MSVD) &
             & +PNHXT0(JROF,JLEV)
            PGMVT1(JROF,JLEV,YT1%MNHX)=PNHXT0(JROF,JLEV)
          ENDDO
          IF (.NOT.LLCTC) THEN
            DO JROF=KST,KPROF
              PB1(JROF,MSLB1VD9+JLEV-NFLSA)=PB1(JROF,MSLB1VD9+JLEV-NFLSA) &
               & -PGMV(JROF,JLEV,YT9%MNHX)
              PB1(JROF,MSLB1NHX9+JLEV-NFLSA)=0.0_JPRB
            ENDDO
          ENDIF
        ENDDO
      ENDIF

    ELSEIF (NVDVAR==4 .AND. ND4SYS==2) THEN

      IF (.NOT.LLCT) THEN
        DO JLEV=1,NFLEVG
          DO JROF=KST,KPROF
            PGMVT1(JROF,JLEV,YT1%MSVD)=PGMVT1(JROF,JLEV,YT1%MSVD) &
             & +PNHXT0(JROF,JLEV)
            PGMVT1(JROF,JLEV,YT1%MNHX)=PNHXT0(JROF,JLEV)
            PB1(JROF,MSLB1VD9+JLEV-NFLSA)=PB1(JROF,MSLB1VD9+JLEV-NFLSA) &
             & -PNHXT0(JROF,JLEV)
            PB1(JROF,MSLB1NHX9+JLEV-NFLSA)=0.0_JPRB
          ENDDO
        ENDDO
        PGMV(KST:KPROF,1:NFLEVG,YT9%MNHX)=PNHXT0(KST:KPROF,1:NFLEVG)
      ELSE
        ! * corrector step for LPC_FULL (in this case PNHXT9=PGMV(.,.,YT9%MNHX))
        DO JLEV=1,NFLEVG
          DO JROF=KST,KPROF
            PGMVT1(JROF,JLEV,YT1%MSVD)=PGMVT1(JROF,JLEV,YT1%MSVD) &
             & +PGMV(JROF,JLEV,YT9%MNHX)
            PGMVT1(JROF,JLEV,YT1%MNHX)=PNHXT0(JROF,JLEV)
          ENDDO
          IF (.NOT.LLCTC) THEN
            DO JROF=KST,KPROF
              PB1(JROF,MSLB1VD9+JLEV-NFLSA)=PB1(JROF,MSLB1VD9+JLEV-NFLSA) &
               & -PGMV(JROF,JLEV,YT9%MNHX)
              PB1(JROF,MSLB1NHX9+JLEV-NFLSA)=0.0_JPRB
            ENDDO
          ENDIF
        ENDDO
      ENDIF

    ENDIF

  ELSE
   
    DO JLEV=1,NFLEVG
      DO JROF=KST,KPROF
        ZVWVSI9(JROF,JLEV)=PBT*PLPD9(JROF,JLEV)
      ENDDO
    ENDDO
  
    CALL LATTEX_TNT(KST,KPROF,NSVDLAG,PESGP,PESGM,PGMV(1,1,YT9%MSVD),&
     & ZMOY1VWV,ZVWVSI9,&
     & PB2(1,MSLB2VDSI),PGMVT1(1,1,YT1%MSVD),PB1(1,MSLB1VD0),PB1(1,MSLB1VD9))

    ! LPC_FULL not yet coded, only the predictor is provided.
    IF (NVDVAR==4 .AND. ND4SYS==1) THEN
      DO JLEV=1,NFLEVG
        DO JROF=KST,KPROF
          PGMVT1(JROF,JLEV,YT1%MSVD)=PGMVT1(JROF,JLEV,YT1%MSVD) &
           & +PNHXT0(JROF,JLEV)
          PGMVT1(JROF,JLEV,YT1%MNHX)=PNHXT0(JROF,JLEV)
          PB1(JROF,MSLB1VD9+JLEV-NFLSA)=PB1(JROF,MSLB1VD9+JLEV-NFLSA) &
           & +PNHXT0(JROF,JLEV)-2.0_JPRB*PNHXT9(JROF,JLEV)
          PB1(JROF,MSLB1NHX9+JLEV-NFLSA)=0.0_JPRB 
        ENDDO
      ENDDO
    ELSEIF (NVDVAR==4 .AND. ND4SYS==2) THEN
      DO JLEV=1,NFLEVG
        DO JROF=KST,KPROF
          PGMVT1(JROF,JLEV,YT1%MSVD)=PGMVT1(JROF,JLEV,YT1%MSVD) &
           & +PNHXT9(JROF,JLEV)
          PGMVT1(JROF,JLEV,YT1%MNHX)=PNHXT0(JROF,JLEV)
          PB1(JROF,MSLB1VD9+JLEV-NFLSA)=PB1(JROF,MSLB1VD9+JLEV-NFLSA) &
           & -PNHXT9(JROF,JLEV)
          PB1(JROF,MSLB1NHX9+JLEV-NFLSA)=0.0_JPRB 
        ENDDO
      ENDDO
    ENDIF

  ENDIF

ENDIF

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

IF (LNHDYN.AND.(LNH_PDVD.AND.LGWADV)) THEN

  ! To avoid the addition of an extra global 3D array, PB2(.,MSLB2VDSI)
  ! initially holds the SI term for the gw equation. At the end of grid-point
  ! calculations it will be converted to the SI term for the "dver" equation.
  ! The SI stage then follows exactly as usual for the "dver" equation.

  DO JLEV=1,NFLEVG
    DO JROF=KST,KPROF
      PB2(JROF,MSLB2VDSI+JLEV-1)=PBT*PDPD0(JROF,JLEV)
      ZMOY1VWV(JROF,JLEV)=PDTS2*PTNDGW(JROF,JLEV)
    ENDDO
  ENDDO

  IF (LTWOTL) THEN

    ! store "gw(t)" into buffer for corrector step
    IF( LPC_FULL.AND.NCURRENT_ITER==0)THEN
      PGMV(KST:KPROF,1:NFLEVG,YT9%MGW)=PGWT0(KST:KPROF,1:NFLEVG)
    ENDIF

    CALL LATTEX_DNT(KST,KPROF,LSETTLS,NSVDLAG,PESGP,PESGM,&
     & PGWT0,PGMV(1,1,YT9%MGW),ZMOY1VWV,&
     & PB2(1,MSLB2VDSI),PGMV(1,1,YT9%MVWVNL),PGMVT1(1,1,YT1%MSVD),&
     & PB1(1,MSLB1VD0),PB1(1,MSLB1VD9),PGMV(1,1,YT9%MCVWVNL))

    IF (NVDVAR==4 .AND. ND4SYS==2) THEN
      IF (.NOT.LLCT) THEN
        DO JLEV=1,NFLEVG
          PB1(KST:KPROF,MSLB1NHX9+JLEV-NFLSA)=PNHXT0(KST:KPROF,JLEV)
        ENDDO
      ELSE
        DO JLEV=1,NFLEVG
          PB1(KST:KPROF,MSLB1NHX9+JLEV-NFLSA)=PGMV(KST:KPROF,JLEV,YT9%MNHX)
        ENDDO
      ENDIF
    ENDIF

    IF (NVDVAR==4 .AND. ND4SYS==2 .AND. .NOT.LLCT) THEN
      ! ky: this saving does not seem to be necessary for ND4SYS=1,
      !     but this must be checked.
      PGMV(KST:KPROF,1:NFLEVG,YT9%MNHX)=PNHXT0(KST:KPROF,1:NFLEVG)
    ENDIF

  ELSE

    ! This piece of code has not been validated
    !  LGWADV with SL3TL => ABOR1 in the setup, missing interpolations.

    DO JLEV=1,NFLEVG
      DO JROF=KST,KPROF
        ZVWVSI9(JROF,JLEV)=PBT*PDPD9(JROF,JLEV)
      ENDDO
    ENDDO

    CALL LATTEX_TNT(KST,KPROF,NSVDLAG,PESGP,PESGM,PGWT9,ZMOY1VWV,ZVWVSI9,&
     & PB2(1,MSLB2VDSI),PGMVT1(1,1,YT1%MSVD),PB1(1,MSLB1VD0),PB1(1,MSLB1VD9))

    ! LPC_FULL not yet coded, only the predictor is provided.
    IF (NVDVAR==4 .AND. ND4SYS==2) THEN
      DO JLEV=1,NFLEVG
        PB1(KST:KPROF,MSLB1NHX9+JLEV-NFLSA)=PNHXT9(KST:KPROF,JLEV)
      ENDDO
    ENDIF

  ENDIF

ENDIF

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

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

IF (LTWOTL) THEN
  
  IF(LLCT.AND.(.NOT.LLCTC))THEN
    DO JGFL=1,YGFL%NUMFLDS
      IF(YGFLC(JGFL)%LADV) THEN
        IPX=(YGFLC(JGFL)%MP_SL1-1)*(NFLEN-NFLSA+1)
        DO JLEV=1,NFLEVG
          DO JROF=KST,KPROF
            PB1(JROF,MSLB1GFL9+IPX+JLEV-NFLSA) = &
             & PB1(JROF,MSLB1GFL9+IPX+JLEV-NFLSA)&
             & +PGFL(JROF,JLEV,YGFLC(JGFL)%MP9)
          ENDDO
        ENDDO
      ENDIF
    ENDDO
  ELSEIF (.NOT.LLCT) THEN
    DO JGFL=1,YGFL%NUMFLDS
      IF(YGFLC(JGFL)%LADV) THEN
        IPX=(YGFLC(JGFL)%MP_SL1-1)*(NFLEN-NFLSA+1)
        DO JLEV=1,NFLEVG
          DO JROF=KST,KPROF
            PB1(JROF,MSLB1GFL9+IPX+JLEV-NFLSA) = &
             & PB1(JROF,MSLB1GFL9+IPX+JLEV-NFLSA)&
             & +PGFL(JROF,JLEV,YGFLC(JGFL)%MP)
          ENDDO
        ENDDO
      ENDIF
    ENDDO
  ENDIF


!  Qv in YCVGQ for computation of SL Moisture Convergence for French physics

  IF (YCVGQ%LGP .AND. (.NOT.LLCTC)) THEN  
    IPX=(YCVGQ%MP_SL1-1)*(NFLEN-NFLSA+1)
    IPQ=(YQ%MP_SL1-1)*(NFLEN-NFLSA+1)
    DO JLEV=1,NFLEVG
      DO JROF=KST,KPROF
        PB1(JROF,MSLB1GFL9+IPX+JLEV-NFLSA)=PB1(JROF,MSLB1GFL9+IPQ+JLEV-NFLSA)
      ENDDO
    ENDDO
  ENDIF   
  
ELSE
  
  DO JGFL=1,YGFL%NUMFLDS
    IF(YGFLC(JGFL)%LADV) THEN
      IPX=(YGFLC(JGFL)%MP_SL1-1)*(NFLEN-NFLSA+1)
      DO JLEV=1,NFLEVG
        DO JROF=KST,KPROF
          PB1(JROF,MSLB1GFL9+IPX+JLEV-NFLSA) = &
           & PB1(JROF,MSLB1GFL9+IPX+JLEV-NFLSA) &
           & +PGFL(JROF,JLEV,YGFLC(JGFL)%MP9)
        ENDDO
      ENDDO
    ENDIF
  ENDDO

ENDIF

! * Transform the fields to be interpolated with cubic spline in the
!   vertical to "B-spline space".

IF (.NOT.LLCTC) THEN
  DO JGFL=1,YGFL%NUMFLDS
    IF(YGFLC(JGFL)%LADV .AND. YGFLC(JGFL)%CSLINT=='LAITVSPCQM  ' ) THEN
      IPX=(YGFLC(JGFL)%MP_SL1-1)*(NFLEN-NFLSA+1)
      IPXSP=(YGFLC(JGFL)%MP_SPL-1)*(NFLEN-NFLSA+1)
      DO JLEV=1,NFLEVG
        DO JROF=KST,KPROF
          PB1(JROF,MSLB1GFLSP9+IPXSP+JLEV-NFLSA)&
           & =PB1(JROF,MSLB1GFL9+IPX+JLEV-NFLSA)
        ENDDO
      ENDDO
      CALL VSPLTRANS(NPROMA,KST,KPROF,NFLEVG,NFLSA,NFLEN,&
       & PB1(1,MSLB1GFLSP9+IPXSP))
    ENDIF
  ENDDO
ENDIF

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

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

IF(.NOT.LTWOTL)THEN
  IF( ALLOCATED( ZUSI9 ) ) DEALLOCATE( ZUSI9 )
  IF( ALLOCATED( ZVSI9 ) ) DEALLOCATE( ZVSI9 )
  IF( ALLOCATED( ZTSI9 ) ) DEALLOCATE( ZTSI9 )
  IF( LNHDYN )THEN
    IF( ALLOCATED( ZVWVSI9 ) ) DEALLOCATE( ZVWVSI9 )
    IF( ALLOCATED( ZSPDSI9 ) ) DEALLOCATE( ZSPDSI9 )
  ENDIF
ENDIF

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

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

