SUBROUTINE PRLMCHK(KOBTYP,KCDTYP,PLAT,PLON,KOBDATE,KOBTIME,KREPST,PTIMDIF,&
 & LDMESAGE,LDFAIL,LDREMOVE)  

!**** SUBROUTINE PRLMCHK - PRELIMINARY CHECKS

!        D. VASILJEVIC     ECMWF     10/05/01

!     PURPOSE
!     -------

!**   INTERFACE
!     ---------

!        CALL PRLMCHK(KOBTYP,KCDTYP,PLAT,PLON,KOBDATE,KOBTIME,KREPST,
!                     LDMESAGE,LDFAIL,LDREMOVE)
!     WHERE:
!            KOBTYP  = OBS. TYPE
!            KCDTYP  = CODE TYPE
!            PLAT    = LAT
!            PLON    = LON
!            KOBDATE = DATE
!            KOBTIME = TIME
!            KREPST  = STATUS
!            PTIMDIF = TIME DIFFERENCE
!            LDMESAGE= OUTPUT MESSAGE SWITCH  
!            LDFAIL  = FAIL CHECK SWITCH  
!            LDREMOVE= REMOVE REP SWITCH  

!        PRLMCHK IS CALLED BY SUBROUTINE ????????

!     METHOD
!     ------

!     EXTERNALS
!     ---------

!        REPSEL   - REPORT SELECTION

!     MODIFICATIONS
!     -------------
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!        M.Hamrud      01-Dec-2003 CY28R1 Cleaning 

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

USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK
USE YOMANCS  , ONLY : NMDI     ,RMDI     ,RADIANS  ,&
 & RNLATLIM ,RSLATLIM ,RELONLIM ,RWLONLIM ,EPSMAC   ,&
 & RTICE    ,RTWAT    ,R2ES     ,R3IES    ,&
 & R3LES    ,R4IES    ,R4LES    ,RTT  
USE YOMMKODB , ONLY : NTBMAR   ,NTFMAR
USE YOMLUN   , ONLY : NULOUT

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

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KOBTYP 
INTEGER(KIND=JPIM),INTENT(IN)    :: KCDTYP 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PLAT 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PLON 
INTEGER(KIND=JPIM),INTENT(IN)    :: KOBDATE 
INTEGER(KIND=JPIM),INTENT(IN)    :: KOBTIME 
INTEGER(KIND=JPIM),INTENT(INOUT) :: KREPST 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PTIMDIF 
LOGICAL           ,INTENT(INOUT)   :: LDMESAGE 
LOGICAL           ,INTENT(OUT)   :: LDFAIL 
LOGICAL           ,INTENT(INOUT) :: LDREMOVE 
!-----------------------------------------------------------------------

INTEGER(KIND=JPIM) :: ITMO100


REAL(KIND=JPRB) :: ZTIMDIF, ZLEFTTM, ZRIGHTM   
REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "repsel.intfb.h"
#include "timdif.intfb.h"

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

#include "fcobs.h"
#include "stmfun_ifs.h"
!*

!        1.             PRESET
!                       ------

IF (LHOOK) CALL DR_HOOK('PRLMCHK',0,ZHOOK_HANDLE)
PTIMDIF = RMDI

!*
!-----------------------------------------------------------------------

!        2.             GENERAL OBSERVATION INFO.
!                       -------------------------

!*
!-----------------------------------------------------------------------

!        3.             PRELIMINARY CHECKS
!                       ------------------

!*          3.1         IS POSITION PRESENT AND WITHIN LIMITS

IF(PLAT   == RMDI.OR.PLON   == RMDI    )                 THEN
  WRITE(NULOUT,'(''0PRLMCHK; LAT. OR LON. MISSING;'',&
   & '' HENCE REPORT REJECTED'')')  
  LDFAIL   = .TRUE.
  KREPST   = ICHSTAT_REJECT( KREPST )
  LDMESAGE = .TRUE.
  GO TO 6
ENDIF

