2

I have atmospheric data on a lat-lon-time grid, in an xarray Dataset. I want to select from that data along a 'path', so on (lat, lon, time) coordinates.

I have tried to use xarray advanced indexing. If I use only (lat, lon), I get the expected result: a new xarray dataset with a new spatial dimension, and with the existing time dimension. If I also include the time in the advanced indexing, my new dataset has all NaN values.

My xarray dataset looks like this (small example):

Dimensions:      (Time: 133, south_north: 75, west_east: 75)
Coordinates:
    XLONG        (Time, south_north, west_east) float32 3.9869385 ... 5.0813293
    XTIME        (Time) float64 1.539e+09 1.539e+09 ... 1.539e+09 1.539e+09
    XLAT         (Time, south_north, west_east) float32 52.067024 ... 52.73047
Dimensions without coordinates: Time, south_north, west_east
Data variables:
    Q2           (Time, south_north, west_east) float32 0.007977517 ... 0.009767285
    T2           (Time, south_north, west_east) float32 15.0159 ... 18.756592
    U10          (Time, south_north, west_east) float32 -1.9524655 ... -0.9998946
    V10          (Time, south_north, west_east) float32 4.070522 ... 4.83041
    PSFC         (Time, south_north, west_east) float32 102017.22 ... 101689.734

The new coordinates I defined like this:

y = xr.DataArray(Shipdata3.Lats.values, dims = "routetime")
x = xr.DataArray(Shipdata3.Lons.values, dims = "routetime")
ts = xr.DataArray(Shipdata3.time.values, dims = "routetime")

for example ts looks like this:

<xarray.DataArray (routetime: 93)>
array([1.5391494e+09, 1.5391512e+09, ...
       1.5393564e+09])
Dimensions without coordinates: routetime

Then I try to do the advanced indexing:

ship_route_meteo = Data.interp(west_east = x, south_north = y, Time=ts)

Resulting in NaN values:

<xarray.Dataset>
Dimensions:      ( routetime: 93)
Coordinates:
    XLONG        (routetime) float64 nan nan nan nan nan ... nan nan nan nan nan
    XTIME        (routetime) float64 nan nan nan nan nan ... nan nan nan nan nan
    XLAT         (routetime) float64 nan nan nan nan nan ... nan nan nan nan nan
Dimensions without coordinates: routetime
Data variables:
    Q2           (routetime) float64 nan nan nan nan nan ... nan nan nan nan nan
    T2           (routetime) float64 nan nan nan nan nan ... nan nan nan nan nan
    U10          (routetime) float64 nan nan nan nan nan ... nan nan nan nan nan
    V10          (routetime) float64 nan nan nan nan nan ... nan nan nan nan nan
    PSFC         (routetime) float64 nan nan nan nan nan ... nan nan nan nan nan
    west_east    (routetime) float64 4.473 4.449 4.457 ... 4.498 4.498 4.498
    south_north  (routetime) float64 52.39 52.4 52.41 52.41 ... 52.4 52.4 52.4
    Time         (routetime) float64 1.539e+09 1.539e+09 ... 1.539e+09 1.539e+09

Can anybody help me in how I could get this to work?

Aleid
  • 21
  • 1
  • This is old, but I believe that you are doing advanced interpolation. Try using ship_route_meteo = Data.sel(west_east = x, south_north = y, Time=ts, method='nearest') – naja Apr 14 '22 at 19:28

0 Answers0