Skip to content

Commit 30d6ddf

Browse files
Codexilaflott
andauthored
refactor: compare calendars via helper
Co-authored-by: ilaflott <6273252+ilaflott@users.noreply.github.com>
1 parent dc9605b commit 30d6ddf

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

fremorizer/cmor_helpers.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,19 @@ def normalize_calendar(calendar: Optional[str]) -> Optional[str]:
8080
calendar_lc = str(calendar).lower()
8181
return CF_CALENDAR_ALIASES.get(calendar_lc, calendar_lc)
8282

83+
def calendars_are_equivalent(cal1: Optional[str], cal2: Optional[str]) -> bool:
84+
"""
85+
Return True if two CF calendar names refer to the same calendar, accounting for aliases.
86+
87+
:param cal1: First CF calendar name (case-insensitive).
88+
:type cal1: str, optional
89+
:param cal2: Second CF calendar name (case-insensitive).
90+
:type cal2: str, optional
91+
:return: True if both names refer to the same calendar, False otherwise.
92+
:rtype: bool
93+
"""
94+
return normalize_calendar(cal1) == normalize_calendar(cal2)
95+
8396
def get_time_calendar_value(time_var) -> Optional[str]:
8497
"""
8598
Read a time variable's calendar/calendar_type attribute and normalize aliases.

fremorizer/cmor_mixer.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,12 @@ def rewrite_netcdf_file_var( mip_var_cfgs: dict = None,
200200
"this output could have the wrong calendar!")
201201
else:
202202
with open(json_exp_config, "r", encoding="utf-8") as file:
203-
exp_cfg_calendar = normalize_calendar(json.load(file)['calendar'])
204-
normalized_time_calendar = normalize_calendar(time_coords_calendar)
205-
if normalized_time_calendar != exp_cfg_calendar:
206-
raise ValueError(f"data calendar type {normalized_time_calendar} "
207-
f"does not match input config calendar type: {exp_cfg_calendar}")
203+
exp_cfg_calendar = json.load(file)['calendar']
204+
if not calendars_are_equivalent(time_coords_calendar, exp_cfg_calendar):
205+
norm_time = normalize_calendar(time_coords_calendar)
206+
norm_cfg = normalize_calendar(exp_cfg_calendar)
207+
raise ValueError(f"data calendar type {norm_time} "
208+
f"does not match input config calendar type: {norm_cfg}")
208209

209210
# read in time_bnds, if present
210211
fre_logger.info('attempting to read coordinate BNDS, time_bnds')

fremorizer/tests/test_cmor_helpers_update_calendar.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,12 @@ def test_normalize_calendar_aliases_and_passthrough():
141141
assert normalize_calendar("julian") == "julian"
142142
assert normalize_calendar("CustomCalendar") == "customcalendar"
143143
assert normalize_calendar(None) is None
144+
assert calendars_are_equivalent("noleap", "365_day") is True
145+
assert calendars_are_equivalent("standard", "gregorian") is True
146+
assert calendars_are_equivalent("all_leap", "366_day") is True
147+
assert calendars_are_equivalent("julian", "julian") is True
148+
assert calendars_are_equivalent("unknown", "unknown") is True
149+
assert calendars_are_equivalent("unknown", "gregorian") is False
144150

145151

146152
class _FakeTime:

0 commit comments

Comments
 (0)