Commit 0428acef authored by Sævik Pål Næverlid's avatar Sævik Pål Næverlid
Browse files

Get correct results when corner points

parent 537a3a5c
......@@ -890,11 +890,11 @@ def nearest_unmasked(mask, i, j):
j_neigh = np.clip(j_neigh_raw, 0, mask.shape[0] - 1)
# Compute distance to origin
dist = np.abs(i_neigh - i) + np.abs(j_neigh - j)
dist_mask = np.ma.masked_array(dist, mask[j_neigh, i_neigh])
dist2 = (i_neigh - i)**2 + (j_neigh - j)**2
dist2_mask = np.ma.masked_array(dist2, mask[j_neigh, i_neigh])
# Find coordinates of closest unmasked cell
idx = dist_mask.argmin(axis=0)
idx = dist2_mask.argmin(axis=0)
i_close = i_neigh[idx, np.arange(len(idx))]
j_close = j_neigh[idx, np.arange(len(idx))]
......
......@@ -18,3 +18,11 @@ class Test_nearest_unmasked:
ii, jj = gridforce.nearest_unmasked(mask_south, i, j)
assert ii.tolist() == [0, 1, 3]
assert jj.tolist() == [0, 0, 0]
def test_correct_when_corner(self):
mask = np.array([[0, 0, 0, 0], [0, 1, 1, 1], [0, 1, 1, 0]])
i = np.array([0.51, 0.51, 0.99, 1.49, 1.51, 2.00, 3.00])
j = np.array([0.52, 0.98, 0.52, 1.01, 1.01, 1.01, 1.01])
ii, jj = gridforce.nearest_unmasked(mask, i, j)
assert ii.tolist() == [0, 0, 1, 1, 2, 2, 3]
assert jj.tolist() == [1, 1, 0, 0, 0, 0, 2]
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment