SUBROUTINE SUMCC(KULOUT)

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

USE YOEPHY   , ONLY : LECURR   ,LE4ALB   ,LELAIV
USE YOMLUN   , ONLY : NULOUT   ,NULNAM
USE YOMMCC   , ONLY : N923     ,NCLIMTOT ,NCLIMR   ,NCLIMR_SEA, NCLIGC  , &
 & NDIFC    ,NJDCR    ,NP1     ,NP2      , CLIMR   , CLIMR_SEA, &
 & NCLIAER  ,NCLIGC_SEA, LRDISTL1, &
 & NOACOMM  ,LMCC01   ,LMCC02   ,LMCC03   ,LMCC04   ,&
 & LMCCEC   ,LMCCIEC  ,LMCC12   ,LMCC19   ,LN923    ,LMCCICEIC,&
 & LMCC05   ,NFRCPL   ,NFRCPL_CHE

USE YOMCT0   , ONLY : NCONF    ,NSTOP    ,NFRCO     ,NFRCO_CHEM , LTM5CO2, NPRINTLEV, LALLOPR, NFRCO_VEG
USE YOMCT0B  , ONLY : LECMWF
USE YOMDYN   , ONLY : TSTEP
USE YOMGEM   , ONLY : NGPTOT

USE YOMMP    , ONLY : MYPROC
USE YOMCOM  , ONLY :  SSTMSK   ,OMLDTH   ,GTTLIN   ,SSTPRE
USE YOM_YGFL , ONLY : NACTAERO

USE YOMECEARTH, ONLY : LECEARTH, LAMIP, LECEARTH_UNCOUP
USE YOMECEARTH, ONLY : LTM5, LLPJG, LLPJGFB
USE YOERAD,     ONLY : LLPJGON, LLPJGFBON, LTM5O3, LTM5CH4, LTM5AER
USE YOMECEARTH, ONLY : LRDALB, LRDVEG, LPISM, LLANDICE, LRDLANDICE

!**** *SUMCC* * - ROUTINE TO INITIALIZE SWITCHES FOR THE CLIMATE VERSION

!     PURPOSE.
!     --------
!        SET DEFAULT VALUES, THEN READS NAMELIST NAMMCC

!**   INTERFACE.
!     ----------
!        *CALL* *SUMCC(KULOUT)*

!     EXPLICIT ARGUMENTS :  KULOUT
!     --------------------

!     IMPLICIT ARGUMENTS :
!     --------------------
!        COMMON  YOMMCC

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

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

!     AUTHOR.
!     -------
!        Michel Deque *CNRM*

!     MODIFICATIONS.
!     --------------
!        ORIGINAL : 92-09-15
!        Modified by R. EL Khatib : 93-04-02 Set-up defaults controled by LECMWF
!        Modified by M. Deque     : 94-08-29 adds LMCC19 for aerosols
!        Modified by M. Deque     : 95-01-02 adds LMCC20 for soil moisture
!        Modified by M.Hamrud     : 95-02-14 removed read of namdyn,
!                                   routine now called after SUDYN
!        Modified by T. Stockdale : 95-08-24 adds LMCC04 for IFS coupling
!        Modified by D. Giard     : 97-03-13 New 923 -> LN923, N923; LMCC13M
!        Modified by P. Viterbo   : 97-10-07 New version of upcliedm
!                                       NCLIM,NCLIGC,NDIFC,NJDCR,NP1,NP2
!                                       and allocate of array CLIMR
!        Modified by T. Stockdale : 98-07-16 New updcliedm for OASIS coupling
!        Modified by S. Kertesz   : 99-12-16 N923=7 allowed
!        Modified by D. Giard     : 00-09-12 cleaning of old 923
!        Modified by M.Hamrud     : 04-10-01 CY28 Cleaning
!        Modified by D. Giard     : 04-09-15 new 923 configurations allowed
!        Modified by A.Alias      : GMEGEC/EAC list of modifications
!                    JPh. Piedelievre : coupled model in distributed memory mode.
!                                       coupled model in UPDCPL mode (LMCC05,NFRCPL)
!        JJMorcrette, 20060605 MODIS albedo
!        JJMorcrette, 20061026 DU, BC, OM, SU, VOL, SOA climatological fields
!        S. Boussetta/G.Balsamo      May 2009   (Add switch for variable LAI: LELAIV)
!        T. van Noije             :  Jan 2014 Modified for coupling to chemistry module in EC-Earth

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

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KULOUT 
INTEGER(KIND=JPIM)               :: IOPROC, ISTEP, ITIM
REAL(KIND=JPRB) :: ZHOOK_HANDLE
LOGICAL    :: LLP

#include "abor1.intfb.h"
#include "inicou.intfb.h"
#include "posnam.intfb.h"
#include "sucom.intfb.h"

#include "nammcc.h"
!      ----------------------------------------------------------------

!*       1.    SET DEFAULT VALUES.
!              -------------------

!        1.1 Set implicit default values

IF (LHOOK) CALL DR_HOOK('SUMCC',0,ZHOOK_HANDLE)
LMCC01 = .FALSE.
LMCC02 = .FALSE.
LMCC03 = .FALSE.
LMCC04 = .FALSE.
LMCC05 = .FALSE.
LMCC12 = .FALSE.
LMCC19 = .FALSE.
LMCCEC = .FALSE.
LMCCIEC= .TRUE.
LMCCICEIC = .FALSE.
LN923  = .FALSE.
N923 = 0
NCLIGC=-999
NOACOMM=2
NFRCPL=999999
NFRCPL_CHE=-999999
LRDISTL1 = .FALSE.

!     1.2 Modify default values according to LECMWF

IF (LECMWF) THEN
ELSE
  IF (NCONF == 923) LN923 = .TRUE.
ENDIF

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

!*       2.    MODIFIES DEFAULT VALUES.
!              ------------------------

CALL POSNAM(NULNAM,'NAMMCC')
READ(NULNAM,NAMMCC)

N923 = MAX(0,MIN(11,N923))
IF (N923  ==  0) LN923 = .FALSE.
IF (LN923) THEN
  LMCC12 = .FALSE.
  LMCC19 = .FALSE.
ENDIF
IF (NFRCPL <= 0) THEN
  NFRCPL=NSTOP+1
  WRITE(KULOUT,*) ' IN SUMCC, NFRCPL REPLACED BY : ',NFRCPL
ENDIF
LLP = NPRINTLEV >= 1.OR. LALLOPR
IF(LMCC05.AND.NFRCPL <= NSTOP) THEN
  ALLOCATE(OMLDTH(NGPTOT))
  IF(LLP)WRITE(KULOUT,9) 'OMLDTH    ',SIZE(OMLDTH),SHAPE(OMLDTH)
  ALLOCATE(GTTLIN(NGPTOT))
  IF(LLP)WRITE(KULOUT,9) 'GTTLIN    ',SIZE(GTTLIN),SHAPE(GTTLIN)
  ALLOCATE(SSTPRE(NGPTOT))
  IF(LLP)WRITE(KULOUT,9) 'SSTPRE    ',SIZE(SSTPRE),SHAPE(SSTPRE)
  ALLOCATE(SSTMSK(NGPTOT))
  IF(LLP)WRITE(KULOUT,9) 'SSTMSK    ',SIZE(SSTMSK),SHAPE(SSTMSK)
ENDIF
! To initialize the YOMCOM common used by the COM coupling.

IF (LMCCEC) THEN  !BOUNDARY CONDITIONS UPDATED BY ECMWF ROUTINES
  NFRCPL=NFRCO
  NFRCPL_CHE=NFRCO_CHEM

  ! this works for EC-EARTH
  NCLIMR=0
  IF (LRDALB) THEN
    NCLIGC(NCLIMR+1)=174
    NCLIMR=NCLIMR+1
    IF (LE4ALB) THEN
      NCLIGC(NCLIMR+1)=15
      NCLIGC(NCLIMR+2)=16
      NCLIGC(NCLIMR+3)=17
      NCLIGC(NCLIMR+4)=18
      NCLIMR=NCLIMR+4
    ENDIF
  ENDIF
  IF (LELAIV) THEN
    NCLIGC(NCLIMR+1)=66
    NCLIGC(NCLIMR+2)=67
    NCLIMR=NCLIMR+2
  ENDIF
  IF (LRDVEG) THEN
    ! vegetation cover and type
    NCLIGC(NCLIMR+1)=27
    NCLIGC(NCLIMR+2)=28
    NCLIGC(NCLIMR+3)=29
    NCLIGC(NCLIMR+4)=30
    NCLIMR=NCLIMR+4
  ENDIF
  ! Currently the land ice mask has to be read in from the initial ICMGG file (i.e, LRDLANDICE=.TRUE.). 
  ! But ideally (in the future) one could use the snow depth as the ice mask, so it would be possible to 
  ! have different combinatio of the switches.
  IF (LLANDICE) THEN 
    LRDLANDICE=.TRUE.
  ENDIF
  IF (LPISM) THEN
    LLANDICE=.TRUE.
    LRDLANDICE=.TRUE.
  ENDIF

  NCLIMTOT=NCLIMR

  IF (LMCCIEC) THEN !THE BOUNDARY CONDITIONS (SST) ARE INTERPOLATED IN TIME
    NP1=1
    NP2=2
    ITIM=2
  ELSE !THE BOUNDARY CONDITIONS (SST) ARE NOT INTERPOLATED IN TIME
    NP1=1
    NP2=NP1
    ITIM=1
  ENDIF !END THE BOUNDARY CONDITIONS (SST) ARE INTERPOLATED IN TIME

  IF(NCLIMR == 0) THEN
    NDIFC=-999
    NJDCR=-999
  ENDIF
  
  ! Check that enough space is available (4 extra GRIB codes may be added in updclie.F90)
  IF ((NCLIMR+4) > SIZE(NCLIGC)) THEN
     CALL ABOR1('SUMCC ERROR : NCLIGC TOO SMALL ')
  ENDIF
  
! Allocates the necessary space

  ALLOCATE(CLIMR(NGPTOT,ITIM,NCLIMTOT))
  IF(NPRINTLEV >= 1.OR. LALLOPR)&
   & WRITE(NULOUT,"(1X,'ARRAY ',A10,' ALLOCATED ',8I8)")&
   & 'CLIMR     ',SIZE(CLIMR   ),SHAPE(CLIMR    )  

ENDIF  !END BOUNDARY CONDITIONS UPDATED BY ECMWF ROUTINES

CALL SUCOM (KULOUT)

9 FORMAT(1X,'ARRAY ',A10,' ALLOCATED ',8I8)

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

!*       3.    PRINTS FINAL VALUES.
!              --------------------

WRITE(UNIT=KULOUT,FMT='('' COMMON YOMMCC '')')
WRITE(UNIT=KULOUT,FMT='(&
 & '' LMCC01 = '',L2,'' LMCC02 = '',L2,'' LMCC03 = '',L2, &
 & '' LMCC04 = '',L2,'' LMCCEC = '',L2,'' LMCCIEC= '',L2/ &
 & '' LMCCICEIC= '',L2,'' LMCC12 = '',L2,'' LMCC19 = '',L2/ &
 & '' LN923 = '',L2,'' N923 ='',I2,'' LMCC05 = '',L2, &
 & '' LPISM = '',L5,'' LLANDICE ='',L5,'' LRDLANDICE = '',L5 &
 & '' NFRCPL = '',I6,'' NFRCPL_CHE = '',I6)')&
 & LMCC01,LMCC02,LMCC03,LMCC04,LMCCEC,LMCCIEC,LMCCICEIC, &
 & LMCC12,LMCC19,LN923,N923,LMCC05,LPISM, LLANDICE, LRDLANDICE, NFRCPL, NFRCPL_CHE 
