[英]NumPy Interpolate Between Two 2-D Arrays At Various Timesteps
我有一对来自网格数据集(GeoTIFF 格式)的二维数组,它们都具有完全相同的分辨率和行/列数。
假设数组#1 的时间步长为+0 小时,数组#2 的时间步长为+3 小时。 我希望在线性插值中以 [1,2] 的间隔插入和创建额外的数组。
我已经仔细阅读了这个关于利用 scipy 通过堆叠的一维数组实现这一点的链接,但如果可能的话,我更愿意在 NumPy 中执行它。
生成这些处于预定间隔的附加插值二维数组的最佳方法或起点是什么?
我相信你只是在问如何做到这一点:
import numpy as np
t0 = 0
a0 = np.array([[0,0,0],[1,1,1],[2,2,2]], dtype=np.float64)
t3 = 3
a3 = np.array([[3,3,3],[0,5,0],[-2,0,2]], dtype=np.float64)
aDiff = a3 - a0
t1 = 1
a1 = a0 + aDiff * ((t1 - t0) / (t3 - t0))
t2 = 2
a2 = a0 + aDiff * ((t2 - t0) / (t3 - t0))
print(a0)
print(a1)
print(a2)
print(a3)
输出:
[[0. 0. 0.]
[1. 1. 1.]
[2. 2. 2.]]
[[1. 1. 1. ]
[0.66666667 2.33333333 0.66666667]
[0.66666667 1.33333333 2. ]]
[[ 2. 2. 2. ]
[ 0.33333333 3.66666667 0.33333333]
[-0.66666667 0.66666667 2. ]]
[[ 3. 3. 3.]
[ 0. 5. 0.]
[-2. 0. 2.]]
如果你想要一个更通用的解决方案,你可以这样做:
示例代码:
import numpy as np
tFirst, aFirst = 0, np.array([[0,0,0],[1,1,1],[2,2,2]], dtype=np.float64)
tNext, aNext = 3, np.array([[3,3,3],[0,5,0],[-2,0,2]], dtype=np.float64)
tInterp = np.array([1, 2])
aDiff1D = np.reshape(aNext - aFirst, (1, np.size(aFirst)))
aDiffRepeated = np.repeat(aDiff1D, np.size(tInterp), axis=0)
aStep = aDiffRepeated * ((tInterp[:, None] - tFirst) / (tNext - tFirst))
aInterp = np.repeat(np.reshape(aFirst, (1, np.size(aFirst))), np.size(tInterp), axis = 0) + aStep
aInterp = np.reshape(aInterp, (np.size(tInterp), aFirst.shape[0], aFirst.shape[1]))
a1 = aInterp[0]
a2 = aInterp[1]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.