SUBROUTINE UPDTIM(KSTEP,PTDT,PTSTEP,LDCLUPD)

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

USE YOMDIM   , ONLY : NSMAX    ,NTOTFNUDG
USE YOMCT0   , ONLY : LTWOTL   ,NFRHIS   ,NFRCO    ,LSCMEC
USE YOMLUN   , ONLY : NULOUT  , NULERR
USE YOMRIP   , ONLY : NINDAT   ,NSSSSS   ,NSTADD   ,NSTASS   ,&
 & RTIMST   ,RSTATI   ,RTIMTR   ,RHGMT    ,REQTIM   ,&
 & RSOVR    ,RDEASO   ,RDECLI   ,RWSOVR   ,RIP0     ,&
 & RCODEC   ,RSIDEC   ,RCOVSR   ,RSIVSR   ,RDTSA    ,&
 & RDTSA2   ,RDTS62   ,RDTS22   ,RTDT     ,RTMOLT   ,&
 & RSIVSRLU ,RCOVSRLU ,RDECLU   ,RSIDECLU ,RCODECLU  
USE YOMCST   , ONLY : RPI      ,RDAY     ,REA      ,REPSM    ,&
 & RA       ,RI0      ,RV       ,RCPV     ,RETV     ,&
 & RCW      ,RCS      ,RLVTT    ,RLSTT    ,RTT      ,&
 & RALPW    ,RBETW    ,RGAMW    ,RALPS    ,RBETS    ,&
 & RGAMS    ,RALPD    ,RBETD    ,RGAMD  
USE YOMDPHY  , ONLY : NTSSG
USE SURFACE_FIELDS_MIX  , ONLY : YSD_VFD, YSD_VPD, YSD_VVD ,YSD_VAD
USE YOMPHY   , ONLY : LMPHYS   ,LOZONE   ,LRAYFM   ,LSOLV    ,LRAYFM15, LRAYLU
USE YOMPHY2  , ONLY : TSPHY
USE YOMPHY3  , ONLY : RII0
USE YOMMCC   , ONLY : LMCC01   ,LMCC03   ,LMCC04   ,LMCC05   ,LMCCEC  , NFRCPL , NPCO2_1 , NPCO2_2,&
 & CLIMRCO2
USE YOMNUD   , ONLY : NFNUDG  ,LNUDG    ,LWNUDG
USE YOMSNU   , ONLY : XPNUDG   ,XWNUDG
USE YOMRLX   , ONLY : NFRLXG   ,NFRLXU   ,LRLXG
USE YOMSRLX  , ONLY : XPRLXG
USE YOMDYN   , ONLY : TSTEP
USE YOEPHY   , ONLY : LEPHYS   ,LEO3CH ,LECO2DIU   ,LEOCML
USE YOERAD   , ONLY : NOZOCL   ,NRADFR   ,NRADNFR  ,NRADSFR  ,NUV ,&
 & LERAD1H  ,LHVOLCA  ,LNEWAER  ,LECO2VAR, LHGHG, NLNGR1H , NHINCSOL,LPERPET,&
 & NGHGRAD, LESO4HIS, LCMIP5, LCMIP6, LCMIP6_PI_AEROSOLS, LCMIP6_STRATAER_FULL,&
 & LTM5O3
USE YOE_UVRAD, ONLY : RSUVB0   ,RSUVB    ,LUVPROC
USE YOMDYNCORE, ONLY : LAQUA
USE YOERDI  , ONLY : RSOLINC   ,RCARDI
USE YOMRAD15 , ONLY :           NRADFR15 ,NRADNFR15,NRADSFR15,LERAD6H15
USE YOERIP   , ONLY : RCODECM  ,RSIDECM  ,RCOVSRM  ,RSIVSRM
USE YOMRIP15 , ONLY : RCODECM15,RSIDECM15,RCOVSRM15,RSIVSRM15
USE YOEPHLI  , ONLY : LPHYLIN
USE YOMGEM   , ONLY : NGPTOT

USE YOMECEARTH, ONLY : LECEARTH, LAMIP
USE YOMECEARTH, ONLY : LTM5, LLPJG, LLPJGFB
USE CPLNG
!
USE YOMORB, ONLY : LCORBMD, ORBCALDAY, ORBCALDAYM, SHR_ORB_UPD

!**** *UPDTIM*  - UPDATE TIME OF THE MODEL

!     Purpose.
!     --------
!     UPDATE TIME OF THE MODEL

!**   Interface.
!     ----------
!        *CALL* *UPDTIM(KSTEP,PTDT,PTSTEP,LDCLUPD)

