Skip to content

Commit ffe3fc2

Browse files
[#2199] Add get_spatial_hash method to xgrid class
1 parent ef757f9 commit ffe3fc2

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

parcels/spatialhash.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ def __init__(
7272
self._ny = int(np.ceil(Ly / self._dh))
7373

7474
# Generate the mapping from the hash indices to unstructured grid elements
75-
self._face_hash_table = None
76-
# self._face_hash_table = self._initialize_face_hash_table()
75+
self._face_hash_table = self._initialize_face_hash_table()
7776

7877
def _hash_cell_size(self):
7978
"""Computes the size of the hash cells from the source grid.

parcels/xgrid.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from parcels import xgcm
1010
from parcels._index_search import _search_indices_curvilinear_2d
1111
from parcels.basegrid import BaseGrid
12+
from parcels.spatialhash import SpatialHash
1213
from parcels.tools.statuscodes import FieldOutOfBoundError, FieldOutOfBoundSurfaceError
1314

1415
_XGRID_AXES = Literal["X", "Y", "Z"]
@@ -95,6 +96,7 @@ class XGrid(BaseGrid):
9596
def __init__(self, grid: xgcm.Grid, mesh="flat"):
9697
self.xgcm_grid = grid
9798
self.mesh = mesh
99+
self._spatialhash = None
98100
ds = grid._ds
99101

100102
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):
314316

315317
return axis_position_mapping
316318

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+
317346

318347
def get_axis_from_dim_name(axes: _XGCM_AXES, dim: str) -> _XGCM_AXIS_DIRECTION | None:
319348
"""For a given dimension name in a grid, returns the direction axis it is on."""

0 commit comments

Comments
 (0)