SUBROUTINE LATTES(KST,KPROF,PDTS2,PBDT,PESGP,PESGM,&
 & PVCRSSA0F,PVCASRS0F,PVCASRS0H,PRDLR0,POROG,POROGL,POROGM,&
 & PSDIV0,PSDIV9,PSDVBC,PRES0,PGMVS,&
 & PGMV,PGMVT1S,PB1,PB2)

!**** *LATTES*   Semi-Lagrangian scheme.
!                Computation of the t and t-dt useful quantities
!                 at grid-points. Equations for bi-dimensional
!                 variables (continuity equation).

!     Purpose.
!     --------
!        * This subroutine computes the equation quantities to be
!          interpolated at each grid-point of the colocation grid.
!          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 bi-dimensional
!          variables: continuity equation.
!        * 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 P[X]NLT9
!          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 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* *LATTES(..)

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

!        INPUT:
!          KST         - first element of work.
!          KPROF       - depth of work.
!          PDTS2       - 0.5*"pdt", where "pdt" =
!                        time step for the first time-integration step of
!                        a leap-frog scheme or all time-integration steps of
!                        a two-time level scheme; 2*time step for the following
!                        time-integration steps of a leap-frog scheme.
!          PBDT        - zbt if semi-implicit scheme with unreduced
!                        divergence, zbt*(c**2/GM**2) if semi-implicit
!                        scheme with reduced divergence, where zbt
!                        is equal to PDTS2*BETADT (BETADT is in YOMDYN) or zero
!                        according to the value of CDCONF.
!          PESGP       - (1 + uncentering factor).
!          PESGM       - (1 - uncentering factor).
!          PVCRSSA0F   - "rs/a" at full levels at t.
!          PVCASRS0F   - "a/rs" at full levels at t.
!          PVCASRS0H   - "a/rs" at half levels at t.
!          PRDLR0      - array containing "r/a", "a/r", "grad(r/a)" at t.
!          POROG       - surface orography.
!          POROGL      - zonal component of "rssavnabla(surf orography)"
!          POROGM      - meridian component of "rssavnabla(surf orography)"
!          PSDIV0      - SI term at time t for continuity equation (Nu*D).
!          PSDIV9      - SI term at time t-dt for continuity equation (Nu*D).
!          PSDVBC      - vertical integral of divergence computed in "gpcty",
!                        including the "lrubc" and "delta m=1" contributions
!                        of (etadot d prehyd/d eta).
!          PRES0       - hydrostatic pressure at half levels at t.
!          PGMVS       - GMVS variables at t-dt and t.

!        INPUT/OUTPUT:
!          PGMV        - GMV variables at t-dt and t.
!          PGMVT1S     - GMVS variables at t+dt.
!          PB1         - "SLBUF1" buffer for interpolations.
!          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 parts 3.3 et 3.4 
!        of LACDYN. 
!        DO loops have been updated according to F90 norms.

