-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathice_calendar.F90
523 lines (457 loc) · 15.6 KB
/
ice_calendar.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
! $Id: ice_calendar.F90 276 2010-05-05 21:49:36Z eclare $
!=======================================================================
!BOP
!
! !MODULE: ice_calendar - calendar routines for managing time
!
! !DESCRIPTION:
!
! Calendar routines for managing time
!
! !REVISION HISTORY:
!
! authors: Elizabeth C. Hunke, LANL
! Tony Craig, NCAR
!
! 2006 ECH: Removed 'w' option for history; added 'h' and histfreq_n.
! Converted to free form source (F90).
!
! !INTERFACE:
!
module ice_calendar
!
! !USES:
!
use ice_constants
use ice_domain_size, only: max_nstrm
use ice_exit, only: abort_ice
#ifdef AusCOM
use cpl_parameters, only : inidate, iniday, inimon, iniyear, init_date
use cpl_parameters, only : il_out, caltype
use cpl_parameters, only : runtime0 !accumulated runtime by the end of last run
#endif
!
!
!EOP
!
implicit none
save
integer (kind=int_kind) :: &
days_per_year , & ! number of days in one year
daymo(12) , & ! number of days in each month
daycal(13) ! day number at end of month
! 360-day year data
integer (kind=int_kind) :: &
daymo360(12) , & ! number of days in each month
daycal360(13) ! day number at end of month
data daymo360 / 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30/
data daycal360/ 0,30, 60, 90,120,150,180,210,240,270,300,330,360/
! 365-day year data
integer (kind=int_kind) :: &
daymo365(12) , & ! number of days in each month
daycal365(13) ! day number at end of month
data daymo365 / 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31/
data daycal365/ 0,31, 59, 90,120,151,181,212,243,273,304,334,365/
#ifdef AusCOM
! 366-day year data (leap year)
integer (kind=int_kind) :: &
daymo366(12) , & ! number of days in each month
daycal366(13) ! day number at end of month
data daymo366 / 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31/
data daycal366/ 0,31, 60, 91,121,152,182,213,244,274,305,335,366/
#endif
integer (kind=int_kind) :: &
istep , & ! local step counter for time loop
istep0 , & ! counter, number of steps taken in previous run
istep1 , & ! counter, number of steps at current timestep
mday , & ! day of the month
hour , & ! hour of the year
month , & ! month number, 1 to 12
monthp , & ! last month
year_init, & ! initial year
nyr , & ! year number
idate , & ! date (yyyymmdd)
idate0 , & ! initial date (yyyymmdd)
sec , & ! elapsed seconds into date
npt , & ! total number of time steps (dt)
ndyn_dt , & ! reduced timestep for dynamics: ndyn_dt=dt/dyn_dt
stop_now , & ! if 1, end program execution
write_restart, & ! if 1, write restart now
diagfreq , & ! diagnostic output frequency (10 = once per 10 dt)
dumpfreq_n , & ! restart output frequency (10 = once per 10 d,m,y)
nstreams , & ! number of history output streams
histfreq_n(max_nstrm), & ! history output frequency
start_pos(max_nstrm), & ! start index to append data to netCDF file
prev_month(max_nstrm) ! month of previous time step to append data to netCDF file
real (kind=dbl_kind) :: &
dt , & ! thermodynamics timestep (s)
dyn_dt , & ! dynamics/transport/ridging timestep (s)
time , & ! total elapsed time (s)
time_forc , & ! time of last forcing update (s)
yday , & ! day of the year
tday , & ! absolute day number
dayyr ! number of days per year
logical (kind=log_kind) :: &
new_year , & ! new year = .true.
new_month , & ! new month = .true.
new_day , & ! new day = .true.
new_hour , & ! new hour = .true.
write_ic , & ! write initial condition now
write_history(max_nstrm) ! write history now
character (len=1) :: &
histfreq(max_nstrm), & ! history output frequency, 'y','m','d','h','1'
dumpfreq ! restart frequency, 'y','m','d'
character (len=char_len) :: calendar_type
!=======================================================================
contains
!=======================================================================
!BOP
!
! !IROUTINE: init_calendar - initialize calendar variables
!
! !INTERFACE:
!
subroutine init_calendar
!
! !DESCRIPTION:
!
! Initialize calendar variables
!
! !REVISION HISTORY:
!
! authors: Elizabeth C. Hunke, LANL
! Tony Craig, NCAR
!
! !USES:
!
! !INPUT/OUTPUT PARAMETERS:
!
!EOP
!
integer (kind=int_kind) :: &
k , &
istep = 0 ! local timestep number
time=istep0*dt ! s
yday=c0 ! absolute day number
mday=0 ! day of the month
month=0 ! month
nyr=0 ! year
idate=00000101 ! date
sec=0 ! seconds into date
istep1 = istep0 ! number of steps at current timestep
! real (dumped) or imagined (use to set calendar)
stop_now = 0 ! end program execution if stop_now=1
dyn_dt = dt/real(ndyn_dt,kind=dbl_kind) ! dynamics et al timestep
prev_month = (/0,0,0,0,0/)
#ifdef AusCOM
if ((days_year(year_init) == 366) .and. (caltype == 1)) days_per_year = 366
#endif
write(*,*)'CICE (calendar) days_per_year = ', days_per_year
dayyr = real(days_per_year, kind=dbl_kind)
if (days_per_year == 360) then
daymo = daymo360
daycal = daycal360
elseif (days_per_year == 365) then
daymo = daymo365
daycal = daycal365
#ifdef AusCOM
elseif (days_per_year == 366) then
daymo = daymo366
daycal = daycal366
#endif
else ! if using leap years, set days_per_year to 365
#ifdef AusCOM
call abort_ice('ice: days_per_year must be 360, 365 or 366')
#else
call abort_ice('ice: days_per_year must be 360 or 365')
#endif
endif
! determine initial date (assumes namelist year_init, istep0 unchanged)
sec = mod(time,secday) ! elapsed seconds into date at
! end of dt
tday = (time-sec)/secday + c1 ! absolute day number
yday = mod(tday-c1,dayyr) + c1 ! day of the year
do k = 1, 12
if (yday > real(daycal(k),kind=dbl_kind)) month = k
enddo
mday = int(yday) - daycal(month) ! day of the month
nyr = int((tday-c1)/dayyr) + 1 ! year number
idate0 = (nyr+year_init-1)*10000 + month*100 + mday ! date (yyyymmdd)
#ifdef AusCOM
write(il_out,*) '(init_calendar) istep0, dt, time, sec = ', istep0, dt, time, sec
write(il_out,*) '(init_calendar) tday, yday, mday, nyr = ', tday, yday, mday, nyr
write(il_out,*) '(init_calendar) idate0 = ', idate0
idate0 = init_date
write(il_out,*) '(init_calendar) idate0 (-corrected-) = ',idate0
print *, 'CICE (init_calendar) idate0 = ', idate0
#endif
end subroutine init_calendar
!=======================================================================
!BOP
!
! !IROUTINE: calendar - computes date at the end of the time step
!
! !INTERFACE:
!
subroutine calendar(ttime)
!
! !DESCRIPTION:
!
! Determine the date at the end of the time step
!
! !REVISION HISTORY:
!
! authors: Elizabeth C. Hunke, LANL
! Tony Craig, NCAR
!
! !USES:
use ice_fileunits
use ice_communicate, only: my_task, master_task
!
! !INPUT/OUTPUT PARAMETERS:
!
real (kind=dbl_kind), intent(in) :: &
ttime ! time variable
!
!EOP
!
integer (kind=int_kind) :: &
k, ileap, ns , &
nyrp,mdayp,hourp , & ! previous year, day, hour
elapsed_days , & ! since beginning this run
elapsed_months , & ! since beginning this run
elapsed_hours ! since beginning this run
#ifdef AusCOM
integer (kind=int_kind) :: &
newh, newd, newm, newy !date by the end of this step
#endif
nyrp=nyr
monthp=month
mdayp=mday
hourp=hour
new_year=.false.
new_month=.false.
new_day=.false.
new_hour=.false.
write_history(:)=.false.
write_restart=0
#ifdef AusCOM
write(il_out,*) '(calendar) ttime = ', ttime
#endif
sec = mod(ttime,secday) ! elapsed seconds into date at
! end of dt
#ifdef AusCOM
call get_idate(ttime, newh, newd, newm, newy)
!
!note ttime is seconds accumulated from the beginning of this run only.
!the following stuff is required here or there in other routines ...
!
yday = (ttime-sec)/secday + c1 ! day of the year
hour = newh
mday = newd
month = newm
nyr = newy - year_init + 1
!
elapsed_months = (nyr - 1)*12 + month - 1
tday = (ttime+runtime0 - mod(ttime+runtime0,secday))/secday + c1
elapsed_days = int(yday) - 1
elapsed_hours = int(ttime/3600)
#else
tday = (ttime-sec)/secday + c1 ! absolute day number
yday = mod(tday-c1,dayyr) + c1 ! day of the year
hour = int((ttime-dt)/c3600) + c1 ! hour
do k = 1, 12
if (yday > real(daycal(k),kind=dbl_kind)) month = k
enddo
mday = int(yday) - daycal(month) ! day of the month
nyr = int((tday-c1)/dayyr) + 1 ! year number
elapsed_months = (nyr - 1)*12 + month - 1
elapsed_days = int(tday) - 1
elapsed_hours = int(ttime/3600)
#endif
idate = (nyr+year_init-1)*10000 + month*100 + mday ! date (yyyymmdd)
#ifdef AusCOM
write(il_out,*) '(calendar) runtime0 = ', runtime0
write(il_out,*) '(calendar) nyr, year_init, month, mday = ', nyr, year_init, month, mday
write(il_out,*) '(calendar) idate = ', idate
#endif
if (istep >= npt+1) stop_now = 1
if (nyr /= nyrp) new_year = .true.
if (month /= monthp) new_month = .true.
if (mday /= mdayp) new_day = .true.
if (hour /= hourp) new_hour = .true.
! leap years turned off, for now
ileap = 0 ! not a leap year
!if (mod(nyr+year_init-1, 4) == 0) ileap = 1
!if (mod(nyr+year_init-1,100) == 0) ileap = 0
!if (mod(nyr+year_init-1,400) == 0) ileap = 1
!if (ileap == 1) then
! daycal = daycal366
! yday = mod(tday-c1,dayyr+c1) + c1 ! day of the year
! do k = 1, 12
! if (yday > real(daycal(k),kind=dbl_kind)) month = k
! enddo
! mday = int(yday) - daycal(month) ! day of the month
! idate = (nyr+year_init-1)*10000 + month*100 + mday ! date (yyyymmdd)
!elseif (days_per_year == 365) then
! daycal = daycal365
!endif
do ns = 1, nstreams
if (histfreq(ns)=='1' .and. histfreq_n(ns)/=0) then
if (mod(istep1, histfreq_n(ns))==0) &
write_history(ns)=.true.
endif
enddo
if (istep > 1) then
do ns = 1, nstreams
select case (histfreq(ns))
case ("y", "Y")
if (new_year .and. histfreq_n(ns)/=0) then
if (mod(nyr, histfreq_n(ns))==0) &
write_history(ns) = .true.
endif
case ("m", "M")
if (new_month .and. histfreq_n(ns)/=0) then
if (mod(elapsed_months,histfreq_n(ns))==0) &
write_history(ns) = .true.
endif
case ("d", "D")
if (new_day .and. histfreq_n(ns)/=0) then
if (mod(elapsed_days,histfreq_n(ns))==0) &
write_history(ns) = .true.
endif
case ("h", "H")
if (new_hour .and. histfreq_n(ns)/=0) then
if (mod(elapsed_hours,histfreq_n(ns))==0) &
write_history(ns) = .true.
endif
end select
enddo ! nstreams
select case (dumpfreq)
case ("y", "Y")
if (new_year .and. mod(nyr, dumpfreq_n)==0) &
write_restart = 1
case ("m", "M")
if (new_month .and. mod(elapsed_months,dumpfreq_n)==0) &
write_restart=1
case ("d", "D")
if (new_day .and. mod(elapsed_days, dumpfreq_n)==0) &
write_restart = 1
end select
endif ! istep > 1
if (my_task == master_task .and. mod(istep,diagfreq) == 0 &
.and. stop_now /= 1) then
write(nu_diag,*) ' '
write(nu_diag,'(a7,i10,4x,a6,i10,4x,a4,i10)') &
'istep1:', istep1, 'idate:', idate, 'sec:', sec
endif
end subroutine calendar
!=======================================================================
#ifdef AusCOM
subroutine get_idate(ttime, khfin, kdfin, kmfin, kyfin)
use cpl_parameters
implicit none
real (kind=dbl_kind), intent(in) :: ttime
integer, intent(out) :: khfin, kdfin, kmfin, kyfin
integer :: klmo(12) !length of the months
integer :: inc_day !increment of days since the beginning of this run
integer :: jm, jd
logical :: lleap
inc_day = int ((ttime + 0.5)/86400. )
khfin = (ttime - inc_day*86400)/3600
IF (caltype .eq. 0 .or. caltype .eq. 1) THEN
!
! 1. Length of the months
!
DO jm = 1, 12
klmo(jm) = 31
if ( (jm-4)*(jm-6)*(jm-9)*(jm-11) == 0) klmo(jm) = 30
IF (jm .eq. 2) THEN
!
!* Leap years
!
lleap = .FALSE.
IF (caltype .eq. 1) THEN
IF (MOD(iniyear, 4) .eq. 0) lleap = .TRUE.
IF (MOD(iniyear,100) .eq. 0) lleap = .FALSE.
IF (MOD(iniyear,400) .eq. 0) lleap = .TRUE.
ENDIF
klmo(jm) = 28
if (lleap) klmo(jm) = 29
ENDIF
ENDDO !jm=1,12
kdfin = iniday
kmfin = inimon
kyfin = iniyear
!
! 2. Loop on the days
!
DO 210 jd = 1, inc_day
kdfin = kdfin + 1
IF (kdfin .le. klmo(kmfin)) GOTO 210
kdfin = 1
kmfin = kmfin + 1
IF (kmfin .le. 12) GOTO 210
kmfin = 1
kyfin = kyfin + 1
!
!* Leap years
!
lleap = .FALSE.
IF (caltype .eq. 1) THEN
IF (MOD(kyfin, 4) .eq. 0) lleap = .TRUE.
IF (MOD(kyfin,100) .eq. 0) lleap = .FALSE.
IF (MOD(kyfin,400) .eq. 0) lleap = .TRUE.
ENDIF
klmo(2) = 28
if (lleap) klmo(2) = 29
210 CONTINUE
ELSE !for years with constant length of months
!
! 1. Calculate month lengths for current year
!
DO jm = 1, 12
klmo(jm) = caltype
ENDDO
kdfin = iniday
kmfin = inimon
kyfin = iniyear
!
! 2. Loop on the days
!
DO 410 jd = 1, inc_day
kdfin = kdfin + 1
IF (kdfin .le. klmo(kmfin)) GOTO 410
kdfin = 1
kmfin = kmfin + 1
IF (kmfin .le. 12) GOTO 410
kmfin = 1
kyfin = kyfin + 1
410 CONTINUE
ENDIF
end subroutine get_idate
!=======================================================================
function days_year(year)
use cpl_parameters, only : caltype
implicit none
integer, intent(in) :: year
real (kind=dbl_kind) :: days_year
logical :: lleap
IF (caltype .eq. 0 .or. caltype .eq. 1) THEN
lleap = .FALSE.
days_year = 365.
IF (caltype .eq. 1) THEN
IF (MOD(year, 4) .eq. 0) lleap = .TRUE.
IF (MOD(year,100) .eq. 0) lleap = .FALSE.
IF (MOD(year,400) .eq. 0) lleap = .TRUE.
ENDIF
if (lleap) days_year = 366.
ELSE
days_year = dayyr
ENDIF
return
end function days_year
#endif
!=======================================================================
end module ice_calendar
!=======================================================================