MODULE SURFTSTP_CTL_MOD
CONTAINS
SUBROUTINE SURFTSTP_CTL(KIDIA , KFDIA , KLON  , KLEVS , KTILES,& 
 & KLEVI , KLEVO , KSTART , KSTEP,&             
 ! 
 ! this space is left for visual comparison to SURFTSTP.* and CALLPAR call
 ! The variables KLEVSN and KDH* are used in the external routine only
 ! The number of arguments per line is rearranged for readibility
 !
 ! 
 !
 & PTSPHY  , KTVL   , KTVH,&
 & KSOTY , PLIT, PMELTSUM, PSDOR , PFRTI,& 
 & PSST, PUSTRTI , PVSTRTI,&
 & PAHFSTI, PEVAPTI,&
 & PSSRFLTI, LDLAND, LDSICE, LDSI, LDNH, LDOCN_KPP,&
 & PSNM1M  ,PTSNM1M,PASNM1M,PRSNM1M,PTSAM1M,PTIAM1M,&
 & PWLM1M  ,PWSAM1M,&
 & PTLICEM1M,PTLMNWM1M,PTLWMLM1M,PTLBOTM1M,PTLSFM1M,&
 & PHLICEM1M,PHLMLM1M,PGEMU,PGELAM,PLDEPTH,LDLAKE,PCLAKE, &
 & PRSFC   ,PRSFL,& 
 & PSLRFL  ,PSSFC   ,PSSFL,& 
 & PCVL    ,PCVH    ,PWLMX   ,PEVAPSNW,&
 & PUSRF   ,PVSRF   ,PTSRF,&
 & PZO     ,PHO     ,PHO_INV ,PDO     ,POCDEPTH,&
 & PUO0    ,PVO0    ,PUOC    ,PVOC    ,PTO0     ,&      !KPP
 & PSO0    ,PADVT   ,PADVS   ,PTRI0   ,PTRI1    ,&      !KPP
 & PSWDK_SAVE       ,PUSTRC  ,PVSTRC  ,          &      !KPP
!-DIAGNOSTICS OUTPUT
 & PTSDFL  , PROFD , PROFS,&
 & PWFSD   , PMELT , PFWEV, PENES,&
 & PDIFM   , PDIFT , PDIFS,&
!-TENDENCIES OUTPUT
 & PSNE1   , PTSNE1 , PASNE1,&
 & PRSNE1  , PTSAE1 , PTIAE1,&
 & PWLE1   , PWSAE1,&
 & PTLICEE1,PTLMNWE1,PTLWMLE1,&
 & PTLBOTE1,PTLSFE1,PHLICEE1,PHLMLE1,&
 & PUOE1   ,PVOE1   ,PTOE1  ,PSOE1,&
!-DDH OUTPUTS
 & PDHTSS  , PDHTTS , PDHTIS,&
 & PDHSSS  , PDHIIS , PDHWLS)
!-END OF CALL SURFTSTP

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

USE YOS_THF   , ONLY : RHOH2O
USE YOS_SOIL  , ONLY : RDAW, LEVGEN, LESN09,RALFMINPSN,RHOMAXSN,RSNPER
USE YOS_FLAKE , ONLY : LEFLAKE, RH_ICE_MIN_FLK
USE YOS_OCEAN_ML , ONLY : LEOCML 
USE SRFENE_MOD
USE SRFSN_MOD
USE SRFSN_LWIMP_MOD
USE SRFRCG_MOD
USE SRFT_MOD
USE SRFI_MOD
USE SRFWL_MOD
USE SRFWEXC_MOD
USE SRFWEXC_VG_MOD
USE SRFWDIF_MOD
USE SRFWINC_MOD
USE SRFWNG_MOD
USE FLAKE_DRIVER_MOD 
USE OCEAN_ML_DRIVER_MOD   !KPP
USE YOS_CST   , ONLY : RPI
USE YOMECEARTH, ONLY : LLANDICE, LPISM

#ifdef DOC
!**** *SURFTSTP* - UPDATES LAND VALUES OF TEMPERATURE, MOISTURE AND SNOW.

!     PURPOSE.
!     --------
!          This routine updates the sea ice values of temperature
!     and the land values of soil temperature, interception layer 
!     water (kg/m**2), multilayer soil water (in m**3/m**3),
!     snow mass (in kg/m**2), snow temperature, snow density
!     and snow albedo. For temperature, this is done via a forward time
!     step damped with some implicit linear considerations: as if all
!     fluxes that explicitely depend on the variable had only a linear
!     variation around the t-1 value. For moisture: (a) the evaporation
!     of the interception layer is treated implicitely, via a
!     linearization, while the other sources are treated explicitely;
!     (b) The soil transfer is "fully semi-implicit" similar to vertical
!     diffusion. Lower boundary conditons are no heat flux and free
!     drainage.

!**   INTERFACE.
!     ----------
!          *SURFTSTP* IS CALLED FROM *CALLPAR*.
!          THE ROUTINE TAKES ITS INPUT FROM THE LONG-TERM STORAGE:
!     TSA,WL,WSA,SN AT T-1,TSK,SURFACE FLUXES COMPUTED IN OTHER PARTS OF
!     THE PHYSICS, AND W AND LAND-SEA MASK. IT RETURNS AS AN OUTPUT
!     TENDENCIES TO THE SAME VARIABLES (TSA,WL,WSA,SN).

!     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
!    *KTILES*     NUMBER OF TILES (I.E. SUBGRID AREAS WITH DIFFERENT 
!                 SURFACE BOUNDARY CONDITION)
!    *KTVL*       VEGETATION TYPE FOR LOW VEGETATION FRACTION
!    *KTVH*       VEGETATION TYPE FOR HIGH VEGETATION FRACTION
!    *KSOTY*      SOIL TYPE                                   (1-7)
!    *KLEVI*      Number of sea ice layers (diagnostics)
!    *KLEVO*      NUMBER OF LAYERS OF OCEAN MIXED LAYER MODEL       !KPP
!    *KSTART*     FIRST TIMESTEP                                    !KPP 
!    *KSTEP*      CURRENT TIMESTEP                                  !KPP 