! Modifications
! -------------
!   Original : AUGUST 1995.
!   Clive Temperton 96-03-06 : removal of time-filtering in 2TL scheme
!   Clive Temperton 96-04-16 : "alternative averaging" in 2TL scheme
!   Modified OCT 1996 by K. YESSAD: removal of variable uncentering,
!     arrays PESGP, PESGM, PESGA, PESAP, PESAM, PESAA become scalar.
!   C. Temperton 97-02-24: modified first timestep when XIDT>0
!   D. Dvorak    98-03-12: radiative upper boundary condition ("lrubc").
!   K. YESSAD (July 1998): cleaning
!    - rewriting of option "lrubc" in a manner more consistent with
!      the writing of option "ndpsfi=1".
!    - removal of option NODEC=2
!    - 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.
!    - PVIDT0 renamed into PDIVT0.
!    - rewrite code in a more vectorisable way.
!   Modified 99-03-31 by P. SMOLIKOVA: option LRDAORG (double averaging).
!   C. Temperton 00-02-07: removed redundant use of PX0 when NVLAG=3
!   K. YESSAD (Nov 1999): cleaning
!    - PSDIV0 and PSDIV9 have now one dimension.
!    - replace "pevel" by "psdvbc" in "lattes" to avoid the re-computation
!      of a vertical integral of divergence already computed in "gpcty".
!    - recode SL3TL like SL2TL.
!   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.
!   R. El Khatib : 01-08-07 Pruning options
!   D.Salmond (15-10-01) FULLIMP mods
!   J.Vivoda (03-2002)
!    - rationalization, PC schemes for NH dynamics (LPC_XXXX keys)
!   12-Oct-2002 J. Masek   PC bugfix
!   01-Oct-2003 M. Hamrud  CY28 Cleaning
!   09-Jun-2004 J. Masek   NH cleaning (LPC_NOTR, LFULLIMP)
!   01-Jul-2004 K. Yessad  Make clearer the tests for PC scheme.
!   23-Oct-2007 K. Yessad  Cleanings + (hidden) bugs corrections.
!   N. Wedi and K. Yessad (Jan 2008): different dev for NH model and PC scheme
!   K. Yessad (Aug 2008): simplify XIDT treatment with PC + cleanings
!   K. Yessad Aug 2008: rationalisation of dummy argument interfaces
! End Modifications
!------------------------------------------------------------------------------

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

USE YOMCST   , ONLY : RD
USE YOMCT0   , ONLY : LVERCOR  ,LTWOTL   ,LPC_NESC ,LPC_FULL ,LPC_CHEAP
USE YOMCT3   , ONLY : NSTEP
USE YOMCVER  , ONLY : LVERTFE
USE YOMDIM   , ONLY : NPROMA   ,NPROMVC  ,NPROMDLR ,NFLEVG   ,NFLSA
USE YOMGEM   , ONLY : VFE_RDETAH,VDELB
USE YOMGMV   , ONLY : NDIMGMV  ,NDIMGMVS ,YT0      ,YT1      ,YT9
USE YOMDYNA  , ONLY : LRWSDLR
USE YOMDYN   , ONLY : NVLAG    ,XIDT     ,RCMSLP0  ,LSETTLS  ,LSLINLC1 , &
 & NCURRENT_ITER
