Subversion Repositories lagranto.ecmwf

Rev

Rev 3 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3 Rev 29
1
c     **************************************************************************
1
c     **************************************************************************
2
c     * This library provides subroutines related to time and file name        *
2
c     * This library provides subroutines related to time and file name        *
3
c     * handling                                                               *
3
c     * handling                                                               *
4
c     *                                                                        *
4
c     *                                                                        *
5
c     **************************************************************************
5
c     **************************************************************************
6
 
6
 
7
c     ---------------------------------------------------------------------------
7
c     ---------------------------------------------------------------------------
8
c     Concatenate a date string
8
c     Concatenate a date string
9
c     ---------------------------------------------------------------------------
9
c     ---------------------------------------------------------------------------
10
 
10
 
11
      subroutine datestring(datestr,yyyy,mm,dd,hh)
11
      subroutine datestring(datestr,yyyy,mm,dd,hh)
12
 
12
 
13
c     Declaration of subroutine parameters
13
c     Declaration of subroutine parameters
14
      integer yyyy,mm,dd,hh
14
      integer yyyy,mm,dd,hh
15
      character*20 datestr
15
      character*20 datestr
16
 
16
 
17
c     Auxiliary parameters
17
c     Auxiliary parameters
18
      integer f1,f2,i0
18
      integer f1,f2,i0
19
      integer yy,ce
19
      integer yy,ce
20
 
20
 
21
      i0=ichar('0')
21
      i0=ichar('0')
22
      datestr=''
22
      datestr=''
23
 
23
 
24
      yy=mod(yyyy,100)
24
      yy=mod(yyyy,100)
25
      ce=int(yyyy/100)
25
      ce=int(yyyy/100)
26
 
26
 
27
      if ((ce.ne.19).and.(ce.ne.20)) then
27
      if ((ce.ne.19).and.(ce.ne.20)) then
28
         print*,'Invalid year... Stop'
28
         print*,'Invalid year... Stop'
29
         stop
29
         stop
30
      endif
30
      endif
31
 
31
 
32
      if (yy.ge.0) then
32
      if (yy.ge.0) then
33
         f1=yy/10
33
         f1=yy/10
34
         f2=mod(yy,10)
34
         f2=mod(yy,10)
35
         if (ce.eq.19) then
35
         if (ce.eq.19) then
36
            datestr=trim(datestr)//'19'//char(f1+i0)//char(f2+i0)
36
            datestr=trim(datestr)//'19'//char(f1+i0)//char(f2+i0)
37
         else if (ce.eq.20) then
37
         else if (ce.eq.20) then
38
            datestr=trim(datestr)//'20'//char(f1+i0)//char(f2+i0)
38
            datestr=trim(datestr)//'20'//char(f1+i0)//char(f2+i0)
39
         endif
39
         endif
40
      endif
40
      endif
41
      if (mm.gt.0) then
41
      if (mm.gt.0) then
42
         f1=mm/10
42
         f1=mm/10
43
         f2=mod(mm,10)
43
         f2=mod(mm,10)
44
         datestr=trim(datestr)//char(f1+i0)//char(f2+i0)
44
         datestr=trim(datestr)//char(f1+i0)//char(f2+i0)
45
      endif
45
      endif
46
 
46
 
47
      if (dd.gt.0) then
47
      if (dd.gt.0) then
48
         f1=dd/10
48
         f1=dd/10
49
         f2=mod(dd,10)
49
         f2=mod(dd,10)
50
         datestr=trim(datestr)//char(f1+i0)//char(f2+i0)
50
         datestr=trim(datestr)//char(f1+i0)//char(f2+i0)
51
      endif
51
      endif
52
 
52
 
53
      if (hh.ge.0) then
53
      if (hh.ge.0) then
54
         f1=hh/10
54
         f1=hh/10
55
         f2=mod(hh,10)
55
         f2=mod(hh,10)
56
         datestr=trim(datestr)//'_'//char(f1+i0)//char(f2+i0)
56
         datestr=trim(datestr)//'_'//char(f1+i0)//char(f2+i0)
57
      endif
57
      endif
58
 
58
 
59
      return
59
      return
60
      end
60
      end
61
 
61
 
62
 
62
 
63
c     ---------------------------------------------------------------------------
63
c     ---------------------------------------------------------------------------
64
c     Calculates the new date when diff (in hours) is added to date1.
64
c     Calculates the new date when diff (in hours) is added to date1.
65
c     ---------------------------------------------------------------------------
65
c     ---------------------------------------------------------------------------
66
 
66
 
67
      subroutine newdate(date1,diff,date2)
