From cab82bd51bed8c7aceeffbf545c3d3497f4f9ea3 Mon Sep 17 00:00:00 2001 From: Erik van Sebille Date: Wed, 27 Nov 2024 09:53:25 +0100 Subject: [PATCH 1/4] 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 9b7a6631a3..cf68f70240 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 5e336af115..ccc8230eb2 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) From ce11d4c50c075fee4a09bc1c3a0f07c782d21f95 Mon Sep 17 00:00:00 2001 From: Erik van Sebille Date: Wed, 27 Nov 2024 10:31:34 +0100 Subject: [PATCH 2/4] Update parcels/grid.py Co-authored-by: Vecko <36369090+VeckoTheGecko@users.noreply.github.com> --- parcels/grid.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/parcels/grid.py b/parcels/grid.py index cf68f70240..68b8ecd1be 100644 --- a/parcels/grid.py +++ b/parcels/grid.py @@ -114,7 +114,7 @@ def lat(self): def depth(self): return self._depth - def negate_depth(self): + def negate_depth(self) -> None: self._depth = -self._depth @property From 38fbbfcf3103c0c0562bbcc3768af8b54901b941 Mon Sep 17 00:00:00 2001 From: Erik van Sebille Date: Wed, 27 Nov 2024 10:33:23 +0100 Subject: [PATCH 3/4] Adding docstring to grid.negate_depth() --- parcels/grid.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/parcels/grid.py b/parcels/grid.py index 68b8ecd1be..ec17d040de 100644 --- a/parcels/grid.py +++ b/parcels/grid.py @@ -115,6 +115,10 @@ def depth(self): return self._depth def negate_depth(self) -> None: + """Method to flip the sign of the depth dimension of a Grid. + Note that this method does _not_ change the direction of the vertical velocity; + for that users need to add a fieldset.W.set_scaling_factor(-1.0) + """ self._depth = -self._depth @property From bb1a04774e1c145f5157031f6cd12df56dd19386 Mon Sep 17 00:00:00 2001 From: Vecko <36369090+VeckoTheGecko@users.noreply.github.com> Date: Thu, 28 Nov 2024 12:48:44 +0800 Subject: [PATCH 4/4] Revert "Update parcels/grid.py" This reverts commit ce11d4c50c075fee4a09bc1c3a0f07c782d21f95. --- parcels/grid.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/parcels/grid.py b/parcels/grid.py index ec17d040de..382cc0f2b7 100644 --- a/parcels/grid.py +++ b/parcels/grid.py @@ -114,7 +114,7 @@ def lat(self): def depth(self): return self._depth - def negate_depth(self) -> None: + def negate_depth(self): """Method to flip the sign of the depth dimension of a Grid. Note that this method does _not_ change the direction of the vertical velocity; for that users need to add a fieldset.W.set_scaling_factor(-1.0)