Subversion Repositories lagranto.20cr

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
4 michaesp 1
date      PROGRAM datelist
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
21
      real                      randpercent
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)
33
      character*11                datestr,datestr1,datestr2
34
      character                   ch
35
      real                        diff
36
      real                        time
37
      character*80                datefile1,datefile2
38
      integer                     timestamp1,timestamp2
39
      integer                     oldstamp1 ,oldstamp2
40
      integer                     state
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
62
 
63
       elseif ( mode.eq.'-onlyin1' ) then            ! Dates only in file 1
64
          read(10,*) datefile1
65
          read(10,*) datefile2
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
97
      read(finaldate(12:13),*) min2
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
 
225
      if ( mode.ne.'-totime' ) goto 110
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
263
      write(*,'(i6)') nint(diff)
264
 
265
      goto 111
266
 
267
 110  continue
268
 
269
c     Close datefile
270
      close(10)
271
 
272
c     ---------------------------------------------------------------------
273
c     Convert times to a list of dates 
274
c     ---------------------------------------------------------------------
275
 
276
      if ( mode.ne.'-todate' ) goto 120
277
 
278
c     Extract reference date
279
      read(refdate( 1: 4),*) year1
280
      read(refdate( 5: 6),*) month1
281
      read(refdate( 7: 8),*) day1
282
      read(refdate(10:11),*) hour1
283
      read(refdate(12:13),*) min1
284
 
285
c     Loop through the date file
286
      open(10,file=datefile)
287
 
288
 121  read(10,*,end=120) time
289
 
290
c     Calculate the new date
291
      date1(1) = year1
292
      date1(2) = month1
293
      date1(3) = day1
294
      date1(4) = hour1 
295
      date1(5) = 0
296
      call newdate(date1,time,date2)
297
      call datestring(datestr,date2(1),date2(2),date2(3),date2(4) )
298
 
299
c     Write it to screen
300
      write(*,'(a11)') trim(datestr)
301
 
302
      goto 121
303
 
304
 120  continue
305
 
306
c     Close datefile
307
      close(10)
308
 
309
c     ---------------------------------------------------------------------
310
c     Extract all dates which are only in one datefile 
311
c     ---------------------------------------------------------------------
312
 
313
      if ( mode.ne.'-onlyin1' ) goto 130
314
 
315
c     Set reference date   
316
      date1(1) = 1979
317
      date1(2) = 1
318
      date1(3) = 1
319
      date1(4) = 0
320
      date1(5) = 0
321
 
322
c     Open the output file
323
      if ( datefile.ne.'/dev/stdout') then
324
         open(30,file=datefile)
325
      endif
326
 
327
c     Loop through the input date files
328
      open(10,file=datefile1)
329
      open(20,file=datefile2)
330
 
331
c     Loop through both date files
332
      state     = 0
333
      oldstamp1 = 0
334
      oldstamp2 = 0
335
 
336
 131  if ( (state.eq.1).or.(state.eq.0) ) then 
337
         read(10,*,end=133) datestr1
338
      endif
339
      if ( (state.eq.2).or.(state.eq.0) ) then 
340
         read(20,*,end=130) datestr2
341
      endif
342
 
343
c     Get time stamp for both date strings
344
      if ( (state.eq.1).or.(state.eq.0) ) then 
345
         read(datestr1( 1: 4),*) year2
346
         read(datestr1( 5: 6),*) month2
347
         read(datestr1( 7: 8),*) day2
348
         read(datestr1(10:11),*) hour2
349
         date2(1) = year2
350
         date2(2) = month2
351
         date2(3) = day2
352
         date2(4) = hour2 
353
         date2(5) = 0
354
         call timediff(date2,date1,diff)
355
         timestamp1 =  nint( diff )
356
         if ( timestamp1.lt.oldstamp1 ) then
357
            print*,' ERROR: datelist must be ordered ',trim(datefile1)
358
            stop
359
         else
360
            oldstamp1 = timestamp1
361
         endif
362
      endif
363
 
364
      if ( (state.eq.1).or.(state.eq.0) ) then 
365
         read(datestr2( 1: 4),*) year2
366
         read(datestr2( 5: 6),*) month2
367
         read(datestr2( 7: 8),*) day2
368
         read(datestr2(10:11),*) hour2
369
         date2(1) = year2
370
         date2(2) = month2
371
         date2(3) = day2
372
         date2(4) = hour2 
373
         date2(5) = 0
374
         call timediff(date2,date1,diff)
375
         timestamp2 =  nint( diff )
376
         if ( timestamp2.lt.oldstamp2 ) then
377
            print*,' ERROR: datelist must be ordered ',trim(datefile2)
378
            stop
379
         else
380
            oldstamp2 = timestamp2
381
         endif
382
      endif
383
 
384
c     Write output and set new state
385
      if ( timestamp1.gt.timestamp2 ) then
386
        state = 2
387
c        print*,trim(datestr1)//'.'//trim(datestr2)//' >'
388
 
389
      else if ( timestamp1.lt.timestamp2 ) then
390
        state = 1
391
 
392
        if ( datefile.ne.'/dev/stdout') then
393
           write(30,*) datestr1
394
        else
395
           write(*,*) datestr1
396
        endif
397
 
398
c        print*,trim(datestr1)//'.'//trim(datestr2)//' -> out'
399
 
400
      else if (timestamp1.eq.timestamp2 ) then
401
        state = 0
402
c        print*,trim(datestr1)//'.'//trim(datestr2)//' ='
403
 
404
      endif
405
 
406
      goto 131
407
 
408
c     Exit point for parallel reading through files
409
 130  continue
410
 
411
c     Write remaining part of datefile 1
412
 132  continue
413
        read(10,*,end=133) datestr1
414
        if ( datefile.ne.'/dev/stdout') then
415
           write(30,*) datestr1
416
        else
417
           write(*,*) datestr1
418
        endif
419
      goto 132
420
 133  continue
421
 
422
c     Close datefile
423
      close(10)
424
      close(20)
425
      close(30)
426
 
427
c     ---------------------------------------------------------------------
428
c     End
429
c     ---------------------------------------------------------------------
430
 
431
      end
432