Skip to content

Commit 08a6b2b

Browse files
cvanelterenCopilotbeckermr
authored
Add cftime support for non-standard calendars (#344)
* add cftime formatter * add unittest * add converter and register units * Apply suggestion from @Copilot Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * add cftime dep * add more unittests * fix some tests * rm debug * add more points * revert regex * fix resolution * add more tests and redo resolution computation * fix f strings * add more tests for coverage * Update ultraplot/ticker.py Co-authored-by: Matthew R. Becker <beckermr@users.noreply.github.com> * rename classes * rename classes * Update ultraplot/ticker.py Co-authored-by: Matthew R. Becker <beckermr@users.noreply.github.com> * Update ultraplot/ticker.py Co-authored-by: Matthew R. Becker <beckermr@users.noreply.github.com> * safe casting for non-greg calendars * check that resolution is correct * check resolution in test_auto_datetime_locator_tick_values * setters to rc * add validators * replace real_world_calendar with internal cftime variable * use calendar attr lookup * mv max_display_ticks to rc and spelling * update defaults * update defaults * rename standard_unit time_unit * fix typo * remove debug * remove try block in test * get calendars from private var * rm type info from default data range * simplify type registry * Replace datetime specific objects with general datetime * More replacements * Update ultraplot/tests/test_tickers.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update ultraplot/ticker.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * rm resolution format in ticker * make standard a bit less extreme * make standard a bit less extreme * rm dead comment * more tests * refactor tests --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Matthew R. Becker <beckermr@users.noreply.github.com>
1 parent b1e2bdc commit 08a6b2b

File tree

4 files changed

+1464
-0
lines changed

4 files changed

+1464
-0
lines changed

environment.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,6 @@ dependencies:
2828
- sphinx-design
2929
- networkx
3030
- pyarrow
31+
- cftime
3132
- pip:
3233
- git+https://github.com/ultraplot/UltraTheme.git

ultraplot/internals/rcsetup.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,37 @@ def _validate_belongs(value): # noqa: E306
264264
return _validate_belongs
265265

266266

267+
_CFTIME_RESOLUTIONS = (
268+
"SECONDLY",
269+
"MINUTELY",
270+
"HOURLY",
271+
"DAILY",
272+
"MONTHLY",
273+
"YEARLY",
274+
)
275+
276+
277+
def _validate_cftime_resolution_format(units: dict) -> dict:
278+
if not isinstance(units, dict):
279+
raise ValueError("Cftime units expects a dict")
280+
281+
for resolution, format_ in units.items():
282+
unit = _validate_cftime_resolution(resolution)
283+
284+
# Delegate format parsing to cftime
285+
_rc_ultraplot_default["cftime.time_resolution_format"].update(units)
286+
return _rc_ultraplot_default["cftime.time_resolution_format"]
287+
288+
289+
def _validate_cftime_resolution(unit: str) -> str:
290+
if not isinstance(unit, str):
291+
raise TypeError("Time unit cftime is expecting str")
292+
if unit in _CFTIME_RESOLUTIONS:
293+
return unit
294+
msg = f"Unit not understood. Got {unit} expected one of: {_CFTIME_RESOLUTIONS}"
295+
raise ValueError(msg)
296+
297+
267298
def _validate_cmap(subtype):
268299
"""
269300
Validate the colormap or cycle. Possibly skip name registration check
@@ -997,6 +1028,33 @@ def copy(self):
9971028
_validate_fontweight,
9981029
"Font weight for column labels on the bottom of the figure.",
9991030
),
1031+
"cftime.time_unit": (
1032+
"days since 2000-01-01",
1033+
_validate_string,
1034+
"Time unit for non-Gregorian calendars.",
1035+
),
1036+
"cftime.resolution": (
1037+
"DAILY",
1038+
_validate_cftime_resolution,
1039+
"Default time resolution for non-Gregorian calendars.",
1040+
),
1041+
"cftime.time_resolution_format": (
1042+
{
1043+
"SECONDLY": "%S",
1044+
"MINUTELY": "%M",
1045+
"HOURLY": "%H",
1046+
"DAILY": "%d",
1047+
"MONTHLY": "%m",
1048+
"YEARLY": "%Y",
1049+
},
1050+
_validate_cftime_resolution_format,
1051+
"Dict used for formatting non-Gregorian calendars.",
1052+
),
1053+
"cftime.max_display_ticks": (
1054+
7,
1055+
_validate_int,
1056+
"Number of ticks to display for cftime units.",
1057+
),
10001058
# Coastlines
10011059
"coast": (False, _validate_bool, "Toggles coastline lines on and off."),
10021060
"coast.alpha": (

0 commit comments

Comments
 (0)