-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathesm_time_mod.F90
453 lines (392 loc) · 19.5 KB
/
esm_time_mod.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
module esm_time_mod
use shr_kind_mod , only : cx=>shr_kind_cx, cs=>shr_kind_cs, cl=>shr_kind_cl, r8=>shr_kind_r8
use ESMF , only : ESMF_GridComp, ESMF_GridCompGet, ESMF_GridCompSet
use ESMF , only : ESMF_Clock, ESMF_ClockCreate, ESMF_ClockGet, ESMF_ClockSet
use ESMF , only : ESMF_ClockAdvance
use ESMF , only : ESMF_Alarm, ESMF_AlarmCreate, ESMF_AlarmGet
use ESMF , only : ESMF_Calendar, ESMF_CalKind_Flag, ESMF_CalendarCreate
use ESMF , only : ESMF_CALKIND_NOLEAP, ESMF_CALKIND_GREGORIAN
use ESMF , only : ESMF_Time, ESMF_TimeGet, ESMF_TimeSet
use ESMF , only : ESMF_TimeInterval, ESMF_TimeIntervalSet, ESMF_TimeIntervalGet
use ESMF , only : ESMF_SUCCESS, ESMF_LogWrite, ESMF_LOGMSG_INFO, ESMF_FAILURE, ESMF_LOGMSG_ERROR
use ESMF , only : ESMF_VM, ESMF_VMGet, ESMF_VMBroadcast
use ESMF , only : ESMF_VMAllReduce, ESMF_REDUCE_MAX, ESMF_ClockGetAlarm
use ESMF , only : ESMF_LOGMSG_INFO, ESMF_FAILURE, ESMF_GridCompIsPetLocal
use ESMF , only : operator(<), operator(/=), operator(+)
use ESMF , only : operator(-), operator(*) , operator(>=)
use ESMF , only : operator(<=), operator(>), operator(==)
use NUOPC , only : NUOPC_CompAttributeGet
use esm_utils_mod , only : chkerr
use nuopc_shr_methods , only : AlarmInit
implicit none
private ! default private
public :: esm_time_clockinit ! initialize driver clock (assumes default calendar)
private :: esm_time_date2ymd
! Clock and alarm options
character(len=*), private, parameter :: &
optNONE = "none" , &
optNever = "never" , &
optNSeconds = "nsecond" , &
optNMinutes = "nminute" , &
optNHours = "nhour" , &
optNDays = "nday" , &
optNMonths = "nmonth" , &
optNYears = "nyear" , &
optMonthly = "monthly" , &
optYearly = "yearly" , &
optDate = "date" , &
optEnd = "end" , &
optGLCCouplingPeriod = "glc_coupling_period"
! Module data
integer, parameter :: SecPerDay = 86400 ! Seconds per day
character(len=*), parameter :: u_FILE_u = &
__FILE__
!===============================================================================
contains
!===============================================================================
subroutine esm_time_clockinit(ensemble_driver, instance_driver, logunit, maintask, rc)
use nuopc_shr_methods, only : get_minimum_timestep, dtime_drv
! input/output variables
type(ESMF_GridComp) :: ensemble_driver, instance_driver
integer, intent(in) :: logunit
logical, intent(in) :: maintask
integer, intent(out) :: rc
! local variables
type(ESMF_Clock) :: clock
type(ESMF_VM) :: vm, envm
type(ESMF_Time) :: StartTime ! Start time
type(ESMF_Time) :: RefTime ! Reference time
type(ESMF_Time) :: CurrTime ! Current time
type(ESMF_Time) :: StopTime ! Stop time
type(ESMF_Time) :: Clocktime ! Loop time
type(ESMF_TimeInterval) :: TimeStep ! Clock time-step
type(ESMF_Alarm) :: alarm_stop ! alarm
integer :: ref_ymd ! Reference date (YYYYMMDD)
integer :: ref_tod ! Reference time of day (seconds)
integer :: start_ymd ! Start date (YYYYMMDD)
integer :: start_tod ! Start time of day (seconds)
integer :: curr_ymd ! Current ymd (YYYYMMDD)
integer :: curr_tod ! Current tod (seconds)
integer :: stop_n ! Number until stop
integer :: stop_ymd ! Stop date (YYYYMMDD)
integer :: stop_tod ! Stop time-of-day
character(CS) :: stop_option ! Stop option units
character(CS) :: glc_avg_period ! Glc avering coupling period
logical :: read_restart
character(len=CL) :: restart_file
character(len=CL) :: restart_pfile
character(len=CL) :: cvalue
integer :: yr, mon, day ! Year, month, day as integers
integer :: unitn ! unit number
integer :: ierr ! Return code
character(CL) :: tmpstr ! temporary
character(CS) :: inst_suffix
integer :: tmp(4) ! Array for Broadcast
integer :: myid, bcastID(2)
logical :: isPresent
logical :: inDriver
logical, save :: firsttime=.true.
logical :: exists
character(len=*), parameter :: subname = '('//__FILE__//':esm_time_clockInit) '
!-------------------------------------------------------------------------------
rc = ESMF_SUCCESS
call ESMF_LogWrite(trim(subname)//": called", ESMF_LOGMSG_INFO)
!---------------------------------------------------------------------------
! Determine start time, reference time and current time
!---------------------------------------------------------------------------
call NUOPC_CompAttributeGet(ensemble_driver, name="start_ymd", value=cvalue, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
read(cvalue,*) start_ymd
call NUOPC_CompAttributeGet(ensemble_driver, name="start_tod", value=cvalue, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
read(cvalue,*) start_tod
call ESMF_GridCompGet(ensemble_driver, vm=envm, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
call ESMF_VMGet(envm, localPet=myid, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
indriver = ESMF_GridCompIsPetLocal(instance_driver, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
if(indriver) then
call ESMF_GridCompGet(instance_driver, vm=vm, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
call NUOPC_CompAttributeGet(instance_driver, name='read_restart', value=cvalue, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
read(cvalue,*) read_restart
if (read_restart) then
call NUOPC_CompAttributeGet(instance_driver, name='drv_restart_pointer', value=restart_pfile, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
if (trim(restart_pfile) /= 'none') then
if (maintask) then
write(logunit,*) " read rpointer file = "//trim(restart_pfile)
inquire( file=trim(restart_pfile), exist=exists)
if (.not. exists) then
rc = ESMF_FAILURE
call ESMF_LogWrite(trim(subname)//' ERROR rpointer file '//trim(restart_pfile)//' not found', &
ESMF_LOGMSG_ERROR, line=__LINE__, file=__FILE__)
return
endif
call ESMF_LogWrite(trim(subname)//" read rpointer file = "//trim(restart_pfile), &
ESMF_LOGMSG_INFO)
open(newunit=unitn, file=restart_pfile, form='FORMATTED', status='old',iostat=ierr)
if (ierr < 0) then
rc = ESMF_FAILURE
call ESMF_LogWrite(trim(subname)//' ERROR rpointer file open returns error', &
ESMF_LOGMSG_ERROR, line=__LINE__, file=__FILE__)
return
end if
read(unitn,'(a)', iostat=ierr) restart_file
if (ierr < 0) then
rc = ESMF_FAILURE
call ESMF_LogWrite(trim(subname)//' ERROR rpointer file read returns error', &
ESMF_LOGMSG_INFO, line=__LINE__, file=__FILE__)
return
end if
close(unitn)
if (maintask) then
write(logunit,'(a)') trim(subname)//" reading driver restart from file = "//trim(restart_file)
end if
call esm_time_read_restart(restart_file, start_ymd, start_tod, curr_ymd, curr_tod, rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
endif
else
if(maintask) then
write(logunit,*) ' NOTE: the current compset has no mediator - which provides the clock restart information'
write(logunit,*) ' In this case the restarts are handled solely by the component being used and'
write(logunit,*) ' and the driver clock will always be starting from the initial date on restart'
end if
curr_ymd = start_ymd
curr_tod = start_tod
endif
else
curr_ymd = start_ymd
curr_tod = start_tod
end if ! end if read_restart
endif
if(maintask) then
bcastID(1) = myid
tmp(1) = start_ymd ; tmp(2) = start_tod
tmp(3) = curr_ymd ; tmp(4) = curr_tod
else
bcastID(1) = 0
tmp = 0
endif
call ESMF_VMAllReduce(envm, bcastID(1:1), bcastID(2:2), 1, ESMF_REDUCE_MAX,rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
call ESMF_VMBroadcast(envm, tmp, 4, bcastID(2), rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
start_ymd = tmp(1) ; start_tod = tmp(2)
curr_ymd = tmp(3) ; curr_tod = tmp(4)
! Determine start time (THE FOLLOWING ASSUMES THAT THE DEFAULT CALENDAR IS SET in the driver)
call esm_time_date2ymd(start_ymd, yr, mon, day)
call ESMF_TimeSet( StartTime, yy=yr, mm=mon, dd=day, s=start_tod, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
if(maintask) then
write(tmpstr,'(i10)') start_ymd
call ESMF_LogWrite(trim(subname)//': driver start_ymd: '// trim(tmpstr), ESMF_LOGMSG_INFO)
write(logunit,*) trim(subname)//': driver start_ymd: '// trim(tmpstr)
write(tmpstr,'(i10)') start_tod
call ESMF_LogWrite(trim(subname)//': driver start_tod: '// trim(tmpstr), ESMF_LOGMSG_INFO)
write(logunit,*) trim(subname)//': driver start_tod: '// trim(tmpstr)
endif
! Determine current time
call esm_time_date2ymd(curr_ymd, yr, mon, day)
call ESMF_TimeSet( CurrTime, yy=yr, mm=mon, dd=day, s=curr_tod, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
if(maintask) then
write(tmpstr,'(i10)') curr_ymd
call ESMF_LogWrite(trim(subname)//': driver curr_ymd: '// trim(tmpstr), ESMF_LOGMSG_INFO)
write(logunit,*) trim(subname)//': driver curr_ymd: '// trim(tmpstr)
write(tmpstr,'(i10)') curr_tod
call ESMF_LogWrite(trim(subname)//': driver curr_tod: '// trim(tmpstr), ESMF_LOGMSG_INFO)
write(logunit,*) trim(subname)//': driver curr_tod: '// trim(tmpstr)
endif
! Set reference time - HARD-CODED TO START TIME
ref_ymd = start_ymd
ref_tod = start_tod
call esm_time_date2ymd(ref_ymd, yr, mon, day)
call ESMF_TimeSet( RefTime, yy=yr, mm=mon, dd=day, s=ref_tod, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
dtime_drv = get_minimum_timestep(ensemble_driver, rc)
if(maintask) then
write(tmpstr,'(i10)') dtime_drv
call ESMF_LogWrite(trim(subname)//': driver time interval is : '// trim(tmpstr), ESMF_LOGMSG_INFO, rc=rc)
write(logunit,*) trim(subname)//': driver time interval is : '// trim(tmpstr)
endif
call ESMF_TimeIntervalSet( TimeStep, s=dtime_drv, rc=rc )
if (ChkErr(rc,__LINE__,u_FILE_u)) return
!---------------------------------------------------------------------------
! Create an instance_driver clock
!---------------------------------------------------------------------------
! Create the clock
clock = ESMF_ClockCreate(TimeStep, StartTime, refTime=RefTime, name='ESMF Driver Clock', rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! Advance the clock to the current time (in case of a restart)
call ESMF_ClockGet(clock, currTime=clocktime, rc=rc )
if (ChkErr(rc,__LINE__,u_FILE_u)) return
do while( clocktime < CurrTime)
call ESMF_ClockAdvance( clock, rc=rc )
if (ChkErr(rc,__LINE__,u_FILE_u)) return
call ESMF_ClockGet( clock, currTime=clocktime, rc=rc )
if (ChkErr(rc,__LINE__,u_FILE_u)) return
end do
! Set the driver gridded component clock to the created clock
if (indriver) then
call ESMF_GridCompSet(instance_driver, clock=clock, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
endif
! Set driver clock stop time
call NUOPC_CompAttributeGet(ensemble_driver, name="stop_option", value=stop_option, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
call NUOPC_CompAttributeGet(ensemble_driver, name="stop_n", value=cvalue, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
read(cvalue,*) stop_n
call NUOPC_CompAttributeGet(ensemble_driver, name="stop_ymd", value=cvalue, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
read(cvalue,*) stop_ymd
call NUOPC_CompAttributeGet(ensemble_driver, name="stop_tod", value=cvalue, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
read(cvalue,*) stop_tod
if ( stop_ymd < 0) then
stop_ymd = 99990101
stop_tod = 0
endif
if (maintask) then
write(tmpstr,'(i10)') stop_ymd
call ESMF_LogWrite(trim(subname)//': driver stop_ymd: '// trim(tmpstr), ESMF_LOGMSG_INFO)
write(logunit,*) trim(subname)//': driver stop_ymd: '// trim(tmpstr)
write(tmpstr,'(i10)') stop_tod
call ESMF_LogWrite(trim(subname)//': driver stop_tod: '// trim(tmpstr), ESMF_LOGMSG_INFO)
write(logunit,*) trim(subname)//': driver stop_tod: '// trim(tmpstr)
endif
call alarmInit(clock, &
alarm = alarm_stop, &
option = stop_option, &
opt_n = stop_n, &
opt_ymd = stop_ymd, &
opt_tod = stop_tod, &
RefTime = CurrTime, &
alarmname = 'alarm_stop', rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
call ESMF_AlarmGet(alarm_stop, RingTime=StopTime, rc=rc )
if (ChkErr(rc,__LINE__,u_FILE_u)) return
call ESMF_ClockSet(clock, StopTime=StopTime, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
!---------------------------------------------------------------------------
! Create the ensemble driver clock
!---------------------------------------------------------------------------
if(firsttime) then
TimeStep = StopTime - ClockTime
clock = ESMF_ClockCreate(TimeStep, ClockTime, StopTime=StopTime, &
refTime=RefTime, name='ESMF ensemble Driver Clock', rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
call ESMF_GridCompSet(ensemble_driver, clock=clock, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
firsttime = .false.
endif
end subroutine esm_time_clockinit
!===============================================================================
subroutine esm_time_date2ymd (date, year, month, day)
! input/output variables
integer, intent(in) :: date ! coded-date (yyyymmdd)
integer, intent(out) :: year,month,day ! calendar year,month,day
! local variables
integer :: tdate ! temporary date
character(*),parameter :: subName = "(esm_time_date2ymd)"
!-------------------------------------------------------------------------------
tdate = abs(date)
year = int(tdate/10000)
if (date < 0) then
year = -year
end if
month = int( mod(tdate,10000)/ 100)
day = mod(tdate, 100)
end subroutine esm_time_date2ymd
!===============================================================================
subroutine esm_time_read_restart(restart_file, start_ymd, start_tod, curr_ymd, curr_tod, rc)
use netcdf , only : nf90_open, nf90_nowrite, nf90_noerr
use netcdf , only : nf90_inq_varid, nf90_get_var, nf90_close
use ESMF , only : ESMF_LogWrite, ESMF_LOGMSG_INFO
! input/output variables
character(len=*), intent(in) :: restart_file
integer, intent(out) :: start_ymd ! Start date (YYYYMMDD)
integer, intent(out) :: start_tod ! Start time of day (seconds)
integer, intent(out) :: curr_ymd ! Current ymd (YYYYMMDD)
integer, intent(out) :: curr_tod ! Current tod (seconds)
integer, intent(out) :: rc
! local variables
integer :: status, ncid, varid ! netcdf stuff
character(CL) :: tmpstr ! temporary
character(len=*), parameter :: subname = "(esm_time_read_restart)"
!----------------------------------------------------------------
! use netcdf here since it's serial
rc = ESMF_SUCCESS
status = nf90_open(restart_file, NF90_NOWRITE, ncid)
if (status /= nf90_NoErr) then
call ESMF_LogWrite(trim(subname)//' ERROR: nf90_open: '//trim(restart_file), ESMF_LOGMSG_ERROR)
rc = ESMF_FAILURE
return
endif
status = nf90_inq_varid(ncid, 'start_ymd', varid)
if (status /= nf90_NoErr) then
call ESMF_LogWrite(trim(subname)//' ERROR: nf90_inq_varid start_ymd', ESMF_LOGMSG_ERROR)
rc = ESMF_FAILURE
return
end if
status = nf90_get_var(ncid, varid, start_ymd)
if (status /= nf90_NoErr) then
call ESMF_LogWrite(trim(subname)//' ERROR: nf90_get_var start_ymd', ESMF_LOGMSG_ERROR)
rc = ESMF_FAILURE
return
end if
status = nf90_inq_varid(ncid, 'start_tod', varid)
if (status /= nf90_NoErr) then
call ESMF_LogWrite(trim(subname)//' ERROR: nf90_inq_varid start_tod', ESMF_LOGMSG_ERROR)
rc = ESMF_FAILURE
return
end if
status = nf90_get_var(ncid, varid, start_tod)
if (status /= nf90_NoErr) then
call ESMF_LogWrite(trim(subname)//' ERROR: nf90_get_var start_tod', ESMF_LOGMSG_ERROR)
rc = ESMF_FAILURE
return
end if
status = nf90_inq_varid(ncid, 'curr_ymd', varid)
if (status /= nf90_NoErr) then
call ESMF_LogWrite(trim(subname)//' ERROR: nf90_inq_varid curr_ymd', ESMF_LOGMSG_ERROR)
rc = ESMF_FAILURE
return
end if
status = nf90_get_var(ncid, varid, curr_ymd)
if (status /= nf90_NoErr) then
call ESMF_LogWrite(trim(subname)//' ERROR: nf90_get_var curr_ymd', ESMF_LOGMSG_ERROR)
rc = ESMF_FAILURE
return
end if
status = nf90_inq_varid(ncid, 'curr_tod', varid)
if (status /= nf90_NoErr) then
call ESMF_LogWrite(trim(subname)//' ERROR: nf90_inq_varid curr_tod', ESMF_LOGMSG_ERROR)
rc = ESMF_FAILURE
return
end if
status = nf90_get_var(ncid, varid, curr_tod)
if (status /= nf90_NoErr) then
call ESMF_LogWrite(trim(subname)//' ERROR: nf90_get_var curr_tod', ESMF_LOGMSG_ERROR)
rc = ESMF_FAILURE
return
end if
status = nf90_close(ncid)
if (status /= nf90_NoErr) then
call ESMF_LogWrite(trim(subname)//' ERROR: nf90_close', ESMF_LOGMSG_ERROR)
rc = ESMF_FAILURE
return
end if
write(tmpstr,*) trim(subname)//" read start_ymd = ",start_ymd
call ESMF_LogWrite(trim(tmpstr), ESMF_LOGMSG_INFO)
write(tmpstr,*) trim(subname)//" read start_tod = ",start_tod
call ESMF_LogWrite(trim(tmpstr), ESMF_LOGMSG_INFO)
write(tmpstr,*) trim(subname)//" read curr_ymd = ",curr_ymd
call ESMF_LogWrite(trim(tmpstr), ESMF_LOGMSG_INFO)
write(tmpstr,*) trim(subname)//" read curr_tod = ",curr_tod
call ESMF_LogWrite(trim(tmpstr), ESMF_LOGMSG_INFO)
end subroutine esm_time_read_restart
end module esm_time_mod