Skip to content

Commit

Permalink
Merge pull request #1495 from dicaearchus/kdtree-with-nans
Browse files Browse the repository at this point in the history
running parcels in a grid with a coordinate grid with missing values (nans)
  • Loading branch information
erikvansebille authored Jan 12, 2024
2 parents 6507fe3 + eba91d3 commit 2d17966
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions parcels/particleset.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,11 +450,14 @@ def populate_indices(self):
continue

tree_data = np.stack((grid.lon.flat, grid.lat.flat), axis=-1)
tree = KDTree(tree_data)
IN = np.all(~np.isnan(tree_data), axis=1)
tree = KDTree(tree_data[IN, :])
# stack all the particle positions for a single query
pts = np.stack((self.particledata.data['lon'], self.particledata.data['lat']), axis=-1)
# query datatype needs to match tree datatype
_, idx = tree.query(pts.astype(tree_data.dtype))
_, idx_nan = tree.query(pts.astype(tree_data.dtype))

idx = np.where(IN)[0][idx_nan]
yi, xi = np.unravel_index(idx, grid.lon.shape)

self.particledata.data['xi'][:, i] = xi
Expand Down

0 comments on commit 2d17966

Please sign in to comment.