Apply a variable level function over Dataset, dict of DataArray, DataArray, Variable and/or ndarray objects.
(
func,
*args,
signature: _UFuncSignature,
join="inner",
dataset_join="exact",
fill_value=_NO_FILL_VALUE,
exclude_dims=frozenset(),
keep_attrs="override",
on_missing_core_dim: MissingCoreDimOptions = "raise",
)
| 484 | |
| 485 | |
| 486 | def apply_dataset_vfunc( |
| 487 | func, |
| 488 | *args, |
| 489 | signature: _UFuncSignature, |
| 490 | join="inner", |
| 491 | dataset_join="exact", |
| 492 | fill_value=_NO_FILL_VALUE, |
| 493 | exclude_dims=frozenset(), |
| 494 | keep_attrs="override", |
| 495 | on_missing_core_dim: MissingCoreDimOptions = "raise", |
| 496 | ) -> Dataset | tuple[Dataset, ...]: |
| 497 | """Apply a variable level function over Dataset, dict of DataArray, |
| 498 | DataArray, Variable and/or ndarray objects. |
| 499 | """ |
| 500 | from xarray.core.dataset import Dataset |
| 501 | |
| 502 | if dataset_join not in _JOINS_WITHOUT_FILL_VALUES and fill_value is _NO_FILL_VALUE: |
| 503 | raise TypeError( |
| 504 | "to apply an operation to datasets with different " |
| 505 | "data variables with apply_ufunc, you must supply the " |
| 506 | "dataset_fill_value argument." |
| 507 | ) |
| 508 | |
| 509 | objs = _all_of_type(args, Dataset) |
| 510 | |
| 511 | if len(args) > 1: |
| 512 | args = tuple( |
| 513 | deep_align( |
| 514 | args, |
| 515 | join=join, |
| 516 | copy=False, |
| 517 | exclude=exclude_dims, |
| 518 | raise_on_invalid=False, |
| 519 | ) |
| 520 | ) |
| 521 | |
| 522 | list_of_coords, list_of_indexes = build_output_coords_and_indexes( |
| 523 | args, signature, exclude_dims, combine_attrs=keep_attrs |
| 524 | ) |
| 525 | args = tuple(getattr(arg, "data_vars", arg) for arg in args) |
| 526 | |
| 527 | result_vars = apply_dict_of_variables_vfunc( |
| 528 | func, |
| 529 | *args, |
| 530 | signature=signature, |
| 531 | join=dataset_join, |
| 532 | fill_value=fill_value, |
| 533 | on_missing_core_dim=on_missing_core_dim, |
| 534 | ) |
| 535 | |
| 536 | out: Dataset | tuple[Dataset, ...] |
| 537 | if signature.num_outputs > 1: |
| 538 | out = tuple( |
| 539 | itertools.starmap( |
| 540 | _fast_dataset, |
| 541 | zip(result_vars, list_of_coords, list_of_indexes, strict=True), |
| 542 | ) |
| 543 | ) |
no test coverage detected
searching dependent graphs…