SUBROUTINE REAST(KDFI,KDIGLST,KDAT,KSSS,KDEC)

!**** *REAST * - Routine to adjust astronomy before each dfi session

!     Purpose.
!     --------
!           Reinitialize date and time of YOMRIP.

!**   Interface.
!     ----------
!        *CALL* *REAST(KDFI,KDIGLST,KDAT,KSSS,KDEC)*

!        Explicit arguments :
!        --------------------
!        KDFI   : dfi session (1-5) or 999(initialisation)/998(finalisation)
!        KDIGLST: number of dfi timesteps in one direction
!        KDAT   : original date
!        KSSS   : original launching time in seconds
!        KDEC   : shift in NSTEP (KDFI=4 or 6)

!        Implicit arguments :
!        --------------------
!        COMMON YOMRIP , YEMDFI

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

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

!     Reference.
!     ----------
!        ARPEGE/ALADIN DOCUMENTATION

!     Author.
!     -------
!       Gabor Radnoti GMAP/MICECO

!     Modifications.
!     --------------
!        Original : 92-12-24
!        D. Giard : 94-01-03 case KDFI=5, recursive filfering
!        D. Giard : 94-10-18 effective delay for recursive filters
!        V. Ivanovici, D. Giard : 95-01-19 backward+forward filtering
!        D. Giard : 98-08-27 modifications for finalisation
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!     ------------------------------------------------------------------

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

USE YOMCST   , ONLY : RDAY
USE YOMDYN   , ONLY : TSTEP
USE YOMRIP   , ONLY : NSSSSS   ,RTIMST   ,RTIMTR
USE YOMDFI   , ONLY : HWREC    ,NORDFI   ,LRECURS

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(INOUT) :: KDFI 
INTEGER(KIND=JPIM),INTENT(IN)    :: KDIGLST 
INTEGER(KIND=JPIM),INTENT(IN)    :: KDAT 
INTEGER(KIND=JPIM),INTENT(IN)    :: KSSS 
INTEGER(KIND=JPIM),INTENT(OUT)   :: KDEC 
INTEGER(KIND=JPIM) :: IA, ID, IDAT, IDEC, IM, ISSS

LOGICAL :: LLCHANGE

REAL(KIND=JPRB) :: ZJU, ZTI
REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "fcttim.h"
!     ------------------------------------------------------------------

IF (LHOOK) CALL DR_HOOK('REAST',0,ZHOOK_HANDLE)
LLCHANGE=.FALSE.

!*             POTENTIAL SHIFT IN NSTEP.
!              -------------------------

! corresponding step for filtered fields, in case of finalisation
IF (LRECURS) THEN
  IDEC= MAX(0,2*KDIGLST-NINT(HWREC(2*NORDFI+3)/ABS(TSTEP)))
ELSE
  IDEC= KDIGLST
ENDIF
! check effective shift
KDEC=0
! forward filtering -> finalisation or
!  initialisation (recursive filtering with time-span lower than delay)
IF (KDFI == 4.OR. KDFI == 998) THEN
  KDEC= IDEC
! backward+forward dfi sessions
ELSEIF (KDFI == 6) THEN
  KDEC= -MAX(0,SIGN(IDEC,NINT(TSTEP)))
ENDIF

!*             CURRENT TIME.
!              -------------

! forward dfi session (t<=0)
IF (KDFI == 2) THEN
  NSSSSS=KSSS-INT(KDIGLST*ABS(TSTEP))
! forward dfi session (t<=0) for modified diabatic scheme
ELSEIF (KDFI == 5) THEN
  NSSSSS=KSSS-INT((KDIGLST+1)*ABS(TSTEP))
! backward+forward dfi sessions
ELSEIF (KDFI == 6) THEN
  NSSSSS=KSSS+INT(KDEC*ABS(TSTEP))
! forward filtering -> finalisation or
!  initialisation (recursive filtering with time-span lower than delay)
ELSEIF (KDFI == 998) THEN
  NSSSSS=KSSS+INT(KDEC*ABS(TSTEP))
  IF (NSSSSS == KSSS) LLCHANGE=.TRUE.
! backward dfi session (t<=0) : KDFI=1    !
! forward dfi session (t>=0) : KDFI=3,4   ! initial time reset
! initialisation : KDFI=999               !
ELSE
  NSSSSS=KSSS
ENDIF
IF (KDFI == 998.AND.LLCHANGE) KDFI=999

!*             REDEFINE ASTRONOMICAL CONSTANTS.
!              --------------------------------

IF (NSSSSS < 0) NSSSSS=INT(RDAY) + NSSSSS
IDAT=KDAT
ISSS=NSSSSS
ID=NDD(IDAT)
IM=NMM(IDAT)
IA=NCCAA(IDAT)
ZJU=RJUDAT(IA,IM,ID)
ZTI=RTIME(IA,IM,ID,ISSS,RDAY)
RTIMST=ZTI
RTIMTR=ZTI

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