IF(PLAT > RNLATLIM.OR.PLAT < RSLATLIM    )               THEN
  WRITE(NULOUT,'(''0PRLMCHK; LAT. VALUE OUT OF RANGE;'',&
   & '' HENCE REPORT REJECTED'')')  
  WRITE(NULOUT,'('' N. LAT. LIM. :'',F10.2)') RNLATLIM
  WRITE(NULOUT,'('' S. LAT. LIM. :'',F10.2)') RSLATLIM
  LDFAIL   = .TRUE.
  KREPST   = ICHSTAT_REJECT( KREPST )
  LDMESAGE = .TRUE.
  GO TO 6
ENDIF

IF(PLON < RWLONLIM.OR.PLON > RELONLIM    )               THEN
  WRITE(NULOUT,'(''0PRLMCHK; LON. VALUE OUT OF RANGE;'',&
   & '' HENCE REPORT REJECTED'')')  
  WRITE(NULOUT,'('' W. LON. LIM. :'',F10.2)') RWLONLIM
  WRITE(NULOUT,'('' E. LON. LIM. :'',F10.2)') RELONLIM
  LDFAIL   = .TRUE.
  KREPST   = ICHSTAT_REJECT( KREPST )
  LDMESAGE = .TRUE.
  GO TO 6
ENDIF

!*          3.2         IS OBS. WANTED

CALL REPSEL(LDREMOVE,KOBTYP,KCDTYP,PLAT,PLON)
IF(LDREMOVE)   THEN
  KREPST = ICHSTAT_REJECT( KREPST )
! GO TO 6
ENDIF

!*          3.3         IS DATE/TIME PRESENT

IF(KOBDATE == NMDI.OR.KOBTIME == NMDI) THEN
  WRITE(NULOUT,'(''0PRLMCHK; DATE OR TIME MISSING;'',&
   & '' HENCE REPORT REJECTED'')')  
  WRITE(NULOUT,'('' OBS DATE:'',I10)')   KOBDATE
  WRITE(NULOUT,'('' OBS TIME:'',I10)')   KOBTIME
  LDFAIL   = .TRUE.
  KREPST   = ICHSTAT_REJECT( KREPST )
  LDMESAGE = .TRUE.
  GO TO 6
ENDIF

!*          3.4         TIME DIFFERENCE; IF 3D VAR CHECK IF TIME
!                       DIFFERENCE WITHIN DATA TIME WINDOW

ITMO100 = KOBTIME / 100
ZTIMDIF = 0.0_JPRB
CALL TIMDIF(KOBDATE,ITMO100,ZTIMDIF)
PTIMDIF = ZTIMDIF

ZLEFTTM = REAL(NTBMAR,JPRB)
ZRIGHTM = REAL(NTFMAR,JPRB)
IF(ZTIMDIF < ZLEFTTM.OR.ZTIMDIF > ZRIGHTM    )      THEN
  WRITE(NULOUT,'(''0PRLMCHK; TIME OUT OF RANGE;'',&
   & '' HENCE REPORT REJECTED'')')  
  WRITE(NULOUT,'('' TIME DIFF.  : '',F10.2)') ZTIMDIF
  WRITE(NULOUT,'('' OBS DATE    : '',I10  )') KOBDATE
  WRITE(NULOUT,'('' OBS TIME    : '',I10  )') KOBTIME
  WRITE(NULOUT,'('' OBS/CODE TYPE: '',2(I10,1X))') KOBTYP,KCDTYP
  LDFAIL   = .TRUE.
  KREPST   = ICHSTAT_REJECT( KREPST )
  LDMESAGE = .TRUE.
  GO TO 6
ENDIF
!*
!-----------------------------------------------------------------------

!        4.             ?

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

!        5.             FINISH OFF REPORT (re)CREATION

!*
!-----------------------------------------------------------------------

!        6.             RETURN
!                       ------

6 CONTINUE
IF (LHOOK) CALL DR_HOOK('PRLMCHK',1,ZHOOK_HANDLE)
END SUBROUTINE PRLMCHK
