MCPcopy
hub / github.com/pydata/xarray / decode_cf_variables

Function decode_cf_variables

xarray/conventions.py:350–495  ·  view source on GitHub ↗

Decode several CF encoded variables. See: decode_cf_variable

(
    variables: T_Variables,
    attributes: T_Attrs,
    concat_characters: bool | Mapping[str, bool] = True,
    mask_and_scale: bool | Mapping[str, bool] = True,
    decode_times: bool | CFDatetimeCoder | Mapping[str, bool | CFDatetimeCoder] = True,
    decode_coords: bool | Literal["coordinates", "all"] = True,
    drop_variables: T_DropVariables = None,
    use_cftime: bool | Mapping[str, bool] | None = None,
    decode_timedelta: bool
    | CFTimedeltaCoder
    | Mapping[str, bool | CFTimedeltaCoder]
    | None = None,
)

Source from the content-addressed store, hash-verified

348
349
350def decode_cf_variables(
351 variables: T_Variables,
352 attributes: T_Attrs,
353 concat_characters: bool | Mapping[str, bool] = True,
354 mask_and_scale: bool | Mapping[str, bool] = True,
355 decode_times: bool | CFDatetimeCoder | Mapping[str, bool | CFDatetimeCoder] = True,
356 decode_coords: bool | Literal["coordinates", "all"] = True,
357 drop_variables: T_DropVariables = None,
358 use_cftime: bool | Mapping[str, bool] | None = None,
359 decode_timedelta: bool
360 | CFTimedeltaCoder
361 | Mapping[str, bool | CFTimedeltaCoder]
362 | None = None,
363) -> tuple[T_Variables, T_Attrs, set[Hashable]]:
364 """
365 Decode several CF encoded variables.
366
367 See: decode_cf_variable
368 """
369 # Only emit one instance of the decode_timedelta default change
370 # FutureWarning. This can be removed once this change is made.
371 warnings.filterwarnings("once", "decode_timedelta", FutureWarning)
372
373 dimensions_used_by = defaultdict(list)
374 for v in variables.values():
375 for d in v.dims:
376 dimensions_used_by[d].append(v)
377
378 def stackable(dim: Hashable) -> bool:
379 # figure out if a dimension can be concatenated over
380 if dim in variables:
381 return False
382 for v in dimensions_used_by[dim]:
383 if v.dtype.kind != "S" or dim != v.dims[-1]:
384 return False
385 return True
386
387 coord_names = set()
388
389 if isinstance(drop_variables, str):
390 drop_variables = [drop_variables]
391 elif drop_variables is None:
392 drop_variables = []
393 drop_variables = set(drop_variables)
394
395 # Time bounds coordinates might miss the decoding attributes
396 if decode_times:
397 _update_bounds_attributes(variables)
398
399 new_vars = {}
400 for k, v in variables.items():
401 if k in drop_variables:
402 continue
403 stack_char_dim = (
404 _item_or_default(concat_characters, k, True)
405 and v.dtype == "S1"
406 and v.ndim > 0
407 and stackable(v.dims[-1])

Callers 2

decode_cfFunction · 0.85
cf_decoderFunction · 0.85

Calls 13

emit_user_level_warningFunction · 0.90
_item_or_defaultFunction · 0.85
stackableFunction · 0.85
decode_cf_variableFunction · 0.85
itemsMethod · 0.80
splitMethod · 0.80
replaceMethod · 0.80
stripMethod · 0.80
keysMethod · 0.80
valuesMethod · 0.45
updateMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…