MOM6
time_utils.F90
Go to the documentation of this file.
1 !> Set of time utilities for converting between FMS and ESMF time type.
3 
4 ! FMS
5 use fms_mod, only: uppercase
6 use mpp_mod, only: mpp_error, fatal
7 use time_manager_mod, only: time_type, set_time, set_date, get_date
8 use time_manager_mod, only: gregorian, julian, noleap, thirty_day_months, no_calendar
9 use time_manager_mod, only: fms_get_calendar_type => get_calendar_type
10 ! ESMF
11 use esmf, only: esmf_calkind_flag, esmf_calkind_gregorian
12 use esmf, only: esmf_calkind_julian, esmf_calkind_noleap
13 use esmf, only: esmf_calkind_360day, esmf_calkind_nocalendar
14 use esmf, only: esmf_time, esmf_timeget, esmf_logfounderror
15 use esmf, only: esmf_logerr_passthru,esmf_timeinterval
16 use esmf, only: esmf_timeintervalget, esmf_timeset, esmf_success
17 
18 implicit none; private
19 
20 !> Converts calendar from FMS to ESMF format
21 interface fms2esmf_cal
22  module procedure fms2esmf_cal_c
23  module procedure fms2esmf_cal_i
24 end interface fms2esmf_cal
25 
26 !> Converts time from FMS to ESMF format
27 interface esmf2fms_time
28  module procedure esmf2fms_time_t
29  module procedure esmf2fms_timestep
30 end interface esmf2fms_time
31 
32 public fms2esmf_cal
33 public esmf2fms_time
34 public fms2esmf_time
35 public string_to_date
36 
37 contains
38 
39 !> Sets fms2esmf_cal_c to the corresponding ESMF calendar type
40 function fms2esmf_cal_c(calendar)
41  type(esmf_calkind_flag) :: fms2esmf_cal_c !< ESMF calendar type
42  character(len=*), intent(in) :: calendar !< Type of calendar
43 
44  select case( uppercase(trim(calendar)) )
45  case( 'GREGORIAN' )
46  fms2esmf_cal_c = esmf_calkind_gregorian
47  case( 'JULIAN' )
48  fms2esmf_cal_c = esmf_calkind_julian
49  case( 'NOLEAP' )
50  fms2esmf_cal_c = esmf_calkind_noleap
51  case( 'THIRTY_DAY' )
52  fms2esmf_cal_c = esmf_calkind_360day
53  case( 'NO_CALENDAR' )
54  fms2esmf_cal_c = esmf_calkind_nocalendar
55  case default
56  call mpp_error(fatal, &
57  'ocean_solo: ocean_solo_nml entry calendar must be one of GREGORIAN|JULIAN|NOLEAP|THIRTY_DAY|NO_CALENDAR.' )
58  end select
59 end function fms2esmf_cal_c
60 
61 !> Sets fms2esmf_cal_i to the corresponding ESMF calendar type
62 function fms2esmf_cal_i(calendar)
63  type(esmf_calkind_flag) :: fms2esmf_cal_i !< ESMF calendar structure
64  integer, intent(in) :: calendar !< Type of calendar
65 
66  select case(calendar)
67  case(thirty_day_months)
68  fms2esmf_cal_i = esmf_calkind_360day
69  case(gregorian)
70  fms2esmf_cal_i = esmf_calkind_gregorian
71  case(julian)
72  fms2esmf_cal_i = esmf_calkind_julian
73  case(noleap)
74  fms2esmf_cal_i = esmf_calkind_noleap
75  case(no_calendar)
76  fms2esmf_cal_i = esmf_calkind_nocalendar
77  end select
78 end function fms2esmf_cal_i
79 
80 !> Converts date from ESMF format to FMS format.
81 function esmf2fms_time_t(time)
82  type(time_type) :: esmf2fms_time_t !< FMS time structure
83  type(esmf_time), intent(in) :: time !< ESMF time structure
84 
85  ! Local Variables
86  integer :: yy, mm, dd, h, m, s
87  type(esmf_calkind_flag) :: calkind
88 
89  integer :: rc
90 
91  call esmf_timeget(time, yy=yy, mm=mm, dd=dd, h=h, m=m, s=s, &
92  calkindflag=calkind, rc=rc)
93  if (esmf_logfounderror(rctocheck=rc, msg=esmf_logerr_passthru, &
94  line=__line__, &
95  file=__file__)) &
96  return ! bail out
97 
98  esmf2fms_time_t = set_date(yy, mm, dd, h, m, s)
99 
100 end function esmf2fms_time_t
101 
102 !> Converts time-interval from ESMF format to FMS format.
103 function esmf2fms_timestep(timestep)
104  type(time_type) :: esmf2fms_timestep !< FMS time structure
105  type(esmf_timeinterval), intent(in):: timestep !< time-interval following
106  !! ESMF format [s]
107  ! Local Variables
108  integer :: s
109  type(esmf_calkind_flag) :: calkind
110 
111  integer :: rc
112 
113  call esmf_timeintervalget(timestep, s=s, calkindflag=calkind, rc=rc)
114  if (esmf_logfounderror(rctocheck=rc, msg=esmf_logerr_passthru, &
115  line=__line__, &
116  file=__file__)) &
117  return ! bail out
118 
119  esmf2fms_timestep = set_time(s, 0)
120 
121 end function esmf2fms_timestep
122 
123 !> Converts date from FMS format to ESMF format.
124 function fms2esmf_time(time, calkind)
125  type(esmf_time) :: fms2esmf_time !< ESMF time structure
126  type(time_type), intent(in) :: time !< FMS time structure
127  type(esmf_calkind_flag), intent(in), optional :: calkind !< ESMF calendar structure
128 
129  ! Local Variables
130  integer :: yy, mm, d, h, m, s
131  type(esmf_calkind_flag) :: l_calkind
132 
133  integer :: rc
134 
135  if(present(calkind)) then
136  l_calkind = calkind
137  else
138  l_calkind = fms2esmf_cal(fms_get_calendar_type())
139  endif
140 
141  call get_date(time, yy, mm, d, h, m, s)
142 
143  call esmf_timeset(fms2esmf_time, yy=yy, mm=mm, d=d, h=h, m=m, s=s, &
144  calkindflag=l_calkind, rc=rc)
145  if (esmf_logfounderror(rctocheck=rc, msg=esmf_logerr_passthru, &
146  line=__line__, &
147  file=__file__)) &
148  return ! bail out
149 
150 end function fms2esmf_time
151 
152 !> Converts a string (I4.4,I2.2,I2.2,".",I2.2,I2.2,I2.2) that represents
153 !! yr, mon, day, hr, min, sec to a FMS data format.
154 function string_to_date(string, rc)
155  character(len=15), intent(in) :: string !< String representing a date
156  integer, intent(out), optional :: rc !< ESMF error handler
157  type(time_type) :: string_to_date!< FMS time structure
158 
159  ! Local variables
160  integer :: yr,mon,day,hr,min,sec
161 
162  if(present(rc)) rc = esmf_success
163 
164  read(string, '(I4.4,I2.2,I2.2,".",I2.2,I2.2,I2.2)') yr, mon, day, hr, min, sec
165  string_to_date = set_date(yr, mon, day, hr, min, sec)
166 
167 end function string_to_date
168 
169 end module time_utils_mod
time_utils_mod::fms2esmf_time
type(esmf_time) function, public fms2esmf_time(time, calkind)
Converts date from FMS format to ESMF format.
Definition: time_utils.F90:125
time_utils_mod::esmf2fms_time_t
type(time_type) function esmf2fms_time_t(time)
Converts date from ESMF format to FMS format.
Definition: time_utils.F90:82
time_utils_mod::esmf2fms_timestep
type(time_type) function esmf2fms_timestep(timestep)
Converts time-interval from ESMF format to FMS format.
Definition: time_utils.F90:104
time_utils_mod::string_to_date
type(time_type) function, public string_to_date(string, rc)
Converts a string (I4.4,I2.2,I2.2,".",I2.2,I2.2,I2.2) that represents yr, mon, day,...
Definition: time_utils.F90:155
time_utils_mod::fms2esmf_cal
Converts calendar from FMS to ESMF format.
Definition: time_utils.F90:21
time_utils_mod::fms2esmf_cal_c
type(esmf_calkind_flag) function fms2esmf_cal_c(calendar)
Sets fms2esmf_cal_c to the corresponding ESMF calendar type.
Definition: time_utils.F90:41
time_utils_mod
Set of time utilities for converting between FMS and ESMF time type.
Definition: time_utils.F90:2
time_utils_mod::esmf2fms_time
Converts time from FMS to ESMF format.
Definition: time_utils.F90:27
time_utils_mod::fms2esmf_cal_i
type(esmf_calkind_flag) function fms2esmf_cal_i(calendar)
Sets fms2esmf_cal_i to the corresponding ESMF calendar type.
Definition: time_utils.F90:63