      program makedates

      implicit none

      integer i,yyyymmdd,hhmiss
      integer iyb,imb,idb,ihb,iye,ime,ide,ihe
      double precision juldate,jul,julbeg,julend
      logical ex
      character adum1*8,adum2*6,datesname*80
      character*110 directory, tracerdir
      integer ncount,ncountmax
      parameter(ncountmax=80)
      integer len, len2
      real concormix


C Read available wind fields
****************************
      read(*,*) datesname
      read(*,*) iyb
      read(*,*) imb
      read(*,*) idb
      read(*,*) ihb
      read(*,*) iye
      read(*,*) ime
      read(*,*) ide
      read(*,*) ihe
      read(*,*) concormix
      read(*,'(a)') directory
      read(*,'(a)') tracerdir


C     open(10,file='/home/ocooper/public_html/temp/var_for_FLEXPART.txt')
C     read(10,*)
C     read(10,*)
C     read(10,*) iyb
C     read(10,*) imb
C     read(10,*) idb
C     read(10,*) ihb
C     do 8 i=1,11
C8       read(10,*)
C     read(10,*) iye
C     read(10,*) ime
C     read(10,*) ide
C     read(10,*) ihe

      julbeg=juldate(iyb*10000+imb*100+idb,10000*ihb)
      julend=juldate(iye*10000+ime*100+ide,10000*ihe)

      len=index(directory,' ')-1
      len2=index(tracerdir,' ')-1

