Eth*_*xxx 2 python interpolation numpy scipy
我想将随机散布在球体表面上的数据插入到规则的经度/纬度网格上。我试图SmoothSphereBivariateSpline()从scipy.interpolate包中做到这一点(见下面的代码)。
import numpy as np
from scipy.interpolate import SmoothSphereBivariateSpline
#Define the input data and the original sampling points
NSamp = 2000
Theta = np.random.uniform(0,np.pi,NSamp)
Phi = np.random.uniform(0,2*np.pi, NSamp)
Data = np.ones(NSamp)
Interpolator = SmoothSphereBivariateSpline(Theta, Phi, Data, s=3.5)
#Prepare the grid to which the input shall be interpolated
NLon = 64
NLat = 32
GridPosLons = np.arange(NLon)/NLon * 2 * np.pi
GridPosLats = np.arange(NLat)/NLat * np.pi
LatsGrid, LonsGrid = np.meshgrid(GridPosLats, GridPosLons)
Lats = LatsGrid.ravel()
Lons = LonsGrid.ravel()
#Interpolate
Interpolator(Lats, Lons)
Run Code Online (Sandbox Code Playgroud)
但是,当我执行此代码时,它给了我以下错误:
ValueError: Error code returned by bispev: 10
Run Code Online (Sandbox Code Playgroud)
有谁知道问题是什么以及如何解决它?这是一个错误还是我做错了什么?
小智 5
在SmoothSphereBivariateSpline__call__方法的文档中,注意grid标志(其他一些插值器也有)。如果为 True,则可以理解您正在输入要从中形成网格的一维数组。这是默认值。但是您已经meshgrid从一维数组中创建了 a ,因此此默认行为不适用于您的输入。
解决方案:使用
Interpolator(Lats, Lons, grid=False)
Run Code Online (Sandbox Code Playgroud)
或者,哪个更简单更好:
Interpolator(GridPosLats, GridPosLons)
Run Code Online (Sandbox Code Playgroud)
后者将以网格形式(二维数组)返回数据,这比第一个版本获得的扁平化数据更有意义。