SUBROUTINE PPREQ(KTIME,LDNOPPFL,CDYFILE)

!**** *PPREQ* -  SEARCH FOR A SUITABLE POSTPROCESSING NAMELIST FILE
!                AND READS IT.

!     VERSION 1     D. DENT        14/10/93    E. C. M. W. F.

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

!     *PPREQ*  SEARCHES A UNIX DIRECTORY LISTING
!               AS GENERATED BY THE COMMAND 'ls'
!               THE LISTING IS CONTAINED IN A FILE DEFINED BY CFDIRLST
!               THE TIME LEVEL SUPPLIED TO *PPREQ* IS
!               MATCHED WITH THE FILE NAMES FOUND
!               USING THE NAMING CONVENTION DESCRIBED UNDER *METHOD*.

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

!     *CALL* *PPREQ(KTIME,LDNOPPFL)*

!        *KTIME* - INTEGER TIME LEVEL REQUIRED (IN SECONDS)
!                  SPECIAL CASE *KTIME=-1* FOR UNINITIALISED DATA
!        *LDNOPPFL* LOGICAL, RETURNED TRUE IF NO FILE FOUND

!     METHOD.
!     -------

!          READ THE GIVEN DIRECTORY LISTING FILE ASSUMING THE
!          FOLLOWING FORMAT:     (xx are any characters)

!            xxtDDDDHHMM     where DDDDHHMM specifies the date/time
!            xxtu            specifying uninitialised data
!                            (there must be at least one trailing *u*)
!            xxfDHHMMtDHHMM  where DHHMM specify from/to date/times
!            xxFDDDHHTDDDHH  where DDDHH specify from/to date/times
!                            The range    includes the upper time
!                            but does not include  the lower time

!            D may be replaced by 'd' to act as a wild character for days
!            H may be replaced by 'h' to act as a wild character for hours
!            M may be replaced by 'm' to act as a wild character for minutes
!            However, wild characters will not match with date/time=0

!     EXTERNALS.
!     ----------

!          SUPP

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

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

USE YOMCT0   , ONLY : CFDIRLST ,LFPOS
USE YOMLUN   , ONLY : NULOUT   ,NPDIRL

IMPLICIT NONE

INTEGER(KIND=JPIB),INTENT(IN)    :: KTIME 
LOGICAL           ,INTENT(OUT)   :: LDNOPPFL 
CHARACTER(LEN=*)  ,INTENT(OUT)   :: CDYFILE

INTEGER(KIND=JPIM),PARAMETER :: JCHAR = 120

CHARACTER(LEN=JCHAR) :: CLFILE, CLBASE
CHARACTER(LEN=6)     :: CLDAYS
CHARACTER(LEN=2)     :: CLHOURS, CLMINS
CHARACTER(LEN=1)     :: CLAST, CLTYPE
CHARACTER(LEN=6)     :: CLTD
CHARACTER(LEN=2)     :: CLTH, CLTM
CHARACTER(LEN=10)    :: CLNUMBER='0123456789'

INTEGER(KIND=JPIM) :: I, IB, IBLANK, ICH, IDAYS, IFD, IFH, IFM,&
 & IFTIME, IHOURS, ILD, ILENFN, ILH, ILM, ILTIME, &
 & IMINS, ITIME, IWDAY, IWHOUR, IWMIN  
REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "abor1.intfb.h"
#include "supp.intfb.h"

IF (LHOOK) CALL DR_HOOK('PPREQ',0,ZHOOK_HANDLE)
ITIME=KTIME/60
IDAYS=ITIME/24/60
IHOURS=ITIME/60-IDAYS*24
IMINS =ITIME-(IDAYS*24+IHOURS)*60
WRITE(CLTD,'(I6.6)') IDAYS
WRITE(CLTH,'(I2.2)') IHOURS
WRITE(CLTM,'(I2.2)') IMINS

OPEN(NPDIRL,FILE=CFDIRLST)
110 CONTINUE

READ(NPDIRL,'(A)',END=900) CLFILE

!          1.2     LOOK FOR LAST /

!ILENFN=LEN(CLFILE)
ILENFN=JCHAR+1
IBLANK=JCHAR+1
DO I=1,ILENFN
  ICH=I+1
  IF(CLFILE(I:I) == '/') GO TO 130
  IF(CLFILE(I:I) == ' ') IBLANK=I
ENDDO
ICH=1

130 CONTINUE

!          *ICH* CONTAINS POSITION OF FIRST CHARACTER AFTER LAST /

