SUBROUTINE CPEULDYN( &
 ! --- INPUT ------------------------------------------------------------------
 & CDCONF,KST,KPROF,PDT,&
 & POROG,PGFL,PTSOL,PQSOL,PNHXT0,&
 & PGNORDL,PGNORDM,&
 & PRCORI,PRCORIC,PGM,PRATATH,PRATATX,PSDVBC,PKENE,PEVEL,PVVEL,&
 & PSGRTL,PSGRTM,&
 & PVCASRSF,PVCASRSH,PVCWF,PWDLWF,&
 & PRES0,PRDELP,PKAP,PTNDPD,PTNDVD,P3DIVG,PD3Y9,PNHXT9,&
 ! --- INPUT/OUTPUT -----------------------------------------------------------
 & PD3Y0,PGFLT1,PGMVT1,PGMVT1S,PGFLPC,PGMV,PGMVS,&
 ! --- OUTPUT -----------------------------------------------------------------
 & PB2)

!**** *CPEULDYN* - dynamic grid point calculations for Eulerian advection.

!     Purpose.
!     --------
!           Dynamic non-linear computations in grid-point space 
!           for Eulerian advection scheme, for both hydrostatic
!           and NH models.

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

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

!        INPUT:
!          CDCONF      - "stepo" configuration for grid-point calculations.
!          KST         - first element of work.
!          KPROF       - depth of work.
!          PDT         - SL2TL or first timestep: timestep;
!                        otherwise 2*timestep.
!          POROG       - surface orography.
!          PGFL        - GFL variables at t.
!          PTSOL       - surface temperature.
!          PQSOL       - surface specific humidity.
!          PNHXT0      - term 'X' at time t freshly diagnosed.
!          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 2.OMEGA .
!          PRCORIC     - 2 Omega cos(theta).
!          PGM         - mapping factor.
!          PRATATH     - curvature term Cy = dm/dy (used when plane).
!          PRATATX     - curvature term Cx = dm/dx (used when plane).
!          PSDVBC      - vertical integral of divergence which appears in the
!                        RHS of continuity equation, including the "lrubc" and 
!                        "delta m=1" contribution of (ETADOT DP/DETA).
!          PKENE       - kinetic energy.
!          PEVEL       - ETADOT dP/dETA at t.
!          PVVEL       - "omega/prehyd" a t.
!          PSGRTL      - zonal component of the pressure force
!                        (grad(phi)+RT grad(p)).
!          PSGRTM      - meridian component of the pressure force.
!          PVCASRSF    - "a/rs" on layers at t.
!          PVCASRSH    - "a/rs" on interlayers at t.
!          PVCWF       - pseudo-vertical velocity "W = Drs/Dt" on layers at t.
!          PWDLWF      - vertical velocity "w = Dr/Dt" at full levels at t.
!          PRES0       - pressure on an interlayer at t.
!          PRDELP      - 1/(pressure depth of layers) at t.
!          PKAP        - R/CP at t.
!          PTNDPD      - [D(pressure departure variable)/Dt]_adiab
!          PTNDVD      - [D(vertical divergence variable)/Dt]_adiab
!          P3DIVG      - D3 at t.
!          PD3Y9       - term 'Y' at t-dt.
!          PNHXT9      - term 'X' at t-dt.                             

!        INPUT/OUTPUT:
!          PD3Y0       - term 'Y' at t.
!          PGFLT1      - t+dt term for GFL
!                        (as input contains the non lagged physics).
!          PGMVT1      - GMV  (upper air) variables at t+dt.
!          PGMVT1S     - GMVS (surface) variables at t+dt.
!          PGFLPC      - storage array for GFL for LPC_FULL.
!          PGMV        - GMV  (upper air) variables at t-dt and t.
!          PGMVS       - GMVS (surface) variables at t-dt and t.

!        OUTPUT:
!          PB2         - "SLBUF2" buffer.

!        Implicit arguments : none.
!        --------------------

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

!     Externals.
!     ----------
!     Reference.
!     ----------
!        ARPEGE/ALADIN documentation

!     Author.
!     -------
!        Mats Hamrud and Philippe Courtier  *ECMWF*  (CPDYN)
!        Radmila Bubnova *GMAP/COMPAS - stage MRE*   (GNHDYN)

! Modifications
! -------------
!   Original : 93-05-05
!   March-2005 J. Vivoda - Eulerian Iterative Schemes 
!                        - merge of CPDYN and GNHDYN -> CPEULDYN
!   M.Hamrud  15-Jan-2006  Revised GPRCP
!   June -2005 R. Brozkova - cleaning  NH variable d4 computations
!   K. Yessad (Oct 2007) - VFE vertical advections for NH variables
!   N. Wedi and K. Yessad (Jan 2008): different dev for NH model and PC scheme
!   K. Yessad (Sep 2008): LREGETA -> LREGETA+LVFE_REGETA.
!   K. Yessad (Dec 2008): remove dummy CDLOCK
!------------------------------------------------------------------
! End Modifications
!------------------------------------------------------------------

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

USE YOMDIM   , ONLY : NFLEVG   ,NPROMA   ,NPROMVC  ,NPROMDLW
USE YOMGMV   , ONLY : NDIMGMV,NDIMGMVS,YT0,YT1,YT9
USE YOMSC2   , ONLY : NFLDSLB2
USE YOMCST   , ONLY : RG       ,RD       ,RETV     ,RA       ,RCVD     ,&
 & RCPD
USE YOMCT0   , ONLY : LSPRT    ,LPC_OLD  ,LPC_FULL ,LNHDYN   ,LRFRIC   ,&
 & LVERCOR
USE YOMDYN   , ONLY : BETADT   ,SITR     ,SITRA    ,NCURRENT_ITER,&
 & LIMPF   ,LSIDG    ,RKRF     ,RC_PD1   ,RCORPDF
USE YOMGEM   , ONLY : RSTRET   ,VFE_RDETAH         ,VBH      ,VBF      ,VDELB
USE YOMCVER  , ONLY : LVERTFE  ,LVFE_DBCS,LVFE_DBCT,LVFE_DERIB
USE YOEPHY   , ONLY : LEPHYS   ,LAGPHY
USE YOMPHY   , ONLY : NDPSFI   ,LMPHYS
USE YOMDYNA  , ONLY : NPDVAR   ,NVDVAR   ,LNH_PDVD ,LNH_GEOGW,LRWSDLW  ,&
 & LRWSDLR
USE YOM_YGFL , ONLY : YGFL     ,YGFLC    ,YQ
USE YOMCT3   , ONLY : NSTEP
USE PTRSLB2  , ONLY : MSLB2USI ,MSLB2VSI ,MSLB2TSI ,MSLB2PDSI,MSLB2VDSI,&
 & MSLB2SPSI

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

IMPLICIT NONE

