python - scipy.interpolate.griddata sensitivity to original grid -
i using scipy.interpolate.griddata in order interpolate data original grid sub-region of grid @ higher resolution. in order speed computation, instead of using whole original data, used part contains target region (with buffer). surprise, obtain different results if use linear interpolation, in middle of subregion.
the code follows illustrate this. wondering if expected or if bug in griddata. notice if use 'nearest' instead of 'linear' in code differences null, expected.
any appreciated.
# modification expample 1 of # http://scipy-cookbook.readthedocs.io/item/matplotlib_gridding_irregularly_spaced_data.html # show problems griddata import numpy np scipy.interpolate import griddata import matplotlib.pyplot plt import numpy.ma ma # generate inital fields on 2d grid x = np.arange(-2,2.1,0.1) y = np.arange(-2,2.1,0.1) x2d, y2d = np.meshgrid(x,y) z = np.abs(x*np.exp(-x2d**2-y2d**2)) + 1 # define target grid # (sub region of initial grid finer mesh) x_target = np.arange(-1,1,0.01) y_target = np.arange(-1,1,0.01) x2d_target, y2d_target = np.meshgrid(x_target, y_target) # define interpolation function def f_int(data, x, y, xt, yt): nj, ni = data.shape nij = ni * nj zi = data.reshape((nij)) xi = x.ravel() yi = y.ravel() xo = xt.ravel() yo = yt.ravel() zo = griddata((xi, yi), zi, (xo, yo), method = 'linear', fill_value = np.nan) zo = zo.reshape(xt.shape) return zo # interpolate on target grid using whole initial field z_int_full = f_int(z, x2d, y2d, x2d_target, y2d_target) # interpolate on taget grid using subset of initial field buffer = 5 x2d_sub = x2d[buffer:-buffer, buffer:-buffer] y2d_sub = y2d[buffer:-buffer, buffer:-buffer] z_sub = z[buffer:-buffer, buffer:-buffer] z_int_sub = f_int(z_sub, x2d_sub, y2d_sub, x2d_target, y2d_target) # show results fig = plt.figure() fig.set_size_inches(fig.get_size_inches() * 2) ax1 = fig.add_subplot(221) plt.pcolormesh(x2d, y2d, z) plt.colorbar() plt.title('initial data') ax2 = fig.add_subplot(222) plt.pcolormesh(x2d_target, y2d_target, z_int_full) plt.colorbar() plt.title('interpolate full grid') ax3 = fig.add_subplot(223) plt.pcolormesh(x2d_target, y2d_target, z_int_sub) plt.colorbar() plt.title('interpolate sub grid') ax4 = fig.add_subplot(224) diff = (z_int_full - z_int_sub) rel = diff / z_int_full * 100 plt.pcolormesh(x2d_target, y2d_target, rel) plt.colorbar() plt.title('relative difference between \n interpolated data [%]') plt.show()
edit added figure produced code. can see on bottom right graph, there relative error of order of 0.1% not expecting. tried simplify problem formulate question on data working on, translate difference of order of 1 deg_k interpolated surface temperature, problem me.
Comments
Post a Comment