我有一个具有三个维度的 DataArrays 列表。对于列表中的每个项目,其中两个维度是单个值,但所有项目的组合将产生完整的组合值。
import itertools
import numpy as np
import xarray as xr
ds = []
for vals_dim1, vals_dim2 in itertools.product(list(range(2)), list(range(3))):
d = xr.DataArray(np.random.rand(1, 1, 4),
coords={'dim1': [vals_dim1], 'dim2': [vals_dim2], 'dim3': range(4)},
dims=['dim1', 'dim2', 'dim3'])
ds.append(d)
然后我想结合这些免费DataArray
的,但到目前为止我尝试的所有方法似乎都不起作用。结果应该是DataArray
带有形状|2x3x4|
和尺寸的dim1: |2|, dim2: |3|, dim3: |4|
。
以下不起作用:
# does not automatically infer dimensions and fails with
# "ValueError: conflicting sizes for dimension 'concat_dim': length 2 on 'concat_dim' and length 6 on <this-array>"
ds = xr.concat(ds, dim=['dim1', 'dim2'])
# will still try to insert a new `concat_dim` and fails with
# "ValueError: conflicting MultiIndex level name(s): 'dim1' (concat_dim), (dim1) 'dim2' (concat_dim), (dim2)"
import pandas as pd
dims = [[0] * 3 + [1] * 3, list(range(3)) * 2]
dims = pd.MultiIndex.from_arrays(dims, names=['dim1', 'dim2'])
ds = xr.concat(ds, dim=dims)
# fails with
# AttributeError: 'DataArray' object has no attribute 'data_vars'
ds = xr.auto_combine(ds)