diff --git a/CHANGELOG.rst b/CHANGELOG.rst index b642f67e..8f574b6f 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -26,6 +26,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) diff --git a/src/xscen/aggregate.py b/src/xscen/aggregate.py index 32a9e187..62a1954c 100644 --- a/src/xscen/aggregate.py +++ b/src/xscen/aggregate.py @@ -23,6 +23,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 @@ -565,6 +566,10 @@ 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].attrs["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..145fc851 100644 --- a/src/xscen/xclim_modules/conversions.py +++ b/src/xscen/xclim_modules/conversions.py @@ -99,12 +99,11 @@ 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, "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 diff --git a/tests/test_aggregate.py b/tests/test_aggregate.py index fde0d965..023c8971 100644 --- a/tests/test_aggregate.py +++ b/tests/test_aggregate.py @@ -75,6 +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 + 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 1de15b39..2576ea36 100644 --- a/tests/test_xclimmod_conversions.py +++ b/tests/test_xclimmod_conversions.py @@ -57,5 +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_metadata"] == "temperature: difference" np.testing.assert_array_equal(dtr, (tasmax + 273.15) - tasmin)