!     INPUT PARAMETERS (REAL):
!    *PSICE*      .LT.0.5 FOR LATITUDE ROWS WITH NO SEA ICE POINTS
!    *PEPS*       COEFFICIENT FOR TIME FILTER
!    *PTSPHY*     TIME STEP FOR THE PHYSICS                      S
!    *PSDOR*      OROGRAPHIC PARAMETER                        (m)
!    *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
!    *PAHFSTI*      SURFACE SENSIBLE HEAT FLUX, FOR EACH TILE  W/M2
!    *PSST*         SEA SURFACE TEMPERATURE                    K
!    *PUSTRTI*      X-STRESS                                   N/m2
!    *PVSTRTI*      Y-STRESS                                   N/m2
!    *PEVAPTI*      SURFACE MOISTURE FLUX, FOR EACH TILE      KG/M2/S
!    *PSSRFLTI*     NET SHORTWAVE RADIATION FLUX AT SURFACE, FOR
!                       EACH TILE                                 W/M2
!    *PUO0*         HORIZONTAL VELOCITY OF OCEAN MIXED LAYER MODEL  !KPP
!    *PVO0*         HORIZONTAL VELOCITY OF OCEAN MIXED LAYER MODEL  !KPP
!    *PTO0*         TEMPERATURE OF OCEAN MIXED LAYER MODEL          !KPP
!    *PSO0*         SALINITY OF OCEAN MIXED LAYER MODEL             !KPP
!    *PADVT*        TEMPERATURE ADVECTION TERM FOR OCEAN MIXED LAYER MODEL !KPP
!    *PADVS*        SALINITY ADVECTION TERM FOR OCEAN MIXED LAYER MODEL    !KPP

!     INPUT PARAMETERS (LOGICAL):
!    *LDLAND*     LAND/SEA MASK (TRUE/FALSE)
!    *LDSICE*     SEA ICE MASK (.T. OVER SEA ICE)
!    *LDLAKE*     LAKE MASK (.T. OVER LAKE)
!    *LDSI*       TRUE IF THERMALLY RESPONSIVE SEA-ICE
!    *LDNH*       TRUE FOR NORTHERN HEMISPHERE LATITUDE ROW
!    *LDOCN_KPP   TRUE IF OCEAN MIXED LAYER GRID                    !KPP
!    *PCLAKE      REAL - lake cover 

!     INPUT PARAMETERS AT T-1 OR CONSTANT IN TIME (REAL):
!    *PSNM1M*     SNOW MASS (per unit area)                      KG/M**2
!    *PTSAM1M*    SOIL TEMPERATURE                               K
!    *PTIAM1M*    SEA-ICE TEMPERATURE                            K
!          (NB: REPRESENTS THE FRACTION OF ICE ONLY, NOT THE GRID-BOX)
!    *PWLM1M*     SKIN RESERVOIR WATER CONTENT                   kg/m**2
!    *PWSAM1M*    SOIL MOISTURE                                m**3/m**3
!    *PTLICEM1M*  LAKE ICE TEMPERATURE                           K
!    *PTLMNWM1M*  LAKE MEAN WATER TEMPERATURE                    K
!    *PTLWMLM1M*  LAKE MIX-LAYER TEMPERATURE                     K
!    *PTLBOTM1M*  LAKE BOTTOM TEMPERATURE                        K
!    *PTLSFM1M*   LAKE SHAPE FACTOR (THERMOCLINE)                -
!    *PHLICEM1M*  LAKE ICE THICKNESS                             m
!    *PHLMLM1M*   LAKE MIX-LAYER THICKNESS                       m
!    *PGEMU*      SIN OF LATITUDE                                -
!    *PGELAM*     LONGITUDE (RADIANS)
!    *PLDEPTH*    LAKE DEPTH                                     m
!    *PRSFC*      CONVECTIVE RAIN FLUX AT THE SURFACE          KG/M**2/S
!    *PRSFL*      LARGE SCALE RAIN FLUX AT THE SURFACE         KG/M**2/S
!    *PSLRFL*     NET LONGWAVE  RADIATION AT THE SURFACE         W/M**2
!    *PSSFC*      CONVECTIVE  SNOW FLUX AT THE SURFACE         KG/M**2/S
!    *PSSFL*      LARGE SCALE SNOW FLUX AT THE SURFACE         KG/M**2/S
!    *PCVL*       LOW VEGETATION COVER  (CORRECTED)              (0-1)
!    *PCVH*       HIGH VEGETATION COVER (CORRECTED)              (0-1)
!    *PWLMX*      MAXIMUM SKIN RESERVOIR CAPACITY                kg/m**2
!    *PEVAPSNW*   EVAPORATION FROM SNOW UNDER FOREST           KG/M**2/S
!    *POCDEPTH*   OCEAN DEPTH FOR OCEAN MIXED LAYER MODEL    (m)  !KPP
!    *PZO*        VERTICAL LAYER FOR OCEAN MIXED LAYER MODEL (m)  !KPP
!    *PDO*        VERTICAL LAYER FOR OCEAN MIXED LAYER MODEL (m)  !KPP
!    *PUSRF*      LOWEST LEVEL WIND U COMPONENT                   M/S
!    *PVSRF*      LOWEST LEVEL WIND V COMPONENT                   M/S
!    *PTSRF*      LOWEST LEVEL AIR TEMPERATURE                    K
!    *POCDEPTH*   OCEAN DEPTH FOR OCEAN MIXED LAYER MODEL    (m)  !KPP
!    *PZO*        VERTICAL LAYER FOR OCEAN MIXED LAYER MODEL (m)  !KPP
!    *PDO*        VERTICAL LAYER FOR OCEAN MIXED LAYER MODEL (m)  !KPP
!     OUTPUT PARAMETERS (TENDENCIES):
!    *PSNE1*      SNOW MASS (per unit area) TENDENCY           KG/M**2/S
!    *PTSNE1*     SNOW TEMPERATURE                              K/S
!    *PASNE1*     SNOW ALBEDO                                   -/S
!    *PRSNE1*     SNOW DENSITY                                KG/M**3/S
!    *PTSAE1*     SOIL TEMPERATURE TENDENCY                     K/S
!    *PTIAE1*     SEA-ICE TEMPERATURE TENDENCY                  K/S
!          (NB: REPRESENTS THE FRACTION OF ICE ONLY, NOT THE GRID-BOX)
!    *PWLE1*      SKIN RESERVOIR WATER CONTENT TENDENCY       kg/m**2/S
!    *PWSAE1*     SOIL MOISTURE TENDENCY                   (m**3/m**3)/S
!    *PTLICEE1*   LAKE ICE TEMPERATURE TENDENCY                 K/S
!    *PTLMNWE1*   LAKE MEAN WATER TEMPERATURE TENDENCY          K/S
!    *PTLWMLE1*   LAKE MIX-LAYER TEMPERATURE TENDENCY           K/S
!    *PTLBOTE1*   LAKE BOTTOM TEMPERATURE TENDENCY              K/S
!    *PTLSFE1*    LAKE SHAPE FACTOR (THERMOCLINE) TENDENCY      -/S
!    *PHLICEE1*   LAKE ICE THICKNESS TENDENCY                   m/S
!    *PHLMLE1*    LAKE MIX-LAYER THICKNESS TENDENCY             m/S
!    *PTOE1*      TEMPERATURE TENDENCY OF OCEAN MIXED LAYER MODEL  K/S
!    *PTOE1*      SALINITY TENDENCY OF OCEAN MIXED LAYER MODEL  psu/S
!    *PUOE1*      VELOCITY TENDENCY OF OCEAN MIXED LAYER MODEL M/S**2
!    *PVOE1*      VELOCITY TENDENCY OF OCEAN MIXED LAYER MODEL M/S**2

