77from uxarray .grid .neighbors import _barycentric_coordinates
88
99from parcels .field import FieldOutOfBoundError # Adjust import as necessary
10+ from parcels .xgrid import _search_1d_array
1011
1112from .basegrid import BaseGrid
1213
@@ -62,18 +63,7 @@ def try_face(fid):
6263 return bcoords , fid
6364 return None , None
6465
65- def find_vertical_index () -> int :
66- nz = self .z .shape [0 ]
67- if nz == 1 :
68- return 0
69- zf = self .z .values
70- # Return zi such that zf[zi] <= z < zf[zi+1]
71- zi = np .searchsorted (zf , z , side = "right" ) - 1 # Search assumes that z is positive and increasing with i
72- if zi < 0 or zi >= nz - 1 :
73- raise FieldOutOfBoundError (z , y , x )
74- return zi
75-
76- zi = find_vertical_index () # Find the vertical cell center nearest to z
66+ zi , zeta = _search_1d_array (self .z .values , z )
7767
7868 if ei is not None :
7969 _ , fi = self .unravel_index (ei )
@@ -93,7 +83,7 @@ def find_vertical_index() -> int:
9383 if fi == - 1 :
9484 raise FieldOutOfBoundError (z , y , x )
9585
96- return bcoords [ 0 ], self . ravel_index (zi , fi [0 ])
86+ return { "Z" : (zi , zeta ), "CELL" : ( fi , bcoords [0 ])}
9787
9888 def _get_barycentric_coordinates (self , y , x , fi ):
9989 """Checks if a point is inside a given face id on a UxGrid."""
@@ -112,40 +102,10 @@ def _get_barycentric_coordinates(self, y, x, fi):
112102 err = abs (np .dot (bcoord , nodes [:, 0 ]) - coord [0 ]) + abs (np .dot (bcoord , nodes [:, 1 ]) - coord [1 ])
113103 return bcoord , err
114104
115- def ravel_index (self , zi , fi ):
116- """
117- Converts a face index and a vertical index into a single encoded index.
118-
119- Parameters
120- ----------
121- zi : int
122- Vertical index (not used in unstructured grids, but kept for compatibility).
123- fi : int
124- Face index.
125-
126- Returns
127- -------
128- int
129- Encoded index combining the face index and vertical index.
130- """
131- return fi + self .uxgrid .n_face * zi
105+ def ravel_index (self , axis_indices : dict [_UXGRID_AXES , int ]):
106+ return axis_indices ["CELL" ] + self .uxgrid .n_face * axis_indices ["Z" ]
132107
133- def unravel_index (self , ei ):
134- """
135- Converts a single encoded index back into a vertical index and face index.
136-
137- Parameters
138- ----------
139- ei : int
140- Encoded index to be unraveled.
141-
142- Returns
143- -------
144- zi : int
145- Vertical index.
146- fi : int
147- Face index.
148- """
108+ def unravel_index (self , ei ) -> dict [_UXGRID_AXES , int ]:
149109 zi = ei // self .uxgrid .n_face
150110 fi = ei % self .uxgrid .n_face
151- return zi , fi
111+ return { "Z" : zi , "CELL" : fi }
0 commit comments