Skip to content

Commit c41f2fa

Browse files
authored
Add "get_bounds_dim_name" (#159)
1 parent 36a9b01 commit c41f2fa

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed

cf_xarray/accessor.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,6 +1378,27 @@ def get_bounds(self, key: str) -> DataArray:
13781378
obj = self._maybe_to_dataset()
13791379
return obj[bounds]
13801380

1381+
def get_bounds_dim_name(self, key: str) -> str:
1382+
"""
1383+
Get bounds dim name for variable corresponding to key.
1384+
1385+
Parameters
1386+
----------
1387+
key : str
1388+
Name of variable whose bounds dimension name is desired.
1389+
1390+
Returns
1391+
-------
1392+
str
1393+
"""
1394+
crd = self[key]
1395+
bounds = self.get_bounds(key)
1396+
bounds_dims = set(bounds.dims) - set(crd.dims)
1397+
assert len(bounds_dims) == 1
1398+
bounds_dim = bounds_dims.pop()
1399+
assert self._obj.sizes[bounds_dim] in [2, 4]
1400+
return bounds_dim
1401+
13811402
def add_bounds(self, dims: Union[Hashable, Iterable[Hashable]]):
13821403
"""
13831404
Returns a new object with bounds variables. The bounds values are guessed assuming

cf_xarray/tests/test_accessor.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,16 @@
99

1010
import cf_xarray # noqa
1111

12-
from ..datasets import airds, anc, ds_no_attrs, forecast, multiple, popds, romsds
12+
from ..datasets import (
13+
airds,
14+
anc,
15+
ds_no_attrs,
16+
forecast,
17+
mollwds,
18+
multiple,
19+
popds,
20+
romsds,
21+
)
1322
from . import raise_if_dask_computes
1423

1524
mpl.use("Agg")
@@ -522,6 +531,15 @@ def test_bounds_to_vertices():
522531
assert "time_bounds" in dsc
523532

524533

534+
def test_get_bounds_dim_name():
535+
ds = airds.copy(deep=True).cf.add_bounds("lat")
536+
assert ds.cf.get_bounds_dim_name("latitude") == "bounds"
537+
assert ds.cf.get_bounds_dim_name("lat") == "bounds"
538+
539+
assert mollwds.cf.get_bounds_dim_name("longitude") == "bounds"
540+
assert mollwds.cf.get_bounds_dim_name("lon") == "bounds"
541+
542+
525543
def test_docstring():
526544
assert "One of ('X'" in airds.cf.groupby.__doc__
527545
assert "One or more of ('X'" in airds.cf.mean.__doc__

doc/api.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ Methods
8181
Dataset.cf.decode_vertical_coords
8282
Dataset.cf.describe
8383
Dataset.cf.get_bounds
84+
Dataset.cf.get_bounds_dim_name
8485
Dataset.cf.guess_coord_axis
8586
Dataset.cf.keys
8687
Dataset.cf.rename_like

doc/whats-new.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ v0.4.1 (unreleased)
1515
by :py:meth:`Dataset.cf.__getitem__`. This allows extraction of DataArrays when there are clashes
1616
between DataArray names and "special" CF names like ``T``.
1717
(:issue:`129`, :pr:`130`). By `Deepak Cherian`_
18-
- Fix iteration and arithemtic with ``GroupBy`` objects. By `Deepak Cherian`_.
18+
- Retrieve bounds dimension name with :py:meth:`Dataset.cf.get_bounds_dim_name`. By `Pascal Bourgault`_.
19+
- Fix iteration and arithmetic with ``GroupBy`` objects. By `Deepak Cherian`_.
1920

2021
v0.4.0 (Jan 22, 2021)
2122
=====================

0 commit comments

Comments
 (0)