!        Explicit arguments :
!        --------------------
!        KSTEP   : TIME STEP INDEX
!        PTDT    : TIME STEP LEAPFROG
!        PTSTEP  : TIME STEP
!        LDCLUPD : .TRUE. IF CALLED DURING A MODEL RUN EITHER:
!                       1) DURING INITIALISATION OF TENDENCIES
!                    OR 2) AFTER NMI IS COMPLETED
!                    (ONLY RELEVANT FOR A CLIMATE FILE UPDATE)

!        Implicit arguments :
!        --------------------
!        YOMRIP

!     Method.
!     -------
!        See documentation

!     Externals.
!     ----------

!     Reference.
!     ----------
!        ECMWF Research Department documentation of the IFS

!     Author.
!     -------
!        Mats Hamrud and Philippe Courtier  *ECMWF*

!     Modifications.
!     --------------
!        Original : 87-10-15
!        modified by Michel Deque 91-02-28  Update of climatic fields
!        Modified DEC 1992 by K. YESSAD: changing RDTFLS2 into RTDT (=PTDT).
!        modified by Michel Deque 94-09-05  Update of climatic fields
!        Modified by Pedro Viterbo: Update of SST for ECMWF long runs
!        Modified J.-J. Morcrette: 95-06-29 2time-levels for radiation
!        Modified J.-J. Morcrette: 96-03-08 intermittent EC radiation
!        Modified by A. Untch: 96-04 Update of O3 chemistry (ECMWF style)
!        Modified M. Deque: 96-07-02 nudging fields (UPDNUD)
!        Modified M. Deque: 98-03-04 nudging fields (UPDNUDSM) and frozen FMR
!        Modified by Pedro Viterbo: 97-10 Add LDCLUPD, to help update of
!                                        SST for ECMWF long runs
!        Modified by J.M. Piriou: 99-04 Moon radiation
!        Modified by J.-J. Morcrette: 98-12 GISS volcanic aer. climatol. 
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!        M.Hamrud      20-Apr-2004 Save time by supressing repeated calls 
!                                  to SUECAEOR etc.
!        Modified by A. Alias: 05-08-30 GMGEC/EAC modif.
!                    M. Deque   -  nudging with model data
!                    JPh Piedelievre - Coupled modes (UPDCPL)
!                    M. Deque   - nudging with time-variable coefficients
!                    P. Marquet - LWNUDG added
!                    M. Deque   - Call to CORMASS2
!                    M. Deque   - grid point nudging
!        M.Janiskova 060111 bug-fix for use of new aerosols in TL/AD
!        S.Serrar  : 06-01-01 time updating of CO2 surface fields
!        M. Ko"hler  6-6-2006 Single Column Model option (LSCMEC) 
!        M.Hamrud      01-Jul-2006 Revised surface fields
!        Modified by A. Alias: 06-03-10 clean up
!        JJMorcrette 20080318 O.Boucher SO4 climatol (Observ and A1B)
!        JJMorcrette 20080423 3D climatologies of CO2, CH4, N2O for radiation
!        Modified by A. Alias: 07-10-19 NTOTFNUDG  moved to YOMDIM
!        Y. Takaya 01-Feb-2009 call UPDCLIE for the ocean mixed layer model
!        L. Batte (IC3) May 2013: adding necessary interpolation coeffs for
!                                     IFS relaxation
!        T. van Noije Jan-2014 Including option for receiving TM5 fields (for EC-Earth)
!     ------------------------------------------------------------------

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KSTEP 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTDT 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTSTEP 
LOGICAL           ,INTENT(IN)    :: LDCLUPD 
INTEGER(KIND=JPIM) :: IBASE, IFNUDG, IFRHIS, IGP, IMINUT, IOZOCL15, IPR, ISTADD,&
 & ISTASS, ISTASS0, ISTP1, ISTPF, ITIME, JSTEP, IFRLXG  
INTEGER(KIND=JPIB) :: IZT
INTEGER(KIND=JPIM) :: ISEC, ICNT, ISTEP, IUPGHG
LOGICAL :: LLCPL

REAL(KIND=JPRB) :: ZANGOZC, ZCOTHOZ, ZDEASOM, ZDECLIM, ZDEL,&
 & ZEQTIMM, ZHGMT, ZI0, ZSITHOZ, ZSOVRM, ZSTATI, &
 & ZTETA, ZTHETOZ, ZTIMTR, ZWSOVRM, ZSEASON  

INTEGER(KIND=JPIM) :: JUV

INTEGER(KIND=JPIM) :: IRLXI   ! number of intervals between two reference fields

