Skip to content
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
17 changes: 9 additions & 8 deletions cf_xarray/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@ def _guess_bounds_1d(da, dim):
da = da.assign_coords({dim: da[dim]})
ADDED_INDEX = True

diff = da.diff(dim)
lower = da - diff / 2
upper = da + diff / 2
bounds = xr.concat([lower, upper], dim="bounds")
bound_position = 0.5
diff = da.diff(dim).pad({dim: (1, 1)}, mode="edge").reset_index(dim)
lower = (
da.reset_index(dim) - bound_position * diff.isel({dim: slice(0, -1)})
).assign_coords({dim: da[dim]})
upper = (
da.reset_index(dim) + bound_position * diff.isel({dim: slice(1, None)})
).assign_coords({dim: da[dim]})
Comment on lines +28 to +33
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops, merged too early.

we could optimize here by calling reset_index once and assigning coords to result directly

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries, I'm happy to continue working on this. I'm still pretty new to xarray, but I think I found a better way to do this and opened a pull request in #627.

result = xr.concat([lower, upper], dim="bounds").transpose(..., "bounds")

first = (bounds.isel({dim: 0}) - diff.isel({dim: 0})).assign_coords(
{dim: da[dim][0]}
)
result = xr.concat([first, bounds], dim=dim).transpose(..., "bounds")
if ADDED_INDEX:
result = result.drop_vars(dim)
return result.drop_attrs(deep=False)
Expand Down
34 changes: 32 additions & 2 deletions cf_xarray/tests/test_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,36 @@ def test_add_bounds(dims):
_check_unchanged(original, ds)


def test_add_irregularly_spaced_bounds_do_not_overlap() -> None:
# Test that added bounds with irregular spacing do not overlap.
ds = airds
original = ds.copy(deep=True)
ds["time"] = ds["time"].copy(data=pd.date_range("2013-01", "2013-04", freq="MS"))
expected = xr.DataArray(
data=[
pd.to_datetime(t)
for t in [
["2012-12-16T12", "2013-01-16T12"],
["2013-01-16T12", "2013-02-15T00"],
["2013-02-15T00", "2013-03-16T12"],
["2013-03-16T12", "2013-04-16T12"],
]
],
dims=["time", "bounds"],
name="time_bounds",
coords={"time": ds.time.data},
)
added = ds.copy(deep=False)
added = added.cf.add_bounds("time")

name = "time_bounds"
assert name in added.coords
assert added["time"].attrs["bounds"] == name
assert_allclose(added[name].reset_coords(drop=True), expected)

_check_unchanged(original, ds)


def test_add_bounds_multiple() -> None:
# Test multiple dimensions
assert not {"x1_bounds", "x2_bounds"} <= set(multiple.variables)
Expand Down Expand Up @@ -882,11 +912,11 @@ def test_add_bounds_nd_variable() -> None:
expected = (
xr.concat([ds.z - 1.5, ds.z + 1.5], dim="bounds")
.rename("z_bounds")
.transpose("bounds", "y", "x")
.transpose(..., "bounds")
)

actual = ds.cf.add_bounds("z", dim="x").z_bounds.reset_coords(drop=True)
xr.testing.assert_identical(expected.transpose(..., "bounds"), actual)
xr.testing.assert_identical(expected, actual)

# Requesting bounds on a non-variable dimension
with pytest.raises(ValueError, match="are dimensions with no index."):
Expand Down