!      ----------------------------------------------------------------

!*       4.    PREPARES SIGNAL FOR OCEAN COUPLER.
!              ----------------------------------

! With OASIS, IOPROC must be 1. (see wrcodm.F)
IOPROC=1

IF((LMCC03).OR.(LMCC04))THEN
  ISTEP=INT(TSTEP)
  IF (NOACOMM == 5) THEN
     WRITE(NULOUT,'(A)')'OASIS3 run in SUMCC'
  ELSE
  IF (LMCC03) THEN
    IF (MYPROC == IOPROC) CALL INICOU(NSTOP,NFRCPL,ISTEP)
  ELSE
    CALL INICOU(NSTOP,NFRCPL,ISTEP)
  ENDIF
  ENDIF
ENDIF

!*       5.    SET FLAG FOR EC-EARTH.
!              ----------------------

IF (LMCC04 .AND. NOACOMM==5) THEN
  IF (NFRCPL>0 .OR. NFRCPL_CHE>0 .OR. NFRCO_VEG>0) THEN
     LECEARTH=.TRUE.
  ENDIF
  IF (NFRCPL>0) THEN
    LRDISTL1=.TRUE.
  ELSE
    LAMIP=.TRUE.
    LECEARTH_UNCOUP=.TRUE.
! this block of the code is left here (commented) if anybody wants to
! reactivate the old ICMSEA forcing
!    !KW! allocate space for sst and sea-ice
!    NCLIGC_SEA=-999
!    IF (LRDISTL1) THEN
!      NCLIMR_SEA=3
!      NCLIGC_SEA(1:3)=(/34,31,35/)
!    ELSE
!      NCLIMR_SEA=2
!      NCLIGC_SEA(1:2)=(/34,31/)
!    ENDIF
!    ALLOCATE(CLIMR_SEA(NGPTOT,ITIM,NCLIMR_SEA))
!    IF(NPRINTLEV >= 1.OR. LALLOPR)&
!     & WRITE(NULOUT,"(1X,'ARRAY ',A10,' ALLOCATED ',8I8)")&
!     & 'CLIMR_SEA ',SIZE(CLIMR_SEA),SHAPE(CLIMR_SEA)  
 ENDIF
 
  IF (NFRCPL_CHE>0) THEN
     LTM5=.TRUE.
  ELSE
     ! These have been read in SUECRAD. Cannot be T, if no coupling with TM.
     LTM5AER=.FALSE.
     LTM5O3 =.FALSE.
     LTM5CH4=.FALSE.
     ! This has been read in SU
     LTM5CO2=.FALSE.
  ENDIF

  ! LPJ-GUESS switches
  IF (LECEARTH) THEN
     IF (LLPJGON) THEN
         LLPJG=.TRUE.
     ELSE
         LLPJG=.FALSE.
     ENDIF

     IF (LLPJGFBON) THEN
         LLPJGFB=.TRUE.
     ELSE
         LLPJGFB=.FALSE.
     ENDIF
  ENDIF

  WRITE(NULOUT,'(A,L2)') ' LPJG coupled run = ', LLPJG
  WRITE(NULOUT,'(A,L2)') ' LPJG coupled run (with feedback) = ', LLPJGFB
  WRITE(NULOUT,'(A,L2)') ' TM5 coupled run = ', LTM5
  WRITE(NULOUT,'(A,L2)') ' TM5-CO2-only run = ',LTM5CO2
  WRITE(NULOUT,'(A,3L2)')' TM5 feedback (O3, CH4, AEROSOLS) = ', LTM5O3, LTM5CH4, LTM5AER
  WRITE(NULOUT,'(A,L2)') ' NEMO coupled run = ', .NOT.LECEARTH_UNCOUP
  WRITE(NULOUT,'(A,L2)')' AMIP coupled run = ', LAMIP
ENDIF
!     ------------------------------------------------------------------

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