如何通过XArray中的坐标组合2D平铺数据集

问题描述

我正在做一个项目,需要在Xarray中将不同的地理图像图块组合成一个大图块。我遇到了一个问题。我在下面做了一个简化的例子。

square1 = xr.DataArray(name="Box1",data=np.random.randint(5,size=(3,2)),coords=[("x",[0,1,2]),('y',1])])
square2 = xr.DataArray(name="Box2",[3,4,5]),1])])
square3 = xr.DataArray(name="Box3",[2,3])])
square4 = xr.DataArray(name="Box4",3])])

combineddata = xr.combine_by_coords([square1,square2,square3,square4])

我以为这就是您要做的全部,但我收到此错误

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-57-fc5add80d55a> in <module>
----> 1 xr.combine_by_coords([square1,square4])

~/my-conda-envs/dem/lib/python3.8/site-packages/xarray/core/combine.py in combine_by_coords(datasets,compat,data_vars,coords,fill_value,join,combine_attrs)
    713 
    714     # Group by data vars
--> 715     sorted_datasets = sorted(datasets,key=vars_as_keys)
    716     grouped_by_vars = itertools.groupby(sorted_datasets,key=vars_as_keys)
    717 

~/my-conda-envs/dem/lib/python3.8/site-packages/xarray/core/combine.py in vars_as_keys(ds)
    502 
    503 def vars_as_keys(ds):
--> 504     return tuple(sorted(ds))
    505 
    506 

~/my-conda-envs/dem/lib/python3.8/site-packages/xarray/core/common.py in __bool__(self)
    118 
    119     def __bool__(self: Any) -> bool:
--> 120         return bool(self.values)
    121 
    122     def __float__(self: Any) -> float:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

我在做什么错了?

解决方法

Xarray combine_by_coords当前仅适用于xarray数据集对象,不适用于DataArray对象。请参见以下问题:https://github.com/pydata/xarray/issues/3248。 @dcherian提供的解决方法是将DataArrays转换为Dataset,合并,然后再转换回DataArray。

这可行:

import numpy as np
import xarray as xr

square1 = xr.DataArray(name="box",data=np.random.randint(5,size=(3,2)),coords=[("x",[0,1,2]),('y',1])]) 
square2 = xr.DataArray(name="box",[3,4,5]),1])]) 
square3 = xr.DataArray(name="box",[2,3])]) 
square4 = xr.DataArray(name="box",3])]) 

combineddata = xr.combine_by_coords(sq.to_dataset() for sq in [square1,square2,square3,square4]).to_array()