简体   繁体   中英

scipy ND Interpolating over NaNs

I have been trouble working out how to use the scipy.interpolate functions (either LinearNDInterpolator, griddata or Preferably NearestNDInterpolator)

There are some tutorials online but i am confused what form my data needs to be in. The online documentation for nearestND is terrible.

The function asks for:

    x : (Npoints, Ndims) ndarray of floats
         Data point coordinates.
    y : (Npoints,) ndarray of float or complex
         Data point values.

I have data in the form: lat,long,data,time held within an xarray dataset. There are some gaps in the data I would like to fill in.

I don't understand how to tell the function my x points. i have tried (lat,long) as a tuple and np.meshgrid(lat,long) but can't seem to get it going.

Any help on how i can pass my lat,long coordinates into the function? Bonus points for time coordinates as well to make the estimates more robust through the third dimension.

Thanks!

i have tried (lat,long) as a tuple

If lat and long are 1D arrays or lists, try this:

points = np.array((lat, long)).T # make a 2D array of shape Npoints x 2
nd = NearestNDInterpolator(points, data)

The you can compute interpolated values as nd(lat1, long1) , etc.

Scipy provides multivariate interpolation methods for both unstructured data and data point regularly placed on a grid. Unstructured data means the data could be provided as a list of non-ordered points. It seems that your data is structured: it is an array of size (480, 2040). However, the NearestNDInterpolator works on unstructured data. The flatten method can be used to transform the array to a list (1d) of value (of length 480*2040). The same have to be done for the coordinates. meshgrid is used to have the coordinates for every points of the grid, and again flatten is used to obtain a "list" of 2d coordinates (an array of shape 480*2040 x 2).

Here is an example which go from structured data to unstructured:

import numpy as np

lat = np.linspace(2, 6, 10)
lon = np.linspace(5, 9, 14)

latM, lonM = np.meshgrid(lat, lon)  # M is for Matrix

dataM = np.sin(latM)*np.cos(lonM)  # example of data, Matrix form

from scipy.interpolate import NearestNDInterpolator

points = np.array((latM.flatten(), lonM.flatten())).T
print( points.shape )
# >>> (140, 2)

f_nearest = NearestNDInterpolator(points, dataM.flatten())
f_nearest(5, 5)

Working with NaNs should not be a big problem in this case, because it is just a missing point in the list, except that the coordinates of the missing points have to be removed from the list too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM