Skip to content

Commit 205e673

Browse files
aulemahalpre-commit-ci[bot]dcherian
authored
2D bounds - simple version (#370)
* 2D bounds - simple version * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix test * upd whatsnew * pre-commit black * add doc page and better docstring * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Apply suggestions from code review Co-authored-by: Deepak Cherian <[email protected]> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Raise on name collision - changes to doc after review * Fix typo Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Deepak Cherian <[email protected]>
1 parent 614b8ef commit 205e673

File tree

5 files changed

+78
-16
lines changed

5 files changed

+78
-16
lines changed

cf_xarray/accessor.py

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -461,17 +461,37 @@ def wrapper(obj: DataArray | Dataset, key: str):
461461
}
462462

463463

464-
def _guess_bounds_dim(da, dim=None):
464+
def _guess_bounds_dim(da, dim=None, out_dim="bounds"):
465465
"""
466-
Guess bounds values given a 1D coordinate variable.
466+
Guess bounds values given a 1D or 2D coordinate variable.
467467
Assumes equal spacing on either side of the coordinate label.
468+
This is a coarse approximation, especially for 2D bounds on curvilinear grids.
468469
"""
469470
if dim is None:
470-
if da.ndim != 1:
471+
if da.ndim not in [1, 2]:
471472
raise ValueError(
472-
f"If dim is None, variable {da.name} must be 1D. Received {da.ndim}D variable instead."
473+
f"If dim is None, variable {da.name} must be 1D or 2D. Received {da.ndim}D variable instead."
473474
)
474-
(dim,) = da.dims
475+
dim = da.dims
476+
if not isinstance(dim, str):
477+
if len(dim) > 2:
478+
raise NotImplementedError(
479+
"Adding bounds with more than 2 dimensions is not supported."
480+
)
481+
elif len(dim) == 2:
482+
daX = _guess_bounds_dim(da, dim[0]).rename(bounds="Xbnds")
483+
daXY = _guess_bounds_dim(daX, dim[1]).rename(bounds="Ybnds")
484+
return xr.concat(
485+
[
486+
daXY.isel(Xbnds=0, Ybnds=0),
487+
daXY.isel(Xbnds=0, Ybnds=1),
488+
daXY.isel(Xbnds=1, Ybnds=1),
489+
daXY.isel(Xbnds=1, Ybnds=0),
490+
],
491+
out_dim,
492+
)
493+
else:
494+
dim = dim[0]
475495
if dim not in da.dims:
476496
(dim,) = da.cf.axes[dim]
477497
if dim not in da.coords:
@@ -482,7 +502,7 @@ def _guess_bounds_dim(da, dim=None):
482502
diff = da.diff(dim)
483503
lower = da - diff / 2
484504
upper = da + diff / 2
485-
bounds = xr.concat([lower, upper], dim="bounds")
505+
bounds = xr.concat([lower, upper], dim=out_dim)
486506

