Subversion Repositories lagranto.ecmwf

Rev

Rev 5 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
9 michaesp 1
date      PROGRAM datelist
3 michaesp 2
 
3
c     **********************************************************************
4
c     * Handling of date lists                                             *
5
c     * Michael Sprenger                                                   *
6
c     **********************************************************************
7
 
8
      implicit none
9
 
10
c     ---------------------------------------------------------------------
11
c     Declaration of variables
12
c     ---------------------------------------------------------------------
13
 
14
c     Parameters
15
      character*80              datefile
16
      character*80              mode 
17
      character*80              startdate
18
      character*80              finaldate
19
      character*80              refdate
20
      integer                   interval
9 michaesp 21
      real                      randpercent
3 michaesp 22
 
23
c     Date list
24
      integer                   ndates
25
      integer,allocatable,dimension(:)    :: year,month,day,hour
26
 
27
c     Auxiliary variables
28
      integer                     i
29
      integer                     year1,month1,day1,hour1,min1
30
      integer                     year2,month2,day2,hour2,min2
31
      character                   direction
32
      integer                     date1(5),date2(5)
9 michaesp 33
      character*11                datestr,datestr1,datestr2
3 michaesp 34
      character                   ch
35
      real                        diff
36
      real                        time
9 michaesp 37
      character*80                datefile1,datefile2
38
      integer                     timestamp1,timestamp2
39
      integer                     oldstamp1 ,oldstamp2
40
      integer                     state
3 michaesp 41
 
42
c     ---------------------------------------------------------------------
43
c     Preparations
44
c     ---------------------------------------------------------------------
45
 
46
c     Read parameter file
47
      open(10,file='datelist.param')
48
 
49
       read(10,*) datefile                           ! General parameters
50
       read(10,*) mode
51
 
52
       if ( mode.eq.'-create' ) then                 ! Create date list
53
          read(10,*) startdate
54
          read(10,*) finaldate
55
          read(10,*) interval
56
 
57
       elseif ( mode.eq.'-totime' ) then             ! Convert to time list
58
          read(10,*) refdate
59
 
60
       elseif ( mode.eq.'-todate' ) then             ! Convert to date list
61
          read(10,*) refdate
9 michaesp 62
 
63
       elseif ( mode.eq.'-onlyin1' ) then            ! Dates only in file 1
64
          read(10,*) datefile1
65
          read(10,*) datefile2
3 michaesp 66
 
67
       else                                          ! Invalid mode
68
          print*,' ERROR: invalid mode for datelist'
69
          stop
70
       endif
71
 
72
      close(10)
73
 
74
c     ---------------------------------------------------------------------
75
c     Create a date list (-create)
76
c     ---------------------------------------------------------------------
77
 
78
      if ( mode.ne.'-create' ) goto 100
79
 
80
c     Check whether interval is ok
81
      if ( ( interval.le.0 ).or.(interval.gt.24) ) then
82
         print*,'Interval must be between 1 h and 24 h... Stop'
83
         stop
84
      endif
85
 
86
c     Extract dates and times
87
      read(startdate( 1: 4),*) year1
88
      read(startdate( 5: 6),*) month1
89
      read(startdate( 7: 8),*) day1
90
      read(startdate(10:11),*) hour1
91
      read(startdate(12:13),*) min1
92
 
93
      read(finaldate( 1: 4),*) year2
94
      read(finaldate( 5: 6),*) month2
95
      read(finaldate( 7: 8),*) day2
96
      read(finaldate(10:11),*) hour2
9 michaesp 97
      read(finaldate(12:13),*) min2
3 michaesp 98
 
99
c     Get direction of the date file
100
      if (year2.gt.year1) then
101
         direction = 'f'
102
         goto 101
103
      elseif (year2.lt.year1) then
104
         direction = 'b'
105
         goto 101
106
      endif
107
 
108
      if (month2.gt.month1) then
109
         direction = 'f'
110
         goto 101
111
      elseif (month2.lt.month1) then
112
         direction = 'b'
113
         goto 101
114
      endif
115
 
116
      if (day2.gt.day1) then
117
         direction = 'f'
118
         goto 101
119
      elseif (day2.lt.day1) then
120
         direction = 'b'
121
         goto 101
122
      endif
123
 
124
      if (hour2.gt.hour1) then
125
         direction = 'f'
126
         goto 101
127
      elseif (hour2.lt.hour1) then
128
         direction = 'b'
129
         goto 101
130
      endif
131
 
132
      if (min2.gt.min1) then
133
         direction = 'f'
134
         goto 101
135
      elseif (min2.lt.min1) then
136
         direction = 'b'
137
         goto 101
138
      endif
139
 
140
      direction = 'f'
141
 
142
 101  continue
143
 
