10 use esmf ,
only : esmf_time, esmf_clock, esmf_calendar, esmf_alarm
11 use esmf ,
only : esmf_timeget, esmf_timeset
12 use esmf ,
only : esmf_timeinterval, esmf_timeintervalset
13 use esmf ,
only : esmf_clockget, esmf_alarmcreate
14 use esmf ,
only : esmf_success, esmf_logwrite, esmf_logmsg_info
15 use esmf ,
only : esmf_logseterror, esmf_logfounderror, esmf_logerr_passthru
16 use esmf ,
only : esmf_rc_arg_bad
17 use esmf ,
only :
operator(<),
operator(/=),
operator(+),
operator(-),
operator(*) ,
operator(>=)
18 use esmf ,
only :
operator(<=),
operator(>),
operator(==)
20 implicit none;
private
28 character(len=*),
private,
parameter :: &
67 subroutine alarminit( clock, alarm, option, &
68 opt_n, opt_ymd, opt_tod, RefTime, alarmname, rc)
69 type(esmf_clock) ,
intent(inout) :: clock
70 type(esmf_alarm) ,
intent(inout) :: alarm
71 character(len=*) ,
intent(in) :: option
72 integer ,
optional ,
intent(in) :: opt_n
73 integer ,
optional ,
intent(in) :: opt_ymd
74 integer ,
optional ,
intent(in) :: opt_tod
75 type(esmf_time) ,
optional ,
intent(in) :: reftime
76 character(len=*) ,
optional ,
intent(in) :: alarmname
77 integer ,
intent(inout) :: rc
80 type(esmf_calendar) :: cal
83 integer :: cyy,cmm,cdd,csec
84 integer :: nyy,nmm,ndd,nsec
85 character(len=64) :: lalarmname
86 logical :: update_nextalarm
87 type(esmf_time) :: currtime
88 type(esmf_time) :: nextalarm
89 type(esmf_timeinterval) :: alarminterval
90 character(len=*),
parameter :: subname =
'(AlarmInit): '
95 lalarmname =
'alarm_unknown'
96 if (
present(alarmname)) lalarmname = trim(alarmname)
98 if (
present(opt_tod)) ltod = opt_tod
100 if (
present(opt_ymd)) lymd = opt_ymd
111 if (.not.
present(opt_n))
then
112 call esmf_logseterror(esmf_rc_arg_bad, &
113 msg=subname//trim(option)//
' requires opt_n', &
115 file=__file__, rctoreturn=rc)
119 call esmf_logseterror(esmf_rc_arg_bad, &
120 msg=subname//trim(option)//
' invalid opt_n', &
122 file=__file__, rctoreturn=rc)
127 call esmf_clockget(clock, currtime=currtime, rc=rc)
128 if (esmf_logfounderror(rctocheck=rc, msg=esmf_logerr_passthru, &
133 call esmf_timeget(currtime, yy=cyy, mm=cmm, dd=cdd, s=csec, rc=rc )
134 if (esmf_logfounderror(rctocheck=rc, msg=esmf_logerr_passthru, &
139 call esmf_timeget(currtime, yy=nyy, mm=nmm, dd=ndd, s=nsec, rc=rc )
140 if (esmf_logfounderror(rctocheck=rc, msg=esmf_logerr_passthru, &
146 if (
present(reftime))
then
153 call esmf_clockget(clock, calendar=cal, rc=rc)
154 if (esmf_logfounderror(rctocheck=rc, msg=esmf_logerr_passthru, &
160 selectcase (trim(option))
163 call esmf_timeintervalset(alarminterval, yy=9999, rc=rc)
164 if (esmf_logfounderror(rctocheck=rc, msg=esmf_logerr_passthru, &
168 call esmf_timeset( nextalarm, yy=9999, mm=12, dd=1, s=0, calendar=cal, rc=rc )
169 if (esmf_logfounderror(rctocheck=rc, msg=esmf_logerr_passthru, &
173 update_nextalarm = .false.
176 if (.not.
present(opt_ymd))
then
177 call esmf_logseterror(esmf_rc_arg_bad, &
178 msg=subname//trim(option)//
' requires opt_ymd', &
180 file=__file__, rctoreturn=rc)
183 if (lymd < 0 .or. ltod < 0)
then
184 call esmf_logseterror(esmf_rc_arg_bad, &
185 msg=subname//trim(option)//
'opt_ymd, opt_tod invalid', &
187 file=__file__, rctoreturn=rc)
190 call esmf_timeintervalset(alarminterval, yy=9999, rc=rc)
191 if (esmf_logfounderror(rctocheck=rc, msg=esmf_logerr_passthru, &
195 call timeinit(nextalarm, lymd, cal, tod=ltod, desc=
"optDate", rc=rc)
196 if (esmf_logfounderror(rctocheck=rc, msg=esmf_logerr_passthru, &
200 update_nextalarm = .false.
203 if (.not.
present(opt_ymd))
then
204 call esmf_logseterror(esmf_rc_arg_bad, &
205 msg=subname//trim(option)//
' requires opt_ymd', &
207 file=__file__, rctoreturn=rc)
210 call esmf_timeintervalset(alarminterval, mm=1, rc=rc)
211 if (esmf_logfounderror(rctocheck=rc, msg=esmf_logerr_passthru, &
215 call esmf_timeset( nextalarm, yy=cyy, mm=cmm, dd=opt_n, s=0, calendar=cal, rc=rc )
216 if (esmf_logfounderror(rctocheck=rc, msg=esmf_logerr_passthru, &
220 update_nextalarm = .true.
223 call esmf_clockget(clock, timestep=alarminterval, rc=rc)
224 if (esmf_logfounderror(rctocheck=rc, msg=esmf_logerr_passthru, &
228 alarminterval = alarminterval * opt_n
229 update_nextalarm = .true.
232 call esmf_timeintervalset(alarminterval, s=1, rc=rc)
233 if (esmf_logfounderror(rctocheck=rc, msg=esmf_logerr_passthru, &
237 alarminterval = alarminterval * opt_n
238 update_nextalarm = .true.
241 call esmf_timeintervalset(alarminterval, s=60, rc=rc)
242 if (esmf_logfounderror(rctocheck=rc, msg=esmf_logerr_passthru, &
246 alarminterval = alarminterval * opt_n
247 update_nextalarm = .true.
250 call esmf_timeintervalset(alarminterval, s=3600, rc=rc)
251 if (esmf_logfounderror(rctocheck=rc, msg=esmf_logerr_passthru, &
255 alarminterval = alarminterval * opt_n
256 update_nextalarm = .true.
259 call esmf_timeintervalset(alarminterval, d=1, rc=rc)
260 if (esmf_logfounderror(rctocheck=rc, msg=esmf_logerr_passthru, &
264 alarminterval = alarminterval * opt_n
265 update_nextalarm = .true.
268 call esmf_timeintervalset(alarminterval, mm=1, rc=rc)
269 if (esmf_logfounderror(rctocheck=rc, msg=esmf_logerr_passthru, &
273 alarminterval = alarminterval * opt_n
274 update_nextalarm = .true.
277 call esmf_timeintervalset(alarminterval, mm=1, rc=rc)
278 if (esmf_logfounderror(rctocheck=rc, msg=esmf_logerr_passthru, &
282 call esmf_timeset( nextalarm, yy=cyy, mm=cmm, dd=1, s=0, calendar=cal, rc=rc )
283 if (esmf_logfounderror(rctocheck=rc, msg=esmf_logerr_passthru, &
287 update_nextalarm = .true.
290 call esmf_timeintervalset(alarminterval, yy=1, rc=rc)
291 if (esmf_logfounderror(rctocheck=rc, msg=esmf_logerr_passthru, &
295 alarminterval = alarminterval * opt_n
296 update_nextalarm = .true.
299 call esmf_timeintervalset(alarminterval, yy=1, rc=rc)
300 if (esmf_logfounderror(rctocheck=rc, msg=esmf_logerr_passthru, &
304 call esmf_timeset( nextalarm, yy=cyy, mm=1, dd=1, s=0, calendar=cal, rc=rc )
305 if (esmf_logfounderror(rctocheck=rc, msg=esmf_logerr_passthru, &
309 update_nextalarm = .true.
312 call esmf_logseterror(esmf_rc_arg_bad, &
313 msg=subname//
' unknown option: '//trim(option), &
315 file=__file__, rctoreturn=rc)
327 if (update_nextalarm)
then
328 nextalarm = nextalarm - alarminterval
329 do while (nextalarm <= currtime)
330 nextalarm = nextalarm + alarminterval
334 alarm = esmf_alarmcreate( name=lalarmname, clock=clock, ringtime=nextalarm, ringinterval=alarminterval, rc=rc)
335 if (esmf_logfounderror(rctocheck=rc, msg=esmf_logerr_passthru, &
345 subroutine timeinit( Time, ymd, cal, tod, desc, logunit, rc)
346 type(esmf_time) ,
intent(inout) :: time
347 integer ,
intent(in) :: ymd
348 type(esmf_calendar) ,
intent(in) :: cal
349 integer ,
intent(in),
optional :: tod
350 character(len=*) ,
intent(in),
optional :: desc
351 integer ,
intent(in),
optional :: logunit
352 integer ,
intent(out),
optional :: rc
355 integer :: yr, mon, day
357 character(len=256) :: ldesc
358 character(len=*),
parameter :: subname =
'(TimeInit) '
362 if (
present(tod)) ltod = tod
364 if (
present(desc)) ldesc = desc
366 if ( (ymd < 0) .or. (ltod < 0) .or. (ltod >
secperday) )
then
367 if (
present(logunit))
then
368 write(logunit,*) subname//
': ERROR yymmdd is a negative number or '// &
369 'time-of-day out of bounds', ymd, ltod
371 call esmf_logseterror(esmf_rc_arg_bad, &
372 msg=subname//
' yymmdd is negative or time-of-day out of bounds ', &
374 file=__file__, rctoreturn=rc)
380 call esmf_timeset( time, yy=yr, mm=mon, dd=day, s=ltod, calendar=cal, rc=rc )
381 if (esmf_logfounderror(rctocheck=rc, msg=esmf_logerr_passthru, &
389 subroutine date2ymd (date, year, month, day)
390 integer,
intent(in) :: date
391 integer,
intent(out) :: year,month,day
395 character(*),
parameter :: subname =
"(date2ymd)"
399 year = int(tdate/10000)
403 month = int( mod(tdate,10000)/ 100)
404 day = mod(tdate, 100)