From 6efd0bbde61cf5fccefc82ce10c8f2f10e38b6bd Mon Sep 17 00:00:00 2001 From: juliettelavoie Date: Fri, 12 Jul 2024 15:12:58 -0400 Subject: [PATCH 1/9] delta and dtr --- CHANGELOG.rst | 2 ++ src/xscen/aggregate.py | 6 ++++++ src/xscen/xclim_modules/conversions.py | 6 +++--- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index edd16644..f7d6dfed 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -6,6 +6,7 @@ v0.9.2 (unreleased) ------------------- Contributors to this version: Juliette Lavoie (:user:`juliettelavoie`), Pascal Bourgault (:user:`aulemahal`). + Bug fixes ^^^^^^^^^ * Fixed bug with reusing weights. (:issue:`411`, :pull:`414`). @@ -18,6 +19,7 @@ Internal changes * Added pins to `xarray`, `xclim`, `h5py`, and `netcdf4`. (:pull:`414`). * Add ``.zip`` and ``.zarr.zip`` as possible file extensions for Zarr datasets. (:pull:`426`). * Explicitly assign coords of multiindex in `xs.unstack_fill_nan`. (:pull:`427`). +* Include CF convention for temperature differences and on scale (:pull:`428`, :issue:`428`). v0.9.1 (2024-06-04) ------------------- diff --git a/src/xscen/aggregate.py b/src/xscen/aggregate.py index 7bf04dee..89ba6f44 100644 --- a/src/xscen/aggregate.py +++ b/src/xscen/aggregate.py @@ -31,6 +31,9 @@ from .spatial import subset from .utils import standardize_periods, unstack_dates, update_attr +# from xclim.core.units import pint2cfattrs, units2pint + + logger = logging.getLogger(__name__) __all__ = [ @@ -625,6 +628,9 @@ def compute_deltas( # noqa: C901 if (isinstance(kind, dict) and kind[vv] == "+") or kind == "+": _kind = "abs." deltas[v_name] = other_hz[vv] - ref[vv] + # will only work with xclim 0.52 + # unit=pint2cfattrs(units2pint(other_hz[vv].units),is_difference=True) + # deltas[v_name].attrs.update(unit) elif (isinstance(kind, dict) and kind[vv] == "/") or kind == "/": _kind = "rel." deltas[v_name] = other_hz[vv] / ref[vv] diff --git a/src/xscen/xclim_modules/conversions.py b/src/xscen/xclim_modules/conversions.py index 72ab7626..1668c8ac 100644 --- a/src/xscen/xclim_modules/conversions.py +++ b/src/xscen/xclim_modules/conversions.py @@ -103,8 +103,8 @@ def dtr_from_minmax(tasmin: xr.DataArray, tasmax: xr.DataArray) -> xr.DataArray: Daily temperature range """ # We force K to overcome the "delta degree" ambiguity - tasmin = convert_units_to(tasmin, "K") - tasmax = convert_units_to(tasmax, "K") + tasmin = convert_units_to(tasmin, tasmax) dtr = tasmax - tasmin - dtr.attrs["units"] = "K" + dtr.attrs["units"] = tasmin.attrs["units"] + dtr.attrs["units_metadata"] = "temperature: difference" return dtr From a916d899873c88c7367b765c198c090fed5bde4e Mon Sep 17 00:00:00 2001 From: juliettelavoie Date: Fri, 12 Jul 2024 15:17:37 -0400 Subject: [PATCH 2/9] test --- src/xscen/aggregate.py | 9 +++------ tests/test_aggregate.py | 1 + 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/xscen/aggregate.py b/src/xscen/aggregate.py index 89ba6f44..7af64fe7 100644 --- a/src/xscen/aggregate.py +++ b/src/xscen/aggregate.py @@ -24,6 +24,7 @@ except ImportError: xe = None from xclim.core.indicator import Indicator +from xclim.core.units import pint2cfattrs, units2pint from .config import parse_config from .extract import subset_warming_level @@ -31,9 +32,6 @@ from .spatial import subset from .utils import standardize_periods, unstack_dates, update_attr -# from xclim.core.units import pint2cfattrs, units2pint - - logger = logging.getLogger(__name__) __all__ = [ @@ -628,9 +626,8 @@ def compute_deltas( # noqa: C901 if (isinstance(kind, dict) and kind[vv] == "+") or kind == "+": _kind = "abs." deltas[v_name] = other_hz[vv] - ref[vv] - # will only work with xclim 0.52 - # unit=pint2cfattrs(units2pint(other_hz[vv].units),is_difference=True) - # deltas[v_name].attrs.update(unit) + unit = pint2cfattrs(units2pint(other_hz[vv].units), is_difference=True) + deltas[v_name].attrs.update(unit) elif (isinstance(kind, dict) and kind[vv] == "/") or kind == "/": _kind = "rel." deltas[v_name] = other_hz[vv] / ref[vv] diff --git a/tests/test_aggregate.py b/tests/test_aggregate.py index 0d7f10d3..f6163fcf 100644 --- a/tests/test_aggregate.py +++ b/tests/test_aggregate.py @@ -86,6 +86,7 @@ def test_options(self, kind, rename_variables, to_level): assert deltas[variable].attrs["delta_kind"] == delta_kind assert deltas[variable].attrs["delta_reference"] == "1981-2010" assert deltas[variable].attrs["units"] == units + assert deltas[variable].attrs["units_metadata"] == "temperature: difference" np.testing.assert_array_equal(deltas[variable], results) @pytest.mark.parametrize("cal", ["proleptic_gregorian", "noleap", "360_day"]) From 499f3cea9c22dc208ab93aef7e6d987bb3f069f9 Mon Sep 17 00:00:00 2001 From: juliettelavoie Date: Fri, 12 Jul 2024 15:19:14 -0400 Subject: [PATCH 3/9] attrs --- src/xscen/aggregate.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/xscen/aggregate.py b/src/xscen/aggregate.py index 7af64fe7..e4dc787c 100644 --- a/src/xscen/aggregate.py +++ b/src/xscen/aggregate.py @@ -626,7 +626,9 @@ def compute_deltas( # noqa: C901 if (isinstance(kind, dict) and kind[vv] == "+") or kind == "+": _kind = "abs." deltas[v_name] = other_hz[vv] - ref[vv] - unit = pint2cfattrs(units2pint(other_hz[vv].units), is_difference=True) + unit = pint2cfattrs( + units2pint(other_hz[vv].attrs["units"]), is_difference=True + ) deltas[v_name].attrs.update(unit) elif (isinstance(kind, dict) and kind[vv] == "/") or kind == "/": _kind = "rel." From 17a6bf9d03306e23653bc93037a0ce0a6d200005 Mon Sep 17 00:00:00 2001 From: juliettelavoie Date: Fri, 12 Jul 2024 15:24:56 -0400 Subject: [PATCH 4/9] test --- tests/test_xclimmod_conversions.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_xclimmod_conversions.py b/tests/test_xclimmod_conversions.py index 1de15b39..363fd429 100644 --- a/tests/test_xclimmod_conversions.py +++ b/tests/test_xclimmod_conversions.py @@ -58,4 +58,5 @@ def test_dtr(): dtr = conv.dtr_from_minmax(tasmin, tasmax) assert dtr.attrs["units"] == "K" + assert dtr.attrs["units"] == "temperature: difference" np.testing.assert_array_equal(dtr, (tasmax + 273.15) - tasmin) From 21b9014ffbd70fd392d55f8768871b9bb4f15bbe Mon Sep 17 00:00:00 2001 From: juliettelavoie Date: Fri, 12 Jul 2024 15:28:09 -0400 Subject: [PATCH 5/9] remove comment --- src/xscen/xclim_modules/conversions.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/xscen/xclim_modules/conversions.py b/src/xscen/xclim_modules/conversions.py index 1668c8ac..145fc851 100644 --- a/src/xscen/xclim_modules/conversions.py +++ b/src/xscen/xclim_modules/conversions.py @@ -99,10 +99,9 @@ def dtr_from_minmax(tasmin: xr.DataArray, tasmax: xr.DataArray) -> xr.DataArray: Returns ------- - xr.DataArray, K + xr.DataArray Daily temperature range """ - # We force K to overcome the "delta degree" ambiguity tasmin = convert_units_to(tasmin, tasmax) dtr = tasmax - tasmin dtr.attrs["units"] = tasmin.attrs["units"] From 79996f9d1689eb59fa988897486f31f88b2f3911 Mon Sep 17 00:00:00 2001 From: juliettelavoie Date: Thu, 24 Oct 2024 11:58:56 -0400 Subject: [PATCH 6/9] new xclim --- environment-dev.yml | 2 +- environment.yml | 2 +- pyproject.toml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/environment-dev.yml b/environment-dev.yml index 9d4434da..0ff5c03c 100644 --- a/environment-dev.yml +++ b/environment-dev.yml @@ -30,7 +30,7 @@ dependencies: - sparse - toolz - xarray >=2023.11.0, !=2024.6.0 - - xclim >=0.52.2, <0.53 + - xclim >=0.53.1, <0.54" - xesmf >=0.7 - zarr >=2.13 # Opt diff --git a/environment.yml b/environment.yml index e5336ab8..28e6daa3 100644 --- a/environment.yml +++ b/environment.yml @@ -30,7 +30,7 @@ dependencies: - sparse - toolz - xarray >=2023.11.0, !=2024.6.0 - - xclim >=0.52.2, <0.53 + - xclim >=0.53.1, <0.54" - xesmf >=0.7 - zarr >=2.13 # To install from source diff --git a/pyproject.toml b/pyproject.toml index d8771658..f2e810aa 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -65,7 +65,7 @@ dependencies = [ "sparse", "toolz", "xarray >=2023.11.0, !=2024.6.0", - "xclim >=0.52.2, <0.53", + "xclim >=0.53.1, <0.54", "zarr >=2.13" ] From 48188735838363774fb225c1bc9ea1b375e38a5a Mon Sep 17 00:00:00 2001 From: RondeauG Date: Mon, 4 Nov 2024 10:26:56 -0500 Subject: [PATCH 7/9] fix changelog --- CHANGELOG.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 33ca92d5..fa4361ad 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -24,6 +24,7 @@ Bug fixes Internal changes ^^^^^^^^^^^^^^^^ +* Include CF convention for temperature differences and on scale (:pull:`428`, :issue:`428`). * Bumped the version of `xclim` to 0.53.2. (:pull:`482`). v0.10.0 (2024-09-30) @@ -85,7 +86,6 @@ Internal changes * Added pins to `xarray`, `xclim`, `h5py`, and `netcdf4`. (:pull:`414`). * Add ``.zip`` and ``.zarr.zip`` as possible file extensions for Zarr datasets. (:pull:`426`). * Explicitly assign coords of multiindex in `xs.unstack_fill_nan`. (:pull:`427`). -* Include CF convention for temperature differences and on scale (:pull:`428`, :issue:`428`). * French translations are compiled offline. A new check ensures no PR are merged with missing messages. (:issue:`342`, :pull:`443`). * Continued work to add tests. (:pull:`450`). * Updated the cookiecutter template via `cruft`: (:pull:`452`) From 789fb9534683712512bf84453d544821d97a3de8 Mon Sep 17 00:00:00 2001 From: juliettelavoie Date: Mon, 4 Nov 2024 11:05:44 -0500 Subject: [PATCH 8/9] fix test --- tests/test_aggregate.py | 3 ++- tests/test_xclimmod_conversions.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/test_aggregate.py b/tests/test_aggregate.py index 05626d5a..023c8971 100644 --- a/tests/test_aggregate.py +++ b/tests/test_aggregate.py @@ -75,7 +75,8 @@ def test_options(self, kind, rename_variables, to_level): assert deltas[variable].attrs["delta_kind"] == delta_kind assert deltas[variable].attrs["delta_reference"] == "1981-2010" assert deltas[variable].attrs["units"] == units - assert deltas[variable].attrs["units_metadata"] == "temperature: difference" + if kind == "+": + assert deltas[variable].attrs["units_metadata"] == "temperature: difference" np.testing.assert_array_equal(deltas[variable], results) @pytest.mark.parametrize("cal", ["proleptic_gregorian", "noleap", "360_day"]) diff --git a/tests/test_xclimmod_conversions.py b/tests/test_xclimmod_conversions.py index 363fd429..7e2e60cd 100644 --- a/tests/test_xclimmod_conversions.py +++ b/tests/test_xclimmod_conversions.py @@ -57,6 +57,6 @@ def test_dtr(): ) dtr = conv.dtr_from_minmax(tasmin, tasmax) - assert dtr.attrs["units"] == "K" + assert dtr.attrs["units"] == "°C" assert dtr.attrs["units"] == "temperature: difference" np.testing.assert_array_equal(dtr, (tasmax + 273.15) - tasmin) From 63ee2198b06a010d302c12632dfe56a93be3bf3a Mon Sep 17 00:00:00 2001 From: juliettelavoie Date: Mon, 4 Nov 2024 11:13:27 -0500 Subject: [PATCH 9/9] typo --- tests/test_xclimmod_conversions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_xclimmod_conversions.py b/tests/test_xclimmod_conversions.py index 7e2e60cd..2576ea36 100644 --- a/tests/test_xclimmod_conversions.py +++ b/tests/test_xclimmod_conversions.py @@ -58,5 +58,5 @@ def test_dtr(): dtr = conv.dtr_from_minmax(tasmin, tasmax) assert dtr.attrs["units"] == "°C" - assert dtr.attrs["units"] == "temperature: difference" + assert dtr.attrs["units_metadata"] == "temperature: difference" np.testing.assert_array_equal(dtr, (tasmax + 273.15) - tasmin)