SUBROUTINE SUPHEC(KULOUT)


!**** *SUPHEC - INITIALISES PHYSICAL CONSTANTS OF UNCERTAIN VALUE.
!               WITHIN THE E.C.M.W.F. PHYSICS PACKAGE

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

!          THIS ROUTINE SETS THE VALUES FOR THE PHYSICAL CONSTANTS USED
!     IN THE PARAMETERIZATION ROUTINES WHENEVER THESE VALUES ARE NOT
!     KNOWN WELL ENOUGH TO FORBID ANY TUNING OR WHENEVER THEY ARE
!     SUBJECT TO AN ARBITRARY CHOICE OF THE MODELLER. THESE CONSTANTS
!     ARE DISTRIBUTED IN COMMON DECKS *YOEXXXX* WHERE XXXX CORRESPONDS
!     TO THE INDIVIDUAL PHYSICAL PARAMETRIZATION

!**   INTERFACE.
!     ----------

!          *SUPHEC* IS CALLED FROM *SUPHY*

!     METHOD.
!     -------

!          NONE.

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

!          *SUVDFS*, *SUVDF*, *SUVEG*, *SUSOIL*, *SURDI*


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

!          SEE PHYSICAL ROUTINES FOR AN EXACT DEFINITION OF THE
!     CONSTANTS.

!     AUTHOR.
!     -------
!          J.-J. MORCRETTE  E.C.M.W.F.    91/06/15  ADAPTATION TO I.F.S.

!     MODIFICATIONS
!     -------------
!          J.-F. Mahfouf E.C.M.W.F.       95/03/01 (For 1D surface scheme)
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!        P.Viterbo     24-May-2004 surf library
!        P. Viterbo   ECMWF   03-12-2004  Include user-defined RTHRFRTI
!        Y. Takaya    ECMWF   07-10-2008  Include flag for ocean ML
!        S. Boussetta/G.Balsamo May 2009  Add switch for variable LAI: LELAIV
!        E. Dutra             16-11-2009  snow 2009 cleaning
!        S. Boussetta/G.Balsamo May 2010  Include CTESSEL switch LECTESSEL
!        G.Balsamo/S. Boussetta June 2011 Include switch LEAGS (for modularity CO2&Evap)
!     ----------------------------------------------------------------

USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOMCST   , ONLY : RD       ,RV       ,RCPD     ,RCPV     ,&
            &RLVTT    ,RLSTT   ,RLMLT    ,RTT      ,RATM     ,&
            &RESTT    ,RPI     ,R        ,RDAY     ,RSIGMA
USE YOETHF   , ONLY : R2ES     ,R3LES    ,R3IES    ,R4LES    ,&
            &R4IES    ,R5LES    ,R5IES   ,RVTMP2   ,RHOH2O   ,&
            &R5ALVCP  ,R5ALSCP  ,RALVDCP ,RALSDCP  ,RALFDCP  ,&
            &RTWAT    ,RTBER    ,RTICE
USE YOERAD   , ONLY : NSW      ,NTSW     ,LONEWSW  ,LCCNL    ,&
            &LCCNO    ,RCCNSEA ,RCCNLND
USE YOMDPHY  , ONLY : NCSS     ,NTILES , YDSURF , NCSNEC , NGPTOT, NLON
USE YOEPHY   , ONLY : RTHRFRTI, LEVGEN   ,LESSRO   ,LESN09   ,&
            &LE4ALB, LEOCWA, LEOCCO, LEOCSA, LEOCLA,&
            &LEFLAKE,LELAIV ,LECTESSEL, LEAGS, RLAIINT, & 
            &LEOCML,LESNML,LELIMITSK5
USE YOELW    , ONLY : NSIL     ,TSTAND   ,XP
USE YOESW    , ONLY : RSUN
USE YOESOIL1S, ONLY : RDAW 
USE YOMCT01S  , ONLY  : LSCMEC   ,LROUGH   ,REXTZ0M  ,REXTZ0H
USE YOMLUN1S  , ONLY : NULNAM

USE YOMECEARTH, ONLY : LLPJGFB, LAMIP, LRDALB,LECEARTH,&
                     & N_COMPUTE_EFF_VEG_FRACTION,&
                     & LRDVEG,LLPJG