!     OUTPUT PARAMETERS (DIAGNOSTIC):
!    *PTSDFL*     UPWARD FLUX BETWEEN SURFACE AND DEEP LAYER   W/M**2
!    *PROFD*      DEEP LAYER RUN-OFF                          kg/m**2/s
!    *PROFS*      SURFACE RUN-OFF                             kg/m**2/s
!    *PWFSD*      WATER FLUX BETWEEN LAYER 1 AND 2            kg/m**2/s
!    *PMELT*      WATER FLUX CORRESPONDING TO SNOW MELT       kg/m**2/s
!    *PFWEV*      EVAPORATION OVER LAND SURFACE               kg/m**2/s
!    *PENES*      SOIL ENERGY per unit area                   J/M**2
!    *PDHTSS*     Diagnostic array for snow T (see module yomcdh)
!    *PDHTTS*     Diagnostic array for soil T (see module yomcdh)
!    *PDHTIS*     Diagnostic array for ice T (see module yomcdh)
!    *PDHSSS*     Diagnostic array for snow mass (see module yomcdh)
!    *PDHIIS*     Diagnostic array for interception layer (see module yomcdh)
!    *PDHWLS*     Diagnostic array for soil water (see module yomcdh)
!    *PDIFM*      VISCOSITY OF OCEAN MIXED LAYER MODEL                !KPP
!    *PDIFT*      TEMPERATURE DIFFUSIVITY OF OCEAN MIXED LAYER MODEL  !KPP
!    *PDIFS*      SCALAR DIFFUSIVITY OF OCEAN MIXED LAYER MODEL       !KPP

!     METHOD.
!     -------
!          STRAIGHTFORWARD ONCE THE DEFINITION OF THE CONSTANTS IS
!     UNDERSTOOD. FOR THIS REFER TO DOCUMENTATION. FOR THE TIME FILTER
!     SEE CORRESPONDING PART OF THE DOCUMENTATION OF THE ADIABATIC CODE.

!     EXTERNALS.
!     ----------
!          *SRFT*   COMPUTES THE TEMPERATURE CHANGES BEFORE THE SNOW
!                   MELTING.
!          *SRFWL*  COMPUTES THE SKIN RESERVOIR CHANGES.
!          *SRFWEXC*COMPUTES THE RUN-OFF AND SETS THE COEFFICIENTS OF
!                   THE TRIDIAGONAL MOISTURE SYSTEM OF EQUATIONS.
!          *SRFWDIF*SOLUTION OF THE TRIDIAGONAL MOISTURE SYSTEM OF EQUATIONS
!          *SRFWINC*INCREMENTS OF SOIL MOSITURE, COMPUTATION OF DIAGNOSTICS
!          *SRFSN*  COMPUTES SNOW DEPTH CHANGES BEFORE SNOW MELTING.
!          *SRFSN_LWIMP*  REVISED SNOW SCHEME W. DIAG. LIQ. WATER.
!          *SRFSML* COMPUTES TEMPERATURE, SKIN RESERVOIR, SOIL WATER
!                   AND SNOW DEPTH CHANGES DUE TO SNOW MELTING.
!          *SRFWNG* CORRECTIONS TO AVOID NEGATIVE SOIL MOISTURE.

!     REFERENCE.
!     ----------
!          SEE SOIL PROCESSES' PART OF THE MODEL'S DOCUMENTATION FOR
!     DETAILS ABOUT THE MATHEMATICS OF THIS ROUTINE.

!     P.VITERBO       E.C.M.W.F.     16/03/93.
!     MODIFIED BY
!     SURFACE TILES   P.VITERBO/ACMB          8/03/99.
!     Surface DDH for TILES P. Viterbo        17/05/2000.
!     J.F. Estrade *ECMWF* 03-10-01 move in surf vob
!     P. Viterbo    24-05-2004      New argument PENES; Change surface units
!     G. Balsamo    03-07-2004      Add soil type and orographic var (runoff)
!     V. Stepanenko/G. Balsamo 01-05-2008   Add lake parameterization (FLAKE)
!     Y. Takaya     07-10-2008      Add ocean mixed layer model (KPP scheme)
!     E. Dutra      12-11.2008     Include new snow parameterization 
!     E. Dutra      16-11-2009     snow 2009 cleaning
!     ------------------------------------------------------------------
#endif

IMPLICIT NONE

! Declaration of arguments

