Skip to content

Commit 8b8ffcc

Browse files
committed
add functions for ClimaCoupler
1 parent 97be0c5 commit 8b8ffcc

File tree

2 files changed

+104
-43
lines changed

2 files changed

+104
-43
lines changed

docs/src/callbackmanager.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ functions from Julia's [Dates](https://docs.julialang.org/en/v1/stdlib/Dates/) m
77
## CallbackManager API
88

99
```@docs
10+
ClimaUtilities.CallbackManager.HourlyCallback
11+
ClimaUtilities.CallbackManager.MonthlyCallback
12+
ClimaUtilities.CallbackManager.Monthly
13+
ClimaUtilities.CallbackManager.EveryTimestep
1014
ClimaUtilities.CallbackManager.to_datetime
1115
ClimaUtilities.CallbackManager.strdate_to_datetime
1216
ClimaUtilities.CallbackManager.datetime_to_strdate
1317
ClimaUtilities.CallbackManager.trigger_callback
14-
ClimaUtilities.CallbackManager.Monthly
15-
ClimaUtilities.CallbackManager.EveryTimestep
1618
```

src/CallbackManager.jl

Lines changed: 100 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,64 +8,71 @@ module CallbackManager
88

99
import Dates
1010

11-
export to_datetime,
12-
strdate_to_datetime,
13-
datetime_to_strdate,
14-
trigger_callback,
11+
export HourlyCallback,
12+
MonthlyCallback,
1513
Monthly,
16-
EveryTimestep
14+
EveryTimestep,
15+
trigger_callback,
16+
to_datetime,
17+
strdate_to_datetime,
18+
datetime_to_strdate
1719

1820
"""
19-
to_datetime(date)
21+
AbstractCallback
22+
"""
23+
abstract type AbstractCallback end
2024

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}
2627
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
2942

30-
This function is similar to `reinterpret` in CFTime.jl.
43+
"""
44+
MonthlyCallback{FT}
3145
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.
3447
"""
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 = []
4559
end
4660

4761
"""
48-
strdate_to_datetime(strdate::String)
62+
dt_cb(cb::HourlyCallback)
63+
dt_cb(cb::MonthlyCallback)
4964
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.
5266
"""
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)
5869

59-
"""
60-
datetime_to_strdate(datetime::Dates.DateTime)
6170

62-
Convert from DateTime to String ("YYYYMMDD") format.
6371
"""
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
6873
74+
This is an abstract type for the frequency of a callback function.
75+
"""
6976
abstract type AbstractFrequency end
7077
struct Monthly <: AbstractFrequency end
7178
struct EveryTimestep <: AbstractFrequency end
@@ -105,4 +112,56 @@ function trigger_callback(
105112
end
106113
end
107114

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+
108167
end # module CallbackManager

0 commit comments

Comments
 (0)