libtime.f 18.2 KB
Newer Older
1
2
c this is <libtime.f>
cS
3
4
c $Id: libtime.f,v 1.3 2000-08-05 19:52:01 thof Exp $
c
5
6
7
8
9
10
11
12
13
14
15
16
17
c Thomas Forbriger (IfG Stuttgart) 1997
c
c a fortran library to manipulate absolute and relative data times
c
c----------------------------------------------------------------------
c
c REVISIONS and CHANGES
c   27/05/97   V1.0   first version
c   25/06/97   V1.01  changed write(1, *) error messages to print *,
c                     changed time_compare looking for date(1) instead
c                     of date(0)
c   13/08/97   V1.02  time_clear still did clear up to element 8
c   02/02/99   V1.03  added time_read
18
19
c   01/08/00   V1.04  in time_read: if month value OR year value is greater
c                     than zero expect absolute time
20
c   05/08/00          I'm about to split the whole thing into pieces
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
c
c----------------------------------------------------------------------
c 
c time format declaration
c
c   the absolute time value is stored in an integer array:
c
c   integer date(7)
c   date(1)=year
c   date(2)=day of year
c   date(3)=hour
c   date(4)=minute
c   date(5)=second
c   date(6)=millisecond
c   date(7)=microsecond
c 
c A year of value 0 will indicate that this is a relative time. This
c affects the routines time_norm and time_getdate and time_sprint.
c Notice that the routine time_fullyear will NOT be affected by this
c declaration and will set a year of value 0 to 2000.
c
c The best way to be aware of absolute/relative time confusion is
c to finish every manual setting of absolute times by a call to
c time_finish and to keep the year value fo relative times zero 
c in any case.
c
c Absolute times are only accepted from year 100 on. Users must expect
c some routines to set year values below 100 to the range of 1970 to 2069.
c 
c Only positive time values will be accepted. Therefore routines like
c time_sub will return only the absolute value of the difference.
c
c----------------------------------------------------------------------
c 
c subroutines and what they do
c
c time_libversion        returns library version                V1.0
c time_isleapyear        returns leapyear flag                  V1.0
c time_fullyear          returns full year value                V1.0
c time_setdoy            returns day of year                    V1.0
c time_getdate           get date from doy                      V1.0
c time_sprint            print time to character array          V1.0
c time_clear             clears a time record to zero           V1.0
c time_norm              set all field to correct value range   V1.0
c time_add               add two time records                   V1.0
c time_sub               calculate difference                   V1.0
c time_copy              copy a time record                     V1.0
c time_finish            finish setting of absolute time value  V1.0
c time_compare           compare two time values                V1.0
c time_mul               multiply relative time by integer n    V1.0
c time_div               divide relative time by integer n      V1.0
c time_nfit              find number of samples fiitng in time  V1.0
c time_read              extra time from a timestring           V1.03
c
c======================================================================
c
      real function time_libversion()
c
c returns the actual library version
c
cE
      real version
83
      parameter(version=1.04)
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
      time_libversion=version
      return
      end
cS
c----------------------------------------------------------------------
c
      logical function time_isleapyear(year)
c
c is true if year is a leap-year (else false)
c
      integer year
cE
      integer iyear
      logical result
c 
      iyear=year
      call time_fullyear(iyear)
      result=(((mod(iyear,4).eq.0).and.(mod(iyear,100).ne.0)).or.
     &     (mod(iyear,400).eq.0)) 
      time_isleapyear=result
      return
      end
cS
c----------------------------------------------------------------------
c
      subroutine time_fullyear(year)
c
c Makes year to be a full 4 digit year value. This is used to set
c the year to a meanigfull value. Notice that this routine does not
c make sense in combination with relative times.
c
      integer year
cE
      if (year.lt.70) year=year+2000
      if (year.lt.100) year=year+1900
      if (year.lt.1970) print *,
     &  'NOTICE (time_fullyear): spurious year value: ',year
      return
      end
cS
c----------------------------------------------------------------------
c
      subroutine time_setdoy(day, month, date)
c 
c Set doy in date from day and month (january first is doy 1).
c This routine will call time_fullyear first!
c 
c This routine is senseless in combination with relative times!
c
      integer day, month, date(7)
cE
      integer result
      integer sel, mon
      logical time_isleapyear
      integer days(12,2)
      data days/31,28,31,30,31,30,31,31,30,31,30,31,
     &          31,29,31,30,31,30,31,31,30,31,30,31/
