SUBROUTINE UPDCLIE(PTSTEP)

!**** *UPDCLIE*

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

!     Updates the climatological data 
!     (ECMWF version, distributed or shared memory)

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

!     CALL UPDCLIE(PTSTEP)

!        Explicit arguments :
!        --------------------
!        PTSTEP: TIME STEP

!     METHOD.
!     -------

!     Reads the time-varying climate fields (currently SST and albedo
!     only). Two options are possible: a) time interpolation (in which
!     the input file contains monthly fields) whereby for a given day,
!     the value is interpolated linearly between the 15th of the nearest
!     months; b) No time interpolation, the most recent fields are used.
!     Values for all months are on unit NUNITC. Special care is taken
!     when transition between sea and sea-ice occurs.

!     If the OASIS coupler is active, SST and sea ice data are taken
!     from OASIS, and not read from the climate files.

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

!     UPDCAL

!     AUTHORS.
!     --------

!         P. VITERBO (ECMWF) APR 94.  (based on M. DEQUE  routine UPDCLI)

!     MODIFICATIONS.
!     --------------

!         P. VITERBO (ECMWF) MAR 95.  (OPTION OF NO TIME INTERPOLATION OF
!                                      SST ADDED)
!         T.Stockdale  24.08.95   OASIS coupling with unformatted i/o
!         G.Mozdzynski 19.03.96   support distributed memory
!         P. Viterbo    6.10.97   Cleaning distributed memory stuff,
!                                 tighter checking of files/options,
!                                 support several fields (instead of SST
!                                 only)
!         T.Stockdale  15.07.98   Merge updcliesm and updcliedm,
!                                 include latest OASIS coupling.
!    R. El Khatib : 99-12-28 remove poles rows
!         P. Viterbo   23.06.99   Tiling of the surface.
!         P. Viterbo   13.01.03   Fix for Sea-Ice Gribex.
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!        M.Hamrud      10-Jan-2004 CY28R1 Cleaning
!        T.Stockdale   08-Feb-2005 Initial sea-ice to OASIS
!        T.Stockdale   31-Mar-2005 Ocean surface currents from OASIS
!        M.Hamrud      26-Apr-2006 Replace old routine by MPL_BROADCAST
!        JJMorcrette   20060605    MODIS albedo
!        M.Hamrud      01-Jul-2006 Revised surface fields
!        J.J.Morcrette   20060925  DU, BC, OM, SU, VOL, SOA climatological fields
!        S. Boussetta/G.Balsamo      May 2009   (Add variable LAI)
!     ------------------------------------------------------------------

USE PARKIND1  ,ONLY : JPIM, JPIB, JPRB
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK
USE YOERAD   , ONLY : LPERPET
USE YOEPHY   , ONLY : LECURR   ,LEOCWA   ,LEOCCO   ,LE4ALB   ,LEOCML,  LELAIV
USE YOMCST   , ONLY : RDAY     ,RTT      ,RPI
USE YOMCT0   , ONLY : NSTOP    ,CNMEXP   ,LTWOTL
USE YOMCT3   , ONLY : NSTEP
USE YOMDIM   , ONLY : NDGLG    ,NDGSAG   ,NDGENG   ,NDGNH    ,&
 & NFLEVG   ,NPROMA  ,NDLON  
USE YOMGEM   , ONLY : NGPTOT   ,NGPTOTMX ,NHTYP    ,&
 & NLOENG   ,NGPTOTG  
USE YOMLUN   , ONLY : NULOUT   ,NULCO    ,NULERR
USE YOMMCC   , ONLY : NCLIMR   ,NCLIGC   ,NDIFC    ,&
 & NJDCR    ,NP1      ,NP2      ,NUNITC   ,CLIMR   ,LRDISTL1, &
 & LMCC04   ,LMCCIEC  ,NOACOMM  ,LMCCICEIC
USE YOMCOU   , ONLY : NCULF
USE YOMMP    , ONLY : MYPROC
USE YOMMSC   , ONLY : NINTLEN  ,NREALEN
USE YOMRIP   , ONLY : NINDAT   ,NSTADD
USE YOMGC    , ONLY : GELAT
USE YOM_YGFL , ONLY : NACTAERO
USE SURFACE_FIELDS_MIX, ONLY : &
 & SP_SB, YSP_SB, SP_RR, YSP_RR , SD_VF, YSD_VF, SP_OM, YSP_OM
USE MPL_MODULE, ONLY : MPL_BROADCAST

USE YOMECEARTH , ONLY : LECEARTH, LECEARTH_UNCOUP,  LLPJG, LLPJGFB
USE YOMECEARTH , ONLY : LECEARTH, LAMIP, LRDALB, LRDVEG
USE CPLNG

IMPLICIT NONE

INTERFACE
#include "surf_inq.h"
END INTERFACE

#include "abor1.intfb.h"
#include "ircvgpf.intfb.h"
#include "isndgpf.intfb.h"
#include "updcal.intfb.h"
#include "gathergpf.intfb.h"

REAL(KIND=JPRB)   ,INTENT(IN)    :: PTSTEP 
INTEGER(KIND=JPIM) :: ILMOIS(12),IDM(3)
INTEGER(KIND=JPIM) :: IINFO(10000,2,NCLIMR)
INTEGER(KIND=JPIM) :: IGRIG(NGPTOTG)
INTEGER(KIND=JPIM) ::   ISEC0(2),ISEC1(60),ISEC2(22+(NDGENG-NDGSAG+1)),&
 & ISEC3(2),ISEC4(42)  
REAL(KIND=JPRB) ::      ZSEC2(10+2*(NFLEVG+1)),ZSEC3(2)
REAL(KIND=JPRB) :: ZBUF (NGPTOTG)
REAL(KIND=JPRB) :: ZSST (NGPTOTG), ZICE (NGPTOTG)
REAL(KIND=JPRB) :: ZUCURR (NGPTOTG), ZVCURR (NGPTOTG)
REAL(KIND=JPRB) :: ZFLD (NGPTOTMX)  , ZFLDB (NGPTOTMX)
REAL(KIND=JPRB) :: ZFLDC (NGPTOTMX)  , ZFLDD (NGPTOTMX)
REAL(KIND=JPRB) :: ZINIICE(NGPTOT)  , ZINIICEG(NGPTOTG,1)
REAL(KIND=JPRB) :: ZREAL(NDLON*NDGLG)
CHARACTER :: CLNOMF*13
CHARACTER :: CLFIELD*8
LOGICAL :: LLICEO(0:NGPTOT),LLICEN(0:NGPTOT)

INTEGER(KIND=JPIM) :: IA, IA0, IADD, IAE, IAN, ICCYY0, ICCYYE,&
 & ICLIM, ICOU, ID, IDIF, IDIFD, IDUMY, IEND, &
 & IERR, IFIRST, &
 & IIM1, IIM2, IJ0, IJDCR, IJE, IJOUR, &
 & IJT1, IJT2, IJUL, IJUL0, IJUL1, IJUL2, IJULE, &
 & ILENBYT, ILENGG, ILEVTY, ILONG, IM, &
 & IM0, IME, IMM0, IMME, IMOIS, IMT1, &
 & IPAR, IPAR31, IPAR139, IPAR174, IPARAM, IPARAM1, IPARAM2, &
 & IPARAM3, IPARAM4, IPAR131, IPAR132, JGRIBC, IPARMAL, &
 & IBL, IPOI, IPOIN, &
 & IREPRT, IRET, IST, ISTADDE, &
 & ITAG, ITIM, ITIME, IUNITC, IWORD, &
 & IYYM0, IYYMD, J, JCL, JF, JGL, &
 & JM, JROF, JSTGLO, JTIM, JY, II, ISST1, ISST2, ICSTEP, ISTEP, JL, ISECND
INTEGER(KIND=JPIB) :: IZTE

LOGICAL :: LLFIRST, LLFOUND, LLREAD, LLDONE, LLTEST
INTEGER(KIND=JPIM),SAVE :: IDATEREF=0
INTEGER(KIND=JPIM) :: IDATE
INTEGER(KIND=JPIM) :: IPOID1, IPOID2

REAL(KIND=JPRB) :: ZPOID1, ZPOID2, ZTS, ZTI, ZCI, ZDSST0
REAL(KIND=JPRB) :: ZCU, ZCV
REAL(KIND=JPRB) :: ZRTFREEZSICE,ZRTMELTSICE,ZRCIMIN
REAL(KIND=JPRB) :: ZR2D, ZLAT, ZW
REAL(KIND=JPRB) :: ZSTL

REAL(KIND=JPRB),POINTER :: TEMPSEA(:)
REAL(KIND=JPRB),POINTER :: FRACICE(:)
REAL(KIND=JPRB),POINTER :: TEMPICE(:)

! LPJ-GUESS variables and structures
REAL(KIND=JPRB) :: ZLAIL, ZLAIH, ZCVL, ZCVH, ZTVL, ZTVH, COVERSUM
REAL(KIND=JPRB),POINTER :: VEG_LAIL(:)
REAL(KIND=JPRB),POINTER :: VEG_LAIH(:)
REAL(KIND=JPRB),POINTER :: VEG_CVL(:)
REAL(KIND=JPRB),POINTER :: VEG_CVH(:)
REAL(KIND=JPRB),POINTER :: VEG_TVL(:)
REAL(KIND=JPRB),POINTER :: VEG_TVH(:)

REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "fcttim.h"

DATA LLFIRST /.TRUE./

!*
!     1. SETTING CONSTANT VALUES.
!     ---------------------------

IF (LHOOK) CALL DR_HOOK('UPDCLIE',0,ZHOOK_HANDLE)
CALL SURF_INQ(PRTFREEZSICE=ZRTFREEZSICE,PRTMELTSICE=ZRTMELTSICE,&
 & PRCIMIN=ZRCIMIN)  

!*    1.1 Calendar

IJ0=NDD(NINDAT)
IM0=NMM(NINDAT)
IA0=NCCAA(NINDAT)
CALL UPDCAL(IJ0,IM0,IA0,NSTADD,IJOUR,IMOIS,IAN,ILMOIS,NULOUT)
IF(IJOUR > 15)THEN
  IMT1=IMOIS
  IJT1=15
  IJT2=15+ILMOIS(IMT1)
ELSE
  IMT1=1+MOD(IMOIS+10,12)
  IJT1=15-ILMOIS(IMT1)
  IJT2=15
ENDIF

!*
!     2. OPEN AND SCAN THE FILE, IF CLIMATE FIELDS REQUIRED
!     -----------------------------------------------------

!*    2.1 OPEN CLIMATE FILE

SCANIF:IF(NCLIMR >= 1.AND.LLFIRST .AND. .NOT.LLPJGFB) THEN

  MYPROCIF: IF (MYPROC == 1) THEN

! ONLY PROCESSOR 1 SHOULD OPEN 

    CLNOMF='ICMCL'//CNMEXP(1:4)//'INIT'
    WRITE(UNIT=NULOUT,FMT='(A)') ' READ CLIMATE FIELDS FROM GRIB '
    WRITE(UNIT=NULOUT,FMT='(A,A)')&
     & ' INITIAL DATA TO BE READ FROM FILE ',CLNOMF  
    CALL PBOPEN(IUNITC,CLNOMF,'r',IRET)
    IF(IRET /= 0) THEN
      WRITE(NULERR,'(A,I2)')'UPDCLIE: PROBLEM IN PBOPEN, IRET=',IRET
      CALL ABOR1('UPDCLIE: PROBLEM IN PBOPEN')
    ENDIF

!*    2.2 READ, DECODE HEADERS, CHECK THE FIELDS AND POSITIONS THE FILE

    ILENGG=NGPTOTG
    ILENBYT=NINTLEN*ILENGG
    IPOI=0
    SCAN: DO JTIM=1,10000
      DO JCL=1,NCLIMR
        IRET=0
        CALL PBGRIB(IUNITC,IGRIG,ILENBYT,ILONG,IRET)
        IF(IRET == 0) THEN
          ILONG=ILONG/NINTLEN
        ELSEIF(IRET == -1) THEN
          ITIM=JTIM-1
          EXIT SCAN
        ELSE
          WRITE(NULERR,'(A,I2)')'UPDCLIE: PROBLEM IN PBGRIB, IRET=',IRET
          CALL ABOR1('UPDCLIE: PROBLEM IN PBGRIB')
        ENDIF
        CALL PBTELL(IUNITC,IPOIN)
        IF (IPOIN == -2) THEN
          WRITE(NULERR,'(A,I2)')'UPDCLIE: PROBLEM IN PBTELL, IRET=',IPOIN
          CALL ABOR1('UPDCLIE: PROBLEM IN PBTELL')        
        ENDIF
        CALL GSTATS(1703,0)
        CALL GRIBEX(ISEC0,ISEC1,ISEC2,ZSEC2,ISEC3,ZSEC3,ISEC4,&
         & ZBUF,ILENGG,IGRIG,ILONG,IWORD,'I',IERR)  
        CALL GSTATS(1703,1)
        IPARAM=ISEC1(6)
        IYYMD=ISEC1(12)+ISEC1(11)*100+ISEC1(10)*10000+(ISEC1(21)-1)*1000000
        WRITE(NULOUT,'(A,I3,A,I8.8,A)')&
         & 'UPDCLIE: PARAMETER ',IPARAM, ' FOR DATE ',&
         & IYYMD,' FOUND IN CLIMATE FILE'  
        ILEVTY=ISEC1(7)
        IREPRT=ISEC2(1)

!      check some parameters

        IF (IREPRT /= 4) THEN
          WRITE(NULERR,'(" UPDCLIE :UNEXPECTED DATA ",&
           & "REPRESENTATION TYPE",I6)') IREPRT  
          CALL ABOR1(' UPDCLIE:INVALID DATA REPRESENTATION TYPE')
        ENDIF
        IF(ISEC2(10) /= NDGNH) THEN
          WRITE(NULERR,'(" UPDCLIE: RESOLUTION OF MODEL ",I6,&
           & " ,OF INITIAL DATA ",I6)') NDGNH,ISEC2(10)  
          CALL ABOR1(' UPDCLIE : INVALID DATA RESOLUTION')
        ENDIF
        IF(ILEVTY /= 1.AND.ILEVTY /= 112) THEN
          WRITE(NULERR,'(" UDPCLIEDM :  UNEXPECTED LEVEL TYPE ",I6)') ILEVTY
          CALL ABOR1(' UPDCLIE : INVALID LEVEL TYPE')
        ENDIF

        IF( NHTYP /= 0 )THEN
          DO JGL=1,NDGLG
            IF( NLOENG(JGL) /= ISEC2(22+JGL) )THEN
              WRITE(NULERR,*) ' UPDCLIE :'
              WRITE(NULERR,*) ' INCONSISTENT REDUCED GRID'
              WRITE(NULERR,*) ' IN MODEL ',(NLOENG(J),J=1,NDGLG)
              WRITE(NULERR,*) ' IN FILE  ',(ISEC2(22+J),J=1,NDGLG)
              CALL ABOR1(' UPDCLIE : INCONSISTENT REDUCED GRID')
            ENDIF
          ENDDO
        ENDIF

!      record/communicate field index

        IF( NCLIGC(JCL) == IPARAM )THEN
          IINFO(JTIM,1,JCL)=IPOI
          IINFO(JTIM,2,JCL)=IYYMD
        ELSE
          WRITE(NULERR,'("UPDCLIE: SKIPPING OVER FIELD,",&
           & "GRIB CODE=",I3)') IPARAM  
          WRITE(NULERR,'("UPDCLIE: EXPECTED FIELD,",&
           & "GRIB CODE=",I3)') NCLIGC(JCL)
          CALL ABOR1(' UPDCLIE : UNEXPECTED FIELD')
        ENDIF
        IPOI=IPOIN
      ENDDO
    ENDDO SCAN

! Check fields come in a consistent order and position file at the
!   right place

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

!--
    IF (LPERPET) THEN
      ISECND=IZTE/NINT(RDAY)
      IZTE=IZTE-ISECND*NINT(RDAY)
    ENDIF
!--

    ISTADDE=IZTE/NINT(RDAY,KIND(IZTE))
    CALL UPDCAL(IJ0,IM0,IA0,ISTADDE,IJE,IME,IAE,ILMOIS,NULOUT)
    IF (LMCCIEC) THEN
      IF (IJOUR > 15) THEN
        ICCYY0=IAN
        IMM0=IMOIS
      ELSE
        IMM0=1+MOD(IMOIS+10,12)
        IF (IMM0 == 12) THEN
          ICCYY0=IAN-1
        ELSE
          ICCYY0=IAN
        ENDIF
      ENDIF
      IF (IJE > 15) THEN
        IMME=1+MOD(IME,12)
        IF (IMME == 1) THEN
          ICCYYE=IAE+1
        ELSE
          ICCYYE=IAE
        ENDIF
      ELSE
        ICCYYE=IAE
        IMME=IME
      ENDIF

! Locates the first field needed

      IYYM0=IMM0+100*ICCYY0
      LLFOUND=.FALSE.
      DO J=1,ITIM
        IF (IYYM0 == IINFO(J,2,1)/100) THEN
          IFIRST=J
          IADD=IINFO(J,1,1)
          LLFOUND=.TRUE.
          EXIT
        ENDIF
      ENDDO
      IF (.NOT.LLFOUND) THEN
        CALL ABOR1(' UPDCLIE : FIRST FILE NOT FOUND')
      ENDIF

! Check all fields in the right time order

      ICOU=IFIRST
      DO JY=ICCYY0,ICCYYE
        IF (JY == ICCYY0) THEN
          IIM1=IMM0
        ELSE
          IIM1=1
        ENDIF
        IF (JY == ICCYYE) THEN
          IIM2=IMME
        ELSE
          IIM2=12
        ENDIF
        DO JM=IIM1,IIM2
          IDUMY=JM+100*JY
          DO JCL=1,NCLIMR
            IF (IDUMY /= IINFO(ICOU,2,JCL)/100) THEN
              WRITE(NULERR,'(A,I4,A,I6.6,A)')&
               & 'UPDCLIE: FIELD ',NCLIGC(JCL),' NOT FOUND FOR ',&
               & IDUMY,'00'  
              CALL ABOR1(' UPDCLIE : FIELD NOT FOUND')
            ENDIF
          ENDDO
          ICOU=ICOU+1
        ENDDO
      ENDDO
      IDIF=-999
      IJDCR=-999

    ELSE

! Locates the first field needed

      IJUL0=RJUDAT(IAN,IMOIS,IJOUR)
      DO J=1,ITIM
        IA=NCCAA(IINFO(J,2,1))
        IM=NMM(IINFO(J,2,1))
        ID=NDD(IINFO(J,2,1))
        IJUL=RJUDAT(IA,IM,ID)
        IF (IJUL > IJUL0) THEN
          IF (J == 1) THEN
            WRITE(NULERR,'(A,I8.8)')&
             & 'UPDCLIE: FIRST FILE IS FOR ',IINFO(J,2,1)  
            CALL ABOR1(' UPDCLIE : FIRST FILE NEEDED NOT FOUND')
          ENDIF
          IFIRST=J-1
          IADD=IINFO(IFIRST,1,1)
          EXIT
        ENDIF
      ENDDO

