Skip to content

Commit 1298277

Browse files
MuellerSebdcherianpre-commit-ci[bot]aulemahal
authored
Correctly shaped bounds for add_bounds method (#347)
* Correctly shaped bounds for add_bounds method * Update cf_xarray/accessor.py saver transposing Co-authored-by: Deepak Cherian <[email protected]> * test_accessor: transpose expected added bounds * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * test_helpers: transpose expected added bounds * fix helpers for bounds as last dim * fix 2D bounds with bounds as last dim Co-authored-by: Deepak Cherian <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Pascal Bourgault <[email protected]>
1 parent 205e673 commit 1298277

File tree

5 files changed

+26
-22
lines changed

5 files changed

+26
-22
lines changed

cf_xarray/accessor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ def _guess_bounds_dim(da, dim=None, out_dim="bounds"):
489489
daXY.isel(Xbnds=1, Ybnds=0),
490490
],
491491
out_dim,
492-
)
492+
).transpose(..., "bounds")
493493
else:
494494
dim = dim[0]
495495
if dim not in da.dims:
@@ -507,7 +507,7 @@ def _guess_bounds_dim(da, dim=None, out_dim="bounds"):
507507
first = (bounds.isel({dim: 0}) - diff.isel({dim: 0})).assign_coords(
508508
{dim: da[dim][0]}
509509
)
510-
result = xr.concat([first, bounds], dim=dim)
510+
result = xr.concat([first, bounds], dim=dim).transpose(..., "bounds")
511511

512512
return result
513513

cf_xarray/datasets.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@
156156

157157
def _create_mollw_ds():
158158
# Dataset with random data on a grid that is some sort of Mollweide projection
159-
XX, YY = np.mgrid[:11, :11] * 5 - 25
160-
XX_bnds, YY_bnds = np.mgrid[:12, :12] * 5 - 27.5
159+
YY, XX = np.mgrid[:11, :11] * 5 - 25
160+
YY_bnds, XX_bnds = np.mgrid[:12, :12] * 5 - 27.5
161161

162162
R = 50
163163
theta = np.arcsin(YY / (R * np.sqrt(2)))
@@ -179,7 +179,7 @@ def _create_mollw_ds():
179179
lon_vertices[1:, 1:],
180180
lon_vertices[1:, :-1],
181181
),
182-
axis=0,
182+
axis=-1,
183183
)
184184
lat_bounds = np.stack(
185185
(
@@ -188,31 +188,31 @@ def _create_mollw_ds():
188188
lat_vertices[1:, 1:],
189189
lat_vertices[1:, :-1],
190190
),
191-
axis=0,
191+
axis=-1,
192192
)
193193

194194
mollwds = xr.Dataset(
195195
coords=dict(
196196
lon=xr.DataArray(
197197
lon,
198-
dims=("x", "y"),
198+
dims=("y", "x"),
199199
attrs={"units": "degrees_east", "bounds": "lon_bounds"},
200200
),
201201
lat=xr.DataArray(
202202
lat,
203-
dims=("x", "y"),
203+
dims=("y", "x"),
204204
attrs={"units": "degrees_north", "bounds": "lat_bounds"},
205205
),
206206
),
207207
data_vars=dict(
208208
lon_bounds=xr.DataArray(
209-
lon_bounds, dims=("bounds", "x", "y"), attrs={"units": "degrees_east"}
209+
lon_bounds, dims=("y", "x", "bounds"), attrs={"units": "degrees_east"}
210210
),
211211
lat_bounds=xr.DataArray(
212-
lat_bounds, dims=("bounds", "x", "y"), attrs={"units": "degrees_north"}
212+
lat_bounds, dims=("y", "x", "bounds"), attrs={"units": "degrees_north"}
213213
),
214-
lon_vertices=xr.DataArray(lon_vertices, dims=("x_vertices", "y_vertices")),
215-
lat_vertices=xr.DataArray(lat_vertices, dims=("x_vertices", "y_vertices")),
214+
lon_vertices=xr.DataArray(lon_vertices, dims=("y_vertices", "x_vertices")),
215+
lat_vertices=xr.DataArray(lat_vertices, dims=("y_vertices", "x_vertices")),
216216
),
217217
)
218218

