Skip to content

Commit

Permalink
Follow CF convention for temperature difference (#429)
Browse files Browse the repository at this point in the history
<!-- Please ensure the PR fulfills the following requirements! -->
<!-- If this is your first PR, make sure to add your details to the
AUTHORS.rst! -->
### Pull Request Checklist:
- [x] This PR addresses an already opened issue (for bug fixes /
features)
    - This PR fixes #428
- [ ] (If applicable) Documentation has been added / updated (for bug
fixes / features).
- [x] (If applicable) Tests have been added.
- [x] This PR does not seem to break the templates.
- [x] CHANGELOG.rst has been updated (with summary of main changes).
- [x] Link to issue (:issue:`number`) and pull request (:pull:`number`)
has been added.

### What kind of change does this PR introduce?

* Add unit_metadata for the dtr conversion to be clear that this is a
difference and not convert the units in a non-sensical way.
* Added unit update for delta. If it's a temperature xclim will add the
unit_metadata. Do we want this for the climatological delta ?

### Does this PR introduce a breaking change?
yes, changes the attrs

### Other information:
* this needs to wait for the next xclim release for the new functions. 
* Should we add `temperature: on_scale` for `tasmax_from_dtr` and
`tasmin_from_dtr` or is it unecessary?
* Is it necessary somewhere else ?
  • Loading branch information
RondeauG authored Nov 4, 2024
2 parents 399e7a4 + 351ea8c commit 4eaea42
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions src/xscen/aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down
9 changes: 4 additions & 5 deletions src/xscen/xclim_modules/conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions tests/test_aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down
3 changes: 2 additions & 1 deletion tests/test_xclimmod_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 4eaea42

Please sign in to comment.