! Check all fields in the right time order

      IA=NCCAA(IINFO(IFIRST,2,1))
      IM=NMM(IINFO(IFIRST,2,1))
      ID=NDD(IINFO(IFIRST,2,1))
      IJUL1=RJUDAT(IA,IM,ID)
      IJDCR=IJUL1
      IA=NCCAA(IINFO(IFIRST+1,2,1))
      IM=NMM(IINFO(IFIRST+1,2,1))
      ID=NDD(IINFO(IFIRST+1,2,1))
      IJUL2=RJUDAT(IA,IM,ID)
      IDIF=IJUL2-IJUL1
      IF (NCLIMR > 1) THEN
        DO JCL=2,NCLIMR
          IA=NCCAA(IINFO(IFIRST,2,JCL))
          IM=NMM(IINFO(IFIRST,2,JCL))
          ID=NDD(IINFO(IFIRST,2,JCL))
          IJUL1=RJUDAT(IA,IM,ID)
          IA=NCCAA(IINFO(IFIRST+1,2,JCL))
          IM=NMM(IINFO(IFIRST+1,2,JCL))
          ID=NDD(IINFO(IFIRST+1,2,JCL))
          IJUL2=RJUDAT(IA,IM,ID)
          IDIFD=IJUL2-IJUL1
          IF (IDIFD /= IDIF) THEN
            WRITE(NULERR,'(A,2I20,A,I4)')&
             & 'UPDCLIE: JULIAN DAYS ',IJUL1,IJUL2,' FOR FIELD '&
             & ,NCLIGC(JCL)  
            WRITE(NULERR,'(A,I2)')'UPDCLIE: DIFFERENCE EXPECTED ',IDIF
            CALL ABOR1(' UPDCLIE : IRREGULARLY SPACED FIELDS')
          ENDIF
        ENDDO
      ENDIF
      DO J=IFIRST+1,ITIM-1
        DO JCL=1,NCLIMR
          IA=NCCAA(IINFO(J,2,JCL))
          IM=NMM(IINFO(J,2,JCL))
          ID=NDD(IINFO(J,2,JCL))
          IJUL1=RJUDAT(IA,IM,ID)
          IA=NCCAA(IINFO(J+1,2,JCL))
          IM=NMM(IINFO(J+1,2,JCL))
          ID=NDD(IINFO(J+1,2,JCL))
          IJUL2=RJUDAT(IA,IM,ID)
          IDIFD=IJUL2-IJUL1
          IF (IDIFD /= IDIF) THEN
            WRITE(NULERR,'(A,2I20,A,I4)')&
             & 'UPDCLIE: JULIAN DAYS ',IJUL1,IJUL2,' FOR FIELD '&
             & ,NCLIGC(JCL)  
            WRITE(NULERR,'(A,I2)')'UPDCLIE: DIFFERENCE EXPECTED ',IDIF
            CALL ABOR1(' UPDCLIE : IRREGULARLY SPACED FIELDS')
          ENDIF
        ENDDO
      ENDDO
      IJULE=RJUDAT(IAE,IME,IJE)
      DO JCL=1,NCLIMR
        IA=NCCAA(IINFO(ITIM,2,JCL))
        IM=NMM(IINFO(ITIM,2,JCL))
        ID=NDD(IINFO(ITIM,2,JCL))
        IJUL2=RJUDAT(IA,IM,ID)
        IDIFD=IJULE-IJUL2
        IF (IDIFD > IDIF) THEN
          WRITE(NULERR,'(A)')'UPDCLIE: FILE TOO SHORTJULIAN DAYS '
          WRITE(NULERR,'(A,I20)')'UPDCLIE: FORECAST ENDS ',IJULE
          WRITE(NULERR,'(A,I20)')'UPDCLIE: CLIMATE DATA ENDS ',IJUL2
          WRITE(NULERR,'(A,I2)')'UPDCLIE: DIFFERENCE EXPECTED ',IDIF
          CALL ABOR1(' UPDCLIE : IRREGULARLY SPACED FIELDS')
        ENDIF
      ENDDO
    ENDIF

! Positions the file in the first field needed

    IRET=0
    CALL PBSEEK(IUNITC,IADD,0,IRET)
    IF(IRET < 0) THEN
      WRITE(NULERR,'(A,I2)')'UPDCLIE: PROBLEM IN PBSEEK, IRET=',IRET
      CALL ABOR1('UPDCLIE: PROBLEM IN PBSEEK')
    ENDIF

! Pack message

    IDM(1)=IDIF
    IDM(2)=IJDCR
    IDM(3)=IUNITC

  ENDIF MYPROCIF

! Broadcast

  ITAG=19591204
  CALL MPL_BROADCAST(IDM,KTAG=ITAG,KROOT=1,CDSTRING='UPDCLIE')

  NDIFC=IDM(1)
  NJDCR=IDM(2)
  NUNITC=IDM(3)

ENDIF SCANIF

!*    3. READ THE FILE, IF NEEDED.
!     -----------------------------

READIF: IF(NCLIMR >= 1 .AND. .NOT.LLPJGFB) THEN

!* Decide if reading time

  IF (LMCCIEC) THEN
    IF (LLFIRST) THEN
      IDATEREF=0
      LLREAD=.TRUE.
      IFIRST=2
    ELSE
      IDATE=IAN*10000+IMOIS*100+IJOUR
      WRITE(NULOUT,*) 'IDATE=',IDATE,IDATEREF
      IF (IJOUR == 16 .AND. IDATE > IDATEREF) THEN
        LLREAD=.TRUE.
        IFIRST=1
      ELSE
        LLREAD=.FALSE.
        IFIRST=1
      ENDIF
    ENDIF
  ELSE
    IF (LLFIRST) THEN
      LLREAD=.TRUE.
      IFIRST=1
    ELSE
      IFIRST=1
      IJUL=RJUDAT(IAN,IMOIS,IJOUR)
      LLREAD=NJDCR == IJUL
    ENDIF
  ENDIF

!*    3.1 READ, DECODE AND SELECT THE FIELDS

  ILENGG=NGPTOTG
  ILENBYT=NINTLEN*ILENGG
  IF (LLREAD) THEN
    FIRST: DO JF=1,IFIRST
      IF (LMCCIEC) THEN
        NP1=3-NP1
        NP2=3-NP2
      ELSE
        NJDCR=NJDCR+NDIFC
      ENDIF
      READ: DO JCL=1,NCLIMR
        IPARAM=NCLIGC(JCL)
        IF (MYPROC == 1) THEN
          CALL PBGRIB(NUNITC,IGRIG,ILENBYT,ILONG,IRET)
          IF(IRET == 0) THEN
            ILONG=ILONG/NINTLEN
          ELSE
            WRITE(NULERR,'(A,I2)')'UPDCLIE: PROBLEM IN PBGRIB, IRET=',IRET
            CALL ABOR1('UPDCLIE: PROBLEM IN PBGRIB')
          ENDIF
          ZSEC3(2)=19591204.0_JPRB
          CALL GSTATS(1703,0)
          CALL GRIBEX(ISEC0,ISEC1,ISEC2,ZSEC2,ISEC3,ZSEC3,ISEC4,&
           & ZBUF,ILENGG,IGRIG,ILONG,IWORD,'D',IERR)  
          CALL GSTATS(1703,1)
          IF(ISEC1(5) == 192.AND.ISEC1(6) == 31)THEN
            DO II=1,ISEC4(1)
              IF(ZBUF(II) == 19591204.0_JPRB)THEN
                ZBUF(II)=0.0_JPRB
              ENDIF
            ENDDO
          ENDIF
          IPAR=ISEC1(6)
          IYYMD=ISEC1(12)+ISEC1(11)*100+ISEC1(10)*10000+(ISEC1(21)-1)*1000000
          WRITE(NULOUT,'(A,I3,A,I8.8,A)')&
           & 'UPDCLIE: PARAMETER ',IPARAM, ' FOR DATE ',&
           & IYYMD,' READ FROM CLIMATE FILE'  

          IDATEREF=IYYMD

