Skip to content

Commit

Permalink
Merge pull request #1834 from OceanParcels/scipy_vertical_indexsearch…
Browse files Browse the repository at this point in the history
…_inconsistency

Fixing an inconsistency in vertical index search between JIT and Scipy
  • Loading branch information
erikvansebille authored Jan 30, 2025
2 parents d1dde5a + 762d911 commit e1043f3
Showing 1 changed file with 27 additions and 15 deletions.
42 changes: 27 additions & 15 deletions parcels/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -1006,22 +1006,28 @@ def _search_indices_vertical_z(self, z):
if self.gridindexingtype in ["croco"] and z < 0:
return (-2, 1)
raise FieldOutOfBoundError(z, 0, 0, field=self)
depth_indices = grid.depth <= z
depth_indices = grid.depth < z
if z >= grid.depth[-1]:
zi = len(grid.depth) - 2
else:
zi = depth_indices.argmin() - 1 if z >= grid.depth[0] else 0
zi = depth_indices.argmin() - 1 if z > grid.depth[0] else 0
else:
if z > grid.depth[0]:
raise FieldOutOfBoundSurfaceError(z, 0, 0, field=self)
elif z < grid.depth[-1]:
raise FieldOutOfBoundError(z, 0, 0, field=self)
depth_indices = grid.depth >= z
depth_indices = grid.depth > z
if z <= grid.depth[-1]:
zi = len(grid.depth) - 2
else:
zi = depth_indices.argmin() - 1 if z <= grid.depth[0] else 0
zi = depth_indices.argmin() - 1 if z < grid.depth[0] else 0
zeta = (z - grid.depth[zi]) / (grid.depth[zi + 1] - grid.depth[zi])
while zeta > 1:
zi += 1
zeta = (z - grid.depth[zi]) / (grid.depth[zi + 1] - grid.depth[zi])
while zeta < 0:
zi -= 1
zeta = (z - grid.depth[zi]) / (grid.depth[zi + 1] - grid.depth[zi])
return (zi, zeta)

@deprecated_made_private # TODO: Remove 6 months after v3.1.0
Expand Down Expand Up @@ -1065,26 +1071,32 @@ def _search_indices_vertical_s(
z = np.float32(z) # type: ignore # TODO: remove type ignore once we migrate to float64

if depth_vector[-1] > depth_vector[0]:
depth_indices = depth_vector <= z
if z < depth_vector[0]:
raise FieldOutOfBoundSurfaceError(z, 0, 0, field=self)
elif z > depth_vector[-1]:
raise FieldOutOfBoundError(z, y, x, field=self)
depth_indices = depth_vector < z
if z >= depth_vector[-1]:
zi = len(depth_vector) - 2
else:
zi = depth_indices.argmin() - 1 if z >= depth_vector[0] else 0
if z < depth_vector[zi]:
zi = depth_indices.argmin() - 1 if z > depth_vector[0] else 0
else:
if z > depth_vector[0]:
raise FieldOutOfBoundSurfaceError(z, 0, 0, field=self)
elif z > depth_vector[zi + 1]:
elif z < depth_vector[-1]:
raise FieldOutOfBoundError(z, y, x, field=self)
else:
depth_indices = depth_vector >= z
depth_indices = depth_vector > z
if z <= depth_vector[-1]:
zi = len(depth_vector) - 2
else:
zi = depth_indices.argmin() - 1 if z <= depth_vector[0] else 0
if z > depth_vector[zi]:
raise FieldOutOfBoundSurfaceError(z, 0, 0, field=self)
elif z < depth_vector[zi + 1]:
raise FieldOutOfBoundError(z, y, x, field=self)
zi = depth_indices.argmin() - 1 if z < depth_vector[0] else 0
zeta = (z - depth_vector[zi]) / (depth_vector[zi + 1] - depth_vector[zi])
while zeta > 1:
zi += 1
zeta = (z - depth_vector[zi]) / (depth_vector[zi + 1] - depth_vector[zi])
while zeta < 0:
zi -= 1
zeta = (z - depth_vector[zi]) / (depth_vector[zi + 1] - depth_vector[zi])
return (zi, zeta)

@deprecated_made_private # TODO: Remove 6 months after v3.1.0
Expand Down

0 comments on commit e1043f3

Please sign in to comment.