如何从scipy.interpolate.RectSphereBivariateSpline反推纬度和经度?

0 投票
1 回答
763 浏览
提问于 2025-04-17 14:12

在这个例子中,使用的是来自 scipy.interpolate 子包的 RectSphereBivariateSpline 函数。我想要通过这个函数来反向计算一些数组,这些数组包含了以度数表示的纬度和经度,以及每个坐标在网格上的插值数据值。

这个插值数据对象 RectSphereBivariateSpline 创建出来的似乎是数据值的 uv 组件,每个度数变化对应一个值(在这个例子中,纬度范围是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)

1 个回答

1

看起来可以用来绘图的经纬度向量是在我们的“geo_interp”函数里生成的(分别叫“net_lats”和“new_lons”)。如果你想在主程序中使用这些向量,你要么把这些向量在函数外面声明出来,要么让这个函数把生成的向量返回,这样主程序就能使用了。

希望这对你有帮助。

撰写回答