!        distribute field to nodes

          CALL ISNDGPF(ZBUF,ZFLD)

        ENDIF

! ALL PROCESSORS SHOULD NOW RECEIVE

        CALL IRCVGPF(CLIMR(1,NP2,JCL),ZFLD,1)

! Synchronize IDATEREF
        ITAG=19591205
        CALL MPL_BROADCAST(IDATEREF,KTAG=ITAG,KROOT=1,CDSTRING='UPDCLIE')

      ENDDO READ
    ENDDO FIRST
  ENDIF

ENDIF READIF

!*    3.2  Read SST and ice mask from OASIS coupler, if needed

!   ECMWF convention from Cy22r2 onwards is that the ice field is passed
!   from OASIS as the fractional coverage (0.0 - 1.0). This differs
!   from the earlier CERFACS convention, which uses discrete values of
!   0.0, 1.0 and 2.0. Be careful not to use incompatible OASIS 
!   routines when coupling via this subroutine.

! for uncoupled EC-EARTH: update SST and sea-ice
! this line of the code is left here (commented) if anybody wants to
! reactivate the old ICMSEA forcing
!IF (LAMIP) CALL UPDCLIE_SEA(PTSTEP)
IF (LAMIP) THEN
    TEMPSEA => CPLNG_FLD(CPLNG_IDX('A_SST'     ))%D(:,1,1)
    FRACICE => CPLNG_FLD(CPLNG_IDX('A_Ice_frac'))%D(:,1,1)
ENDIF


! For EC-Earth coupled runs, the ocean fields were already received in UPDTIM
! before UPDCLIE was called. We just need to let the rest of UPDCLIE know where
! to find the data.
IF (LECEARTH .AND. .NOT.LAMIP) THEN

    !-----------!--------------------------!---------------
    ! Grib code ! Field name               ! Coupling field
    !-----------!--------------------------!---------------
    !       139 ! Soil temperature level 1 ! A_SST
    !        31 ! Sea-ice cover            ! A_Ice_frac
    !       131 ! U component of wind      ! not used
    !       132 ! V component of wind      ! not used
    !-----------!--------------------------!---------------
    NCLIGC(NCLIMR+1) = 139
    NCLIGC(NCLIMR+2) =  31
    IF (LECURR) THEN
        NCLIGC(NCLIMR+3) = 131
        NCLIGC(NCLIMR+4) = 132
    ENDIF

    TEMPSEA => CPLNG_FLD(CPLNG_IDX('A_SST'     ))%D(:,1,1)
    FRACICE => CPLNG_FLD(CPLNG_IDX('A_Ice_frac'))%D(:,1,1)
    TEMPICE => CPLNG_FLD(CPLNG_IDX('A_Ice_temp'))%D(:,1,1)

ENDIF

! LPJ-GUESS coupling. Always receive fields when LPJ-GUESS is 
! coupled. LLPJGFB determines whether or not they're used - see below
IF (LLPJG) THEN

    VEG_LAIL => CPLNG_FLD(CPLNG_IDX('LAILVeg'))%D(:,1,1)
    VEG_LAIH => CPLNG_FLD(CPLNG_IDX('LAIHVeg'))%D(:,1,1)
    VEG_CVL => CPLNG_FLD(CPLNG_IDX('FracLVeg'))%D(:,1,1)
    VEG_CVH => CPLNG_FLD(CPLNG_IDX('FracHVeg'))%D(:,1,1)
    VEG_TVL => CPLNG_FLD(CPLNG_IDX('TypeLVeg'))%D(:,1,1)
    VEG_TVH => CPLNG_FLD(CPLNG_IDX('TypeHVeg'))%D(:,1,1)

ENDIF



!*
!     4. TIME INTERPOLATION AND WRITING OF CLIMATE FIELDS
!     ---------------------------------------------------

IF (LMCCIEC) THEN
  ZPOID1=REAL(IJT2-IJOUR,JPRB)/REAL(IJT2-IJT1,JPRB)
  ZPOID2=1.0_JPRB-ZPOID1
ENDIF

DO JSTGLO=1,NGPTOT,NPROMA
  IEND=MIN(NPROMA,NGPTOT-JSTGLO+1)
  IST=1
  IBL=(JSTGLO-1)/NPROMA+1

  IF(LMCC04) THEN
    ICLIM=NCLIMR+2
    IF (LECURR) THEN
      ICLIM=NCLIMR+4
    ENDIF
  ELSE
    ICLIM=NCLIMR
  ENDIF

  IF (LRDALB) THEN
! 174, albedo
    DO JCL=1,ICLIM
      IF (NCLIGC(JCL) == 174) EXIT
    ENDDO
    IPAR174=JCL
    DO JROF =1,IEND
      IF (LMCCIEC) THEN
        ZTS=CLIMR(JSTGLO+JROF-1,NP1,IPAR174)*ZPOID1 &
         & +CLIMR(JSTGLO+JROF-1,NP2,IPAR174)*ZPOID2  
      ELSE
        ZTS=CLIMR(JSTGLO+JROF-1,NP2,IPAR174)
      ENDIF
      SD_VF(JROF,YSD_VF%YALBF%MP,IBL)    =ZTS
    ENDDO