CHARACTER(LEN=1)  ,INTENT(IN)    :: CDCONF 
INTEGER(KIND=JPIM),INTENT(IN)    :: KST 
INTEGER(KIND=JPIM),INTENT(IN)    :: KPROF 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDT 
REAL(KIND=JPRB)   ,INTENT(IN)    :: POROG(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGFL(NPROMA,NFLEVG,YGFL%NDIM) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTSOL(NPROMA)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQSOL(NPROMA)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PNHXT0(NPROMA,NFLEVG) 
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)    :: PGM(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRATATH(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRATATX(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSDVBC(NPROMA,0:NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PKENE(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PEVEL(NPROMA,0:NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVVEL(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSGRTL(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSGRTM(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVCASRSF(NPROMVC,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVCASRSH(NPROMVC,0:NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVCWF(NPROMVC,NFLEVG)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PWDLWF(NPROMDLW,NFLEVG)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRES0(NPROMA,0:NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRDELP(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PKAP(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTNDPD(NPROMA,NFLEVG)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTNDVD(NPROMA,NFLEVG)
REAL(KIND=JPRB)   ,INTENT(IN)    :: P3DIVG(NPROMA,NFLEVG)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PD3Y9(NPROMA,NFLEVG)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PNHXT9(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PD3Y0(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PGFLT1(NPROMA,NFLEVG,YGFL%NDIM1) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PGMVT1(NPROMA,NFLEVG,YT1%NDIM)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PGMVT1S(NPROMA,YT1%NDIMS)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PGFLPC(NPROMA,NFLEVG,YGFL%NDIMPC) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PGMV(NPROMA,NFLEVG,NDIMGMV)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PGMVS(NPROMA,NDIMGMVS)
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PB2(NPROMA,NFLDSLB2)

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

REAL(KIND=JPRB) :: ZR0   (NPROMA,NFLEVG)
REAL(KIND=JPRB) :: ZSID  (NPROMA,NFLEVG)
REAL(KIND=JPRB) :: ZSIDL (NPROMA,NFLEVG)
REAL(KIND=JPRB) :: ZSIDM (NPROMA,NFLEVG)
REAL(KIND=JPRB) :: ZSIPR (NPROMA,NFLEVG)
REAL(KIND=JPRB) :: ZSIPR9(NPROMA,NFLEVG)
REAL(KIND=JPRB) :: ZSIPRL(NPROMA,NFLEVG)
REAL(KIND=JPRB) :: ZSIPRM(NPROMA,NFLEVG)
REAL(KIND=JPRB) :: ZSISP (NPROMA)
REAL(KIND=JPRB) :: ZSISPL(NPROMA)
REAL(KIND=JPRB) :: ZSISPM(NPROMA)
REAL(KIND=JPRB) :: ZSIT  (NPROMA,NFLEVG)
REAL(KIND=JPRB) :: ZSIT9 (NPROMA,NFLEVG)
REAL(KIND=JPRB) :: ZSITL (NPROMA,NFLEVG)
REAL(KIND=JPRB) :: ZSITM (NPROMA,NFLEVG)
REAL(KIND=JPRB) :: ZSIVD (NPROMA,NFLEVG)
REAL(KIND=JPRB) :: ZTT0L (NPROMA,NFLEVG)
REAL(KIND=JPRB) :: ZTT0M (NPROMA,NFLEVG)

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

REAL(KIND=JPRB) :: ZCPCV(NPROMA), ZRCV(NPROMA)
REAL(KIND=JPRB) :: ZUADV (NPROMA,NFLEVG)
REAL(KIND=JPRB) :: ZVADV (NPROMA,NFLEVG)
REAL(KIND=JPRB) :: ZTADV (NPROMA,NFLEVG)
REAL(KIND=JPRB) :: ZSPDADV (NPROMA,NFLEVG)
REAL(KIND=JPRB) :: ZSVDADV (NPROMA,NFLEVG)
REAL(KIND=JPRB) :: ZUM(NPROMA,NFLEVG) 
REAL(KIND=JPRB) :: ZVM(NPROMA,NFLEVG) 
REAL(KIND=JPRB) :: ZTM(NPROMA,NFLEVG) 
REAL(KIND=JPRB) :: ZSPDM(NPROMA,NFLEVG) 
REAL(KIND=JPRB) :: ZSVDM(NPROMA,NFLEVG) 
REAL(KIND=JPRB) :: ZSPM(NPROMA) 
REAL(KIND=JPRB) :: ZURES0(NPROMA,NFLEVG) 
REAL(KIND=JPRB) :: ZVRES0(NPROMA,NFLEVG) 
REAL(KIND=JPRB) :: ZTRES0(NPROMA,NFLEVG) 
REAL(KIND=JPRB) :: ZSPDRES0(NPROMA,NFLEVG) 
REAL(KIND=JPRB) :: ZSVDRES0(NPROMA,NFLEVG) 
REAL(KIND=JPRB) :: ZSPRES0(NPROMA) 
REAL(KIND=JPRB) :: ZUSI0(NPROMA,NFLEVG) 
REAL(KIND=JPRB) :: ZVSI0(NPROMA,NFLEVG) 
REAL(KIND=JPRB) :: ZTSI0(NPROMA,NFLEVG) 
REAL(KIND=JPRB) :: ZSPDSI0(NPROMA,NFLEVG) 
REAL(KIND=JPRB) :: ZSVDSI0(NPROMA,NFLEVG) 
REAL(KIND=JPRB) :: ZSPSI0(NPROMA) 

REAL(KIND=JPRB) :: ZSPDT0(NPROMA,NFLEVG) 
REAL(KIND=JPRB) :: ZSVDT0(NPROMA,NFLEVG) 

REAL(KIND=JPRB) :: ZDUDE(NPROMA,NFLEVG)
REAL(KIND=JPRB) :: ZDVDE(NPROMA,NFLEVG)
REAL(KIND=JPRB) :: ZDTDE(NPROMA,NFLEVG)
REAL(KIND=JPRB) :: ZDSPDDE(NPROMA,NFLEVG)
REAL(KIND=JPRB) :: ZDSVDDE(NPROMA,NFLEVG)
REAL(KIND=JPRB) :: ZDGFDE(NPROMA,NFLEVG,YGFL%NUMFLDS)

REAL(KIND=JPRB) :: ZW1L(NPROMA,0:NFLEVG+1)
REAL(KIND=JPRB) :: ZW1M(NPROMA,0:NFLEVG+1)
REAL(KIND=JPRB) :: ZW2(NPROMA,0:NFLEVG+1)
REAL(KIND=JPRB) :: ZW3(NPROMA,0:NFLEVG+1)
REAL(KIND=JPRB) :: ZW4(NPROMA,0:NFLEVG+1)
REAL(KIND=JPRB) :: ZIN(NPROMA,0:NFLEVG+1)

REAL(KIND=JPRB) :: ZCOEF, ZCOEG, ZCOEH, ZCOEI, ZCOEJ
REAL(KIND=JPRB) :: ZFM, ZFTEND, ZUSA, ZDT, ZEPS, ZEVEL, ZBDT, ZED

LOGICAL         :: LLDPSFI

REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

#include "abor1.intfb.h"
#include "gprcp.intfb.h"
#include "gptf1pc.intfb.h"
#include "gptf2pc.intfb.h"
#include "siseve.intfb.h"
#include "sidd.intfb.h"
#include "sigam.intfb.h"
#include "siptp.intfb.h"
#include "sitnu.intfb.h"
#include "sivderi.intfb.h"
#include "verder.intfb.h"

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

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

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

!*       1.    INITIALISATION OF AUX QUANTITIES
!              --------------------------------

! ZFM = factor to apply on quantities to have always in buffer dt*M resp. dt*L
!       independently of time step
! ZFTEND = factor to apply in order to get correct tendency for first time step
IF( NSTEP<= 0 )THEN
  ZFM = 1.0_JPRB
  ZFTEND = 0.5_JPRB
ELSE
  ZFM = 0.5_JPRB
  ZFTEND = 1.0_JPRB
ENDIF

! put advective buffers to 0
ZUADV = 0.0_JPRB
ZVADV = 0.0_JPRB
ZTADV = 0.0_JPRB
IF(LNHDYN)THEN
  ZSPDADV = 0.0_JPRB
  ZSVDADV = 0.0_JPRB
ENDIF

! dimension for calls to SI routines
IPROFS=KPROF-KST+1

ZUSA=1.0_JPRB/RA

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

!*       2.    VERTICAL ADVECTION.
!              -------------------
!     Due to stability reasons advection is evaluated 
!     at time t in predictor and corrector as well.
!     For advection we use notation A(t) in this routine.
!     A(t) is computed during predictor (SI step) and
!     than stored in GMV structure (PXRHS arrays in this routine)

IF( NCURRENT_ITER == 0 )THEN

  ! * GMV variables:

  IF(LVERTFE) THEN

    ! * In this case the vertical advection of a variable X is discretized
    !   as follows:
    !   [etadot (dX/d eta)]_l = [etadot (d prehyd/d eta)]_l
    !    * [Delta eta]_l * [1/[Delta prehyd]_l] * [dX/d eta]_l,
    !   where [dX/d eta]_l is computed on the full level number "l"
    !   from [X]_l by calling the routine VERDER.

    ! * remark KY: ZEPS can be replaced by a tunable variable in NAMDYN
    !   and YOMDYN or YOMCVER.
    ZEPS=0.25_JPRB

    ! * One first computes [dX/d eta]_l (in array ZD[X]DE):
    CALL VERDER(NPROMA,KST,KPROF,NFLEVG,PGMV(1,1,YT0%MU),ZDUDE)
    CALL VERDER(NPROMA,KST,KPROF,NFLEVG,PGMV(1,1,YT0%MV),ZDVDE)
    CALL VERDER(NPROMA,KST,KPROF,NFLEVG,PGMV(1,1,YT0%MT),ZDTDE)
    IF (LNHDYN) THEN
      IF ( NPDVAR == 3 )THEN
        DO JLEV=1,NFLEVG
          DO JROF=KST,KPROF
            ZSPDT0(JROF,JLEV)=PGMV(JROF,JLEV,YT0%MSPD)+VBF(JLEV)*POROG(JROF)
          ENDDO
        ENDDO
        CALL VERDER(NPROMA,KST,KPROF,NFLEVG,ZSPDT0,ZDSPDDE)
      ELSEIF ( NPDVAR == 2 .AND. LRWSDLR )THEN
        ! d(rcorpdf)/dt = 0, and grad(rcorpdf)=0,
        ! so some vertical advection terms cancel between here and
        ! gnh_tndlagadiab_spd, and they are omitted at both locations.
        DO JLEV=1,NFLEVG
          DO JROF=KST,KPROF
            ZSPDT0(JROF,JLEV)=PGMV(JROF,JLEV,YT0%MSPD)-RC_PD1*RCORPDF(JLEV)
          ENDDO
        ENDDO
        CALL VERDER(NPROMA,KST,KPROF,NFLEVG,ZSPDT0,ZDSPDDE)
      ELSE
        CALL VERDER(NPROMA,KST,KPROF,NFLEVG,PGMV(1,1,YT0%MSPD),ZDSPDDE)
      ENDIF
    ENDIF
    IF (LNHDYN) THEN
      IF( NVDVAR == 4 )THEN
        DO JLEV=1,NFLEVG
          DO JROF=KST,KPROF
            ZSVDT0(JROF,JLEV)=PGMV(JROF,JLEV,YT0%MSVD)-PNHXT0(JROF,JLEV)
          ENDDO
        ENDDO
        CALL VERDER(NPROMA,KST,KPROF,NFLEVG,ZSVDT0,ZDSVDDE)
      ELSE
        CALL VERDER(NPROMA,KST,KPROF,NFLEVG,PGMV(1,1,YT0%MSVD),ZDSVDDE)
      ENDIF
    ENDIF

    ! * One computes then the vertical advection, with a vertical filter
    !   on [etadot (d prehyd/d eta)]_l in the high atmosphere.
    DO JLEV=1,NFLEVG
      DO JROF=KST,KPROF
        IF(VBH(JLEV) < 1.E-5_JPRB .AND. JLEV > 1 .AND. JLEV < NFLEVG) THEN
          ZEVEL=(1._JPRB-2*ZEPS)*PEVEL(JROF,JLEV)+ZEPS*(PEVEL(JROF,JLEV-1)+&
          & PEVEL(JROF,JLEV+1))
        ELSE
          ZEVEL=PEVEL(JROF,JLEV)
        ENDIF
        ZED=PDT*ZEVEL*PRDELP(JROF,JLEV)/VFE_RDETAH(JLEV)
        ZUADV(JROF,JLEV)=ZUADV(JROF,JLEV)-ZED*ZDUDE(JROF,JLEV)
        ZVADV(JROF,JLEV)=ZVADV(JROF,JLEV)-ZED*ZDVDE(JROF,JLEV)
        ZTADV(JROF,JLEV)=ZTADV(JROF,JLEV)-ZED*ZDTDE(JROF,JLEV)
        IF(LNHDYN) ZSPDADV(JROF,JLEV)=ZSPDADV(JROF,JLEV)-ZED*ZDSPDDE(JROF,JLEV)
        IF(LNHDYN) ZSVDADV(JROF,JLEV)=ZSVDADV(JROF,JLEV)-ZED*ZDSVDDE(JROF,JLEV)
      ENDDO
    ENDDO
    IF (LNHDYN) THEN
      IF ( NPDVAR == 3 )THEN
        DO JLEV=1,NFLEVG
          DO JROF=KST,KPROF
            ZSPDADV(JROF,JLEV)=ZSPDADV(JROF,JLEV) &
             & +PDT*VDELB(JLEV)*PEVEL(JROF,JLEV)*PRDELP(JROF,JLEV)*POROG(JROF)
          ENDDO
        ENDDO
      ENDIF
    ENDIF

  ELSE

    ! * U,V,T:
    DO JLEV=1,NFLEVG-1
      DO JROF=KST,KPROF
        ZED=PDT*0.5_JPRB*PEVEL(JROF,JLEV)
        ZUADV(JROF,JLEV)=ZUADV(JROF,JLEV)+&
         & (PGMV(JROF,JLEV,YT0%MU)-PGMV(JROF,JLEV+1,YT0%MU))&
         & *ZED*PRDELP(JROF,JLEV)  
        ZUADV(JROF,JLEV+1)=ZUADV(JROF,JLEV+1)+&
         & (PGMV(JROF,JLEV,YT0%MU)-PGMV(JROF,JLEV+1,YT0%MU))&
         & *ZED*PRDELP(JROF,JLEV+1)  

        ZVADV(JROF,JLEV)=ZVADV(JROF,JLEV)+&
         & (PGMV(JROF,JLEV,YT0%MV)-PGMV(JROF,JLEV+1,YT0%MV))&
         & *ZED*PRDELP(JROF,JLEV)
        ZVADV(JROF,JLEV+1)=ZVADV(JROF,JLEV+1)+&
         & (PGMV(JROF,JLEV,YT0%MV)-PGMV(JROF,JLEV+1,YT0%MV))&
         & *ZED*PRDELP(JROF,JLEV+1)

        ZTADV(JROF,JLEV)=ZTADV(JROF,JLEV)+&
         & (PGMV(JROF,JLEV,YT0%MT)-PGMV(JROF,JLEV+1,YT0%MT))&
         & *ZED*PRDELP(JROF,JLEV)  
        ZTADV(JROF,JLEV+1)=ZTADV(JROF,JLEV+1)+&
         & (PGMV(JROF,JLEV,YT0%MT)-PGMV(JROF,JLEV+1,YT0%MT))&
         & *ZED*PRDELP(JROF,JLEV+1)  
      ENDDO
    ENDDO

    IF(LNHDYN)THEN

      IF (LNH_GEOGW) CALL ABOR1(' CPEULDYN: FD vert adv for LNH_GEOGW not coded')

      ! * Pressure departure variable.
      IF ( NPDVAR == 2 .AND. LRWSDLR )THEN
        ! d(rcorpdf)/dt = 0, and grad(rcorpdf)=0,
        ! so some vertical advection terms cancel between here and
        ! gnh_tndlagadiab_spd, and they are omitted at both locations.
        DO JLEV=1,NFLEVG-1
          DO JROF=KST,KPROF
            ZED=PDT*0.5_JPRB*PEVEL(JROF,JLEV)
            ZSPDADV(JROF,JLEV)=ZSPDADV(JROF,JLEV)+&
             & (PGMV(JROF,JLEV,YT0%MSPD)-RC_PD1*RCORPDF(JLEV) &
             & -PGMV(JROF,JLEV+1,YT0%MSPD)+RC_PD1*RCORPDF(JLEV+1))&
             & *ZED*PRDELP(JROF,JLEV)  
            ZSPDADV(JROF,JLEV+1)=ZSPDADV(JROF,JLEV+1)+&
             & (PGMV(JROF,JLEV,YT0%MSPD)-RC_PD1*RCORPDF(JLEV) &
             & -PGMV(JROF,JLEV+1,YT0%MSPD)+RC_PD1*RCORPDF(JLEV+1))&
             & *ZED*PRDELP(JROF,JLEV+1)  
          ENDDO
        ENDDO
      ELSE
        DO JLEV=1,NFLEVG-1
          DO JROF=KST,KPROF
            ZED=PDT*0.5_JPRB*PEVEL(JROF,JLEV)
            ZSPDADV(JROF,JLEV)=ZSPDADV(JROF,JLEV)+&
             & (PGMV(JROF,JLEV,YT0%MSPD)-PGMV(JROF,JLEV+1,YT0%MSPD))&
             & *ZED*PRDELP(JROF,JLEV)  
            ZSPDADV(JROF,JLEV+1)=ZSPDADV(JROF,JLEV+1)+&
             & (PGMV(JROF,JLEV,YT0%MSPD)-PGMV(JROF,JLEV+1,YT0%MSPD))&
             & *ZED*PRDELP(JROF,JLEV+1)  
          ENDDO
        ENDDO
      ENDIF

      ! * Vertical divergence variable.
      IF( NVDVAR == 4 )THEN
        DO JLEV=1,NFLEVG-1
          DO JROF=KST,KPROF
            ZED=PDT*0.5_JPRB*PEVEL(JROF,JLEV)
            ZSVDADV(JROF,JLEV)=ZSVDADV(JROF,JLEV)+&
             & (PGMV(JROF,JLEV,YT0%MSVD)-PNHXT0(JROF,JLEV)-&
             & PGMV(JROF,JLEV+1,YT0%MSVD)+PNHXT0(JROF,JLEV+1)) &
             & *ZED*PRDELP(JROF,JLEV)  
            ZSVDADV(JROF,JLEV+1)=ZSVDADV(JROF,JLEV+1)+&
             & (PGMV(JROF,JLEV,YT0%MSVD)-PNHXT0(JROF,JLEV)-&
             & PGMV(JROF,JLEV+1,YT0%MSVD)+PNHXT0(JROF,JLEV+1)) &
             & *ZED*PRDELP(JROF,JLEV+1)  
          ENDDO
        ENDDO
      ELSE
        DO JLEV=1,NFLEVG-1
          DO JROF=KST,KPROF
            ZED=PDT*0.5_JPRB*PEVEL(JROF,JLEV)
            ZSVDADV(JROF,JLEV)=ZSVDADV(JROF,JLEV)+&
             & (PGMV(JROF,JLEV,YT0%MSVD)-PGMV(JROF,JLEV+1,YT0%MSVD))&
             & *ZED*PRDELP(JROF,JLEV)  
            ZSVDADV(JROF,JLEV+1)=ZSVDADV(JROF,JLEV+1)+&
             & (PGMV(JROF,JLEV,YT0%MSVD)-PGMV(JROF,JLEV+1,YT0%MSVD))&
             & *ZED*PRDELP(JROF,JLEV+1)  
          ENDDO
        ENDDO
      ENDIF

    ENDIF

  ENDIF

  ! * GFL variables:
  ! GFL quantities are scalars with no thermodynamics tendency terms
  ! Only dynamical process for them is advection (hor + ver). Advection 
  ! in 3TL ICI scheme is always evaluated at time instant t and therefore
  ! there is no need to iterate it here

  IF(LVERTFE) THEN

    ! * remark KY: ZEPS can be replaced by a tunable variable in NAMDYN
    !   and YOMDYN or YOMCVER.
    ZEPS=0.25_JPRB

    ! * One first computes [dX/d eta]_l (in array ZDGFDE):
    DO JGFL=1,YGFL%NUMFLDS
      IF(YGFLC(JGFL)%LADV) THEN
        CALL VERDER(NPROMA,KST,KPROF,NFLEVG,&
         & PGFL(1,1,YGFLC(JGFL)%MP),ZDGFDE(1,1,YGFLC(JGFL)%MP))
      ENDIF
    ENDDO

    ! * One computes then the vertical advection, with a vertical filter
    !   on [etadot (d prehyd/d eta)]_l in the high atmosphere.
    DO JLEV=1,NFLEVG
      DO JROF=KST,KPROF
        IF(VBH(JLEV) < 1.E-5_JPRB .AND. JLEV > 1 .AND. JLEV < NFLEVG) THEN
          ZEVEL=(1._JPRB-2*ZEPS)*PEVEL(JROF,JLEV)+ZEPS*(PEVEL(JROF,JLEV-1)+&
           & PEVEL(JROF,JLEV+1))
        ELSE
          ZEVEL=PEVEL(JROF,JLEV)
        ENDIF
        ZED=PDT*ZEVEL*PRDELP(JROF,JLEV)/VFE_RDETAH(JLEV)
        DO JGFL=1,YGFL%NUMFLDS
          IF(YGFLC(JGFL)%LADV) THEN
            PGFLT1(JROF,JLEV,YGFLC(JGFL)%MP1)=&
             & PGFLT1(JROF,JLEV,YGFLC(JGFL)%MP1)-&
             & ZED*ZDGFDE(JROF,JLEV,YGFLC(JGFL)%MP)
          ENDIF
        ENDDO
      ENDDO
    ENDDO

  ELSE

    DO JGFL=1,YGFL%NUMFLDS
      IF(YGFLC(JGFL)%LADV) THEN
        DO JLEV=1,NFLEVG-1
          DO JROF=KST,KPROF
            ZED=PDT*0.5_JPRB*PEVEL(JROF,JLEV)
            PGFLT1(JROF,JLEV,YGFLC(JGFL)%MP1)=&
             & PGFLT1(JROF,JLEV,YGFLC(JGFL)%MP1)+&
             & (PGFL(JROF,JLEV,YGFLC(JGFL)%MP)-&
             &  PGFL(JROF,JLEV+1,YGFLC(JGFL)%MP))&
             & *ZED*PRDELP(JROF,JLEV)  
            PGFLT1(JROF,JLEV+1,YGFLC(JGFL)%MP1)=&
             & PGFLT1(JROF,JLEV+1,YGFLC(JGFL)%MP1)+&
             & (PGFL(JROF,JLEV,YGFLC(JGFL)%MP)-&
             &  PGFL(JROF,JLEV+1,YGFLC(JGFL)%MP))&
             & *ZED*PRDELP(JROF,JLEV+1)  
          ENDDO
        ENDDO
      ENDIF
    ENDDO

  ENDIF

  ! * Additional advection term at the bottom for case "delta m=1":

  LLDPSFI=((NDPSFI == 1).AND.(CDCONF /= 'E').AND.(CDCONF /= 'F') &
   & .AND.(LMPHYS.OR.LEPHYS).AND.(.NOT.LAGPHY))

  IF (LLDPSFI.AND.LVERTFE) THEN
    ! * This case is not yet coded.
    !   The good way to code this option is probably to:
    !   - take properly account of the "delta m=1" correction in
    !     [etadot d prehyd/d eta] at full levels (must be done in
    !     routine CPMVVPS but currently not yet coded for LVERTFE).
    !   - take properly account of the surface value of X
    !     (additionally to the upper air full level values of X),
    !     when computing [dX/deta]_(l=nflevg) in routine VERDER.
    CALL ABOR1(' CPEULDYN: LVERTFE with NDPSFI=1 not yet coded')
  ENDIF

  IF (LLDPSFI.AND.(.NOT.LVERTFE)) THEN

    ! * GMV variables:
    DO JROF=KST,KPROF
      ZED = PDT* PRDELP(JROF,NFLEVG)* PEVEL(JROF,NFLEVG)
      ! * U_surf and V_surf are currently assumed to be zero here
      !   (not consistent with what is done in routine GPUVS)
      !   Must be changed in the future, using the output of
      !   routine GPUVS.
      ZUADV(JROF,NFLEVG) =ZUADV(JROF,NFLEVG) + ZED* PGMV(JROF,NFLEVG,YT0%MU)
      ZVADV(JROF,NFLEVG) =ZVADV(JROF,NFLEVG) + ZED* PGMV(JROF,NFLEVG,YT0%MV)
      ! * T_surf is contained in array PTSOL.
      ZTADV(JROF,NFLEVG) =ZTADV(JROF,NFLEVG) -&
       & ZED* (PTSOL(JROF) - PGMV(JROF,NFLEVG,YT0%MT))  
    ENDDO

    ! * GMV NH variables: for the time being quantities are
    !   assumed to be constant under the full layer l=L,
    !   so there is no additional vertical advection term.
    !   For the pressure departure variable, this assumption
    !   is consistent with what is coded in routine GNHPREH,
    !   but for the vertical divergence variable, this assumption
    !   may create inconsistencies with the lower boundary condition
    !   (gw)_surf = V_surf * grad(Phi_surf).

    ! * Specific humidity:
    DO JROF=KST,KPROF
      ZED = PDT* PRDELP(JROF,NFLEVG)* PEVEL(JROF,NFLEVG)
      ! * Q_surf is contained in array PQSOL.
      PGFLT1(JROF,NFLEVG,YQ%MP1) = PGFLT1(JROF,NFLEVG,YQ%MP1) -&
       & ZED* (PQSOL(JROF) - PGFL(JROF,NFLEVG,YQ%MP))   
    ENDDO

    ! * Other GFL variables: quantity is assumed to be zero at the surface.
    DO JGFL=1,YGFL%NUMFLDS
      IF(JGFL == YQ%MP) CYCLE
      IF(YGFLC(JGFL)%LADV) THEN
        DO JROF=KST,KPROF
          ZED = PDT* PRDELP(JROF,NFLEVG)* PEVEL(JROF,NFLEVG)
          PGFLT1(JROF,NFLEVG,YGFLC(JGFL)%MP1) = &
           & PGFLT1(JROF,NFLEVG,YGFLC(JGFL)%MP1)+&
           & ZED*PGFL(JROF,NFLEVG,YGFLC(JGFL)%MP)  
        ENDDO
      ENDIF
    ENDDO

  ENDIF

ENDIF

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

!*       3.    CASE "LSPRT".
!              -------------

! * Convert gradients of 'TV' to gradients of T if necessary.
!   If no gradient of Q approximate (for EUL NMI)

! GFL here contains  
! quantities valid at time t    during predictor
! quantities valid at time t+dt during corrector

IF (LSPRT) THEN
  CALL GPRCP(NPROMA,KST,KPROF,NFLEVG,PGFL=PGFL,PR=ZR0)
  DO JLEV=1,NFLEVG
    IF(YQ%LCDERS) THEN
      DO JROF=KST,KPROF
        ZTT0L(JROF,JLEV)=RD*(PGMV(JROF,JLEV,YT0%MTL)&
         & -RETV*PGMV(JROF,JLEV,YT0%MT)*PGFL(JROF,JLEV,YQ%MPL))/ZR0(JROF,JLEV)  
        ZTT0M(JROF,JLEV)=RD*(PGMV(JROF,JLEV,YT0%MTM)&
         & -RETV*PGMV(JROF,JLEV,YT0%MT)*PGFL(JROF,JLEV,YQ%MPM))/ZR0(JROF,JLEV)  
      ENDDO
    ELSE
      DO JROF=KST,KPROF
        ZTT0L(JROF,JLEV)=RD*PGMV(JROF,JLEV,YT0%MTL)/ZR0(JROF,JLEV)
        ZTT0M(JROF,JLEV)=RD*PGMV(JROF,JLEV,YT0%MTM)/ZR0(JROF,JLEV)
      ENDDO
    ENDIF
  ENDDO
ELSE
  DO JLEV=1,NFLEVG
    DO JROF=KST,KPROF
      ZTT0L(JROF,JLEV)=PGMV(JROF,JLEV,YT0%MTL)
      ZTT0M(JROF,JLEV)=PGMV(JROF,JLEV,YT0%MTM)
    ENDDO
  ENDDO
ENDIF

!     ------------------------------------------------------------------
!*       4.    HORIZONTAL ADVECTION.
!              ---------------------
!     Due to stability reasons advection is evaluated 
!     at time t in predictor and corrector as well.
!     For advection we use notation A(t) in this routine.
!     A(t) is computed during predictor (SI step) and
!     than stored in GMV structure (PXRHS arrays in this routine)

IF(NCURRENT_ITER==0)THEN 

  IF(LVERCOR)THEN
     
    !JV this I have to check with Karim

    ! * GMV hydrostatic variables:
    DO JLEV=1,NFLEVG
      DO JROF=KST,KPROF
        ZDT=PDT*PVCASRSF(JROF,JLEV)
        ZUADV(JROF,JLEV)=ZUADV(JROF,JLEV)+ZDT* (&
         &  PGMV(JROF,JLEV,YT0%MVOR)*PGMV(JROF,JLEV,YT0%MV)&
         & -PGMV(JROF,JLEV,YT0%MV)*PGMV(JROF,JLEV,YT0%MVL)&
         & -PGMV(JROF,JLEV,YT0%MU)*PGMV(JROF,JLEV,YT0%MUL) )
        ZVADV(JROF,JLEV)=ZVADV(JROF,JLEV)+ZDT* (&
         & -PGMV(JROF,JLEV,YT0%MDIV)*PGMV(JROF,JLEV,YT0%MV)&
         & -PGMV(JROF,JLEV,YT0%MU)*PGMV(JROF,JLEV,YT0%MVL)&
         & +PGMV(JROF,JLEV,YT0%MV)*PGMV(JROF,JLEV,YT0%MUL) )
        ZTADV(JROF,JLEV)=ZTADV(JROF,JLEV)&
         & -ZDT*(ZTT0L(JROF,JLEV)*PGMV(JROF,JLEV,YT0%MU)&
         & +ZTT0M(JROF,JLEV)*PGMV(JROF,JLEV,YT0%MV))  
      ENDDO
    ENDDO

    ! * GMV non-hydrostatic variables:
    IF(LNHDYN)THEN
      CALL ABOR1("CPEULDYN: LVERCOR NOT CODED FOR NH DYNAMICS")
    ENDIF

    ! * GFL variables:
    DO JGFL=1,YGFL%NUMFLDS
      IF(YGFLC(JGFL)%LCDERS .AND. YGFLC(JGFL)%LADV) THEN
        DO JLEV=1,NFLEVG
          DO JROF=KST,KPROF
            ZDT=PDT*PVCASRSF(JROF,JLEV)
            PGFLT1(JROF,JLEV,YGFLC(JGFL)%MP1)=&
             & PGFLT1(JROF,JLEV,YGFLC(JGFL)%MP1)-ZDT* &
             & (PGFL(JROF,JLEV,YGFLC(JGFL)%MPL)*PGMV(JROF,JLEV,YT0%MU)&
             & +PGFL(JROF,JLEV,YGFLC(JGFL)%MPM)*PGMV(JROF,JLEV,YT0%MV))  
          ENDDO
        ENDDO
      ENDIF
    ENDDO

  ELSE !LVERCOR

    ! * GMV hydrostatic variables:
    DO JLEV=1,NFLEVG
      DO JROF=KST,KPROF
        ZUADV(JROF,JLEV)=ZUADV(JROF,JLEV)+PDT* (&
         &  PGMV(JROF,JLEV,YT0%MVOR)*PGMV(JROF,JLEV,YT0%MV)&
         & -PGMV(JROF,JLEV,YT0%MV)*PGMV(JROF,JLEV,YT0%MVL)&
         & -PGMV(JROF,JLEV,YT0%MU)*PGMV(JROF,JLEV,YT0%MUL) )
        ZVADV(JROF,JLEV)=ZVADV(JROF,JLEV)+PDT* (&
         & -PGMV(JROF,JLEV,YT0%MDIV)*PGMV(JROF,JLEV,YT0%MV)&
         & -PGMV(JROF,JLEV,YT0%MU)*PGMV(JROF,JLEV,YT0%MVL)&
         & +PGMV(JROF,JLEV,YT0%MV)*PGMV(JROF,JLEV,YT0%MUL) )
        ZTADV(JROF,JLEV)=ZTADV(JROF,JLEV)&
         & -PDT*(ZTT0L(JROF,JLEV)*PGMV(JROF,JLEV,YT0%MU)&
         & +ZTT0M(JROF,JLEV)*PGMV(JROF,JLEV,YT0%MV))  
      ENDDO
    ENDDO

    ! * GMV non-hydrostatic variables:
    IF(LNHDYN)THEN
      DO JLEV=1,NFLEVG
        DO JROF=KST,KPROF
          ZSPDADV(JROF,JLEV)=ZSPDADV(JROF,JLEV)&
           & -PDT*(PGMV(JROF,JLEV,YT0%MSPDL)*PGMV(JROF,JLEV,YT0%MU)&
           & +PGMV(JROF,JLEV,YT0%MSPDM)*PGMV(JROF,JLEV,YT0%MV))  
        ENDDO
      ENDDO
      IF( NVDVAR == 4 )THEN 
        DO JLEV=1,NFLEVG
          DO JROF=KST,KPROF
            ZSVDADV(JROF,JLEV)=ZSVDADV(JROF,JLEV)&
             & -PDT*((PGMV(JROF,JLEV,YT0%MSVDL)-PGMV(JROF,JLEV,YT0%MNHXL))&
             & *PGMV(JROF,JLEV,YT0%MU)&
             & +(PGMV(JROF,JLEV,YT0%MSVDM)-PGMV(JROF,JLEV,YT0%MNHXM))&
             & *PGMV(JROF,JLEV,YT0%MV))  
          ENDDO
        ENDDO
      ELSE
        DO JLEV=1,NFLEVG
          DO JROF=KST,KPROF
            ZSVDADV(JROF,JLEV)=ZSVDADV(JROF,JLEV)&
             & -PDT*(PGMV(JROF,JLEV,YT0%MSVDL)*PGMV(JROF,JLEV,YT0%MU)&
             & +PGMV(JROF,JLEV,YT0%MSVDM)*PGMV(JROF,JLEV,YT0%MV))  
          ENDDO
        ENDDO
      ENDIF
    ENDIF

    ! * GFL variables:
    DO JGFL=1,YGFL%NUMFLDS
      IF(YGFLC(JGFL)%LCDERS .AND. YGFLC(JGFL)%LADV) THEN
        DO JLEV=1,NFLEVG
          DO JROF=KST,KPROF
            PGFLT1(JROF,JLEV,YGFLC(JGFL)%MP1)=&
             & PGFLT1(JROF,JLEV,YGFLC(JGFL)%MP1)-PDT* &
             & (PGFL(JROF,JLEV,YGFLC(JGFL)%MPL)*PGMV(JROF,JLEV,YT0%MU)&
             & +PGFL(JROF,JLEV,YGFLC(JGFL)%MPM)*PGMV(JROF,JLEV,YT0%MV))  
          ENDDO
        ENDDO
      ENDIF
    ENDDO

  ENDIF !LVERCOR

ENDIF

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

!*       5.      SEMI-IMPLICIT CORRECTION
!              -----------------------------

!    Predictor:
!       SI-correction = dt (  L(t-dt) - 2*L(t) )  
!    Corrector:
!       SI-correction = dt (  - L(t+dt) )

!    SI correction is stored in PXSI arrays.
!              -----------------------------

ZBDT=0.0_JPRB
IF (CDCONF=='A'.OR.CDCONF == 'B'.OR.(LPC_FULL.AND.NCURRENT_ITER>0)) THEN
  ZBDT=BETADT
ELSEIF (CDCONF=='E'.OR.CDCONF=='F'.OR.CDCONF=='M') THEN
  ZBDT=0.0_JPRB
ENDIF

!        5.1     Prepare input arrays for SI calculations

! * GMV variables:

IF( NCURRENT_ITER == 0 )THEN

  DO JLEV=1,NFLEVG
    IF (LSIDG) THEN
      DO JROF=KST,KPROF
        ZSID(JROF,JLEV)=PGMV(JROF,JLEV,YT9%MDIV)-2.0_JPRB*&
         & PGMV(JROF,JLEV,YT0%MDIV)
      ENDDO
    ELSE
      DO JROF=KST,KPROF
        ZSID(JROF,JLEV)=(PGMV(JROF,JLEV,YT9%MDIV)-2.0_JPRB*&
         & PGMV(JROF,JLEV,YT0%MDIV))*RSTRET*RSTRET/(PGM(JROF)*PGM(JROF))  
      ENDDO
    ENDIF
    DO JROF=KST,KPROF
      ZSITL(JROF,JLEV)=PGMV(JROF,JLEV,YT9%MTL)-2.0_JPRB*PGMV(JROF,JLEV,YT0%MTL)
      ZSITM(JROF,JLEV)=PGMV(JROF,JLEV,YT9%MTM)-2.0_JPRB*PGMV(JROF,JLEV,YT0%MTM)
    ENDDO
  ENDDO

  IF(LNHDYN)THEN
    DO JLEV=1,NFLEVG
      DO JROF=KST,KPROF
        ZSIPR9(JROF,JLEV)=&
         & PGMV(JROF,JLEV,YT9%MSPD)-2.0_JPRB*PGMV(JROF,JLEV,YT0%MSPD)
        ZSIVD(JROF,JLEV)=&
         & PGMV(JROF,JLEV,YT9%MSVD)-2.0_JPRB*PGMV(JROF,JLEV,YT0%MSVD)
        ZSIPRL(JROF,JLEV)=&
         & PGMV(JROF,JLEV,YT9%MSPDL)-2.0_JPRB*PGMV(JROF,JLEV,YT0%MSPDL)
        ZSIPRM(JROF,JLEV)=&
         & PGMV(JROF,JLEV,YT9%MSPDM)-2.0_JPRB*PGMV(JROF,JLEV,YT0%MSPDM)
      ENDDO
      IF (LNH_GEOGW) THEN
        DO JROF=KST,KPROF
          ZSIT9(JROF,JLEV)=&
           & PGMV(JROF,JLEV,YT9%MT)-2.0_JPRB*PGMV(JROF,JLEV,YT0%MT)
        ENDDO
      ENDIF
    ENDDO
  ENDIF

ELSE

  DO JLEV=1,NFLEVG
    IF (LSIDG) THEN
      DO JROF=KST,KPROF
        ZSID(JROF,JLEV)=-PGMV(JROF,JLEV,YT0%MDIV)
      ENDDO
    ELSE
      DO JROF=KST,KPROF
        ZSID(JROF,JLEV)=&
         & -PGMV(JROF,JLEV,YT0%MDIV)*RSTRET*RSTRET/(PGM(JROF)*PGM(JROF))  
      ENDDO
    ENDIF
    DO JROF=KST,KPROF
      ZSITL(JROF,JLEV)=-PGMV(JROF,JLEV,YT0%MTL)
      ZSITM(JROF,JLEV)=-PGMV(JROF,JLEV,YT0%MTM)
    ENDDO
  ENDDO

  IF(LNHDYN)THEN
    DO JLEV=1,NFLEVG
      DO JROF=KST,KPROF
        ZSIPR9(JROF,JLEV)=-PGMV(JROF,JLEV,YT0%MSPD)
        ZSIVD(JROF,JLEV)=-PGMV(JROF,JLEV,YT0%MSVD)
        ZSIPRL(JROF,JLEV)=-PGMV(JROF,JLEV,YT0%MSPDL)
        ZSIPRM(JROF,JLEV)=-PGMV(JROF,JLEV,YT0%MSPDM)
      ENDDO
      IF (LNH_GEOGW) THEN
        DO JROF=KST,KPROF
          ZSIT9(JROF,JLEV)=-PGMV(JROF,JLEV,YT0%MT)
        ENDDO
      ENDIF
    ENDDO
  ENDIF

ENDIF

! * GMVS variables:
IF( NCURRENT_ITER == 0 )THEN
  DO JROF=KST,KPROF
    ZSISPL(JROF)=PGMVS(JROF,YT9%MSPL)-2.0_JPRB*PGMVS(JROF,YT0%MSPL)
    ZSISPM(JROF)=PGMVS(JROF,YT9%MSPM)-2.0_JPRB*PGMVS(JROF,YT0%MSPM)
  ENDDO
ELSE
  DO JROF=KST,KPROF
    ZSISPL(JROF)=-PGMVS(JROF,YT0%MSPL)
    ZSISPM(JROF)=-PGMVS(JROF,YT0%MSPM)
  ENDDO
ENDIF

!        5.2     Do SI calculations
!                This should match what is in LASSIE/LANHSI

IF(LNHDYN)THEN

  IF (LNH_PDVD) THEN

    CALL SIPTP(NPROMA,1,ZSID(KST,1),ZSIVD(KST,1),&
     & ZSIPR(KST,1),ZSIT(KST,1),ZSISP(KST),IPROFS)  
    CALL SIDD (NPROMA,1,ZSIDL(KST,1),ZSIVD(KST,1),&
     & ZSIPRL(KST,1),ZSITL(KST,1),ZSISPL(KST),IPROFS)  
    CALL SIDD (NPROMA,1,ZSIDM(KST,1),ZSIVD(KST,1),&
     & ZSIPRM(KST,1),ZSITM(KST,1),ZSISPM(KST),IPROFS)  
    CALL SISEVE (NPROMA,1,ZSIPR9(KST,1),ZSIVD(KST,1),IPROFS)
    ZCOEJ=RG*RG/(RD*SITRA)
    DO JLEV=1,NFLEVG
      DO JROF=KST,KPROF
        ZSIVD(JROF,JLEV)=ZCOEJ*ZSIVD(JROF,JLEV)
      ENDDO
    ENDDO

  ELSEIF (LNH_GEOGW) THEN

    CALL SITNU(NPROMA,1,ZSID(KST,1),ZSIT(KST,1),ZSISP(KST),IPROFS)

    ! - Computation of (sivderi+1)*Phi, (sivderi+1).(sivderi)*Phi,
    !   (sivderi+1)*grad(phi), sivderi*(gw), (sivderi+1)*T.

    ! Constant upper BC; lower BC=0.
    ZIN(KST:KPROF,1:NFLEVG)=ZSIPR9(KST:KPROF,1:NFLEVG)
    ZIN(KST:KPROF,0)=ZIN(KST:KPROF,1)
    ZIN(KST:KPROF,NFLEVG+1)=0.0_JPRB
    CALL SIVDERI(IPROFS,2,LDVDERIB=LVFE_DERIB,LDBCS=LVFE_DBCS,LDBCT=LVFE_DBCT,&
     & PGPIN=ZIN(KST,0),PGPOUT=ZW2(KST,0))

    ! Constant upper BC; lower BC=0.
    ZIN(KST:KPROF,1:NFLEVG)=ZSIPRL(KST:KPROF,1:NFLEVG)
    ZIN(KST:KPROF,0)=ZIN(KST:KPROF,1)
    ZIN(KST:KPROF,NFLEVG+1)=0.0_JPRB
    CALL SIVDERI(IPROFS,1,LDVDERIB=LVFE_DERIB,LDBCS=LVFE_DBCS,LDBCT=LVFE_DBCT,&
     & PGPIN=ZIN(KST,0),PGPOUT=ZW1L(KST,0))

    ! Constant upper BC; lower BC=0.
    ZIN(KST:KPROF,1:NFLEVG)=ZSIPRM(KST:KPROF,1:NFLEVG)
    ZIN(KST:KPROF,0)=ZIN(KST:KPROF,1)
    ZIN(KST:KPROF,NFLEVG+1)=0.0_JPRB
    CALL SIVDERI(IPROFS,1,LDVDERIB=LVFE_DERIB,LDBCS=LVFE_DBCS,LDBCT=LVFE_DBCT,&
     & PGPIN=ZIN(KST,0),PGPOUT=ZW1M(KST,0))

    ! Constant upper BC; lower BC=0.
    ZIN(KST:KPROF,1:NFLEVG)=ZSIVD(KST:KPROF,1:NFLEVG)
    ZIN(KST:KPROF,0)=ZIN(KST:KPROF,1)
    ZIN(KST:KPROF,NFLEVG+1)=0.0_JPRB
    CALL SIVDERI(IPROFS,0,LDVDERIB=LVFE_DERIB,LDBCS=LVFE_DBCS,LDBCT=LVFE_DBCT,&
     & PGPIN=ZIN(KST,0),PGPOUT=ZW3(KST,0))

    ! Constant upper BC; constant lower BC.
    ZIN(KST:KPROF,1:NFLEVG)=ZSIT9(KST:KPROF,1:NFLEVG)
    ZIN(KST:KPROF,0)=ZIN(KST:KPROF,1)
    ZIN(KST:KPROF,NFLEVG+1)=ZIN(KST:KPROF,NFLEVG)
    CALL SIVDERI(IPROFS,1,LDVDERIB=LVFE_DERIB,LDBCS=LVFE_DBCS,LDBCT=LVFE_DBCT,&
     & PGPIN=ZIN(KST,0),PGPOUT=ZW4(KST,0))

    ! - Momentum equation.
    DO JLEV=1,NFLEVG
      DO JROF=KST,KPROF
        ZSIDL(JROF,JLEV)=RD*ZSITL(JROF,JLEV)+RD*SITR*ZSISPL(JROF) &
         & +ZW1L(JROF,JLEV)
        ZSIDM(JROF,JLEV)=RD*ZSITM(JROF,JLEV)+RD*SITR*ZSISPM(JROF) &
         & +ZW1M(JROF,JLEV)
      ENDDO
    ENDDO
    ! - Temperature equation.
    ZCOEF=RD*SITR/RCVD
    ZCOEG=1.0_JPRB/(RD*SITRA)
    DO JLEV=1,NFLEVG
      DO JROF=KST,KPROF
        ZSIT(JROF,JLEV)=ZCOEF*(ZSID(JROF,JLEV)-ZCOEG*ZW3(JROF,JLEV))
      ENDDO
    ENDDO
    ! - Phi equation.
    ZCOEH=RD*SITR
    ZCOEI=RCPD/(RD*SITR)
    DO JLEV=1,NFLEVG
      DO JROF=KST,KPROF
        ZSIPR(JROF,JLEV)=-ZCOEH*ZSISP(JROF)+ZCOEH*ZCOEI*ZSIT(JROF,JLEV) &
         & -ZSIVD(JROF,JLEV)
      ENDDO
    ENDDO
    ! - gw equation.
    ZCOEJ=RG*RG/(RD*SITRA)
    DO JLEV=1,NFLEVG
      DO JROF=KST,KPROF
        ZSIVD(JROF,JLEV)=ZCOEJ*(-RD*ZW4(JROF,JLEV)-ZW2(JROF,JLEV))
      ENDDO
    ENDDO

  ENDIF

ELSE

  CALL SITNU(NPROMA,1,ZSID(KST,1),ZSIT(KST,1),ZSISP(KST),IPROFS)
  CALL SIGAM(NPROMA,1,ZSIDL(KST,1),ZSITL(KST,1),ZSISPL(KST),IPROFS)
  CALL SIGAM(NPROMA,1,ZSIDM(KST,1),ZSITM(KST,1),ZSISPM(KST),IPROFS)

ENDIF

IF(LIMPF)THEN
  IF(NCURRENT_ITER==0)THEN
    DO JLEV=1,NFLEVG
      DO JROF=KST,KPROF
        ZSIDL(JROF,JLEV)=ZSIDL(JROF,JLEV)-PRCORI(JROF)* &
         & (PGMV(JROF,JLEV,YT9%MV)-2.0_JPRB*PGMV(JROF,JLEV,YT0%MV))  
        ZSIDM(JROF,JLEV)=ZSIDM(JROF,JLEV)+PRCORI(JROF)* &
         & (PGMV(JROF,JLEV,YT9%MU)-2.0_JPRB*PGMV(JROF,JLEV,YT0%MU))  
      ENDDO
    ENDDO
  ELSE
    DO JLEV=1,NFLEVG
      DO JROF=KST,KPROF
        ZSIDL(JROF,JLEV)=ZSIDL(JROF,JLEV)-PRCORI(JROF)*(-PGMV(JROF,JLEV,YT0%MV))
        ZSIDM(JROF,JLEV)=ZSIDM(JROF,JLEV)+PRCORI(JROF)*(-PGMV(JROF,JLEV,YT0%MU))
      ENDDO
    ENDDO
  ENDIF
ENDIF

! * For "spectral RT" option, adjust semi-implicit term in
!   T-equation to compensate for later multiplication by R/Rd

IF (LSPRT) THEN
  DO JLEV=1,NFLEVG
    DO JROF=KST,KPROF
      ZSIT(JROF,JLEV)=RD*ZSIT(JROF,JLEV)/ZR0(JROF,JLEV)
    ENDDO
  ENDDO
ENDIF

!        5.3     Fill P[X]SIC arrays

DO JLEV=1,NFLEVG
  DO JROF=KST,KPROF
    PB2(JROF,MSLB2USI-1+JLEV)=-ZBDT*ZSIDL(JROF,JLEV)*PDT*ZFM
    PB2(JROF,MSLB2VSI-1+JLEV)=-ZBDT*ZSIDM(JROF,JLEV)*PDT*ZFM
    PB2(JROF,MSLB2TSI-1+JLEV)=-ZBDT*ZSIT(JROF,JLEV)*PDT*ZFM
  ENDDO
ENDDO

IF(LNHDYN)THEN
  DO JLEV=1,NFLEVG
    DO JROF=KST,KPROF
      PB2(JROF,MSLB2PDSI-1+JLEV)=-ZBDT*ZSIPR(JROF,JLEV)*PDT*ZFM
      PB2(JROF,MSLB2VDSI-1+JLEV)=-ZBDT*ZSIVD(JROF,JLEV)*PDT*ZFM
    ENDDO
  ENDDO
ENDIF

DO JROF=KST,KPROF
  PB2(JROF,MSLB2SPSI)= -ZBDT*ZSISP(JROF)*PDT*ZFM
ENDDO

!        5.4     LPC_OLD: update PD3Y0

! * Y term combination: Y+ first guess to enter iterations
IF( LNHDYN.AND.LPC_OLD )THEN
  DO JLEV=1,NFLEVG
    DO JROF=KST,KPROF
      PD3Y0(JROF,JLEV)=2.0_JPRB*PD3Y0(JROF,JLEV)-PD3Y9(JROF,JLEV)
    ENDDO
  ENDDO
ENDIF

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

!*       6.    SEMI-IMPLICIT FOR PC SCHEME ONLY
!              ---------------------------------

!    Predictor:
!       SI-PC-correction = dt  L(t)  
!    Corrector:
!       SI-PC-correction = dt  L(t+dt)

!    SI correction is stored in ZXSI0 array.
!    This quantity is computed to compute nonlinear residual
!    R(t) = dt ( M(t) - L(t) ) resp. R(t+dt) = dt ( M(t+dt) - L(t+dt) ).

!    During predictor R(t) is stored into GMV structure and it is used
!    then as R(t-dt) next time step. Asseline filter was applied 
!    to get correct value of R(t-dt).
!              ---------------------------------

IF( LPC_FULL )THEN

  !      6.1     Prepare input arrays for SI calculations

  DO JLEV=1,NFLEVG
    IF (LSIDG) THEN
      DO JROF=KST,KPROF
        ZSID(JROF,JLEV)=PGMV(JROF,JLEV,YT0%MDIV)
      ENDDO
    ELSE
      DO JROF=KST,KPROF
        ZSID(JROF,JLEV)=&
         & PGMV(JROF,JLEV,YT0%MDIV)*RSTRET*RSTRET/(PGM(JROF)*PGM(JROF))  
      ENDDO
    ENDIF
    DO JROF=KST,KPROF
      ZSITL(JROF,JLEV)=PGMV(JROF,JLEV,YT0%MTL)
      ZSITM(JROF,JLEV)=PGMV(JROF,JLEV,YT0%MTM)
    ENDDO
  ENDDO

  DO JROF=KST,KPROF
    ZSISPL(JROF)=PGMVS(JROF,YT0%MSPL)
    ZSISPM(JROF)=PGMVS(JROF,YT0%MSPM)
  ENDDO

  IF(LNHDYN)THEN
    DO JLEV=1,NFLEVG
      DO JROF=KST,KPROF
        ZSIPR9(JROF,JLEV)=PGMV(JROF,JLEV,YT0%MSPD)
        ZSIVD(JROF,JLEV)=PGMV(JROF,JLEV,YT0%MSVD)
        ZSIPRL(JROF,JLEV)=PGMV(JROF,JLEV,YT0%MSPDL)
        ZSIPRM(JROF,JLEV)=PGMV(JROF,JLEV,YT0%MSPDM)
      ENDDO
      IF (LNH_GEOGW) THEN
        DO JROF=KST,KPROF
          ZSIT9(JROF,JLEV)=PGMV(JROF,JLEV,YT0%MT)
        ENDDO
      ENDIF
    ENDDO
  ENDIF

  !      6.2     Do SI calculations
  !              This should match what is in LASSIE/LANHSI

  IF(LNHDYN)THEN

    IF (LNH_PDVD) THEN

      CALL SIPTP(NPROMA,1,ZSID(KST,1),ZSIVD(KST,1),&
       & ZSIPR(KST,1),ZSIT(KST,1),ZSISP(KST),IPROFS)  
      CALL SIDD (NPROMA,1,ZSIDL(KST,1),ZSIVD(KST,1),&
       & ZSIPRL(KST,1),ZSITL(KST,1),ZSISPL(KST),IPROFS)  
      CALL SIDD (NPROMA,1,ZSIDM(KST,1),ZSIVD(KST,1),&
       & ZSIPRM(KST,1),ZSITM(KST,1),ZSISPM(KST),IPROFS)  
      CALL SISEVE (NPROMA,1,ZSIPR9(KST,1),ZSIVD(KST,1),IPROFS)
      ZCOEJ=RG*RG/(RD*SITRA)
      DO JLEV=1,NFLEVG
        DO JROF=KST,KPROF
          ZSIVD(JROF,JLEV)=ZCOEJ*ZSIVD(JROF,JLEV)
        ENDDO
      ENDDO

    ELSEIF (LNH_GEOGW) THEN

      CALL SITNU(NPROMA,1,ZSID(KST,1),ZSIT(KST,1),ZSISP(KST),IPROFS)
      ! - Computation of (sivderi+1)*Phi, (sivderi+1).(sivderi)*Phi,
      !   (sivderi+1)*grad(phi), sivderi*(gw), (sivderi+1)*T.

      ! Constant upper BC; lower BC=0.
      ZIN(KST:KPROF,1:NFLEVG)=ZSIPR9(KST:KPROF,1:NFLEVG)
      ZIN(KST:KPROF,0)=ZIN(KST:KPROF,1)
      ZIN(KST:KPROF,NFLEVG+1)=0.0_JPRB
      CALL SIVDERI(IPROFS,2,&
       & LDVDERIB=LVFE_DERIB,LDBCS=LVFE_DBCS,LDBCT=LVFE_DBCT, &
       & PGPIN=ZIN(KST,0),PGPOUT=ZW2(KST,0))

      ! Constant upper BC; lower BC=0.
      ZIN(KST:KPROF,1:NFLEVG)=ZSIPRL(KST:KPROF,1:NFLEVG)
      ZIN(KST:KPROF,0)=ZIN(KST:KPROF,1)
      ZIN(KST:KPROF,NFLEVG+1)=0.0_JPRB
      CALL SIVDERI(IPROFS,1,&
       & LDVDERIB=LVFE_DERIB,LDBCS=LVFE_DBCS,LDBCT=LVFE_DBCT, &
       & PGPIN=ZIN(KST,0),PGPOUT=ZW1L(KST,0))

      ! Constant upper BC; lower BC=0.
      ZIN(KST:KPROF,1:NFLEVG)=ZSIPRM(KST:KPROF,1:NFLEVG)
      ZIN(KST:KPROF,0)=ZIN(KST:KPROF,1)
      ZIN(KST:KPROF,NFLEVG+1)=0.0_JPRB
      CALL SIVDERI(IPROFS,1,&
       & LDVDERIB=LVFE_DERIB,LDBCS=LVFE_DBCS,LDBCT=LVFE_DBCT, &
       & PGPIN=ZIN(KST,0),PGPOUT=ZW1M(KST,0))

      ! Constant upper BC; lower BC=0.
      ZIN(KST:KPROF,1:NFLEVG)=ZSIVD(KST:KPROF,1:NFLEVG)
      ZIN(KST:KPROF,0)=ZIN(KST:KPROF,1)
      ZIN(KST:KPROF,NFLEVG+1)=0.0_JPRB
      CALL SIVDERI(IPROFS,0,&
       & LDVDERIB=LVFE_DERIB,LDBCS=LVFE_DBCS,LDBCT=LVFE_DBCT, &
       & PGPIN=ZIN(KST,0),PGPOUT=ZW3(KST,0))

      ! Constant upper BC; constant lower BC.
      ZIN(KST:KPROF,1:NFLEVG)=ZSIT9(KST:KPROF,1:NFLEVG)
      ZIN(KST:KPROF,0)=ZIN(KST:KPROF,1)
      ZIN(KST:KPROF,NFLEVG+1)=ZIN(KST:KPROF,NFLEVG)
      CALL SIVDERI(IPROFS,1,&
       & LDVDERIB=LVFE_DERIB,LDBCS=LVFE_DBCS,LDBCT=LVFE_DBCT, &
       & PGPIN=ZIN(KST,0),PGPOUT=ZW4(KST,0))

      ! - Momentum equation.
      DO JLEV=1,NFLEVG
        DO JROF=KST,KPROF
          ZSIDL(JROF,JLEV)=RD*ZSITL(JROF,JLEV)+RD*SITR*ZSISPL(JROF) &
           & +ZW1L(JROF,JLEV)
          ZSIDM(JROF,JLEV)=RD*ZSITM(JROF,JLEV)+RD*SITR*ZSISPM(JROF) &
           & +ZW1M(JROF,JLEV)
        ENDDO
      ENDDO
      ! - Temperature equation.
      ZCOEF=RD*SITR/RCVD
      ZCOEG=1.0_JPRB/(RD*SITRA)
      DO JLEV=1,NFLEVG
        DO JROF=KST,KPROF
          ZSIT(JROF,JLEV)=ZCOEF*(ZSID(JROF,JLEV)-ZCOEG*ZW3(JROF,JLEV))
        ENDDO
      ENDDO
      ! - Phi equation.
      ZCOEH=RD*SITR
      ZCOEI=RCPD/(RD*SITR)
      DO JLEV=1,NFLEVG
        DO JROF=KST,KPROF
          ZSIPR(JROF,JLEV)=-ZCOEH*ZSISP(JROF)+ZCOEH*ZCOEI*ZSIT(JROF,JLEV) &
           & -ZSIVD(JROF,JLEV)
        ENDDO
      ENDDO
      ! - gw equation.
      ZCOEJ=RG*RG/(RD*SITRA)
      DO JLEV=1,NFLEVG
        DO JROF=KST,KPROF
          ZSIVD(JROF,JLEV)=ZCOEJ*(-RD*ZW4(JROF,JLEV)-ZW2(JROF,JLEV))
        ENDDO
      ENDDO

    ENDIF

  ELSE

    CALL SITNU(NPROMA,1,ZSID(KST,1),ZSIT(KST,1),ZSISP(KST),IPROFS)
    CALL SIGAM(NPROMA,1,ZSIDL(KST,1),ZSITL(KST,1),ZSISPL(KST),IPROFS)
    CALL SIGAM(NPROMA,1,ZSIDM(KST,1),ZSITM(KST,1),ZSISPM(KST),IPROFS)

  ENDIF

  IF (LIMPF) THEN
    DO JLEV=1,NFLEVG
      DO JROF=KST,KPROF
        ZSIDL(JROF,JLEV)=ZSIDL(JROF,JLEV)-PRCORI(JROF)*PGMV(JROF,JLEV,YT0%MV)  
        ZSIDM(JROF,JLEV)=ZSIDM(JROF,JLEV)+PRCORI(JROF)*PGMV(JROF,JLEV,YT0%MU)  
      ENDDO
    ENDDO
  ENDIF   

  ! * For "spectral RT" option, adjust semi-implicit term in
  !   T-equation to compensate for later multiplication by R/Rd

  IF (LSPRT) THEN
    DO JLEV=1,NFLEVG
      DO JROF=KST,KPROF
        ZSIT(JROF,JLEV)=RD*ZSIT(JROF,JLEV)/ZR0(JROF,JLEV)
      ENDDO
    ENDDO
  ENDIF

  !      6.3     Fill Z[X]SI0 arrays

  DO JLEV=1,NFLEVG
    DO JROF=KST,KPROF
      ZUSI0(JROF,JLEV)=-ZBDT*ZSIDL(JROF,JLEV)*PDT*ZFM
      ZVSI0(JROF,JLEV)=-ZBDT*ZSIDM(JROF,JLEV)*PDT*ZFM
      ZTSI0(JROF,JLEV)=-ZBDT*ZSIT(JROF,JLEV)*PDT*ZFM
    ENDDO
  ENDDO
    
  IF(LNHDYN)THEN
    DO JLEV=1,NFLEVG
      DO JROF=KST,KPROF
        ZSPDSI0(JROF,JLEV)=-ZBDT*ZSIPR(JROF,JLEV)*PDT*ZFM
        ZSVDSI0(JROF,JLEV)=-ZBDT*ZSIVD(JROF,JLEV)*PDT*ZFM
      ENDDO
    ENDDO
  ENDIF
  DO JROF=KST,KPROF
    ZSPSI0(JROF)= -ZBDT*ZSISP(JROF)*PDT*ZFM
  ENDDO

ENDIF

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

!*       7.    NONLINEAR TENDENCY AT  TIME T
!              -----------------------------

!    Nonlinear model M(t) is computed (advection is not included).
!    The coriolis term, curvature terms are considered here in momentum
!    nonlinear tendency (it was a part of horizontal advection in
!    the previous version of cpdyn and gnhdyn)


!        7.1   U,V, surface pressure (same code for HYD and NH):

! * Add pressure gradient term in the horizontal wind equation.
! * Add coriolis force and curvature terms
IF (LVERCOR) THEN

  ! convert semi-reduced pressure gradient term into geographic pressure
  ! gradient term before adding by multiplying it by "a/rs".
  DO JLEV=1,NFLEVG
    DO JROF=KST,KPROF
      ZUM(JROF,JLEV)=ZFM*PDT*(&
       & PVCASRSF(JROF,JLEV)*(&
       & -PSGRTL(JROF,JLEV)+PKENE(JROF,JLEV)*PRATATX(JROF)&
       & -ZUSA*PVCWF(JROF,JLEV)*PGMV(JROF,JLEV,YT0%MU))&
       & +PRCORI(JROF)*PGMV(JROF,JLEV,YT0%MV)&
       & -PGNORDM(JROF)*PRCORIC(JROF)*PVCWF(JROF,JLEV))
      ZVM(JROF,JLEV)=ZFM*PDT*(&
       & PVCASRSF(JROF,JLEV)*(&
       & -PSGRTM(JROF,JLEV)-PKENE(JROF,JLEV)*PRATATH(JROF)&
       & -ZUSA*PVCWF(JROF,JLEV)*PGMV(JROF,JLEV,YT0%MV))&
       & -PRCORI(JROF)*PGMV(JROF,JLEV,YT0%MU)&
       & +PGNORDL(JROF)*PRCORIC(JROF)*PVCWF(JROF,JLEV))
    ENDDO
  ENDDO

ELSEIF (LRWSDLW.AND.(.NOT.LRWSDLR)) THEN

  DO JLEV=1,NFLEVG
    DO JROF=KST,KPROF
      ZUM(JROF,JLEV)=ZFM*PDT*(&
       & (&
       & -PSGRTL(JROF,JLEV)+PKENE(JROF,JLEV)*PRATATX(JROF)&
       & -ZUSA*PWDLWF(JROF,JLEV)*PGMV(JROF,JLEV,YT0%MU))&
       & +PRCORI(JROF)*PGMV(JROF,JLEV,YT0%MV)&
       & -PGNORDM(JROF)*PRCORIC(JROF)*PWDLWF(JROF,JLEV))
      ZVM(JROF,JLEV)=ZFM*PDT*(&
       & (&
       & -PSGRTM(JROF,JLEV)-PKENE(JROF,JLEV)*PRATATH(JROF)&
       & -ZUSA*PWDLWF(JROF,JLEV)*PGMV(JROF,JLEV,YT0%MV))&
       & -PRCORI(JROF)*PGMV(JROF,JLEV,YT0%MU)&
       & +PGNORDL(JROF)*PRCORIC(JROF)*PWDLWF(JROF,JLEV))
    ENDDO
  ENDDO

ELSEIF (LRWSDLW.AND.LRWSDLR) THEN
  ! the same as (LRWSDLW.AND.(.NOT.LRWSDLR)) but with the metric term a/r.
  CALL ABOR1(' CPEULDYN: LRWSDLW.AND.LRWSDLR not yet coded')
ELSE

  DO JLEV=1,NFLEVG
    DO JROF=KST,KPROF
      ZUM(JROF,JLEV) = ZFM*PDT*(-PSGRTL(JROF,JLEV) &
       & +PRCORI(JROF)*PGMV(JROF,JLEV,YT0%MV)&
       & +PKENE(JROF,JLEV)*PRATATX(JROF))
      ZVM(JROF,JLEV) = ZFM*PDT*(-PSGRTM(JROF,JLEV) &
       & -PRCORI(JROF)*PGMV(JROF,JLEV,YT0%MU)&
       & -PKENE(JROF,JLEV)*PRATATH(JROF) )
    ENDDO
  ENDDO

ENDIF

! * Add Rayleigh friction in the horizontal wind equation.
IF(LRFRIC) THEN
  DO JLEV=1,NFLEVG
    DO JROF=KST,KPROF
      ZUM(JROF,JLEV)=ZUM(JROF,JLEV)-ZFM*PDT* RKRF(JLEV)*PGMV(JROF,JLEV,YT0%MU)
    ENDDO
  ENDDO
ENDIF

! * Add the vertical integral of divergence with the "lrubc" and "delta m=1"
!   contributions in the continuity equation.
IF (LVERCOR) THEN
  ! the vertical integral of divergence has to be multiplied by
  ! "a**2/rs[surf]**2" before adding.
  DO JROF=KST,KPROF
    ZSPM(JROF)=-ZFM*PDT*PVCASRSH(JROF,NFLEVG)*PVCASRSH(JROF,NFLEVG)&
     & *PSDVBC(JROF,NFLEVG)/PRES0(JROF,NFLEVG)  
  ENDDO
ELSE
  DO JROF=KST,KPROF
    ZSPM(JROF)=-ZFM*PDT*PSDVBC(JROF,NFLEVG)/PRES0(JROF,NFLEVG)  
  ENDDO
ENDIF 


!        7.2   Temperature and NH variables:

IF(LNHDYN)THEN

  DO JLEV=1,NFLEVG
    DO JROF=KST,KPROF
      ZCPCV(JROF) = 1.0_JPRB/(1.0_JPRB-PKAP(JROF,JLEV))
      ZRCV(JROF)  = ZCPCV(JROF) - 1.0_JPRB
      ZTM(JROF,JLEV) =&
       & -ZFM*PDT*ZRCV(JROF)*PGMV(JROF,JLEV,YT0%MT)*P3DIVG(JROF,JLEV)
      ZSPDM(JROF,JLEV) = ZFM*PDT*PTNDPD(JROF,JLEV)
      ZSVDM(JROF,JLEV) = ZFM*PDT*PTNDVD(JROF,JLEV)
    ENDDO
  ENDDO

ELSE

  ! * Add conversion term in the temperature equation.
  DO JLEV=1,NFLEVG
    DO JROF=KST,KPROF
      ZTM(JROF,JLEV)=&
       & ZFM*PDT*PKAP(JROF,JLEV)*PGMV(JROF,JLEV,YT0%MT)*PVVEL(JROF,JLEV)  
    ENDDO
  ENDDO

ENDIF

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

!*       8.    TEMPORAL ADVANCE
!              ----------------

!     This part of code is now very similar to LATTEX_DNT resp. LATTES dataflow.
!     Here we mix precomputed or stored quantities to produce RHS of system.

!     P/C formulations is
      
!     (I - dt L) X(t+dt(n)) =  X(t-dt) + 2dt A(t) + 2 dt M(t)
!                  + dt ( L(t-dt) - 2 L(t) ) +
!                  + dt ( R(t+dt(n-1)) + R(t-dt) - 2 R(t) ) + phys_tendency  

!     M = nonlinear model 
!     L = linear model    
!     R = (M-L) = iterated nonlinear residual
!     X = vector of prognostic variables

!     During predictor we precompute quantity PXRHS such that the scheme can be
!     than written as

!     (I - dt L) X(t+dt(n)) = PXRHS + dt ( R(t+dt(n-1))

!     The problematic term here is R(t-dt). To avoid coplete computation of
!     M(t-dt) we rather use 2 arrays in GMV structure to transfer already
!     computed M(t) into M(t-dt). Those arrays are PXNLT9 and PCXNLT9 and
!     they are 2 due to need to perform Asseline filter on M in a same way
!     as it is done for X. This is to assure consistency between X and M.
!    ---------------------------------------------------------------------

! nonlinear residual R=dt(M-L)
IF( LPC_FULL )THEN
  DO JLEV=1,NFLEVG
    DO JROF=KST,KPROF
      ZURES0(JROF,JLEV)=ZUM(JROF,JLEV)-ZUSI0(JROF,JLEV)
      ZVRES0(JROF,JLEV)=ZVM(JROF,JLEV)-ZVSI0(JROF,JLEV)
      ZTRES0(JROF,JLEV)=ZTM(JROF,JLEV)-ZTSI0(JROF,JLEV)
    ENDDO
  ENDDO

  IF(LNHDYN)THEN
    DO JLEV=1,NFLEVG
      DO JROF=KST,KPROF
        ZSPDRES0(JROF,JLEV)=ZSPDM(JROF,JLEV)-ZSPDSI0(JROF,JLEV)
        ZSVDRES0(JROF,JLEV)=ZSVDM(JROF,JLEV)-ZSVDSI0(JROF,JLEV)
      ENDDO
    ENDDO
  ENDIF
  DO JROF=KST,KPROF
    ZSPRES0(JROF)=ZSPM(JROF)-ZSPSI0(JROF)
  ENDDO
ENDIF ! LPC_FULL

! second of time filter
IF( LPC_FULL.AND.NCURRENT_ITER == 0 )THEN
  CALL GPTF2PC(&
   ! --- INPUT --------------------------------------------------
   & KST,KPROF,&
   & PGMV(1,1,YT9%MUNL),PGMV(1,1,YT9%MVNL),PGMV(1,1,YT9%MTNL),&
   & PGMV(1,1,YT9%MSPDNL),PGMV(1,1,YT9%MVWVNL),&
   & PGMVS(1,YT9%MSPNL) ,PGMV(1,1,YT9%MNHXNL),&
   & ZURES0  ,ZVRES0   ,ZTRES0  ,ZSPDRES0  ,ZSVDRES0  ,&
   & ZSPRES0 ,PNHXT0    ,&
   ! --- OUTPUT --------------------------------------------------
   & PGMV(1,1,YT9%MCUNL),PGMV(1,1,YT9%MCVNL),PGMV(1,1,YT9%MCTNL),&
   & PGMV(1,1,YT9%MCSPDNL),PGMV(1,1,YT9%MCVWVNL),&
   & PGMVS(1,YT9%MCSPNL),PGMV(1,1,YT9%MCNHXNL))
ENDIF

! 3D arrays
IF( LPC_FULL.AND.NCURRENT_ITER == 0 )THEN

  ! right hand side of corrector step (this quantity is used
  ! during corrector and is overwritten at the beginnig of next
  ! time step) 
  DO JLEV=1,NFLEVG
    DO JROF=KST,KPROF
      PGMV(JROF,JLEV,YT9%MCURHS) =&
       & PGMVT1(JROF,JLEV,YT1%MU)+ZUADV(JROF,JLEV) + ZFTEND*( &
       & 2.0_JPRB*ZUM(JROF,JLEV)&     ! 2 dt M(t)
       &+ PB2(JROF,MSLB2USI-1+JLEV) & ! dt(L(t-dt) - 2 L(t))
       &+ PGMV(JROF,JLEV,YT9%MCUNL) &
       &- 2.0_JPRB*ZURES0(JROF,JLEV) &! dt( R(t-dt) - 2 R(t))
       & )
      PGMV(JROF,JLEV,YT9%MCVRHS) =&
       & PGMVT1(JROF,JLEV,YT1%MV)+ZVADV(JROF,JLEV) + ZFTEND*( &
       & 2.0_JPRB*ZVM(JROF,JLEV)&     ! 2 dt M(t)
       &+ PB2(JROF,MSLB2VSI-1+JLEV) & ! dt(L(t-dt) - 2 L(t))
       &+ PGMV(JROF,JLEV,YT9%MCVNL) &
       &- 2.0_JPRB*ZVRES0(JROF,JLEV) &! dt( R(t-dt) - 2 R(t))
       & )
      PGMV(JROF,JLEV,YT9%MCTRHS) =&
       & PGMVT1(JROF,JLEV,YT1%MT)+ZTADV(JROF,JLEV) + ZFTEND*( &
       & 2.0_JPRB*ZTM(JROF,JLEV)&     ! 2 dt M(t)
       &+ PB2(JROF,MSLB2TSI-1+JLEV) & ! dt(L(t-dt) - 2 L(t))
       &+ PGMV(JROF,JLEV,YT9%MCTNL) &
       &- 2.0_JPRB*ZTRES0(JROF,JLEV) &! dt( R(t-dt) - 2 R(t))
       & )
    ENDDO
  ENDDO

  IF(LNHDYN)THEN
    DO JLEV=1,NFLEVG
      DO JROF=KST,KPROF
        PGMV(JROF,JLEV,YT9%MCSPDRHS) =&
         & PGMVT1(JROF,JLEV,YT1%MSPD)+ZSPDADV(JROF,JLEV) + ZFTEND*( &
         & 2.0_JPRB*ZSPDM(JROF,JLEV)&     ! 2 dt M(t)
         &+ PB2(JROF,MSLB2PDSI-1+JLEV) &  ! dt(L(t-dt) - 2 L(t))
         &+ PGMV(JROF,JLEV,YT9%MCSPDNL) &
         &- 2.0_JPRB*ZSPDRES0(JROF,JLEV) &! dt( R(t-dt) - 2 R(t))
         & )
        PGMV(JROF,JLEV,YT9%MCSVDRHS) =&
         & PGMVT1(JROF,JLEV,YT1%MSVD)+ZSVDADV(JROF,JLEV) + ZFTEND*( &
         & 2.0_JPRB*ZSVDM(JROF,JLEV)&     ! 2 dt M(t)
         &+ PB2(JROF,MSLB2VDSI-1+JLEV) &  ! dt(L(t-dt) - 2 L(t))
         &+ PGMV(JROF,JLEV,YT9%MCVWVNL) &
         &- 2.0_JPRB*ZSVDRES0(JROF,JLEV) &! dt( R(t-dt) - 2 R(t))
         & )
      ENDDO
    ENDDO
  ENDIF
                        
ENDIF ! Predictor of LPC_FULL


! ordinary time step 
IF( NCURRENT_ITER == 0 )THEN
  DO JLEV=1,NFLEVG
    DO JROF=KST,KPROF
      PGMVT1(JROF,JLEV,YT1%MU)=PGMVT1(JROF,JLEV,YT1%MU)&
       & +2.0_JPRB*ZFTEND*ZUM(JROF,JLEV)+ZUADV(JROF,JLEV)
      PGMVT1(JROF,JLEV,YT1%MV)=PGMVT1(JROF,JLEV,YT1%MV)&
       & +2.0_JPRB*ZFTEND*ZVM(JROF,JLEV)+ZVADV(JROF,JLEV)
      PGMVT1(JROF,JLEV,YT1%MT)=PGMVT1(JROF,JLEV,YT1%MT)&
       & +2.0_JPRB*ZFTEND*ZTM(JROF,JLEV)+ZTADV(JROF,JLEV)
    ENDDO
  ENDDO
  IF(LNHDYN)THEN
    DO JLEV=1,NFLEVG
      DO JROF=KST,KPROF
        PGMVT1(JROF,JLEV,YT1%MSPD)=PGMVT1(JROF,JLEV,YT1%MSPD)&
         & +2.0_JPRB*ZFTEND*ZSPDM(JROF,JLEV)+ZSPDADV(JROF,JLEV)
        PGMVT1(JROF,JLEV,YT1%MSVD)=PGMVT1(JROF,JLEV,YT1%MSVD)&
         & +2.0_JPRB*ZFTEND*ZSVDM(JROF,JLEV)+ZSVDADV(JROF,JLEV)
      ENDDO
    ENDDO
  ENDIF
ENDIF

! 2D arrays
IF( LPC_FULL.AND.NCURRENT_ITER==0 )THEN
  DO JROF=KST,KPROF
    PGMVS(JROF,YT9%MCSPRHS) = PGMVT1S(JROF,YT1%MSP) + ZFTEND*( &
     & 2.0_JPRB*ZSPM(JROF)&     ! 2 dt M(t)
     &+ PB2(JROF,MSLB2SPSI) &   ! dt(L(t-dt) - 2 L(t))
     &+ PGMVS(JROF,YT9%MCSPNL) &
     &- 2.0_JPRB*ZSPRES0(JROF) &! dt( R(t-dt) - 2 R(t))
     & )
  ENDDO
ENDIF

! first part of time filter 
IF( LPC_FULL.AND.NCURRENT_ITER == 0)THEN
  CALL GPTF1PC(&
   ! --- INPUT ---------------------------------------------------------
   & KST,KPROF,&
   & PGMV(1,1,YT9%MCUNL),PGMV(1,1,YT9%MCVNL),PGMV(1,1,YT9%MCTNL),&
   & PGMV(1,1,YT9%MCSPDNL),PGMV(1,1,YT9%MCVWVNL),&
   & PGMVS(1,YT9%MCSPNL),PGMV(1,1,YT9%MCNHXNL),&
   & ZURES0  ,ZVRES0   ,ZTRES0  ,ZSPDRES0  ,ZSVDRES0  ,&
   & ZSPRES0 ,PNHXT0    ,&
   ! --- OUTPUT --------------------------------------------------
   & PGMV(1,1,YT9%MUNL),PGMV(1,1,YT9%MVNL),PGMV(1,1,YT9%MTNL),&
   & PGMV(1,1,YT9%MSPDNL),PGMV(1,1,YT9%MVWVNL),&
   & PGMVS(1,YT9%MSPNL) ,PGMV(1,1,YT9%MNHXNL))
ENDIF

! ordinary time step
IF( NCURRENT_ITER==0 )THEN
  DO JROF=KST,KPROF
    PGMVT1S(JROF,YT1%MSP)=PGMVT1S(JROF,YT1%MSP)+2.0_JPRB*ZFTEND*ZSPM(JROF)
  ENDDO
ENDIF

! corrector time step
IF( LPC_FULL.AND.NCURRENT_ITER > 0) THEN
  DO JLEV=1,NFLEVG
    DO JROF=KST,KPROF
      PGMVT1(JROF,JLEV,YT1%MU)=&
       & PGMV(JROF,JLEV,YT9%MCURHS)+ZFTEND*ZURES0(JROF,JLEV) 
      PGMVT1(JROF,JLEV,YT1%MV)=&
       & PGMV(JROF,JLEV,YT9%MCVRHS)+ZFTEND*ZVRES0(JROF,JLEV) 
      PGMVT1(JROF,JLEV,YT1%MT)=&
       & PGMV(JROF,JLEV,YT9%MCTRHS)+ZFTEND*ZTRES0(JROF,JLEV) 
    ENDDO
  ENDDO
  IF(LNHDYN)THEN
    DO JLEV=1,NFLEVG
      DO JROF=KST,KPROF
        PGMVT1(JROF,JLEV,YT1%MSPD)=&
         & PGMV(JROF,JLEV,YT9%MCSPDRHS)+ZFTEND*ZSPDRES0(JROF,JLEV) 
        PGMVT1(JROF,JLEV,YT1%MSVD)=&
         & PGMV(JROF,JLEV,YT9%MCSVDRHS)+ZFTEND*ZSVDRES0(JROF,JLEV) 
      ENDDO
    ENDDO
  ENDIF
  DO JROF=KST,KPROF
    PGMVT1S(JROF,YT1%MSP)=PGMVS(JROF,YT9%MCSPRHS)+ ZFTEND*ZSPRES0(JROF)
  ENDDO
ENDIF

! evolution of aux qantities for NH divergence variable d4
IF (LNHDYN.AND.NVDVAR==4) THEN

  IF (LPC_FULL) THEN
    IF( NCURRENT_ITER  == 0 )THEN
      DO JLEV=1,NFLEVG
        DO JROF=KST,KPROF
          PGMVT1(JROF,JLEV,YT1%MNHX)= - PGMV(JROF,JLEV,YT9%MCNHXNL) + &
           & 2.0_JPRB*PNHXT0(JROF,JLEV) 
          PGMVT1(JROF,JLEV,YT1%MSVD)=PGMVT1(JROF,JLEV,YT1%MSVD)+&
           & 2.0_JPRB*(PNHXT0(JROF,JLEV)-PGMV(JROF,JLEV,YT9%MCNHXNL))
        ENDDO
      ENDDO
    ELSE
      DO JLEV=1,NFLEVG
        DO JROF=KST,KPROF
          PGMVT1(JROF,JLEV,YT1%MNHX)=PNHXT0(JROF,JLEV)
          PGMVT1(JROF,JLEV,YT1%MSVD)=PGMVT1(JROF,JLEV,YT1%MSVD)+&
           & PNHXT0(JROF,JLEV)-PGMV(JROF,JLEV,YT9%MCNHXNL)
        ENDDO
      ENDDO
    ENDIF
  ELSE
    ! normal SI step
    DO JLEV=1,NFLEVG
      DO JROF=KST,KPROF
        PGMVT1(JROF,JLEV,YT1%MNHX)= - PNHXT9(JROF,JLEV) + &
         & 2.0_JPRB*PNHXT0(JROF,JLEV) 
        PGMVT1(JROF,JLEV,YT1%MSVD)=PGMVT1(JROF,JLEV,YT1%MSVD)+&
         & 2.0_JPRB*(PNHXT0(JROF,JLEV)-PNHXT9(JROF,JLEV))
      ENDDO
    ENDDO
  ENDIF

ENDIF

! correct SI correction with factor ZFTEND since it is added to the RHS in
! gpendtr routine
DO JLEV=1,NFLEVG
  DO JROF=KST,KPROF
    PB2(JROF,MSLB2USI-1+JLEV)=ZFTEND*PB2(JROF,MSLB2USI-1+JLEV)
    PB2(JROF,MSLB2VSI-1+JLEV)=ZFTEND*PB2(JROF,MSLB2VSI-1+JLEV)
    PB2(JROF,MSLB2TSI-1+JLEV)=ZFTEND*PB2(JROF,MSLB2TSI-1+JLEV)
  ENDDO
ENDDO
IF(LNHDYN)THEN
  DO JLEV=1,NFLEVG
    DO JROF=KST,KPROF
      PB2(JROF,MSLB2PDSI-1+JLEV)=ZFTEND*PB2(JROF,MSLB2PDSI-1+JLEV)
      PB2(JROF,MSLB2VDSI-1+JLEV)=ZFTEND*PB2(JROF,MSLB2VDSI-1+JLEV)
    ENDDO
  ENDDO
ENDIF
DO JROF=KST,KPROF
  PB2(JROF,MSLB2SPSI)=ZFTEND*PB2(JROF,MSLB2SPSI)
ENDDO


! artificially add to the RHS quantity +dt L X(+) 
! PXT1 arrays then contain explicit guess of X+
! It could be used to call lagged physics.
! this quantity is then sustracted in GPENDTR in order to get correct RHS.
IF( LPC_FULL.AND.NCURRENT_ITER > 0 )THEN

  DO JLEV=1,NFLEVG
    DO JROF=KST,KPROF
      PGMVT1(JROF,JLEV,YT1%MU)=&
       & PGMVT1(JROF,JLEV,YT1%MU) - PB2(JROF,MSLB2USI-1+JLEV)
      PGMVT1(JROF,JLEV,YT1%MV)=&
       & PGMVT1(JROF,JLEV,YT1%MV) - PB2(JROF,MSLB2VSI-1+JLEV)
      PGMVT1(JROF,JLEV,YT1%MT)=&
       & PGMVT1(JROF,JLEV,YT1%MT) - PB2(JROF,MSLB2TSI-1+JLEV)
    ENDDO
  ENDDO
  IF(LNHDYN)THEN
    DO JLEV=1,NFLEVG
      DO JROF=KST,KPROF
        PGMVT1(JROF,JLEV,YT1%MSPD)=&
         & PGMVT1(JROF,JLEV,YT1%MSPD) - PB2(JROF,MSLB2PDSI-1+JLEV)
        PGMVT1(JROF,JLEV,YT1%MSVD)=&
         & PGMVT1(JROF,JLEV,YT1%MSVD) - PB2(JROF,MSLB2VDSI-1+JLEV)
      ENDDO
    ENDDO
  ENDIF
  DO JROF=KST,KPROF
    PGMVT1S(JROF,YT1%MSP)=PGMVT1S(JROF,YT1%MSP) - PB2(JROF,MSLB2SPSI)
  ENDDO

ENDIF


! store value of advected GFL array into auxiliary PC scheme buffer
! this has to be done in order to perform on this quantity coupling
! each corrector to have biperiodic array before spectral transforms 

! Here assumption is that content of GFL array is not changed. So, please
! do not use for lagged physics (for that purpose please move this part of the
! code into gpendtr routine).
IF( LPC_FULL )THEN
  IF( NCURRENT_ITER == 0 )THEN
    DO JGFL=1,YGFL%NUMFLDS
      IF(YGFLC(JGFL)%LPC) THEN
        PGFLPC(KST:KPROF,1:NFLEVG,YGFLC(JGFL)%MPPC)=&
         & PGFLT1(KST:KPROF,1:NFLEVG,YGFLC(JGFL)%MP1)
      ENDIF
    ENDDO
  ELSE
    DO JGFL=1,YGFL%NUMFLDS
      IF(YGFLC(JGFL)%LPC) THEN
        PGFLT1(KST:KPROF,1:NFLEVG,YGFLC(JGFL)%MP1)=&
         & PGFLPC(KST:KPROF,1:NFLEVG,YGFLC(JGFL)%MPPC)
      ENDIF
    ENDDO
  ENDIF
ENDIF

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

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