c 
      if (date(1).eq.0) then
        print *,
     &    'WARNING (time_setdoy): do not use this with relative times'
        print *,
     &    'WARNING (time_setdoy): routine skipped...'
      else
        call time_fullyear(date(1))
        if (time_isleapyear(date(1))) then
          sel=2
        else
          sel=1
        endif
        result=0
        mon=1
    1   if (mon.ge.month) goto 2
          result=result+days(mon,sel)
          mon=mon+1
          if (mon.gt.13) stop 'ERROR (time_setdoy): month value out of range'
          goto 1
    2   continue
        result=result+day
        date(2)=result
      endif
      return
      end
cS
c----------------------------------------------------------------------
c 
      subroutine time_getdate(day, month, date)
c
c set day and month from given doy (day of year) in date
c
      integer date(7), day, month
cE
      integer days(12,2), d(7)
      integer sel
      logical time_isleapyear
      data days/31,28,31,30,31,30,31,31,30,31,30,31,
     &          31,29,31,30,31,30,31,31,30,31,30,31/
c 
      call time_copy(date, d)
      call time_norm(d)
      day=d(2)
c check for realtive date
      if (d(1).gt.0) then
        if (time_isleapyear(d(1))) then
          sel=2
        else
          sel=1
        endif
        month=1
    1   if (day.le.days(month,sel)) goto 2
          day=day-days(month,sel)
          month=month+1
          goto 1
    2   continue
      else
        month=0
      endif
      return
      end
cS
c----------------------------------------------------------------------
c 
      subroutine time_sprint(date, string)
c 
c print date to string (string must be 35 characters long at least)
c
      integer date(7)
      character*(*) string
cE
      character*(35) ostring
      integer i, day, month, d(7)
c 
      call time_copy(date, d)
      call time_norm(d)
c check for relative date
      if (d(1).gt.0) then
        call time_getdate(day, month, d)
        write(ostring, 1) d(2), day, month, d(1), (d(i), i=3,7)
    1   format(i3.3,1x,2(i2.2,1h/),i4.4,1x,i2.2,2(1h:,i2.2),1h.,2(i3.3))
      else
        if (d(2).eq.1) then
          write(ostring, 2) (d(i), i=2,7)
        else
          write(ostring, 3) (d(i), i=2,7)
        endif
    2   format(i5,' day ',i2.2,2(1h:,i2.2),1h.,2(i3.3))
    3   format(i5,' days ',i2.2,2(1h:,i2.2),1h.,2(i3.3))
      endif
      string=ostring
      return
      end
cS
c----------------------------------------------------------------------
c 
      subroutine time_clear(date)
c
c clear a time record
c
      integer date(7)
cE
      integer i
c
      do i=1,7
        date(i)=0
      enddo
      return
      end
cS
c----------------------------------------------------------------------
c 
      subroutine time_norm(date)
c
c set all fields to correct value range
c
      integer date(7)
cE
      integer carry, i, diy
      integer limit(7)
      logical time_isleapyear
      data limit/-1,-1,24,60,60,1000,1000/
c
c set linear value ranges (from hours on)
      do i=7,3,-1
        carry=int(date(i)/limit(i))
        if (date(i).lt.0) carry=carry-1
        date(i)=date(i)-carry*limit(i)
        date(i-1)=date(i-1)+carry
      enddo
c work on date if not relative
      if (date(1).gt.0) then
c set full 4 digit year
        call time_fullyear(date(1))
        if (date(2).gt.0) then
          if (time_isleapyear(date(1))) then
            diy=366
          else
            diy=365
          endif
    1     if (date(2).le.diy) goto 2
            date(2)=date(2)-diy
            date(1)=date(1)+1
            if (time_isleapyear(date(1))) then
              diy=366
            else
              diy=365
            endif
            goto 1
    2     continue
        else
    3     if (date(2).gt.0) goto 4
            date(1)=date(1)-1
            if (time_isleapyear(date(1))) then
              diy=366
            else
              diy=365
            endif
            date(2)=date(2)+diy
            goto 3
    4     continue
        endif
      endif
      return
      end
cS
c----------------------------------------------------------------------
c 
      subroutine time_add(date1, date2, date3)
c
c date3=date1+date2
c
c One of both should be a relative time: 
c   relative + absolute -> absolute
c   relative + relative -> relative
c   absolute + absolute -> senseless
c
      integer date1(7), date2(7), date3(7)
cE
      integer i
c
      if ((date1(1).ne.0).and.(date2(1).ne.0)) print *,
     &  'WARNING (time_add): adding two absolute times is senseless'
      call time_norm(date1)
      call time_norm(date2)
      do i=1,7
        date3(i)=date1(i)+date2(i)
      enddo
      call time_norm(date3)
      return
      end
