Using your function:
def twoD_interpolate(arr, xmin, xmax, ymin, ymax, x1, y1): """ interpolate in two dimensions with "hard edges" """ ny, nx = arr.shape
Now run some tests:
print test_val(4, 11, z) [ 1.7] print test_val(2, 10, z) [ 3.7] print test_val(2.5, 9, z) [ 4.475] print test_val(2.5, 9.5, z) [ 3.925] #Can even use 1D numpy arrays now print test_val(np.arange(4),np.arange(4)+9,z) [ 6.4 4.5 3. 1.95]
Explain:
np.atleast_1d is a function that ensures that your array has at least 1 dimension. np.array([x1]) will return a 2D array if x1 is a numpy array. This is undesirable.
>>> np.atleast_1d(5) array([5]) >>> np.atleast_1d(np.arange(5)) array([0, 1, 2, 3, 4])
The setting order=1 refers to the order of spline interpolation. In the above example, you showed linear interpolation, therefore, 1 , if you want to take into account more values, you can increase it to achieve the desired effect.
np.vstack used to correctly position the x and y indices. In this terminology, map_coordinates needs data like:
coords=[[y1,y2,y3,... [x1,y2,y3,...]] ycoords,xcoords=['y1','y2','y3'],['x1','x2','x3'] >>> np.vstack((xcoords,ycoords)) array([['y1', 'y2','y3'], ['x1', 'x2','x3']], dtype='|S2')