      PROGRAM datelist

c     **********************************************************************
c     * Handling of date lists                                             *
c     * Michael Sprenger                                                   *
c     **********************************************************************

      implicit none

c     ---------------------------------------------------------------------
c     Declaration of variables
c     ---------------------------------------------------------------------

c     Parameters
      character*80              datefile
      character*80              mode 
      character*80              startdate
      character*80              finaldate
      integer                   interval

c     Date list
      integer                   ndates
      integer,allocatable,dimension(:)    :: year,month,day,hour

c     Auxiliary variables
      integer                     i
      integer                     year1,month1,day1,hour1,min1
      integer                     year2,month2,day2,hour2,min2
      character                   direction
      integer                     date1(5),date2(5)
      character*11                datestr
      character                   ch
      real                        diff

c     ---------------------------------------------------------------------
c     Preparations
c     ---------------------------------------------------------------------

c     Read parameter file
      open(10,file='datelist.param')

       read(10,*) datefile                           ! General parameters
       read(10,*) mode

       if ( mode.eq.'-create' ) then                 ! Create date list
          read(10,*) startdate
          read(10,*) finaldate
          read(10,*) interval

       else                                          ! Invalid mode
          print*,' ERROR: invalid mode for datelist'
          stop
       endif
       
      close(10)

c     ---------------------------------------------------------------------
c     Create a date list (-create)
c     ---------------------------------------------------------------------

      if ( mode.ne.'-create' ) goto 100

c     Check whether interval is ok
      if ( ( interval.le.0 ).or.(interval.gt.24) ) then
         print*,'Interval must be between 1 h and 24 h... Stop'
         stop
      endif

c     Extract dates and times
      read(startdate( 1: 4),*) year1
      read(startdate( 5: 6),*) month1
      read(startdate( 7: 8),*) day1
      read(startdate(10:11),*) hour1
      read(startdate(12:13),*) min1
      
      read(finaldate( 1: 4),*) year2
      read(finaldate( 5: 6),*) month2
      read(finaldate( 7: 8),*) day2
      read(finaldate(10:11),*) hour2
      read(startdate(12:13),*) min2

c     Get direction of the date file
      if (year2.gt.year1) then
         direction = 'f'
         goto 101
      elseif (year2.lt.year1) then
         direction = 'b'
         goto 101
      endif

      if (month2.gt.month1) then
         direction = 'f'
         goto 101
      elseif (month2.lt.month1) then
         direction = 'b'
         goto 101
      endif

      if (day2.gt.day1) then
         direction = 'f'
         goto 101
      elseif (day2.lt.day1) then
         direction = 'b'
         goto 101
      endif
      
      if (hour2.gt.hour1) then
         direction = 'f'
         goto 101
      elseif (hour2.lt.hour1) then
         direction = 'b'
         goto 101
      endif

      if (min2.gt.min1) then
         direction = 'f'
         goto 101
      elseif (min2.lt.min1) then
         direction = 'b'
         goto 101
      endif

      direction = 'f'

 101  continue

c     Set the interval step depending on the direction
      if ( direction.eq.'b' ) then
         interval = -interval
      endif

c     Save the dates in arrays
      date1(1) = year1
      date1(2) = month1
      date1(3) = day1
      date1(4) = hour1 
      date1(5) = 0

      date2(1) = year2
      date2(2) = month2
      date2(3) = day2
      date2(4) = hour2 
      date2(5) = 0


c     Get starting and ending date for the date list
      if ( direction.eq.'f' ) then

         do while ( mod(date1(4),interval) .ne. 0 )
            date1(4) = date1(4) - 1
         enddo

         if (min2.ne.0) call newdate(date2,1.,date2)

         do while ( mod(date2(4),interval) .ne. 0 )
            date2(4) = date2(4) + 1
         enddo

      else

         if (min1.ne.0) call newdate(date1,1.,date1)

         do while ( mod(date1(4),interval) .ne. 0 )
            date1(4) = date1(4) + 1
         enddo
       
         do while ( mod(date2(4),interval) .ne. 0 )
            date2(4) = date2(4) - 1
         enddo
         
      endif

c     Create and write the datefile
      if ( datefile.ne.'/dev/stdout') then
         open(10,file=datefile)
      endif

 102  continue

       call datestring(datestr,date1(1),date1(2),date1(3),date1(4) )

       if ( datefile.ne.'/dev/stdout') then
          write(10,*) datestr
       else
          write(*,*) datestr
       endif

       if ( ( date1(1).ne.date2(1) ).or.
     >      ( date1(2).ne.date2(2) ).or.
     >      ( date1(3).ne.date2(3) ).or.
     >      ( date1(4).ne.date2(4) ) ) 
     > then
          diff = real(interval)
          call newdate(date1,diff,date1)
          goto 102
       endif

      if ( datefile.ne.'/dev/stdout') then
         close(10)
      endif

 100  continue


c     ---------------------------------------------------------------------
c     End
c     ---------------------------------------------------------------------

      end
  
