Interpolating a variable with regular grid to a location not on the regular grid with Python scipy interpolate.interpn value error
<p>I have a variable from a netcdf file it is a function of time, height, lon, and lat of regular gridded data: U[time,height,lon,lat]. I want to interpolate this variable to a defined location of lon_new,lat_new that is not on the regular grid (it is in between grid points). I want to be able to have the variable U[0,0,lon_new,lat_new] in terms of the single interpolated location. </p>

<p>I read up on the scipy interpolation functions and think that scipy.interpolate.interpn is the function that I want to use. I attempted to do a simple example of this function but keep getting an error. </p>

<pre><code>x_points = [1,2,3,4] #lets call this list of lons on the grid
y_points = [1,2,3,4] #lets call this list of lats on the grid

#Get the lon,lat pairs
point_pairs=[]
for i in x_points:
    for j in y_points:
        points = [i,j]
        point_pairs.append(points)
print point_pairs
print np.shape(point_pairs)

[[1, 1], [1, 2], [1, 3], [1, 4], [2, 1], [2, 2], [2, 3], [2, 4], [3, 1], [3,  2], [3, 3], [3, 4], [4, 1], [4, 2], [4, 3], [4, 4]]
(16L, 2L)

xi = (2.5,2.5) #point in between grid points that I am interested in getting     the interpolated value
xi=np.array(xi)
print xi
print np.shape(xi)

[ 2.5  2.5]
(2L,)

values = np.ones(16) #array of values at every grid point Let's say I loop   over every grid point and get the value at each one
print values

[ 1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]

interpolated_value = interpolate.interpn(point_pairs, values, xi, method='linear')

ValueError: There are 16 point arrays, but values has 1 dimensions
</code></pre>

<p>You can use any appropriate multivariate interpolation function from <a href="http://docs.scipy.org/doc/scipy/reference/interpolate.html" rel="nofollow">scipy</a>. With corrections below your example produces proper result.</p>

<pre><code># -*- coding: utf-8 -*-

import numpy as np
from scipy import interpolate

x_points = np.array([1, 2, 3, 4])
y_points = np.array([1, 2, 3, 4])
values = np.ones((4, 4))   # 2 dimensional array
xi = np.array([2.5, 2.5])

interpolated_value = interpolate.interpn((x_points, y_points), values, xi, method='linear')
print(interpolated_value)
</code></pre>

python scipy interpolation