Skip to content

Commit 825356a

Browse files
committed
review feedback
1 parent 0d19fe2 commit 825356a

File tree

7 files changed

+51
-59
lines changed

7 files changed

+51
-59
lines changed

parcels/_core/constants.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +0,0 @@
1-
# Used in the searching of 1D arrays for time and space
2-
LEFT_OUT_OF_BOUNDS = -2
3-
RIGHT_OUT_OF_BOUNDS = -1

parcels/_core/field.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@
88
import uxarray as ux
99
import xarray as xr
1010

11-
from parcels._core.constants import LEFT_OUT_OF_BOUNDS, RIGHT_OUT_OF_BOUNDS
1211
from parcels._core.converters import (
1312
UnitConverter,
1413
_unitconverters_map,
1514
)
16-
from parcels._core.index_search import GRID_SEARCH_ERROR, _search_time_index
15+
from parcels._core.index_search import GRID_SEARCH_ERROR, LEFT_OUT_OF_BOUNDS, RIGHT_OUT_OF_BOUNDS, _search_time_index
1716
from parcels._core.particle import KernelParticle
1817
from parcels._core.statuscodes import (
1918
AllParcelsErrorCodes,

parcels/_core/index_search.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,53 @@
1313

1414

1515
GRID_SEARCH_ERROR = -3
16+
LEFT_OUT_OF_BOUNDS = -2
17+
RIGHT_OUT_OF_BOUNDS = -1
18+
19+
20+
def _search_1d_array(
21+
arr: np.array,
22+
x: float,
23+
) -> tuple[int, int]:
24+
"""
25+
Searches for particle locations in a 1D array and returns barycentric coordinate along dimension.
26+
27+
Assumptions:
28+
- array is strictly monotonically increasing.
29+
30+
Parameters
31+
----------
32+
arr : np.array
33+
1D array to search in.
34+
x : float
35+
Position in the 1D array to search for.
36+
37+
Returns
38+
-------
39+
array of int
40+
Index of the element just before the position x in the array. Note that this index is -2 if the index is left out of bounds and -1 if the index is right out of bounds.
41+
array of float
42+
Barycentric coordinate.
43+
"""
44+
# TODO v4: We probably rework this to deal with 0D arrays before this point (as we already know field dimensionality)
45+
if len(arr) < 2:
46+
return np.zeros(shape=x.shape, dtype=np.int32), np.zeros_like(x)
47+
index = np.searchsorted(arr, x, side="right") - 1
48+
# Use broadcasting to avoid repeated array access
49+
arr_index = arr[index]
50+
arr_next = arr[np.clip(index + 1, 1, len(arr) - 1)] # Ensure we don't go out of bounds
51+
bcoord = (x - arr_index) / (arr_next - arr_index)
52+
53+
# TODO check how we can avoid searchsorted when grid spacing is uniform
54+
# dx = arr[1] - arr[0]
55+
# index = ((x - arr[0]) / dx).astype(int)
56+
# index = np.clip(index, 0, len(arr) - 2)
57+
# bcoord = (x - arr[index]) / dx
58+
59+
index = np.where(x < arr[0], LEFT_OUT_OF_BOUNDS, index)
60+
index = np.where(x >= arr[-1], RIGHT_OUT_OF_BOUNDS, index)
61+
62+
return np.atleast_1d(index), np.atleast_1d(bcoord)
1663

1764

1865
def _search_time_index(field: Field, time: datetime):

parcels/_core/utils/array.py

Lines changed: 0 additions & 48 deletions
This file was deleted.

parcels/_core/uxgrid.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
import uxarray as ux
77

88
from parcels._core.basegrid import BaseGrid
9-
from parcels._core.index_search import GRID_SEARCH_ERROR, uxgrid_point_in_cell
10-
from parcels._core.utils.array import _search_1d_array
9+
from parcels._core.index_search import GRID_SEARCH_ERROR, _search_1d_array, uxgrid_point_in_cell
1110
from parcels._typing import assert_valid_mesh
1211

1312
_UXGRID_AXES = Literal["Z", "FACE"]

parcels/_core/xgrid.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
import xgcm
99

1010
from parcels._core.basegrid import BaseGrid
11-
from parcels._core.index_search import _search_indices_curvilinear_2d
12-
from parcels._core.utils.array import _search_1d_array
11+
from parcels._core.index_search import _search_1d_array, _search_indices_curvilinear_2d
1312
from parcels._typing import assert_valid_mesh
1413

1514
_XGRID_AXES = Literal["X", "Y", "Z"]

tests/test_xgrid.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
import xarray as xr
77
from numpy.testing import assert_allclose
88

9-
from parcels._core.constants import LEFT_OUT_OF_BOUNDS, RIGHT_OUT_OF_BOUNDS
10-
from parcels._core.utils.array import _search_1d_array
9+
from parcels._core.index_search import LEFT_OUT_OF_BOUNDS, RIGHT_OUT_OF_BOUNDS, _search_1d_array
1110
from parcels._core.xgrid import (
1211
XGrid,
1312
_transpose_xfield_data_to_tzyx,

0 commit comments

Comments
 (0)