cS
c----------------------------------------------------------------------
c 
      subroutine time_sub(date1, date2, date3)
c
c date3=date1-date2
c 
c This routine will only return the absolute value of the result.
c We do not handle signs. Therefore in any case the smaller time
c value will be subtracted from the larger one.
c
c relative - relative -> relative   case 1
c absolute - relative -> absolute   case 2
c absolute - absolute -> relative   case 3
c
      integer date1(7), date2(7), date3(7)
cE
      integer i, larger(7), smaller(7), case, time_compare
      logical time_isleapyear
c
      call time_norm(date1)
      call time_norm(date2)
c find out case and set larger and smaller
      if ((date1(1).eq.0).or.(date2(1).eq.0)) then
        if (date1(1).eq.date2(1)) then
          case=1
        else
          case=2
          if (date1(1).eq.0) then
            call time_copy(date2, larger)
            call time_copy(date1, smaller)
          else
            call time_copy(date1, larger)
            call time_copy(date2, smaller)
          endif
        endif
      else
        case=3
      endif
c 
      if (case.ne.2) then
        if (time_compare(date1, date2).gt.0) then
          call time_copy(date1, larger)
          call time_copy(date2, smaller)
        else
          call time_copy(date2, larger)
          call time_copy(date1, smaller)
        endif
      endif
c handle year boundaries of absolute times
      if (case.eq.3) then
    1   if (larger(1).eq.smaller(1)) goto 2        
          larger(1)=larger(1)-1
          larger(2)=larger(2)+365
          if (time_isleapyear(larger(1))) larger(2)=larger(2)+1
          goto 1
    2   continue
      endif
c build difference
      do i=1,7
        date3(i)=larger(i)-smaller(i)
      enddo
      call time_norm(date3)
      return
      end
cS
c----------------------------------------------------------------------
c 
      subroutine time_copy(date1, date2)
c
c copy: date1 --> date2
c
      integer date1(7), date2(7)
cE
      integer i
c
      do i=1,7
        date2(i)=date1(i)
      enddo
      return
      end
cS
c----------------------------------------------------------------------
c
      subroutine time_finish(date)
c
c Finish setting of absolute time value. This routine is senseless
c in combination with relative time values (year=0).
c
      integer date(7)
cE
      call time_fullyear(date(1))
      call time_norm(date)
      return
      end
cS
c----------------------------------------------------------------------
c
      integer function time_compare(date1, date2)
c
c Compare values of date1 and date2. Both must be absolute or
c both must be relative.
c
c result = 0   for   date1 = date2
c result = -1  for   date1 < date2
c result = 1   for   date1 > date2
c
      integer date1(7), date2(7)
cE
      integer result, i
c 
      if (((date1(1).eq.0).and.(date2(1).eq.0)).or.
     &    ((date1(1).ne.0).and.(date2(1).ne.0))) then
        result=0
        i=1
    1   if (date1(i).gt.date2(i)) then
          result=1
        elseif (date1(i).lt.date2(i)) then
          result=-1
        else
          i=i+1
          if (i.lt.8) goto 1
        endif
      else
        print *,
     &    'WARNING (time_compare): do not mix absolute and relative times'
        print *, 'WARNING (time_compare): routine skipped... (result=-2)'
        result=-2
      endif
      time_compare=result
      return
      end
cS
c----------------------------------------------------------------------
c 
      subroutine time_mul(date1, date2, n)
c
c Multiply relative time date1 with n and store in date2.
c
      integer date1(7), date2(7), n
cE
      integer quotient(7), rest(7), product(7), carry(7), i, limit(7), help
      data limit/-1,-1,24,60,60,1000,1000/
c
      if (date1(1).ne.0) then
        stop 'ERROR (time_mul): no absolute time allowed'
      else
        do i=3,7
c split n to avoid conflicts with integer range
          quotient(i)=int(n/limit(i))
          rest(i)=n-limit(i)*quotient(i)
          help=int(rest(i)*date1(i)/limit(i))
          carry(i)=quotient(i)*date1(i)+help
          product(i)=rest(i)*date1(i)-help*limit(i)
        enddo
        product(2)=n*date1(2)
        do i=2,6
          date2(i)=product(i)+carry(i+1)
        enddo
        date2(7)=product(7)
        date2(1)=0
        call time_norm(date2)
      endif
      return
      end
cS
c----------------------------------------------------------------------
c 
      subroutine time_div(date1, date2, n, rest)
