Ber*_*haw 6 python subdomain numpy overlap multidimensional-array
我目前正在使用模型输出,我似乎无法想出一种结合两个数据数组的好方法.数组A和B存储不同的数据,每个数据中的条目对应一些空间(x,y)点 - A保存一些参数,B保存模型输出.问题是B是A的空间子部分 - 也就是说,如果模型是针对整个世界的,A会将参数存储在地球上的每个点上,B将仅存储非洲的那些点的模型输出. .
所以我需要找到多少B从A偏移 - 换另一种方式,我需要找到它们开始重叠的索引.因此,如果A.shape =(1000,1500),B是(750:850,200:300)的一部分,还是(783:835,427:440)子部分?我有与A和B相关联的数组,它们存储每个网格点的(x,y)位置.
这似乎是一个简单的问题 - 找到两个数组重叠的位置.我可以用scipy.spatial的KDTree来解决它,但它很慢.有没有更好的想法?
我有与 A 和 B 关联的数组,其中存储每个数组的网格点的 (x,y) 位置。
在这种情况下,答案应该相当简单......
这两个网格是否严格采用相同的网格方案?假设它们是,你可以这样做:
np.argwhere((Ax == Bx.min()) & (Ay == By.min()))
Run Code Online (Sandbox Code Playgroud)
假设两个网格的世界坐标以与网格索引相同的方向增加,这给出了子集网格的左下角。(如果它们不沿同一方向增加(即负数dx或dy),它只会给出其他角之一)
在下面的示例中,我们显然可以从 等计算正确的索引ix = (Bxmin - Axmin) / dx,但假设您有一个更复杂的网格系统,这仍然有效。然而,这是假设两个网格采用相同的网格方案!如果他们不这样做的话,情况会稍微复杂一些......
import numpy as np
# Generate grids of coordinates from a min, max, and spacing
dx, dy = 0.5, 0.5
# For the larger grid...
Axmin, Axmax = -180, 180
Aymin, Aymax = -90, 90
# For the smaller grid...
Bxmin, Bxmax = -5, 10
Bymin, Bymax = 30, 40
# Generate the indicies on a 2D grid
Ax = np.arange(Axmin, Axmax+dx, dx)
Ay = np.arange(Aymin, Aymax+dy, dy)
Ax, Ay = np.meshgrid(Ax, Ay)
Bx = np.arange(Bxmin, Bxmax+dx, dx)
By = np.arange(Bymin, Bymax+dy, dy)
Bx, By = np.meshgrid(Bx, By)
# Find the corner of where the two grids overlap...
ix, iy = np.argwhere((Ax == Bxmin) & (Ay == Bymin))[0]
# Assert that the coordinates are identical.
assert np.all(Ax[ix:ix+Bx.shape[0], iy:iy+Bx.shape[1]] == Bx)
assert np.all(Ay[ix:ix+Bx.shape[0], iy:iy+Bx.shape[1]] == By)
Run Code Online (Sandbox Code Playgroud)