!-- MODIS albedo grib codes 15, 16, 17, 18
    IF(LE4ALB) THEN
      DO JGRIBC=15,18
        IPARMAL=-1
        DO JCL=1,ICLIM
          IF (NCLIGC(JCL) == JGRIBC) THEN
            IPARMAL=JCL
            EXIT
          ENDIF
        ENDDO
        IF(IPARMAL > 0) THEN
          DO JROF =1,IEND
            IF (LMCCIEC) THEN
              ZTS=CLIMR(JSTGLO+JROF-1,NP1,IPARMAL)*ZPOID1 &
               & +CLIMR(JSTGLO+JROF-1,NP2,IPARMAL)*ZPOID2  
            ELSE
              ZTS=CLIMR(JSTGLO+JROF-1,NP2,IPARMAL)
            ENDIF
            IF     (JGRIBC == 15) THEN
              SD_VF(JROF,YSD_VF%YALUVP%MP,IBL)  =ZTS
            ELSEIF (JGRIBC == 16) THEN
              SD_VF(JROF,YSD_VF%YALUVD%MP,IBL)  =ZTS
            ELSEIF (JGRIBC == 17) THEN
              SD_VF(JROF,YSD_VF%YALNIP%MP,IBL)  =ZTS
            ELSEIF (JGRIBC == 18) THEN
              SD_VF(JROF,YSD_VF%YALNID%MP,IBL)  =ZTS
            ENDIF
          ENDDO
        ENDIF
      ENDDO
    ENDIF
  ENDIF


  ! Only update the vegetation fields 27, 28, 29, 30, 66 and 67 if
  ! IFS will not use LPJ-GUESS values.  

  !-- LAI Low/High grib codes 66, 67
  IF(LELAIV .AND. .NOT.LLPJGFB) THEN
    DO JGRIBC=66,67
      IPARMAL=-1
      DO JCL=1,ICLIM
        IF (NCLIGC(JCL) == JGRIBC) THEN
          IPARMAL=JCL
          EXIT
        ENDIF
      ENDDO
      IF(IPARMAL > 0) THEN
        DO JROF =1,IEND
          IF (LMCCIEC) THEN
            ZTS=CLIMR(JSTGLO+JROF-1,NP1,IPARMAL)*ZPOID1 &
             & +CLIMR(JSTGLO+JROF-1,NP2,IPARMAL)*ZPOID2  
          ELSE
            ZTS=CLIMR(JSTGLO+JROF-1,NP2,IPARMAL)
          ENDIF

          IF   (JGRIBC == 66) THEN
            SD_VF(JROF,YSD_VF%YLAIL%MP,IBL)  =ZTS
          ELSEIF (JGRIBC == 67) THEN
            SD_VF(JROF,YSD_VF%YLAIH%MP,IBL)  =ZTS
          ENDIF

        ENDDO
      ENDIF
    ENDDO
  ENDIF

  IF (LRDVEG .AND. .NOT.LLPJGFB) THEN
    !-- Vegetation cover
    DO JGRIBC=27,28
      IPARMAL=-1
      DO JCL=1,ICLIM
        IF (NCLIGC(JCL) == JGRIBC) THEN
          IPARMAL=JCL
          EXIT
        ENDIF
      ENDDO
      IF(IPARMAL > 0) THEN
        DO JROF =1,IEND
          IF (LMCCIEC) THEN
            ZTS=CLIMR(JSTGLO+JROF-1,NP1,IPARMAL)*ZPOID1 &
             & +CLIMR(JSTGLO+JROF-1,NP2,IPARMAL)*ZPOID2  
          ELSE
            ZTS=CLIMR(JSTGLO+JROF-1,NP2,IPARMAL)
          ENDIF

          SELECT CASE (JGRIBC)
          CASE (27)
            SD_VF(JROF,YSD_VF%YCVL%MP,IBL)  =ZTS
          CASE (28)
            SD_VF(JROF,YSD_VF%YCVH%MP,IBL)  =ZTS
          ENDSELECT

        ENDDO
      ENDIF
    ENDDO

    !-- Vegetation type
    ! vegetation type is *not* interpolated in time
    ! can switch to a new value at the end of a month
    IPOID1=NINT(ZPOID1)
    IPOID2=1-IPOID1
    DO JGRIBC=29,30
      IPARMAL=-1
      DO JCL=1,ICLIM
        IF (NCLIGC(JCL) == JGRIBC) THEN
          IPARMAL=JCL
          EXIT
        ENDIF
      ENDDO
      IF(IPARMAL > 0) THEN
        DO JROF =1,IEND
          IF (LMCCIEC) THEN
            ZTS=CLIMR(JSTGLO+JROF-1,NP1,IPARMAL)*IPOID1 &
             & +CLIMR(JSTGLO+JROF-1,NP2,IPARMAL)*IPOID2  
          ELSE
            ZTS=CLIMR(JSTGLO+JROF-1,NP2,IPARMAL)
          ENDIF

          SELECT CASE (JGRIBC)
          CASE (29)
            SD_VF(JROF,YSD_VF%YTVL%MP,IBL)  =ZTS
          CASE (30)
            SD_VF(JROF,YSD_VF%YTVH%MP,IBL)  =ZTS
          ENDSELECT

        ENDDO
      ENDIF
    ENDDO

  ENDIF

  IF(NACTAERO > 0) THEN
    DO JGRIBC=210031,210042
      IPARMAL=-1
      DO JCL=1,ICLIM
        IF (NCLIGC(JCL) == JGRIBC) THEN
          IPARMAL=JCL
          EXIT
        ENDIF
      ENDDO
      IF(IPARMAL > 0) THEN
        DO JROF =1,IEND
          IF (LMCCIEC) THEN
            ZTS=CLIMR(JSTGLO+JROF-1,NP1,IPARMAL)*ZPOID1 &
             & +CLIMR(JSTGLO+JROF-1,NP2,IPARMAL)*ZPOID2  
          ELSE
            ZTS=CLIMR(JSTGLO+JROF-1,NP2,IPARMAL)
          ENDIF
          IF     (JGRIBC == 210031) THEN
            SD_VF(JROF,YSD_VF%YBCBF%MP,IBL)  =ZTS
          ELSEIF (JGRIBC == 210032) THEN
            SD_VF(JROF,YSD_VF%YBCFF%MP,IBL)  =ZTS
          ELSEIF (JGRIBC == 210033) THEN
            SD_VF(JROF,YSD_VF%YOMBF%MP,IBL)  =ZTS
          ELSEIF (JGRIBC == 210034) THEN
            SD_VF(JROF,YSD_VF%YOMFF%MP,IBL)  =ZTS
          ELSEIF (JGRIBC == 210035) THEN
            SD_VF(JROF,YSD_VF%YSO2L%MP,IBL)  =ZTS
          ELSEIF (JGRIBC == 210036) THEN
            SD_VF(JROF,YSD_VF%YSO2H%MP,IBL)  =ZTS
          ELSEIF (JGRIBC == 210037) THEN
            SD_VF(JROF,YSD_VF%YVOLC%MP,IBL)  =ZTS
          ELSEIF (JGRIBC == 210038) THEN
            SD_VF(JROF,YSD_VF%YVOLE%MP,IBL)  =ZTS
          ELSEIF (JGRIBC == 210039) THEN
            SD_VF(JROF,YSD_VF%YSOA%MP,IBL)  =ZTS
          ELSEIF (JGRIBC == 210040) THEN
            SD_VF(JROF,YSD_VF%YBCGF%MP,IBL)  =ZTS
          ELSEIF (JGRIBC == 210041) THEN
            SD_VF(JROF,YSD_VF%YOMGF%MP,IBL)  =ZTS
          ELSEIF (JGRIBC == 210042) THEN
            SD_VF(JROF,YSD_VF%YSOGF%MP,IBL)  =ZTS
          ENDIF
        ENDDO
      ENDIF
    ENDDO

    DO JGRIBC=210052,210054
      IPARMAL=-1
      DO JCL=1,ICLIM
        IF (NCLIGC(JCL) == JGRIBC) THEN
          IPARMAL=JCL
          EXIT
        ENDIF
      ENDDO
      IF(IPARMAL > 0) THEN
        DO JROF =1,IEND
          IF (LMCCIEC) THEN
            ZTS=CLIMR(JSTGLO+JROF-1,NP1,IPARMAL)*ZPOID1 &
             & +CLIMR(JSTGLO+JROF-1,NP2,IPARMAL)*ZPOID2  
          ELSE
            ZTS=CLIMR(JSTGLO+JROF-1,NP2,IPARMAL)
          ENDIF
          IF     (JGRIBC == 210052) THEN
            SD_VF(JROF,YSD_VF%YAERDEP%MP,IBL)  =ZTS
          ELSEIF (JGRIBC == 210053) THEN
            SD_VF(JROF,YSD_VF%YAERLTS%MP,IBL)  =ZTS
          ELSEIF (JGRIBC == 210054) THEN
            SD_VF(JROF,YSD_VF%YAERSCC%MP,IBL)  =ZTS
          ENDIF
        ENDDO
      ENDIF
    ENDDO

  ENDIF

