Skip to content

Commit e581e93

Browse files
committed
Improve documentation
1 parent 16e27f9 commit e581e93

File tree

1 file changed

+45
-3
lines changed
  • packages/common/src/weathergen/common

1 file changed

+45
-3
lines changed

packages/common/src/weathergen/common/io.py

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,30 +37,66 @@ def is_ndarray(obj: typing.Any) -> bool:
3737

3838

3939
class TimeRange:
40+
"""
41+
Holds information about a time interval used in forecasting.
42+
43+
Time interval is left-closed, right-open. TimeRange can be instatiated from
44+
numpy datetime64 objects or strings as outputed by TimeRange.as_dict.
45+
Both will be converted to datetime64 with nanosecond precision.
46+
47+
Attrs:
48+
start: Start of the time range in nanoseconds.
49+
end: End of the time range in nanoseconds
50+
"""
51+
4052
def __init__(self, start: NPDT64 | str, end: NPDT64 | str):
4153
# ensure consistent type => convert serialized strings
4254
self.start = np.datetime64(start, "ns")
4355
self.end = np.datetime64(end, "ns")
4456

4557
assert self.start < self.end
4658

47-
def as_dict(self):
48-
"""Convert instance to a JSON-serializable dict."""
59+
def as_dict(self) -> dict[str, str]:
60+
"""
61+
Convert instance to a JSON-serializable dict.
62+
63+
will convert datetime objects as "YYYY-MM-DDThh:mm:s.sssssssss"
4964
50-
# will output as "YYYY-MM-DDThh:mm:s.sssssssss"
65+
Returns:
66+
JSON-serializable dict, wher datetime objects were converted to strings.
67+
"""
5168
return {
5269
"start": str(self.start),
5370
"end": str(self.end),
5471
}
5572

5673
def forecast_interval(self, forecast_dt_hours: int, fstep: int) -> "TimeRange":
74+
"""
75+
Infer the interval cosidered at forecast step `fstep`.
76+
77+
Args:
78+
forecast_dt_hours: number of hours the source TimeRange is shifted per forecast step.
79+
fstep: current forecast step.
80+
81+
Returns:
82+
New TimeRange shifted TimeRange.
83+
"""
5784
assert forecast_dt_hours > 0 and fstep >= 0
5885
offset = np.timedelta64(forecast_dt_hours * fstep, "h")
5986
return TimeRange(self.start + offset, self.end + offset)
6087

6188
def get_lead_time(
6289
self, abs_time: np.datetime64 | NDArray[np.datetime64]
6390
) -> NDArray[np.timedelta64]:
91+
"""
92+
Calculate lead times based on the end of the TimeRange.
93+
94+
Args:
95+
abs_time: Single timestamp or array of timestamps.
96+
97+
Returns:
98+
Array of time differences (lead times) for each input timestamp.
99+
"""
64100
if isinstance(abs_time, np.datetime64):
65101
abs_time = np.array([abs_time])
66102

@@ -191,6 +227,12 @@ class OutputDataset:
191227
def create(cls, name, key, arrays: dict[str, ArrayType], attrs: dict[str, typing.Any]):
192228
"""
193229
Create Output dataset from dictonaries.
230+
231+
Args:
232+
name: Name of dataset (target/prediction/source)
233+
item_key: ItemKey to associated with the parent OutputItem.
234+
arrays: Data and Coordinate arrays.
235+
attrs: Additional metadata.
194236
"""
195237
assert "source_interval" in attrs, "missing expected attribute 'source_interval'"
196238

0 commit comments

Comments
 (0)