[英]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.