I have a large 2D domain that is broken up into equally-sized 2D patches in several xarray DataArrays. So, for example, a simulation of what I have would be:
a=xr.DataArray(np.random.rand(4,4)+0, dims=("x", "y"), coords=dict(x=range(4), y=range(4)))
b=xr.DataArray(np.random.rand(4,4)+1, dims=("x", "y"), coords=dict(x=range(4,8), y=range(4)))
c=xr.DataArray(np.random.rand(4,4)+2, dims=("x", "y"), coords=dict(x=range(4,8), y=range(4,8)))
I am trying to put all these patches together to recreate the domain. So in the example case above, if I were to concatenate everything, I would have an 8 by 8 matrix at the end (with one of the "patches" as np.nan
) and the other three patches corresponding to their respective DataArrays.
I've been trying to automatize this with xarray but so far I've had no success. My closest attempt is using xr.concat
:
d=xr.concat([a,b,c], dim="x")
d.plot.imshow(x="x")
This produces the following image, which is obviously wrong:
Note that in my case the patches are all scrambled, so I can't really rely on a specific order to concatenate. Here, for example, I could do d=xr.concat([xr.concat([a,b], dim="x"), c], dim="y")
, but only because I know the order.
PS: This may be somewhat related to this question, although it's not clear to me what this question actually asks.
Found our you can do this with sequential applications of combine_first
. It's not ideal, since I have to iterative apply it given a huge list of DataArray
s, but it's good enough:
d=a.combine_first(c).combine_first(b)