!OPTIONS XOPT(HSFUN)
SUBROUTINE VDFMAINSAD (KIDIA,KFDIA,KLON,KLEV,KLEVS,KSTEP,KTILES,&
 & KTRAC,PTSPHY,KTVL,KTVH,PCVL,PCVH, &
 & PLAIL, PLAIH, PSIGFLT, &
 & PUM15  ,PVM15  ,PTM15  ,PQM15  ,PCM15  ,PAPHM15  ,PGEOM15, PGEOH5,&
 & PTSKM1M5,PTSAM1M5,PWSAM1M5, &
 & PSSRFL5,PSLRFL5 ,PEMIS5 ,&
 & PTHKICE, PSNTICE, &
 & PTSNOW5, PTICE5,&
 ! & PHLICE5, PTLICE5, PTLWML5, &  !lake passive
 & PSST   , KSOTY , PFRTI ,PALBTI5 ,PWLMX5 ,&
 & PCHAR ,PTSKRAD5 ,PCFLX5,&
 ! OUTPUT (TRAJECTORY)           
 & PZ0M5   ,PZ0H5  , &
 & PU10M5  , PV10M5  , PT2M5   , PD2M5   , PQ2M5 ,&
 & P10NU5  , P10NV5  ,&
 ! OUTPUT TENDENCIES (TRAJECTORY)           
 & PTE5   ,PQE5   ,PVOM5  ,PVOL5 ,PTENC5  ,PTSKE15,&
 !-UPDATED FIELDS FOR TILES (TRAJECTORY)
 & PUSTRTI5,PVSTRTI5,PAHFSTI5,PEVAPTI5,PTSKTI5,&
 ! OUTPUT FLUXES (TRAJECTORY)           
 & PDIFTS5,PDIFTQ5,PSTRTU5,PSTRTV5, &
 ! ========         
 & PUM1   ,PVM1   ,PTM1   ,PQM1  ,PCM1  ,PAPHM1   ,PGEOM1, PGEOH,&
 & PTSKM1M,PTSAM1M,PWSAM1M, &
 & PSSRFL,PSLRFL, &
 & PALBTI,PTSKRAD,PCFLX, &
 ! OUTPUT           
 & PZ0M   ,PZ0H   , &
 & PU10M  , PV10M  , PT2M   , PD2M   , PQ2M ,&
 & P10NU  , P10NV  ,&
 ! OUTPUT TENDENIES           
 & PTE    ,PQE    ,PVOM   ,PVOL  ,PTENC  ,PTSKE1, &
 !-UPDATED FIELDS FOR TILES
 & PUSTRTI,PVSTRTI,PAHFSTI,PEVAPTI,PTSKTI,&
 ! OUTPUT FLUXES
 & PDIFTS ,PDIFTQ ,PSTRTU ,PSTRTV)  
!-----------------------------------------------------------------------     

!***

!**   *VDFMAINSAD* - DOES THE VERTICAL EXCHANGE OF U,V,T,Q BY TURBULENCE.
!                   (Adjoint)
!     J.F. MAHFOUF     E.C.M.W.F.         09/05/96

!     Modified AD code

!     M. Janiskova                        11/06/99 (AD for surface tile)
!     M. Janiskova                        13/10/03 (AD of new tile coupling)
!     P. Viterbo                          24/05/2004 (Change surface units)
!     M. Ko"hler                           3/12/2004 (adapted to moist 
!                                                     advection-diffusion)
!     P. Lopez                            02/06/2005 (new nonlinear version for
!                                                     trajectory in adjoint)
!     M. Janiskova                        04/07/05 (Rewritten for externalized
!                                                   surface)
!     A. Beljaars                         10/12/05  TOFD
!     P. Lopez                            13/02/2006 (Added tracer diffusion)
!     G. Balsamo                          15/01/2007 (Added soil type)
!     G. Balsamo                          20/10/2008 (Added lake tile)

!     H. Hersbach                         04/12/2009 (10-m neutral wind)
!     S. Boussetta/G.Balsamo                May 2009 (Added variable LAI)

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

!     adapted from

!     J.F.GELEYN       E.C.M.W.F.         20/04/82.
!     MODIFIED BY
!     C.A.BLONDIN      E.C.M.W.F.         18/12/86.
!     IFS-VERSION (TECHNICAL REVISION OF *CY34*) BY
!     A.C.M. BELJAARS  E.C.M.W.F.         20/10/89.

!     OBUKHOV-L UPDATE      ACMB          26/03/90.

!     MODIFIED BY

!     A.C.M. BELJAARS  E.C.M.W.F.         30/09/98 (SURFACE TILES) 

!     PURPOSE.
!     --------

!          THIS ROUTINE COMPUTES THE PHYSICAL TENDENCIES OF THE FOUR
!     PROGNOSTIC VARIABLES U,V,T AND Q DUE TO THE VERTICAL EXCHANGE BY
!     TURBULENT (= NON-MOIST CONVECTIVE) PROCESSES. THESE TENDENCIES ARE
!     OBTAINED AS THE DIFFERENCE BETWEEN THE RESULT OF AN IMPLICIT
!     TIME-STEP STARTING FROM VALUES AT T-1 AND THESE T-1 VALUES. ALL
!     THE DIAGNOSTIC COMPUTATIONS (EXCHANGE COEFFICIENTS, ...) ARE DONE
!      FROM THE T-1 VALUES. AS A BY-PRODUCT THE ROUGHNESS LENGTH OVER SEA
!     IS UPDATED ACCORDINGLY TO THE *CHARNOCK FORMULA. HEAT AND MOISTURE
!     SURFACE FLUXES AND THEIR DERIVATIVES AGAINST TS, WS AND WL
!     (THE LATTER WILL BE LATER WEIGHTED WITH THE SNOW FACTOR IN
!     *VDIFF*), LATER TO BE USED FOR SOIL PROCESSES TREATMENT, ARE ALSO
!     COMPUTED AS WELL AS A STABILITY VALUE TO BE USED AS A DIAGNOSTIC
!     OF THE DEPTH OF THE WELL MIXED LAYER IN CONVECTIVE COMPUTATIONS.

!     INTERFACE.
!     ----------
!          *VDIFF* TAKES THE MODEL VARIABLES AT T-1 AND RETURNS THE VALUES
!     FOR THE PROGNOSTIC TIME T+1 DUE TO VERTICAL DIFFUSION.
!     THE MODEL VARIABLES, THE MODEL DIMENSIONS AND THE DIAGNOSTICS DATA
!     ARE PASSED AS SUBROUTINE ARGUMENTS. CONSTANTS THAT DO NOT CHANGE
!     DURING A MODEL RUN (E.G. PHYSICAL CONSTANTS, SWITCHES ETC.) ARE
!     STORED IN A SINGLE COMMON BLOCK *YOMVDF*, WHICH IS INITIALIZED
!     BY SET-UP ROUTINE *SUVDF*.

!     PARAMETER     DESCRIPTION                                   UNITS
!     ---------     -----------                                   -----
!     INPUT PARAMETERS (INTEGER):

!    *KIDIA*        START POINT
!    *KFDIA*        END POINT
!    *KLEV*         NUMBER OF LEVELS
!    *KLON*         NUMBER OF GRID POINTS PER PACKET
!    *KLEVS*         NUMBER OF SOIL LAYERS
!    *KSTEP*        CURRENT TIME STEP INDEX
!    *KTILES*       NUMBER OF TILES (I.E. SUBGRID AREAS WITH DIFFERENT
!                   OF SURFACE BOUNDARY CONDITION)
!    *KTRAC*        number of tracers

!    *KTVL*         VEGETATION TYPE FOR LOW VEGETATION FRACTION
!    *KTVH*         VEGETATION TYPE FOR HIGH VEGETATION FRACTION
!    *KSOTY*        SOIL TYPE                                         (1-7)

