-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
.sel(...., method='nearest') fails for large requests. #4630
Comments
You should be able to do this with "vectorized indexing": https://xarray.pydata.org/en/stable/indexing.html#vectorized-indexing |
@dcherian Thanks for pointing me in the right direction. I'm trying to implement this with vectorized indexing, but it seems that my queries need to exactly match the xarray object lat/lon, which is why I tried |
TO use "vectorized indexing", import xarray as xr
import numpy as np
ds = xr.tutorial.open_dataset("air_temperature")
# Define taget latitude and longitude
tgt_lat = xr.DataArray(np.linspace(0, 100, num=10), dims="points") # <---
tgt_lon = xr.DataArray(np.linspace(0, 100, num=10), dims="points") # <---
# Retrieve data at target latitude and longitude
tgt_data = ds['air'].sel(lon=tgt_lon, lat=tgt_lat, method='nearest')
tgt_data |
👏 👍 I didn't realize I needed to do that. Thanks for letting me know. Problem solved - marking this as closed. |
this trick is not mentioned in the narrative documentation (or rather: I can't find it), and the docstrings of Since I believe it should be documented somewhere I'm reopening this to make sure we don't forget. Also, we would definitely welcome a PR adding this, if you're up for it. |
I'd be happy to give this a shot. But I'm not sure where to start... Can you point me to an example PR that has done something similar? |
Thanks for sharing! I'll give this a first shot before the end of the year. |
A common usage of
xarray
is to retrieve climate model data from the grid cells closest to a weather station. That might look like thisMy problem is that I am trying subset
ds
to 10 points in space (which is the length of tgt_lat and tgt_lon), but in factxarray
retrieves 100 points (10 latitude by 10 longitude). I can get around this by callingtgt_data = tgt_data.values.diagonal()
. But this results in a non-xarray object. Furthermore, if instead of querying for 10 points in space, I query for 10,000, I run out of memory becausexarray
retrieves 100,000,000 points in space (10,000^2).Is there a way to only retrieve the diagonal elements? If not, is this something that should be added?
The text was updated successfully, but these errors were encountered: