SUBROUTINE TSLVSTST(KSTART,KSTOP,PSTEP,KNOTSLOTS,KTSLVSTST)

!**** *TSLVSTST* - FINDS OUT RELATIONSHIP BETWEEN TIMESLOT AND TIMESTEP
!                  (3D/4D VAR)

!     Purpose.
!     --------
!     TIMESLOT VS TIMESTEP

!**   Interface.
!     ----------
!        *CALL* *CNT4AD

!        Explicit arguments :
!        --------------------
!        KSTART    : START OF ANAL. PERIOD (IN TERMS OF TIME STEPS)
!        KSTOP     : END OF ANAL. PERIOD (IN TERMS OF TIME STEPS)
!        PSTEP     : TIMESTEP
!        KNOTSLOTS : TOTAL NO. OF TIMESLOTS
!        KTSLVSTST : TIMESLOTS VS TIMESTEPS

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

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

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

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

!     Author.
!     -------
!        DRASKO VASILJEVIC *ECMWF*

!     Modifications.
!     --------------
!        Original : 01-04-20
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!     ------------------------------------------------------------------

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

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KNOTSLOTS 
INTEGER(KIND=JPIM),INTENT(IN)    :: KSTART 
INTEGER(KIND=JPIM),INTENT(IN)    :: KSTOP 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSTEP 
INTEGER(KIND=JPIM),INTENT(OUT)   :: KTSLVSTST(KNOTSLOTS) 
INTEGER(KIND=JPIM) :: JTSL, JTST

REAL(KIND=JPRB) :: ZANPER, ZTSLOT, ZMIN

REAL(KIND=JPRB) :: ZTSTTS(KNOTSLOTS,0:KSTOP), ZMIDT(KNOTSLOTS)
REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

!*       1.    Check observation time.
!              -----------------------

!*       1.1   Preset

IF (LHOOK) CALL DR_HOOK('TSLVSTST',0,ZHOOK_HANDLE)
ZANPER = (KSTOP-KSTART) * PSTEP
ZTSLOT = ZANPER / (KNOTSLOTS-1)

!*       1.2   Time slot mid times

DO JTSL = 1 , KNOTSLOTS
  ZMIDT(JTSL) = (JTSL-1) * ZTSLOT
ENDDO

!*       1.3   Time slot vs Time step
 
DO JTSL = 1 , KNOTSLOTS
  DO JTST = 0 , KSTOP
    ZTSTTS(JTSL,JTST)  = ZMIDT(JTSL) - REAL(NINT(JTST*PSTEP),JPRB)
  ENDDO
ENDDO

DO JTSL = 1 , KNOTSLOTS
  ZMIN         = ABS(ZTSTTS(JTSL,0))
  KTSLVSTST(JTSL) = 0
  DO JTST = 1 , KSTOP
    IF(ABS(ZTSTTS(JTSL,JTST)) < ZMIN) THEN
      ZMIN   = ABS(ZTSTTS(JTSL,JTST))
      KTSLVSTST(JTSL) = JTST
    ENDIF
  ENDDO
ENDDO

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

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