SUBROUTINE MONVAR(KREFTS,KANATS,KGRATS,KHECPS)

!**** *MONVAR* - Management of the 3-D/4-D VAR  events

!     Purpose.
!     --------
!     Set up the 3-D/4-D VAR events control arrays

!**   Interface.
!     ----------
!        *CALL* *MONVAR(...)

!        Explicit arguments :
!        --------------------
!        KREFTS : ARRAY CONTAINING SIMULATED OBS. EVENTS STEPS
!        KANATS : ARRAY CONTAINING ANALYSIS WRITE-OUT EVENTS
!        KGRATS : ARRAY CONTAINING GRADIENT WRITE-OUT EVENTS
!        KHECPS : ARRAY CONTAINING AVARC I/O EVENTS

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

!     Method.
!     -------
!        See documentation (mon oeil !!)

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

!     Reference.
!     ----------
!        Documentation ARPEGE  (mon oeil !!)

!     Author.
!     -------
!        Jean-Noel Thepaut  *ECMWF*
!        somewhat based on MONIO written by P.Courtier.
!     Modifications.
!     --------------
!        Original : 91-09-05
!        Modified by Erik Andersson: 91-11-01 AN/GR events added
!        Modified by R. Brozkova   : 01-04-27 AVARC events added
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!     ------------------------------------------------------------------

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

USE YOMCT0   , ONLY : NSTART   ,NSTOP
USE YOMVAR   , ONLY : NREFTS   ,NANATS   ,NGRATS   ,&
 & NSIMU    ,NDIAG    ,NFRREF   ,NFRANA   ,&
 & NFRGRA   ,NSIM4DL  
USE YOMHCP   , ONLY : NHECP
USE YOMDYN   , ONLY : TSTEP

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(OUT)   :: KREFTS(0:NSTOP/NFRREF) 
INTEGER(KIND=JPIM),INTENT(OUT)   :: KANATS(0:NSIM4DL/NFRANA) 
INTEGER(KIND=JPIM),INTENT(OUT)   :: KGRATS(0:NSIM4DL/NFRGRA) 
INTEGER(KIND=JPIM),INTENT(OUT)   :: KHECPS(0:NSTOP) 
INTEGER(KIND=JPIM) :: ISS, ILS, JS

REAL(KIND=JPRB) :: ZISS, ZISSX, ZLS, ZTSTEP, ZUNIT
REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

!*       1.    Prepare occurences of simulated obs. events.
!              --------------------------------------------

!  ZUNIT  : NUMBER OF SECONDS IN TIME UNIT
IF (LHOOK) CALL DR_HOOK('MONVAR',0,ZHOOK_HANDLE)
ZUNIT=3600._JPRB
IF(ABS(TSTEP) < TINY(ZLS))THEN
  ZTSTEP=TINY(ZLS)
ELSE
  ZTSTEP=TSTEP
ENDIF
ZISSX=REAL(HUGE(ILS),JPRB)

KHECPS(:)=0

DO JS=NSTART,NSTOP/NFRREF
  KREFTS(JS)=0
ENDDO

!*    1.5   SIMULATED OBS. EVENTS

IF(NREFTS(0) >= 1)THEN
  DO JS=1,NREFTS(0)
    ISS=NREFTS(JS)*NFRREF
    IF(ISS >= NSTART.AND.ISS <= NSTOP)THEN
      KREFTS(NREFTS(JS))=1
    ENDIF
  ENDDO
ELSE
  DO JS=NSTART,NSTOP
    IF(MOD(JS,NFRREF) == 0)THEN
      KREFTS(JS/NFRREF)=1
    ENDIF
  ENDDO
ENDIF

!*    1.6   AVARC EVENTS (trajectory corrections)
!      -------------------------------------------
IF(NHECP(0) > 0) THEN
  DO JS=1,NHECP(0)
    ZISS=ZUNIT*REAL(NHECP(JS),JPRB)/ZTSTEP
    ZISS=MIN(ZISSX,ZISS)
    ISS=NINT(ZISS)
    IF(ISS >= NSTART.AND.ISS <= NSTOP)THEN
      KHECPS(ISS)=1
    ENDIF
  ENDDO
ENDIF

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

!*       3.    Prepare occurences of ANALYSIS write-outs
!              -----------------------------------------

DO JS=0,NSIM4DL/NFRANA
  KANATS(JS)=0
ENDDO

!*    3.5   ANALYSIS WRITE OUT EVENTS

IF (NANATS(0) >= 1)THEN
  DO JS=1,NANATS(0)
    ISS=NANATS(JS)*NFRANA
    IF (ISS <= NSIMU) KANATS(NANATS(JS))=NDIAG
  ENDDO
ELSE
  DO JS=0,NSIMU
    IF (MOD(JS,NFRANA) == 0) KANATS(JS/NFRANA)=NDIAG
  ENDDO
ENDIF

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

!*       4.    Prepare occurences of GRADIENT write-outs
!              -----------------------------------------

DO JS=0,NSIM4DL/NFRGRA
  KGRATS(JS)=0
ENDDO

!*    3.5   GRADIENT WRITE OUT EVENTS

IF (NGRATS(0) >= 1)THEN
  DO JS=1,NGRATS(0)
    ISS=NGRATS(JS)*NFRGRA
    IF (ISS <= NSIMU) KGRATS(NGRATS(JS))=NDIAG
  ENDDO
ELSE
  DO JS=0,NSIMU
    IF (MOD(JS,NFRGRA) == 0) KGRATS(JS/NFRGRA)=NDIAG
  ENDDO
ENDIF

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

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

