Determine which dataset variables need to be concatenated in the result,
(
datasets: list[T_Dataset],
dim: Hashable,
all_dims: set[Hashable],
data_vars: T_DataVars | Iterable[Hashable] | CombineKwargDefault,
coords: ConcatOptions | Iterable[Hashable] | CombineKwargDefault,
compat: CompatOptions | CombineKwargDefault,
)
| 369 | |
| 370 | |
| 371 | def _calc_concat_over( |
| 372 | datasets: list[T_Dataset], |
| 373 | dim: Hashable, |
| 374 | all_dims: set[Hashable], |
| 375 | data_vars: T_DataVars | Iterable[Hashable] | CombineKwargDefault, |
| 376 | coords: ConcatOptions | Iterable[Hashable] | CombineKwargDefault, |
| 377 | compat: CompatOptions | CombineKwargDefault, |
| 378 | ) -> tuple[set[Hashable], dict[Hashable, bool], list[int], set[Hashable]]: |
| 379 | """ |
| 380 | Determine which dataset variables need to be concatenated in the result, |
| 381 | """ |
| 382 | # variables to be concatenated |
| 383 | concat_over = set() |
| 384 | # variables checked for equality |
| 385 | equals: dict[Hashable, bool] = {} |
| 386 | # skip merging these variables. |
| 387 | # if concatenating over a dimension 'x' that is associated with an index over 2 variables, |
| 388 | # 'x' and 'y', then we assert join="equals" on `y` and don't need to merge it. |
| 389 | # that assertion happens in the align step prior to this function being called |
| 390 | skip_merge: set[Hashable] = set() |
| 391 | |
| 392 | if dim in all_dims: |
| 393 | concat_over_existing_dim = True |
| 394 | concat_over.add(dim) |
| 395 | else: |
| 396 | concat_over_existing_dim = False |
| 397 | |
| 398 | if data_vars == "minimal" and coords == "minimal" and not concat_over_existing_dim: |
| 399 | raise ValueError( |
| 400 | "Cannot specify both data_vars='minimal' and coords='minimal' when " |
| 401 | "concatenating over a new dimension." |
| 402 | ) |
| 403 | |
| 404 | if data_vars is None or ( |
| 405 | isinstance(data_vars, CombineKwargDefault) and data_vars._value is None |
| 406 | ): |
| 407 | data_vars = "minimal" if concat_over_existing_dim else "all" |
| 408 | |
| 409 | concat_dim_lengths = [] |
| 410 | for ds in datasets: |
| 411 | if concat_over_existing_dim and dim not in ds.dims and dim in ds: |
| 412 | ds = ds.set_coords(dim) |
| 413 | concat_over.update(k for k, v in ds.variables.items() if dim in v.dims) |
| 414 | for _, idx_vars in ds.xindexes.group_by_index(): |
| 415 | if any(dim in v.dims for v in idx_vars.values()): |
| 416 | skip_merge.update(idx_vars.keys()) |
| 417 | concat_dim_lengths.append(ds.sizes.get(dim, 1)) |
| 418 | |
| 419 | def process_subset_opt( |
| 420 | opt: ConcatOptions | Iterable[Hashable] | CombineKwargDefault, |
| 421 | subset: Literal["coords", "data_vars"], |
| 422 | ) -> None: |
| 423 | original = set(concat_over) |
| 424 | compat_str = ( |
| 425 | compat._value if isinstance(compat, CombineKwargDefault) else compat |
| 426 | ) |
| 427 | assert compat_str is not None |
| 428 | if isinstance(opt, str | CombineKwargDefault): |
no test coverage detected
searching dependent graphs…