Skip to content

Commit 654a709

Browse files
authored
docs fixes (#154)
1 parent 68cc78e commit 654a709

File tree

9 files changed

+142
-104
lines changed

9 files changed

+142
-104
lines changed

cf_xarray/accessor.py

Lines changed: 72 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -561,11 +561,6 @@ def _getitem(
561561
kind = str(type(obj).__name__)
562562
scalar_key = isinstance(key, str)
563563

564-
if isinstance(obj, DataArray) and not scalar_key:
565-
raise KeyError(
566-
f"Cannot use a list of keys with DataArrays. Expected a single string. Received {key!r} instead."
567-
)
568-
569564
if scalar_key:
570565
key = (key,) # type: ignore
571566

@@ -1172,9 +1167,6 @@ def get_associated_variable_names(self, name: Hashable) -> Dict[str, List[str]]:
11721167

11731168
return coords
11741169

1175-
def __getitem__(self, key: Union[str, List[str]]):
1176-
return _getitem(self, key)
1177-
11781170
def _maybe_to_dataset(self, obj=None) -> Dataset:
11791171
if obj is None:
11801172
obj = self._obj
@@ -1283,6 +1275,37 @@ def guess_coord_axis(self, verbose: bool = False) -> Union[DataArray, Dataset]:
12831275

12841276
@xr.register_dataset_accessor("cf")
12851277
class CFDatasetAccessor(CFAccessor):
1278+
def __getitem__(self, key: Union[str, List[str]]) -> Union[DataArray, Dataset]:
1279+
"""
1280+
Index into a Dataset making use of CF attributes.
1281+
1282+
Parameters
1283+
----------
1284+
1285+
key: str, Iterable[str], optional
1286+
One of
1287+
- axes names: "X", "Y", "Z", "T"
1288+
- coordinate names: "longitude", "latitude", "vertical", "time"
1289+
- cell measures: "area", "volume", or other names present in the \
1290+
``cell_measures`` attribute
1291+
- standard names: names present in ``standard_name`` attribute
1292+
1293+
Returns
1294+
-------
1295+
DataArray or Dataset
1296+
``Dataset.cf[str]`` will return a DataArray, \
1297+
``Dataset.cf[List[str]]``` will return a Dataset.
1298+
1299+
Notes
1300+
-----
1301+
In all cases, associated CF variables will be attached as coordinate variables
1302+
by parsing attributes such as ``bounds``, ``ancillary_variables``, etc.
1303+
1304+
``bounds`` variables will not be attached when a DataArray is returned. This
1305+
is a limitation of the xarray data model.
1306+
"""
1307+
return _getitem(self, key)
1308+
12861309
def get_bounds(self, key: str) -> DataArray:
12871310
"""
12881311
Get bounds variable corresponding to key.
@@ -1317,9 +1340,12 @@ def add_bounds(self, dims: Union[Hashable, Iterable[Hashable]]):
13171340
-------
13181341
DataArray or Dataset with bounds variables added and appropriate "bounds" attribute set.
13191342
1343+
Raises
1344+
------
1345+
KeyError
1346+
13201347
Notes
13211348
-----
1322-
13231349
The bounds variables are automatically named f"{dim}_bounds" where ``dim``
13241350
is a dimension name.
13251351
"""
@@ -1511,4 +1537,41 @@ def decode_vertical_coords(self, prefix="z"):
15111537

15121538
@xr.register_dataarray_accessor("cf")
15131539
class CFDataArrayAccessor(CFAccessor):
1540+
def __getitem__(self, key: Union[str, List[str]]) -> DataArray:
1541+
"""
1542+
Index into a DataArray making use of CF attributes.
1543+
1544+
Parameters
1545+
----------
1546+
key: str, Iterable[str], optional
1547+
One of
1548+
- axes names: "X", "Y", "Z", "T"
1549+
- coordinate names: "longitude", "latitude", "vertical", "time"
1550+
- cell measures: "area", "volume", or other names present in the \
1551+
``cell_measures`` attribute
1552+
- standard names: names present in ``standard_name`` attribute of \
1553+
coordinate variables
1554+
1555+
Returns
1556+
-------
1557+
DataArray
1558+
1559+
Raises
1560+
------
1561+
KeyError
1562+
``DataArray.cf[List[str]]`` will raise KeyError.
1563+
1564+
Notes
1565+
-----
1566+
Associated CF variables will be attached as coordinate variables
1567+
by parsing attributes such as ``cell_measures``, ``coordinates`` etc.
1568+
"""
1569+
1570+
if not isinstance(key, str):
1571+
raise KeyError(
1572+
f"Cannot use a list of keys with DataArrays. Expected a single string. Received {key!r} instead."
1573+
)
1574+
1575+
return _getitem(self, key)
1576+
15141577
pass
File renamed without changes.

cf_xarray/tests/test_accessor.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
import cf_xarray # noqa
1111

12+
from ..datasets import airds, anc, ds_no_attrs, multiple, popds, romsds
1213
from . import raise_if_dask_computes
13-
from .datasets import airds, anc, ds_no_attrs, multiple, popds
1414

1515
mpl.use("Agg")
1616

@@ -586,7 +586,7 @@ def test_missing_variable_in_coordinates():
586586

587587

588588
def test_Z_vs_vertical_ROMS():
589-
from .datasets import romsds
589+
from ..datasets import romsds
590590

591591
assert_identical(romsds.s_rho.reset_coords(drop=True), romsds.temp.cf["Z"])
592592
assert_identical(
@@ -612,8 +612,6 @@ def test_Z_vs_vertical_ROMS():
612612

613613

614614
def test_param_vcoord_ocean_s_coord():
615-
from .datasets import romsds
616-
617615
romsds.s_rho.attrs["standard_name"] = "ocean_s_coordinate_g2"
618616
Zo_rho = (romsds.hc * romsds.s_rho + romsds.Cs_r * romsds.h) / (
619617
romsds.hc + romsds.h

cf_xarray/tests/test_helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import cf_xarray as cfxr # noqa
55

6-
from .datasets import airds, mollwds
6+
from ..datasets import airds, mollwds
77

88
try:
99
from dask.array import Array as DaskArray

doc/api.rst

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,28 @@
1-
.. currentmodule:: xarray
2-
31
API
42
===
53

4+
.. currentmodule:: cf_xarray
5+
6+
Top-level API
7+
-------------
8+
9+
.. autosummary::
10+
:toctree: generated/
11+
12+
bounds_to_vertices
13+
vertices_to_bounds
14+
15+
16+
.. currentmodule:: xarray
17+
618
DataArray
719
---------
820

21+
.. _daattr:
22+
23+
Attributes
24+
~~~~~~~~~~
25+
926
.. autosummary::
1027
:toctree: generated/
1128
:template: autosummary/accessor_attribute.rst
@@ -16,6 +33,12 @@ DataArray
1633
DataArray.cf.standard_names
1734
DataArray.cf.plot
1835

36+
37+
.. _dameth:
38+
39+
Methods
40+
~~~~~~~
41+
1942
.. autosummary::
2043
:toctree: generated/
2144
:template: autosummary/accessor_method.rst
@@ -29,6 +52,11 @@ DataArray
2952
Dataset
3053
-------
3154

55+
.. _dsattr:
56+
57+
Attributes
58+
~~~~~~~~~~
59+
3260
.. autosummary::
3361
:toctree: generated/
3462
:template: autosummary/accessor_attribute.rst
@@ -38,11 +66,16 @@ Dataset
3866
Dataset.cf.coordinates
3967
Dataset.cf.standard_names
4068

69+
.. _dsmeth:
70+
71+
Methods
72+
~~~~~~~
73+
4174
.. autosummary::
4275
:toctree: generated/
4376
:template: autosummary/accessor_method.rst
4477

45-
DataArray.cf.__getitem__
78+
Dataset.cf.__getitem__
4679
Dataset.cf.add_bounds
4780
Dataset.cf.bounds_to_vertices
4881
Dataset.cf.decode_vertical_coords
@@ -51,14 +84,3 @@ Dataset
5184
Dataset.cf.guess_coord_axis
5285
Dataset.cf.keys
5386
Dataset.cf.rename_like
54-
55-
.. currentmodule:: cf_xarray
56-
57-
Top-level API
58-
-------------
59-
60-
.. autosummary::
61-
:toctree: generated/
62-
63-
bounds_to_vertices
64-
vertices_to_bounds

doc/criteria.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
.. currentmodule:: xarray
22

3+
.. _criteria:
4+
35
CF Criteria
46
-----------
57

0 commit comments

Comments
 (0)