使用RectSphereBivariateSpline
子包中scipy.interpolate
函数的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()
我以为我可能会使用向量加法(即毕达哥拉斯定理),但这不起作用,因为我只有一个值用于每个度数变化,而不是点
pow((pow(data_interp[0,:],2.0)+pow(data_interp[:,0],2.0)),1/2.0)
答案 0 :(得分:1)
似乎可以用于绘图的纬度和经度向量是在我们的“geo_interp”函数(“net_lats”和“new_lons”)中生成的。如果你在主程序中需要这些,你应该在函数之外声明这些向量,或者你应该让函数返回这些生成的向量以在主程序中使用。
希望这有帮助。