SUBROUTINE MOD_INI(KUDATE,KUSSSS,KSTEP,KUCONF,KUSTOP,&
 & KSTEPINI,PTSTEP,KULOUT)  

!**** *TGBSEC1*   - Routine to translate grib section 1 into date for Arpege/Ifs

!     Purpose.
!     --------
!           Initialize date of model
!**   Interface.
!     ----------
!        *CALL* *TGBSEC1

!        Explicit arguments :
!        --------------------
!          KSEC1 : grib section 1  
!          KLEN1 : dimension of KSEC1
!          KUDATE: date in model           
!          KUSSSS: time in model           
!          KUCONF: configuration            
!          KUSTOP: number of time step for the model
!          KSTEPINI: initial step in hours
!          PTSTEP: time step lenght in model 
!          KULOUT: output unit number                

!        Implicit arguments :
!        --------------------
!        None

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

!     Externals.
!     ----------
!        Called by SUARG.

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

!     Author.
!     -------
!        Ryad El Khatib *METEO-FRANCE*

!     Modifications.
!     --------------
!        Original : 96-01-04
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!        M.Fisher : 2004-08-18 Increment date correctly when KSTEPINI>=48
!        M.Hamrud      01-May-2006 Generalized IO scheme
!        Carla Cardinali    13 January 2009 KUDATE not update for 801
!     ------------------------------------------------------------------

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

IMPLICIT NONE

!     EXTERNAL INTEGER FUNCTIONS
INTEGER(KIND=JPIM),INTENT(INOUT)   :: KUDATE 
INTEGER(KIND=JPIM),INTENT(INOUT)   :: KUSSSS 
INTEGER(KIND=JPIM),INTENT(IN)    :: KSTEP
INTEGER(KIND=JPIM),INTENT(IN)    :: KUCONF 
INTEGER(KIND=JPIM),INTENT(IN)    :: KUSTOP 
INTEGER(KIND=JPIM),INTENT(INOUT) :: KSTEPINI 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTSTEP 
INTEGER(KIND=JPIM),INTENT(IN)    :: KULOUT 
INTEGER(KIND=JPIM), EXTERNAL :: ICD2YMD
INTEGER(KIND=JPIM), EXTERNAL :: IYMD2CD

INTEGER(KIND=JPIM) :: ILMO(12)

INTEGER(KIND=JPIM) :: ICD, ID1, IDD, IHOUR, IHOUR1, IM1, IMM, INSSS,&
 & IRET, ISECRUN, ISHOUR, ISSS, &
 & IYMD, IYYYY, IYYYY1, INC

REAL(KIND=JPRB) :: ZEPS
REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "fcttim.h"

#include "abor1.intfb.h"
#include "updcal.intfb.h"

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

!*        1. Preparations

IF (LHOOK) CALL DR_HOOK('MOD_INI',0,ZHOOK_HANDLE)

ZEPS=1.E-10_JPRB
ISHOUR=3600

!*        2. Translate date and time 

!KUDATE=(KSEC1(21)-1)*10**6+KSEC1(10)*10**4+KSEC1(11)*10**2+KSEC1(12)
!KUSSSS=ISHOUR*KSEC1(13)

!*        3. Update the current date and time with forecast time

IF(KUCONF == 2.OR. (KUCONF/100) == 1) THEN
  IYYYY=NCCAA(KUDATE)
  IMM=NMM(KUDATE)
  IDD=NDD(KUDATE)
  IHOUR=NCTH(KUSSSS)
  IHOUR1=MOD(IHOUR+NCTH(KSTEP),24)
  KSTEPINI=NCTH(KSTEP)
  WRITE(KULOUT,FMT='('' GRIB IY IM ID IH:'',I6,3I3,&
   & '' NEW HOUR:'',I3)')&
   & IYYYY,IMM,IDD,IHOUR,IHOUR1  
  WRITE(KULOUT,FMT='('' KSTEPINI :'',I6)') KSTEPINI
  IF(IHOUR+ NCTH(KSTEP)>= 24) THEN
    INC=(IHOUR+NCTH(KSTEP))/24
    CALL UPDCAL(IDD,IMM,IYYYY,INC,ID1,IM1,IYYYY1,ILMO,KULOUT)
  ELSE
    IYYYY1=IYYYY
    IM1=IMM
    ID1=IDD
  ENDIF
  WRITE(KULOUT,FMT='('' NEW DATE:'',I6,2I3)') IYYYY1,IM1,ID1
  KUDATE=IYYYY1*10**4+IM1*10**2+ID1
  KUSSSS=ISHOUR*IHOUR1
ENDIF

!*        3. Set initial date for sensitivity calculations
IF (IABS(KUCONF)/100 == 8) THEN
WRITE (KULOUT,*)' 801 mod_ini NINDAT=', kudate, 'HOUR= ',KUSSSS
  IF (KUSTOP < 0.OR. PTSTEP < ZEPS) THEN
    WRITE (KULOUT,*)'TIME STEP AND LENGTH OF INTEGRATION MUST BE SET'
    WRITE (KULOUT,*)'ON THE COMMAND LINE FOR THIS CONFIGURATION.'
    CALL ABOR1('MOD_INI: ABOR1 CALLED')
  ENDIF

  IYMD= KUDATE
  ICD = IYMD2CD(IYMD,IRET)
  IF(IRET /= 0) CALL ABOR1('MOD_INI:CALL TO IYMD2CD FAILED')
  INSSS= KUSSSS
  ISECRUN =  KUSTOP * PTSTEP

  ISSS = INSSS - ISECRUN + KSTEP
  DO WHILE (ISSS < 0)
    ISSS = ISSS+86400
    ICD  = ICD-1
  ENDDO

  IF (ICD <= 0) THEN
    WRITE(KULOUT,*) 'START DATE IN DIFFERENT CENTURY'
    CALL ABOR1('MOD_INI: ABOR1 CALLED')
  ENDIF

  IYMD = ICD2YMD(ICD,IRET)
  IF(IRET /= 0) CALL ABOR1('MOD_INI:CALL TO ICD2YMD FAILED')
  WRITE (KULOUT,*)'801 mod_ini LECMWF= ',LECMWF
  IF(.NOT.LECMWF) THEN
!   KUDATE = IYMD
!   KUSSSS = ISSS
  WRITE (KULOUT,*)'801 mod_ini WHYYYYYYYYYY'
  ENDIF
  WRITE (KULOUT,*)'801 mod_ini NINDAT= ', KUDATE, 'HOUR= ',KUSSSS 
ENDIF

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

