我想对griddata进行一个时态线性插值(= xarray with dimensions:lat,lon,time),这意味着我有一个时间步,其中没有数据,但之前和之后的时间步有信息。我尝试使用scipy.interpolate.griddata,其中我首先为所有的nan数据创建了一个遮罩层,然后用那里的数据对nan进行插值。
虚拟数据:
[0.47942554, 0.48971277, 0.47942554, 0.98971277, 1.23971277],
[0.2171174 , 0.4671174 , 0.7171174 , 0.9671174 , 1.2171174 ],
[0.18102272, 0.43102272, 0.68102272, 0.93102272, 1.18102272],
[0.47942554, 0.38367293, 0.63367293, 0.88367293, 1.13367293]],
[[nan, nan, nan, nan, nan],
[nan, nan, nan, nan, nan],
[nan, nan, nan, nan, nan],
[nan, nan, nan, nan, nan],
[nan, nan, nan, nan, nan]],
[[0.47942554, 0.47942554, 0.97942554, 1.22942554, 1.47942554],
[0.46452136, 0.71452136, 0.96452136, 1.21452136, 1.46452136],
[0.42073549, 0.67073549, 0.92073549, 1.17073549, 0.47942554],
[0.35079033, 0.60079033, 0.85079033, 1.10079033, 1.35079033],
[0.47942554, 0.50903472, 0.75903472, 0.47942554, 1.25903472]]])
我尝试了什么(这个问题的帮助下:(How to use scipy.interpolate.interpn function with xarray (3d), to fill nan gaps? Current Error [The points in dimension 0 must be strictly ascending]):
# ravel all points and find the valid ones
points = da.data.ravel()
valid = ~np.isnan(points)
points_valid = points[valid]
# construct arrays of (x, y, z) points, masked to only include the valid points
xx, yy, zz = np.meshgrid(x, y, z)
xx, yy, zz = xx.ravel(), yy.ravel(), zz.ravel()
xxv = xx[valid]
yyv = yy[valid]
zzv = zz[valid]
# feed these into the interpolator, and also provide the target grid
interpolated = scipy.interpolate.griddata(np.stack([xxv, yyv, zzv]).T, points_valid, (xx, yy, zz), method="linear")
# reshape to match the original array and replace the DataArray values with
# the interpolated data
da.values = interpolated.reshape(da.shape)
但是,在此方法中,空间插值和时间插值之间没有区别,因为仅使用最接近的值(同样使用线性插值-然后只取接下来的3个点)。问题是,由于某种原因,它总是使用空间上而不是时间上最近的点。因此,空间插值看起来很好,但是时间插值看起来很混乱。我想要的只是一个时间插值(没有空间邻居)。
我能改变什么?请帮帮我!
1条答案
按热度按时间wgxvkvu91#
xarray
有一些插值工具。以下示例从文件位置
fname
打开netCDF数据集,并在lon、lat和time上插入param1和param2。我不能确切地告诉你的代码看起来如何,但我相信上面的代码可以引导你找到正确的答案。有关interp的更多细节,请参见xarray docs here。它继承自
scipy.interp