USE YOMSC2   , ONLY : NFLDSLB1 ,NFLDSLB2
USE YOMSTA   , ONLY : RTSUR
USE PTRSLB1  , ONLY : MSLB1SP9 ,MSLB1C9  ,MSLB1C9_SI
USE PTRSLB2  , ONLY : MSLB2SPSI,MSLB2VCVWEI
USE PAR_RDLR , ONLY : JP_ASRF  ,JP_DIMR0

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

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)    :: PBDT(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PESGP 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PESGM 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVCRSSA0F(NPROMVC,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVCASRS0F(NPROMVC,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVCASRS0H(NPROMVC,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRDLR0(NPROMDLR,0:NFLEVG,JP_DIMR0)
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)    :: PSDIV0(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSDIV9(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSDVBC(NPROMA,0:NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRES0(NPROMA,0:NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGMVS(NPROMA,NDIMGMVS)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PGMV(NPROMA,NFLEVG,NDIMGMV)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PGMVT1S(NPROMA,YT1%NDIMS)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PB1(NPROMA,NFLDSLB1)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PB2(NPROMA,NFLDSLB2)

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

REAL(KIND=JPRB) :: ZMOY1SP(NPROMA,NFLEVG) 
REAL(KIND=JPRB) :: ZMOYSPNL(NPROMA),ZMOYSPNL_SI(NPROMA)
REAL(KIND=JPRB) :: ZSPNLT0(NPROMA),ZSPNLT0_SI(NPROMA)
REAL(KIND=JPRB) :: ZSPNLT1(NPROMA),ZSPNLT1_SI(NPROMA)
REAL(KIND=JPRB) :: ZSPNLT_FE(NPROMA,NFLEVG)
REAL(KIND=JPRB) :: ZSPTB(NPROMA)
REAL(KIND=JPRB) :: ZSPT1(NPROMA,NFLEVG+1)
REAL(KIND=JPRB) :: ZVWEI(NPROMA,NFLEVG)
REAL(KIND=JPRB) :: ZVWEIM(NPROMA)
REAL(KIND=JPRB) :: ZIN(NPROMA,NFLEVG),ZOU(NPROMA,NFLEVG+1)

INTEGER(KIND=JPIM) :: JLEV, JROF

REAL(KIND=JPRB) :: ZCMSLP, ZXIGP
REAL(KIND=JPRB) :: ZXIDT0, ZXIDT9

LOGICAL :: LLCT, LLCTC
REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

#include "abor1.intfb.h"
#include "verint.intfb.h"

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

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

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

!############################################
! 1. AUXILIARITIES
!############################################

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

! basic checking for compatibility of coding

IF(NVLAG == 2.AND.LVERTFE) THEN
  CALL ABOR1(' LATTES: NVLAG=2 and LVERTFE are incompatible')
ENDIF
IF((NVLAG == 3.AND.LVERTFE).AND. XIDT > 0.0_JPRB ) THEN
  CALL ABOR1 &
   & (' LATTES: LVERTFE not implemented for XIDT > 0.0_JPRB and NVLAG = 3')
ENDIF
IF( LPC_NESC.AND.LVERTFE.AND. XIDT > 0.0_JPRB )THEN
  CALL ABOR1 &
   & ('LATTES: LPC_NESC.AND.LVERTFE AND SECOND ORDER DECENTERING NOT CODED')
ENDIF

ZXIDT0=1.0_JPRB+XIDT
ZXIDT9=1.0_JPRB+XIDT
ZXIGP=1.0_JPRB+XIDT

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

!############################################
! 2. NONLINEAR MODEL
!############################################

ZCMSLP=RCMSLP0/(RD*RTSUR)
DO JROF=KST,KPROF
  ZSPTB(JROF)=PSDVBC(JROF,NFLEVG)/PRES0(JROF,NFLEVG)
ENDDO
IF (LVERCOR) THEN
  ! * the vertical integral of divergence
  !   has to be multiplied by "a**2/rs[surf]**2"
  DO JROF=KST,KPROF
    ZSPTB(JROF)=ZSPTB(JROF)*PVCASRS0H(JROF,NFLEVG)*PVCASRS0H(JROF,NFLEVG)
  ENDDO
ENDIF

IF (LVERCOR) THEN
! * [vertical weight]_l =
!   {[delta B]_l (rs**2/a**2)_l} / {sum_k=1_to_k=L [delta B]_k (rs**2/a**2)_k}
  IF (LVERTFE) THEN
    DO JLEV=1,NFLEVG
      DO JROF=KST,KPROF
        ZVWEI(JROF,JLEV)=VDELB(JLEV)*PVCRSSA0F(JROF,JLEV)*PVCRSSA0F(JROF,JLEV)
      ENDDO
    ENDDO
    DO JLEV=1,NFLEVG
      DO JROF=KST,KPROF
        ZIN(JROF,JLEV)=ZVWEI(JROF,JLEV)*VFE_RDETAH(JLEV)
      ENDDO
    ENDDO
    CALL VERINT(NPROMA,KST,KPROF,NFLEVG,ZIN,ZOU,0)
    ZVWEIM(KST:KPROF)=ZOU(KST:KPROF,NFLEVG+1)
  ELSE
    ZVWEIM(KST:KPROF)=0.0_JPRB
    DO JLEV=1,NFLEVG
      DO JROF=KST,KPROF
        ZVWEI(JROF,JLEV)=VDELB(JLEV)*PVCRSSA0F(JROF,JLEV)*PVCRSSA0F(JROF,JLEV)
        ZVWEIM(JROF)=ZVWEIM(JROF)+ZVWEI(JROF,JLEV)
      ENDDO
    ENDDO
  ENDIF
  DO JLEV=1,NFLEVG
    DO JROF=KST,KPROF
      ZVWEI(JROF,JLEV)=ZVWEI(JROF,JLEV)/ZVWEIM(JROF)
    ENDDO
  ENDDO
  DO JLEV=1,NFLEVG
    DO JROF=KST,KPROF
      PB2(JROF,MSLB2VCVWEI+JLEV-1)=PB2(JROF,MSLB2VCVWEI+JLEV-1)+ZVWEI(JROF,JLEV)
    ENDDO
  ENDDO
ELSE
! * [vertical weight]_l = [delta B]_l
  DO JLEV=1,NFLEVG
    ZVWEI(KST:KPROF,JLEV)=VDELB(JLEV)
  ENDDO
ENDIF

! linear model at time t

DO JROF=KST,KPROF
  PB2(JROF,MSLB2SPSI)=PBDT(JROF)*PSDIV0(JROF)
  ZSPTB(JROF)=-PDTS2*ZSPTB(JROF)
ENDDO

! nonlinear model at time t

DO JLEV=1,NFLEVG
  DO JROF=KST,KPROF
    ZMOY1SP(JROF,JLEV)=PDTS2*&
     & (PGMV(JROF,JLEV,YT0%MU)*(PGMVS(JROF,YT0%MSPL)+ZCMSLP*POROGL(JROF))&
     & +PGMV(JROF,JLEV,YT0%MV)*(PGMVS(JROF,YT0%MSPM)+ZCMSLP*POROGM(JROF)))
  ENDDO
  IF (LVERCOR) THEN
    ! * multiply the semi-reduced derivatives by "a/rs".
    DO JROF=KST,KPROF
      ZMOY1SP(JROF,JLEV)=ZMOY1SP(JROF,JLEV)*PVCASRS0F(JROF,JLEV)
    ENDDO
  ENDIF
  IF (LRWSDLR) THEN
    ! * multiply the semi-reduced derivatives by "a/r".
    DO JROF=KST,KPROF
      ZMOY1SP(JROF,JLEV)=ZMOY1SP(JROF,JLEV)*PRDLR0(JROF,JLEV,JP_ASRF)
    ENDDO
  ENDIF
  DO JROF=KST,KPROF
    ZMOY1SP(JROF,JLEV)=ZMOY1SP(JROF,JLEV)+ZSPTB(JROF)
  ENDDO
ENDDO

!*       *   Continuity equation.

IF (LTWOTL) THEN

  IF(.NOT.LLCT)THEN

    !############################################
    ! Predictor for LPC_FULL
    ! or predictor for LPC_OLD
    ! or case nsiter=0.
    !############################################

    IF (LPC_CHEAP.AND.(.NOT.LPC_NESC)) THEN
      ! LPC_CHEAP=T is currently coded for LPC_NESC only
      !  (for LPC_NESC=F interpolated quantities are not the same ones
      !  at the predictor and corrector steps, and the LPC_CHEAP code
      !  currently assumes that they are identical).
      CALL ABOR1('LATTES: If LPC_CHEAP=T, LPC_NESC should be T')
    ENDIF

    !---------------------------------------------------------------
    ! 3D buffers 
    !---------------------------------------------------------------
    DO JLEV=1,NFLEVG

      ! nonlinear residual time t
      IF (LSLINLC1) THEN
        DO JROF=KST,KPROF
          ZSPNLT0(JROF)=ZMOY1SP(JROF,JLEV)
          ZSPNLT0_SI(JROF)=ZXIDT0*PB2(JROF,MSLB2SPSI)
        ENDDO
      ELSE
        DO JROF=KST,KPROF
          ZSPNLT0(JROF)=ZMOY1SP(JROF,JLEV)+ZXIDT0*PB2(JROF,MSLB2SPSI)
        ENDDO
      ENDIF
            
      ! Fill PB1(.,MSLB1C9),PGMVT1S(.,YT1%MSP),ZSPNLT_FE:
      IF (NVLAG == 2 .OR. NVLAG == 3) THEN

        IF ( NSTEP <= 0 .OR. (LPC_FULL.AND.LPC_NESC) ) THEN

          DO JROF=KST,KPROF
            PB1(JROF,MSLB1C9+JLEV-NFLSA)=PB1(JROF,MSLB1C9+JLEV-NFLSA) &
             & +PESGM*ZMOY1SP(JROF,JLEV)
          ENDDO
          IF (LVERTFE) THEN
            DO JROF=KST,KPROF
              ZSPNLT_FE(JROF,JLEV)=ZSPNLT0(JROF) &
               & *ZVWEI(JROF,JLEV)*VFE_RDETAH(JLEV)*PESGP
            ENDDO
            IF (LSLINLC1) THEN
              DO JROF=KST,KPROF
                PGMVT1S(JROF,YT1%MSP)=PGMVT1S(JROF,YT1%MSP) &
                 & +PESGP*ZVWEI(JROF,JLEV)*ZSPNLT0_SI(JROF)
              ENDDO
            ENDIF
          ELSE
            DO JROF=KST,KPROF
              PGMVT1S(JROF,YT1%MSP)=PGMVT1S(JROF,YT1%MSP) &
               & +PESGP*ZVWEI(JROF,JLEV)*ZSPNLT0(JROF)
            ENDDO
          ENDIF
 
        ELSEIF (LSETTLS) THEN

          IF (LSLINLC1) THEN
            DO JROF=KST,KPROF
              PB1(JROF,MSLB1C9+JLEV-NFLSA)=PB1(JROF,MSLB1C9+JLEV-NFLSA) &
               & +PESGM*ZMOY1SP(JROF,JLEV) &
               & +(ZSPNLT0(JROF)-PGMV(JROF,JLEV,YT9%MSPNL))
              PB1(JROF,MSLB1C9_SI+JLEV-NFLSA)=PB1(JROF,MSLB1C9_SI+JLEV-NFLSA) &
               & +(ZSPNLT0_SI(JROF)-PGMV(JROF,JLEV,YT9%MSPNL_SI))
            ENDDO
          ELSE
            DO JROF=KST,KPROF
              PB1(JROF,MSLB1C9+JLEV-NFLSA)=PB1(JROF,MSLB1C9+JLEV-NFLSA) &
               & +PESGM*ZMOY1SP(JROF,JLEV) &
               & +(ZSPNLT0(JROF)-PGMV(JROF,JLEV,YT9%MSPNL))
            ENDDO
          ENDIF
          IF( LVERTFE )THEN
            DO JROF=KST,KPROF
              ZSPNLT_FE(JROF,JLEV)=&
               & ZSPNLT0(JROF)*PESGP*ZVWEI(JROF,JLEV)*VFE_RDETAH(JLEV)
            ENDDO
            IF (LSLINLC1) THEN
              DO JROF=KST,KPROF
                PGMVT1S(JROF,YT1%MSP)=PGMVT1S(JROF,YT1%MSP) &
                 & +PESGP*ZVWEI(JROF,JLEV)*ZSPNLT0_SI(JROF)
              ENDDO
            ENDIF
          ELSE
            DO JROF=KST,KPROF
              PGMVT1S(JROF,YT1%MSP)=PGMVT1S(JROF,YT1%MSP) &
               & +PESGP*ZVWEI(JROF,JLEV)*ZSPNLT0(JROF)
            ENDDO
          ENDIF

        ELSE

          ! remaining case: ldsettls=false, lpc_nesc=false.
          IF (LSLINLC1) THEN
            DO JROF=KST,KPROF
              PB1(JROF,MSLB1C9+JLEV-NFLSA)=PB1(JROF,MSLB1C9+JLEV-NFLSA) &
               & +PESGM*ZMOY1SP(JROF,JLEV) &
               & +0.5_JPRB*PESGM*(ZSPNLT0(JROF)-PGMV(JROF,JLEV,YT9%MSPNL))
              PB1(JROF,MSLB1C9_SI+JLEV-NFLSA)=PB1(JROF,MSLB1C9_SI+JLEV-NFLSA) &
               & +0.5_JPRB*PESGM*(ZSPNLT0_SI(JROF)-PGMV(JROF,JLEV,YT9%MSPNL_SI))
            ENDDO 
          ELSE
            DO JROF=KST,KPROF
              PB1(JROF,MSLB1C9+JLEV-NFLSA)=PB1(JROF,MSLB1C9+JLEV-NFLSA) &
               & +PESGM*ZMOY1SP(JROF,JLEV) &
               & +0.5_JPRB*PESGM*(ZSPNLT0(JROF)-PGMV(JROF,JLEV,YT9%MSPNL))
            ENDDO 
          ENDIF
          IF (LVERTFE) THEN
            DO JROF=KST,KPROF
              ZSPNLT_FE(JROF,JLEV)= &
               & (1.5_JPRB*ZSPNLT0(JROF)-0.5_JPRB*PGMV(JROF,JLEV,YT9%MSPNL)) &
               & *ZVWEI(JROF,JLEV)*VFE_RDETAH(JLEV)*PESGP
            ENDDO
            IF (LSLINLC1) THEN
              DO JROF=KST,KPROF
                PGMVT1S(JROF,YT1%MSP)=PGMVT1S(JROF,YT1%MSP) &
                 & +PESGP*ZVWEI(JROF,JLEV)*(1.5_JPRB*ZSPNLT0_SI(JROF) &
                 & -0.5_JPRB*PGMV(JROF,JLEV,YT9%MSPNL_SI))
              ENDDO
            ENDIF
          ELSE
            DO JROF=KST,KPROF
              PGMVT1S(JROF,YT1%MSP)=PGMVT1S(JROF,YT1%MSP) &
               & +PESGP*ZVWEI(JROF,JLEV) &
               & *(1.5_JPRB*ZSPNLT0(JROF)-0.5_JPRB*PGMV(JROF,JLEV,YT9%MSPNL))
            ENDDO
          ENDIF

        ENDIF

      ENDIF

      ! Save quantities for corrector step
      IF( LPC_FULL )THEN
        ! save nonlinear model at time t
        PGMV(KST:KPROF,JLEV,YT9%MCSPNL)=ZMOY1SP(KST:KPROF,JLEV)
      ENDIF

      IF( .NOT.(LPC_FULL.AND.LPC_NESC) )THEN
        ! save of nonlinear residual at time t
        ! to be used as nonlinear residual at time t-dt next time step
        IF (LSLINLC1) THEN
          PGMV(KST:KPROF,JLEV,YT9%MSPNL)=ZMOY1SP(KST:KPROF,JLEV)
          PGMV(KST:KPROF,JLEV,YT9%MSPNL_SI)=ZXIDT9*PB2(KST:KPROF,MSLB2SPSI)
        ELSE
          DO JROF=KST,KPROF
            PGMV(JROF,JLEV,YT9%MSPNL)=ZMOY1SP(JROF,JLEV) &
             & +ZXIDT9*PB2(JROF,MSLB2SPSI)
          ENDDO
        ENDIF
      ENDIF

    ENDDO

    !------------------------------------------------------------------------
    ! 3D additional actions
    !------------------------------------------------------------------------
    IF (LVERTFE) THEN
      IF(NVLAG == 2 .OR. NVLAG == 3)THEN
        CALL VERINT(NPROMA,KST,KPROF,NFLEVG,ZSPNLT_FE,ZSPT1,0)
        DO JROF=KST,KPROF
          PGMVT1S(JROF,YT1%MSP)=PGMVT1S(JROF,YT1%MSP)+ZSPT1(JROF,NFLEVG+1)
        ENDDO
      ENDIF
    ENDIF

    !------------------------------------------------------------------------
    ! 2D buffers
    !------------------------------------------------------------------------
    IF(NVLAG == 2 .OR. NVLAG == 3)THEN
      DO JROF=KST,KPROF
        PB1(JROF,MSLB1SP9)=PB1(JROF,MSLB1SP9)+PGMVS(JROF,YT0%MSP) &
         & +ZCMSLP*POROG(JROF)
        PGMVT1S(JROF,YT1%MSP)=PGMVT1S(JROF,YT1%MSP)-ZCMSLP*POROG(JROF)
      ENDDO
    ENDIF

  ELSE

    !############################################
    ! Corrector for LPC_FULL
    !############################################
    !---------------------------------------------------------------
    ! 3D buffers 
    !---------------------------------------------------------------
    DO JLEV=1,NFLEVG

      ! nonlinear residual time t

      IF (LSLINLC1) THEN
        ZSPNLT1(KST:KPROF)=ZMOY1SP(KST:KPROF,JLEV)
        ZSPNLT1_SI(KST:KPROF)=ZXIDT9*PB2(KST:KPROF,MSLB2SPSI)
      ELSE
        DO JROF=KST,KPROF
          ZSPNLT1(JROF)=ZMOY1SP(JROF,JLEV)+ZXIDT9*PB2(JROF,MSLB2SPSI)
        ENDDO
      ENDIF

      IF (.NOT.LLCTC) THEN
        DO JROF=KST,KPROF
          PB1(JROF,MSLB1C9+JLEV-NFLSA)=PB1(JROF,MSLB1C9+JLEV-NFLSA) &
           & +PESGM*PGMV(JROF,JLEV,YT9%MCSPNL)
        ENDDO
      ENDIF
      IF (LVERTFE) THEN
        DO JROF=KST,KPROF
          ZSPNLT_FE(JROF,JLEV)=ZSPNLT1(JROF) &
           & *ZVWEI(JROF,JLEV)*VFE_RDETAH(JLEV)*PESGP
        ENDDO
        IF (LSLINLC1) THEN
          DO JROF=KST,KPROF
            PGMVT1S(JROF,YT1%MSP)=PGMVT1S(JROF,YT1%MSP) &
             & +ZVWEI(JROF,JLEV)*ZSPNLT1_SI(JROF)*PESGP
          ENDDO
        ENDIF
      ELSE
        DO JROF=KST,KPROF
          PGMVT1S(JROF,YT1%MSP)=PGMVT1S(JROF,YT1%MSP) &
           & +ZVWEI(JROF,JLEV)*ZSPNLT1(JROF)*PESGP
        ENDDO
      ENDIF

    ENDDO

    !------------------------------------------------------------------------
    ! 3D additional actions
    !------------------------------------------------------------------------
    IF (LVERTFE) THEN
      CALL VERINT(NPROMA,KST,KPROF,NFLEVG,ZSPNLT_FE,ZSPT1,0)
      DO JROF=KST,KPROF
        PGMVT1S(JROF,YT1%MSP)=PGMVT1S(JROF,YT1%MSP)+ZSPT1(JROF,NFLEVG+1)
      ENDDO
    ENDIF

    !------------------------------------------------------------------------
    ! 2D buffers
    !------------------------------------------------------------------------
    IF (.NOT.LLCTC) THEN
      DO JROF=KST,KPROF
        PB1(JROF,MSLB1SP9)=PB1(JROF,MSLB1SP9)+PGMVS(JROF,YT9%MSP) &
         & +ZCMSLP*POROG(JROF)
      ENDDO
    ENDIF
    DO JROF=KST,KPROF
      PGMVT1S(JROF,YT1%MSP)=PGMVT1S(JROF,YT1%MSP)-ZCMSLP*POROG(JROF)
    ENDDO

  ENDIF

  ! add preliminary quantity for LAPGHY physics

  IF( XIDT > 0.0_JPRB )THEN
    DO JROF=KST,KPROF
      PGMVT1S(JROF,YT1%MSP)=PGMVT1S(JROF,YT1%MSP)-ZXIGP*PB2(JROF,MSLB2SPSI)
      PB2(JROF,MSLB2SPSI)=ZXIGP*PB2(JROF,MSLB2SPSI)
    ENDDO
  ELSE
    DO JROF=KST,KPROF
      PGMVT1S(JROF,YT1%MSP)=PGMVT1S(JROF,YT1%MSP)-PESGP*PB2(JROF,MSLB2SPSI)
      PB2(JROF,MSLB2SPSI)=PESGP*PB2(JROF,MSLB2SPSI)
    ENDDO
  ENDIF

ELSE

  IF(NVLAG == 2 .OR. NVLAG == 3)THEN

    IF(LVERTFE) THEN
      DO JLEV=1,NFLEVG
        IF (LSLINLC1) THEN
          DO JROF=KST,KPROF
            ZMOYSPNL(JROF)=ZMOY1SP(JROF,JLEV)
            ZMOYSPNL_SI(JROF)=PB2(JROF,MSLB2SPSI)
            PB1(JROF,MSLB1C9+JLEV-NFLSA)=PB1(JROF,MSLB1C9+JLEV-NFLSA)&
             & +PESGM*ZMOYSPNL(JROF)-PESGM*PBDT(JROF)*PSDIV9(JROF)
            PB1(JROF,MSLB1C9_SI+JLEV-NFLSA)=PB1(JROF,MSLB1C9_SI+JLEV-NFLSA) &
             & +PESGM*ZMOYSPNL_SI(JROF)
            ZSPNLT_FE(JROF,JLEV)=PESGP*ZVWEI(JROF,JLEV)*ZMOYSPNL(JROF)
            PGMVT1S(JROF,YT1%MSP)=PGMVT1S(JROF,YT1%MSP) &
             & +PESGP*ZVWEI(JROF,JLEV)*ZMOYSPNL_SI(JROF)
          ENDDO
        ELSE
          DO JROF=KST,KPROF
            ZMOYSPNL(JROF)=ZMOY1SP(JROF,JLEV)+PB2(JROF,MSLB2SPSI)
            PB1(JROF,MSLB1C9+JLEV-NFLSA)=PB1(JROF,MSLB1C9+JLEV-NFLSA)&
             & +PESGM*ZMOYSPNL(JROF)-PESGM*PBDT(JROF)*PSDIV9(JROF)
            ZSPNLT_FE(JROF,JLEV)=PESGP*ZVWEI(JROF,JLEV)*ZMOYSPNL(JROF)
          ENDDO
        ENDIF
      ENDDO
      CALL VERINT(NPROMA,KST,KPROF,NFLEVG,ZSPNLT_FE,ZSPT1,0)
      DO JROF=KST,KPROF
        PGMVT1S(JROF,YT1%MSP)=PGMVT1S(JROF,YT1%MSP)+ZSPT1(JROF,NFLEVG+1)
      ENDDO
    ELSE
      DO JLEV=1,NFLEVG
        DO JROF=KST,KPROF
          ZMOYSPNL(JROF)=ZMOY1SP(JROF,JLEV)+PB2(JROF,MSLB2SPSI)
          PB1(JROF,MSLB1C9+JLEV-NFLSA)=PB1(JROF,MSLB1C9+JLEV-NFLSA)&
           & +PESGM*ZMOYSPNL(JROF)-PESGM*PBDT(JROF)*PSDIV9(JROF)
          PGMVT1S(JROF,YT1%MSP)=PGMVT1S(JROF,YT1%MSP) &
           & +PESGP*ZVWEI(JROF,JLEV)*ZMOYSPNL(JROF)
        ENDDO
      ENDDO
    ENDIF

    DO JROF=KST,KPROF
      PB1(JROF,MSLB1SP9)=PB1(JROF,MSLB1SP9)+PGMVS(JROF,YT9%MSP) &
       & +ZCMSLP*POROG(JROF)
      PGMVT1S(JROF,YT1%MSP)=PGMVT1S(JROF,YT1%MSP)-ZCMSLP*POROG(JROF) &
       & -PESGP*PB2(JROF,MSLB2SPSI)
      PB2(JROF,MSLB2SPSI)=PESGP*PB2(JROF,MSLB2SPSI)
    ENDDO

  ENDIF

ENDIF

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

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

END SUBROUTINE LATTES

