Collect variables and indexes from list of mappings of xarray objects. Mappings can be Dataset or Coordinates objects, in which case both variables and indexes are extracted from it. It can also have values of one of the following types: - an xarray.Variable - a tuple `(dims, d
(
list_of_mappings: Iterable[DatasetLike],
indexes: Mapping[Any, Any] | None = None,
)
| 356 | |
| 357 | |
| 358 | def collect_variables_and_indexes( |
| 359 | list_of_mappings: Iterable[DatasetLike], |
| 360 | indexes: Mapping[Any, Any] | None = None, |
| 361 | ) -> dict[Hashable, list[MergeElement]]: |
| 362 | """Collect variables and indexes from list of mappings of xarray objects. |
| 363 | |
| 364 | Mappings can be Dataset or Coordinates objects, in which case both |
| 365 | variables and indexes are extracted from it. |
| 366 | |
| 367 | It can also have values of one of the following types: |
| 368 | - an xarray.Variable |
| 369 | - a tuple `(dims, data[, attrs[, encoding]])` that can be converted in |
| 370 | an xarray.Variable |
| 371 | - or an xarray.DataArray |
| 372 | |
| 373 | If a mapping of indexes is given, those indexes are assigned to all variables |
| 374 | with a matching key/name. For dimension variables with no matching index, a |
| 375 | default (pandas) index is assigned. DataArray indexes that don't match mapping |
| 376 | keys are also extracted. |
| 377 | |
| 378 | """ |
| 379 | from xarray.core.coordinates import Coordinates |
| 380 | from xarray.core.dataarray import DataArray |
| 381 | from xarray.core.dataset import Dataset |
| 382 | |
| 383 | if indexes is None: |
| 384 | indexes = {} |
| 385 | |
| 386 | grouped: dict[Hashable, list[MergeElement]] = defaultdict(list) |
| 387 | |
| 388 | def append(name, variable, index): |
| 389 | grouped[name].append((variable, index)) |
| 390 | |
| 391 | def append_all(variables, indexes): |
| 392 | for name, variable in variables.items(): |
| 393 | append(name, variable, indexes.get(name)) |
| 394 | |
| 395 | for mapping in list_of_mappings: |
| 396 | if isinstance(mapping, Coordinates | Dataset): |
| 397 | append_all(mapping.variables, mapping.xindexes) |
| 398 | continue |
| 399 | |
| 400 | for name, variable in mapping.items(): |
| 401 | if isinstance(variable, DataArray): |
| 402 | coords_ = variable._coords.copy() # use private API for speed |
| 403 | indexes_ = dict(variable._indexes) |
| 404 | # explicitly overwritten variables should take precedence |
| 405 | coords_.pop(name, None) |
| 406 | indexes_.pop(name, None) |
| 407 | append_all(coords_, indexes_) |
| 408 | |
| 409 | variable = as_variable(variable, name=name, auto_convert=False) |
| 410 | if name in indexes: |
| 411 | append(name, variable, indexes[name]) |
| 412 | elif variable.dims == (name,): |
| 413 | idx, idx_vars = create_default_index_implicit(variable) |
| 414 | append_all(idx_vars, dict.fromkeys(idx_vars, idx)) |
| 415 | else: |
no test coverage detected
searching dependent graphs…