Christopher Bull bio photo

Christopher Bull

Aspiring oceanographer. PhD candidate (UNSW). Big data python enthusiast. Outdoor adventurer.

Email Twitter Github

Climate scientists are always wanting data on different grids. Python docs are typically excellent but I couldn’t find a nice example using rectangular/mesh grids so here it is…

import numpy as np
import scipy.interpolate


#old grid dim
loni=np.array([109.94999695, 110.05000305, 110.15000153])
depi=np.array([3.04677272, 9.45404911, 16.36396599, 23.89871025])

#new grid dim
lon=np.arange(110.,110.3,.1) #NB: 110.2 outside of convex hull of old so will produce nan

#create mesh
X, Y = np.meshgrid(loni, depi)
XI, YI = np.meshgrid(lon,depth)

new_grid=scipy.interpolate.griddata((X.flatten(),Y.flatten()),old_grid_data.flatten() , (XI,YI),method='cubic')

print "this is original"
print old_grid_data.reshape(4,3)
print ""
print "this is interp' by cubic"
print new_grid

print "this is diff"
print new_grid-old_grid_data.reshape(4,3)

NB: the old_grid and data you pass to scipy.interpolate.griddata needs to be flattened or ravel (the error messages are not instructive). You can pass a masked array too!

In category: python