编辑:更新了内容以反映您的澄清。您的问题现在更加清楚了,谢谢!
基本上,您只是想在任意点插入2D数组。
scipy.ndimage.map_coordinates是您想要的…。
据我所知,您有一个二维数组的“ z”值,在每个方向上的范围从xmin到xmax,从ymin到ymax。
您想要从数组边缘返回值的那些边界坐标之外的任何东西。
map_coordinates有几个选项可以处理网格边界之外的点,但是它们都不能够完全满足您的要求。取而代之的是,我们可以将边界之外的任何内容设置为位于边缘,并照常使用map_coordinates。
因此,要使用map_coordinates,您需要打开实际的coodinates:
| <1 2 3 4 5+-------|----------------------------<10000 | 3.6 6.5 9.1 11.5 13.820000 | 3.9 7.3 10.0 13.1 15.920000+ | 4.5 9.2 12.2 14.8 18.2
进入索引坐标:
| 0 1 2 3 4-------|---------------------------- 0 | 3.6 6.5 9.1 11.5 13.8 1 | 3.9 7.3 10.0 13.1 15.9 2 | 4.5 9.2 12.2 14.8 18.2
请注意,您的边界在每个方向上的行为都不同…在x方向上,事物的行为很平滑,但是在y方向上,您要求的是“硬”中断,其中y = 20000-> 3.9,但是y
= 20000.000001-> 4.5。
举个例子:
import numpy as npfrom scipy.ndimage import map_coordinates#-- Setup ---------------------------z = np.array([ [3.6, 6.5, 9.1, 11.5, 13.8], [3.9, 7.3, 10.0, 13.1, 15.9], [4.5, 9.2, 12.2, 14.8, 18.2] ])ny, nx = z.shapexmin, xmax = 1, 5ymin, ymax = 10000, 20000# Points we want to interpolate atx1, y1 = 1.3, 25000x2, y2 = 0.2, 50000x3, y3 = 2.5, 15000# To make our lives easier down the road, let's # turn these into arrays of x & y coordsxi = np.array([x1, x2, x3], dtype=np.float)yi = np.array([y1, y2, y3], dtype=np.float)# Now, we'll set points outside the boundaries to lie along an edgexi[xi > xmax] = xmaxxi[xi < xmin] = xmin# To deal with the "hard" break, we'll have to treat y differently, # so we're ust setting the min here...yi[yi < ymin] = ymin# We need to convert these to (float) indicies# (xi should range from 0 to (nx - 1), etc)xi = (nx - 1) * (xi - xmin) / (xmax - xmin)# Deal with the "hard" break in the y-directionyi = (ny - 2) * (yi - ymin) / (ymax - ymin)yi[yi > 1] = 2.0# Now we actually interpolate# map_coordinates does cubic interpolation by default, # use "order=1" to preform bilinear interpolation instead...z1, z2, z3 = map_coordinates(z, [yi, xi])# Display the resultsfor X, Y, Z in zip((x1, x2, x3), (y1, y2, y3), (z1, z2, z3)): print X, ',', Y, '-->', Z
这样产生:
1.3 , 25000 --> 5.18073750.2 , 50000 --> 4.52.5 , 15000 --> 8.12252371652
希望有帮助…