SUBROUTINE REPSEL(LDREMOVE,KOBTYP,KCDTYP,PLAT,PLON)

!**** SUBROUTINE REPSEL - REPORT SELECTION

!          D. VASILJEVIC     ECMWF          08/07/1991

!*    METHOD
!     ------

!        REPORTS ARE SLECTED ACCORDING TO OBS./CODE TYPE AND AREA FOR
!     FURTHER PROCESSING.

!*    EXTERNALS
!     ---------

!        CALL REPSEL(LDREMOVE,KOBTYP,KCDTYP,PLAR,PLON)
!     WHERE:
!           LDREMOVE = REMOVE/KEEP SWITCH
!           KOBTYP   = OBS. TYPE
!           KCDTYP   = CODE TYPE
!           PLAT     = OBS. LAT.
!           PLON     = OBS. LON.

!        REPSEL IS CALLED FROM

!**   EXTERNALS
!     ---------

!        NONE

!**   MODIFICATIONS
!     -------------
!     23-12-2004  N. Bormann    RARAD becomes LIMB; new codetypes for LIMB
!-----------------------------------------------------------------------

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

USE YOMCOCTP , ONLY : NSYNOP   ,NSRSCD   ,NATSCD   ,NSHSCD   ,&
 & NABSCD   ,NSHRED   ,NATSHS   ,NAIREP   ,NAIRCD   ,&
 & NCODAR   ,NCOLBA   ,NAMDAR   ,NACARS   ,NSIMAI   ,&
 & NSATOB   ,NSTBCD   ,NSTB86   ,NSTB87   ,NSST     ,&
 & NDRIBU   ,&
 & NDRBCD   ,NBATHY   ,NTESAC   ,NDERS1   ,NTEMP    ,&
 & NLDTCD   ,NSHTCD   ,NTDROP   ,NROCOB   ,NROCSH   ,&
 & NMBTMP   ,NSIMTE   ,NPILOT   ,NLDPCD   ,NSHPCD   ,&
 & NWPPCD   ,NSATEM   ,NSTMCD   ,NSTOVS   ,NSTRO3   ,&
 & NSTDWL   ,NSTTOV   ,NGTSTB   ,NGTST1   ,NGTST2   ,&
 & NGTHRB   ,NGTHR1   ,NGTHR2   ,NSSMI    ,NPAOB    ,&
 & NPABCD   ,NSCATT   ,NSCAT1   ,NSCAT2   ,NSCAT3   ,&
 & NLIMB    ,NGPSRO   ,NLRAD    ,NSCAT4   ,NSCAT5   ,NSCAT6 ,&
 & NEWPPCD  ,NJWPPCD  
USE YOMMKODB , ONLY : LSYNOP   ,LCD011   ,LCD014   ,LCD021   ,&
 & LCD022   ,LCD023   ,LCD024   ,LAIREP   ,LCD041   ,&
 & LCD141   ,LCD142   ,LCD144   ,LCD145   ,LCD241   ,&
 & LSATOB   ,LCD088   ,LCD089   ,LCD090   ,LCD188   ,&
 & LODRIBU  ,&
 & LCD063   ,LCD064   ,LCD165   ,LCD160   ,LTEMP    ,&
 & LCD035   ,LCD036   ,LCD037   ,LCD135   ,LCD137   ,&
 & LCD039   ,LCD040   ,&
 & LOPILOT  ,LCD032   ,LCD033   ,LCD034   ,LCD131   ,&
 & LCD134   ,&
 & LSATEM   ,LCD086   ,LCD186   ,LCD185   ,&
 & LCD184   ,LCD206   ,LCD200   ,LCD201   ,LCD202   ,&
 & LCD210   ,LCD211   ,LCD212   ,LCD215   ,LOPAOB   ,&
 & LCD180   ,LSCATT   ,LCD008   ,LCD122   ,LCD210SC ,&
 & LCD139   ,LALIMB   ,LCD250   ,LCD251   ,&
 & EXNLAT   ,EXSLAT   ,EXWLON   ,EXELON   ,&
 & LCD301   ,LCD302,&
 & OBNLATE  ,OBSLATE  ,OBWLONE  ,OBELONE  
USE YOMLUN   , ONLY : NULOUT

IMPLICIT NONE

LOGICAL           ,INTENT(OUT)   :: LDREMOVE 
INTEGER(KIND=JPIM),INTENT(IN)    :: KOBTYP 
INTEGER(KIND=JPIM),INTENT(IN)    :: KCDTYP 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PLAT 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PLON 
REAL(KIND=JPRB) :: ZEXELON, ZEXNLAT, ZEXSLAT, ZEXWLON, ZOBELON,&
 & ZOBNLAT, ZOBSLAT, ZOBWLON  
REAL(KIND=JPRB) :: ZHOOK_HANDLE