REAL(KIND=JPRB) :: ZHOOK_HANDLE
LOGICAL :: LLFIRSTCALL = .TRUE.

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

#include "abor1.intfb.h"
#include "suecaebc.intfb.h"
#include "suecaec.intfb.h"
#include "suecaeor.intfb.h"
#include "suecaesd.intfb.h"
#include "suecaess.intfb.h"
#include "suecaesu.intfb.h"
!!!!! #include "suecso4his.intfb.h"
#include "suecso4.intfb.h"
#include "suecozc.intfb.h"
#include "suecozv.intfb.h"
#include "suecozo.intfb.h"
#include "su_ghgclim.intfb.h"
#include "suecozcaqua.intfb.h"
#include "updcli.intfb.h"
#include "updclie.intfb.h"
#include "updclie_co2.intfb.h"
#include "updcpl.intfb.h"
#include "updmoon.intfb.h"
#include "updnud.intfb.h"
#include "updo3ch.intfb.h"
#include "updrgas.intfb.h"

#include "fctast.h"
#include "fcttim.h"
#include "fcttrm.h"

IF (LHOOK) CALL DR_HOOK('UPDTIM',0,ZHOOK_HANDLE)
ITIME=NINT(PTSTEP)

IF (LTWOTL) THEN
  IZT=NINT(PTSTEP*(REAL(KSTEP,JPRB)+0.5_JPRB),KIND(IZT))
ELSE
  IZT=INT(ITIME,KIND(IZT))*INT(KSTEP,KIND(IZT))
ENDIF

!--
IF (LPERPET) THEN
  ISEC=INT(IZT/NINT(RDAY,KIND(IZT)),KIND(ISEC))
  IZT=IZT-ISEC*NINT(RDAY,KIND(IZT))
ENDIF
!--

RSTATI=REAL(IZT,JPRB)
NSTADD=INT(IZT/NINT(RDAY),KIND(NSTADD))
NSTASS=INT(MOD(IZT,NINT(RDAY)),KIND(NSTASS))
ISTASS=INT(MOD(IZT+NINT(RDAY)/2-NSSSSS,NINT(RDAY)),KIND(ISTASS))
RTIMTR=RTIMST+RSTATI
IPR=0
IF(IPR == 1)THEN
  WRITE(UNIT=NULOUT,FMT='(1X,'' TIME OF THE MODEL '',E20.14,&
   & '' TIME SINCE START '',E20.14)') RTIMTR,RSTATI
ENDIF
RHGMT=REAL(MOD(NINT(RSTATI,JPIB)+NSSSSS,NINT(RDAY)),JPRB)

ZTETA=RTETA(RTIMTR)
IF( LAQUA ) THEN
  RDEASO=RRSAQUA(ZTETA)
  RDECLI=RDSAQUA(ZTETA)
  REQTIM=RETAQUA(ZTETA)
ELSE  
  RDEASO=RRS(ZTETA)
  RDECLI=RDS(ZTETA)
  REQTIM=RET(ZTETA)
ENDIF
RSOVR =REQTIM+RHGMT
RWSOVR=RSOVR*2.0_JPRB*RPI/RDAY
IF (NHINCSOL /= 0) THEN
  ZI0=RSOLINC
ELSE
  ZI0=RI0
ENDIF
ZSEASON= REA*REA/(RDEASO*RDEASO)
IF( LAQUA ) THEN
  ZSEASON=1._JPRB
ENDIF


IF(LCORBMD) THEN
   CALL SHR_ORB_UPD(RSTATI, ORBCALDAY, RDECLI, ZSEASON)
ENDIF


RIP0=ZI0*ZSEASON
RII0 = RIP0
IF (LUVPROC) THEN
  DO JUV=1,NUV
    RSUVB(JUV)=RSUVB0(JUV)*ZSEASON
  ENDDO
ENDIF

RCODEC=COS(RDECLI)
RSIDEC=SIN(RDECLI)

RCOVSR=COS(RWSOVR)
RSIVSR=SIN(RWSOVR)

ICNT=5
ISTEP=ICNT*INT(KSTEP/ICNT)
IF (ISTEP == KSTEP) THEN
  WRITE(NULOUT,FMT='(''ISTEP='',I8,'' RSOLINC='',F10.4,'' RI0='',F10.4,&
    & '' ZI0='',F10.4,'' RIP0='',F10.4)') ISTEP,RSOLINC,RI0,ZI0,RIP0
  WRITE(NULOUT,FMT='(''ISTEP='',I8,'' ZI0='',F10.4,'' ZSEASON='',F12.9,&
    & '' REA='',F15.2,'' RDEASO='',F15.2)') ISTEP,ZI0,ZSEASON,REA,RDEASO
