Skip to content

Commit

Permalink
Merge pull request #7 from climate-resource/yr-support
Browse files Browse the repository at this point in the history
Support year in date formatting
  • Loading branch information
znichollscr authored Dec 12, 2023
2 parents 06b8db0 + 75b7cfe commit ee854f0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
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

0 comments on commit ee854f0

Please sign in to comment.