I'm working in a xarray dataset with rectilinear grids (latitude constant over x-axis / longitude constant over y-axis) which have 2D coordinates. I needed to subset this dataset to a particular lat/lon value, but unfortunately subsetting a dataset according to lat/lon variables in multidimensional coordinates are a little less intuitive and became quite exhausting for me, since we cannot use ds.sel(lat=y, lon=x, method='nearest')
, for example. Ultimately, I resort to ds.where()
option but ended up having to find the nearest lat/lon coordinates in the dataset manually. Here's a print of my dataset:
xarray.DataArray 'u' (MT: 366, Depth: 1, Y: 266, X: 99)
[9638244 values with dtype=float32]
Coordinates:
MT (MT) datetime64[ns] 2012-01-01 ... 2012-12-31
Depth (Depth) float32 0.0
Latitude (Y, X) float32 array([[-21.92 -21.92 ... -1.28 -1.28]])
Longitude (Y, X) float32 array([[319.6 319.7 319.8 ... 327.4 327.4]])
Attributes:
standard_name: eastward_sea_water_velocity
units: m/s
long_name: u-veloc. [90.9H]
_ChunkSizes: [1 3 413 563]
As a result, I expect to convert the 2D coordinates into 1D lat/lon dimensions as below:
xarray.DataArray 'u' (MT: 366, Depth: 1, Lat: 266, Lon: 99)
[9638244 values with dtype=float32]
Coordinates:
MT (MT) datetime64[ns] 2012-01-01 ... 2012-12-31
Depth (Depth) float32 0.0
Latitude (Lat) float32 -21.92 -21.92 ... -1.28 -1.28
Longitude (Lon) float32 319.6 319.7 319.8 ... 327.4 327.4
Attributes:
standard_name: eastward_sea_water_velocity
units: m/s
long_name: u-veloc. [90.9H]
_ChunkSizes: [1 3 413 563]
After some research, I found the xESMF package which does a great job for regridding 2D curvilinear grids into 2D rectilinear grids, and also this thread comparing both xESMF and Scipy performances in terms of interpolating 2D to 1D coordinates. However, I didn't find any proper solution for converting these 2D coordinates into nominal 1D lat/lon dimensions. Is it possible to be done?