Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combining tiled data sets in xarray #4338

Closed
nicholaskgeorge opened this issue Aug 12, 2020 · 4 comments
Closed

Combining tiled data sets in xarray #4338

nicholaskgeorge opened this issue Aug 12, 2020 · 4 comments

Comments

@nicholaskgeorge
Copy link

I'm working on a project where I need to combine different sets of geographic image tiles into one large tile in Xarray. I am running into an issue. I've made a simplified example bellow.

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

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

I thought this is all you need to do it but I get this error

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-57-fc5add80d55a> in <module>
----> 1 xr.combine_by_coords([square1,square2,square3,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()

I am not really sure why this is not working.

@dcherian
Copy link
Contributor

Thanks for the well written issue @nicholaskgeorge

This is a duplicate of #3248 . The solution here is to make all tiles have the same name, use .to_dataset() and then pass that to combine_by_coords

In [9]: square1 = xr.DataArray(name="box", data=np.random.randint(5, size=(3, 2)), coords=[("x", [0,1,2]),('y',[0,1])]) 
   ...: square2 = xr.DataArray(name="box", data=np.random.randint(5, size=(3, 2)), coords=[("x", [2,3,4]),('y',[0,1])]) 
   ...: square3 = xr.DataArray(name="box", data=np.random.randint(5, size=(3, 2)), coords=[("x", [0,1,2]),('y',[2,3])]) 
   ...: square4 = xr.DataArray(name="box", data=np.random.randint(5, size=(3, 2)), coords=[("x", [2,3,4]),('y',[2,3])]) 
   ...:  
   ...: combineddata = xr.combine_by_coords(sq.to_dataset() for sq in [square1,square2,square3,square4])  

You can use combineddata.to_array() to convert back to a DataArray.

@nicholaskgeorge
Copy link
Author

Thank you @dcherian ! It works. I'm now getting an error telling me the that the axis are not monotonic when I apply it to my real data but I think its something I have to sort out. Sorry I didn't realize this issue was put up before.

@dcherian
Copy link
Contributor

no worries. I'll close this as a duplicate.

@rsignell-usgs
Copy link

rsignell-usgs commented Aug 13, 2020

@nicholaskgeorge your minimal test would be monotonic if square2 and square4 had x coordinates [3,4,5] instead of [2,3,4], but it seems combine_by_coords doesn't mind that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants