Subversion Repositories lagranto.20cr

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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