144
c     Set the interval step depending on the direction
145
      if ( direction.eq.'b' ) then
146
         interval = -interval
147
      endif
148
 
149
c     Save the dates in arrays
150
      date1(1) = year1
151
      date1(2) = month1
152
      date1(3) = day1
153
      date1(4) = hour1 
154
      date1(5) = 0
155
 
156
      date2(1) = year2
157
      date2(2) = month2
158
      date2(3) = day2
159
      date2(4) = hour2 
160
      date2(5) = 0
161
 
162
 
163
c     Get starting and ending date for the date list
164
      if ( direction.eq.'f' ) then
165
 
166
         do while ( mod(date1(4),interval) .ne. 0 )
167
            date1(4) = date1(4) - 1
168
         enddo
169
 
170
         if (min2.ne.0) call newdate(date2,1.,date2)
171
 
172
         do while ( mod(date2(4),interval) .ne. 0 )
173
            date2(4) = date2(4) + 1
174
         enddo
175
 
176
      else
177
 
178
         if (min1.ne.0) call newdate(date1,1.,date1)
179
 
180
         do while ( mod(date1(4),interval) .ne. 0 )
181
            date1(4) = date1(4) + 1
182
         enddo
183
 
184
         do while ( mod(date2(4),interval) .ne. 0 )
185
            date2(4) = date2(4) - 1
186
         enddo
187
 
188
      endif
189
 
190
c     Create and write the datefile
191
      if ( datefile.ne.'/dev/stdout') then
192
         open(10,file=datefile)
193
      endif
194
 
195
 102  continue
196
 
197
       call datestring(datestr,date1(1),date1(2),date1(3),date1(4) )
198
 
199
       if ( datefile.ne.'/dev/stdout') then
200
          write(10,*) datestr
201
       else
202
          write(*,*) datestr
203
       endif
204
 
205
       if ( ( date1(1).ne.date2(1) ).or.
206
     >      ( date1(2).ne.date2(2) ).or.
207
     >      ( date1(3).ne.date2(3) ).or.
208
     >      ( date1(4).ne.date2(4) ) ) 
209
     > then
210
          diff = real(interval)
211
          call newdate(date1,diff,date1)
212
          goto 102
213
       endif
214
 
215
      if ( datefile.ne.'/dev/stdout') then
216
         close(10)
217
      endif
218
 
219
 100  continue
220
 
221
c     ---------------------------------------------------------------------
222
c     Convert dates to a list of times
223
c     ---------------------------------------------------------------------
224
 
9 michaesp 225
      if ( mode.ne.'-totime' ) goto 112
3 michaesp 226
 
227
c     Extract reference date
228
      read(refdate( 1: 4),*) year1
229
      read(refdate( 5: 6),*) month1
230
      read(refdate( 7: 8),*) day1
231
      read(refdate(10:11),*) hour1
232
      read(refdate(12:13),*) min1
233
 
234
c     Loop through the date file
235
      open(10,file=datefile)
236
 
237
 111  read(10,*,end=110) datestr
238
 
239
c     Extract date
240
      read(datestr( 1: 4),*) year2
241
      read(datestr( 5: 6),*) month2
242
      read(datestr( 7: 8),*) day2
243
      read(datestr(10:11),*) hour2
244
      min1 = 0
245
 
246
c     Get the time difference
247
      date1(1) = year1
248
      date1(2) = month1
249
      date1(3) = day1
250
      date1(4) = hour1 
251
      date1(5) = 0
252
 
253
      date2(1) = year2
254
      date2(2) = month2
255
      date2(3) = day2
256
      date2(4) = hour2 
257
      date2(5) = 0
258
 
259
      call timediff(date2,date1,diff)
260
 
261
 
262
c     Write it to screen
5 michaesp 263
      write(*,'(i6)') nint(diff)
3 michaesp 264
 
265
      goto 111
266
 
267
 110  continue
268
 
269
c     Close datefile
270
      close(10)
9 michaesp 271
 
272
c     Exit point      
273
  112 continue
3 michaesp 274
 
275
c     ---------------------------------------------------------------------
276
c     Convert times to a list of dates 
277
c     ---------------------------------------------------------------------
278
 
9 michaesp 279
      if ( mode.ne.'-todate' ) goto 122
3 michaesp 280
 
281
c     Extract reference date
282
      read(refdate( 1: 4),*) year1
283
      read(refdate( 5: 6),*) month1
284
      read(refdate( 7: 8),*) day1
285
      read(refdate(10:11),*) hour1
286
      read(refdate(12:13),*) min1
287
 
288
c     Loop through the date file
289
      open(10,file=datefile)
290
 
291
 121  read(10,*,end=120) time
292
 
293
c     Calculate the new date
294
      date1(1) = year1
295
      date1(2) = month1
296
      date1(3) = day1
297
      date1(4) = hour1 
298
      date1(5) = 0
299
      call newdate(date1,time,date2)
300
      call datestring(datestr,date2(1),date2(2),date2(3),date2(4) )
301
 
302
c     Write it to screen
5 michaesp 303
      write(*,'(a11)') trim(datestr)
3 michaesp 304
 
305
      goto 121
306
 
307
 120  continue
308
 
309
c     Close datefile
310
      close(10)
9 michaesp 311
 
312
c     Exit point      
313
 122  continue
314
 
315
c     ---------------------------------------------------------------------
316
c     Extract all dates which are only in one datefile 
317
c     ---------------------------------------------------------------------
3 michaesp 318
 
9 michaesp 319
      if ( mode.ne.'-onlyin1' ) goto 134
320
 
321
c     Set reference date   
322
      date1(1) = 1979
323
      date1(2) = 1
324
      date1(3) = 1
325
      date1(4) = 0
326
      date1(5) = 0
327
 
328
c     Open the output file
329
      if ( datefile.ne.'/dev/stdout') then
330
         open(30,file=datefile)
331
      endif
332
 
333
c     Loop through the input date files
334
      open(10,file=datefile1)
335
      open(20,file=datefile2)
336
 
337
c     Loop through both date files
338
      state     = 0
339
      oldstamp1 = 0
340
      oldstamp2 = 0
341
 
342
 131  if ( (state.eq.1).or.(state.eq.0) ) then 
343
         read(10,*,end=133) datestr1
344
      endif
345
      if ( (state.eq.2).or.(state.eq.0) ) then 
346
         read(20,*,end=130) datestr2
347
      endif
348
 
349
c     Get time stamp for both date strings
350
      if ( (state.eq.1).or.(state.eq.0) ) then 
351
         read(datestr1( 1: 4),*) year2
352
         read(datestr1( 5: 6),*) month2
353
         read(datestr1( 7: 8),*) day2
354
         read(datestr1(10:11),*) hour2
355
         date2(1) = year2
356
         date2(2) = month2
357
         date2(3) = day2
358
         date2(4) = hour2 
359
         date2(5) = 0
360
         call timediff(date2,date1,diff)
361
         timestamp1 =  nint( diff )
362
         if ( timestamp1.lt.oldstamp1 ) then
363
            print*,' ERROR: datelist must be ordered ',trim(datefile1)
364
            stop
365
         else
366
            oldstamp1 = timestamp1
367
         endif
368
      endif
369
 
370
      if ( (state.eq.1).or.(state.eq.0) ) then 
371
         read(datestr2( 1: 4),*) year2
372
         read(datestr2( 5: 6),*) month2
373
         read(datestr2( 7: 8),*) day2
374
         read(datestr2(10:11),*) hour2
375
         date2(1) = year2
376
         date2(2) = month2
377
         date2(3) = day2
378
         date2(4) = hour2 
379
         date2(5) = 0
380
         call timediff(date2,date1,diff)
381
         timestamp2 =  nint( diff )
382
         if ( timestamp2.lt.oldstamp2 ) then
383
            print*,' ERROR: datelist must be ordered ',trim(datefile2)
384
            stop
385
         else
386
            oldstamp2 = timestamp2
387
         endif
388
      endif
389
 
390
c     Write output and set new state
391
      if ( timestamp1.gt.timestamp2 ) then
392
        state = 2
393
c        print*,trim(datestr1)//'.'//trim(datestr2)//' >'
394
 
395
      else if ( timestamp1.lt.timestamp2 ) then
396
        state = 1
397
 
398
        if ( datefile.ne.'/dev/stdout') then
399
           write(30,*) datestr1
400
        else
401
           write(*,*) datestr1
402
        endif
403
 
404
c        print*,trim(datestr1)//'.'//trim(datestr2)//' -> out'
405
 
406
      else if (timestamp1.eq.timestamp2 ) then
407
        state = 0
408
c        print*,trim(datestr1)//'.'//trim(datestr2)//' ='
409
 
410
      endif
411
 
412
      goto 131
413
 
414
c     Exit point for parallel reading through files
415
 130  continue
416
 
417
c     Write remaining part of datefile 1
418
 132  continue
419
        read(10,*,end=133) datestr1
420
        if ( datefile.ne.'/dev/stdout') then
421
           write(30,*) datestr1
422
        else
423
           write(*,*) datestr1
424
        endif
425
      goto 132
426
 133  continue
427
 
428
c     Close datefile
429
      close(10)
430
      close(20)
431
      close(30)
432
 
433
c     Exit point      
434
 134  continue
435
 
3 michaesp 436
c     ---------------------------------------------------------------------
437
c     End
438
c     ---------------------------------------------------------------------
439
 
440
      end
441