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,
)
| 348 | |
| 349 | |
| 350 | def 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]) |
no test coverage detected
searching dependent graphs…