Skip to content

Commit e1043f3

Browse files
Merge pull request #1834 from OceanParcels/scipy_vertical_indexsearch_inconsistency
Fixing an inconsistency in vertical index search between JIT and Scipy
2 parents d1dde5a + 762d911 commit e1043f3

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

parcels/field.py

+27-15
Original file line numberDiff line numberDiff line change
@@ -1006,22 +1006,28 @@ def _search_indices_vertical_z(self, z):
10061006
if self.gridindexingtype in ["croco"] and z < 0:
10071007
return (-2, 1)
10081008
raise FieldOutOfBoundError(z, 0, 0, field=self)
1009-
depth_indices = grid.depth <= z
1009+
depth_indices = grid.depth < z
10101010
if z >= grid.depth[-1]:
10111011
zi = len(grid.depth) - 2
10121012
else:
1013-
zi = depth_indices.argmin() - 1 if z >= grid.depth[0] else 0
1013+
zi = depth_indices.argmin() - 1 if z > grid.depth[0] else 0
10141014
else:
10151015
if z > grid.depth[0]:
10161016
raise FieldOutOfBoundSurfaceError(z, 0, 0, field=self)
10171017
elif z < grid.depth[-1]:
10181018
raise FieldOutOfBoundError(z, 0, 0, field=self)
1019-
depth_indices = grid.depth >= z
1019+
depth_indices = grid.depth > z
10201020
if z <= grid.depth[-1]:
10211021
zi = len(grid.depth) - 2
10221022
else:
1023-
zi = depth_indices.argmin() - 1 if z <= grid.depth[0] else 0
1023+
zi = depth_indices.argmin() - 1 if z < grid.depth[0] else 0
10241024
zeta = (z - grid.depth[zi]) / (grid.depth[zi + 1] - grid.depth[zi])
1025+
while zeta > 1:
1026+
zi += 1
1027+
zeta = (z - grid.depth[zi]) / (grid.depth[zi + 1] - grid.depth[zi])
1028+
while zeta < 0:
1029+
zi -= 1
1030+
zeta = (z - grid.depth[zi]) / (grid.depth[zi + 1] - grid.depth[zi])
10251031
return (zi, zeta)
10261032

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

10671073
if depth_vector[-1] > depth_vector[0]:
1068-
depth_indices = depth_vector <= z
1074+
if z < depth_vector[0]:
1075+
raise FieldOutOfBoundSurfaceError(z, 0, 0, field=self)
1076+
elif z > depth_vector[-1]:
1077+
raise FieldOutOfBoundError(z, y, x, field=self)
1078+
depth_indices = depth_vector < z
10691079
if z >= depth_vector[-1]:
10701080
zi = len(depth_vector) - 2
10711081
else:
1072-
zi = depth_indices.argmin() - 1 if z >= depth_vector[0] else 0
1073-
if z < depth_vector[zi]:
1082+
zi = depth_indices.argmin() - 1 if z > depth_vector[0] else 0
1083+
else:
1084+
if z > depth_vector[0]:
10741085
raise FieldOutOfBoundSurfaceError(z, 0, 0, field=self)
1075-
elif z > depth_vector[zi + 1]:
1086+
elif z < depth_vector[-1]:
10761087
raise FieldOutOfBoundError(z, y, x, field=self)
1077-
else:
1078-
depth_indices = depth_vector >= z
1088+
depth_indices = depth_vector > z
10791089
if z <= depth_vector[-1]:
10801090
zi = len(depth_vector) - 2
10811091
else:
1082-
zi = depth_indices.argmin() - 1 if z <= depth_vector[0] else 0
1083-
if z > depth_vector[zi]:
1084-
raise FieldOutOfBoundSurfaceError(z, 0, 0, field=self)
1085-
elif z < depth_vector[zi + 1]:
1086-
raise FieldOutOfBoundError(z, y, x, field=self)
1092+
zi = depth_indices.argmin() - 1 if z < depth_vector[0] else 0
10871093
zeta = (z - depth_vector[zi]) / (depth_vector[zi + 1] - depth_vector[zi])
1094+
while zeta > 1:
1095+
zi += 1
1096+
zeta = (z - depth_vector[zi]) / (depth_vector[zi + 1] - depth_vector[zi])
1097+
while zeta < 0:
1098+
zi -= 1
1099+
zeta = (z - depth_vector[zi]) / (depth_vector[zi + 1] - depth_vector[zi])
10881100
return (zi, zeta)
10891101

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

0 commit comments

Comments
 (0)