67
      subroutine newdate(date1,diff,date2)
68
C
68
C
69
C     date1	int	input	array contains a date in the form
69
C     date1	int	input	array contains a date in the form
70
C				year,month,day,hour,step
70
C				year,month,day,hour,step
71
C     diff	real	input	timestep in hours to go from date1
71
C     diff	real	input	timestep in hours to go from date1
72
C     date2	int	output	array contains new date in the same form
72
C     date2	int	output	array contains new date in the same form
73
 
73
 
74
      integer   date1(5),date2(5)
74
      integer   date1(5),date2(5)
75
      integer   idays(12)       ! array containing the days of the monthes
75
      integer   idays(12)       ! array containing the days of the monthes
76
      real	diff
76
      real	diff
77
      logical	yearchange
77
      logical	yearchange
78
 
78
 
79
      data idays/31,28,31,30,31,30,31,31,30,31,30,31/
79
      data idays/31,28,31,30,31,30,31,31,30,31,30,31/
80
 
80
 
81
      yearchange=.false.
81
      yearchange=.false.
82
 
82
 
83
      if ((mod(date1(1),4).eq.0).and.(date1(2).le.2)) idays(2)=29
83
      if ((mod(date1(1),4).eq.0).and.(date1(2).le.2)) idays(2)=29
84
 
84
 
85
      date2(1)=date1(1)
85
      date2(1)=date1(1)
86
      date2(2)=date1(2)
86
      date2(2)=date1(2)
87
      date2(3)=date1(3)
87
      date2(3)=date1(3)
88
      date2(4)=date1(4)
88
      date2(4)=date1(4)
89
      date2(5)=0
89
      date2(5)=0
90
      date2(4)=date1(4)+int(diff)+date1(5)
90
      date2(4)=date1(4)+int(diff)+date1(5)
91
 
91
 
92
      if (date2(4).ge.24) then
92
      if (date2(4).ge.24) then
93
        date2(3)=date2(3)+int(date2(4)/24)
93
        date2(3)=date2(3)+int(date2(4)/24)
94
        date2(4)=date2(4)-int(date2(4)/24)*24
94
        date2(4)=date2(4)-int(date2(4)/24)*24
95
      endif
95
      endif
96
      if (date2(4).lt.0) then
96
      if (date2(4).lt.0) then
97
        if (mod(date2(4),24).eq.0) then
97
        if (mod(date2(4),24).eq.0) then
98
          date2(3)=date2(3)-int(abs(date2(4))/24)
98
          date2(3)=date2(3)-int(abs(date2(4))/24)
99
          date2(4)=date2(4)+int(abs(date2(4))/24)*24
99
          date2(4)=date2(4)+int(abs(date2(4))/24)*24
100
        else
100
        else
101
          date2(3)=date2(3)-(1+int(abs(date2(4))/24))
101
          date2(3)=date2(3)-(1+int(abs(date2(4))/24))
102
          date2(4)=date2(4)+(1+int(abs(date2(4))/24))*24
102
          date2(4)=date2(4)+(1+int(abs(date2(4))/24))*24
103
        endif
103
        endif
104
      endif
104
      endif
105
 
105
 
106
  100 if (date2(3).gt.idays(date2(2))) then
106
  100 if (date2(3).gt.idays(date2(2))) then
107
        if ((date2(2).eq.2).and.(mod(date2(1),4).eq.0)) idays(2)=29
107
        if ((date2(2).eq.2).and.(mod(date2(1),4).eq.0)) idays(2)=29
108
        date2(3)=date2(3)-idays(date2(2))
108
        date2(3)=date2(3)-idays(date2(2))
109
        if (idays(2).eq.29) idays(2)=28
109
        if (idays(2).eq.29) idays(2)=28
110
        date2(2)=date2(2)+1
110
        date2(2)=date2(2)+1
111
        if (date2(2).gt.12) then
111
        if (date2(2).gt.12) then
112
*         date2(1)=date2(1)+int(date2(2)/12)
112
*         date2(1)=date2(1)+int(date2(2)/12)
113
*         date2(2)=date2(2)-int(date2(2)/12)*12
113
*         date2(2)=date2(2)-int(date2(2)/12)*12
114
          date2(1)=date2(1)+1
114
          date2(1)=date2(1)+1
115
          date2(2)=date2(2)-12
115
          date2(2)=date2(2)-12
116
        endif
116
        endif
117
        if (date2(2).lt.1) then
117
        if (date2(2).lt.1) then
118
          date2(1)=date2(1)-(1+int(abs(date2(2)/12)))