ENDIF

! MOON LOCATION COMPUTATIONS.

IF(LRAYLU) THEN

  CALL UPDMOON

  RCODECLU=COS(RDECLU)
  RSIDECLU=SIN(RDECLU)

  RCOVSRLU=COS(RTMOLT)
  RSIVSRLU=SIN(RTMOLT)

ENDIF

RDTSA=0.5_JPRB*PTDT/RA
RDTSA2=RDTSA**2
RDTS62=RDTSA2/6._JPRB
RDTS22=RDTSA2/2.0_JPRB

RTDT=PTDT
TSPHY = MAX(PTDT,1.0_JPRB)

IF(LNUDG)THEN
!       CALCULATION OF THE TIME WEIGTHS FOR NUDGING
!      one assumes that the frequency of input restart
!      is the same as the output restart, but at least once a day
  IFRHIS=MIN(NFRHIS,86400/NINT(TSTEP))
  IF(NFNUDG == 7 ) THEN
    IFNUDG=3+86400/(IFRHIS*NINT(TSTEP))
  ENDIF
  IF(NFNUDG == 2 ) THEN
    IFNUDG=43200/(IFRHIS*NINT(TSTEP))
  ENDIF
  IF(NFNUDG /= IFNUDG)THEN
    WRITE(NULOUT,'('' ERROR NFNUDG != '',I2)')IFNUDG
    CALL ABOR1('UPDTIM: ABOR1 CALLED')
  ENDIF
  DO JSTEP=1,NFNUDG
    XPNUDG(JSTEP)=0.0_JPRB
  ENDDO
! ISTPF: LENGTH BETWEEN TWO OBSERVATIONS (S)
  ISTPF=IFRHIS*ITIME
! ISTP1: TIME OF THE STEP (S)
  ISTP1=MOD(KSTEP*ITIME,NINT(RDAY))
  ZDEL=REAL(ISTP1,JPRB)/REAL(ISTPF,JPRB)-REAL(ISTP1/ISTPF,JPRB)
  IF(NFNUDG == 7) THEN
    IBASE=2+ISTP1/ISTPF
    XPNUDG(IBASE-1)=-0.5_JPRB*ZDEL*(1.0_JPRB-ZDEL)**2
    XPNUDG(IBASE)=1.0_JPRB-0.5_JPRB*ZDEL**2*(5._JPRB-3._JPRB*ZDEL)
    XPNUDG(IBASE+1)=1.0_JPRB-0.5_JPRB*(1.0_JPRB-ZDEL)**2*(2.0_JPRB+3._JPRB*ZDEL)
    XPNUDG(IBASE+2)=-0.5_JPRB*ZDEL**2*(1.0_JPRB-ZDEL)
  ELSEIF(NFNUDG == 2) THEN
    IBASE=1
    XPNUDG(IBASE)=1.0_JPRB-ZDEL
    XPNUDG(IBASE+1)=ZDEL
  ENDIF
  IF(LWNUDG)THEN
    XWNUDG=MAX(0.0_JPRB,COS(REAL(2*KSTEP,JPRB)/REAL(NFRHIS,JPRB)*RPI))
  ELSE
    XWNUDG=1.0_JPRB
  ENDIF
ENDIF

!  CALCULATION OF THE TIME WEIGTHS FOR RELAXATION
!  one assumes that the frequency of input restart
!  is the same as the output restart, but at least once a day
IF(LRLXG)THEN
IF (.not.ALLOCATED (XPRLXG)) ALLOCATE(XPRLXG(NFRLXG))


! IRLXI: Number of intervals between two reference fields
  IRLXI=NINT(REAL(NFRLXU)/(TSTEP/3600.0))

  IFRHIS=MIN(IRLXI,86400/NINT(TSTEP))
  IF(NFRLXG == 2 ) THEN
    IFRLXG=43200/(IFRHIS*NINT(TSTEP))
  ENDIF
  IF(NFRLXG /= IFRLXG)THEN
    WRITE(NULOUT,'('' ERROR NFRLXG != '',I2)')IFRLXG
    CALL ABOR1('UPDTIM: ABOR1 CALLED')
  ENDIF
  DO JSTEP=1,NFRLXG
    XPRLXG(JSTEP)=0.0_JPRB
  ENDDO
! ISTPF: LENGTH BETWEEN TWO OBSERVATIONS (S)
  ISTPF=IFRHIS*ITIME