INTEGER(KIND=JPIM), INTENT(IN)   :: KIDIA
INTEGER(KIND=JPIM), INTENT(IN)   :: KFDIA
INTEGER(KIND=JPIM), INTENT(IN)   :: KLON
INTEGER(KIND=JPIM), INTENT(IN)   :: KLEVS
INTEGER(KIND=JPIM), INTENT(IN)   :: KSTART        !KPP
INTEGER(KIND=JPIM), INTENT(IN)   :: KSTEP         !KPP
INTEGER(KIND=JPIM), INTENT(IN)   :: KTILES
INTEGER(KIND=JPIM), INTENT(IN)   :: KLEVI
INTEGER(KIND=JPIM), INTENT(IN)   :: KLEVO         !KPP
INTEGER(KIND=JPIM), INTENT(IN)   :: KTVL(:)
INTEGER(KIND=JPIM), INTENT(IN)   :: KTVH(:)
INTEGER(KIND=JPIM), INTENT(IN)   :: KSOTY(:)
REAL(KIND=JPRB),    INTENT(IN)   :: PLIT(:)
REAL(KIND=JPRB),    INTENT(INOUT):: PMELTSUM(:)
REAL(KIND=JPRB),    INTENT(IN)   :: PFRTI(:,:)
REAL(KIND=JPRB),    INTENT(IN)   :: PSDOR(:)
REAL(KIND=JPRB),    INTENT(IN)   :: PUSTRTI(:,:) 
REAL(KIND=JPRB),    INTENT(IN)   :: PVSTRTI(:,:) 
REAL(KIND=JPRB),    INTENT(IN)   :: PAHFSTI(:,:)
REAL(KIND=JPRB),    INTENT(IN)   :: PEVAPTI(:,:)
REAL(KIND=JPRB),    INTENT(IN)   :: PSSRFLTI(:,:)
REAL(KIND=JPRB),    INTENT(IN)   :: PSNM1M(:)
REAL(KIND=JPRB),    INTENT(IN)   :: PTSNM1M(:)
REAL(KIND=JPRB),    INTENT(IN)   :: PASNM1M(:)
REAL(KIND=JPRB),    INTENT(IN)   :: PRSNM1M(:)
REAL(KIND=JPRB),    INTENT(IN)   :: PTSAM1M(:,:)
REAL(KIND=JPRB),    INTENT(IN)   :: PTIAM1M(:,:)
REAL(KIND=JPRB),    INTENT(IN)   :: PWLM1M(:)
REAL(KIND=JPRB),    INTENT(IN)   :: PWSAM1M(:,:)
REAL(KIND=JPRB),    INTENT(IN)   :: PRSFC(:)
REAL(KIND=JPRB),    INTENT(IN)   :: PRSFL(:)
REAL(KIND=JPRB),    INTENT(IN)   :: PSLRFL(:)
REAL(KIND=JPRB),    INTENT(IN)   :: PSSFC(:)
REAL(KIND=JPRB),    INTENT(IN)   :: PSSFL(:)
REAL(KIND=JPRB),    INTENT(IN)   :: PCVL(:)
REAL(KIND=JPRB),    INTENT(IN)   :: PCVH(:)
REAL(KIND=JPRB),    INTENT(IN)   :: PWLMX(:)
REAL(KIND=JPRB),    INTENT(IN)   :: PEVAPSNW(:)
REAL(KIND=JPRB),    INTENT(IN)   :: PUSRF(:)
REAL(KIND=JPRB),    INTENT(IN)   :: PVSRF(:)
REAL(KIND=JPRB),    INTENT(IN)   :: PTSRF(:)
REAL(KIND=JPRB),    INTENT(IN)   :: PTSPHY
REAL(KIND=JPRB),    INTENT(INOUT):: PZO(:,:)     !KPP
REAL(KIND=JPRB),    INTENT(INOUT):: PHO(:,:)     !KPP
REAL(KIND=JPRB),    INTENT(INOUT):: PHO_INV(:,:) !KPP
REAL(KIND=JPRB),    INTENT(INOUT):: PDO(:,:)     !KPP
REAL(KIND=JPRB),    INTENT(IN)   :: POCDEPTH(:)  !KPP

LOGICAL,   INTENT(IN)   :: LDLAND(:)
LOGICAL,   INTENT(IN)   :: LDSICE(:)
LOGICAL,   INTENT(IN)   :: LDSI
LOGICAL,   INTENT(IN)   :: LDNH(:)
LOGICAL,   INTENT(IN)   :: LDLAKE(:)   
LOGICAL,   INTENT(IN)   :: LDOCN_KPP(:)          !KPP
REAL(KIND=JPRB),    INTENT(IN)   :: PCLAKE(:)

REAL(KIND=JPRB),    INTENT(INOUT):: PROFS(:)
REAL(KIND=JPRB),    INTENT(INOUT):: PSNE1(:)
REAL(KIND=JPRB),    INTENT(INOUT):: PTSNE1(:)
REAL(KIND=JPRB),    INTENT(INOUT):: PASNE1(:)
REAL(KIND=JPRB),    INTENT(INOUT):: PRSNE1(:)
REAL(KIND=JPRB),    INTENT(INOUT):: PTSAE1(:,:)
REAL(KIND=JPRB),    INTENT(INOUT):: PTIAE1(:,:)
REAL(KIND=JPRB),    INTENT(INOUT):: PWLE1(:)
REAL(KIND=JPRB),    INTENT(INOUT):: PWSAE1(:,:)
REAL(KIND=JPRB),    INTENT(INOUT):: PDHWLS(:,:,:)
REAL(KIND=JPRB),    INTENT(INOUT):: PUO0(:,:)  !KPP
REAL(KIND=JPRB),    INTENT(INOUT):: PVO0(:,:)  !KPP
REAL(KIND=JPRB),    INTENT(INOUT):: PUOC(:,:)  !KPP
REAL(KIND=JPRB),    INTENT(INOUT):: PVOC(:,:)  !KPP
REAL(KIND=JPRB),    INTENT(INOUT):: PUSTRC(:)  !KPP
REAL(KIND=JPRB),    INTENT(INOUT):: PVSTRC(:)  !KPP
REAL(KIND=JPRB),    INTENT(INOUT):: PTO0(:,:)  !KPP
REAL(KIND=JPRB),    INTENT(INOUT):: PSO0(:,:)  !KPP
REAL(KIND=JPRB),    INTENT(INOUT):: PUOE1(:,:) !KPP
REAL(KIND=JPRB),    INTENT(INOUT):: PVOE1(:,:) !KPP
REAL(KIND=JPRB),    INTENT(INOUT):: PTOE1(:,:) !KPP
REAL(KIND=JPRB),    INTENT(INOUT):: PSOE1(:,:) !KPP
REAL(KIND=JPRB),    INTENT(INOUT):: PADVT(:,:) !KPP
REAL(KIND=JPRB),    INTENT(INOUT):: PADVS(:,:) !KPP
REAL(KIND=JPRB),    INTENT(INOUT):: PTRI0(:,:) !KPP
REAL(KIND=JPRB),    INTENT(INOUT):: PTRI1(:,:) !KPP
REAL(KIND=JPRB),    INTENT(INOUT):: PSWDK_SAVE(:,:) !KPP

