Skip to content

Commit

Permalink
Merge pull request #856 from Ouranosinc/sdba-fix-scalar-auxcoord
Browse files Browse the repository at this point in the history
[sdba] Work around map_blocks bug with 1D input and auxiliary coordinates
  • Loading branch information
aulemahal authored Sep 29, 2021
2 parents 6120e3d + e0b3a90 commit e33a6de
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
7 changes: 7 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
History
=======

0.30.1 (unreleased)
-------------------

Bug fixes
~~~~~~~~~
* Fix a bug in sdba's ``map_groups`` where 1D input including an auxialiary coordinate would fail with an obscure error on a reducing operation.

0.30.0 (2021-09-28)
-------------------

Expand Down
3 changes: 2 additions & 1 deletion xclim/sdba/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,8 @@ def _call_and_transpose_on_exit(dsblock, **kwargs):
nam: crd for nam, crd in ds.coords.items() if nam not in crd.dims
}
ds = ds.drop_vars(extra_coords.keys())
tmpl = tmpl.drop_vars(extra_coords.keys())
# Coords not sharing dims with `all_dims` (like scalar aux coord on reduced 1D input) are absent from tmpl
tmpl = tmpl.drop_vars(extra_coords.keys(), errors="ignore")

# Call
out = ds.map_blocks(
Expand Down
13 changes: 9 additions & 4 deletions xclim/testing/tests/test_sdba/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def func(ds, *, group, lon=None):

data = func(
xr.Dataset(dict(tas=tas)), group="time.dayofyear", window=5, lon=[1, 2, 3, 4]
)
).load()
assert set(data.data.dims) == {"time", "lon"}

@map_groups(data=[Grouper.PROP])
Expand All @@ -185,7 +185,7 @@ def func(ds, *, dim):

data = func(
xr.Dataset(dict(tas=tas)), group="time.dayofyear", window=5, add_dims=["lat"]
)
).load()
assert set(data.data.dims) == {"dayofyear"}

@map_groups(data=[Grouper.PROP], main_only=True)
Expand All @@ -194,5 +194,10 @@ def func(ds, *, dim):
data = ds.tas.mean(dim)
return data.rename("data").to_dataset()

data = func(xr.Dataset(dict(tas=tas)), group="time.dayofyear")
assert set(data.data.dims) == {"dayofyear", "lat"}
# with a scalar aux coord
data = func(
xr.Dataset(dict(tas=tas.isel(lat=0, drop=True)), coords=dict(leftover=1)),
group="time.dayofyear",
).load()
assert set(data.data.dims) == {"dayofyear"}
assert "leftover" in data

0 comments on commit e33a6de

Please sign in to comment.