cf_xarray/helpers.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def _bounds_helper(values, n_core_dims, nbounds, order):
9494
top_left = values[..., -1:, :, 3]
9595
vertex_vals = np.block([[bot_left, bot_right], [top_left, top_right]])
9696
if order is None: # We verify if the ccw version works.
97-
calc_bnds = np.moveaxis(vertices_to_bounds(vertex_vals).values, 0, -1)
97+
calc_bnds = vertices_to_bounds(vertex_vals).values
9898
order = (
9999
"counterclockwise" if np.allclose(calc_bnds, values) else "clockwise"
100100
)
@@ -155,4 +155,6 @@ def vertices_to_bounds(
155155
raise ValueError(
156156
f"vertices format not understood. Got {vertices.dims} with shape {vertices.shape}."
157157
)
158-
return xr.DataArray(bnd_vals, dims=out_dims[: vertices.ndim + 1])
158+
return xr.DataArray(bnd_vals, dims=out_dims[: vertices.ndim + 1]).transpose(
159+
..., out_dims[0]
160+
)

cf_xarray/tests/test_accessor.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,9 @@ def test_add_bounds(dims):
788788
name = f"{dim}_bounds"
789789
assert name in added.coords
790790
assert added[dim].attrs["bounds"] == name
791-
assert_allclose(added[name].reset_coords(drop=True), expected[dim])
791+
assert_allclose(
792+
added[name].reset_coords(drop=True), expected[dim].transpose(..., "bounds")
793+
)
792794

793795
_check_unchanged(original, ds)
794796

@@ -824,7 +826,7 @@ def test_add_bounds_nd_variable():
824826
)
825827

826828
actual = ds.cf.add_bounds("z", dim="x").z_bounds.reset_coords(drop=True)
827-
xr.testing.assert_identical(expected, actual)
829+
xr.testing.assert_identical(expected.transpose(..., "bounds"), actual)
828830

829831
with pytest.raises(NotImplementedError):
830832
ds.drop_vars("x").cf.add_bounds("z", dim="x")

cf_xarray/tests/test_helpers.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def test_bounds_to_vertices():
1717
lat_c = cfxr.bounds_to_vertices(ds.lat_bounds, bounds_dim="bounds")
1818
assert_array_equal(ds.lat.values + 1.25, lat_c.values[:-1])
1919

20-
# 2D case, CF- order
20+
# 2D case
2121
lat_ccw = cfxr.bounds_to_vertices(
2222
mollwds.lat_bounds, bounds_dim="bounds", order="counterclockwise"
2323
)
@@ -35,13 +35,13 @@ def test_bounds_to_vertices():
3535
assert_equal(lon_no, lon_ccw)
3636

3737
# Transposing the array changes the bounds direction
38-
ds = mollwds.transpose("bounds", "y", "x", "y_vertices", "x_vertices")
39-
lon_c = cfxr.bounds_to_vertices(
38+
ds = mollwds.transpose("x", "y", "x_vertices", "y_vertices", "bounds")
39+
lon_cw = cfxr.bounds_to_vertices(
4040
ds.lon_bounds, bounds_dim="bounds", order="clockwise"
4141
)
42-
lon_c2 = cfxr.bounds_to_vertices(ds.lon_bounds, bounds_dim="bounds", order=None)
43-
assert_equal(ds.lon_vertices, lon_c)
44-
assert_equal(ds.lon_vertices, lon_c2)
42+
lon_no2 = cfxr.bounds_to_vertices(ds.lon_bounds, bounds_dim="bounds", order=None)
43+
assert_equal(ds.lon_vertices, lon_cw)
44+
assert_equal(ds.lon_vertices, lon_no2)
4545

4646
# Preserves dask-backed arrays
4747
if DaskArray is not None:

0 commit comments

Comments
 (0)