Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix conversion of units of multivariate DataArray in sdba #1972

Merged
merged 3 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
Changelog
=========

v0.54 (unpublished)
--------------------
Contributors to this version: Éric (:user:`coxipi`).

Bug fixes
^^^^^^^^^
* Conversion of units of multivariate DataArray is now properly handled in `sdba.TrainAdjust` and `sdba.Adjust`. There was a bug where the units could be changed before a conversion of the magntitudes could occur. (:pull:`1972`).

v0.53.1 (2024-10-21)
--------------------
Contributors to this version: Trevor James Smith (:user:`Zeitsperre`).
Expand Down
19 changes: 14 additions & 5 deletions xclim/sdba/adjustment.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,21 @@ def __convert_units_to(_input_da, _internal_dim, _internal_target):
v: _inputs[0][_dim].attrs["_units"][iv]
for iv, v in enumerate(_inputs[0][_dim].values)
}
return (
__convert_units_to(
_input_da, _internal_dim=_dim, _internal_target=_target

# `__convert_units_to`` was changing the units of the 3rd dataset during the 2nd loop
# This explicit loop is designed to avoid this
_outputs = []
original_units = list(
[_inp[_dim].attrs["_units"].copy() for _inp in _inputs]
)
for _inp, units in zip(_inputs, original_units, strict=False):
_inp[_dim].attrs["_units"] = units
_outputs.append(
__convert_units_to(
_inp, _internal_dim=_dim, _internal_target=_target
)
)
for _input_da in _inputs
), _target
return _outputs, _target
coxipi marked this conversation as resolved.
Show resolved Hide resolved

for dim, crd in inputs[0].coords.items():
if crd.attrs.get("is_variables"):
Expand Down
Loading