! Fill arrays, ensuring compatibility of ice T, ice cover, SST and
!    surface T 
  DO JCL=1,ICLIM
    IF (NCLIGC(JCL) == 139) EXIT
  ENDDO
  IPAR139=JCL
  DO JCL=1,ICLIM
    IF (NCLIGC(JCL) == 31) EXIT
  ENDDO
  IPAR31=JCL
  ZR2D = 180.0_JPRB / RPI

  DO JROF =1,IEND
      
      ! use LSM from IFS for uncoupled runs
      ! use SST from Oasis to distinguish between ocean and land points
      IF((LAMIP.AND.SD_VF(JROF,YSD_VF%YLSM%MP,IBL)<=0.5_JPRB) &
           &  .OR. ((.NOT.LAMIP).AND.TEMPSEA(JSTGLO+JROF-1)>0_JPRB)) THEN
        
! this block of the code is left here (commented) if anybody wants to
! reactivate the old ICMSEA forcing
!        IF (LECEARTH_UNCOUP) THEN
!          IF (LMCCIEC) THEN
!            ZTS=CLIMR_SEA(JSTGLO+JROF-1,NP1,1)*ZPOID1 &
!             & +CLIMR_SEA(JSTGLO+JROF-1,NP2,1)*ZPOID2  
!            ZCI=MIN(1.0_JPRB,MAX(0.0_JPRB, &
!             &  CLIMR_SEA(JSTGLO+JROF-1,NP1,2)*ZPOID1 &
!             & +CLIMR_SEA(JSTGLO+JROF-1,NP2,2)*ZPOID2 ))
!            IF (LRDISTL1) THEN
!              ZTI=CLIMR_SEA(JSTGLO+JROF-1,NP1,3)*ZPOID1 &
!               & +CLIMR_SEA(JSTGLO+JROF-1,NP2,3)*ZPOID2
!            ENDIF
!          ELSE
!            ZTS=CLIMR_SEA(JSTGLO+JROF-1,NP2,1)
!            ZCI=MIN(1.0_JPRB,MAX(0.0_JPRB,CLIMR_SEA(JSTGLO+JROF-1,NP2,2)))
!            IF (LRDISTL1) THEN
!              ZTI=CLIMR_SEA(JSTGLO+JROF-1,NP2,3)
!            ENDIF
!          ENDIF

        ZTS=TEMPSEA(JSTGLO+JROF-1)
        ! the sea-ice fraction coming from Oasis may be outside the [0,1] range
        ! reset if necessary
        ZCI=MAX(0._JPRB,MIN(1._JPRB,FRACICE(JSTGLO+JROF-1)))
        IF (LRDISTL1) ZTI=TEMPICE(JSTGLO+JROF-1)

        SD_VF(JROF,YSD_VF%YSST%MP,IBL)=ZTS
        LLICEN(JROF)=ZCI > ZRCIMIN
        IF (LLICEN(JROF)) THEN
          SD_VF(JROF,YSD_VF%YCI%MP,IBL)=ZCI
          ! update only temperature of top ice layer, leave other layers
          IF (LRDISTL1) THEN
            SP_SB(JROF,1,YSP_SB%YTL%MP,IBL)=ZTI
          ENDIF
        ELSE
          SD_VF(JROF,YSD_VF%YCI%MP,IBL)=0.
          ! reset ice temp to freezing point
          IF (LRDISTL1) THEN
            SP_SB(JROF,1,YSP_SB%YTL%MP,IBL)=ZRTFREEZSICE
            SP_SB(JROF,2,YSP_SB%YTL%MP,IBL)=ZRTFREEZSICE
            SP_SB(JROF,3,YSP_SB%YTL%MP,IBL)=ZRTFREEZSICE
            SP_SB(JROF,4,YSP_SB%YTL%MP,IBL)=ZRTFREEZSICE
          ENDIF
        ENDIF

        ! update surface and soil temp
        SP_SB(JROF,1,YSP_SB%YT%MP,IBL)=SD_VF(JROF,YSD_VF%YCI%MP,IBL)*SP_SB(JROF,1,YSP_SB%YTL%MP,IBL)+ &
         & (1.-SD_VF(JROF,YSD_VF%YCI%MP,IBL))*SD_VF(JROF,YSD_VF%YSST%MP,IBL)  
        SP_SB(JROF,2,YSP_SB%YT%MP,IBL)=SD_VF(JROF,YSD_VF%YCI%MP,IBL)*SP_SB(JROF,2,YSP_SB%YTL%MP,IBL)+ &
         & (1.-SD_VF(JROF,YSD_VF%YCI%MP,IBL))*SD_VF(JROF,YSD_VF%YSST%MP,IBL)  
        SP_SB(JROF,3,YSP_SB%YT%MP,IBL)=SD_VF(JROF,YSD_VF%YCI%MP,IBL)*SP_SB(JROF,3,YSP_SB%YTL%MP,IBL)+ &
         & (1.-SD_VF(JROF,YSD_VF%YCI%MP,IBL))*SD_VF(JROF,YSD_VF%YSST%MP,IBL)  
        SP_SB(JROF,4,YSP_SB%YT%MP,IBL)=SD_VF(JROF,YSD_VF%YCI%MP,IBL)*SP_SB(JROF,4,YSP_SB%YTL%MP,IBL)+ &
         & (1.-SD_VF(JROF,YSD_VF%YCI%MP,IBL))*SD_VF(JROF,YSD_VF%YSST%MP,IBL)  

        ! new skin temp(?)
        SP_RR(JROF,YSP_RR%YT%MP,IBL) =                                                 &
        & (       SD_VF(JROF,YSD_VF%YCI%MP,IBL)  * SP_SB(JROF,1,YSP_SB%YTL%MP, IBL)**4 &
        &   + (1.-SD_VF(JROF,YSD_VF%YCI%MP,IBL)) * SD_VF(JROF,  YSD_VF%YSST%MP,IBL)**4 &
        & )**.25

      ENDIF

    ! LPJ-GUESS coupling.
    ! Replace fields in SD_VF with LPJ-GUESS values iff LLPJGFB is true.
    IF (LLPJG .AND. LLPJGFB) THEN

        IF((LAMIP.AND.SD_VF(JROF,YSD_VF%YLSM%MP,IBL)>0.5_JPRB) &
           &  .OR. ((.NOT.LAMIP).AND.TEMPSEA(JSTGLO+JROF-1)<=0_JPRB)) THEN

        ! Land points, so use LPJ-GUESS values.

        ! LOW LAI
        ZLAIL=MAX(0.0_JPRB,VEG_LAIL(JSTGLO+JROF-1))
        SD_VF(JROF,YSD_VF%YLAIL%MP,IBL)=ZLAIL

        ! HIGH LAI
        ZLAIH=MAX(0.0_JPRB,VEG_LAIH(JSTGLO+JROF-1))
        SD_VF(JROF,YSD_VF%YLAIH%MP,IBL)=ZLAIH

        ! LOW COVER FRACTION
        ZCVL=MAX(0.0_JPRB,MIN(1.0_JPRB,VEG_CVL(JSTGLO+JROF-1)))

        ! HIGH COVER FRACTION
        ZCVH=MAX(0.0_JPRB,MIN(1.0_JPRB,VEG_CVH(JSTGLO+JROF-1)))

        COVERSUM = ZCVL + ZCVH
        ! Rescale if necessary to catch rounding errors leading to cover
        ! fractions > 1
        IF (COVERSUM > 1._JPRB) THEN
            WRITE(UNIT=NULOUT,FMT='(A)') 'LPJG - RESCALING LPJ-GUESS FIELDS '
            ZCVL = ZCVL / COVERSUM
            ZCVH = ZCVH / COVERSUM
        ENDIF

        ! UPDATE COVER FRACTIONS
        SD_VF(JROF,YSD_VF%YCVL%MP,IBL)=ZCVL
        SD_VF(JROF,YSD_VF%YCVH%MP,IBL)=ZCVH

        ! LOW COVER TYPE. Restrict to be between 0 and 20
        ZTVL=VEG_TVL(JSTGLO+JROF-1)
        IF (ZTVL==8.0_JPRB) THEN
            ZTVL=0.0_JPRB ! Force deserts to have ZTVL=0
        ENDIF
        SD_VF(JROF,YSD_VF%YTVL%MP,IBL)=MIN(MAX(ZTVL,0.0_JPRB),20.0_JPRB)

        ! HIGH COVER TYPE. Restrict to be between 0 and 20
        ZTVH=VEG_TVH(JSTGLO+JROF-1)
        SD_VF(JROF,YSD_VF%YTVH%MP,IBL)=MIN(MAX(ZTVH,0.0_JPRB),20.0_JPRB)

        ELSE

        ! Ocean points, so zeros throughout.
        
        ! LOW LAI
        ZLAIL=0.0_JPRB
        SD_VF(JROF,YSD_VF%YLAIL%MP,IBL)=ZLAIL

        ! HIGH LAI
        ZLAIH=0.0_JPRB
        SD_VF(JROF,YSD_VF%YLAIH%MP,IBL)=ZLAIH

        ! LOW COVER FRACTION
        ZCVL=0.0_JPRB
        SD_VF(JROF,YSD_VF%YCVL%MP,IBL)=ZCVL

        ! HIGH COVER FRACTION
        ZCVH=0.0_JPRB
        SD_VF(JROF,YSD_VF%YCVH%MP,IBL)=ZCVH

        ! LOW COVER TYPE
        ZTVL=0.0_JPRB
        SD_VF(JROF,YSD_VF%YTVL%MP,IBL)=ZTVL

        ! HIGH COVER TYPE
        ZTVH=0.0_JPRB
        SD_VF(JROF,YSD_VF%YTVH%MP,IBL)=ZTVH

        ENDIF

    ENDIF ! LPJ-GUESS

  ENDDO

  IF (LECURR .AND. LMCC04) THEN
    DO JCL=1,ICLIM
      IF (NCLIGC(JCL) == 131) EXIT
    ENDDO
    IPAR131=JCL
    DO JCL=1,ICLIM
      IF (NCLIGC(JCL) == 132) EXIT
    ENDDO
    IPAR132=JCL
    DO JROF =1,IEND
      IF((SD_VF(JROF,YSD_VF%YLSM%MP,IBL) <= 0.5_JPRB).AND..NOT.LLICEN(JROF))THEN
        ZCU=CLIMR(JSTGLO+JROF-1,NP2,IPAR131)
        SD_VF(JROF,YSD_VF%YUCUR%MP,IBL)    =ZCU
        ZCV=CLIMR(JSTGLO+JROF-1,NP2,IPAR132)
        SD_VF(JROF,YSD_VF%YVCUR%MP,IBL)    =ZCV
      ELSE
        SD_VF(JROF,YSD_VF%YUCUR%MP,IBL)=0.0_JPRB
        SD_VF(JROF,YSD_VF%YVCUR%MP,IBL)=0.0_JPRB
      ENDIF
    ENDDO
  ENDIF
ENDDO

LLFIRST=.FALSE.

WRITE(NULOUT,*) 'UPDCLIE finished'
CALL FLUSH(NULOUT)

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