IMPLICIT NONE
INTERFACE
#include "susurf.h"
#include "surf_inq.h"
END INTERFACE
#include "suvdfs.intfb.h"
#include "suvdf.intfb.h"
#include "surdi.intfb.h"
#include "surdi1s.intfb.h"
#include "sulwn.intfb.h"
#include "suswn.intfb.h"
INTEGER(KIND=JPIM) :: KULOUT,NCOM,I
REAL(KIND=JPRB)    :: RCIMIN
LOGICAL :: LEOCMTKE

!EC TYPE(TSURF), POINTER :: YSURF
LOGICAL :: LEWBCHECK,LEWBCHECKAbort,LESNCHECK,LESNCHECKAbort,LESKTI5,&
           & LESKTI8,LEWBSOILFIX,LESOILCOND,LESNWBCON
NAMELIST/NAMPHYOFF/ LEWBCHECK,LEWBCHECKAbort,LESNCHECK,LESNCHECKAbort,LESKTI5,&
           & LESKTI8,LEWBSOILFIX,RDAW,LELIMITSK5,LESOILCOND,LESNWBCON,&
           & LLPJGFB, LAMIP, LRDALB,LECEARTH,N_COMPUTE_EFF_VEG_FRACTION, &
           & LRDVEG,LLPJG
!     ------------------------------------------------------------------


!*         0.1    DEFINING DERIVED CONSTANTS FROM UNIVERSAL CONSTANTS
!                 ---------------------------------------------------

!1s RVTMP2=RCPV/RCPD-1.
!1s RHOH2O=RATM/100.
!1s R2ES=RESTT*RD/RV
!1s R3LES=17.269
!1s R3IES=21.875
!1s R4LES=35.86
!1s R4IES=7.66
!1s R5LES=R3LES*(RTT-R4LES)
!1s R5IES=R3IES*(RTT-R4IES)
!1s R5ALVCP=R5LES*RLVTT/RCPD
!1s R5ALSCP=R5IES*RLSTT/RCPD
!1s RALVDCP=RLVTT/RCPD
!1s RALSDCP=RLSTT/RCPD
!1s RALFDCP=RLMLT/RCPD
!1s RTWAT=RTT
!1s RTBER=RTT-5.
!1s RTICE=RTT-23.

!RVTMP2=RCPV/RCPD-1.0_JPRB   !use cp,moist
RVTMP2=0.0_JPRB              !neglect cp,moist
RHOH2O=RATM/100._JPRB
R2ES=611.21_JPRB*RD/RV
R3LES=17.502_JPRB
R3IES=22.587_JPRB
R4LES=32.19_JPRB
R4IES=-0.7_JPRB
R5LES=R3LES*(RTT-R4LES)
R5IES=R3IES*(RTT-R4IES)
R5ALVCP=R5LES*RLVTT/RCPD
R5ALSCP=R5IES*RLSTT/RCPD
RALVDCP=RLVTT/RCPD
RALSDCP=RLSTT/RCPD
RALFDCP=RLMLT/RCPD
RTWAT=RTT
RTBER=RTT-5._JPRB
RTICE=RTT-23._JPRB



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

!*         1.     SETTING CONSTANTS FOR VERTICAL DIFFUSION
!                 ----------------------------------------

CALL SUVDFS

CALL SUVDF

!*         2.     SETTING CONSTANTS FOR RADIATION 
!                 -------------------------------


NCOM    = 1
NTSW   = 6 !4
NSW    = 6 !2
LONEWSW=.TRUE.
LCCNL  =.FALSE.
LCCNO  =.FALSE.
RCCNSEA = 0.
RCCNLND = 0.
CALL SURDI
CALL SURDI1S
CALL SULWN
CALL SUSWN   (NTSW,NSW)

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

!*         3.     SETTING CONSTANTS FOR SURFACE SCHEME
!                 ------------------------------------
LEOCMTKE =.FALSE.
RCIMIN=0.005_JPRB

