使用scipy.interpolate
子包中RectSphereBivariateSpline
函数中的THIS示例,我想反算具有纬度和经度的数组,以及网格上每个坐标的插值数据值的数组。在
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()
我想我可以使用向量加法(即毕达哥拉斯定理),但这行不通,因为我只有一个值来表示每个度的变化,而不是点
^{pr2}$
似乎可以用于绘图的经纬度矢量是在我们的“geo_interp”函数(“net_lats”和“new_lons”)中生成的。如果在主程序中需要这些向量,则应该在函数之外声明这些向量,或者让函数返回这些生成的向量,以便在主程序中使用。在
希望这有帮助。在
相关问题 更多 >
编程相关推荐