Skip to content

Commit bb3d5bf

Browse files
reint-fischerreint-fischer
authored andcommitted
Merge remote-tracking branch 'origin/fixing_InvDistInterp_on_grid_corner' into interpolation_tutorial_v4
2 parents e0f9d48 + 8a8f89e commit bb3d5bf

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

src/parcels/interpolators.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,16 +616,26 @@ def XLinearInvdistLandTracer(
616616
eta_b = eta[None, None, None, None, :]
617617
xsi_b = xsi[None, None, None, None, :]
618618

619-
inv_dist = 1.0 / ((eta_b - j_grid) ** 2 + (xsi_b - i_grid) ** 2)
619+
dist2 = (eta_b - j_grid) ** 2 + (xsi_b - i_grid) ** 2
620620

621621
valid_mask = ~land_mask
622+
# Normal inverse-distance weighting
623+
inv_dist = 1.0 / dist2
622624
weighted = np.where(valid_mask, corner_data * inv_dist, 0.0)
623625

624626
val = np.sum(weighted, axis=(0, 1, 2, 3))
625627
w_sum = np.sum(np.where(valid_mask, inv_dist, 0.0), axis=(0, 1, 2, 3))
626628

627629
values[not_all_land] = val[not_all_land] / w_sum[not_all_land]
628630

631+
# If a particle hits exactly one of the 8 corner points, extract it
632+
exact_mask = dist2 == 0 & valid_mask
633+
exact_vals = np.sum(np.where(exact_mask, corner_data, 0.0), axis=(0, 1, 2, 3))
634+
has_exact = np.any(exact_mask, axis=(0, 1, 2, 3))
635+
636+
exact_particles = not_all_land & has_exact
637+
values[exact_particles] = exact_vals[exact_particles]
638+
629639
return values.compute() if is_dask_collection(values) else values
630640

631641

0 commit comments

Comments
 (0)