使用子包中函数的THIS示例,我想反向计算以度为单位的纬度和经度数组,以及网格上每个坐标的插值数据值的数组。RectSphereBivariateSpline
scipy.interpolate
创建的插值数据对象RectSphereBivariateSpline
似乎是数据值的u和v分量,网格的每个度数变化都有一个值(本例中纬度 = 180 和经度 = 360)。
那正确吗?
我如何回算纬度、经度及其各自的绘图数据值?
import numpy as np
from scipy.interpolate import RectSphereBivariateSpline
def geo_interp(lats,lons,data,grid_size_deg):
'''We want to interpolate it to a global one-degree grid'''
deg2rad = np.pi/180.
new_lats = np.linspace(grid_size_deg, 180, 180) * deg2rad
new_lons = np.linspace(grid_size_deg, 360, 360) * deg2rad
new_lats, new_lons = np.meshgrid(new_lats, new_lons)
'''We need to set up the interpolator object'''
lut = RectSphereBivariateSpline(lats*deg2rad, lons*deg2rad, data)
'''Finally we interpolate the data. The RectSphereBivariateSpline
object only takes 1-D arrays as input, therefore we need to do some reshaping.'''
data_interp = lut.ev(new_lats.ravel(),
new_lons.ravel()).reshape((360, 180)).T
return data_interp
if __name__ == '__main__':
import matplotlib.pyplot as plt
'''Suppose we have global data on a coarse grid'''
lats = np.linspace(10, 170, 9) # in degrees
lons = np.linspace(0, 350, 18) # in degrees
data = np.dot(np.atleast_2d(90. - np.linspace(-80., 80., 18)).T,
np.atleast_2d(180. - np.abs(np.linspace(0., 350., 9)))).T
'''Interpolate data to 1 degree grid'''
data_interp = geo_interp(lats,lons,data,1)
'''Looking at the original and the interpolated data,
one can see that the interpolant reproduces the original data very well'''
fig = plt.figure()
ax1 = fig.add_subplot(211)
ax1.imshow(data, interpolation='nearest')
ax2 = fig.add_subplot(212)
ax2.imshow(data_interp, interpolation='nearest')
plt.show()
我想我可能会使用向量加法(即毕达哥拉斯定理),但这不起作用,因为每个度数变化我只有一个值,而不是点
pow((pow(data_interp[0,:],2.0)+pow(data_interp[:,0],2.0)),1/2.0)