!        1.       REPORT SELECTION
!                 ----------------

!*          1.0   CHECK WHAT RUN

IF (LHOOK) CALL DR_HOOK('REPSEL',0,ZHOOK_HANDLE)
ZOBNLAT = OBNLATE
ZOBSLAT = OBSLATE
ZOBWLON = OBWLONE
ZOBELON = OBELONE
ZEXNLAT = EXNLAT
ZEXSLAT = EXSLAT
ZEXWLON = EXWLON
ZEXELON = EXELON

!*          1.1   REPORT SELECTION ACCORDNG TO OBS./CODE TYPE

IF((KOBTYP == NSYNOP.AND..NOT.LSYNOP) .OR.&
   & (KOBTYP == NAIREP.AND..NOT.LAIREP) .OR.&
   & (KOBTYP == NSATOB.AND..NOT.LSATOB) .OR.&
   & (KOBTYP == NDRIBU.AND..NOT.LODRIBU).OR.&
   & (KOBTYP == NTEMP .AND..NOT.LTEMP ) .OR.&
   & (KOBTYP == NPILOT.AND..NOT.LOPILOT).OR.&
   & (KOBTYP == NSATEM.AND..NOT.LSATEM) .OR.&
   & (KOBTYP == NPAOB .AND..NOT.LOPAOB ).OR.&
   & (KOBTYP == NSCATT.AND..NOT.LSCATT) .OR.&
   & (KOBTYP == NLIMB .AND..NOT.LALIMB)     )     THEN  
  IF(ZEXWLON > 0.0_JPRB.AND.ZEXELON < 0.0_JPRB)             THEN
    IF((PLAT <= ZEXNLAT.AND.PLAT >= ZEXSLAT).AND.&
     & (PLON >= ZEXWLON.OR.PLON <= ZEXELON)     )  GO TO 130  
  ELSE
    IF((PLAT <= ZEXNLAT.AND.PLAT >= ZEXSLAT).AND.&
     & (PLON >= ZEXWLON.AND.PLON <= ZEXELON)     )  GO TO 130  
  ENDIF
ENDIF

IF(KOBTYP == NSYNOP)      THEN
  IF((KCDTYP == NSRSCD.AND..NOT.LCD011).OR.&
     & (KCDTYP == NATSCD.AND..NOT.LCD014).OR.&
     & (KCDTYP == NSHSCD.AND..NOT.LCD021).OR.&
     & (KCDTYP == NABSCD.AND..NOT.LCD022).OR.&
     & (KCDTYP == NSHRED.AND..NOT.LCD023).OR.&
     & (KCDTYP == NATSHS.AND..NOT.LCD024)    )       THEN  
    IF(ZEXWLON > 0.0_JPRB.AND.ZEXELON < 0.0_JPRB)                THEN
      IF((PLAT <= ZEXNLAT.AND.PLAT >= ZEXSLAT).AND.&
       & (PLON >= ZEXWLON.OR.PLON <= ZEXELON)     ) GO TO 130  
    ELSE
      IF((PLAT <= ZEXNLAT.AND.PLAT >= ZEXSLAT).AND.&
       & (PLON >= ZEXWLON.AND.PLON <= ZEXELON)     ) GO TO 130  
    ENDIF
  ENDIF
ENDIF

IF(KOBTYP == NAIREP)      THEN
  IF((KCDTYP == NAIRCD.AND..NOT.LCD141).OR.&
     & (KCDTYP == NCODAR.AND..NOT.LCD041).OR.&
     & (KCDTYP == NCOLBA.AND..NOT.LCD241).OR.&
     & (KCDTYP == NAMDAR.AND..NOT.LCD144).OR.&
     & (KCDTYP == NACARS.AND..NOT.LCD145).OR.&
     & (KCDTYP == NSIMAI.AND..NOT.LCD142)    )       THEN  
    IF(ZEXWLON > 0.0_JPRB.AND.ZEXELON < 0.0_JPRB)                THEN
      IF((PLAT <= ZEXNLAT.AND.PLAT >= ZEXSLAT).AND.&
       & (PLON >= ZEXWLON.OR.PLON <= ZEXELON)     ) GO TO 130  
    ELSE
      IF((PLAT <= ZEXNLAT.AND.PLAT >= ZEXSLAT).AND.&
       & (PLON >= ZEXWLON.AND.PLON <= ZEXELON)     ) GO TO 130  
    ENDIF
  ENDIF
ENDIF

