Concatenate a sequence of datatrees along a new or existing dimension
(
objs: Iterable[DataTree],
dim: Hashable | Variable | T_DataArray | pd.Index | Any,
data_vars: T_DataVars | Iterable[Hashable] | CombineKwargDefault,
coords: ConcatOptions | Iterable[Hashable] | CombineKwargDefault,
compat: CompatOptions | CombineKwargDefault,
positions: Iterable[Iterable[int]] | None,
fill_value: Any,
join: JoinOptions | CombineKwargDefault,
combine_attrs: CombineAttrsOptions,
create_index_for_new_dim: bool,
)
| 927 | |
| 928 | |
| 929 | def _datatree_concat( |
| 930 | objs: Iterable[DataTree], |
| 931 | dim: Hashable | Variable | T_DataArray | pd.Index | Any, |
| 932 | data_vars: T_DataVars | Iterable[Hashable] | CombineKwargDefault, |
| 933 | coords: ConcatOptions | Iterable[Hashable] | CombineKwargDefault, |
| 934 | compat: CompatOptions | CombineKwargDefault, |
| 935 | positions: Iterable[Iterable[int]] | None, |
| 936 | fill_value: Any, |
| 937 | join: JoinOptions | CombineKwargDefault, |
| 938 | combine_attrs: CombineAttrsOptions, |
| 939 | create_index_for_new_dim: bool, |
| 940 | ) -> DataTree: |
| 941 | """ |
| 942 | Concatenate a sequence of datatrees along a new or existing dimension |
| 943 | """ |
| 944 | from xarray.core.datatree import DataTree |
| 945 | from xarray.core.treenode import TreeIsomorphismError, group_subtrees |
| 946 | |
| 947 | dim_name, _ = _calc_concat_dim_index(dim) |
| 948 | |
| 949 | objs = list(objs) |
| 950 | if not all(isinstance(obj, DataTree) for obj in objs): |
| 951 | raise TypeError("All objects to concatenate must be DataTree objects") |
| 952 | |
| 953 | if compat == "identical": |
| 954 | if any(obj.name != objs[0].name for obj in objs[1:]): |
| 955 | raise ValueError("DataTree names not identical") |
| 956 | |
| 957 | dim_in_tree = any(dim_name in node.dims for node in objs[0].subtree) |
| 958 | |
| 959 | results = {} |
| 960 | try: |
| 961 | for path, nodes in group_subtrees(*objs): |
| 962 | datasets_to_concat = [node.to_dataset() for node in nodes] |
| 963 | results[path] = _dataset_concat( |
| 964 | datasets_to_concat, |
| 965 | dim=dim, |
| 966 | data_vars=data_vars, |
| 967 | coords=coords, |
| 968 | compat=compat, |
| 969 | positions=positions, |
| 970 | fill_value=fill_value, |
| 971 | join=join, |
| 972 | combine_attrs=combine_attrs, |
| 973 | create_index_for_new_dim=create_index_for_new_dim, |
| 974 | preexisting_dim=dim_in_tree, |
| 975 | ) |
| 976 | except TreeIsomorphismError as e: |
| 977 | raise ValueError("All trees must be isomorphic to be concatenated") from e |
| 978 | |
| 979 | return DataTree.from_dict(results, name=objs[0].name) |
no test coverage detected
searching dependent graphs…