@@ -255,6 +255,31 @@ def _get_measure(obj: Union[DataArray, Dataset], key: str) -> List[str]:
255
255
return list (results )
256
256
257
257
258
+ def _get_bounds (obj : Union [DataArray , Dataset ], key : str ) -> List [str ]:
259
+ """
260
+ Translate from key (either CF key or variable name) to appropriate bounds names.
261
+ This function interprets the ``bounds`` attribute on DataArrays.
262
+
263
+ Parameters
264
+ ----------
265
+ obj : DataArray, Dataset
266
+ DataArray belonging to the coordinate to be checked
267
+ key : str
268
+ key to check for.
269
+
270
+ Returns
271
+ -------
272
+ List[str], Variable name(s) in parent xarray object that matches axis or coordinate `key`
273
+ """
274
+
275
+ results = []
276
+ for var in apply_mapper (_get_all , obj , key , error = False , default = [key ]):
277
+ if "bounds" in obj [var ].attrs :
278
+ results += [obj [var ].attrs ["bounds" ]]
279
+
280
+ return results
281
+
282
+
258
283
def _get_with_standard_name (
259
284
obj : Union [DataArray , Dataset ], name : Union [str , List [str ]]
260
285
) -> List [str ]:
@@ -1027,13 +1052,15 @@ def make_text_section(subtitle, vardict, valid_values, default_keys=None):
1027
1052
"Cell Measures" , self .cell_measures , coords , _CELL_MEASURES
1028
1053
)
1029
1054
text += make_text_section ("Standard Names" , self .standard_names , coords )
1055
+ text += make_text_section ("Bounds" , self .bounds , coords )
1030
1056
if isinstance (self ._obj , Dataset ):
1031
1057
data_vars = self ._obj .data_vars
1032
1058
text += "\n Data Variables:"
1033
1059
text += make_text_section (
1034
1060
"Cell Measures" , self .cell_measures , data_vars , _CELL_MEASURES
1035
1061
)
1036
1062
text += make_text_section ("Standard Names" , self .standard_names , data_vars )
1063
+ text += make_text_section ("Bounds" , self .bounds , data_vars )
1037
1064
1038
1065
return text
1039
1066
@@ -1132,6 +1159,26 @@ def cell_measures(self) -> Dict[str, List[str]]:
1132
1159
1133
1160
return {k : sorted (set (v )) for k , v in measures .items () if v }
1134
1161
1162
+ @property
1163
+ def bounds (self ) -> Dict [str , List [str ]]:
1164
+ """
1165
+ Property that returns a dictionary mapping valid keys to variable names of their bounds.
1166
+
1167
+ Returns
1168
+ -------
1169
+ Dictionary mapping valid keys to variable names of their bounds.
1170
+ """
1171
+
1172
+ obj = self ._obj
1173
+ keys = self .keys ()
1174
+ keys |= set (obj .variables if isinstance (obj , Dataset ) else obj .coords )
1175
+
1176
+ vardict = {
1177
+ key : apply_mapper (_get_bounds , obj , key , error = False ) for key in keys
1178
+ }
1179
+
1180
+ return {k : sorted (v ) for k , v in vardict .items () if v }
1181
+
1135
1182
def get_standard_names (self ) -> List [str ]:
1136
1183
1137
1184
warnings .warn (
@@ -1493,12 +1540,8 @@ def get_bounds(self, key: str) -> DataArray:
1493
1540
-------
1494
1541
DataArray
1495
1542
"""
1496
- name = apply_mapper (
1497
- _single (_get_all ), self ._obj , key , error = False , default = [key ]
1498
- )[0 ]
1499
- bounds = self ._obj [name ].attrs ["bounds" ]
1500
- obj = self ._maybe_to_dataset ()
1501
- return obj [bounds ]
1543
+
1544
+ return apply_mapper (_variables (_single (_get_bounds )), self ._obj , key )[0 ]
1502
1545
1503
1546
def get_bounds_dim_name (self , key : str ) -> str :
1504
1547
"""
0 commit comments