@@ -8,64 +8,71 @@ module CallbackManager
8
8
9
9
import Dates
10
10
11
- export to_datetime,
12
- strdate_to_datetime,
13
- datetime_to_strdate,
14
- trigger_callback,
11
+ export HourlyCallback,
12
+ MonthlyCallback,
15
13
Monthly,
16
- EveryTimestep
14
+ EveryTimestep,
15
+ trigger_callback,
16
+ to_datetime,
17
+ strdate_to_datetime,
18
+ datetime_to_strdate
17
19
18
20
"""
19
- to_datetime(date)
21
+ AbstractCallback
22
+ """
23
+ abstract type AbstractCallback end
20
24
21
- Convert a `DateTime`-like object (e.g. `DateTimeNoLeap`) to a `DateTime`.
22
- We need this since some data files we use contain
23
- `DateTimeNoLeap` objects for dates, which can't be used for math with `DateTime`s.
24
- The `DateTimeNoLeap` type uses the Gregorian calendar without leap years, while
25
- the `DateTime` type uses Gregorian calendar with leap years.
25
+ """
26
+ HourlyCallback{FT}
26
27
27
- For consistency, all input data files should have dates converted to `DateTime`
28
- before being used in a simulation.
28
+ This is a callback type that triggers at intervals of 1h or multiple hours.
29
+ """
30
+ @kwdef struct HourlyCallback{FT} <: AbstractCallback
31
+ """ Time interval at which the callback is triggered. """
32
+ dt:: FT = FT (1 ) # hours
33
+ """ Function to be called at each trigger. """
34
+ func:: Function = do_nothing
35
+ """ Reference date for the callback. """
36
+ ref_date:: Array = [Dates. DateTime (0 )]
37
+ """ Whether the callback is active. """
38
+ active:: Bool = false
39
+ """ Data to be passed to the callback function. """
40
+ data:: Array = []
41
+ end
29
42
30
- This function is similar to `reinterpret` in CFTime.jl.
43
+ """
44
+ MonthlyCallback{FT}
31
45
32
- # Arguments
33
- - `date`: `DateTime`-like object to be converted to `DateTime`
46
+ This is a callback type that triggers at intervals of 1 month or multiple months.
34
47
"""
35
- function to_datetime (date)
36
- return Dates. DateTime (
37
- Dates. year (date),
38
- Dates. month (date),
39
- Dates. day (date),
40
- Dates. hour (date),
41
- Dates. minute (date),
42
- Dates. second (date),
43
- Dates. millisecond (date),
44
- )
48
+ @kwdef struct MonthlyCallback{FT} <: AbstractCallback
49
+ """ Time interval at which the callback is triggered. """
50
+ dt:: FT = FT (1 ) # months
51
+ """ Function to be called at each trigger. """
52
+ func:: Function = do_nothing
53
+ """ Reference date for the callback. """
54
+ ref_date:: Array = [Dates. DateTime (0 )]
55
+ """ Whether the callback is active. """
56
+ active:: Bool = false
57
+ """ Data to be passed to the callback function. """
58
+ data:: Array = []
45
59
end
46
60
47
61
"""
48
- strdate_to_datetime(strdate::String)
62
+ dt_cb(cb::HourlyCallback)
63
+ dt_cb(cb::MonthlyCallback)
49
64
50
- Convert from String ("YYYYMMDD") to Date format,
51
- required by the official AMIP input files.
65
+ This function returns the time interval for the callback.
52
66
"""
53
- strdate_to_datetime (strdate:: String ) = Dates. DateTime (
54
- parse (Int, strdate[1 : 4 ]),
55
- parse (Int, strdate[5 : 6 ]),
56
- parse (Int, strdate[7 : 8 ]),
57
- )
67
+ dt_cb (cb:: HourlyCallback ) = Dates. Hour (cb. dt)
68
+ dt_cb (cb:: MonthlyCallback ) = Dates. Month (cb. dt)
58
69
59
- """
60
- datetime_to_strdate(datetime::Dates.DateTime)
61
70
62
- Convert from DateTime to String ("YYYYMMDD") format.
63
71
"""
64
- datetime_to_strdate (datetime:: Dates.DateTime ) =
65
- string (lpad (Dates. year (datetime), 4 , " 0" )) *
66
- string (string (lpad (Dates. month (datetime), 2 , " 0" ))) *
67
- string (lpad (Dates. day (datetime), 2 , " 0" ))
72
+ AbstractFrequency
68
73
74
+ This is an abstract type for the frequency of a callback function.
75
+ """
69
76
abstract type AbstractFrequency end
70
77
struct Monthly <: AbstractFrequency end
71
78
struct EveryTimestep <: AbstractFrequency end
@@ -105,4 +112,56 @@ function trigger_callback(
105
112
end
106
113
end
107
114
115
+ """
116
+ to_datetime(date)
117
+
118
+ Convert a `DateTime`-like object (e.g. `DateTimeNoLeap`) to a `DateTime`.
119
+ We need this since some data files we use contain
120
+ `DateTimeNoLeap` objects for dates, which can't be used for math with `DateTime`s.
121
+ The `DateTimeNoLeap` type uses the Gregorian calendar without leap years, while
122
+ the `DateTime` type uses Gregorian calendar with leap years.
123
+
124
+ For consistency, all input data files should have dates converted to `DateTime`
125
+ before being used in a simulation.
126
+
127
+ This function is similar to `reinterpret` in CFTime.jl.
128
+
129
+ # Arguments
130
+ - `date`: `DateTime`-like object to be converted to `DateTime`
131
+ """
132
+ function to_datetime (date)
133
+ return Dates. DateTime (
134
+ Dates. year (date),
135
+ Dates. month (date),
136
+ Dates. day (date),
137
+ Dates. hour (date),
138
+ Dates. minute (date),
139
+ Dates. second (date),
140
+ Dates. millisecond (date),
141
+ )
142
+ end
143
+
144
+ """
145
+ strdate_to_datetime(strdate::String)
146
+
147
+ Convert from String ("YYYYMMDD") to Date format,
148
+ required by the official AMIP input files.
149
+ """
150
+ strdate_to_datetime (strdate:: String ) = Dates. DateTime (
151
+ parse (Int, strdate[1 : 4 ]),
152
+ parse (Int, strdate[5 : 6 ]),
153
+ parse (Int, strdate[7 : 8 ]),
154
+ )
155
+
156
+ """
157
+ datetime_to_strdate(datetime::Dates.DateTime)
158
+
159
+ Convert from DateTime to String ("YYYYMMDD") format.
160
+ """
161
+ datetime_to_strdate (datetime:: Dates.DateTime ) =
162
+ string (lpad (Dates. year (datetime), 4 , " 0" )) *
163
+ string (string (lpad (Dates. month (datetime), 2 , " 0" ))) *
164
+ string (lpad (Dates. day (datetime), 2 , " 0" ))
165
+
166
+
108
167
end # module CallbackManager
0 commit comments