2
votes

I'm trying to interpolate information from a 3D dataset (lon,lat,time) ussing directly xarray.

When I made a simply interpolation with only one point I have no problem at all.

lat = [44.25]
lon = [-4.5]
t = datetime.strptime('2000-02-28 01:00:00', '%Y-%m-%d %H:%M:%S')

ds = xr.open_dataset('file.nc')
vx = ds['uo_surface'].interp(longitude=lon, latitude=lat, time=t)

But now I'm trying to interpolate in the same way several points and the result of this operation following the same syntax shows more results of what I will expected.

lat = [44.25, 45.25]
lon = [-4.5, -5]
t = datetime.strptime('2000-02-28 01:00:00', '%Y-%m-%d %H:%M:%S')

ds = xr.open_dataset('Currents\oceanTESEO.nc')
vx = ds['uo_surface'].interp(longitude=lon, latitude=lat, time=[t, t])

The result is this array:

array([[[0.01750018, 0.05349977],
        [0.03699994, 0.11299999]],

       [[0.01750018, 0.05349977],
        [0.03699994, 0.11299999]]])

However, I expect only 2 values, one for each (lon,lat,t) point. Do I have to implement a loop to do that? I suposse this feature is already included in xarray. Do you know other way to calculate this sort of point interpolation faster and with 4D datarrays (lon,lat,z,time)?

Thank you in advance!!!

1

1 Answers

2
votes

Yes, it is possible.

It is a bit "less intuitive" at first sight, but powerful and documented here: http://xarray.pydata.org/en/stable/interpolation.html#advanced-interpolation

The call you need to make is:

ds['uo_surface'].interp(longitude=('z', lon), latitude=('z', lat), 
                        time=('z', [t, t]))

This realizes "vectorized" indexing, while in your previous call you were doing "orthogonal" indexing. For more information see http://xarray.pydata.org/en/stable/indexing.html#vectorized-indexing