487507
first = (bounds.isel({dim: 0}) - diff.isel({dim: 0})).assign_coords(
488508
{dim: da[dim][0]}
@@ -2169,18 +2189,28 @@ def get_bounds_dim_name(self, key: str) -> str:
21692189
assert self._obj.sizes[bounds_dim] in [2, 4]
21702190
return bounds_dim
21712191

2172-
def add_bounds(self, keys: str | Iterable[str], *, dim=None):
2192+
def add_bounds(
2193+
self,
2194+
keys: str | Iterable[str],
2195+
*,
2196+
dim: str | Iterable[str] | None = None,
2197+
output_dim: str = "bounds",
2198+
):
21732199
"""
21742200
Returns a new object with bounds variables. The bounds values are guessed assuming
2175-
equal spacing on either side of a coordinate label.
2201+
equal spacing on either side of a coordinate label. The linear estimation is only a
2202+
coarse approximation, especially 2D bounds on curvilinear grids. It is always better to use
2203+
bounds generated as part of the grid creation process. This method is purely for convenience.
21762204
21772205
Parameters
21782206
----------
21792207
keys : str or Iterable[str]
21802208
Either a single variable name or a list of variable names.
2181-
dim : str, optional
2182-
Core dimension along whch to estimate bounds. If None, ``keys``
2183-
must refer to 1D variables only.
2209+
dim : str or Iterable[str], optional
2210+
Core dimension(s) along which to estimate bounds. For 2D bounds, it can
2211+
be a list of 2 dimension names.
2212+
output_dim : str
2213+
The name of the bounds dimension to add.
21842214
21852215
Returns
21862216
-------
@@ -2226,9 +2256,17 @@ def add_bounds(self, keys: str | Iterable[str], *, dim=None):
22262256
bname = f"{var}_bounds"
22272257
if bname in obj.variables:
22282258
raise ValueError(f"Bounds variable name {bname!r} will conflict!")
2229-
obj.coords[bname] = _guess_bounds_dim(
2230-
obj[var].reset_coords(drop=True), dim=dim
2259+
out = _guess_bounds_dim(
2260+
obj[var].reset_coords(drop=True), dim=dim, out_dim=output_dim
22312261
)
2262+
if output_dim in obj.dims and (new := out[output_dim].size) != (
2263+
old := obj[output_dim].size
2264+
):
2265+
raise ValueError(
2266+
f"The `{output_dim}` dimension already exists but has a different length than the new one "
2267+
f"({old} vs {new}). Please provide another bound dimension name with `output_dim`."
2268+
)
2269+
obj.coords[bname] = out
22322270
obj[var].attrs["bounds"] = bname
22332271

22342272
return self._maybe_to_dataarray(obj)

cf_xarray/tests/test_accessor.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from xarray.testing import assert_allclose, assert_identical
1414

1515
import cf_xarray # noqa
16+
from cf_xarray.helpers import vertices_to_bounds
1617
from cf_xarray.utils import parse_cf_standard_name_table
1718

1819
from ..datasets import (
@@ -799,26 +800,38 @@ def test_add_bounds_multiple():
799800

800801

801802
def test_add_bounds_nd_variable():
802-
803803
ds = xr.Dataset(
804804
{"z": (("x", "y"), np.arange(12).reshape(4, 3))},
805805
coords={"x": np.arange(4), "y": np.arange(3)},
806806
)
807807

808+
# 2D
809+
expected = (
810+
vertices_to_bounds(
811+
np.arange(0, 13, 3).reshape(5, 1) + np.arange(-2, 2).reshape(1, 4)
812+
)
813+
.rename("z_bounds")
814+
.assign_coords(**ds.coords)
815+
)
816+
actual = ds.cf.add_bounds("z").z_bounds.reset_coords(drop=True)
817+
xr.testing.assert_identical(actual, expected)
818+
819+
# 1D
808820
expected = (
809821
xr.concat([ds.z - 1.5, ds.z + 1.5], dim="bounds")
810822
.rename("z_bounds")
811823
.transpose("bounds", "y", "x")
812824
)
813-
with pytest.raises(ValueError):
814-
ds.cf.add_bounds("z")
815825

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

819829
with pytest.raises(NotImplementedError):
820830
ds.drop_vars("x").cf.add_bounds("z", dim="x")
821831

832+
with pytest.raises(ValueError, match="The `bounds` dimension already exists"):
833+
ds.cf.add_bounds("z").cf.add_bounds("x")
834+
822835

823836
def test_bounds():
824837
ds = airds.copy(deep=False).cf.add_bounds("lat")

doc/2D_bounds_error.png

60.7 KB
Loading

doc/bounds.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,9 @@ See
1010
1. {py:func}`Dataset.cf.add_bounds`,
1111
1. {py:func}`cf_xarray.bounds_to_vertices`,
1212
1. {py:func}`cf_xarray.vertices_to_bounds`
13+
14+
`cf_xarray` supports parsing [coordinate bounds](http://cfconventions.org/Data/cf-conventions/cf-conventions-1.10/cf-conventions.html#cell-boundaries) as encoded in the CF `bounds` attribute. A useful feature for incomplete dataset is also the automatic bounds estimation possible through `cf.add_bounds`. This method will estimate the missing bounds by finding the middle points between elements of the given coordinate, but also by extrapolating to find the outer bounds of the grid. This linear estimation works well with rectilinear grids, but it is only a coarse approximation for curvilinear and simple irregular grids.
15+
16+
As an example, we present a "rotated pole" grid. It is defined on a rotated rectilinear grid which uses the `rlat` and `rlon` 1D coordinates, over North America at a resolution of 0.44°. The datasets comes with 2D `lat` and `lon` coordinates. `cf_xarray` will estimate the bounds by linear interpolation (extrapolation at the edges) of the existing `lon` and `lat`, which yields good results on parts of the grid where the rotation is small. However the errors is larger in other places, as seen when visualizing the distance in degrees between the estimated bounds and the true bounds.
17+
18+
![2d bounds error](2D_bounds_error.png)

doc/whats-new.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
What's New
44
----------
55

6+
v0.7.5 (unreleased)
7+
===================
8+
- ``cf.add_bounds`` can estimate 2D bounds using an approximate linear interpolation (:pr:`370`).
9+
By `Pascal Bourgault`_.
10+
611
v0.7.4 (July 14, 2022)
712
======================
813

0 commit comments

Comments
 (0)