|
6 | 6 |
|
7 | 7 | import numpy as np |
8 | 8 | import xarray as xr |
9 | | -from scipy.spatial import KDTree |
10 | 9 | from tqdm import tqdm |
11 | 10 |
|
12 | 11 | from parcels._core.utils.time import TimeInterval, maybe_convert_python_timedelta_to_numpy |
13 | 12 | from parcels._reprs import particleset_repr |
14 | 13 | from parcels.application_kernels.advection import AdvectionRK4 |
15 | | -from parcels.basegrid import GridType |
16 | 14 | from parcels.kernel import Kernel |
17 | 15 | from parcels.particle import KernelParticle, Particle, create_particle_data |
18 | 16 | from parcels.tools.converters import convert_to_flat_array |
@@ -308,28 +306,17 @@ def _neighbors_by_coor(self, coor): |
308 | 306 | neighbor_ids = self._data["trajectory"][neighbor_idx] |
309 | 307 | return neighbor_ids |
310 | 308 |
|
311 | | - # TODO: This method is only tested in tutorial notebook. Add unit test? |
312 | 309 | def populate_indices(self): |
313 | | - """Pre-populate guesses of particle ei (element id) indices using a kdtree. |
314 | | -
|
315 | | - This is only intended for curvilinear grids, where the initial index search |
316 | | - may be quite expensive. |
317 | | - """ |
| 310 | + """Pre-populate guesses of particle ei (element id) indices""" |
318 | 311 | for i, grid in enumerate(self.fieldset.gridset): |
319 | | - if grid._gtype not in [GridType.CurvilinearZGrid, GridType.CurvilinearSGrid]: |
320 | | - continue |
321 | | - |
322 | | - tree_data = np.stack((grid.lon.flat, grid.lat.flat), axis=-1) |
323 | | - IN = np.all(~np.isnan(tree_data), axis=1) |
324 | | - tree = KDTree(tree_data[IN, :]) |
325 | | - # stack all the particle positions for a single query |
326 | | - pts = np.stack((self._data["lon"], self._data["lat"]), axis=-1) |
327 | | - # query datatype needs to match tree datatype |
328 | | - _, idx_nan = tree.query(pts.astype(tree_data.dtype)) |
329 | | - |
330 | | - idx = np.where(IN)[0][idx_nan] |
331 | | - |
332 | | - self._data["ei"][:, i] = idx # assumes that we are in the surface layer (zi=0) |
| 312 | + position = grid.search(self.depth, self.lat, self.lon) |
| 313 | + self._data["ei"][:, i] = grid.ravel_index( |
| 314 | + { |
| 315 | + "X": position["X"][0], |
| 316 | + "Y": position["Y"][0], |
| 317 | + "Z": position["Z"][0], |
| 318 | + } |
| 319 | + ) |
333 | 320 |
|
334 | 321 | @classmethod |
335 | 322 | def from_particlefile(cls, fieldset, pclass, filename, restart=True, restarttime=None, **kwargs): |
|
0 commit comments