REAL(KIND=JPRB),    INTENT(IN)   :: PSST(:) 

REAL(KIND=JPRB),    INTENT(OUT)  :: PTSDFL(:)
REAL(KIND=JPRB),    INTENT(OUT)  :: PROFD(:)
REAL(KIND=JPRB),    INTENT(OUT)  :: PWFSD(:)
REAL(KIND=JPRB),    INTENT(OUT)  :: PMELT(:)
REAL(KIND=JPRB),    INTENT(OUT)  :: PFWEV(:)
REAL(KIND=JPRB),    INTENT(OUT)  :: PENES(:,:)
REAL(KIND=JPRB),    INTENT(OUT)  :: PDHTSS(:,:,:)
REAL(KIND=JPRB),    INTENT(OUT)  :: PDHTTS(:,:,:)
REAL(KIND=JPRB),    INTENT(OUT)  :: PDHTIS(:,:,:)
REAL(KIND=JPRB),    INTENT(OUT)  :: PDHSSS(:,:,:)
REAL(KIND=JPRB),    INTENT(OUT)  :: PDHIIS(:,:)

REAL(KIND=JPRB),    INTENT(OUT)  :: PDIFM(:,:)    !KPP
REAL(KIND=JPRB),    INTENT(OUT)  :: PDIFT(:,:)    !KPP
REAL(KIND=JPRB),    INTENT(OUT)  :: PDIFS(:,:)    !KPP

REAL(KIND=JPRB)   ,INTENT(IN)    :: PTLICEM1M(:) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTLMNWM1M(:) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTLWMLM1M(:) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTLBOTM1M(:) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTLSFM1M(:) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PHLICEM1M(:) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PHLMLM1M(:) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGEMU(:)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGELAM(:)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PLDEPTH(:)

REAL(KIND=JPRB)   ,INTENT(OUT)    :: PTLICEE1(:) 
REAL(KIND=JPRB)   ,INTENT(OUT)    :: PTLMNWE1(:) 
REAL(KIND=JPRB)   ,INTENT(OUT)    :: PTLWMLE1(:) 
REAL(KIND=JPRB)   ,INTENT(OUT)    :: PTLBOTE1(:) 
REAL(KIND=JPRB)   ,INTENT(OUT)    :: PTLSFE1(:) 
REAL(KIND=JPRB)   ,INTENT(OUT)    :: PHLICEE1(:) 
REAL(KIND=JPRB)   ,INTENT(OUT)    :: PHLMLE1(:) 



!*         0.2    DECLARATION OF LOCAL VARIABLES.
!                 ----------- -- ----- ----------

REAL(KIND=JPRB) :: ZSN(KLON)      ,ZTSN(KLON)        ,ZASN(KLON)   ,&
 & ZRSN(KLON)     ,ZTSA(KLON,KLEVS)      ,ZTIA(KLON,KLEVS)   ,&
 & ZWL(KLON)      ,ZWSA(KLON,KLEVS)  
REAL(KIND=JPRB) :: ZCFW(KLON,KLEVS),ZRHSW(KLON,KLEVS), ZWSADIF(KLON,KLEVS),&
 & ZSAWGFL(KLON,KLEVS),ZCTSA(KLON,KLEVS)  
REAL(KIND=JPRB) :: ZTSFC(KLON),ZTSFL(KLON)
REAL(KIND=JPRB) :: ZRSFC(KLON)    ,&
 & ZRSFL(KLON)    ,&
 & ZSSFC(KLON)    ,ZSSFL(KLON)  
REAL(KIND=JPRB) :: ZLW(KLON), ZPMSNINT(KLON)

REAL(KIND=JPRB) :: ZFWEL1(KLON)   ,ZFWE234(KLON)
REAL(KIND=JPRB) :: ZGSN(KLON) ,ZMSN(KLON),ZEMSSN(KLON),ZEINTTI(KLON,KTILES)
REAL(KIND=JPRB) :: ZCDAWZ(KLON,KLEVS)
REAL(KIND=JPRB) :: ZTSPHY,ZHOH2O,ZSNPERT,ZSNTHRESHOLD

INTEGER(KIND=JPIM) :: JK, JL
REAL(KIND=JPRB) :: ZHOOK_HANDLE

REAL(KIND=JPRB) :: ZTLICE(KLON),ZTLMNW(KLON),ZTLWML(KLON),&           
                   &ZTLBOT(KLON),ZTLSF(KLON),ZHLICE(KLON),ZHLML(KLON),&
                   & ZTSFCIN(KLON),ZTSFLIN(KLON) 

LOGICAL         :: LLLANDICEMASK
!     ------------------------------------------------------------------

!*         1.1   SET UP SOME CONSTANTS, INITIALISE ARRAYS.
!                --- -- ---- -----------------------------
IF (LHOOK) CALL DR_HOOK('SURFTSTP_CTL_MOD:SURFTSTP_CTL',0,ZHOOK_HANDLE)
ZTSPHY=1.0_JPRB/PTSPHY
ZHOH2O=1.0_JPRB/RHOH2O

DO JK=1,KLEVS
  DO JL=KIDIA,KFDIA
    ZCDAWZ(JL,JK)=RDAW(JK)
  ENDDO
ENDDO
DO JL=KIDIA,KFDIA
  ZFWEL1(JL)=0.0_JPRB
  ZFWE234(JL)=0.0_JPRB
  PROFS(JL)=0.0_JPRB

! protect against negative precip

  ZRSFC(JL)=MAX(0.0_JPRB,PRSFC(JL))
  ZRSFL(JL)=MAX(0.0_JPRB,PRSFL(JL))
  ZSSFC(JL)=MAX(0.0_JPRB,PSSFC(JL))
  ZSSFL(JL)=MAX(0.0_JPRB,PSSFL(JL))
ENDDO

CALL SRFENE(KIDIA, KFDIA  , KLON  , KLEVS,& 
 & LDLAND , LDSICE ,&
 & PTSAM1M, KSOTY, PCVL , PCVH ,&
 & PENES)
 

!     ------------------------------------------------------------------
!*         0.1     INTERCEPTION RESERVOIR AND ASSOCIATED RUNOFF CHANGES.

CALL SRFWL(KIDIA,KFDIA,KTILES,&
 & PTSPHY,PWLM1M,PCVL,PCVH,PWLMX,&
 & PFRTI  , PEVAPTI ,ZRSFC,ZRSFL,PEVAPSNW,&
 & ZWL,ZFWEL1,ZTSFC,ZTSFL,ZEINTTI,&
 & LDLAND,PDHIIS)  

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

!*         2.     SNOW CHANGES.
!                 ---- --------
IF ( LESN09 ) THEN
 !* LIQUID WATER DIAGNOSTIC
   CALL SRFSN_LWIMP(KIDIA  ,KFDIA  ,KLON   ,PTSPHY,     &
 & PSNM1M ,PTSNM1M ,PASNM1M,PRSNM1M,PTSAM1M,PHLICEM1M,  &
 & PSLRFL ,PSSRFLTI,PFRTI  ,PAHFSTI,PEVAPTI,            &
 & ZSSFC  ,ZSSFL   ,PEVAPSNW,                           &
 & ZTSFC  ,ZTSFL   ,PUSRF  ,PVSRF   ,PTSRF,             &
 & ZSN    ,ZTSN    ,ZASN   ,ZRSN   ,ZGSN   ,ZMSN,       &
 & ZEMSSN ,ZTSFCIN,ZTSFLIN,                             &

 & PDHTSS , PDHSSS,PLIT)
 
  ! CHANGE THROUGHFALL AT THE SURFACE
  DO JL=KIDIA,KFDIA
    ZTSFC(JL)=ZTSFC(JL)-ZTSFCIN(JL)
    ZTSFL(JL)=ZTSFL(JL)-ZTSFLIN(JL)
  ENDDO

ELSE
 !* BASE ORIGINAL SNOW SCHEME 
   CALL SRFSN(      KIDIA  ,KFDIA  ,KLON   ,PTSPHY,          &
 & PSNM1M ,PTSNM1M ,PASNM1M,PRSNM1M,PTSAM1M,PHLICEM1M,       &
 & PSLRFL ,PSSRFLTI,PFRTI  ,PAHFSTI,PEVAPTI,                 &
 & ZSSFC  ,ZSSFL   ,PEVAPSNW ,                               &
 & ZSN    ,ZTSN    ,ZASN   ,ZRSN   ,ZGSN   ,ZMSN   ,         &
 & ZEMSSN ,                                                  &  
 & PDHTSS , PDHSSS )
 
 ! EXTRA DIAGNOSTICS SET TO ZERO IF SRFSN_LWIMP IS BYPASSED
!  PDHSSS(KIDIA:KFDIA,1,6)=0._JPRB
!  PDHSSS(KIDIA:KFDIA,1,7)=0._JPRB
!  PDHTSS(KIDIA:KFDIA,1,16)=0._JPRB
 
ENDIF

! Take care of permanent snow areas! 
!Permanent snow reset was potentially dangerous at 1m threshold and 9m is used instead.
ZSNPERT=0.9_JPRB*RSNPER   ! snow threshold for where the varying snow albdle scheme is applied
ZSNTHRESHOLD=0.9_JPRB   ! snow threshold for where the varying snow albdle scheme is applied
DO JL=KIDIA,KFDIA
  IF (LLANDICE) THEN
    LLLANDICEMASK = (PLIT(JL) .GT. ZSNTHRESHOLD)
  ELSE
    LLLANDICEMASK = (PSNM1M(JL) >= ZSNPERT)
  ENDIF
 
  IF (LLLANDICEMASK) THEN
    ! apply snow density over permanent snow. Standard IFS setup
    ZRSN(JL)=RHOMAXSN
    IF (LLANDICE) THEN
!  IF (PSNM1M(JL) >= ZSNPERT ) THEN

        ! Varying snow albedo over permanent snow over Greenland (North of 60 N, between 80W-0E) 
!        IF ( ( PGEMU(JL) >= SIN(RPI/3._JPRB) ) .AND. &
!        &    ( ((PGELAM(JL) >= (-RPI*80._JPRB/180._JPRB)).AND.(PGELAM(JL) < 0._JPRB) ) .OR. &
!        &      (PGELAM(JL) >  (RPI*280._JPRB/180._JPRB)) ) ) THEN
!
!           ! but not over Iceland
!           IF (   (PGEMU(JL) >= SIN(RPI*62._JPRB/180._JPRB)) .AND. &  
!           &      (PGEMU(JL) <  SIN(RPI*67._JPRB/180._JPRB)) .AND. &
!           &      ((PGELAM(JL) > (RPI*334._JPRB/180._JPRB)) .OR.  &
!           &       (PGELAM(JL) > (-RPI*26._JPRB/180._JPRB)) .AND. (PGELAM(JL) < 0._JPRB) ) ) THEN
!              ZASN(JL)=RALFMINPSN
!           ENDIF
!        ELSE
!        ENDIF
        ! apply constant snow albedo for Antarctic
        IF ( PGEMU(JL) <= -SIN(2._JPRB*RPI/3._JPRB) ) ZASN(JL)=RALFMINPSN
     ELSE
        ! Standard IFS setup for snow albedo on permanent snow 
        ZASN(JL)=RALFMINPSN
     ENDIF
     
    !=======***NOTE***==========
    ! SETTING ZSN IN PERMANENT SNOW AREAS DOES NOT CONSERVE MASS !!!!
    ! IF MASS IS TO BE CONSERVED THE FOLLOWING LINE SHOULD BE COMMENTED ! 
    ! ZSN(JL)=10000.0_JPRB !reset to glaciers value of 10000 kg/m2 (SWE=10m)
  ENDIF
