将二维网格数据的时间序列高效地插值到一个新的二维网格中



我有一个带时间轴的二维网格数据的numpy数组,所以我的数组的形状是(nsteps,ny,nx(

我试图将数据从这个网格插值到一个稍微不同的网格(不同的分辨率,因此节点点(。

我能够通过做到这一点

import numpy as np
from scipy.interpolate import RectBivariateSpline
#some example arrays
p_dat = np.random.random((10, 182, 361)) #old grid, 182rows, 361cols
w_dat = np.random.random((10, 200, 400)) #new grid, 200rows, 400cols
#the grids
x0  = np.linspace(0, 360, 361) #old
y0  = np.linsapce(-90, 90, 182) #old
x   = np.linspace(0, 360, 400) #new
y   = np.linspace(-90, 90 , 200) #new
#new array with 2d shape of w_dat
out = np.full((10, 200, 400), np.nan)
#interpolate one timestep at a time
for i in range(out.shape[0]):
interp = RectBivariateSpline(y0, x0, p_dat[i])
dat = interp(y, x)
out[i,:,:] = dat

有没有一种方法可以避免这个循环,并在第0轴上对插值进行矢量化?

您有任何数据可以添加到您的问题中吗?这将使我们能够更仔细地观察。

我认为np.apply_along_axis可能在这里工作。

现在我只能提供这种未经测试的方法:

def helper(idx: int):
interp = RectBivariateSpline(pres.geometry.y, pres.geometry.x, p_dat[idx])
dat = interp(wind.geometry.y, wind.geometry.x)
return dat
out = np.apply_along_axis(helper, axis=0, arr=np.arange(out.shape[0]))

编辑:由于缺乏测试数据,我无法评论加速,但这与map(helper, np.arange(out.shape[0]))非常相似。本质上,它只是隐藏了循环。