c      write(*,*) directory(1:len)//'/'//tracerdir(1:len2)//'/grid_pptv_
c    +'//adum1//adum2

      open(50,file='/viper/home/flexpart/public_html/flexpart_interactiv
     +e/temp/dates_'//datesname)

      ncount=0
      do 10 i=0,1000
        jul=julbeg+dble(float(i))/dble(24.)
        if (jul.gt.julend) goto 99
        call caldate(jul,yyyymmdd,hhmiss)
        write(adum1,'(i8.8)') yyyymmdd
        write(adum2,'(i6.6)') hhmiss

 
        if (concormix.eq.2) then
        inquire(file=directory(1:len)//'/'//tracerdir(1:len2)//'/grid_pp
     +tv_'//adum1//adum2,exist=ex)
        else
        inquire(file=directory(1:len)//'/'//tracerdir(1:len2)//'/grid_co
     +nc_'//adum1//adum2,exist=ex)
        endif
c        write(*,*) adum1,adum2,datesname 
         if (ex) then
          write(50,'(i8.8,i6.6)') yyyymmdd,hhmiss
c         write(*,*) yyyymmdd,hhmiss
          ncount=ncount+1
        endif
        if (ncount.gt.ncountmax) goto 99
10      continue

99      continue

      close(50)
      end




      FUNCTION juldate(YYYYMMDD,HHMISS)
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
*                                                                             *
*     Calculates the Julian date                                              *
*                                                                             *
*     AUTHOR: Andreas Stohl (15 October 1993)                                 *
*                                                                             *
*     Variables:                                                              *
*     DD             Day                                                      *
*     HH             Hour                                                     *
*     HHMISS         Hour, minute + second                                    *
*     JA,JM,JY       help variables                                           *
*     JULDATE        Julian Date                                              *
*     JULDAY         help variable                                            *
*     MI             Minute                                                   *
*     MM             Month                                                    *
*     SS             Second                                                   *
*     YYYY           Year                                                     *
*     YYYYMMDDHH     Date and Time                                            *
*                                                                             *
*     Constants:                                                              *
*     IGREG          help constant                                            *
*                                                                             *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 

      IMPLICIT NONE

      INTEGER YYYYMMDD,YYYY,MM,DD,HH,MI,SS,HHMISS
      INTEGER JULDAY,JY,JM,JA,IGREG
      DOUBLE PRECISION JULDATE
      PARAMETER (IGREG=15+31*(10+12*1582))

      YYYY=YYYYMMDD/10000
      MM=(YYYYMMDD-10000*YYYY)/100
      DD=YYYYMMDD-10000*YYYY-100*MM
      HH=HHMISS/10000
      MI=(HHMISS-10000*HH)/100
      SS=HHMISS-10000*HH-100*MI

      IF (YYYY.EQ.0) PAUSE 'There is no Year Zero.'
      IF (YYYY.LT.0) YYYY=YYYY+1
      IF (MM.GT.2) THEN
        JY=YYYY
        JM=MM+1
      ELSE
        JY=YYYY-1
        JM=MM+13
      ENDIF
      JULDAY=INT(365.25*JY)+INT(30.6001*JM)+DD+1720995
      IF (DD+31*(MM+12*YYYY).GE.IGREG) THEN
        JA=INT(0.01*JY)
        JULDAY=JULDAY+2-JA+INT(0.25*JA)
      ENDIF

      JULDATE=DBLE(FLOAT(JULDAY))+DBLE(FLOAT(HH)/24.)+
     +DBLE(FLOAT(MI)/1440.)+DBLE(FLOAT(SS)/86400.)

      RETURN
      END




      SUBROUTINE caldate(JULDATE,YYYYMMDD,HHMISS)
c                           i       o       o
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
*                                                                             *
*     Calculates the Gregorian date from the Julian date                      *
*                                                                             *
*     AUTHOR: Andreas Stohl (21 January 1994), adapted from Numerical Recipes *
*                                                                             *
*     Variables:                                                              *
*     DD             Day                                                      *
*     HH             Hour                                                     *
*     HHMISS         Hour, Minute, Second                                     *
*     JA,JB,JC,JD,JE help variables                                           *
*     JALPHA         help variable                                            *
*     JULDATE        Julian Date                                              *
*     JULDAY         help variable                                            *
*     MI             Minute                                                   *
*     MM             Month                                                    *
*     SS             Seconds                                                  *
*     YYYY           Year                                                     *
*     YYYYMMDD       Year, Month, Day                                         *
*                                                                             *
*     Constants:                                                              *
*     IGREG          help constant                                            *
*                                                                             *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 

      IMPLICIT NONE

      INTEGER YYYYMMDD,YYYY,MM,DD,HHMISS,HH,MI,SS
      INTEGER JULDAY,JA,JB,JC,JD,JE,IGREG,JALPHA
      DOUBLE PRECISION JULDATE
      PARAMETER (IGREG=2299161)

      JULDAY=INT(JULDATE)
      IF(JULDAY.GE.IGREG)THEN
        JALPHA=INT(((JULDAY-1867216)-0.25)/36524.25)
        JA=JULDAY+1+JALPHA-INT(0.25*JALPHA)
      ELSE
        JA=JULDAY
      ENDIF
      JB=JA+1524
      JC=INT(6680.+((JB-2439870)-122.1)/365.25)
      JD=365*JC+INT(0.25*JC)
      JE=INT((JB-JD)/30.6001)
      DD=JB-JD-INT(30.6001*JE)
      MM=JE-1
      IF (MM.GT.12) MM=MM-12
      YYYY=JC-4715
      IF (MM.GT.2) YYYY=YYYY-1
      IF (YYYY.LE.0) YYYY=YYYY-1

      YYYYMMDD=10000*YYYY+100*MM+DD
      HH=INT(24.*(JULDATE-FLOAT(JULDAY)))
      MI=INT(1440.*(JULDATE-FLOAT(JULDAY))-60.*FLOAT(HH))
      SS=NINT(86400.*(JULDATE-FLOAT(JULDAY))-3600.*FLOAT(HH))
     +-60.*FLOAT(MI)
      IF (SS.EQ.60) THEN  ! 60 seconds = 1 minute
        SS=0
        MI=MI+1
      ENDIF
      IF (MI.EQ.60) THEN
        MI=0
        HH=HH+1
      ENDIF
      HHMISS=10000*HH+100*MI+SS

      RETURN
      END
