[英]Python 2D Interpolation
I would be very much grateful of someone can someone help me with understand 2D interpolation for the below mentioned problem. 我非常感谢有人可以帮助我了解以下问题的2D插值。 I have a list of temperature values corresponding to x and y indices of pixels. 我有一个与像素的x和y索引相对应的温度值列表。 And also I have latitudes and longitudes corresponding to selected indices of pixels. 而且我也有对应于选定像素索引的经度和纬度。 Now I want to interpolate my latitudes and longitudes, so that I can get latitudes and longitudes corresponding to all the pixels, that I have temperature data. 现在,我想对我的纬度和经度进行插值,以便获得与所有像素相对应的纬度和经度,并获得温度数据。
#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 has an interp2d function for 2d interpolation. 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')
Your x
and y
can be your pixels or latitude and longitude and z
will be your temperature. 您的x
和y
可以是像素或纬度和经度,而z
是您的温度。 Then after you construct f
, that is a function you can call with any arbitrary x
and y
. 然后,在构造f
,可以使用任意x
和y
调用该函数。 For example: 例如:
f(0.1, 0.32)
Outputs: 输出:
array([0.09988448])
This is not the cleanest or most reusable code, but I think it is clear enough: 这不是最干净或最可重用的代码,但我认为这很清楚:
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]
Final values: 最终值:
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.