!OPTIONS XOPT(HSFUN)
SUBROUTINE VDFMAINS (KIDIA,KFDIA,KLON,KLEV,KLEVS,KSTEP,KTILES,&
 & KTRAC,PTSPHY,KTVL,KTVH,PCVL,PCVH, &
 & PLAIL, PLAIH,PSIGFLT, &
 & PUM1  ,PVM1  ,PTM1  ,PQM1  ,PCM1  ,PAPHM1  ,PGEOM1, PGEOH , &
 & PTSKM1M,PTSAM1M,PWSAM1M, &
 & PSSRFL,PSLRFL ,PEMIS ,&
 & PTHKICE, PSNTICE, &
 & PTSNOW, PTICE,&
 ! & PHLICE, PTLICE, PTLWML, &  !lake passive
 & PSST  ,KSOTY  ,PFRTI ,PALBTI ,PWLMX ,&
 & PCHAR ,PTSKRAD, PCFLX,&
 ! OUTPUT (TRAJECTORY)           
 & PZ0M   ,PZ0H , &
 & PU10M  , PV10M  , PT2M   , PD2M   , PQ2M ,&
 & P10NU  , P10NV  ,&
 ! OUTPUT TENDENCIES (TRAJECTORY)           
 & PTE   ,PQE   ,PVOM  ,PVOL ,PTENC  ,PTSKE1,&
 !-UPDATED FIELDS FOR TILES (TRAJECTORY)
 & PUSTRTI,PVSTRTI,PAHFSTI,PEVAPTI,PTSKTI,&
 ! OUTPUT FLUXES (TRAJECTORY)           
 & PDIFTS,PDIFTQ,PSTRTU,PSTRTV)
!-----------------------------------------------------------------------     

!***

!**   *VDFMAINS* - DOES THE VERTICAL EXCHANGE OF U,V,T,Q BY TURBULENCE.
!                   (Nonlinear version for trajectory in adjoint)

!     J.F. MAHFOUF     E.C.M.W.F.         05/10/95

!     Modified TL code

!     M. Janiskova                        11/06/99 (TL for surface tile)
!     M. Janiskova                        03/09/03 (TL of new tile coupling)
!     P. Viterbo                          24/05/2004 (Change surface units)
!     P. Lopez                            24/02/2005 (nonlinear version for 
!                                                     trajectory in adjoint)
!     M. Janiskova                        27/06/2005 (removed option for MASS
!                                                    vector functions noy used
!                                                    in corresponding TL/AD)
!     A. Beljaars                         10/12/2005 TOFD
!     P. Lopez                            13/02/2006 (Added tracer diffusion)
!     G. Balsamo                          15/01/2007 (Added soil type)
!     G. Balsamo                          21/10/2008 (Added lake tile)
!     H. Hersbach                         04/12/2009 (10-m neutral wind)
!     S. Boussetta/G.Balsamo                May 2009 (Add 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)

!    *PTSPHY*       TIME STEP FOR THE PHYSICS

