|
9 | 9 | from parcels import xgcm |
10 | 10 | from parcels._index_search import _search_indices_curvilinear_2d |
11 | 11 | from parcels.basegrid import BaseGrid |
| 12 | +from parcels.spatialhash import SpatialHash |
12 | 13 | from parcels.tools.statuscodes import FieldOutOfBoundError, FieldOutOfBoundSurfaceError |
13 | 14 |
|
14 | 15 | _XGRID_AXES = Literal["X", "Y", "Z"] |
@@ -95,6 +96,7 @@ class XGrid(BaseGrid): |
95 | 96 | def __init__(self, grid: xgcm.Grid, mesh="flat"): |
96 | 97 | self.xgcm_grid = grid |
97 | 98 | self.mesh = mesh |
| 99 | + self._spatialhash = None |
98 | 100 | ds = grid._ds |
99 | 101 |
|
100 | 102 | if len(set(grid.axes) & {"X", "Y", "Z"}) > 0: # Only if spatial grid is >0D (see #2054 for further development) |
@@ -314,6 +316,33 @@ def _fpoint_info(self): |
314 | 316 |
|
315 | 317 | return axis_position_mapping |
316 | 318 |
|
| 319 | + def get_spatial_hash( |
| 320 | + self, |
| 321 | + global_grid=False, |
| 322 | + reconstruct=False, |
| 323 | + ): |
| 324 | + """Get the SpatialHash data structure of this Grid that allows for |
| 325 | + fast face search queries. Face searches are used to find the faces that |
| 326 | + a list of points, in spherical coordinates, are contained within. |
| 327 | +
|
| 328 | + Parameters |
| 329 | + ---------- |
| 330 | + global_grid : bool, default=False |
| 331 | + If true, the hash grid is constructed using the domain [-pi,pi] x [-pi,pi] |
| 332 | + reconstruct : bool, default=False |
| 333 | + If true, reconstructs the spatial hash |
| 334 | +
|
| 335 | + Returns |
| 336 | + ------- |
| 337 | + self._spatialhash : parcels.spatialhash.SpatialHash |
| 338 | + SpatialHash instance |
| 339 | +
|
| 340 | + """ |
| 341 | + if self._spatialhash is None or reconstruct: |
| 342 | + self._spatialhash = SpatialHash(self, global_grid, reconstruct) |
| 343 | + |
| 344 | + return self._spatialhash |
| 345 | + |
317 | 346 |
|
318 | 347 | def get_axis_from_dim_name(axes: _XGCM_AXES, dim: str) -> _XGCM_AXIS_DIRECTION | None: |
319 | 348 | """For a given dimension name in a grid, returns the direction axis it is on.""" |
|
0 commit comments