Skip to content

Commit

Permalink
Adding support for grid.negate_depth() method
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
erikvansebille committed Nov 27, 2024
1 parent a1f7a56 commit cab82bd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
3 changes: 3 additions & 0 deletions parcels/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ def lat(self):
def depth(self):
return self._depth

def negate_depth(self):
self._depth = -self._depth

Check warning on line 118 in parcels/grid.py

View check run for this annotation

Codecov / codecov/patch

parcels/grid.py#L117-L118

Added lines #L117 - L118 were not covered by tests

@property
def mesh(self):
return self._mesh
Expand Down
10 changes: 10 additions & 0 deletions tests/test_grids.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit cab82bd

Please sign in to comment.