!     INPUT PARAMETERS AT T-1 OR CONSTANT IN TIME (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
!    *PUM1*        X-VELOCITY COMPONENT                       (Trajectory)
!    *PVM1*        Y-VELOCITY COMPONENT                       (Trajectory)
!    *PTM1*        TEMPERATURE                                (Trajectory) 
!    *PQM1*        SPECIFIC HUMIDITY                          (Trajectory)
!    *PCM1*        TRACER CONCENTRATION                         (KG/KG)
!    *PAPHM1*      PRESSURE                                   (Trajectory)
!    *PGEOM1*      GEOPOTENTIAL                               (Trajectory)
!    *PGEOH*        GEOPOTENTIAL AT HALF LEVELS                   M2/S2
!    *PTSKM1M*     SKIN TEMPERATURE                           (Trajectory)
!    *PTSKRAD*     SKIN TEMPERATURE at last full radiation
!                    time step                                 (Trajectory)
!    *PTSAM1M*      SURFACE TEMPERATURE                        (Trajectory)
!    *PWSAM1M*     SOIL MOISTURE ALL LAYERS                   (Trajectory)
!    *PSSRFL*      NET SHORTWAVE RADIATION FLUX AT SURFACE    (Trajectory)
!    *PSLRFL*      NET LONGWAVE RADIATION FLUX AT SURFACE     (Trajectory)
!    *PEMIS*       MODEL SURFACE LONGWAVE EMISSIVITY          (Trajectory)
!    *PTHKICE*     Sea ice thickness                           m
!    *PSNTICE*     Thickness of snow layer on sea ice          m
!    *PTSNOW*      SNOW TEMPERATURE                           (Trajectory) 
!    *PTICE*       ICE TEMPERATURE (TOP SLAB)                 (Trajectory) 
!    *PHLICE*      LAKE ICE THICKNESS                         (Trajectory) 
!    *PTLICE*      LAKE ICE TEMPERATURE                       (Trajectory)
!    *PTLWML*      LAKE MIXED LAYER TEMPERATURE               (Trajectory)
!    *PSST*         (OPEN) SEA SURFACE TEMPERATURE             (Trajectory) 
!    *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 LOW-VEG  8 : BARE SOIL
!    *PALBTI*      BROADBAND ALBEDO FOR TILE FRACTIONS        (Trajectory)
!    *PWLMX*       MAXIMUM SKIN RESERVOIR CAPACITY            (Trajectory)
!    *PCHAR*        "EQUIVALENT" CHARNOCK PARAMETER            (Trajectory)
!    *PCFLX*       TRACER SURFACE FLUX                         (KG/(M2 S))

!    UPDATED PARAMETERS (REAL):

!    *PTE*         TEMPERATURE TENDENCY                     (Trajectory)
!    *PQE*         MOISTURE TENDENCY                        (Trajectory)
!    *PVOM*        MERIODINAL VELOCITY TENDENCY (DU/DT)     (Trajectory)
!    *PVOL*        LATITUDE TENDENCY            (DV/DT)     (Trajectory)
!    *PTENC*       TRACER TENDENCY                           (KG/(KG S))
!    *PTSKE1*      SKIN TEMPERATURE TENDENCY                (Trajectory)
!    *PZ0M*        AERODYNAMIC ROUGHNESS LENGTH             (Trajectory)
!    *PZ0H*        ROUGHNESS LENGTH FOR HEAT                (Trajectory)

!    UDATED PARAMETERS FOR TILES (REAL):

!    *PU10M*        U-COMPONENT WIND AT 10 M                      M/S
!    *PV10M*        V-COMPONENT WIND AT 10 M                      M/S
!    *P10NV*        U-COMPONENT NEUTRAL WIND AT 10 M              M/S
!    *P10NV*        V-COMPONENT NEUTRAL WIND AT 10 M              M/S
!    *PT2M*         TEMPERATURE AT 2M                             K
!    *PD2M*         DEW POINT TEMPERATURE AT 2M                   K
!    *PQ2M*         SPECIFIC HUMIDITY AT 2M                       KG/KG
!    *PUSTRTI*      SURFACE U-STRESS                        (Trajectory)    
!    *PVSTRTI*      SURFACE V-STRESS                        (Trajectory)    
!    *PAHFSTI*      SURFACE SENSIBLE HEAT FLUX              (Trajectory)   
!    *PEVAPTI*      SURFACE MOISTURE FLUX                   (Trajectory) 
!    *PTSKTI*       SKIN TEMPERATURE                        (Trajectory)       

!    OUTPUT PARAMETERS (REAL):

!    *PSTRTU*       TURBULENT FLUX OF U-MOMEMTUM            (Trajectory)        
!    *PSTRTV*       TURBULENT FLUX OF V-MOMEMTUM            (Trajectory)       
!    *PDIFTS*       TURBULENT FLUX OF HEAT                  (Trajectory)        
!    *PDIFTQ*       TRUBULENT FLUX OF SPECIFIC HUMIDITY     (Trajectory)       

!     METHOD.
!     -------

!     SEE VDFMAIN

!     --------------------------------------------------------
!     THIS TANGENT LINEAR VERSION IS ONLY COMPATIBLE WITH THE
!     OPTION LPHYLIN=.TRUE. IN VDFMAIN
!     --------------------------------------------------------

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

!     *VDFMAINS* CALLS SUCESSIVELY:
!         *SURFEXCDRIVERS*
!         *VDFEXCUS*
!         *VDFTOFDCS*
!         *VDFDIFMS*
!         *VDFDIFHS*
!         *VDFINCRS*
!         *VDFDIFCS*

!     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 : LEPPCFLS
USE YOEPHY    , ONLY : LVDFTRAC

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)    :: PUM1(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVM1(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTM1(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQM1(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCM1(KLON,KLEV,KTRAC)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAPHM1(KLON,0:KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGEOM1(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGEOH(KLON,0:KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTSKM1M(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTSAM1M(KLON,KLEVS) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PWSAM1M(KLON,KLEVS) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSSRFL(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSLRFL(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PEMIS(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTHKICE(KLON)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSNTICE(KLON)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTSNOW(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTICE(KLON) 
!REAL(KIND=JPRB)   ,INTENT(IN)    :: PHLICE(KLON) !lake passive
!REAL(KIND=JPRB)   ,INTENT(IN)    :: PTLICE(KLON) !lake passive
!REAL(KIND=JPRB)   ,INTENT(IN)    :: PTLWML(KLON) !lake passive
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSST(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFRTI(KLON,KTILES) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PALBTI(KLON,KTILES) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PWLMX(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCHAR(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTSKRAD(KLON)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCFLX(KLON,KTRAC)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PZ0M(KLON) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PZ0H(KLON) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PU10M(KLON) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PV10M(KLON) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: P10NU(KLON) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: P10NV(KLON) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PT2M(KLON) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PD2M(KLON) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: 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(OUT)   :: PDIFTS(KLON,0:KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: 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) ::    ZVDIS(KLON)
REAL(KIND=JPRB) ::    ZCPTGZ(KLON,KLEV), ZCFM(KLON,KLEV) , ZCFH(KLON,KLEV),&
                    & ZUDIF(KLON,KLEV) , ZVDIF(KLON,KLEV) ,&
                    & ZTDIF(KLON,KLEV) , ZQDIF(KLON,KLEV) ,ZTOFDC(KLON,KLEV)  
REAL(KIND=JPRB) ::    ZKHFL(KLON)       , ZKQFL(KLON)       , ZKMFL(KLON)  
REAL(KIND=JPRB) ::    ZQEA(KLON,KLEV), ZTEA(KLON,KLEV), ZTEWODIS(KLON,KLEV),&
                    & 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),&
                    & ZQSTI(KLON,KTILES)  , ZDQSTI(KLON,KTILES),&
                    & ZCSATTI(KLON,KTILES), ZCAIRTI(KLON,KTILES), &
                    & ZCFHTI(KLON,KTILES) , ZCFQTI(KLON,KTILES),&
                    & ZAHFLTI(KLON,KTILES),&
                    & ZTSKTIP1(KLON,KTILES),ZQSTIP1(KLON,KTILES),&
                    & ZCPTSTIP1(KLON,KTILES),ZSSRFLTI(KLON,KTILES)  
REAL(KIND=JPRB) ::    ZSTR(KLON,KTILES),ZG0(KLON,KTILES)
REAL(KIND=JPRB) ::    ZPEVAPSNW(KLON)
REAL(KIND=JPRB) ::    ZPBLH(KLON), ZZINV(KLON), ZKHVFL(KLON)

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

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

REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

#include "vdfdifhs.intfb.h"
#include "vdfdifms.intfb.h"
#include "vdfexcus.intfb.h"
#include "vdfincrs.intfb.h"
#include "vdftofdcs.intfb.h"
#include "vdfdifcs.intfb.h"
#include "vdfdpbls.intfb.h"

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

#include "fcttre.h"

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

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

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

!          1.1  GET PROVISONAL T+DT WINDS

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

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

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

!*         2.  Compute all surface related quantities
!          ------------------------------------------

CALL SURFEXCDRIVERS( &
 & KIDIA=KIDIA, KFDIA=KFDIA, KLON=KLON, KLEVS=KLEVS, KTILES=KTILES, &
 & KSTEP=KSTEP, &
 & PTSTEP=PTSPHY, PRVDIFTS=RVDIFTS, &
! input data, non-tiled
 & KTVL=KTVL, KTVH=KTVH, PCVL=PCVL, PCVH=PCVH, &
 & PLAIL=PLAIL, PLAIH=PLAIH, &
 & PUMLEV=PUM1(:,KLEV), PVMLEV=PVM1(:,KLEV), PTMLEV=PTM1(:,KLEV), &
 & PQMLEV=PQM1(:,KLEV), PAPHMS=PAPHM1(:,KLEV), PGEOMLEV=PGEOM1(:,KLEV), &
 & PCPTGZLEV=ZCPTGZ(:,KLEV), PSST=PSST, PTSKM1M=PTSKM1M, PCHAR=PCHAR, &
 & PSSRFL=PSSRFL, PTICE=PTICE, PTSNOW=PTSNOW, PWLMX=PWLMX, &
! input data, soil
 & PTSAM1M=PTSAM1M, PWSAM1M=PWSAM1M, KSOTY=KSOTY,&
! input data, tiled
 & PFRTI=PFRTI, 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), PKMFL=ZKMFL, PKHFL=ZKHFL, PKQFL=ZKQFL, &
 & PEVAPSNW=ZPEVAPSNW, &
 & PZ0MW=ZZ0MW, PZ0HW=ZZ0HW, PZ0QW=ZZ0QW, PCPTSPP=ZZCPTS, PQSAPP=ZZQSA, &
 & PBUOMPP=ZZBUOM &
 & )

!*         5.5  BOUNDARY LAYER HEIGHT FOR DIANOSTICS ONLY

!CALL VDFDPBL(KIDIA,KFDIA,KLON,KLEV,&
! & PUM1,PVM1,PTM1,PQM1,PGEOM1,&
! & ZKMFL,ZKHFL,ZKQFL,ZPBLH)

CALL VDFDPBLS(KIDIA,KFDIA,KLON,KLEV,&
 & PTM1,PQM1,PGEOM1,ZKMFL,ZKHFL,ZKQFL,ZPBLH)

DO JL=KIDIA,KFDIA
  ZKHVFL(JL)  = ZKHFL(JL) + RETV * PTM1(JL,KLEV) * ZKQFL(JL) !w'theta,v'
  IF ( ZKHVFL(JL) >= 0.0_JPRB ) THEN
    ZZINV(JL) = 0.0_JPRB   ! stable, therefore 0 PBL depth
  ELSE
    ZZINV(JL) = ZPBLH(JL)
  ENDIF
ENDDO


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

CALL VDFEXCUS (KIDIA, KFDIA  , KLON , KLEV, ZTMST, PZ0M, &
 & PUM1   , PVM1    , PTM1   , PQM1 , &
 & PAPHM1 , PGEOM1  , PGEOH  , ZCPTGZ, &
 & ZKMFL  , ZKHFL   , ZKQFL  , ZZINV , ZCFM , ZCFH)
 

!*         3.5     TURBULENT OROGRAPHIC DRAG COEFFICIENTS 

CALL VDFTOFDCS(KIDIA,KFDIA,KLON,KLEV,ZTMST,&
 & PUM1,PVM1,PGEOM1,PSIGFLT,&
 & ZTOFDC)  

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

!          4.1    MOMENTUM

ITOP=1

CALL VDFDIFMS (KIDIA, KFDIA, KLON, KLEV, ITOP,&
 & ZTMST ,PUM1 ,PVM1  ,PAPHM1 , ZCFM, ZTOFDC, &
 & PVOM ,PVOL ,ZUDIF ,ZVDIF) 

!          4.2    DRY STATIC ENERGY AND MOISTURE

CALL VDFDIFHS (KIDIA ,KFDIA   ,KLON     ,KLEV  ,ITOP  ,KTILES,&
 & ZTMST    ,PFRTI   ,ZSSRFLTI,PSLRFL ,PEMIS  ,ZPEVAPSNW,&
! & PHLICE , PTLICE , PTLWML , & !Lake passive
 & ZCPTGZ  ,PTM1     ,PQM1     ,PAPHM1 ,&
 & ZCFH    ,ZCFHTI   ,ZCFQTI   ,&
 & PTHKICE, PSNTICE, &
 & ZTDIF   ,ZQDIF    ,ZCPTSTI  ,ZQSTI  ,ZCAIRTI  ,ZCSATTI,&
 & ZDQSTI  ,PTSKTI   ,PTSKRAD  ,&
 & PTSAM1M(1,1),PTSNOW,PTICE, PSST,&
 & ZTSKTIP1,ZQSTIP1  ,ZCPTSTIP1,PTE ,PQE  ,&
 & PEVAPTI ,PAHFSTI  ,ZAHFLTI  ,ZSTR ,ZG0)

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

CALL VDFINCRS (KIDIA ,KFDIA   ,KLON   ,KLEV   ,ITOP   ,ZTMST ,&
 & PUM1   ,PVM1  , PTM1   ,PQM1  ,PAPHM1, &
 & PGEOM1 ,ZCFM  , ZTOFDC ,ZCPTGZ ,&
 & ZUDIF  ,ZVDIF , ZTDIF  ,ZQDIF , &
 & PVOM   ,PVOL  , PTE    ,PQE   ,&
 & ZTEWODIS, ZVDIS ,PSTRTU, PSTRTV)

!          4.4  Solve for tracers

IF (LVDFTRAC .AND. KTRAC > 0) THEN 
  CALL VDFDIFCS(KIDIA,KFDIA,KLON,KLEV,ITOP,KTRAC,&
              & ZTMST,PCM1,PTENC,PAPHM1,ZCFH,PCFLX)
ENDIF

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

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

CALL SURFPPS( KIDIA=KIDIA,KFDIA=KFDIA,KLON=KLON,KTILES=KTILES, &
 & LDPPCFLS=LEPPCFLS,PTSTEP=PTSPHY, &
! input
 & PFRTI=PFRTI, 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 &
 & )

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

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

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

DO JK = KLEV, 2, -1
  DO JL = KIDIA, KFDIA
    ZGDPH=(PAPHM1(JL,JK-1)-PAPHM1(JL,JK))*ZRG
!...change of dry static energy is converted to flux of d.s.e.
    ZCP=RCPD*(1.0_JPRB+RVTMP2*PQM1(JL,JK))
    PDIFTS(JL,JK-1)=ZCP*(ZTEWODIS(JL,JK)-ZTEA(JL,JK))*&
     & ZGDPH+PDIFTS(JL,JK)  
!...changes in Q,U,V tendencies are converted to fluxes
    PDIFTQ(JL,JK-1)=(PQE (JL,JK)-ZQEA(JL,JK))*ZGDPH+PDIFTQ(JL,JK)
    PSTRTU(JL,JK-1)=(PVOM(JL,JK)-ZUEA(JL,JK))*ZGDPH+PSTRTU(JL,JK)
    PSTRTV(JL,JK-1)=(PVOL(JL,JK)-ZVEA(JL,JK))*ZGDPH+PSTRTV(JL,JK)
  ENDDO
ENDDO

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