Skip to content

Commit 9666262

Browse files
authored
accessor utility properties and tweaks (#14)
1 parent 4897ff5 commit 9666262

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

xdggs/accessor.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,33 @@ def __init__(self, obj: xr.Dataset | xr.DataArray):
1919
for k, idx in obj.xindexes.items():
2020
if isinstance(idx, DGGSIndex):
2121
if index is not None:
22-
raise ValueError("Only one DGGSIndex per object is supported")
22+
raise ValueError("Only one DGGSIndex per dataset or dataarray is supported")
2323
index = idx
2424
name = k
2525
self._name = name
2626
self._index = index
2727

2828
@property
2929
def index(self) -> DGGSIndex:
30+
"""Returns the DGGSIndex instance for this Dataset or DataArray.
31+
32+
Raise a ``ValueError`` if no such index is found.
33+
"""
3034
if self._index is None:
3135
raise ValueError("no DGGSIndex found on this Dataset or DataArray")
3236
return self._index
3337

38+
@property
39+
def coord(self) -> xr.DataArray:
40+
"""Returns the indexed DGGS (cell ids) coordinate as a DataArray.
41+
42+
Raise a ``ValueError`` if no such coordinate is found on this Dataset or DataArray.
43+
44+
"""
45+
if not self._name:
46+
raise ValueError("no coordinate with a DGGSIndex found on this Dataset or DataArray")
47+
return self._obj[self._name]
48+
3449
def sel_latlon(
3550
self, latitude: npt.ArrayLike, longitude: npt.ArrayLike
3651
) -> xr.Dataset | xr.DataArray:

0 commit comments

Comments
 (0)