Skip to content

Commit

Permalink
xclim 0.48 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
RondeauG committed Feb 21, 2024
1 parent 30c5507 commit 824c00c
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 33 deletions.
6 changes: 3 additions & 3 deletions environment-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: xscen-dev
channels:
- conda-forge
dependencies:
- python >=3.9
- python >=3.9,<3.13
# Don't forget to sync changes between environment.yml, environment-dev.yml, and setup.py!
# Also consider updating the list in xs.utils.show_versions if you add a new package.
# Main packages
Expand All @@ -21,15 +21,15 @@ dependencies:
- netCDF4
- numcodecs
- numpy
- pandas >=2.0,<2.2
- pandas >=2.2
- parse
- pyyaml
- rechunker
- scipy
- shapely >=2.0
- sparse
- toolz
- xarray <2023.11.0 # FIXME: Remove when pandas 2.2 is released and xclim is fixed.
- xarray
- xclim >=0.46.0
- xesmf >=0.7
- zarr
Expand Down
6 changes: 3 additions & 3 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: xscen
channels:
- conda-forge
dependencies:
- python >=3.9
- python >=3.9,<3.13
# Don't forget to sync changes between environment.yml, environment-dev.yml, and setup.py!
# Also consider updating the list in xs.utils.show_versions if you add a new package.
# Main packages
Expand All @@ -21,15 +21,15 @@ dependencies:
- netCDF4
- numcodecs
- numpy
- pandas >=2.0,<2.2
- pandas >=2.2
- parse
- pyyaml
- rechunker
- scipy
- shapely >=2.0
- sparse
- toolz
- xarray <2023.11.0 # FIXME: Remove when pandas 2.2 is released and xclim is fixed.
- xarray
- xclim >=0.46.0
- xesmf >=0.7
- zarr
Expand Down
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ classifiers = [
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
# "Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: Implementation :: CPython",
"Topic :: Scientific/Engineering :: Atmospheric Science"
]
Expand All @@ -52,7 +52,7 @@ dependencies = [
"netCDF4",
"numcodecs",
"numpy",
"pandas>=2.0,<2.2",
"pandas>=2.2",
"parse",
# Used when opening catalogs.
"pyarrow",
Expand All @@ -62,7 +62,7 @@ dependencies = [
"shapely>=2.0",
"sparse",
"toolz",
"xarray<2023.11.0", # FIXME: Remove when pandas 2.2 is released and xclim is fixed.
"xarray",
"xclim>=0.46.0",
"zarr"
]
Expand Down
4 changes: 2 additions & 2 deletions tests/test_catutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ def test_build_path(samplecat):

def test_build_path_ds():
ds = xr.tutorial.open_dataset("air_temperature")
ds = ds.assign(time=xr.cftime_range("0001-01-01", freq="6H", periods=ds.time.size))
ds = ds.assign(time=xr.cftime_range("0001-01-01", freq="6h", periods=ds.time.size))
ds.attrs.update(source="source", institution="institution")
new_path = cu.build_path(
ds,
Expand All @@ -240,7 +240,7 @@ def test_build_path_ds():
},
)
assert new_path == Path(
"source/institution/air_6H/source_institution_air_6hr_0001-0002"
"source/institution/air_6h/source_institution_air_6hr_0001-0002"
)


Expand Down
4 changes: 2 additions & 2 deletions tests/test_diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def test_variables(self):
xs.diagnostics.health_checks(ds, variables_and_units={"tas": "degC"})
with pytest.warns(
UserWarning,
match="Data units kelvin are not compatible with requested mm.",
match="are not compatible with requested mm.",
):
xs.diagnostics.health_checks(ds, variables_and_units={"tas": "mm"})

Expand Down Expand Up @@ -162,7 +162,7 @@ def test_cfchecks(self):
xs.diagnostics.health_checks(ds, cfchecks=bad_cfcheck)

