-
Notifications
You must be signed in to change notification settings - Fork 1
/
NORTRIP_time_functions.f90
607 lines (493 loc) · 16.6 KB
/
NORTRIP_time_functions.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
83
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
!----------------------------------------------------------------------
! Various functions for manipulating time
!----------------------------------------------------------------------
!----------------------------------------------------------------------
subroutine number_to_date(date_num,date_array)
implicit none
double precision date_num
integer ref_year
integer y,m,d,i
integer date_array(6)
double precision day_fraction
integer day_int
integer day_count,days_in_year
integer rest_seconds
integer daysinmonth(12)
data (daysinmonth(i),i=1,12) /31,28,31,30,31,30,31,31,30,31,30,31/
ref_year=1970
!Set day fraction to the nearest second. Avoiding round off errors
day_int=idint(date_num)
day_fraction=(date_num-day_int)
!write(*,*) day_fraction
!Determine hours, minutes and seconds
date_array=0
rest_seconds=idint(day_fraction*24.*3600.+.5) !Rounded off for seconds
date_array(4)=int(rest_seconds/3600.)
date_array(5)=int((rest_seconds/60.-date_array(4)*60.))
date_array(6)=int((rest_seconds-date_array(4)*3600.-date_array(5)*60.))
!Count up days keeping track of the year, month and day of month
!Determine year
y=ref_year-1
day_count=0
do while (day_count.le.day_int)
y=y+1
days_in_year=365
if (((mod(y,4).eq.0).and.(mod(y,100).ne.0)).or.(mod(y,400).eq.0)) days_in_year=366
day_count=day_count+days_in_year
enddo
date_array(1)=y
day_count=day_count-days_in_year
!Determine month given the year
daysinmonth(2)=28
if (((mod(date_array(1),4).eq.0).and.(mod(date_array(1),100).ne.0)).or.(mod(date_array(1),400).eq.0)) daysinmonth(2)=29
m=0
!day_count=0
do while (day_count.le.day_int)
m=m+1
day_count=day_count+daysinmonth(m)
enddo
date_array(2)=m
day_count=day_count-daysinmonth(m)
!Determine day
d=0
do while (day_count.le.day_int)
d=d+1
day_count=day_count+1
enddo
date_array(3)=d
end subroutine number_to_date
!----------------------------------------------------------------------
!----------------------------------------------------------------------
function date_to_number(a)
implicit none
double precision date_to_number
integer ref_year
integer y,m,d,i
integer a(6)
integer daysinmonth(12)
data (daysinmonth(i),i=1,12) /31,28,31,30,31,30,31,31,30,31,30,31/
ref_year=1970
date_to_number=0.
daysinmonth(2)=28
if (a(1).gt.ref_year) then
!Add up days in the year
do y=ref_year,a(1)-1
if (((mod(y,4).eq.0).and.(mod(y,100).ne.0)).or.(mod(y,400).eq.0)) then
daysinmonth(2)=29
else
daysinmonth(2)=28
endif
do m=1,12
date_to_number=date_to_number+sngl(daysinmonth(m))
end do
end do
endif
!Add up days in the remaining months
if (((mod(a(1),4).eq.0).and.(mod(a(1),100).ne.0)).or.(mod(a(1),400).eq.0)) then
daysinmonth(2)=29
else
daysinmonth(2)=28
endif
if (a(2).gt.1) then
do m=1,a(2)-1
date_to_number=date_to_number+sngl(daysinmonth(m))
enddo
endif
date_to_number=date_to_number+sngl(a(3))-1.
date_to_number=date_to_number+sngl(a(4))/24. !starts at 0
date_to_number=date_to_number+sngl(a(5))/24./60. !starts at 0
date_to_number=date_to_number+sngl(a(6))/24./60./60. !starts at 0
!write(*,*) date_to_number
end function date_to_number
!----------------------------------------------------------------------
!----------------------------------------------------------------------
function date_to_julian(a)
implicit none
double precision date_to_number
real date_to_julian
integer a(6),b(6)
b(1)=a(1)
b(2)=1
b(3)=1
b(4)=0
b(5)=0
b(6)=0
date_to_julian=date_to_number(a)-date_to_number(b)+1
end function date_to_julian
!----------------------------------------------------------------------
!----------------------------------------------------------------------
subroutine datestr_to_date(a_str,format_str,a)
implicit none
character(24) a_str,format_str
integer a(6)
integer pos
!based on (yyyy.mm.dd HH:MM:SS)
!extract year
pos=index(format_str,'yyyy')
if (pos.gt.0) then
read(a_str(pos:pos+3),*) a(1)
else
a(1)=0
endif
pos=index(format_str,'mm')
if (pos.gt.0) then
read(a_str(pos:pos+1),*) a(2)
else
a(2)=0
endif
pos=index(format_str,'dd')
if (pos.gt.0) then
read(a_str(pos:pos+1),*) a(3)
else
a(3)=0
endif
pos=index(format_str,'HH')
if (pos.gt.0) then
read(a_str(pos:pos+1),*) a(4)
else
a(4)=0
endif
pos=index(format_str,'MM')
if (pos.gt.0) then
read(a_str(pos:pos+1),*) a(5)
else
a(5)=0
endif
pos=index(format_str,'SS')
if (pos.gt.0) then
read(a_str(pos:pos+1),*) a(6)
else
a(6)=0
endif
end subroutine datestr_to_date
!----------------------------------------------------------------------
!----------------------------------------------------------------------
subroutine date_to_datestr(a,format_str,a_str)
implicit none
character(*) a_str,format_str
integer a(6)
integer pos
!based on (yyyy.mm.dd HH:MM:SS)
a_str=format_str
!To avoid just putting in date parts e.g. mm or dd that might occurr in a string then it is required that at least two of the date
!strings are present, i.e. yyyy, mm and dd or HH, MM and SS
if ((index(format_str,'yyyy').gt.0.and.index(format_str,'mm').gt.0).or.(index(format_str,'yyyy').gt.0.and.index(format_str,'dd').gt.0).or.(index(format_str,'dd').gt.0.and.index(format_str,'mm').gt.0).or. &
(index(format_str,'HH').gt.0.and.index(format_str,'MM').gt.0).or.(index(format_str,'HH').gt.0.and.index(format_str,'SS').gt.0).or.(index(format_str,'MM').gt.0.and.index(format_str,'SS').gt.0)) then
!Do nothing but continue with routine as this is a valid format for date string substitution
else
!Leave the routine
return
endif
!Now it only accepts the two strings 'yyyymmdd' and 'yyyymmddHH' for replacement
pos=index(format_str,'yyyymmddHH')
if (pos.gt.0) then
write(a_str(pos:pos+3),'(i4)') a(1)
if (a(2).gt.9) then
write(a_str(pos+4:pos+5),'(i2)') a(2)
else
write(a_str(pos+4:pos+5),'(a1,i1)') '0',a(2)
endif
if (a(3).gt.9) then
write(a_str(pos+6:pos+7),'(i2)') a(3)
else
write(a_str(pos+6:pos+7),'(a1,i1)') '0',a(3)
endif
if (a(4).gt.9) then
write(a_str(pos+8:pos+9),'(i2)') a(4)
else
write(a_str(pos+8:pos+9),'(a1,i1)') '0',a(4)
endif
return
else
!a_str(pos:pos+3)='0000'
endif
pos=index(format_str,'yyyymmdd')
if (pos.gt.0) then
write(a_str(pos:pos+3),'(i4)') a(1)
if (a(2).gt.9) then
write(a_str(pos+4:pos+5),'(i2)') a(2)
else
write(a_str(pos+4:pos+5),'(a1,i1)') '0',a(2)
endif
if (a(3).gt.9) then
write(a_str(pos+6:pos+7),'(i2)') a(3)
else
write(a_str(pos+6:pos+7),'(a1,i1)') '0',a(3)
endif
return
else
!a_str(pos:pos+3)='0000'
endif
!return
!Do not do the rest
!extract year
pos=index(format_str,'yyyy')
if (pos.gt.0) then
write(a_str(pos:pos+3),'(i4)') a(1)
else
!a_str(pos:pos+3)='0000'
endif
pos=index(format_str,'mm')
if (pos.gt.0) then
if (a(2).gt.9) then
write(a_str(pos:pos+1),'(i2)') a(2)
else
write(a_str(pos:pos+1),'(a1,i1)') '0',a(2)
endif
else
!a_str(pos:pos+1)='00'
endif
pos=index(format_str,'dd')
if (pos.gt.0) then
if (a(3).gt.9) then
write(a_str(pos:pos+1),'(i2)') a(3)
else
write(a_str(pos:pos+1),'(a1,i1)') '0',a(3)
endif
else
!a_str(pos:pos+1)='00'
endif
pos=index(format_str,'HH')
if (pos.gt.0) then
if (a(4).gt.9) then
write(a_str(pos:pos+1),'(i2)') a(4)
else
write(a_str(pos:pos+1),'(a1,i1)') '0',a(4)
endif
else
!a_str(pos:pos+1)='00'
endif
pos=index(format_str,'MM')
if (pos.gt.0) then
if (a(5).gt.9) then
write(a_str(pos:pos+1),'(i2)') a(5)
else
write(a_str(pos:pos+1),'(a1,i1)') '0',a(5)
endif
else
!a_str(pos:pos+1)='00'
endif
pos=index(format_str,'SS')
if (pos.gt.0) then
if (a(6).gt.9) then
write(a_str(pos:pos+1),'(i2)') a(6)
else
write(a_str(pos:pos+1),'(a1,i1)') '0',a(6)
endif
else
!a_str(pos:pos+1)='00'
endif
end subroutine date_to_datestr
!----------------------------------------------------------------------
!----------------------------------------------------------------------
subroutine date_to_datestr_bracket(a,in_format_str,out_a_str)
implicit none
character(*), intent(out) :: out_a_str
character(*), intent(in) :: in_format_str
integer, intent(in) :: a(6)
character(256) format_str,a_str
integer pos
integer pos1,pos2
!based on (yyyy.mm.dd HH:MM:SS)
!a_str=format_str
!To avoid just putting in date parts e.g. mm or dd that might occurr in a string then it is required that at least two of the date
!strings are present, i.e. yyyy, mm and dd or HH, MM and SS
!Only changes dates when they are inside '<.....>'
!Removes these once changed
pos1=index(in_format_str,'<')
pos2=index(in_format_str,'>')
if (pos1.le.0.or.pos2.le.0.or.pos1+1.gt.pos2-1) then
out_a_str=in_format_str
return
endif
!Reassign format_str to be just the text between <..>
format_str=in_format_str(pos1+1:pos2-1)
a_str=format_str
!extract year
pos=index(format_str,'yyyy')
if (pos.gt.0) then
write(a_str(pos:pos+3),'(i4)') a(1)
endif
pos=index(format_str,'mm')
if (pos.gt.0) then
if (a(2).gt.9) then
write(a_str(pos:pos+1),'(i2)') a(2)
else
write(a_str(pos:pos+1),'(a1,i1)') '0',a(2)
endif
endif
pos=index(format_str,'dd')
if (pos.gt.0) then
if (a(3).gt.9) then
write(a_str(pos:pos+1),'(i2)') a(3)
else
write(a_str(pos:pos+1),'(a1,i1)') '0',a(3)
endif
endif
pos=index(format_str,'HH')
if (pos.gt.0) then
if (a(4).gt.9) then
write(a_str(pos:pos+1),'(i2)') a(4)
else
write(a_str(pos:pos+1),'(a1,i1)') '0',a(4)
endif
endif
pos=index(format_str,'MM')
if (pos.gt.0) then
if (a(5).gt.9) then
write(a_str(pos:pos+1),'(i2)') a(5)
else
write(a_str(pos:pos+1),'(a1,i1)') '0',a(5)
endif
endif
pos=index(format_str,'SS')
if (pos.gt.0) then
if (a(6).gt.9) then
write(a_str(pos:pos+1),'(i2)') a(6)
else
write(a_str(pos:pos+1),'(a1,i1)') '0',a(6)
endif
endif
!insert the a_str into out_a_str, removing the '<>' text
if (len_trim(in_format_str).gt.pos2) then
out_a_str=in_format_str(1:pos1-1)//trim(a_str)//in_format_str(pos2+1:)
else
out_a_str=in_format_str(1:pos1-1)//trim(a_str)
endif
!write(*,*) trim(in_format_str),trim(out_a_str)
!stop
end subroutine date_to_datestr_bracket
!----------------------------------------------------------------------
!----------------------------------------------------------------------
subroutine num_to_numstr_squarebracket(a,in_format_str,out_a_str)
implicit none
character(*), intent(out) :: out_a_str
character(*), intent(in) :: in_format_str
integer, intent(in) :: a
character(256) format_str,a_str
integer pos
integer pos1,pos2
!Replaces 'num' inbetween brackets with a 3 digit number. Used for NORA3 reading
!Only changes dates when they are inside '<.....>'
!Removes these once changed
pos1=index(in_format_str,'[')
pos2=index(in_format_str,']')
if (pos1.le.0.or.pos2.le.0.or.pos1+1.gt.pos2-1) then
out_a_str=in_format_str
return
endif
!Reassign format_str to be just the text between <..>
format_str=in_format_str(pos1+1:pos2-1)
a_str=format_str
pos=index(format_str,'num')
if (pos.gt.0) then
if (a.gt.99) then
write(a_str(pos:pos+2),'(i3)') a
elseif (a.gt.9) then
write(a_str(pos:pos+2),'(a1,i2)') '0',a
else
write(a_str(pos:pos+2),'(a2,i1)') '00',a
endif
endif
!insert the a_str into out_a_str, removing the '<>' text
if (len_trim(in_format_str).gt.pos2) then
out_a_str=in_format_str(1:pos1-1)//trim(a_str)//in_format_str(pos2+1:)
else
out_a_str=in_format_str(1:pos1-1)//trim(a_str)
endif
!write(*,*) trim(in_format_str),trim(out_a_str)
!stop
end subroutine num_to_numstr_squarebracket
!----------------------------------------------------------------------
!----------------------------------------------------------------------
function day_of_week (a)
!Adapted from EPISODE code
implicit none
!The subroutine calculates the day of week given current datetime,
!where DAYW = 1 corresponds to Monday and DAYW = 7 to Sunday. The
!algorithm is based on the tables in "Hvem Hva Hvor 1971" (p. 121)
!and is valid for all years from 1800 to infinity!
!USE mod_time
! Local variables
INTEGER JM(12)
INTEGER IR
INTEGER JC
INTEGER NT
INTEGER JK
INTEGER J4
INTEGER J100
INTEGER J400
LOGICAL LEAP
! JM - Column number for each month
! IR - Row in HHH table for day of month
! JC - Column in HHH table for month
! NT - Number in HHH table for row IR and column JC
! JK - Column in HHH table for year
! J4 - Adjustment value for leap year
! J100 - Adjustment value for leap year
! J400 - Adjustment value for leap year
! LEAP - If leap year then true else false
integer DAYM,MNTH,YEAR
integer day_of_week
integer a(6)
DAYM=a(3)
MNTH=a(2)
YEAR=a(1)
!Calculate leap year or not
LEAP = .FALSE.
IF (MOD(YEAR, 4) .EQ. 0 .AND. .NOT. (MOD(YEAR,100) .EQ. 0 .AND. MOD(YEAR,400) .NE. 0)) LEAP = .TRUE.
! Set data in table JM
DATA JM/1,5,5,2,7,4,2,6,3,1,5,3/
! Calculate row number for day of month
IR = MOD(DAYM - 1,7) + 1
! Calculate column number for month
JC = JM(MNTH)
IF (LEAP .AND. (MNTH .EQ. 1 .OR. MNTH .EQ. 2)) JC = JC + 1
! Calculate "number" in HHH table with row IR and column JC
NT = MOD(IR + 7 - JC,7) + 1
! Calculate column number for year (adjusting for leap years)
J4 = (YEAR - 1800)/4
J100 = (YEAR - 1800)/100
J400 = (YEAR - 1600)/400
JK = MOD(YEAR - 1800 + J4 - J100 + J400 + 3 - 1,7) + 1
! Calculate day of week
day_of_week = MOD(JK - 1 + NT - 1,7) + 1
RETURN
! End of subroutine CDAYW
end function day_of_week
!----------------------------------------------------------------------
!----------------------------------------------------------------------
function summer_time_europe(a_in)
implicit none
logical summer_time_europe
integer a(6),a_in(6)
integer b_start(6),b_end(6)
integer ref_year
integer year
double precision datenum_start,datenum_end,datenum
double precision date_to_number
a=a_in
ref_year=2000
b_start=0
b_end=0
year=a(1)
b_start(1)=a(1)
b_start(2)=3
b_start(3)=(31 - mod((((5 * year)/4) + 4),7))
b_start(4)=1
b_end(1)=a(1)
b_end(2)=10
b_end(3)=(31 - mod((((5 * year)/4) + 1),7))
b_end(4)=1
datenum_start=date_to_number(b_start)
datenum_end=date_to_number(b_end)
datenum=date_to_number(a)
summer_time_europe=.false.
if (datenum.ge.datenum_start.and.datenum.lt.datenum_end) summer_time_europe=.true.
!write(*,'(6i6)') a
!write(*,'(6i6)') b_start
!write(*,'(6i6)') b_end
!write(*,*) summer_time_europe
!write(*,*) datenum_start,datenum_end,datenum
end function summer_time_europe
!----------------------------------------------------------------------