CLBASE=CLFILE(ICH:)
IB=IBLANK-1
CLAST=CLBASE(IB:IB)

CLTYPE=CLBASE(3:3)
IF(CLTYPE == 't'.OR.CLTYPE == 'T') THEN
!          CHECK FOR SPECIAL CASE 'U'
  IF(CLAST == 'U'.OR.CLAST == 'u') THEN
    IF(IMINS < 0) THEN
      GO TO 800
    ELSE
      GO TO 700
    ENDIF
  ENDIF
!          TIME VALUE GIVEN IN FILE NAME
  CLDAYS =CLBASE(4:9)
  CLHOURS=CLBASE(10:11)
  CLMINS ='00'
  IF(CLBASE(10:11) /= '  ')CLMINS=CLBASE(12:13)
!          CHECK FOR WILD CHARACTER 'd'
  IWDAY=0
  DO I=1,6
    IF(CLDAYS(I:I) /= 'd'.AND.CLDAYS(I:I) /= CLTD(I:I))GO TO 700
    IF(CLDAYS(I:I) == 'd') IWDAY=IWDAY+1
  ENDDO
!          CHECK FOR WILD CHARACTERS 'h' AND 'm'
  IWHOUR=0
  IWMIN =0
  DO I=1,2
    IF(CLHOURS(I:I) /= 'h'.AND.CLHOURS(I:I) /= CLTH(I:I))GO TO 700
    IF(CLHOURS(I:I) == 'h') IWHOUR=IWHOUR+1
    IF(CLMINS (I:I) /= 'm'.AND.CLMINS (I:I) /= CLTM(I:I))GO TO 700
    IF(CLMINS (I:I) == 'm') IWMIN =IWMIN +1
  ENDDO
!          REJECT IF WILD CHARACTERS AND DATE/TIME = 0
  IF(IWDAY+IWHOUR+IWMIN > 0.AND.ITIME <= 0) GO TO 700
!          ACCEPT FILE NAME
  GO TO 800
ELSEIF(CLTYPE == 'f'.OR.CLTYPE == 'F') THEN
!          RANGE OF TIME VALUES GIVEN IN FILE NAME
  DO I=0,4
    IF(INDEX(CLNUMBER,CLBASE( 4+I: 4+I)) == 0) GO TO 700
    IF(INDEX(CLNUMBER,CLBASE(10+I:10+I)) == 0) GO TO 700
  ENDDO
  READ(CLBASE( 4: 8),'(I3,I2)')IFD,IFH
  READ(CLBASE(10:14),'(I3,I2)')ILD,ILH
  IF(CLTYPE == 'f') THEN
!             RANGE OF TIME VALUES IN FORM *DHHMM*
    IF(CLBASE(9:9) /= 't') GO TO 700
    IFM=IFH
    ILM=ILH
    IFH=MOD(IFD,100)
    ILH=MOD(ILD,100)
    IFD=IFD/100
    ILD=ILD/100
  ELSE
!             RANGE OF TIME VALUES IN FORM *DDDHH*
    IF(CLBASE(9:9) /= 'T') GO TO 700
    IFM=0
    ILM=0
  ENDIF
  IFTIME=(IFD*24+IFH)*60+IFM
  ILTIME=(ILD*24+ILH)*60+IFM
  IF(ITIME <= IFTIME.OR.ITIME > ILTIME) GO TO 700
  GO TO 800
ENDIF
!        REJECT FILE NAME
700 CONTINUE
GO TO 110
!        ACCEPT FILE NAME
800 CONTINUE

!        read namelist

WRITE(UNIT=NULOUT,FMT=&
 & '('' POST-PROCESSING AT TIME ''&
 & ,I6,'' DAYS, '',I4,'' HOURS, '',I4,'' MINUTES ''&
 & , '' FROM DIRECTIVE FILE '',A)')IDAYS,IHOURS,IMINS,CLFILE  
LDNOPPFL=.FALSE.
CDYFILE=CLFILE
IF (.NOT.LFPOS) CALL SUPP(CLFILE)

GO TO 910

!        END OF DIRECTORY FILE
900 CONTINUE

WRITE(CLFILE,FMT=&
 & '('' NO POST-PROCESSING FILE FOUND AT TIME ''&
 & ,I4,'' DAYS, '',I4,'' HOURS, '',I4,'' MINUTES ''&
 & )') IDAYS,IHOURS,IMINS  
WRITE(NULOUT,'(A)') CLFILE

CALL ABOR1(CLFILE)

910 CONTINUE

CLOSE(NPDIRL)

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