@pytest.mark.parametrize(
"freq, gap", [("D", False), ("MS", False), ("3H", False), ("D", True)]
"freq, gap", [("D", False), ("MS", False), ("3h", False), ("D", True)]
)
def test_freq(self, freq, gap):
ds = timeseries(
Expand Down
18 changes: 9 additions & 9 deletions tests/test_indicators.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ def test_input_types(self, input):
def test_level(self, to_level):
if to_level is None:
ind_dict = xs.compute_indicators(self.ds, indicators=self.yaml_file)
assert "indicators" in ind_dict["AS-JAN"].attrs["cat:processing_level"]
assert "indicators" in ind_dict["YS-JAN"].attrs["cat:processing_level"]
else:
ind_dict = xs.compute_indicators(
self.ds, indicators=self.yaml_file, to_level=to_level
)
assert to_level in ind_dict["AS-JAN"].attrs["cat:processing_level"]
assert to_level in ind_dict["YS-JAN"].attrs["cat:processing_level"]

# Periods needed to cover both branches of the if/else in compute_indicators, so no need to test separately.
@pytest.mark.parametrize(
Expand All @@ -76,13 +76,13 @@ def test_output(self, periods):
assert all(
xrfreq in ind_dict[xrfreq].attrs["cat:xrfreq"] for xrfreq in ind_dict.keys()
)
assert all(v in ind_dict["AS-JAN"] for v in ["tg_min", "growing_degree_days"])
assert all(v in ind_dict["YS-JAN"] for v in ["tg_min", "growing_degree_days"])
if periods is None:
assert "time" not in ind_dict["fx"].dims
assert len(ind_dict["AS-JAN"].time) == 2
assert len(ind_dict["YS-JAN"].time) == 2
else:
assert len(ind_dict["fx"].time) == len(periods)
assert len(ind_dict["AS-JAN"].time) == 8
assert len(ind_dict["YS-JAN"].time) == 8

def test_qs_dec(self):
indicator = xclim.core.indicator.Indicator.from_dict(
Expand Down Expand Up @@ -118,7 +118,7 @@ def test_multiple_outputs(self, periods):
)

assert all(
v in ind_dict["AS-JAN"]
v in ind_dict["YS-JAN"]
for v in [
"prcpavg",
"rain_season_start",
Expand All @@ -130,7 +130,7 @@ def test_multiple_outputs(self, periods):
@pytest.mark.parametrize("restrict_years", [True, False])
def test_as_jul(self, restrict_years):
indicator = xclim.core.indicator.Indicator.from_dict(
data={"base": "freezing_degree_days", "parameters": {"freq": "AS-JUL"}},
data={"base": "freezing_degree_days", "parameters": {"freq": "YS-JUL"}},
identifier="degree_days_below_0_annual_start_july",
module="tests",
)
Expand All @@ -140,8 +140,8 @@ def test_as_jul(self, restrict_years):
restrict_years=restrict_years,
)

assert "AS-JUL" in ind_dict["AS-JUL"].attrs["cat:xrfreq"]
out = ind_dict["AS-JUL"]
assert "YS-JUL" in ind_dict["YS-JUL"].attrs["cat:xrfreq"]
out = ind_dict["YS-JUL"]
if restrict_years:
assert len(out.time) == 3 # same length as input ds
assert out.time[0].dt.strftime("%Y-%m-%d").item() == "2001-07-01"
Expand Down
6 changes: 3 additions & 3 deletions xscen/CVs/frequency_to_timedelta.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"1hr": "1H",
"3hr": "3H",
"6hr": "6H",
"1hr": "1h",
"3hr": "3h",
"6hr": "6h",
"day": "1D",
"sem": "1W",
"2sem": "2W",
Expand Down
6 changes: 3 additions & 3 deletions xscen/CVs/frequency_to_xrfreq.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"1hr": "H",
"3hr": "3H",
"6hr": "6H",
"1hr": "h",
"3hr": "3h",
"6hr": "6h",
"day": "D",
"sem": "W",
"2sem": "2W",
Expand Down
7 changes: 5 additions & 2 deletions xscen/CVs/xrfreq_to_frequency.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
{
"is_regex": true,
"h": "1hr",
"H": "1hr",
"3h": "3hr",
"3H": "3hr",
"6h": "6hr",
"6H": "6hr",
"D": "day",
"W": "sem",
"2W": "2sem",
"14d": "2sem",
"14D": "2sem",
"M.*": "mon",
"Q.*": "qtr",
"2Q.*": "6mon",
"A.*": "yr",
"YS": "yr",
"Y.*": "yr",
"fx": "fx"
}
5 changes: 4 additions & 1 deletion xscen/CVs/xrfreq_to_timedelta.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{
"is_regex": true,
"h": "1h",
"H": "1H",
"3h": "3h",
"3H": "3H",
"6h": "6h",
"6H": "6H",
"D": "1D",
"W": "7D",
Expand All @@ -10,6 +13,6 @@
"Q.*": "90D",
"2Q.*": "180D",
"A.*": "365D",
"YS": "365D",
"Y.*": "365D",
"fx": "NAN"
}
4 changes: 2 additions & 2 deletions xscen/indicators.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def _infer_freq_from_meta(ind):
else:
freq = "fx"
if freq == "YS":
freq = "AS-JAN" # To fix an inconsistency in xclim default 'freq'
freq = "YS-JAN"

else:
# Multiple time periods to concatenate
Expand All @@ -197,7 +197,7 @@ def _infer_freq_from_meta(ind):
freq = "fx"

if freq == "YS":
freq = "AS-JAN" # To fix an inconsistency in xclim default 'freq'
freq = "YS-JAN"
# In order to concatenate time periods, the indicator still needs a time dimension
if freq == "fx":
tmp = tmp.assign_coords({"time": ds_subset.time[0]})
Expand Down

0 comments on commit 824c00c

Please sign in to comment.