! ISTP1: TIME OF THE STEP (S)
  ISTP1=MOD(KSTEP*ITIME,NINT(RDAY))
  ZDEL=REAL(ISTP1,JPRB)/REAL(ISTPF,JPRB)-REAL(ISTP1/ISTPF,JPRB)
  IF (NFRLXG == 2) THEN
    IBASE=1
    XPRLXG(IBASE)=1.0_JPRB-ZDEL
    XPRLXG(IBASE+1)=ZDEL
  ENDIF
  WRITE(NULOUT,'(''TEST: XPRLXG='',2F9.3)')XPRLXG(1),XPRLXG(NFRLXG)
ENDIF

!          2.   PARAMETERS FOR ECMWF-STYLE INTERMITTENT RADIATION 
!               -------------------------------------------------

IF (LEPHYS.OR.(LMPHYS.AND.LRAYFM)) THEN

  CALL GSTATS(1904,0)
  IF (NSMAX >= 63 .AND. LERAD1H) THEN
    IF (KSTEP*TSTEP  <  NLNGR1H*3600._JPRB) THEN
      NRADFR=NRADSFR
    ELSE
      NRADFR=NRADNFR
    ENDIF
  ENDIF

  ITIME=NINT(TSTEP)
  IF (LTWOTL) THEN
    IZT=NINT(TSTEP*(REAL(KSTEP,JPRB)+0.5_JPRB),KIND(IZT))
  ELSE
    IZT=INT(ITIME,KIND(IZT))*INT(KSTEP,KIND(IZT))
  ENDIF

  !--
  IF (LPERPET) THEN
    ISEC=INT(IZT/NINT(RDAY,KIND(IZT)),KIND(ISEC))
    IZT=IZT-ISEC*NINT(RDAY,KIND(IZT))
  ENDIF
  !--
  ZSTATI=REAL(IZT,JPRB)+0.5_JPRB*NRADFR*ITIME
  ISTADD=INT(IZT/NINT(RDAY),KIND(ISTADD))
  ISTASS=INT(MOD(IZT,NINT(RDAY)),KIND(ISTASS))
  ZTIMTR=RTIMST+ZSTATI
  ZHGMT=REAL(MOD(NINT(ZSTATI,JPIB)+NSSSSS,NINT(RDAY)),JPRB)

! Updates concentrations for uniformly mixed gases and solar constant if required
! Recalculate solar insolation
  IUPGHG=0
  IF ((LECO2VAR.OR.LHGHG).AND. MOD(KSTEP,NRADFR) == 0) THEN
    CALL UPDRGAS
    IF (NHINCSOL /= 0) THEN
      ZI0=RSOLINC
    ELSE
      ZI0=RI0
    ENDIF
    RIP0=ZI0*ZSEASON
    RII0 = RIP0
    IUPGHG=1
    ICNT=1
    ISTEP=ICNT*INT(KSTEP/ICNT)
    IF (ISTEP == KSTEP) THEN
      WRITE(NULOUT,FMT='(''ISTEP='',I8,'': Reset RSOLINC='',F10.4,&
        & '' RIP0='',F10.4)') ISTEP,RSOLINC,RIP0
    ENDIF
  ENDIF

  ZTETA=RTETA(ZTIMTR)
  IF( LAQUA ) THEN
    ZDEASOM=RRSAQUA(ZTETA)
    ZDECLIM=RDSAQUA(ZTETA)
    ZEQTIMM=RETAQUA(ZTETA)
  ELSE 
    ZDEASOM=RRS(ZTETA)
    ZDECLIM=RDS(ZTETA)
    ZEQTIMM=RET(ZTETA)
  ENDIF

  IF(LCORBMD) THEN
   CALL SHR_ORB_UPD(ZSTATI, ORBCALDAYM, ZDECLIM)
  ENDIF

  ZSOVRM =ZEQTIMM+ZHGMT
  ZWSOVRM=ZSOVRM*2.0_JPRB*RPI/RDAY
  IF (NHINCSOL /= 0) THEN
    ZI0=RSOLINC
  ELSE
    ZI0=RI0
  ENDIF

  RCODECM=COS(ZDECLIM)
  RSIDECM=SIN(ZDECLIM)

  RCOVSRM=COS(ZWSOVRM)
  RSIVSRM=SIN(ZWSOVRM)
  ZTHETOZ=RTETA(RTIMTR)
  ZANGOZC=REL(ZTHETOZ)-1.7535_JPRB
  ZCOTHOZ=COS(ZANGOZC)
  ZSITHOZ=SIN(ZANGOZC)

  CALL SUECOZO (ZANGOZC)
