@@ -149,11 +149,11 @@ def _get_axis_coord(
149
149
150
150
Parameters
151
151
----------
152
- var : DataArray, Dataset
152
+ var: DataArray, Dataset
153
153
DataArray belonging to the coordinate to be checked
154
- key : str, ["X", "Y", "Z", "T", "longitude", "latitude", "vertical", "time"]
154
+ key: str, ["X", "Y", "Z", "T", "longitude", "latitude", "vertical", "time"]
155
155
key to check for.
156
- error : bool
156
+ error: bool
157
157
raise errors when key is not found or interpretable. Use False and provide default
158
158
to replicate dict.get(k, None).
159
159
default: Any
@@ -217,7 +217,24 @@ def _get_measure(
217
217
da : xr .DataArray , key : str , error : bool = True , default : str = None
218
218
) -> Optional [str ]:
219
219
"""
220
- Interprets 'cell_measures'.
220
+ Translate from cell measures ("area" or "volume") to appropriate variable name.
221
+ This function interprets the ``cell_measures`` attribute on DataArrays.
222
+
223
+ Parameters
224
+ ----------
225
+ da: DataArray
226
+ DataArray belonging to the coordinate to be checked
227
+ key: str, ["area", "volume"]
228
+ key to check for.
229
+ error: bool
230
+ raise errors when key is not found or interpretable. Use False and provide default
231
+ to replicate dict.get(k, None).
232
+ default: Any
233
+ default value to return when error is False.
234
+
235
+ Returns
236
+ -------
237
+ List[str], Variable name(s) in parent xarray object that matches axis or coordinate `key`
221
238
"""
222
239
if not isinstance (da , DataArray ):
223
240
raise NotImplementedError ("Measures not implemented for Datasets yet." )
@@ -254,6 +271,9 @@ def _get_measure(
254
271
255
272
256
273
#: Default mappers for common keys.
274
+ # TODO: Make the values of this a tuple,
275
+ # so that multiple mappers can be used for a single key
276
+ # We need this for groupby("T.month") and groupby("latitude") for example.
257
277
_DEFAULT_KEY_MAPPERS : Mapping [str , Mapper ] = {
258
278
"dim" : _get_axis_coord ,
259
279
"dims_or_levels" : _get_axis_coord , # reset_index
@@ -280,7 +300,19 @@ def _filter_by_standard_names(ds: xr.Dataset, name: Union[str, List[str]]) -> Li
280
300
281
301
282
302
def _get_list_standard_names (obj : xr .Dataset ) -> List [str ]:
283
- """ Returns a sorted list of standard names in Dataset. """
303
+ """
304
+ Returns a sorted list of standard names in Dataset.
305
+
306
+ Parameters
307
+ ----------
308
+
309
+ obj: DataArray, Dataset
310
+ Xarray objec to process
311
+
312
+ Returns
313
+ -------
314
+ list of standard names in dataset
315
+ """
284
316
names = []
285
317
for k , v in obj .variables .items ():
286
318
if "standard_name" in v .attrs :
@@ -332,15 +364,18 @@ def wrapper(*args, **kwargs):
332
364
333
365
334
366
class _CFWrappedClass :
367
+ """
368
+ This class is used to wrap any class in _WRAPPED_CLASSES.
369
+ """
370
+
335
371
def __init__ (self , towrap , accessor : "CFAccessor" ):
336
372
"""
337
- This class is used to wrap any class in _WRAPPED_CLASSES.
338
-
339
373
Parameters
340
374
----------
341
375
towrap : Resample, GroupBy, Coarsen, Rolling, Weighted
342
376
Instance of xarray class that is being wrapped.
343
377
accessor : CFAccessor
378
+ Parent accessor object
344
379
"""
345
380
self .wrapped = towrap
346
381
self .accessor = accessor
@@ -369,7 +404,10 @@ def __init__(self, obj, accessor):
369
404
370
405
def _plot_decorator (self , func ):
371
406
"""
372
- This decorator is used to set kwargs on plotting functions.
407
+ This decorator is used to set default kwargs on plotting functions.
408
+
409
+ For now, this is setting ``xincrease`` and ``yincrease``. It could set
410
+ other arguments in the future.
373
411
"""
374
412
valid_keys = self .accessor .get_valid_keys ()
375
413
@@ -402,6 +440,9 @@ def _plot_wrapper(*args, **kwargs):
402
440
return _plot_wrapper
403
441
404
442
def __call__ (self , * args , ** kwargs ):
443
+ """
444
+ Allows .plot()
445
+ """
405
446
plot = _getattr (
406
447
obj = self ._obj ,
407
448
attr = "plot" ,
@@ -411,6 +452,9 @@ def __call__(self, *args, **kwargs):
411
452
return self ._plot_decorator (plot )(* args , ** kwargs )
412
453
413
454
def __getattr__ (self , attr ):
455
+ """
456
+ Wraps .plot.contour() for example.
457
+ """
414
458
return _getattr (
415
459
obj = self ._obj .plot ,
416
460
attr = attr ,
@@ -423,10 +467,21 @@ def __getattr__(self, attr):
423
467
424
468
425
469
class CFAccessor :
470
+ """
471
+ Common Dataset and DataArray accessor functionality.
472
+ """
473
+
426
474
def __init__ (self , da ):
427
475
self ._obj = da
428
476
429
- def _process_signature (self , func , args , kwargs , key_mappers ):
477
+ def _process_signature (self , func : Callable , args , kwargs , key_mappers ):
478
+ """
479
+ Processes a function's signature, args, kwargs:
480
+ 1. Binds *args so that everthing is a Mapping from kwarg name to values
481
+ 2. Calls _rewrite_values to rewrite any special CF names to normal xarray names.
482
+ This uses key_mappers
483
+ 3. Unpacks arguments if necessary before returning them.
484
+ """
430
485
sig = inspect .signature (func , follow_wrapped = False )
431
486
432
487
# Catch things like .isel(T=5).
@@ -456,7 +511,24 @@ def _process_signature(self, func, args, kwargs, key_mappers):
456
511
return arguments
457
512
458
513
def _rewrite_values (self , kwargs , key_mappers : dict , var_kws ):
459
- """ rewrites 'dim' for example using 'mapper' """
514
+ """
515
+ Rewrites the values in a Mapping from kwarg to value.
516
+
517
+ Parameters
518
+ ----------
519
+ kwargs: Mapping
520
+ Mapping from kwarg name to value
521
+ key_mappers: Mapping
522
+ Mapping from kwarg name to a Mapper function that will convert a
523
+ given CF "special" name to an xarray name.
524
+ var_kws: List[str]
525
+ List of variable kwargs that need special treatment.
526
+ e.g. **indexers_kwargs in isel
527
+
528
+ Returns
529
+ -------
530
+ dict of kwargs with fully rewritten values.
531
+ """
460
532
updates : dict = {}
461
533
462
534
# allow multiple return values here.
@@ -558,11 +630,17 @@ def _describe(self):
558
630
return text
559
631
560
632
def describe (self ):
633
+ """
634
+ Print a string repr to screen.
635
+ """
561
636
print (self ._describe ())
562
637
563
638
def get_valid_keys (self ) -> Set [str ]:
564
639
"""
565
- Returns valid keys for .cf[]
640
+ Utility function that returns valid keys for .cf[].
641
+
642
+ This is useful for checking whether a key is valid for indexing, i.e.
643
+ that the attributes necessary to allow indexing by that key exist.
566
644
567
645
Returns
568
646
-------
0 commit comments