!     INPUT PARAMETERS (LOGICAL)

!     INPUT PARAMETERS (REAL)
!    *PCVL*         LOW VEGETATION COVER                          -  
!    *PCVH*         HIGH VEGETATION COVER                         -  
!    *PLAIL*        LOW VEGETATION LAI                            m2/m2
!    *PLAIH*        HIGH VEGETATION LAI                           m2/m2
!    *PSIGFLT*      STANDARD DEVIATION OF FILTERED OROGRAPHY      M

!    *PTSPHY*       TIME STEP FOR THE PHYSICS

!*      INPUT PARAMETERS AT T-1 OR CONSTANT IN TIME (REAL):
!  Trajectory  Perturbation  Description                               Unit
!  PUM15       PUM1          X-VELOCITY COMPONENT                      m/s
!  PVM15       PVM1          Y-VELOCITY COMPONENT                      m/s
!  PTM15       PTM1          TEMPERATURE                               K
!  PQM15       PQM1          SPECIFIC HUMIDITY                         kg/kg
!  PCM15       PCM1          TRACER CONCENTRATION                      kg/kg
!  PAPHM15     PAPHM1        PRESSURE                                  Pa
!  PGEOM15     PGEOM1        GEOPOTENTIAL                             m**2/s**2
!  PGEOH5      PGEOH         GEOPOTENTIAL AT HALF LEVELS               M2/S2
!  PTSKM1M5    PTSKM1M       SKIN TEMPERATURE                          K
!  PTSKRAD5    PTSKRAD       SKIN TEMPERATURE at last ful radiation    K
!                            time step
!  PTSAM1M5    PTSAM1M       SURFACE TEMPERATURE                       K
!  PWSAM1M5    PWSAM1M       SOIL MOISTURE ALL LAYERS                 m**3/m**3
!  PSSRFL5     PSSRFL        NET SHORTWAVE RADIATION FLUX AT SURFACE   W/m2
!  PSLRFL5     PSLRFL        NET LONGWAVE RADIATION FLUX AT SURFACE    W/M2
!  PEMIS5      ---           MODEL SURFACE LONGWAVE EMISSIVITY         
!  PTHKICE     ---           Sea ice thickness                         m
!  PSNTICE     ---           Thickness of snow layer on sea ice        m
!  PTSNOW5     ---           SNOW TEMPERATURE                          K
!  PTICE5      ---           ICE TEMPERATURE (TOP SLAB)                K
!  PHLICE5     ---           LAKE ICE THICKNESS                        m
!  PTLICE5     ---           LAKE ICE TEMPERATURE                      K
!  PTLWML5     ---           LAKE MIXED LAYER TEMPERATURE              K
!  PSST        ---           (OPEN) SEA SURFACE TEMPERATURE            K
!  PFRTI       ---           TILE FRACTIONS                            (0-1)
!                            1: WATER         5: SNOW ON LOW-VEG+BARE-SOIL
!                            2: ICE           6: DRY SNOW-FREE HIGH-VEG
!                            3: WET SKIN      7: SNOW UNDER HIGH-VEG
!                            4: DRY SNOW-FREE 8: BARE SOIL
!                               LOW-VEG
!  PALBTI5     PALBTI        BROADBAND ALBEDO FOR TILE FRACTIONS       (0-1)
!  PWLMX5      ---           MAXIMUM SKIN RESERVOIR CAPACITY           kg/m**2
!  PCHAR       ---           "EQUIVALENT" CHARNOCK PARAMETER
!  PCFLX5      PCFLX         TRACER SURFACE FLUX                       kg/(m2 s)

!*      UPDATED PARAMETERS (REAL):
!  Trajectory  Perturbation  Description                               Unit
!  PTE5        PTE           TEMPERATURE TENDENCY                      K/s
!  PQE5        PQE           MOISTURE TENDENCY                        kg/(kg s)
!  PVOM5       PVOM          MERIODINAL VELOCITY TENDENCY (DU/DT)      m/s2
!  PVOL5       PVOL          LATITUDE TENDENCY            (DV/DT)      m/s2
!  PTENC5      PTENC         TRACER TENDENCY                           kg/(kg s)
!  PTSKE15     PTSKE1        SKIN TEMPERATURE TENDENCY                 K/s
!  PZ0M5       PZ0M          AERODYNAMIC ROUGHNESS LENGTH              m
!  PZ0H5       PZ0H          ROUGHNESS LENGTH FOR HEAT                 m

!*      UDATED PARAMETERS FOR TILES (REAL):
!  Trajectory  Perturbation  Description                               Unit
!  PUSTRTI5    PUSTRTI       SURFACE U-STRESS                          N/m2
!  PVSTRTI5    PVSTRTI       SURFACE V-STRESS                          N/m2
!  PAHFSTI5    PAHFSTI       SURFACE SENSIBLE HEAT FLUX                W/m2
!  PEVAPTI5    PEVAPTI       SURFACE MOISTURE FLUX                     kg/m2/s
!  PTSKTI5     PTSKTI        SKIN TEMPERATURE                          K

!*      OUTPUT PARAMETERS (REAL):
!  Trajectory  Perturbation  Description                               Unit
!  PSTRTU5     PSTRTU        TURBULENT FLUX OF U-MOMEMTUM       kg*(m/s)/(m2*s)
!  PSTRTV5     PSTRTV        TURBULENT FLUX OF V-MOMEMTUM       kg*(m/s)/(m2*s)
!  PDIFTS5     PDIFTS        TURBULENT FLUX OF HEAT                    J/(m2*s)
!  PDIFTQ5     PDIFTQ        TRUBULENT FLUX OF SPECIFIC HUMIDITY      kg/(m2*s)


!     METHOD.
!     -------

!     SEE VDFMAIN

!     EXTERNALS.
!     ----------

!     *VDFMAINSAD* CALLS SUCESSIVELY:
!         *SURFEXCDRIVERS* -----> *VUPDZ0*
!                            |--> *VSURF*
!                            |--> *VEXCSS*
!                            |--> *VEVAP*
!         *VDFEXCUS*
!         *VDFDIFMS*
!         *VDFDIFHS*
!         *VDFINCRS*
!         *VDFDIFCS*
!         *SURFPPS*   -----> *SPPCFLS*
!         *SURFPPSAD* -----> *SPPCFLS*
!                       |--> *SPPCFLSAD*
!         *VDFDIFCSAD*
!         *VDFINCRSAD*
!         *VDFDIFHSAD*
!         *VDFDIFMSAD*
!         *VDFEXCUSAD*
!         *SURFEXCDRIVERSAD* -----> *VUPDZ0*
!                              |--> *VSURF*
!                              |--> *VEXCSS*
!                              |--> *VEVAP*

!     REFERENCE.
!     ----------

!          SEE VERTICAL DIFFUSION'S PART OF THE MODEL'S DOCUMENTATION
!     FOR DETAILS ABOUT THE MATHEMATICS OF THIS ROUTINE.

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

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

USE YOEVDF   , ONLY : RVDIFTS
USE YOMCST   , ONLY : RG       ,RCPD     ,RETV     ,RLVTT    ,&
 & RLSTT    ,RTT  
USE YOETHF   , ONLY : RVTMP2   ,R2ES     ,R3LES    ,R3IES    ,R4LES    ,&
 & R4IES    ,R5LES    ,R5IES    ,R5ALVCP  ,&
 & R5ALSCP  ,RALVDCP  ,RALSDCP  ,RTWAT    ,RTICE    ,RTICECU  ,&
 & RTWAT_RTICE_R      ,RTWAT_RTICECU_R  