! Fortuin-Langematz O3 CLIMATOLOGY:
  IF (NOZOCL == 1.AND. .NOT. LRAYFM15 .AND. .NOT.LPHYLIN) THEN
    IF(MOD(KSTEP,NRADFR) == 0) THEN
      IMINUT=NINT((ZSTATI + REAL(NSSSSS,JPRB))/60._JPRB)
      IF( LAQUA ) THEN
        CALL SUECOZCAQUA ( NINDAT, IMINUT )
      ELSE IF (LCMIP5.OR.LCMIP6) THEN
        IF (.NOT. LTM5O3) THEN
          CALL SUECOZV ( NINDAT, IMINUT )
        ENDIF
      ELSE
        CALL SUECOZC ( NINDAT, IMINUT )
      ENDIF
      IF (LCMIP6_PI_AEROSOLS) THEN
        CALL CMIP6_PI_AEROSOLS
      ELSE IF (LCMIP5) THEN
        CALL CMIP5_AEROSOLS
      ENDIF
      IF (LCMIP6_STRATAER_FULL) THEN
        CALL CMIP6_STRATO_AERO
      ENDIF
    ENDIF
  ENDIF

!Greenhouse gases GHG (CO2, O3, CH4, N2O, NO2, CFC11, CFC12, HCFC22, CCl4) CLIMATOLOGIES From D.Cariolle 2D-model:
  ICNT=100
  IF (NGHGRAD /= 0 .AND. .NOT.LRAYFM15 .AND. .NOT.LPHYLIN) THEN
    IF(MOD(KSTEP,NRADFR) == 0) THEN
      IMINUT=NINT((ZSTATI + REAL(NSSSSS,JPRB))/60._JPRB)
      CALL SU_GHGCLIM ( NINDAT, IMINUT, KSTEP, IUPGHG )
      ISTEP=INT(KSTEP/ICNT)*ICNT
      IF(ISTEP == KSTEP) THEN
        WRITE(NULOUT,FMT='(1X,''STEP='',I8,'' UPDTIM: IUPGHG='',I4,'' RCARDI='',E12.5)') KSTEP,IUPGHG, RCARDI
      ENDIF
    ENDIF
  ENDIF

! Aerosol climatology Tegen et al. / GISS Volcanic aerosol climatology 
! and  O.Boucher sulphate history (obs 1920-1990, A1B scenario 2000-2100)
  IF (LHVOLCA .OR. LNEWAER) THEN
    IF (LLFIRSTCALL) THEN
      CALL SUECAEBC
      CALL SUECAEOR
      CALL SUECAESD
      CALL SUECAESS
      CALL SUECAESU
!      IF(LESO4HIS) THEN
!        CALL SUECSO4HIS
!      ENDIF
      LLFIRSTCALL = .FALSE.
    ENDIF
    IF (.NOT.LPHYLIN) THEN
      IF(MOD(KSTEP,NRADFR) == 0) THEN
        IMINUT=NINT((ZSTATI + REAL(NSSSSS,JPRB))/60._JPRB)
        CALL SUECAEC ( NINDAT, IMINUT )
        IF (LESO4HIS) THEN
          CALL SUECSO4 ( NINDAT, IMINUT )
        ENDIF
      ENDIF
    ENDIF
  ENDIF
  CALL GSTATS(1904,1)

ELSEIF (LRAYFM15) THEN

  IOZOCL15=1
  IF (NSMAX >= 63.AND. LERAD6H15) THEN
    IF (KSTEP*TSTEP  <  6._JPRB*3600._JPRB) THEN
      NRADFR15=NRADSFR15
    ELSE
      NRADFR15=NRADNFR15
    ENDIF
  ENDIF

  ITIME=NINT(TSTEP)
  IF (LTWOTL) THEN
    IZT=NINT(TSTEP*(REAL(KSTEP,JPRB)+0.5_JPRB),KIND(IZT))
  ELSE
    IZT=INT(ITIME,KIND(IZT))*INT(KSTEP,KIND(IZT))
  ENDIF

  ZSTATI=REAL(IZT,JPRB)+0.5_JPRB*NRADFR15*ITIME
  ISTADD=INT(IZT/NINT(RDAY),KIND(ISTADD))
  ISTASS=INT(MOD(IZT,NINT(RDAY)),KIND(ISTASS))
  ZTIMTR=RTIMST+ZSTATI
  ZHGMT=REAL(MOD(NINT(ZSTATI,JPIB)+NSSSSS,NINT(RDAY)),JPRB)

  ZTETA=RTETA(ZTIMTR)
  IF( LAQUA ) THEN
    ZDEASOM=RRSAQUA(ZTETA)
    ZDECLIM=RDSAQUA(ZTETA)
    ZEQTIMM=RETAQUA(ZTETA)    
  ELSE  
    ZDEASOM=RRS(ZTETA)
    ZDECLIM=RDS(ZTETA)
    ZEQTIMM=RET(ZTETA)
  ENDIF

  IF(LCORBMD) THEN
    CALL SHR_ORB_UPD(ZSTATI, ORBCALDAYM, ZDECLIM)
  ENDIF

  ZSOVRM =ZEQTIMM+ZHGMT
  ZWSOVRM=ZSOVRM*2.0_JPRB*RPI/RDAY
  
  RCODECM15=COS(ZDECLIM)
  RSIDECM15=SIN(ZDECLIM)

  RCOVSRM15=COS(ZWSOVRM)
  RSIVSRM15=SIN(ZWSOVRM)
  ZTHETOZ=RTETA(RTIMTR)
  ZANGOZC=REL(ZTHETOZ)-1.7535_JPRB
  ZCOTHOZ=COS(ZANGOZC)
  ZSITHOZ=SIN(ZANGOZC)

  CALL SUECOZO (ZANGOZC)
