SUBROUTINE RS_BIAS_VALIDITY(P_IN_DATE_TIME,KSONDE,LD_VALID)

!**** *RS_BIAS_VALIDITY*

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

!         BIAS CORRECTION FOR RS TEMPERATURES

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

!         RS_BIAS_VALIDITY(P_IN_DATE_TIME,KSONDE,LD_VALID)

!         LD_VALID  LOGICAL SWITCH

!       METHOD.
!      ---------

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


!       REFERENCE.
!      ------------


!       AUTHOR.
!      ---------


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

USE PARKIND1  ,ONLY : JPIM,    JPRB
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK
USE YOMRSTBIAS
USE YOMANCS  , ONLY : RMDI,    NMDI
USE YOMLUN   , ONLY : NULOUT

IMPLICIT NONE

!     DUMMY INTEGERs
INTEGER(KIND=JPIM),INTENT(IN)    :: KSONDE

LOGICAL               :: LLPRINT
LOGICAL , INTENT(OUT) :: LD_VALID
LOGICAL               :: LL_DATE_TIME_FROM, LL_DATE_FROM    , LL_TIME_FROM
LOGICAL               :: LL_DATE_TIME_TO  , LL_DATE_TO      , LL_TIME_TO
LOGICAL               :: LL_DATE_TIME_IN  , LL_DATE_TIME_OUT

REAL(KIND=JPRB)              :: ZHOOK_HANDLE
REAL(KIND=JPRB) , INTENT(IN) :: P_IN_DATE_TIME
REAL(KIND=JPRB)              :: Z_DATE_TIME_FROM, Z_DATE_TIME_TO


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

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

!                  1.  CHECK RS BIAS TIME VALIDITY
!                      ---------------------------

LD_VALID          = .FALSE.
LLPRINT           = .FALSE.
IF(LLPRINT) THEN
  WRITE(NULOUT,'('' RS_BIAS_VALIDITY: '',2(I12,1X),F15.0,1X,2(I12,1X))') &
       &MRSDTFR(KSONDE),MRSTIFR(KSONDE),P_IN_DATE_TIME,MRSDTTO(KSONDE),MRSTITO(KSONDE)
  CALL FLUSH(NULOUT)
ENDIF

!             1.1      DATE/TIME FROM (FOR NOW BOTH DATE/TIME HAVE TO BE EITHER MISSING OR PRESENT)

LL_DATE_TIME_FROM = .FALSE.
LL_DATE_FROM      = .FALSE.
LL_TIME_FROM      = .FALSE.

IF(MRSDTFR(KSONDE) == NMDI .AND. MRSTIFR(KSONDE) == NMDI) THEN
  LL_DATE_TIME_FROM  = .TRUE.
  Z_DATE_TIME_FROM   = RMDI
ELSEIF(MRSDTFR(KSONDE) == NMDI) THEN
  LL_TIME_FROM       = .TRUE.
  Z_DATE_TIME_FROM   = MRSTIFR(KSONDE)
ELSEIF(MRSTIFR(KSONDE) == NMDI) THEN
  LL_DATE_FROM       = .TRUE.
  Z_DATE_TIME_FROM   = MRSDTFR(KSONDE)*1000000.0_JPRB
ELSE
  LL_DATE_TIME_FROM  = .TRUE.
  Z_DATE_TIME_FROM   = MRSDTFR(KSONDE)*1000000.0_JPRB + MRSTIFR(KSONDE)
ENDIF

LL_DATE_TIME_IN   = .FALSE.
IF(LL_DATE_TIME_FROM) THEN
  IF(Z_DATE_TIME_FROM /= RMDI) THEN
    IF(P_IN_DATE_TIME >= Z_DATE_TIME_FROM) THEN
      LL_DATE_TIME_IN = .TRUE.
    ENDIF
  ELSE
    LL_DATE_TIME_IN = .TRUE.
  ENDIF
ELSE
  LL_DATE_TIME_IN = .FALSE.
ENDIF

!             1.2      DATE/TIME TO (FOR NOW BOTH DATE/TIME HAVE TO BE EITHER MISSING OR PRESENT)

LL_DATE_TIME_TO  = .FALSE.
LL_DATE_TO       = .FALSE.
LL_TIME_TO       = .FALSE.

IF(MRSDTTO(KSONDE) == NMDI .AND. MRSTITO(KSONDE) == NMDI) THEN
  LL_DATE_TIME_TO    = .TRUE.
  Z_DATE_TIME_TO     = RMDI
ELSEIF(MRSDTTO(KSONDE) == NMDI) THEN
  LL_TIME_TO         = .TRUE.
  Z_DATE_TIME_TO     = MRSTITO(KSONDE)
ELSEIF(MRSTITO(KSONDE) == NMDI) THEN
  LL_DATE_TO         = .TRUE.
  Z_DATE_TIME_TO     = MRSDTTO(KSONDE)*1000000.0_JPRB
ELSE
  LL_DATE_TIME_TO    = .TRUE.
  Z_DATE_TIME_TO     = MRSDTTO(KSONDE)*1000000.0_JPRB + MRSTITO(KSONDE)
ENDIF
    
LL_DATE_TIME_OUT = .FALSE.
IF(LL_DATE_TIME_TO  ) THEN
  IF(Z_DATE_TIME_TO /= RMDI) THEN
    IF(P_IN_DATE_TIME <= Z_DATE_TIME_TO  ) THEN
      LL_DATE_TIME_OUT = .TRUE.
    ENDIF
  ELSE
    LL_DATE_TIME_OUT = .TRUE.
  ENDIF
ELSE
  LL_DATE_TIME_OUT = .FALSE.
ENDIF

!             1.3      CHECK VALIDITY


IF(LLPRINT) THEN
  WRITE(NULOUT,'(''                   '',2(F15.0,1X))') &
       &Z_DATE_TIME_FROM,Z_DATE_TIME_TO
  CALL FLUSH(NULOUT)
ENDIF
IF(LL_DATE_TIME_IN .AND. LL_DATE_TIME_OUT) THEN
  LD_VALID = .TRUE.
ENDIF


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

!                   2.  RETURN
!                       ------

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