USE YOEPHLI  , ONLY : LENOPERT, LEPPCFLS
USE YOEPHY   , ONLY : LVDFTRAC
USE YOPHNC   , ONLY : LEKPERT

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KLON 
INTEGER(KIND=JPIM),INTENT(IN)    :: KLEV 
INTEGER(KIND=JPIM),INTENT(IN)    :: KLEVS 
INTEGER(KIND=JPIM),INTENT(IN)    :: KTILES 
INTEGER(KIND=JPIM),INTENT(IN)    :: KTRAC
INTEGER(KIND=JPIM),INTENT(IN)    :: KIDIA 
INTEGER(KIND=JPIM),INTENT(IN)    :: KFDIA 
INTEGER(KIND=JPIM),INTENT(IN)    :: KSTEP 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTSPHY 
INTEGER(KIND=JPIM),INTENT(IN)    :: KTVL(KLON) 
INTEGER(KIND=JPIM),INTENT(IN)    :: KTVH(KLON) 
INTEGER(KIND=JPIM),INTENT(IN)    :: KSOTY(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCVL(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCVH(KLON)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PLAIL(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PLAIH(KLON)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSIGFLT(KLON)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PUM15(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVM15(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTM15(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQM15(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCM15(KLON,KLEV,KTRAC) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAPHM15(KLON,0:KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGEOM15(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGEOH5(KLON,0:KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTSKM1M5(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTSAM1M5(KLON,KLEVS) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PWSAM1M5(KLON,KLEVS) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSSRFL5(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSLRFL5(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PEMIS5(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTHKICE(KLON)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSNTICE(KLON)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTSNOW5(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTICE5(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSST(KLON) 
!REAL(KIND=JPRB)   ,INTENT(IN)    :: PHLICE5(KLON) !lake passive 
!REAL(KIND=JPRB)   ,INTENT(IN)    :: PTLICE5(KLON) !lake passive
!REAL(KIND=JPRB)   ,INTENT(IN)    :: PTLWML5(KLON) !lake passive
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFRTI(KLON,KTILES) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PALBTI5(KLON,KTILES) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PWLMX5(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCHAR(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTSKRAD5(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCFLX5(KLON,KTRAC)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PZ0M5(KLON) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PZ0H5(KLON) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PU10M5(KLON) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PV10M5(KLON) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: P10NU5(KLON) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: P10NV5(KLON) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PT2M5(KLON) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PD2M5(KLON) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PQ2M5(KLON) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTE5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PQE5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PVOM5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PVOL5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTENC5(KLON,KLEV,KTRAC)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTSKE15(KLON) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PUSTRTI5(KLON,KTILES) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PVSTRTI5(KLON,KTILES) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PAHFSTI5(KLON,KTILES) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PEVAPTI5(KLON,KTILES) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTSKTI5(KLON,KTILES) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PDIFTS5(KLON,0:KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PDIFTQ5(KLON,0:KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PSTRTU5(KLON,0:KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PSTRTV5(KLON,0:KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PUM1(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PVM1(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTM1(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PQM1(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PCM1(KLON,KLEV,KTRAC) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PAPHM1(KLON,0:KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PGEOM1(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PGEOH(KLON,0:KLEV)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTSKM1M(KLON) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTSAM1M(KLON,KLEVS) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PWSAM1M(KLON,KLEVS)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PSSRFL(KLON) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PSLRFL(KLON) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PALBTI(KLON,KTILES) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTSKRAD(KLON) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PCFLX(KLON,KTRAC)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PZ0M(KLON) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PZ0H(KLON) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PU10M (KLON) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PV10M(KLON) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: P10NU(KLON) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: P10NV(KLON) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PT2M(KLON) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PD2M(KLON) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PQ2M(KLON) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTE(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PQE(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PVOM(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PVOL(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTENC(KLON,KLEV,KTRAC)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTSKE1(KLON) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PUSTRTI(KLON,KTILES) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PVSTRTI(KLON,KTILES) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PAHFSTI(KLON,KTILES) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PEVAPTI(KLON,KTILES) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTSKTI(KLON,KTILES) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDIFTS(KLON,0:KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDIFTQ(KLON,0:KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PSTRTU(KLON,0:KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PSTRTV(KLON,0:KLEV) 

!*         0.     LOCAL VARIABLES.
!                 ----- ----------

REAL(KIND=JPRB) :: ZVDIS5(KLON)
REAL(KIND=JPRB) :: ZCPTGZ5(KLON,KLEV), ZCFM5(KLON,KLEV)  , ZCFH5(KLON,KLEV)
REAL(KIND=JPRB) :: ZUDIF5(KLON,KLEV) , ZVDIF5(KLON,KLEV) 
REAL(KIND=JPRB) :: ZTDIF5(KLON,KLEV) , ZQDIF5(KLON,KLEV) , ZTOFDC5(KLON,KLEV)  
REAL(KIND=JPRB) :: ZKHFL5(KLON)      , ZKQFL5(KLON)      , ZKMFL5(KLON)  
REAL(KIND=JPRB) :: ZQEA5(KLON,KLEV), ZTEA5(KLON,KLEV), ZTEWODIS5(KLON,KLEV)
REAL(KIND=JPRB) :: ZUEA5(KLON,KLEV), ZVEA5(KLON,KLEV)
REAL(KIND=JPRB) :: ZZ0MW5(KLON) , ZZ0HW5(KLON), ZZ0QW5(KLON)
REAL(KIND=JPRB) :: ZZCPTS5(KLON), ZZQSA5(KLON), ZZBUOM5(KLON)

REAL(KIND=JPRB) :: ZCPTSTI5(KLON,KTILES)
REAL(KIND=JPRB) :: ZQSTI5(KLON,KTILES) , ZDQSTI5(KLON,KTILES)
REAL(KIND=JPRB) :: ZCSATTI5(KLON,KTILES), ZCAIRTI5(KLON,KTILES)
REAL(KIND=JPRB) :: ZCFHTI5(KLON,KTILES), ZCFQTI5(KLON,KTILES)
REAL(KIND=JPRB) :: ZAHFLTI5(KLON,KTILES)
REAL(KIND=JPRB) :: ZTSKTIP15(KLON,KTILES),ZQSTIP15(KLON,KTILES)
REAL(KIND=JPRB) :: ZCPTSTIP15(KLON,KTILES),ZSSRFLTI5(KLON,KTILES)
REAL(KIND=JPRB) :: ZSTR5(KLON,KTILES),ZG05(KLON,KTILES)
REAL(KIND=JPRB) :: ZPEVAPSNW5(KLON)
REAL(KIND=JPRB) :: ZPBLH5(KLON), ZZINV5(KLON), ZKHVFL5(KLON)

REAL(KIND=JPRB) :: ZVDIS(KLON)
REAL(KIND=JPRB) :: ZCPTGZ(KLON,KLEV), ZCFM(KLON,KLEV)  , ZCFH(KLON,KLEV)
REAL(KIND=JPRB) :: ZUDIF(KLON,KLEV) , ZVDIF(KLON,KLEV)
REAL(KIND=JPRB) :: ZTDIF(KLON,KLEV) , ZQDIF(KLON,KLEV) , ZTOFDC(KLON,KLEV)   
REAL(KIND=JPRB) :: ZQEA(KLON,KLEV), ZTEA(KLON,KLEV), ZTEWODIS(KLON,KLEV)
REAL(KIND=JPRB) :: ZUEA(KLON,KLEV), ZVEA(KLON,KLEV)
REAL(KIND=JPRB) :: ZZ0MW(KLON) , ZZ0HW(KLON), ZZ0QW(KLON)
REAL(KIND=JPRB) :: ZZCPTS(KLON), ZZQSA(KLON), ZZBUOM(KLON)

REAL(KIND=JPRB) :: ZCPTSTI(KLON,KTILES)
REAL(KIND=JPRB) :: ZQSTI(KLON,KTILES) , ZDQSTI(KLON,KTILES)
REAL(KIND=JPRB) :: ZCSATTI(KLON,KTILES), ZCAIRTI(KLON,KTILES)
REAL(KIND=JPRB) :: ZCFHTI(KLON,KTILES), ZCFQTI(KLON,KTILES)
REAL(KIND=JPRB) :: ZAHFLTI(KLON,KTILES)
REAL(KIND=JPRB) :: ZTSKTIP1(KLON,KTILES),ZQSTIP1(KLON,KTILES)
REAL(KIND=JPRB) :: ZCPTSTIP1(KLON,KTILES),ZSSRFLTI(KLON,KTILES)
REAL(KIND=JPRB) :: ZSTR(KLON,KTILES),ZG0(KLON,KTILES)
REAL(KIND=JPRB) :: ZPEVAPSNW(KLON)

!*            EXTRA LOCAL STORAGE FOR TRAJECTORY
!             ----- ----- ------- --- ----------

REAL(KIND=JPRB) :: ZTDIF55(KLON,KLEV),ZQDIF55(KLON,KLEV)
REAL(KIND=JPRB) :: ZTE55(KLON,KLEV),ZQE55(KLON,KLEV)
REAL(KIND=JPRB) :: ZVOL55(KLON,KLEV),ZVOM55(KLON,KLEV)
REAL(KIND=JPRB) :: ZTE55B(KLON,KLEV),ZQE55B(KLON,KLEV)
REAL(KIND=JPRB) :: ZVOL55B(KLON,KLEV),ZVOM55B(KLON,KLEV)
REAL(KIND=JPRB) :: ZZ0H5(KLON),ZZ0M5(KLON)  
REAL(KIND=JPRB) :: ZEVAPTI5(KLON,KTILES),ZAHFSTI5(KLON,KTILES)
REAL(KIND=JPRB) :: ZEVAPTI15(KLON,KTILES),ZEVAPTI25(KLON,KTILES)
REAL(KIND=JPRB) :: ZAHFSTI15(KLON,KTILES),ZAHFSTI25(KLON,KTILES)
REAL(KIND=JPRB) :: ZUSTRTI5(KLON,KTILES),ZUSTRTI55(KLON,KTILES)
REAL(KIND=JPRB) :: ZVSTRTI5(KLON,KTILES),ZVSTRTI55(KLON,KTILES)
REAL(KIND=JPRB) :: ZTSKTI5(KLON,KTILES),ZTSKTI55(KLON,KTILES)
REAL(KIND=JPRB) :: ZTSKE15(KLON)
REAL(KIND=JPRB) :: ZTSKTI15(KLON,KTILES)
REAL(KIND=JPRB) :: ZTENC15(KLON,KLEV,KTRAC)

INTEGER(KIND=JPIM) :: ITOP, JK, JL, JTILE, ITRC

REAL(KIND=JPRB) :: ZCP, ZCP5, ZGDPH, ZGDPH5, ZTMST
REAL(KIND=JPRB) :: ZRG

REAL(KIND=JPRB) :: ZHOOK_HANDLE

INTERFACE
#include "surfexcdrivers.h"
#include "surfpps.h"
#include "surfexcdriversad.h"
#include "surfppsad.h"
END INTERFACE

#include "vdfdifhs.intfb.h"
#include "vdfdifhsad.intfb.h"
#include "vdfdifms.intfb.h"
#include "vdfdifmsad.intfb.h"
#include "vdfexcus.intfb.h"
#include "vdftofdcs.intfb.h"
#include "vdfexcusad.intfb.h"
#include "vdfincrs.intfb.h"
#include "vdfincrsad.intfb.h"
#include "vdfdifcs.intfb.h"
#include "vdfdifcsad.intfb.h"
#include "vdfdpbls.intfb.h"

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

#include "fcttre.h"

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

!*         1.     INITIALIZE CONSTANTS
!                 ---------- ---------

IF (LHOOK) CALL DR_HOOK('VDFMAINSAD',0,ZHOOK_HANDLE)
ZTMST=PTSPHY
ZRG=-1.0_JPRB/RG

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

!*                 -------- TRAJECTORY COMPUTATIONS --------

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

!          1.1  GET PROVISONAL T+DT WINDS

DO JK=1,KLEV
  DO JL=KIDIA,KFDIA
    ZQEA5(JL,JK)=PQE5(JL,JK)
    ZTEA5(JL,JK)=PTE5(JL,JK)
    ZUEA5(JL,JK)=PVOM5(JL,JK)
    ZVEA5(JL,JK)=PVOL5(JL,JK)
  ENDDO
ENDDO

! extra storage for AD
DO JTILE=1,KTILES
  DO JL=KIDIA,KFDIA
    ZEVAPTI5(JL,JTILE)=PEVAPTI5(JL,JTILE)
    ZAHFSTI5(JL,JTILE)=PAHFSTI5(JL,JTILE)
    ZUSTRTI5(JL,JTILE)=PUSTRTI5(JL,JTILE)
    ZVSTRTI5(JL,JTILE)=PVSTRTI5(JL,JTILE)
    ZTSKTI5 (JL,JTILE)=PTSKTI5 (JL,JTILE)
  ENDDO
ENDDO

DO JL=KIDIA,KFDIA
  ZZ0M5(JL)=PZ0M5(JL)
  ZZ0H5(JL)=PZ0H5(JL)
ENDDO

!*         1.2  dry static energy cp(q)*T + gz

DO JK = 1, KLEV
  DO JL = KIDIA, KFDIA
    ZCPTGZ5(JL,JK)=&
     & PGEOM15(JL,JK)+PTM15(JL,JK)*RCPD*(1.0_JPRB+RVTMP2*PQM15(JL,JK))  
  ENDDO
ENDDO

CALL SURFEXCDRIVERS( &
 & KIDIA=KIDIA, KFDIA=KFDIA, KLON=KLON, KLEVS=KLEVS, KTILES=KTILES, &
 & KSTEP=KSTEP, &
 & PTSTEP=PTSPHY, PRVDIFTS=RVDIFTS, &
! input data, non-tiled - trajectory
 & KTVL=KTVL, KTVH=KTVH, PCVL=PCVL, PCVH=PCVH, &
 & PLAIL=PLAIL, PLAIH=PLAIH, &
 & PUMLEV=PUM15(:,KLEV), PVMLEV=PVM15(:,KLEV), PTMLEV=PTM15(:,KLEV), &
 & PQMLEV=PQM15(:,KLEV), PAPHMS=PAPHM15(:,KLEV), &
 & PGEOMLEV=PGEOM15(:,KLEV), PCPTGZLEV=ZCPTGZ5(:,KLEV), &
 & PSST=PSST, PTSKM1M=PTSKM1M5, PCHAR=PCHAR, &
 & PSSRFL=PSSRFL5, PTICE=PTICE5, PTSNOW=PTSNOW5, PWLMX=PWLMX5, &
! input data, soil - trajectory
 & PTSAM1M=PTSAM1M5, PWSAM1M=PWSAM1M5, KSOTY=KSOTY, &
! input data, tiled - trajectory
 & PFRTI=PFRTI, PALBTI=PALBTI5, &
! updated data, tiled - trajectory
 & PUSTRTI=PUSTRTI5, PVSTRTI=PVSTRTI5, PAHFSTI=PAHFSTI5, &
 & PEVAPTI=PEVAPTI5, PTSKTI=PTSKTI5, &
! updated data, non-tiled - trajectory
 & PZ0M=PZ0M5, PZ0H=PZ0H5, &
! output data, tiled - trajectory
 & PSSRFLTI=ZSSRFLTI5, PQSTI=ZQSTI5, PDQSTI=ZDQSTI5, PCPTSTI=ZCPTSTI5, &
 & PCFHTI=ZCFHTI5, PCFQTI=ZCFQTI5, PCSATTI=ZCSATTI5, PCAIRTI=ZCAIRTI5, &
! output data, non-tiled - trajectory
 & PCFMLEV=ZCFM5(:,KLEV), PKMFL=ZKMFL5, PKHFL=ZKHFL5, PKQFL=ZKQFL5, &
 & PEVAPSNW=ZPEVAPSNW5, &
 & PZ0MW=ZZ0MW5, PZ0HW=ZZ0HW5, PZ0QW=ZZ0QW5, &
 & PCPTSPP=ZZCPTS5, PQSAPP=ZZQSA5, PBUOMPP=ZZBUOM5 &
 & )

!*         2.     BOUNDARY LAYER HEIGHT FOR DIANOSTICS ONLY
!          ------------------------------------------------

CALL VDFDPBLS(KIDIA,KFDIA,KLON,KLEV,&
 & PTM15,PQM15,PGEOM15,ZKMFL5,ZKHFL5,ZKQFL5,ZPBLH5)

DO JL=KIDIA,KFDIA
  ZKHVFL5(JL)  = ZKHFL5(JL) + RETV * PTM15(JL,KLEV) * ZKQFL5(JL) !w'theta,v'
  IF ( ZKHVFL5(JL) >= 0.0_JPRB ) THEN
    ZZINV5(JL) = 0.0_JPRB   ! stable, therefore 0 PBL depth
  ELSE
    ZZINV5(JL) = ZPBLH5(JL)
  ENDIF
ENDDO

!*         3.     EXCHANGE COEFFICIENTS ABOVE THE SURFACE LAYER
!          ----------------------------------------------------

CALL VDFEXCUS(KIDIA  , KFDIA  , KLON     , KLEV   , ZTMST, &
            & PZ0M5  , PUM15  , PVM15    , PTM15  , PQM15, &
            & PAPHM15, PGEOM15, PGEOH5   , ZCPTGZ5, &
            & ZKMFL5 , ZKHFL5 , ZKQFL5   , ZZINV5 , &
            & ZCFM5  , ZCFH5)

CALL VDFTOFDCS(KIDIA , KFDIA  , KLON     , KLEV   , ZTMST, &
            & PUM15  , PVM15  , PGEOM15  , PSIGFLT, &
            & ZTOFDC5)  
 
!     ------------------------------------------------------------------

!*         4.     SOLVE DIFFUSION EQUATION.
!                 ----- --------- ---------

!          4.1    MOMENTUM

ITOP=1
CALL VDFDIFMS(KIDIA,KFDIA,KLON,KLEV,ITOP,&
 & ZTMST,PUM15,PVM15,PAPHM15,ZCFM5,ZTOFDC5,&
 & PVOM5,PVOL5,ZUDIF5,ZVDIF5)  

! extra storage for AD
DO JTILE=1,KTILES
  DO JL=KIDIA,KFDIA
    ZEVAPTI15(JL,JTILE)=PEVAPTI5(JL,JTILE)
    ZAHFSTI15(JL,JTILE)=PAHFSTI5(JL,JTILE)
    ZTSKTI15(JL,JTILE)=PTSKTI5(JL,JTILE)
  ENDDO
ENDDO

!          4.2    DRY STATIC ENERGY AND MOISTURE

CALL VDFDIFHS( KIDIA  ,KFDIA     ,KLON    ,KLEV    ,ITOP       ,KTILES,&
 & ZTMST   ,PFRTI     ,ZSSRFLTI5 ,PSLRFL5 ,PEMIS5  ,ZPEVAPSNW5 ,&
! & PHLICE5 ,PTLICE5   ,PTLWML5   ,& !lake passive
 & ZCPTGZ5 ,PTM15     ,PQM15     ,PAPHM15,&
 & ZCFH5   ,ZCFHTI5   ,ZCFQTI5   ,&
 & PTHKICE, PSNTICE, &
 & ZTDIF5  ,ZQDIF5    ,ZCPTSTI5  ,ZQSTI5 ,ZCAIRTI5 ,ZCSATTI5,&
 & ZDQSTI5 ,PTSKTI5   ,PTSKRAD5  ,&
 & PTSAM1M5(1,1),PTSNOW5,PTICE5  ,PSST ,&
 & ZTSKTIP15,ZQSTIP15,ZCPTSTIP15 ,PTE5 ,PQE5,&
 & PEVAPTI5,PAHFSTI5,ZAHFLTI5,ZSTR5,ZG05)

!          4.3     INCREMENTATION OF U AND V TENDENCIES, STORAGE OF
!                  THE DISSIPATION, COMPUTATION OF MULTILEVEL FLUXES.

DO JK = 1,KLEV
  DO JL = KIDIA,KFDIA
    ZTDIF55(JL,JK) =  ZTDIF5(JL,JK)
    ZQDIF55(JL,JK) =  ZQDIF5(JL,JK)
    ZVOM55 (JL,JK) =  PVOM5 (JL,JK)
    ZVOL55 (JL,JK) =  PVOL5 (JL,JK)
    ZTE55  (JL,JK) =  PTE5  (JL,JK)
    ZQE55  (JL,JK) =  PQE5  (JL,JK)
    ZVOM55B(JL,JK) =  PVOM5 (JL,JK)
    ZVOL55B(JL,JK) =  PVOL5 (JL,JK)
    ZTE55B (JL,JK) =  PTE5  (JL,JK)
    ZQE55B (JL,JK) =  PQE5  (JL,JK)
  ENDDO
ENDDO
  
CALL VDFINCRS( KIDIA  , KFDIA  , KLON  , KLEV   , ITOP     , ZTMST   ,&
 & PUM15  , PVM15  , PTM15  , PQM15    , PAPHM15, &
 & PGEOM15, ZCFM5  , ZTOFDC5, ZCPTGZ5,&
 & ZUDIF5 , ZVDIF5 , ZTDIF5 , ZQDIF5 ,&
 & PVOM5  , PVOL5  , PTE5   , PQE5   ,&
 & ZTEWODIS5, ZVDIS5 , PSTRTU5, PSTRTV5)

!          4.4  Solve for tracers

IF (LVDFTRAC .AND. KTRAC > 0) THEN 
! extra storage for adjoint
  DO ITRC=1,KTRAC
    DO JK=1,KLEV
      DO JL=KIDIA,KFDIA
        ZTENC15(JL,JK,ITRC)=PTENC5(JL,JK,ITRC)
      ENDDO  
    ENDDO
  ENDDO  
  CALL VDFDIFCS(KIDIA,KFDIA,KLON,KLEV,ITOP,KTRAC,&
              & ZTMST,PCM15,PTENC5,PAPHM15,ZCFH5,PCFLX5)
ENDIF

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

!*       5.    Wrap-up computations for the surface,
!               compute weather parameters, and skin T tendency

! extra storage for AD
DO JTILE=1,KTILES
  DO JL=KIDIA,KFDIA
    ZEVAPTI25(JL,JTILE)=PEVAPTI5(JL,JTILE)
    ZAHFSTI25(JL,JTILE)=PAHFSTI5(JL,JTILE)
    ZUSTRTI55(JL,JTILE)=PUSTRTI5(JL,JTILE)
    ZVSTRTI55(JL,JTILE)=PVSTRTI5(JL,JTILE)
    ZTSKTI55 (JL,JTILE)=PTSKTI5 (JL,JTILE)
  ENDDO
ENDDO
DO JL=KIDIA,KFDIA
  ZTSKE15(JL)=PTSKE15(JL)
ENDDO


CALL SURFPPS( KIDIA=KIDIA,KFDIA=KFDIA,KLON=KLON,KTILES=KTILES, &
 & LDPPCFLS=LEPPCFLS,PTSTEP=PTSPHY, &
! input - trajectory
 & PFRTI=PFRTI, PTSKTIP1=ZTSKTIP15, PAHFLTI=ZAHFLTI5, PG0TI=ZG05, &
 & PSTRTULEV=PSTRTU5(:,KLEV), PSTRTVLEV=PSTRTV5(:,KLEV), &
 & PTSKM1M=PTSKM1M5, PUMLEV=PUM15(:,KLEV), PVMLEV=PVM15(:,KLEV), &
 & PQMLEV=PQM15(:,KLEV), PGEOMLEV=PGEOM15(:,KLEV), &
 & PCPTSPP=ZZCPTS5, PCPTGZLEV=ZCPTGZ5(:,KLEV), &
 & PAPHMS=PAPHM15(:,KLEV), PZ0MW=ZZ0MW5, PZ0HW=ZZ0HW5, PZ0QW=ZZ0QW5, &
 & PQSAPP=ZZQSA5, PBUOM=ZZBUOM5, &
! updated - trajectory
 & PAHFSTI=PAHFSTI5, PEVAPTI=ZEVAPTI5, PTSKE1=PTSKE15, &
! output - trajectory
 & PDIFTSLEV=PDIFTS5(:,KLEV), PDIFTQLEV=PDIFTQ5(:,KLEV), &
 & PUSTRTI=PUSTRTI5, PVSTRTI=PVSTRTI5, PTSKTI=PTSKTI5, &
 & PU10M=PU10M5, PV10M=PV10M5, PT2M=PT2M5, PD2M=PD2M5, PQ2M=PQ2M5, &
 & P10NU=P10NU5, P10NV=P10NV5 &
 & )

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

!*         6.     FLUX COMPUTATIONS 
!                 ----------------- 

DO JL = KIDIA, KFDIA
  PDIFTQ5(JL,0) = 0.0_JPRB
  PDIFTS5(JL,0) = 0.0_JPRB
  PSTRTU5(JL,0) = 0.0_JPRB
  PSTRTV5(JL,0) = 0.0_JPRB
ENDDO

DO JK = KLEV, 2, -1
  DO JL = KIDIA, KFDIA
    ZGDPH5=(PAPHM15(JL,JK-1)-PAPHM15(JL,JK))*ZRG
!...change of dry static energy is converted to flux of d.s.e.
    ZCP5=RCPD*(1.0_JPRB+RVTMP2*PQM15(JL,JK))
    PDIFTS5(JL,JK-1)=ZCP5*(ZTEWODIS5(JL,JK)-ZTEA5(JL,JK))*&
     & ZGDPH5+PDIFTS5(JL,JK)  
!...changes in Q,U,V tendencies are converted to fluxes
    PDIFTQ5(JL,JK-1)=(PQE5 (JL,JK)-ZQEA5(JL,JK))*ZGDPH5+PDIFTQ5(JL,JK)
    PSTRTU5(JL,JK-1)=(PVOM5(JL,JK)-ZUEA5(JL,JK))*ZGDPH5+PSTRTU5(JL,JK)
    PSTRTV5(JL,JK-1)=(PVOL5(JL,JK)-ZVEA5(JL,JK))*ZGDPH5+PSTRTV5(JL,JK)
  ENDDO
ENDDO

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

!*                  -------- ADJOINT COMPUTATIONS --------

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

!*         7.     INITIALIZATION TO ZERO OF LOCAL VARIABLES

ZQEA    (:,:) = 0.0_JPRB
ZTEA    (:,:) = 0.0_JPRB
ZUEA    (:,:) = 0.0_JPRB
ZVEA    (:,:) = 0.0_JPRB
ZUDIF   (:,:) = 0.0_JPRB
ZVDIF   (:,:) = 0.0_JPRB
ZTDIF   (:,:) = 0.0_JPRB
ZQDIF   (:,:) = 0.0_JPRB
ZCFM    (:,:) = 0.0_JPRB
ZCFH    (:,:) = 0.0_JPRB
ZCPTGZ  (:,:) = 0.0_JPRB
ZTEWODIS(:,:) = 0.0_JPRB

ZQSTI    (:,:) = 0.0_JPRB
ZDQSTI   (:,:) = 0.0_JPRB
ZCPTSTI  (:,:) = 0.0_JPRB
ZCFHTI   (:,:) = 0.0_JPRB
ZCFQTI   (:,:) = 0.0_JPRB
ZCSATTI  (:,:) = 0.0_JPRB
ZCAIRTI  (:,:) = 0.0_JPRB
ZTSKTIP1 (:,:) = 0.0_JPRB
ZQSTIP1  (:,:) = 0.0_JPRB
ZCPTSTIP1(:,:) = 0.0_JPRB
ZAHFLTI  (:,:) = 0.0_JPRB
ZSSRFLTI (:,:) = 0.0_JPRB
ZSTR     (:,:) = 0.0_JPRB
ZG0      (:,:) = 0.0_JPRB
ZTOFDC   (:,:) = 0.0_JPRB

ZPEVAPSNW(:) = 0.0_JPRB
ZVDIS (:) = 0.0_JPRB

ZZ0MW (:) = 0.0_JPRB
ZZ0HW (:) = 0.0_JPRB
ZZ0QW (:) = 0.0_JPRB
ZZCPTS(:) = 0.0_JPRB
ZZQSA (:) = 0.0_JPRB
ZZBUOM(:) = 0.0_JPRB

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

!*         6.     FLUX COMPUTATIONS 
!                 ----------------- 

DO JK = 2, KLEV
  DO JL = KIDIA, KFDIA
    ZGDPH5=(PAPHM15(JL,JK-1)-PAPHM15(JL,JK))*ZRG
    ZCP5=RCPD*(1.0_JPRB+RVTMP2*PQM15(JL,JK))

    ZGDPH = 0.0_JPRB
    ZCP   = 0.0_JPRB

    PVOL (JL,JK) = PVOL (JL,JK) + PSTRTV (JL,JK-1)*ZGDPH5
    ZVEA (JL,JK) = ZVEA (JL,JK) - PSTRTV (JL,JK-1)*ZGDPH5
    ZGDPH = ZGDPH + PSTRTV (JL,JK-1)*(PVOL5(JL,JK)-ZVEA5(JL,JK))
    PSTRTV (JL,JK) = PSTRTV (JL,JK) + PSTRTV (JL,JK-1)
    PSTRTV (JL,JK-1) = 0.0_JPRB
    PVOM (JL,JK) = PVOM (JL,JK) + PSTRTU (JL,JK-1)*ZGDPH5
    ZUEA (JL,JK) = ZUEA (JL,JK) - PSTRTU (JL,JK-1)*ZGDPH5
    ZGDPH = ZGDPH + PSTRTU (JL,JK-1)*(PVOM5(JL,JK)-ZUEA5(JL,JK))
    PSTRTU (JL,JK) = PSTRTU (JL,JK) + PSTRTU (JL,JK-1)
    PSTRTU (JL,JK-1) = 0.0_JPRB
    PQE (JL,JK) = PQE (JL,JK) + PDIFTQ (JL,JK-1)*ZGDPH5
    ZQEA (JL,JK) = ZQEA (JL,JK) - PDIFTQ (JL,JK-1)*ZGDPH5
    ZGDPH = ZGDPH + PDIFTQ (JL,JK-1)*(PQE5(JL,JK)-ZQEA5(JL,JK))
    PDIFTQ (JL,JK) = PDIFTQ (JL,JK) + PDIFTQ (JL,JK-1)
    PDIFTQ (JL,JK-1) = 0.0_JPRB
    ZCP = ZCP + PDIFTS (JL,JK-1)*(ZTEWODIS5(JL,JK)-ZTEA5(JL,JK))* ZGDPH5
    ZTEWODIS (JL,JK) = ZTEWODIS (JL,JK) + PDIFTS (JL,JK-1)*ZCP5*ZGDPH5
    ZTEA (JL,JK) = ZTEA (JL,JK) - PDIFTS (JL,JK-1)*ZCP5*ZGDPH5
    ZGDPH = ZGDPH + PDIFTS (JL,JK-1)*ZCP5*(ZTEWODIS5(JL,JK)-&
     & ZTEA5(JL,JK))  
    PDIFTS (JL,JK) = PDIFTS (JL,JK) + PDIFTS (JL,JK-1)
    PDIFTS (JL,JK-1) = 0.0_JPRB
    PQM1(JL,JK) = PQM1(JL,JK) + ZCP*RCPD*RVTMP2
    PAPHM1 (JL,JK-1) = PAPHM1 (JL,JK-1)+ZGDPH*ZRG
    PAPHM1 (JL,JK  ) = PAPHM1 (JL,JK  )-ZGDPH*ZRG
  ENDDO
ENDDO

DO JL = KIDIA, KFDIA
  PDIFTQ (JL,0) = 0.0_JPRB
  PDIFTS (JL,0) = 0.0_JPRB
  PSTRTU (JL,0) = 0.0_JPRB
  PSTRTV (JL,0) = 0.0_JPRB
ENDDO
!     -----------------------------------------------------------------

!*       5.    Wrap-up computations for the surface,
!               compute weather parameters, and skin T tendency

CALL SURFPPSAD( KIDIA=KIDIA,KFDIA=KFDIA,KLON=KLON,KTILES=KTILES, &
 & LDPPCFLS=LEPPCFLS,PTSTEP=PTSPHY, &
! input - trajectory
 & PFRTI=PFRTI, PTSKTIP15=ZTSKTIP15, PAHFLTI5=ZAHFLTI5, PG0TI5=ZG05, &
 & PSTRTULEV5=PSTRTU5(:,KLEV), PSTRTVLEV5=PSTRTV5(:,KLEV), &
 & PTSKM1M5=PTSKM1M5, PUMLEV5=PUM15(:,KLEV), PVMLEV5=PVM15(:,KLEV), &
 & PQMLEV5=PQM15(:,KLEV), PGEOMLEV5=PGEOM15(:,KLEV), &
 & PCPTSPP5=ZZCPTS5, PCPTGZLEV5=ZCPTGZ5(:,KLEV), &
 & PAPHMS5=PAPHM15(:,KLEV), PZ0MW5=ZZ0MW5, PZ0HW5=ZZ0HW5, PZ0QW5=ZZ0QW5, &
 & PQSAPP5=ZZQSA5, PBUOM5=ZZBUOM5, &
! updated - trajectory
 & PAHFSTI5=ZAHFSTI25, PEVAPTI5=ZEVAPTI25, PTSKE15=ZTSKE15, &
! output - trajectory
 & PDIFTSLEV5=PDIFTS5(:,KLEV), PDIFTQLEV5=PDIFTQ5(:,KLEV), &
 & PUSTRTI5=ZUSTRTI55, PVSTRTI5=ZVSTRTI55,  PTSKTI5=ZTSKTI55, &
 & PU10M5=PU10M5, PV10M5=PV10M5, PT2M5=PT2M5, PD2M5=PD2M5, PQ2M5=PQ2M5, &
 & P10NU5=P10NU5, P10NV5=P10NV5, &
! input
 & PTSKTIP1=ZTSKTIP1, PAHFLTI=ZAHFLTI, PG0TI=ZG0, &
 & PSTRTULEV=PSTRTU(:,KLEV), PSTRTVLEV=PSTRTV(:,KLEV), &
 & PTSKM1M=PTSKM1M , PUMLEV=PUM1(:,KLEV), PVMLEV=PVM1(:,KLEV), &
 & PQMLEV=PQM1(:,KLEV), PGEOMLEV=PGEOM1(:,KLEV), &
 & PCPTSPP=ZZCPTS, PCPTGZLEV=ZCPTGZ(:,KLEV), &
 & PAPHMS=PAPHM1(:,KLEV), PZ0MW=ZZ0MW, PZ0HW=ZZ0HW, PZ0QW=ZZ0QW, &
 & PQSAPP=ZZQSA, PBUOM=ZZBUOM, &
! updated
 & PAHFSTI=PAHFSTI, PEVAPTI=PEVAPTI, PTSKE1=PTSKE1, &
! output
 & PDIFTSLEV=PDIFTS(:,KLEV), PDIFTQLEV=PDIFTQ(:,KLEV), PUSTRTI=PUSTRTI, &
 & PVSTRTI=PVSTRTI,  PTSKTI=PTSKTI, &
 & PU10M=PU10M, PV10M=PV10M, PT2M=PT2M, PD2M=PD2M, PQ2M=PQ2M, &
 & P10NU=P10NU, P10NV=P10NV  &
 & )

!          4.4  Solve for tracers

IF (LVDFTRAC .AND. KTRAC > 0) THEN 
  CALL VDFDIFCSAD(KIDIA,KFDIA,KLON,KLEV,ITOP,KTRAC,ZTMST,&
                & PCM15,ZTENC15,PAPHM15,ZCFH5,PCFLX5,&
                & PCM1 ,PTENC  ,PAPHM1 ,ZCFH ,PCFLX )
ENDIF

!          4.3     INCREMENTATION OF U AND V TENDENCIES, STORAGE OF
!                  THE DISSIPATION, COMPUTATION OF MULTILEVEL FLUXES.

CALL VDFINCRSAD (KIDIA,KFDIA,KLON,KLEV,ITOP,ZTMST,&
 & PUM15    ,PVM15 ,PTM15  ,PQM15  , PAPHM15, &
 & PGEOM15  ,ZCFM5 ,ZTOFDC5 ,ZCPTGZ5 ,&
 & ZUDIF5   ,ZVDIF5,ZTDIF55 ,ZQDIF55 , &
 & ZVOM55   ,ZVOL55,ZTE55   ,ZQE55   ,&
 & ZTEWODIS5,ZVDIS5,PSTRTU5,PSTRTV5, &
 & PUM1     ,PVM1  ,PTM1   ,PQM1   ,PAPHM1, &
 & PGEOM1   ,ZCFM  ,ZTOFDC  ,ZCPTGZ ,&
 & ZUDIF    ,ZVDIF ,ZTDIF  ,ZQDIF  , &
 & PVOM     ,PVOL  ,PTE    ,PQE    , &
 & ZTEWODIS ,ZVDIS ,PSTRTU ,PSTRTV)  

!          4.2    DRY STATIC ENERGY AND MOISTURE

CALL VDFDIFHSAD (KIDIA, KFDIA, KLON, KLEV, ITOP,KTILES,&
 & ZTMST  ,&
 & PFRTI  ,ZSSRFLTI5 ,PSLRFL5 ,PEMIS5  ,ZPEVAPSNW5,&
! & PHLICE5,PTLICE5   ,PTLWML5 ,& !lake passive
 & ZCPTGZ5,PTM15     ,PQM15   ,PAPHM15 ,&
 & ZCFH5  ,ZCFHTI5   ,ZCFQTI5 ,&
 & ZTDIF55,ZQDIF55   ,ZCPTSTI5,ZQSTI5  ,ZCAIRTI5 ,ZCSATTI5,&
 & ZDQSTI5,ZTSKTI15  ,PTSKRAD5,&
 & PTSAM1M5(1,1), PTSNOW5, PTICE5, PSST,&
 & ZTSKTIP15, ZQSTIP15, ZCPTSTIP15, ZTE55B, ZQE55B,&
 & ZEVAPTI15 , ZAHFSTI15, ZAHFLTI5  , ZSTR5 , ZG05,&
 & ZSSRFLTI , PSLRFL  , ZPEVAPSNW,&
 & ZCPTGZ ,PTM1  ,PQM1  , PAPHM1 ,&
 & ZCFH   ,ZCFHTI,ZCFQTI,&
 & PTHKICE, PSNTICE, &
 & ZTDIF  ,ZQDIF ,ZCPTSTI, ZQSTI , ZCAIRTI, ZCSATTI,&
 & ZDQSTI ,PTSKTI,PTSKRAD,&
 & PTSAM1M(1,1),&
 & ZTSKTIP1,ZQSTIP1,ZCPTSTIP1, PTE , PQE,&
 & PEVAPTI ,PAHFSTI,ZAHFLTI  , ZSTR, ZG0)  

!          4.1    MOMENTUM

ITOP=1
CALL VDFDIFMSAD (KIDIA, KFDIA, KLON, KLEV, ITOP,&
 & ZTMST  ,PUM15  ,PVM15  ,PAPHM15 , ZCFM5, ZTOFDC5,&
 & ZVOM55B,ZVOL55B,ZUDIF5 ,ZVDIF5  ,&
 & PUM1   ,PVM1   ,PAPHM1 ,ZCFM    , ZTOFDC, &
 & PVOM   ,PVOL   ,ZUDIF  ,ZVDIF)  

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

!*         3.     EXCHANGE COEFFICIENTS.
!                 -------- -------------

!*         3.3     EXCHANGE COEFFICIENTS ABOVE THE SURFACE LAYER

!    No perturbations 
DO JK = 1,KLEV
  DO JL = KIDIA, KFDIA
    ZTOFDC(JL,JK)=0.0_JPRB
  ENDDO
ENDDO

CALL VDFEXCUSAD (KIDIA, KFDIA, KLON, KLEV, ZTMST,&
 & PZ0M5  ,&
 & PUM15  ,PVM15  ,PTM15  ,PQM15 , &
 & PAPHM15,PGEOM15,PGEOH5 ,ZCPTGZ5 , &
 & ZKMFL5 , ZKHFL5,ZKQFL5 ,ZZINV5 , &
 & ZCFM5  ,ZCFH5, &
 & PZ0M   , &
 & PUM1   ,PVM1   ,PTM1   ,PQM1 , &
 & PAPHM1 ,PGEOM1 ,PGEOH  ,ZCPTGZ ,ZCFM ,ZCFH)


CALL SURFEXCDRIVERSAD( &
 & KIDIA=KIDIA, KFDIA=KFDIA, KLON=KLON, KLEVS=KLEVS, KTILES=KTILES, &
 & KSTEP=KSTEP, &
 & PTSTEP=PTSPHY, PRVDIFTS=RVDIFTS, LDNOPERT=LENOPERT, LDKPERT=LEKPERT, &
! input data, non-tiled - trajectory
 & KTVL=KTVL, KTVH=KTVH, PCVL=PCVL, PCVH=PCVH, &
 & PLAIL=PLAIL, PLAIH=PLAIH, &
 & PUMLEV5=PUM15(:,KLEV), PVMLEV5=PVM15(:,KLEV), PTMLEV5=PTM15(:,KLEV), &
 & PQMLEV5=PQM15(:,KLEV), PAPHMS5=PAPHM15(:,KLEV), &
 & PGEOMLEV5=PGEOM15(:,KLEV), PCPTGZLEV5=ZCPTGZ5(:,KLEV), &
 & PSST=PSST, PTSKM1M5=PTSKM1M5, PCHAR=PCHAR, &
 & PSSRFL5=PSSRFL5, PTICE5=PTICE5, PTSNOW5=PTSNOW5, PWLMX5=PWLMX5, &
! input data, soil - trajectory
 & PTSAM1M5=PTSAM1M5, PWSAM1M5=PWSAM1M5, KSOTY=KSOTY, &
! input data, tiled - trajectory
 & PFRTI=PFRTI, PALBTI5=PALBTI5, &
! updated data, tiled - trajectory
 & PUSTRTI5=ZUSTRTI5, PVSTRTI5=ZVSTRTI5, PAHFSTI5=ZAHFSTI5, &
 & PEVAPTI5=ZEVAPTI5, PTSKTI5=ZTSKTI5, &
! updated data, non-tiled - trajectory
 & PZ0M5=ZZ0M5, PZ0H5=ZZ0H5, &
! output data, tiled - trajectory
 & PSSRFLTI5=ZSSRFLTI5, PQSTI5=ZQSTI5, PDQSTI5=ZDQSTI5, PCPTSTI5=ZCPTSTI5, &
 & PCFHTI5=ZCFHTI5, PCFQTI5=ZCFQTI5, PCSATTI5=ZCSATTI5, PCAIRTI5=ZCAIRTI5, &
! output data, non-tiled - trajectory
 & PCFMLEV5=ZCFM5(:,KLEV), PEVAPSNW5=ZPEVAPSNW5, &
 & PZ0MW5=ZZ0MW5, PZ0HW5=ZZ0HW5, PZ0QW5=ZZ0QW5, &
 & PCPTSPP5=ZZCPTS5, PQSAPP5=ZZQSA5, PBUOMPP5=ZZBUOM5, &
! input data, non-tiled
 & PUMLEV=PUM1(:,KLEV), PVMLEV=PVM1(:,KLEV), PTMLEV=PTM1(:,KLEV), &
 & PQMLEV=PQM1(:,KLEV), PAPHMS=PAPHM1(:,KLEV), &
 & PGEOMLEV=PGEOM1(:,KLEV), PCPTGZLEV=ZCPTGZ(:,KLEV), &
 & PTSKM1M=PTSKM1M, PSSRFL=PSSRFL, &
! input data, soil
 & PTSAM1M=PTSAM1M, PWSAM1M=PWSAM1M, &
! input data, tiled
 & PALBTI=PALBTI, &
! updated data, tiled
 & PUSTRTI=PUSTRTI, PVSTRTI=PVSTRTI, PAHFSTI=PAHFSTI, &
 & PEVAPTI=PEVAPTI, PTSKTI=PTSKTI, &
! updated data, non-tiled
 & PZ0M=PZ0M, PZ0H=PZ0H, &
! output data, tiled
 & PSSRFLTI=ZSSRFLTI, PQSTI=ZQSTI, PDQSTI=ZDQSTI, PCPTSTI=ZCPTSTI, &
 & PCFHTI=ZCFHTI, PCFQTI=ZCFQTI, PCSATTI=ZCSATTI, PCAIRTI=ZCAIRTI, &
! output data, non-tiled
 & PCFMLEV=ZCFM(:,KLEV), PEVAPSNW=ZPEVAPSNW, &
 & PZ0MW=ZZ0MW, PZ0HW=ZZ0HW, PZ0QW=ZZ0QW, &
 & PCPTSPP=ZZCPTS, PQSAPP=ZZQSA, PBUOMPP=ZZBUOM &
 & )

!*         1.2  dry static energy cp(q)*T + gz

DO JK = 1, KLEV
  DO JL = KIDIA, KFDIA
    PGEOM1 (JL,JK) = PGEOM1 (JL,JK) + ZCPTGZ(JL,JK)
    PTM1 (JL,JK) = PTM1 (JL,JK) + ZCPTGZ(JL,JK)*&
     & RCPD*(1.0_JPRB+RVTMP2*PQM15(JL,JK))  
    PQM1 (JL,JK) = PQM1 (JL,JK) + ZCPTGZ(JL,JK)*RCPD*RVTMP2*PTM15(JL,JK)
    ZCPTGZ(JL,JK) = 0.0_JPRB
  ENDDO
ENDDO

!          1.1  GET PROVISONAL T+DT WINDS

DO JK=1,KLEV
  DO JL=KIDIA,KFDIA
    PVOL (JL,JK) = PVOL (JL,JK) + ZVEA (JL,JK)
    ZVEA (JL,JK) = 0.0_JPRB
    PVOM (JL,JK) = PVOM (JL,JK) + ZUEA (JL,JK)
    ZUEA (JL,JK) = 0.0_JPRB
    PTE (JL,JK) = PTE (JL,JK) + ZTEA (JL,JK)
    ZTEA (JL,JK) = 0.0_JPRB
    PQE (JL,JK) = PQE (JL,JK) + ZQEA (JL,JK)
    ZQEA (JL,JK) = 0.0_JPRB
  ENDDO
ENDDO

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