!EC
! CALL SUSURF(KSW=NSW,KCSS=NCSS,KCSNEC=NCSNEC,KSIL=NSIL,KCOM=NCOM,KTILES=NTILES,KTSW=NTSW,&
!     & LD_LEFLAKE=LEFLAKE, LD_LEOCML=LEOCML, LD_LOCMLTKE=LEOCMTKE,&
!     & LD_LLCCNL=LCCNL,LD_LLCCNO=LCCNO,LD_LEVGEN=LEVGEN,LD_LESSRO=LESSRO,&
!     & LD_LELAIV=LELAIV,LD_LECTESSEL=LECTESSEL,LD_LEAGS=LEAGS,LD_LESN09=LESN09,&
!     & LD_LESNML=LESNML,LD_LEOCWA=LEOCWA,LD_LEOCCO=LEOCCO,LD_LEOCSA=LEOCSA,LD_LEOCLA=LEOCLA,&
!     & LD_LLE4ALB=LE4ALB,&
!     & LD_LSCMEC=LSCMEC,LD_LROUGH=LROUGH,PEXTZ0M=REXTZ0M,PEXTZ0H=REXTZ0H,&
!     & PTHRFRTI=RTHRFRTI,PTSTAND=TSTAND,PXP=XP,PRCCNSEA=RCCNSEA,PRCCNLND=RCCNLND,&
!     & PRLAIINT=RLAIINT,PRSUN=RSUN,PRCORIOI=1._JPRB,PRPLRG=1._JPRB,YDSURF=YDSURF,PRCIMIN=RCIMIN) 
!EC 

CALL SUSURF(KSW=NSW,KCSS=NCSS,KSIL=NSIL,KTILES=NTILES,KTSW=NTSW,&
    & LD_LEFLAKE=LEFLAKE, LD_LEOCML=LEOCML, &
    & LD_LLCCNL=LCCNL,LD_LLCCNO=LCCNO,LD_LEVGEN=LEVGEN,LD_LESSRO=LESSRO,&
    & LD_LELAIV=LELAIV,LD_LESN09=LESN09,&
    & LD_LEOCWA=LEOCWA,LD_LEOCCO=LEOCCO,LD_LEOCSA=LEOCSA,LD_LEOCLA=LEOCLA,&
    & LD_LLE4ALB=LE4ALB,&
    & LD_LSCMEC=LSCMEC,LD_LROUGH=LROUGH,PEXTZ0M=REXTZ0M,PEXTZ0H=REXTZ0H,&
    & PTHRFRTI=RTHRFRTI,PTSTAND=TSTAND,PXP=XP,PRCCNSEA=RCCNSEA,PRCCNLND=RCCNLND,&
    & PRSUN=RSUN)

!GPB change
!CALL SUVEG

!* get back some soil parameters used elsewhere in the code
!
IF (.NOT.ALLOCATED(RDAW)) ALLOCATE(RDAW(NCSS))
!ED CALL SURF_INQ(YDSURF,PRDAW=RDAW)
CALL SURF_INQ(PRDAW=RDAW)
!     ------------------------------------------------------------------
WRITE(UNIT=KULOUT,FMT=*) 'PRDAW1',RDAW


!* Workaround to set special flags for ec-earth configuration 
!* Defaults when running offline 

REWIND(NULNAM)
READ(NULNAM,NAMPHYOFF)
WRITE(UNIT=KULOUT,FMT='('' LAMIP  = '',L5)') LAMIP
WRITE(UNIT=KULOUT,FMT='('' LECEARTH  = '',L5)') LECEARTH
WRITE(UNIT=KULOUT,FMT='('' LLPJGFB  = '',L5)') LLPJGFB
WRITE(UNIT=KULOUT,FMT='('' LRDALB  = '',L5)') LRDALB
WRITE(UNIT=KULOUT,FMT='('' N_COMPUTE_EFF_VEG_FRACTION  = '',I3)') N_COMPUTE_EFF_VEG_FRACTION
WRITE(UNIT=KULOUT,FMT='('' LRDVEG  = '',L5)') LRDVEG
WRITE(UNIT=KULOUT,FMT='('' LLPJG  = '',L5)') LLPJG
IF (LLPJG) THEN
  IF (NLON == 25799) THEN
    NGPTOT= 88838
  ELSE IF (NLON == 10407) THEN
    NGPTOT= 35718
  ELSE
    NGPTOT=-1
    WRITE(KULOUT,'(A)') "NGPTOT not set something will go wrong in OASIS"
  ENDIF
