Scipy - 从一个不规则网格到另一个不规则间隔网格的数据插值
我正在努力解决两个网格之间的插值问题,并且找不到适合我的问题的解决方案。
我有 2 个不同的 2D 网格,其中节点点由它们的 X 和 Y 坐标定义。网格本身不是矩形,而是或多或少形成一个平行四边形(因此 (i,j) 的 X 坐标与 (i,j+1) 不同,而 (i,j) 的 Y 坐标为与 (i+1,j) 的 Y 坐标不同。 两个网格的形状均为 37*5,并且几乎完全重叠。
对于第一个网格,每个点都有 X 坐标、Y 坐标和压力值。现在我想将第一个网格的压力分布插值到第二个网格上(其中每个点的 X 和 Y 也是已知的)。
我尝试了不同的插值方法,但由于我的网格分布不规则,我的最终结果永远不正确。网格点。 interp2d 或 griddata 等函数需要一个一维数组作为输入,但如果我这样做,插值的解决方案是错误的(即使我在原始网格上再次插值原始网格的压力值,新的压力值也与原始网格相距数英里) 但
对于不同不规则网格上的一维插值,我使用:
def interpolate(X, Y, xNew):
if xNew<X[0]:
print 'Interp Warning :', xNew,'is under the interval [',X[0],',',X[-1],']'
yNew = Y[0]
elif xNew>X[-1]:
print 'Interp Warning :', xNew,'is above the interval [',X[0],',',X[-1],']'
yNew = Y[-1]
elif xNew == X[-1] : yNew = Y[-1]
else:
ind = numpy.argmax(numpy.bitwise_and(X[:-1]<=xNew,X[1:]>xNew))
yNew = Y[ind] + ((xNew-X[ind])/(X[ind+1]-X[ind]))*(Y[ind+1]-Y[ind])
return yNew
对于二维,我认为 griddata 会更容易使用,其中我的输入是网格的二维数组。数据?
I am struggling with the interpolation between two grids, and I couldn't find an appropriate solution for my problem.
I have 2 different 2D grids, of which the node points are defined by their X and Y coordinates. The grid itself is not rectangular, but forms more or less a parallelogram (so the X-coordinate for (i,j) is not the same as (i,j+1), and the Y coordinate of (i,j) is different from the Y coordinate of (i+1,j).
Both grids have a 37*5 shape and they overlap almost entirely.
For the first grid I have for each point the X-coordinate, the Y-coordinate and a pressure value. Now I would like to interpolate this pressure distribution of the first grid on the second grid (of which also X and Y are known for each point.
I tried different interpolation methods, but my end result was never correct due to the irregular distribution of my grid points.
Functions as interp2d or griddata require as input a 1D array, but if I do this, the interpolated solution is wrong (even if I interpolate the pressure values from the original grid again on the original grid, the new pressure values are miles away from the original values.
For 1D interpolation on different irregular grids I use:
def interpolate(X, Y, xNew):
if xNew<X[0]:
print 'Interp Warning :', xNew,'is under the interval [',X[0],',',X[-1],']'
yNew = Y[0]
elif xNew>X[-1]:
print 'Interp Warning :', xNew,'is above the interval [',X[0],',',X[-1],']'
yNew = Y[-1]
elif xNew == X[-1] : yNew = Y[-1]
else:
ind = numpy.argmax(numpy.bitwise_and(X[:-1]<=xNew,X[1:]>xNew))
yNew = Y[ind] + ((xNew-X[ind])/(X[ind+1]-X[ind]))*(Y[ind+1]-Y[ind])
return yNew
but for 2D I thought griddata would be easier to use. Does anyone have experience with an interpolation where my input is a 2D array for the mesh and for the data?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
再看一下 interp2d。 http://docs.scipy.org/ scipy/docs/scipy.interpolate.interpolate.interp2d/#scipy-interpolate-interp2d
请注意“参数”下的“x,y”部分。 'x' 和 'y' 是松散意义上的一维,但它们可以是扁平数组。
应该是这样的:
我本以为你可以将扁平的“xnew”和“ynew”数组传递给“f()”,但我无法让它工作。 'f()' 函数将接受行、列语法,但这对您没有用。由于“f()”的限制,您必须将“znew”作为循环的一部分进行评估 - 可能应该查看 nditer。当“(xnew,ynew)”位于“(x,y)”域之外时,还要确保它能够执行您想要的操作。
Have another look at interp2d. http://docs.scipy.org/scipy/docs/scipy.interpolate.interpolate.interp2d/#scipy-interpolate-interp2d
Note the second example in the 'x,y' section under 'Parameters'. 'x' and 'y' are 1-D in a loose sense but they can be flattened arrays.
Should be something like this:
I would have thought you could pass flattened 'xnew' and 'ynew' arrays to 'f()' but I couldn't get that to work. The 'f()' function would accept the row, column syntax though, which isn't useful to you. Because of this limitation with 'f()' you will have to evaluate 'znew' as part of a loop - might should look at nditer for that. Make sure also that it does what you want when '(xnew,ynew)' is outside of the '(x,y)' domain.