c
c Divide relative time date1 by integer n and store result in date2.
c
c rest gives the none dividable rest in microseconds.
c
      integer date1(7), date2(7), n, rest
cE
      integer carry, limit(7), help, i
      data limit/-1,-1,24,60,60,1000,1000/
c
      if (date1(1).ne.0) then
        stop 'ERROR (time_div): no absolute time allowed'
      else
        carry=0
        do i=2,7
          help=date1(i)+carry*limit(i)
          date2(i)=int(help/n)
          carry=help-n*date2(i)
        enddo
        rest=carry
        date2(1)=0
        call time_norm(date2)
      endif
      return
      end
cS
c----------------------------------------------------------------------
c
      subroutine time_nfit(date1, date2, n, full)
c
c Evaluate how many (n) samples of length date2 fit into the
c time span date1 at best. 
c 
c full=n*date2
c
c The difference abs(n*date2-full) will be less or equal
c the half of date2.
c
      integer date1(7), date2(7), n, full(7)
cE
      double precision d1, d2
      integer limit(7), i, time_compare, c, dif(7), dhalf(7), rest
      data limit/-1,-1,24,60,60,1000,1000/
c
      if ((date1(1).ne.0).or.(date2(1).ne.0)) then
        stop 'ERROR (time_nfit): no absolute times allowed'
      else
c for our convenience we will first use floating point arithmetics
c for an estimation
        d1=float(date1(2))
        d2=float(date2(2))
        do i=3,7
          d1=d1*float(limit(i))+float(date1(i))
          d2=d2*float(limit(i))+float(date2(i))
        enddo
        n=int(d1/d2)
c get half of sampling interval
        call time_div(date2, dhalf, 2, rest)
        dhalf(7)=dhalf(7)+rest
        call time_norm(dhalf)
c now go and fit result stepwise
    1   call time_mul(date2, full, n)
        c=time_compare(date1, full)
        call time_sub(date1, full, dif)
c next step if difference exceeds half of sampling rate
        if (time_compare(dif, dhalf).gt.0) then
          if (c.gt.0) then
            n=n+1
          else
            n=n-1
          endif
          goto 1
        endif
      endif
      return
      end
c
cS
c----------------------------------------------------------------------
c
      subroutine time_read(string,date)
c
c Reads string and fills date.
c
c The timestring has to provide time information in the order
c 
c   year month day hours minutes seconds
c 
c where seconds may be given as a floating point number. All items must be
c separated by non-numerical characters. 
c
      character*(*) string
      integer date(7)
c 
cE
      integer numbers(5), i, first, last, length, index
      double precision seconds
c 
      call time_clear(date)
c fill array with numbers
      length=len(string)
      first=1
      do i=1,5
        numbers(i)=0
        do while ((first.lt.length).and.
     &      (index('.0123456789', string(first:first)).eq.0))
          first=first+1
        enddo
        last=first
        do while ((last.lt.length).and.
     &      (index('.0123456789', string(last:last)).gt.0))
          last=last+1
        enddo
        if (index('0123456789', string(last:last)).lt.1) last=last-1
        if ((first.le.length).and.(index('0123456789',
     &      string(first:first)).gt.0)) then
          read(string(first:last), *) numbers(i)
          first=last+1
        endif
      enddo
c read seconds
      seconds=0.d0
      do while ((first.lt.length).and.
     &    (index('.0123456789', string(first:first)).eq.0))
        first=first+1
      enddo
      last=first
      do while ((last.lt.length).and.
     &    (index('.0123456789', string(last:last)).gt.0))
        last=last+1
      enddo
      read(string(first:last), *) 
      if ((first.le.length).and.(index('0123456789',
     &    string(first:first)).gt.0)) then
        read(string(first:last), *) seconds
        first=last+1
      endif
c 
640
      if ((numbers(1).gt.0).or.(numbers(2).gt.0)) then
641
642
c absolute date
        date(1)=numbers(1)
643
        call time_fullyear(date(1))
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
        call time_setdoy(numbers(3),numbers(2),date)
      else
c relative date
        date(2)=numbers(3)
      endif
c hour and minute 
      do i=1,2
        date(i+2)=numbers(i+3)
      enddo
c seconds, etc.
      date(5)=int(seconds)
      seconds=(seconds-dble(date(5)))*1000.d0
      date(6)=int(seconds)
      seconds=(seconds-dble(date(6)))*1000.d0
      date(7)=int(seconds+0.5d0)
c 
      call time_norm(date)
c 
      return
      end
c
c ------ END OF libtime.f ------