ENDDO

!     ------------------------------------------------------------------
!*         3.     SOIL HEAT BUDGET.
!                 ---- ---- -------
!*         3.1     COMPUTE SOIL HEAT CAPACITY.

CALL SRFRCG(KIDIA  , KFDIA  , KLON, KLEVS ,&
 & LDLAND , LDSICE ,&
 & PTSAM1M, KSOTY, PCVL   , PCVH   ,ZCTSA)  

!*         3.2     TEMPERATURE CHANGES.
! soil
CALL SRFT(KIDIA  , KFDIA  , KLON   , KLEVS  , &
 & PTSPHY , PTSAM1M, PWSAM1M, KSOTY, PLIT, PMELTSUM,&
 & PFRTI  , PAHFSTI,PEVAPTI ,&
 & PSLRFL ,PSSRFLTI, ZGSN  , &
 & ZCTSA  , ZTSA   ,PTSDFL , LDLAND ,&
 & PDHTTS)  
! sea-ice
IF (LDSI) THEN
  CALL SRFI(&
   & KIDIA  , KFDIA  , KLON   , KLEVS  , KLEVI  , &
   & PTSPHY , PTIAM1M, PFRTI  , PAHFSTI, PEVAPTI, &
   & PSLRFL ,PSSRFLTI, ZTIA   , LDSICE , LDNH   , &
   & PDHTIS)  
ELSE
  DO JK=1,KLEVS
    DO JL=KIDIA,KFDIA
      ZTIA(JL,JK)=PTIAM1M(JL,JK)
      PDHTIS(JL,JK,1:4)=0.0_JPRB
      PDHTIS(JL,JK,9:12)=0.0_JPRB
    ENDDO
  ENDDO
ENDIF

IF (LPISM) THEN
DO JL=KIDIA,KFDIA
IF (PLIT(JL) .GT. 0.1_JPRB) THEN
ZMSN(JL)=ZMSN(JL)+PMELTSUM(JL)
ENDIF
END DO
ENDIF

!     ------------------------------------------------------------------
!*         4.     SOIL WATER BUDGET.
!                 ---- ----- -------
!!*         4.1     INTERCEPTION RESERVOIR AND ASSOCIATED RUNOFF CHANGES.
!
!CALL SRFWL(KIDIA,KFDIA,KTILES,&
! & PTSPHY,PWLM1M,PCVL,PCVH,PWLMX,&
! & PFRTI  , PEVAPTI ,ZRSFC,ZRSFL,PEVAPSNW,&
! & ZWL,ZFWEL1,ZTSFC,ZTSFL,ZEINTTI,&
! & LDLAND,PDHIIS)  

!*         4.2  COMPUTATION OF SOIL MOISTURE DIFFUSIVITY AND RIGHT HAND
!                 SIDES
IF (LEVGEN) THEN
  CALL SRFWEXC_VG(KIDIA,KFDIA,KLON,KLEVS,KTILES,&
 & PTSPHY,KTVL,KTVH,KSOTY,PSDOR,PFRTI,PEVAPTI,&
 & PWSAM1M,PTSAM1M,&
 & ZTSFC,ZTSFL,ZMSN,ZEMSSN,ZEINTTI,PEVAPSNW,&
 & PROFS,ZCFW,ZRHSW,&
 & ZSAWGFL,ZFWEL1,ZFWE234,&
 & LDLAND,PDHWLS)  
ELSE
  CALL SRFWEXC(KIDIA,KFDIA,KLON,KLEVS,KTILES,&
 & PTSPHY,KTVL,KTVH,PFRTI,PEVAPTI,&
 & PWSAM1M,PTSAM1M,&
 & ZTSFC,ZTSFL,ZMSN,ZEMSSN,ZEINTTI,PEVAPSNW,&
 & PROFS,ZCFW,ZRHSW,&
 & ZSAWGFL,ZFWEL1,ZFWE234,&
 & LDLAND,PDHWLS)  
ENDIF


!*         4.3  SOLUTION OF TRIDIAGONAL SYSTEM OF EQUATIONS

CALL SRFWDIF(KIDIA,KFDIA,KLON,KLEVS,PWSAM1M,ZCFW,ZRHSW,ZCDAWZ,ZWSADIF,LDLAND)

!*         4.4  UPDATE SOIL MOISTURE VALUES

CALL SRFWINC(KIDIA,KFDIA,KLEVS,&
 & PTSPHY,PWSAM1M,ZWSADIF,ZSAWGFL,ZCFW,&
 & ZWSA,PWFSD,LDLAND,PDHWLS)  

!*         4.5     WATER CORRECTIONS.

CALL SRFWNG(KIDIA,KFDIA,KLON,PTSPHY,KSOTY,&
 & ZWL,PWLMX,ZWSA,&
 & PROFS,PROFD,PWFSD,&
 & LDLAND,PDHWLS)  

!*         4.6     CONTRIBUTION OF BOTTOM DRAINAGE TO TOTAL RUNOFF.

WHERE (LDLAND(KIDIA:KFDIA)) PROFD(KIDIA:KFDIA)=&
 & PROFD(KIDIA:KFDIA)+ZSAWGFL(KIDIA:KFDIA,KLEVS)*PTSPHY  


!*        4.7     Lake Changes 