! NEW O3 CLIMATOLOGY:
  IF (NOZOCL == 1) THEN
    IF(MOD(KSTEP,NRADFR15) == 0) THEN
      IMINUT=NINT((ZSTATI + REAL(NSSSSS,JPRB))/60._JPRB)
      IF( LAQUA ) THEN
        CALL SUECOZCAQUA ( NINDAT, IMINUT )
      ELSE IF (LCMIP5.OR.LCMIP6) THEN
        CALL SUECOZV ( NINDAT, IMINUT )
      ELSE
        CALL SUECOZC ( NINDAT, IMINUT )
      ENDIF
      IF (LCMIP6_PI_AEROSOLS) THEN
        CALL CMIP6_PI_AEROSOLS
      ELSE IF (LCMIP5) THEN
        CALL CMIP5_AEROSOLS
      ENDIF
      IF (LCMIP6_STRATAER_FULL) THEN
        CALL CMIP6_STRATO_AERO
      ENDIF
    ENDIF
  ENDIF

ENDIF

!          3.     PARAMETERS USED FOR CLIMATE-TYPE RUNS
!                 -------------------------------------

IF(NSTASS < ITIME)THEN

!        Updates climatology Meteo-France style

  IF(LMCC01)THEN
!   basic fields for the climatology
    IGP=YSD_VFD%NUMFLDS+2*YSD_VPD%NUMFLDS+YSD_VAD%NUMFLDS
    IF(LMCC03)THEN
!                coupled SST and Sea-Ice Mask and albedo
      IGP=IGP+3+NTSSG
    ELSE
!                clim SST1+SST2 (2 consecutive months)
      IGP=IGP+2
    ENDIF
    IF(LSOLV)THEN
!                dom.veget+min.sto.res+clay+sand+depth+LAI1+LAI2+(Z0 therm)
      IGP=IGP+YSD_VVD%NUMFLDS
    ENDIF
    IF(LOZONE)THEN
!                7 basic fields
      IGP=MAX(IGP,14)
    ENDIF
    IF(.NOT.LSCMEC) THEN
      CALL UPDCLI(IGP)
    ENDIF
  ENDIF
ENDIF

IF(NSTASS < ITIME .AND. NFNUDG == 7 )THEN
! Nudging of upper-air fields
  IF(LNUDG)THEN
    IGP=NTOTFNUDG/NFNUDG
    IF(.NOT.LSCMEC) THEN
      CALL UPDNUD(IGP)
    ENDIF
  ENDIF
ENDIF
IF (LNUDG .AND. MOD(KSTEP,NFRHIS) == 0 .AND. NFNUDG == 2 ) THEN
  IGP=NTOTFNUDG/NFNUDG
  CALL UPDNUD(IGP)
ENDIF

!* New TEST Version
IF (LRLXG) THEN
  IRLXI=NINT(REAL(NFRLXU)/(TSTEP/3600.0))
  IF (LRLXG .AND. MOD(KSTEP,IRLXI) == 0 .AND. NFRLXG == 2 ) THEN
    WRITE(NULERR,'(''Here we read data soon, KSTEP='',1X,I6)')KSTEP
    CALL UPDRLXREF(PTSTEP)
    WRITE(NULERR,'(''UPDRLXREF sucessfully finished'')')
  ENDIF
ENDIF

