Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support year in date formatting #7

Merged
merged 4 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/carpet_concentrations/input4MIPs/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,12 +467,13 @@ def format_date(
-------
Formatted date
"""
if ds_frequency.endswith("mon"):
datestring = date.strftime("%Y%m")
else:
raise NotImplementedError(ds_frequency)
if ds_frequency.startswith("mon"):
return date.strftime("%Y%m")

if ds_frequency.startswith("yr"):
return date.strftime("%Y")

return datestring
raise NotImplementedError(ds_frequency)


def get_version(creation_date: str) -> str:
Expand Down
21 changes: 19 additions & 2 deletions tests/unit/test_input4mips.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""
import re

import cftime
import pytest
import xarray as xr

Expand Down Expand Up @@ -173,9 +174,25 @@ def test_from_metadata_autoadd_bounds_to_dimensions_multivar():
)


@pytest.mark.parametrize(
"date, freq, exp",
(
(cftime.DatetimeGregorian(2012, 1, 3), "mon", "201201"),
(cftime.DatetimeGregorian(2012, 4, 1), "mon", "201204"),
(cftime.DatetimeGregorian(2022, 1, 3), "yr", "2022"),
(cftime.DatetimeGregorian(2022, 11, 13), "yr", "2022"),
(cftime.DatetimeGregorian(1, 11, 13), "mon", "000111"),
(cftime.DatetimeGregorian(1, 11, 13), "yr", "0001"),
),
)
def test_format_date(date, freq, exp):
res = format_date(date, ds_frequency=freq)
assert res == exp


def test_format_date_not_implemented():
with pytest.raises(NotImplementedError, match="yr"):
format_date("mock", ds_frequency="yr")
with pytest.raises(NotImplementedError, match="junk"):
format_date("mock", ds_frequency="junk")


def test_add_time_bounds_name_clash():
Expand Down