IF(KOBTYP == NSATOB)      THEN
  IF((KCDTYP == NSTBCD.AND..NOT.LCD088).OR.&
     & (KCDTYP == NSTB86.AND..NOT.LCD089).OR. &
     & (KCDTYP == NSST  .AND..NOT.LCD188).OR.&
     & (KCDTYP == NSTB87.AND..NOT.LCD090)    )       THEN  
    IF(ZEXWLON > 0.0_JPRB.AND.ZEXELON < 0.0_JPRB)              THEN
      IF((PLAT <= ZEXNLAT.AND.PLAT >= ZEXSLAT).AND.&
       & (PLON >= ZEXWLON.OR.PLON <= ZEXELON)     ) GO TO 130  
    ELSE
      IF((PLAT <= ZEXNLAT.AND.PLAT >= ZEXSLAT).AND.&
       & (PLON >= ZEXWLON.AND.PLON <= ZEXELON)     ) GO TO 130  
    ENDIF
  ENDIF
ENDIF

IF((KCDTYP == NDRBCD.AND..NOT.LCD165).OR.&
   & (KCDTYP == NBATHY.AND..NOT.LCD063).OR.&
   & (KCDTYP == NTESAC.AND..NOT.LCD064).OR.&
   & (KCDTYP == NDERS1.AND..NOT.LCD160)    )       THEN  
  IF(ZEXWLON > 0.0_JPRB.AND.ZEXELON < 0.0_JPRB)              THEN
    IF((PLAT <= ZEXNLAT.AND.PLAT >= ZEXSLAT).AND.&
     & (PLON >= ZEXWLON.OR.PLON <= ZEXELON)     )   GO TO 130  
  ELSE
    IF((PLAT <= ZEXNLAT.AND.PLAT >= ZEXSLAT).AND.&
     & (PLON >= ZEXWLON.AND.PLON <= ZEXELON)     )   GO TO 130  
  ENDIF
ENDIF

IF(KOBTYP == NTEMP )      THEN
  IF((KCDTYP == NLDTCD.AND..NOT.LCD035).OR.&
     & (KCDTYP == NSHTCD.AND..NOT.LCD036).OR.&
     & (KCDTYP == NTDROP.AND..NOT.LCD135).OR.&
     & (KCDTYP == NROCOB.AND..NOT.LCD039).OR.&
     & (KCDTYP == NROCSH.AND..NOT.LCD040).OR.&
     & (KCDTYP == NMBTMP.AND..NOT.LCD037).OR.&
     & (KCDTYP == NSIMTE.AND..NOT.LCD137)    )      THEN  
    IF(ZEXWLON > 0.0_JPRB.AND.ZEXELON < 0.0_JPRB)                THEN
      IF((PLAT <= ZEXNLAT.AND.PLAT >= ZEXSLAT).AND.&
       & (PLON >= ZEXWLON.OR.PLON <= ZEXELON)     ) GO TO 130  
    ELSE
      IF((PLAT <= ZEXNLAT.AND.PLAT >= ZEXSLAT).AND.&
       & (PLON >= ZEXWLON.AND.PLON <= ZEXELON)     ) GO TO 130  
    ENDIF
  ENDIF
ENDIF

IF(KOBTYP == NPILOT)      THEN
  IF((KCDTYP == NLDPCD .AND..NOT.LCD032).OR.&
     & (KCDTYP == NSHPCD .AND..NOT.LCD033).OR.&
     & (KCDTYP == NWPPCD .AND..NOT.LCD034).OR.&
     & (KCDTYP == NEWPPCD.AND..NOT.LCD134).OR.&
     & (KCDTYP == NJWPPCD.AND..NOT.LCD131)    )      THEN  
    IF(ZEXWLON > 0.0_JPRB.AND.ZEXELON < 0.0_JPRB)                THEN
      IF((PLAT <= ZEXNLAT.AND.PLAT >= ZEXSLAT).AND.&
       & (PLON >= ZEXWLON.OR.PLON <= ZEXELON)     ) GO TO 130  
    ELSE
      IF((PLAT <= ZEXNLAT.AND.PLAT >= ZEXSLAT).AND.&
       & (PLON >= ZEXWLON.AND.PLON <= ZEXELON)     ) GO TO 130  
    ENDIF
  ENDIF
ENDIF

IF(KOBTYP == NSATEM)      THEN
  IF((KCDTYP == NSTMCD.AND..NOT.LCD086).OR.&
     & (KCDTYP == NSTOVS.AND..NOT.LCD186).OR.&
     & (KCDTYP == NSTDWL.AND..NOT.LCD185).OR.&
     & (KCDTYP == NSTTOV.AND..NOT.LCD184).OR.&
     & (KCDTYP == NSTRO3.AND..NOT.LCD206).OR.&
     & (KCDTYP == NGTSTB.AND..NOT.LCD200).OR.&
     & (KCDTYP == NGTST1.AND..NOT.LCD201).OR.&
     & (KCDTYP == NGTST2.AND..NOT.LCD202).OR.&
     & (KCDTYP == NGTHRB.AND..NOT.LCD210).OR.&
     & (KCDTYP == NGTHR1.AND..NOT.LCD211).OR.&
     & (KCDTYP == NGTHR2.AND..NOT.LCD212).OR.&
     & (KCDTYP == NSSMI .AND..NOT.LCD215)    )       THEN  
    IF(ZEXWLON > 0.0_JPRB.AND.ZEXELON < 0.0_JPRB)              THEN
      IF((PLAT <= ZEXNLAT.AND.PLAT >= ZEXSLAT).AND.&
       & (PLON >= ZEXWLON.OR.PLON <= ZEXELON)     ) GO TO 130  
    ELSE
      IF((PLAT <= ZEXNLAT.AND.PLAT >= ZEXSLAT).AND.&
       & (PLON >= ZEXWLON.AND.PLON <= ZEXELON)     ) GO TO 130  
    ENDIF
  ENDIF
ENDIF

IF(KOBTYP == NPAOB )      THEN
  IF( KCDTYP == NPABCD.AND..NOT.LCD180     )      THEN
    IF(ZEXWLON > 0.0_JPRB.AND.ZEXELON < 0.0_JPRB)             THEN
      IF((PLAT <= ZEXNLAT.AND.PLAT >= ZEXSLAT).AND.&
       & (PLON >= ZEXWLON.OR.PLON <= ZEXELON)     ) GO TO 130  
    ELSE
      IF((PLAT <= ZEXNLAT.AND.PLAT >= ZEXSLAT).AND.&
       & (PLON >= ZEXWLON.AND.PLON <= ZEXELON)     ) GO TO 130  
    ENDIF
  ENDIF
ENDIF

IF(KOBTYP == NSCATT)      THEN
  IF  ((KCDTYP == NSCAT1.AND..NOT.LCD008  ).OR.&
     & (KCDTYP == NSCAT2.AND..NOT.LCD122  ).OR.&
     & (KCDTYP == NSCAT3.AND..NOT.LCD139  ).OR.&
     & (KCDTYP == NSCAT4.AND..NOT.LCD210SC).OR.&
     & (KCDTYP == NSCAT5.AND..NOT.LCD301  ).OR.& 
     & (KCDTYP == NSCAT6.AND..NOT.LCD302  ))  THEN  
    IF(ZEXWLON > 0.0_JPRB.AND.ZEXELON < 0.0_JPRB)             THEN
      IF((PLAT <= ZEXNLAT.AND.PLAT >= ZEXSLAT).AND.&
       & (PLON >= ZEXWLON.OR.PLON <= ZEXELON)     ) GO TO 130  
    ELSE
      IF((PLAT <= ZEXNLAT.AND.PLAT >= ZEXSLAT).AND.&
       & (PLON >= ZEXWLON.AND.PLON <= ZEXELON)     ) GO TO 130  
    ENDIF
  ENDIF
ENDIF

IF(KOBTYP == NLIMB)      THEN
  IF(  (KCDTYP == NGPSRO.AND..NOT.LCD250).OR.&
     & (KCDTYP == NLRAD .AND..NOT.LCD251)     )      THEN
    IF(ZEXWLON > 0.0_JPRB.AND.ZEXELON < 0.0_JPRB)             THEN
      IF((PLAT <= ZEXNLAT.AND.PLAT >= ZEXSLAT).AND.&
       & (PLON >= ZEXWLON.OR.PLON <= ZEXELON)     ) GO TO 130  
    ELSE
      IF((PLAT <= ZEXNLAT.AND.PLAT >= ZEXSLAT).AND.&
       & (PLON >= ZEXWLON.AND.PLON <= ZEXELON)     ) GO TO 130  
    ENDIF
  ENDIF
ENDIF

!*          1.2   REPORT SELECTION ACCORDING TO AREA

IF(PLAT   < ZOBSLAT)                                  GO TO 130
IF(PLAT   > ZOBNLAT)                                  GO TO 130
IF(ZOBWLON < ZOBELON)       THEN
  IF(PLON   < ZOBWLON.OR.PLON   > ZOBELON)         GO TO 130
ELSE
  IF(PLON   < ZOBWLON.AND.PLON   > ZOBELON)         GO TO 130
ENDIF
GO TO 140

!*          1.3   REMOVE NOT WANTED REPORT

130 CONTINUE
WRITE(NULOUT,*) 'REPSEL: REMOVING OBS',KOBTYP,KCDTYP,PLAT,PLON
LDREMOVE = .TRUE.
GO TO 2

!*          1.4   KEEP OBS.

140 CONTINUE
LDREMOVE = .FALSE.
!*
!-----------------------------------------------------------------------

!        2.       RETURN
!                 ------

2 CONTINUE
IF (LHOOK) CALL DR_HOOK('REPSEL',1,ZHOOK_HANDLE)
END SUBROUTINE REPSEL

