From 35b2427451814dcdc3dd8ca3addaba4181e75c5a Mon Sep 17 00:00:00 2001 From: Erik van Sebille Date: Tue, 16 Jun 2026 09:27:28 +0200 Subject: [PATCH 1/2] Changing OUT_OF_BOUNDS error codes to be extremely large In the previous implementation, the GRID_SEARCH_ERROR, LEFT_OUT_OF_BOUNDS and RIGHT_OUT_OF_BOUNDS were negative values, but this could create confusion when a user would then use particles.ei (as negative values are wrapped in the array). See also #2629 --- src/parcels/_core/index_search.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/parcels/_core/index_search.py b/src/parcels/_core/index_search.py index 5e84eee2e..6c75fa080 100644 --- a/src/parcels/_core/index_search.py +++ b/src/parcels/_core/index_search.py @@ -12,9 +12,9 @@ from parcels._core.xgrid import XGrid -GRID_SEARCH_ERROR = -3 -LEFT_OUT_OF_BOUNDS = -2 -RIGHT_OUT_OF_BOUNDS = -1 +GRID_SEARCH_ERROR = np.iinfo(np.int32).max - 3 +LEFT_OUT_OF_BOUNDS = np.iinfo(np.int32).max - 2 +RIGHT_OUT_OF_BOUNDS = np.iinfo(np.int32).max - 1 def _search_1d_array( From b49ee3473042d1f98732516864219b15ee7a964f Mon Sep 17 00:00:00 2001 From: Erik van Sebille Date: Tue, 16 Jun 2026 09:37:45 +0200 Subject: [PATCH 2/2] Adding unit test for OutOfBounds field.eval --- tests/test_field.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/test_field.py b/tests/test_field.py index 5e57c43cb..587a39a57 100644 --- a/tests/test_field.py +++ b/tests/test_field.py @@ -5,6 +5,7 @@ from parcels import Field, UxGrid, VectorField, XGrid from parcels._core.fieldset import FieldSet +from parcels._datasets.structured.generated import simple_UV_dataset from parcels._datasets.structured.generic import T as T_structured from parcels._datasets.structured.generic import datasets as datasets_structured from parcels._datasets.unstructured.generic import datasets as datasets_unstructured @@ -270,6 +271,21 @@ def test_field_constant_in_time(): assert np.isclose(P1, P2) +def test_field_eval_out_of_bounds(): + """Test that Field.eval returns IndexError when queried outside the grid boundaries.""" + ds = simple_UV_dataset(mesh="flat") + fieldset = FieldSet.from_sgrid_conventions(ds, mesh="flat") + + with pytest.raises(IndexError, match=".* is out of bounds.*"): + fieldset.U.eval(0.0, 0.0, 0.0, 5e6) + + with pytest.raises(IndexError, match=".* is out of bounds.*"): + fieldset.U.eval(0.0, 0.0, 5e6, 0.0) + + with pytest.raises(IndexError, match=".* is out of bounds.*"): + fieldset.U.eval(0.0, 5e6, 0.0, 0.0) + + def test_field_unstructured_grid_creation(): ...