【发布时间】:2012-12-16 16:37:37
【问题描述】:
我需要在 4 个维度(纬度、经度、高度和时间)中线性插入温度数据。
点的数量相当多(360x720x50x8),我需要一种快速的方法来计算数据范围内空间和时间任意点的温度。
我曾尝试使用scipy.interpolate.LinearNDInterpolator,但使用 Qhull 进行三角测量在矩形网格上效率低下,需要数小时才能完成。
通过阅读此SciPy ticket,解决方案似乎是使用标准interp1d 实现一个新的nd 插值器来计算更多的数据点,然后对新数据集使用“最近邻”方法。
但是,这又需要很长时间(几分钟)。
有没有一种快速的方法可以在 4 维的矩形网格上插入数据而无需花费几分钟的时间来完成?
我想过使用interp1d 4 次 而不 计算更高的点密度,但留给用户使用坐标调用,但我不知道如何这样做。
否则,我可以选择根据自己的需要编写自己的 4D 插值器吗?
这是我用来测试的代码:
使用scipy.interpolate.LinearNDInterpolator:
import numpy as np
from scipy.interpolate import LinearNDInterpolator
lats = np.arange(-90,90.5,0.5)
lons = np.arange(-180,180,0.5)
alts = np.arange(1,1000,21.717)
time = np.arange(8)
data = np.random.rand(len(lats)*len(lons)*len(alts)*len(time)).reshape((len(lats),len(lons),len(alts),len(time)))
coords = np.zeros((len(lats),len(lons),len(alts),len(time),4))
coords[...,0] = lats.reshape((len(lats),1,1,1))
coords[...,1] = lons.reshape((1,len(lons),1,1))
coords[...,2] = alts.reshape((1,1,len(alts),1))
coords[...,3] = time.reshape((1,1,1,len(time)))
coords = coords.reshape((data.size,4))
interpolatedData = LinearNDInterpolator(coords,data)
使用scipy.interpolate.interp1d:
import numpy as np
from scipy.interpolate import LinearNDInterpolator
lats = np.arange(-90,90.5,0.5)
lons = np.arange(-180,180,0.5)
alts = np.arange(1,1000,21.717)
time = np.arange(8)
data = np.random.rand(len(lats)*len(lons)*len(alts)*len(time)).reshape((len(lats),len(lons),len(alts),len(time)))
interpolatedData = np.array([None, None, None, None])
interpolatedData[0] = interp1d(lats,data,axis=0)
interpolatedData[1] = interp1d(lons,data,axis=1)
interpolatedData[2] = interp1d(alts,data,axis=2)
interpolatedData[3] = interp1d(time,data,axis=3)
非常感谢您的帮助!
【问题讨论】:
-
我对python一无所知,但你应该看看quadrilinear或quadricubic插值。
-
您对此有任何其他语言的想法吗?谢谢
-
在任何语言中实现单点的四线性插值应该相当容易。
-
不,他们不是。 3 个维度是恒定的(0.5、0.5、1),但高度维度并不总是在同一点采样,因此该轴上的网格不规则。
-
不完全是。我有关于纬度、经度、压力和时间的常规网格的温度数据。但是,我的目标是拥有一个可以这样调用的函数:getTemperature(lat,lon,ALT,time)。我所拥有的是与温度相同的网格上的 ALTITUDE 数据,因此具有相同的纬度、经度、压力、时间的规则网格。现在,我对该方法的实现会查找给定纬度、经度、时间的两个最接近高度值的压力指数。然后它使用这些高度值和两个压力指数在温度矩阵中构建 4D 超四面体并插值...
标签: python numpy scipy interpolation