xarray interp variable with two dimensional gird into one point
Solution 1:
Don't think you can use ds.analysis_sea_ice_thickness.sel(lat=80.0, lon=100.0, method = "nearest")
directly, because lat
and lon
are not dimensions (see ds.dims
), but coordinates (ds.coords
). So you could do this for example:
import xarray as xr
import numpy as np
# Open dataset
ds = xr.open_dataset(r"/Path/to/your/folder/W_XX-ESA,SMOS_CS2,NH_25KM_EASE2_20211218_20211224_o_v204_01_l4sit.nc")
# Define point-of-interest.
lat = 80.0
lon = 100.0
# Find indices where lon and lat are closest to point-of-interest.
idxs = (np.abs(ds.lon - lon) + np.abs(ds.lat - lat)).argmin(dim = ["xc", "yc"])
# Retrieve value of variable at indices
value = ds.analysis_sea_ice_thickness.isel(idxs).values
# Check the actual lat and lon
lat_in_ds = ds.lat.isel(idxs).values
lon_in_ds = ds.lon.isel(idxs).values
# Print some results.
print(f"Thickness at ({lat_in_ds:.3f}, {lon_in_ds:.3f}) = {value[0]} {ds.analysis_sea_ice_thickness.units}.")
Thickness at (80.107, 99.782) = 0.805 m.