118
          date2(1)=date2(1)-(1+int(abs(date2(2)/12)))
119
          date2(2)=date2(2)+(1+int(abs(date2(2)/12)))*12
119
          date2(2)=date2(2)+(1+int(abs(date2(2)/12)))*12
120
        endif
120
        endif
121
        goto 100
121
        goto 100
122
      endif     
122
      endif     
123
  200 if (date2(3).lt.1) then
123
  200 if (date2(3).lt.1) then
124
        date2(2)=date2(2)-1
124
        date2(2)=date2(2)-1
125
        if (date2(2).gt.12) then
125
        if (date2(2).gt.12) then
126
          date2(1)=date2(1)+int(date2(2)/12)
126
          date2(1)=date2(1)+int(date2(2)/12)
127
          date2(2)=date2(2)-int(date2(2)/12)*12
127
          date2(2)=date2(2)-int(date2(2)/12)*12
128
        endif
128
        endif
129
        if (date2(2).lt.1) then
129
        if (date2(2).lt.1) then
130
          date2(1)=date2(1)-(1+int(abs(date2(2)/12)))
130
          date2(1)=date2(1)-(1+int(abs(date2(2)/12)))
131
          date2(2)=date2(2)+(1+int(abs(date2(2)/12)))*12
131
          date2(2)=date2(2)+(1+int(abs(date2(2)/12)))*12
132
        endif
132
        endif
133
        if ((date2(2).eq.2).and.(mod(date2(1),4).eq.0)) idays(2)=29
133
        if ((date2(2).eq.2).and.(mod(date2(1),4).eq.0)) idays(2)=29
134
        date2(3)=date2(3)+idays(date2(2))
134
        date2(3)=date2(3)+idays(date2(2))
135
        if (idays(2).eq.29) idays(2)=28
135
        if (idays(2).eq.29) idays(2)=28
136
        goto 200
136
        goto 200
137
      endif
137
      endif
138
 
138
 
139
      if (date2(2).gt.12) then
139
      if (date2(2).gt.12) then
140
        date2(1)=date2(1)+int(date2(2)/12)
140
        date2(1)=date2(1)+int(date2(2)/12)
141
        date2(2)=date2(2)-int(date2(2)/12)*12
141
        date2(2)=date2(2)-int(date2(2)/12)*12
142
      endif
142
      endif
143
      if (date2(2).lt.1) then
143
      if (date2(2).lt.1) then
144
        date2(1)=date2(1)-(1+int(abs(date2(2)/12)))
144
        date2(1)=date2(1)-(1+int(abs(date2(2)/12)))
145
        date2(2)=date2(2)+(1+int(abs(date2(2)/12)))*12
145
        date2(2)=date2(2)+(1+int(abs(date2(2)/12)))*12
146
      endif
146
      endif
147
 
147
 
148
      if (date2(1).lt.1000) then
148
      if (date2(1).lt.1000) then
149
      if (date2(1).ge.100) date2(1)=date2(1)-100
149
      if (date2(1).ge.100) date2(1)=date2(1)-100
150
      endif
150
      endif
151
 
151
 
152
      return
152
      return
153
      end
153
      end
154
 
154
 
155
c     ---------------------------------------------------------------------------
155
c     ---------------------------------------------------------------------------
156
c     Convert <hh.mm> -> <frac> and <frac> -> <hhmm>
156
c     Convert <hh.mm> -> <frac> and <frac> -> <hhmm>
157
c     ---------------------------------------------------------------------------
157
c     ---------------------------------------------------------------------------
158
 
158
 
159
      subroutine hhmm2frac (hhmm,frac)
159
      subroutine hhmm2frac (hhmm,frac)
160
 
160
 
-
 
161
      implicit none
-
 
162
 
161
      real hhmm
163
      real hhmm
162
      real frac
164
      real frac
163
      
165
      
164
      frac=real(int(hhmm))+
166
      frac=real(int(hhmm))+
165
     >     100.*(hhmm-real(int(hhmm)))/60.
167
     >     100.*(hhmm-real(int(hhmm)))/60.
166
 
168
 
167
      end
169
      end
168
 
170
 
169
 
171
 
170
      subroutine frac2hhmm (frac,hhmm)
172
      subroutine frac2hhmm (frac,hhmm)
171
 
173
 
-
 
174
      implicit none
-
 
175
 
172
      real hhmm
176
      real hhmm
173
      real frac
177
      real frac
174
 
178
 
175
      real hh,mm
179
      real hh,mm
-
 
180
      integer changesign
-
 
181
 
-
 
182
      real eps
-
 
183
      parameter (eps=0.00001)
-
 
184
 
-
 
185
      changesign = 0.
-
 
186
      if ( frac.lt.0. ) changesign = 1
-
 
187
 
-
 
188
      if ( changesign.eq.1 ) frac = - frac
176
      
189
      
177
      hh =  real(int(frac))
190
      hh =  real( int(frac+eps) )
178
      mm =  60. * (frac-real(int(frac)))
191
      mm =  real( nint( 60. * (frac-hh) ) )
179
 
192
 
180
      hhmm = hh + 0.01 * mm
193
      hhmm = hh + 0.01 * mm
181
 
194
 
-
 
195
      if ( changesign.eq.1 ) then
-
 
196
          hhmm = -hhmm
-
 
197
          frac = -frac
-
 
198
      endif
-
 
199
 
182
      end
200
      end
183
 
201
 
184
c     ---------------------------------------------------------------------------
202
c     ---------------------------------------------------------------------------
185
c     Time difference between two dates
203
c     Time difference between two dates
186
c     ---------------------------------------------------------------------------
204
c     ---------------------------------------------------------------------------
187
 
205
 
188
      subroutine timediff(date1,date2,diff)
206
      subroutine timediff(date1,date2,diff)
189
 
207
 
190
C     New version with hour and minutes! (for hour and step [in hours]
208
C     New version with hour and minutes! (for hour and step [in hours]
191
C     use the routine oldtimediff!)
209
C     use the routine oldtimediff!)
192
C
210
C
193
C     Calculates the time difference in hours (and minutes) for the two
211
C     Calculates the time difference in hours (and minutes) for the two
194
C     dates specified by the two arrays date1 and date2.
212
C     dates specified by the two arrays date1 and date2.
195
C     They are expected to contain the following date information:
213
C     They are expected to contain the following date information:
196
C     year      month   day     hour    minute.
214
C     year      month   day     hour    minute.
197
C
215
C
198
C     date1     array specifying the first date
216
C     date1     array specifying the first date
199
C     date2     array specifying the second date
217
C     date2     array specifying the second date
200
C     diff      time differenc between date1 and date2 in hours
218
C     diff      time differenc between date1 and date2 in hours
201
C
219
C
202
 
220
 
203
      integer   date1(5),date2(5)
221
      integer   date1(5),date2(5)
204
      integer   idays(12)       ! array containing the days of the monthes
222
      integer   idays(12)       ! array containing the days of the monthes
205
      real      diff
223
      real      diff
206
      integer   ixday,imdiff,ihdiff,iddiff,j
224
      integer   ixday,imdiff,ihdiff,iddiff,j
207
      integer   yy,yy1,yy2
225
      integer   yy,yy1,yy2
208
 
226
 
209
      idays(1)=31
227
      idays(1)=31
210
      idays(2)=28
228
      idays(2)=28
211
      idays(3)=31
229
      idays(3)=31
212
      idays(4)=30
230
      idays(4)=30
213
      idays(5)=31
231
      idays(5)=31
214
      idays(6)=30
232
      idays(6)=30
215
      idays(7)=31
233
      idays(7)=31
216
      idays(8)=31
234
      idays(8)=31
217
      idays(9)=30
235
      idays(9)=30
218
      idays(10)=31
236
      idays(10)=31
219
      idays(11)=30
237
      idays(11)=30
220
      idays(12)=31
238
      idays(12)=31
221
 
239
 
222
C     Check format of year (YYYY or YY - in case of YY assume 19YY)
240
C     Check format of year (YYYY or YY - in case of YY assume 19YY)
223
 
241
 
224
      if (date1(1).lt.100) date1(1)=1900+date1(1)
242
      if (date1(1).lt.100) date1(1)=1900+date1(1)
225
      if (date2(1).lt.100) date2(1)=1900+date2(1)
243
      if (date2(1).lt.100) date2(1)=1900+date2(1)
226
 
244
 
227
C     Determine if the period between date1 and date2 contains a Feb.29
245
C     Determine if the period between date1 and date2 contains a Feb.29
228
 
246
 
229
      ixday=0   ! extra day flag
247
      ixday=0   ! extra day flag
230
 
248
 
231
      yy1=min(date1(1),date2(1))
249
      yy1=min(date1(1),date2(1))
232
      yy2=max(date1(1),date2(1))
250
      yy2=max(date1(1),date2(1))
233
      if (yy1.eq.yy2) then
251
      if (yy1.eq.yy2) then
234
        if (mod(yy1,4).eq.0) then
252
        if (mod(yy1,4).eq.0) then
235
          idays(2)=29
253
          idays(2)=29
236
        endif
254
        endif
237
      else
255
      else
238
        if (mod(yy1,4).eq.0) then
256
        if (mod(yy1,4).eq.0) then
239
          if (((yy1.eq.date1(1)).and.(date1(2).le.2)).or.
257
          if (((yy1.eq.date1(1)).and.(date1(2).le.2)).or.
240
     >        ((yy1.eq.date2(1)).and.(date2(2).le.2))) then
258
     >        ((yy1.eq.date2(1)).and.(date2(2).le.2))) then
241
            ixday=ixday+1
259
            ixday=ixday+1
242
          endif
260
          endif
243
        endif
261
        endif
244
        if (mod(yy2,4).eq.0) then
262
        if (mod(yy2,4).eq.0) then
245
          if (((yy2.eq.date1(1)).and.(date1(2).gt.2)).or.
263
          if (((yy2.eq.date1(1)).and.(date1(2).gt.2)).or.
246
     >        ((yy2.eq.date2(1)).and.(date2(2).gt.2))) then
264
     >        ((yy2.eq.date2(1)).and.(date2(2).gt.2))) then
247
            ixday=ixday+1
265
            ixday=ixday+1
248
          endif
266
          endif
249
        endif
267
        endif
250
        if (yy2-yy1.gt.1) then
268
        if (yy2-yy1.gt.1) then
251
          do yy=yy1+1,yy2-1
269
          do yy=yy1+1,yy2-1
252
            if (mod(yy,4).eq.0) then
270
            if (mod(yy,4).eq.0) then
253
              ixday=ixday+1
271
              ixday=ixday+1
254
            endif
272
            endif
255
          enddo
273
          enddo
256
        endif
274
        endif
257
      endif
275
      endif
258
 
276
 
259
      ihdiff=0  ! diff. in hours between date1/date2
277
      ihdiff=0  ! diff. in hours between date1/date2
260
      iddiff=0  ! diff. in days  between date1/date2
278
      iddiff=0  ! diff. in days  between date1/date2
261
 
279
 
262
      if (date1(1).gt.date2(1)) then            ! compare years
280
      if (date1(1).gt.date2(1)) then            ! compare years
263
        do j=date2(1),date1(1)-1
281
        do j=date2(1),date1(1)-1
264
          iddiff=iddiff+365
282
          iddiff=iddiff+365
265
        enddo
283
        enddo
266
        iddiff=iddiff+ixday
284
        iddiff=iddiff+ixday
267
      else if (date1(1).lt.date2(1)) then
285
      else if (date1(1).lt.date2(1)) then
268
        do j=date1(1),date2(1)-1
286
        do j=date1(1),date2(1)-1
269
          iddiff=iddiff-365
287
          iddiff=iddiff-365
270
        enddo
288
        enddo
271
        iddiff=iddiff-ixday
289
        iddiff=iddiff-ixday
272
      endif
290
      endif
273
 
291
 
274
      if (date1(2).gt.date2(2)) then            ! compare monthes
292
      if (date1(2).gt.date2(2)) then            ! compare monthes
275
        do j=date2(2),date1(2)-1
293
        do j=date2(2),date1(2)-1
276
          iddiff=iddiff+idays(j)
294
          iddiff=iddiff+idays(j)
277
        enddo
295
        enddo
278
      else if (date1(2).lt.date2(2)) then
296
      else if (date1(2).lt.date2(2)) then
279
        do j=date1(2),date2(2)-1
297
        do j=date1(2),date2(2)-1
280
          iddiff=iddiff-idays(j)
298
          iddiff=iddiff-idays(j)
281
        enddo
299
        enddo
282
      endif
300
      endif
283
 
301
 
284
      iddiff=iddiff+date1(3)-date2(3)
302
      iddiff=iddiff+date1(3)-date2(3)
285
      ihdiff=iddiff*24+date1(4)-date2(4)
303
      ihdiff=iddiff*24+date1(4)-date2(4)
286
      imdiff=ihdiff*60+date1(5)-date2(5)
304
      imdiff=ihdiff*60+date1(5)-date2(5)
287
 
305
 
288
      ihdiff=imdiff/60
306
      ihdiff=imdiff/60
289
      imdiff=mod(imdiff,60)
307
      imdiff=mod(imdiff,60)
290
 
308
 
291
      diff=real(ihdiff)+real(imdiff)/100.
309
      diff=real(ihdiff)+real(imdiff)/100.
292
 
310
 
293
      return
311
      return
294
      end
312
      end