簡體   English   中英

Python 2D插值

[英]Python 2D Interpolation

我非常感謝有人可以幫助我了解以下問題的2D插值。 我有一個與像素的x和y索引相對應的溫度值列表。 而且我也有對應於選定像素索引的經度和緯度。 現在,我想對我的緯度和經度進行插值,以便獲得與所有像素相對應的緯度和經度,並獲得溫度數據。

#List 1 represents temperature values for each x and y indices of pixels

List1 = [[10,13,17,18,20], [3,5,1,4,5], [13,11,12,11,12]]

#print List1[2][2]

#List 2 represents latitude for just the first, middle and the last indices 
#of pixels
List2=[[2,3,4],[2.4,3.5,6],[2.2,4.5,7]]

#List 2 represents longitude for just the first, middle and the last indices 
#of pixels

List2=[[5,8,12],[4.4,7.5,8.6],[2.5,4.6,7.9]]

#I want to interpolate latitude and longitude values for the unknown 
#indices. 

Scipy具有用於2d插值的interp2d函數。

from scipy import interpolate
x = np.arange(-5.01, 5.01, 0.25)
y = np.arange(-5.01, 5.01, 0.25)
xx, yy = np.meshgrid(x, y)
z = np.sin(xx**2+yy**2)
f = interpolate.interp2d(x, y, z, kind='cubic')

您的xy可以是像素或緯度和經度,而z是您的溫度。 然后,在構造f ,可以使用任意xy調用該函數。 例如:

f(0.1, 0.32)

輸出:

array([0.09988448])

這不是最干凈或最可重用的代碼,但我認為這很清楚:

def interpolate_list(original_list):
    if len(original_list) == 0:
        return []

    interpolated_list = []
    for idx, item in enumerate(original_list[:-1]):
        next_item = original_list[idx+1]
        new_item = (item + next_item) / 2
        interpolated_list.append(item)
        interpolated_list.append(new_item)

    interpolated_list.append(original_list[-1])

    return interpolated_list


list_of_latitude_lists = [[2,3,4],[2.4,3.5,6],[2.2,4.5,7]]
list_of_longitude_lists =[[5,8,12],[4.4,7.5,8.6],[2.5,4.6,7.9]]

list_of_interpolated_latitude_lists = [interpolate_list(latitude_list)
    for latitude_list in list_of_latitude_lists]

list_of_interpolated_longitude_lists = [interpolate_list(longitude_list)
    for longitude_list in list_of_longitude_lists]

最終值:

list_of_interpolated_latitude_lists = [
    [2, 2.5, 3, 3.5, 4],
    [2.4, 2.95, 3.5, 4.75, 6],
    [2.2, 3.35, 4.5, 5.75, 7]
]

list_of_interpolated_longitude_lists = [
    [5, 6.5, 8, 10.0, 12],
    [4.4, 5.95, 7.5, 8.05, 8.6],
    [2.5, 3.55, 4.6, 6.25, 7.9]
]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM