[SciPy-User] Interpolation from a regular grid to a not regular one

Davide davide.lasagna at polito.it
Sat Jun 25 07:19:29 EDT 2011


Ciao Domenico,

Here is some code i wrote to wrap scipy.ndimage.mapcoordinates.


def get_profile( x, y, f, xi, yi, order=3):
     """Interpolate regular data.

     Parameters
     ----------
     x : two dimensional np.ndarray
         an array for the :math:`x` coordinates

     y : two dimensional np.ndarray
         an array for the :math:`y` coordinates

     f : two dimensional np.ndarray
         an array with the value of the function to be interpolated
         at :math:`x,y` coordinates.

     xi : one dimension np.ndarray
         the :math:`x` coordinates of the point where we want
         the function to be interpolated.

     yi : one dimension np.ndarray
         the :math:`y` coordinates of the point where we want
         the function to be interpolated.

     order : int
         the order of the bivariate spline interpolation


     Returns
     -------
     fi : one dimension np.ndarray
         the value of the interpolating spline at :math:`xi,yi`


     """
     conditions = [ xi.min() < x.min(),
                    xi.max() > x.max(),
                    yi.min() < y.min(),
                    yi.max() > y.max() ]

     if True in conditions:
         print "Warning, extrapolation in being done!!"

     dx = x[0,1] - x[0,0]
     dy = y[1,0] - y[0,0]

     jvals = (xi - x[0,0]) / dx
     ivals = (yi - y[0,0]) / dy

     coords = np.array([ivals, jvals])

     return  scipy.ndimage.map_coordinates(f, coords, mode='nearest', 
order=order)


Buona domenica,


Davide

On 06/25/2011 04:23 AM, Zachary Pincus wrote:
>> lats, longs = latitudes and longitudes of a regular grid (shape is (161,201))
>> vals = corresponding values (shape = (161,201))
>>
>> I've got two more 2d numpy arrays which are latitudes and longitudes of a non regular grid (shapes are (1900,2400))
> scipy.ndimage.map_coordinates()
>
> A little confusing, very useful. Sorry I've got no time to provide an example, but it should be what you want.
>
>
>
> On Jun 24, 2011, at 8:02 AM, Domenico Nappo wrote:
>
>> Hi there,
>> hope you can help me.
>>
>> I'm new to SciPy and I'm not aware of all its nice features.
>> I need some indications about how to complete the following task...just giving me some suggestions about which package/methods to use could be enough.
>>
>> I have three 2d numpy arrays representing the followings:
>>
>> lats, longs = latitudes and longitudes of a regular grid (shape is (161,201))
>> vals = corresponding values (shape = (161,201))
>>
>> I've got two more 2d numpy arrays which are latitudes and longitudes of a non regular grid (shapes are (1900,2400))
>>
>> Now, I've got to produce the grid of values for the non regular grid, using interpolation (probably nearest neighbour).
>> I've come out with something using griddata from the matplotlib.mlab module but I'm not sure it's the right way and I don't know how to test the interpolated results...
>>
>> Many thanks in advance.
>>
>> -- 
>> dome
>> _______________________________________________
>> SciPy-User mailing list
>> SciPy-User at scipy.org
>> http://mail.scipy.org/mailman/listinfo/scipy-user
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>




More information about the SciPy-User mailing list