-
Hi all, I am always struggling with this kind of selection of a desired domain or time range. For me it is very common to select data to make other interpolations and this feature will be very useful in my day by day. I make simple code to expose my doubt, thank you in advance! import numpy as np
import pandas as pd
import xarray as xr
from datetime import datetime
# Create random_values dataset with time, latitude, longitude coords
longitudes = np.arange(-180, 180, 5)
latitudes = np.arange(-90, 90, 5)
times = pd.date_range(start=datetime(2021, 1, 1), end=datetime(2021, 12, 31), freq="D")
data = np.random.rand(len(times), len(latitudes), len(longitudes))
da = xr.DataArray(
data=data,
coords=[times, latitudes, longitudes],
dims=["time", "latitude", "longitude"],
)
ds = da.to_dataset(name="random_values")
# Create a slices based on tmin,tmax lonmin,lonmax and latmin,latmax of the desired location and timerange
t_min = datetime(2021, 2, 16, 12, 0, 0)
t_max = datetime(2021, 3, 6, 12, 0, 0)
lon_min = -3
lon_max = 28
lat_min = 12
lat_max = 48
desired_time = slice(t_min, t_max)
desired_lon = slice(lon_min, lon_max)
desired_lat = slice(lat_min, lat_max)
# make standard dataset selection
standard_sel_ds = ds.sel(time=desired_time, latitude=desired_lat, longitude=desired_lon)
print(
f"time_min = {standard_sel_ds['time'].min().values}\n time_max = {standard_sel_ds['time'].max().values}"
)
print(
f"lon_min = {standard_sel_ds['longitude'].min().values}\n lon_max = {standard_sel_ds['longitude'].max().values}"
)
print(
f"lat_min = {standard_sel_ds['latitude'].min().values}\n lat_max = {standard_sel_ds['latitude'].max().values}"
)
# I would like to have a extra argument to select the outside closest neighbours of the sesired coordinates. Resulting:
print(
"time_min = 2021-02-16T00:00:00.000000000\n time_max = 2021-03-07T00:00:00.000000000"
)
print("lon_min = -5\n lon_max = 30")
print("lat_min = 10\n lat_max = 50")
# Anyone knows if that is developed in xarray.sel (???) |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
Hello, I don´t know if I understood correctly, but if you want to simply select the nearest neighbor cells, you can simply do ds.sel(latitude=lat_min, longitude=lon_min , method= "nearest"), this will select the cell closest to your specified lat_min, lon_min. There are a couple of other selection methods, please refer to https://xarray.pydata.org/en/stable/generated/xarray.Dataset.sel.html documentation for more details. Best regards |
Beta Was this translation helpful? Give feedback.
-
Thank you @ChenyaoYang123 for your response, I want to select the domain by the nearest neighbours outside the proposed range. Normally I need currents o winds to use in models and We use the model boundaries to select forcings from global or regional netcdfs, so we need this selection to cover completely the time, lon, lat ranges we ask for in the sel command. I think this method will be a mix of "ffill" and "bfill": using "bfill" form the min longitude, latitude, time coordinates and "ffill" for the max longitude, latitude and time coordinate. But I think is not implemented. |
Beta Was this translation helpful? Give feedback.
-
I make this workaround to force the selection in this way just before make the slices: # FIX - Calculate your domain adding 1 maximum dt, dx, dy to perform an outside buffer
t_min = t_min - np.diff(ds["time"].values).max().astype('timedelta64[s]').item()
t_max = t_max + np.diff(ds["time"].values).max().astype('timedelta64[s]').item()
lon_min = lon_min - np.diff(ds["longitude"].values).max()
lon_max = lon_max + np.diff(ds["latitude"].values).max()
lat_min = lat_min - np.diff(ds["longitude"].values).max()
lat_max = lat_max + np.diff(ds["latitude"].values).max() I think that would be nice to have a proper method based on coordinates labels to do this internally but for now, if someone has the same problem maybe will be helpful, please comment if you know any better alternative! |
Beta Was this translation helpful? Give feedback.
-
I think that your example may be a bit too specific to be implemented directly in Xarray. It is a good use case for Xarray's custom indexes, though (see https://github.com/pydata/xarray/projects/1). Once the feature is available, you could create a specific |
Beta Was this translation helpful? Give feedback.
I think that your example may be a bit too specific to be implemented directly in Xarray. It is a good use case for Xarray's custom indexes, though (see https://github.com/pydata/xarray/projects/1). Once the feature is available, you could create a specific
xarray.Index
subclass which implements the logic so that.sel()
will also select the nearest labels around a given range for a coordinate (or fill the first and/or last items withnan
if the nearest elements are beyond a giventolerance
).