简体   繁体   English

在python中的网格内插值

[英]Interpolating within a grid in python

I have a number of height values across a grid, specified in a series of lists: 我在一系列列表中指定的网格上有许多高度值:

[3, 1, -2, -3, -3] # x = 0m 
[2, -7, -14, -30, -39] # x = 10m
[46, 22, 5, -2, -8] # x = 20m

The example above shows a 40m x 20m grid in the form 上面的示例显示了40m x 20m的网格形式

[x=0y=0, x=0y=10...][x=10y=0, x=10y=10...] etc.

I need to calculate the height at a specific x,y coordinate. 我需要在特定的x,y坐标处计算高度。 I have looked at various interpolate functions and example but can't make any sense out of them - please help! 我看过各种插值函数和示例,但对它们没有任何意义-请帮忙!

An example would be the height at x = 5m, y = 5m in teh grid above, which would be in the middle of the values 3, 1 2 and -7 (-1ish?) 一个示例是上面网格中x = 5m,y = 5m处的高度,该高度将位于值3、1 2和-7(-1ish?)的中间。

Thanks 谢谢

You can use scipy.interpolate.interp2d . 您可以使用scipy.interpolate.interp2d The code below should do the job: 下面的代码可以完成这项工作:

import numpy as np
from scipy.interpolate import interp2d

# Original data (e.g. measurements)
a = [3, 1, -2, -3, -3]
b = [2, -7, -14, -30, -39]
c = [46, 22, 5, -2, -8]

x = [0, 10, 20]             # x-coordinates
y = [0, 10, 20, 30, 40]     # y-coordinates

# Organise data in matrix
z = np.vstack([a, b, c]).T

# Create interpolation function
f_z = interp2d(x, y, z)

# Desired x/y values
x_interp = 5
y_interp = 5

# Collect interpolated z-value
z_interp = f_z(x_interp, y_interp)
print(z_interp)  # (result: [-0.25])

Solution: Transform into 2D array 解决方案:转换为2D阵列

import numpy as np

a = [3, 1, -2, -3, -3] # x = 0m 
b = [2, -7, -14, -30, -39] # x = 10m
c = [46, 22, 5, -2, -8] # x = 20m

all_lists = [a,b,c]


grid = np.vstack(all_lists) # Edit: thanks @jochen for better code

>>print(grid.shape)
(3, 5)

Now you can access grid via x,y coordinates 现在您可以通过x,y坐标访问网格

grid[1,1] and etc grid[1,1]

Based on this, you can create logic. 基于此,您可以创建逻辑。

If x = 5, y = 5, then you can create square corners indexes by this: 如果x = 5,y = 5,则可以通过以下方法创建角索引:

indexes: x/5 +0, x/5+1, y/5,y/5+1 ---> by combining them you get [0,0],[0,1],[1,0],[1,1] 索引:x / 5 + 0,x / 5 + 1,y / 5,y / 5 + 1 --->通过组合它们,您会得到[0,0],[0,1],[1,0], [1,1]

At the end you just feed grid with those indexes 最后,您只需使用这些索引输入网格

SUM = grid[0,0] + grid[0,1] + grid[1,0] + grid[1,1]

You can achieve this with interpolate.gridddata function from scipy . 您可以使用scipy interpolate.gridddata函数来scipy

With below code you can get the any interpolation you want from your grid. 使用以下代码,您可以从网格中获取所需的任何插值。

import itertools
import numpy as np
from scipy.interpolate import griddata

dataX0 = [3, 1, -2, -3, -3] # x = 0m
dataX10 = [2, -7, -14, -30, -39] # x = 10m
dataX20 = [46, 22, 5, -2, -8] # x = 20m
data = dataX0 + dataX10 + dataX20
points = list(itertools.product(range(0,30,10),range(0,50,10)))

outputPoint = (5,5)
outputValue = griddata(points=points,values=data, xi=outputPoint, method="cubic")
print outputValue

The example above give you the interpolation at ouputPoint (5,5) . 上面的示例为您在ouputPoint (5,5)处进行插值。 And the output would give: 输出将给出:

>>> 
-2.78976054957

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM