Subversion Repositories lagranto.ecmwf

Rev

Rev 3 | Details | Compare with Previous | Last modification | View Log | RSS feed

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