IF (LEFLAKE) THEN
  CALL FLAKE_DRIVER( KIDIA, KFDIA, KLON,LDLAKE,&
                 & PSSRFLTI(:,9),PSLRFL(:),PAHFSTI(:,9),PEVAPTI(:,9), &
                 & PUSTRTI(:,9),PVSTRTI(:,9),PFRTI,&
                 & PLDEPTH,PGEMU,PTSPHY,& 
                 & PTLICEM1M,PTLMNWM1M,PTLWMLM1M,PTLBOTM1M,PTLSFM1M, & 
                 & PHLICEM1M,PHLMLM1M,&
                 & ZTLICE,ZTLMNW,ZTLWML,&
                 & ZTLBOT,ZTLSF,ZHLICE,ZHLML )
		 
ELSE ! The prognostic variables are set equal to previous timestep

  ZTLICE  = PTLICEM1M
  ZTLMNW  = PTLMNWM1M
  ZTLWML  = PTLWMLM1M
  ZTLBOT  = PTLBOTM1M
  ZTLSF   = PTLSFM1M
  ZHLICE  = PHLICEM1M
  ZHLML   = PHLMLM1M

ENDIF

!*        4.8     Ocean Mixed layer

IF(LEOCML) THEN
  CALL OCEAN_ML_DRIVER &
 & ( KIDIA    ,KFDIA    ,KLON     ,KLEVO    ,KSTART   ,&
 &   KSTEP    ,LDOCN_KPP,LDSICE   ,PSSRFLTI(:,1),PSLRFL(:),&
 &   PAHFSTI(:,1), PEVAPTI(:,1),PUSTRTI(:,1), PVSTRTI(:,1),&
 &   PUSTRC   ,PVSTRC   ,ZRSFC    ,ZRSFL    ,ZSSFC    ,&
 &   ZSSFL    ,PGEMU    ,PZO      ,PHO      ,PHO_INV  ,&
 &   PDO      ,POCDEPTH ,PUO0     ,PVO0     ,PUOC     ,&
 &   PVOC     ,PTO0     ,PSO0     ,PUOE1    ,PVOE1    ,&
 &   PTOE1    ,PSOE1    ,PADVT    ,PADVS    ,PTSPHY   ,&
 &   PDIFM    ,PDIFT    ,PDIFS    ,PTRI0    ,PTRI1    ,&
 &   PSWDK_SAVE )
ENDIF

!     ------------------------------------------------------------------
!*         5.     COMPUTE TENDENCIES (TEND. FOR TSK COMP. IN VDIFF)
!                 ------- ---------- ------------------------------
!*         5.1   TENDENCIES

DO JL=KIDIA,KFDIA
  PSNE1(JL)   =PSNE1(JL)+&
   & (ZSN(JL)-PSNM1M(JL))*ZTSPHY  
  PTSNE1(JL)  =PTSNE1(JL)+&
   & (ZTSN(JL)-PTSNM1M(JL))*ZTSPHY  
  PASNE1(JL)  =PASNE1(JL)+&
   & (ZASN(JL)-PASNM1M(JL))*ZTSPHY  
  PRSNE1(JL)   =PRSNE1(JL)+&
   & (ZRSN(JL)-PRSNM1M(JL))*ZTSPHY 
  PTLICEE1(JL)   =PTLICEE1(JL)+&              
   & (ZTLICE(JL)-PTLICEM1M(JL))*ZTSPHY        
  PTLMNWE1(JL)   =PTLMNWE1(JL)+&              
   & (ZTLMNW(JL)-PTLMNWM1M(JL))*ZTSPHY        
  PTLWMLE1(JL)   =PTLWMLE1(JL)+&              
   & (ZTLWML(JL)-PTLWMLM1M(JL))*ZTSPHY        
  PTLBOTE1(JL)   =PTLBOTE1(JL)+&              
   & (ZTLBOT(JL)-PTLBOTM1M(JL))*ZTSPHY        
  PTLSFE1(JL)   =PTLSFE1(JL)+&              
   & (ZTLSF(JL)-PTLSFM1M(JL))*ZTSPHY        
  PHLICEE1(JL)   =PHLICEE1(JL)+&              
   & (ZHLICE(JL)-PHLICEM1M(JL))*ZTSPHY        
  PHLMLE1(JL)   =PHLMLE1(JL)+&              
   & (ZHLML(JL)-PHLMLM1M(JL))*ZTSPHY
ENDDO

DO JK=1,KLEVS
  DO JL=KIDIA,KFDIA
    PTIAE1(JL,JK)=PTIAE1(JL,JK)+&
     & (ZTIA(JL,JK)-PTIAM1M(JL,JK))*ZTSPHY  
  ENDDO
ENDDO
! PTSA represents the grid-box average T; the sea-ice fraction
!    has a T representative of the fraction
DO JK=1,KLEVS
  DO JL=KIDIA,KFDIA
    PTSAE1(JL,JK)=PTSAE1(JL,JK)+&
     & (PFRTI(JL,2)*&
     & (ZTIA(JL,JK)-PTIAM1M(JL,JK))+&
     & (1.0_JPRB-PFRTI(JL,1)-PFRTI(JL,2))*&
     & (ZTSA(JL,JK)-PTSAM1M(JL,JK)))*ZTSPHY  
  ENDDO
ENDDO
DO JL=KIDIA,KFDIA
  PWLE1(JL)   =PWLE1(JL)+&
   & (ZWL(JL)-PWLM1M(JL))*ZTSPHY  
ENDDO
DO JK=1,KLEVS
  DO JL=KIDIA,KFDIA
    PWSAE1(JL,JK)=PWSAE1(JL,JK)+&
     & (ZWSA(JL,JK)-PWSAM1M(JL,JK))*ZTSPHY  
  ENDDO
ENDDO

!*         5.2  DIAGNOSTICS

DO JL=KIDIA,KFDIA
  PFWEV(JL) =ZFWEL1(JL)+ZFWE234(JL)
  PROFS(JL) =PROFS(JL)*ZTSPHY
  PROFD(JL) =PROFD(JL)*ZTSPHY
  PTSDFL(JL)=-PTSDFL(JL)
  PMELT(JL) =ZMSN(JL)
ENDDO
IF (LHOOK) CALL DR_HOOK('SURFTSTP_CTL_MOD:SURFTSTP_CTL',1,ZHOOK_HANDLE)

END SUBROUTINE SURFTSTP_CTL
END MODULE SURFTSTP_CTL_MOD