ENDIF
  
!EC - can only be done in CY43* onwards, off for EC-EARTH
!! Many "workaround" setting of surface constants skiiping susurf interface... This only works in offline mode!!! 

! YSURF => GET_SURF(YDSURF)
! !set defaults - defined in sussoil 
! LEWBCHECK=YSURF%YSOIL%LEWBCHECK
! LEWBCHECKAbort=YSURF%YSOIL%LEWBCHECKAbort
! LESNCHECK=YSURF%YSOIL%LESNCHECK
! LESNCHECKAbort=YSURF%YSOIL%LESNCHECKAbort
! LESKTI5=YSURF%YSOIL%LESKTI5
! LESKTI8=YSURF%YSOIL%LESKTI8
! LESOILCOND=YSURF%YSOIL%LESOILCOND
! LELIMITSK5=.FALSE.
! LEWBSOILFIX=YSURF%YSOIL%LEWBSOILFIX
! LESNWBCON=YSURF%YSOIL%LESNWBCON
! ! read namelist 
! REWIND(NULNAM)
! READ(NULNAM,NAMPHYOFF)

! update buffer 
! YSURF%YSOIL%LEWBCHECK=LEWBCHECK
! YSURF%YSOIL%LEWBCHECKAbort=LEWBCHECKAbort
! YSURF%YSOIL%LESNCHECK=LESNCHECK
! YSURF%YSOIL%LESNCHECKAbort=LESNCHECKAbort
! YSURF%YSOIL%LESKTI5=LESKTI5
! YSURF%YSOIL%LESKTI8=LESKTI8
! YSURF%YSOIL%LEWBSOILFIX=LEWBSOILFIX
! YSURF%YSOIL%LESOILCOND=LESOILCOND
! YSURF%YSOIL%LESNWBCON=LESNWBCON
! DO I=1,NCSS
!   YSURF%YSOIL%RDAT(I) = RDAW(I)
!   YSURF%YSOIL%RDAW(I) = RDAW(I)
!   YSURF%YSOIL%RDAI(I) = RDAW(I)
! ENDDO

! CALL SURF_INQ(YDSURF,PRDAW=RDAW)
! WRITE(UNIT=KULOUT,FMT=*) 'PRDAW2',RDAW
! WRITE(UNIT=KULOUT,FMT='('' YSURF%YSOIL%LEWBCHECK  = '',L5)') YSURF%YSOIL%LEWBCHECK
! WRITE(UNIT=KULOUT,FMT='('' YSURF%YSOIL%LEWBCHECKAbort  = '',L5)') YSURF%YSOIL%LEWBCHECKAbort
! WRITE(UNIT=KULOUT,FMT='('' YSURF%YSOIL%LESNCHECK  = '',L5)') YSURF%YSOIL%LESNCHECK
! WRITE(UNIT=KULOUT,FMT='('' YSURF%YSOIL%LESNCHECKAbort  = '',L5)') YSURF%YSOIL%LESNCHECKAbort
! WRITE(UNIT=KULOUT,FMT='('' YSURF%YSOIL%LESKTI5  = '',L5)') YSURF%YSOIL%LESKTI5
! WRITE(UNIT=KULOUT,FMT='('' YSURF%YSOIL%LESKTI8  = '',L5)') YSURF%YSOIL%LESKTI8
! WRITE(UNIT=KULOUT,FMT='('' YSURF%YSOIL%LEWBSOILFIX  = '',L5)') YSURF%YSOIL%LEWBSOILFIX
! WRITE(UNIT=KULOUT,FMT='('' YSURF%YSOIL%LESOILCOND  = '',L5)') YSURF%YSOIL%LESOILCOND
! WRITE(UNIT=KULOUT,FMT='('' YSURF%YSOIL%LESNWBCON  = '',L5)') YSURF%YSOIL%LESNWBCON
! WRITE(UNIT=KULOUT,FMT='('' LELIMITSK5  = '',L5)') LELIMITSK5
! WRITE(UNIT=KULOUT,FMT='('' SUPHEC IS OVER '')')

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

RETURN
END SUBROUTINE SUPHEC
