Skip to content

Commit 2f46ee3

Browse files
authored
Support indexing by cf_role for DSG datasets (#240)
1 parent ed36d29 commit 2f46ee3

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

cf_xarray/accessor.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from xarray import DataArray, Dataset
2626
from xarray.core.arithmetic import SupportsArithmetic
2727

28-
from .criteria import coordinate_criteria, regex
28+
from .criteria import cf_role_criteria, coordinate_criteria, regex
2929
from .helpers import bounds_to_vertices
3030
from .utils import (
3131
_get_version,
@@ -372,6 +372,7 @@ def _get_all(obj: Union[DataArray, Dataset], key: str) -> List[str]:
372372
"""
373373
all_mappers = (
374374
_get_custom_criteria,
375+
functools.partial(_get_custom_criteria, criteria=cf_role_criteria),
375376
_get_axis_coord,
376377
_get_measure,
377378
_get_with_standard_name,
@@ -665,7 +666,7 @@ def check_results(names, key):
665666
successful[k] = bool(measure)
666667
if measure:
667668
varnames.extend(measure)
668-
elif k in custom_criteria:
669+
elif k in custom_criteria or k in cf_role_criteria:
669670
names = _get_all(obj, k)
670671
check_results(names, k)
671672
successful[k] = bool(names)

cf_xarray/criteria.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66

77

88
import re
9-
from typing import MutableMapping, Tuple
9+
from typing import Mapping, MutableMapping, Tuple
10+
11+
cf_role_criteria: Mapping[str, Mapping[str, str]] = {
12+
k: {"cf_role": k} for k in ("timeseries_id", "profile_id", "trajectory_id")
13+
}
1014

1115
coordinate_criteria: MutableMapping[str, MutableMapping[str, Tuple]] = {
1216
"latitude": {

doc/whats-new.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ What's New
55

66
v0.5.3 (unreleased)
77
===================
8+
- Support indexing by ``cf_role`` attribute. By `Deepak Cherian`_.
89
- Implemented :py:meth:`Dataset.cf.add_canonical_attributes` and :py:meth:`DataArray.cf.add_canonical_attributes`
910
to add CF canonical attributes. By `Mattia Almansi`_.
1011
- Begin adding support for units with a unit registry for pint arrays. :pr:`197`.

0 commit comments

Comments
 (0)