From cab82bd51bed8c7aceeffbf545c3d3497f4f9ea3 Mon Sep 17 00:00:00 2001 From: Erik van Sebille Date: Wed, 27 Nov 2024 09:53:25 +0100 Subject: [PATCH] Adding support for grid.negate_depth() method This is useful when the depth needs to be flipped from positive to negative values. Note that this does _not_ change the direction of the vertical velocity; for that users need to add a fieldset.W.set_scaling_factor(-1.0) --- parcels/grid.py | 3 +++ tests/test_grids.py | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/parcels/grid.py b/parcels/grid.py index 9b7a6631a..cf68f7024 100644 --- a/parcels/grid.py +++ b/parcels/grid.py @@ -114,6 +114,9 @@ def lat(self): def depth(self): return self._depth + def negate_depth(self): + self._depth = -self._depth + @property def mesh(self): return self._mesh diff --git a/tests/test_grids.py b/tests/test_grids.py index 5e336af11..ccc8230eb 100644 --- a/tests/test_grids.py +++ b/tests/test_grids.py @@ -112,6 +112,16 @@ def test_time_format_in_grid(): RectilinearZGrid(lon, lat, time=time) +def test_negate_depth(): + depth = np.linspace(0, 5, 10, dtype=np.float32) + fieldset = FieldSet.from_data( + {"U": np.zeros((10, 1, 1)), "V": np.zeros((10, 1, 1))}, {"lon": [0], "lat": [0], "depth": depth} + ) + assert np.all(fieldset.gridset.grids[0].depth == depth) + fieldset.U.grid.negate_depth() + assert np.all(fieldset.gridset.grids[0].depth == -depth) + + def test_avoid_repeated_grids(): lon_g0 = np.linspace(0, 1000, 11, dtype=np.float32) lat_g0 = np.linspace(0, 1000, 11, dtype=np.float32)