! Relaxation of upper air fields (NFRLX=2 only at the moment)
!*****IF (LRLXG .AND. MOD(KSTEP,NFRLXU) == 0 .AND. NFRLXG == 2 ) THEN
!*****  WRITE(NULERR,'(''Here we read data soon, KSTEP='',1X,I6)')KSTEP
!*****  CALL UPDRLXREF(PTSTEP)
!*****  WRITE(NULERR,'(''UPDRLXREF sucessfully finished'')')
!*****ENDIF

LLCPL=LMCC03.OR.LMCC05

IF (NFRCPL>0) THEN
  IF (LLCPL.AND.MOD(KSTEP,NFRCPL) == 0) THEN
    IF(LMCC03)THEN
      IGP=3+NTSSG
    ELSE
      IGP=3
    ENDIF
    IF(.NOT.LSCMEC) THEN
      CALL UPDCPL(IGP)
    ENDIF
  ENDIF
ENDIF

!  Make sure, for EC-Earth coupled runs, that the ocean fields are updated at
!  every time step for later use in UPDCLIE. The coupler decides whether the
!  fields are actually received from the ocean component (coupling time step) or
!  not. This allows for, e.g., time interpolation in the coupler.
IF (LECEARTH .AND. .NOT.LAMIP) THEN
  CALL CPLNG_EXCHANGE(CPLNG_STAGE_RCV_OCE)
ENDIF

IF (LAMIP) THEN
  CALL CPLNG_EXCHANGE(CPLNG_STAGE_RCV_AMIP)
ENDIF

IF (LTM5) THEN
  CALL CPLNG_EXCHANGE(CPLNG_STAGE_RCV_CHE)
ENDIF

! Always receive fields from LPJ-GUESS when it's coupled
! They will not be used unless LLPFGFB is true - see updclie.F90
IF (LLPJG) THEN
   CALL CPLNG_EXCHANGE(CPLNG_STAGE_RCV_VEG)
ENDIF

!  For coupled integrations, update SSTs at the frequency of
!  the ocean-atmosphere coupling instead of once a day.

IF (LMCC04) THEN
  IF (.NOT.LAMIP) THEN
    ISTASS0=INT(MOD(IZT,NFRCO*ITIME),KIND(ISTASS0))
  ELSE
    !KW! for uncoupled EC-EARTH integartions, currently hard-coded at 24 hrs
    ISTASS0=INT(MOD(IZT,24),KIND(ISTASS0))
  ENDIF
  IF(ISTASS0 < ITIME)THEN
    IF (LMCCEC.AND.LDCLUPD) THEN
      IF(.NOT.LSCMEC) THEN
        CALL UPDCLIE(PTSTEP)
      ENDIF
    ENDIF
  ENDIF
ENDIF

!  For coupling to an ocean mixed layer model.
!  UPDCLIE is called at every timestep to update SST. 
IF (LEOCML) THEN
  IF (LMCCEC.AND.LDCLUPD) THEN
    IF(.NOT.LSCMEC) THEN
      CALL UPDCLIE(PTSTEP)
    ENDIF
  ENDIF
ENDIF

! For the diurnal cycle of the terrestrial biosphere CO2 fluxes

IF(LECO2DIU.AND.KSTEP ==  0.AND.LDCLUPD) THEN
  ALLOCATE(CLIMRCO2(NGPTOT,2,1))
  NPCO2_1=1
  NPCO2_2=2
ENDIF

IF(LECO2DIU.AND.KSTEP >= 0.AND.LDCLUPD) THEN
  IF(.NOT.LSCMEC) THEN
    CALL UPDCLIE_CO2(PTSTEP)
  ENDIF
ENDIF

IF(ISTASS < ITIME)THEN

!        Updates climatology (SST) ECMWF style

!  ***   NB: THE CHECK ON LDCLUPD PRECLUDES THE CALL
!            DURING THE ITERATIONS OF NMI;

 IF (.NOT.LMCC04 .AND. .NOT.LEOCML) THEN
   IF (LMCCEC.AND.LDCLUPD) THEN
     IF(.NOT.LSCMEC) THEN
       CALL UPDCLIE(PTSTEP)
     ENDIF
   ENDIF
 ENDIF

!        Updates ozone chemistry

  IF (LEO3CH.AND.KSTEP >= 0) THEN
    CALL UPDO3CH
  ENDIF
  
!        Updates concentrations for uniformly mixed gases

  IF ((LECO2VAR.OR.LHGHG).AND.KSTEP >= 0) THEN
    CALL UPDRGAS
  ENDIF

ENDIF


IF (LHOOK) CALL DR_HOOK('UPDTIM',1,ZHOOK_HANDLE)

END SUBROUTINE UPDTIM
