Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions parcels/fieldset.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,6 @@ def add_constant_field(self, name: str, value, mesh: Mesh = "flat"):
da = xr.DataArray(
data=np.full((1, 1, 1, 1), value),
dims=["time", "ZG", "YG", "XG"],
coords={
"ZG": (["ZG"], np.arange(1), {"axis": "Z"}),
"YG": (["YG"], np.arange(1), {"axis": "Y"}),
"XG": (["XG"], np.arange(1), {"axis": "X"}),
"lon": (["XG"], np.arange(1), {"axis": "X"}),
"lat": (["YG"], np.arange(1), {"axis": "Y"}),
"depth": (["ZG"], np.arange(1), {"axis": "Z"}),
"time": (["time"], np.arange(1), {"axis": "T"}),
},
)
grid = XGrid(xgcm.Grid(da))
self.add_field(
Expand Down
26 changes: 15 additions & 11 deletions parcels/xgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
from parcels.basegrid import BaseGrid
from parcels.tools.converters import TimeConverter

_XGRID_AXES_ORDERING = "ZYX"
_XGRID_AXES = Literal["X", "Y", "Z"]

_XGCM_AXIS_DIRECTION = Literal["X", "Y", "Z", "T"]
_XGCM_AXIS_POSITION = Literal["center", "left", "right", "inner", "outer"]
_AXIS_DIRECTION = Literal["X", "Y", "Z"]
Expand All @@ -29,6 +32,11 @@
return axis._ds[axis.coords["center"]].values


def _get_xgrid_axes(grid: xgcm.Grid) -> list[_XGRID_AXES]:
spatial_axes = [a for a in grid.axes.keys() if a in ["X", "Y", "Z"]]
return sorted(spatial_axes, key=_XGRID_AXES_ORDERING.index)

Check warning on line 37 in parcels/xgrid.py

View check run for this annotation

Codecov / codecov/patch

parcels/xgrid.py#L36-L37

Added lines #L36 - L37 were not covered by tests


class XGrid(BaseGrid):
"""
Class to represent a structured grid in Parcels. Wraps a xgcm-like Grid object (we use a trimmed down version of the xgcm.Grid class that is vendored with Parcels).
Expand All @@ -44,17 +52,13 @@
self.xgcm_grid = grid
self.mesh = mesh
ds = grid._ds
assert_valid_lat_lon(ds["lat"], ds["lon"], grid.axes)

# ! Not ideal... Triggers computation on a throwaway item. Keeping for now for v3 compat, will be removed in v4.
self.lonlat_minmax = np.array(
[
np.nanmin(self.xgcm_grid._ds["lon"]),
np.nanmax(self.xgcm_grid._ds["lon"]),
np.nanmin(self.xgcm_grid._ds["lat"]),
np.nanmax(self.xgcm_grid._ds["lat"]),
]
)

if len(set(grid.axes) & {"X", "Y", "Z"}) > 0: # Only if spatial grid is >0D (see #2054 for further development)
assert_valid_lat_lon(ds["lat"], ds["lon"], grid.axes)

@property
def axes(self) -> list[_XGRID_AXES]:
return _get_xgrid_axes(self.xgcm_grid)

Check warning on line 61 in parcels/xgrid.py

View check run for this annotation

Codecov / codecov/patch

parcels/xgrid.py#L61

Added line #L61 was not covered by tests

@property
def lon(self):
Expand Down
7 changes: 6 additions & 1 deletion tests/v4/test_xgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,15 @@ def test_xgrid_against_old(ds, attr):


@pytest.mark.parametrize("ds", [pytest.param(ds, id=key) for key, ds in datasets.items()])
def test_grid_init_on_generic_datasets(ds):
def test_xgrid_init_on_generic_datasets(ds):
XGrid(xgcm.Grid(ds, periodic=False))


def test_xgrid_axes():
# Tests that the xgrid.axes property correctly identifies the axes and ordering
...


def test_invalid_xgrid_field_array():
"""Stress test initialiser by creating incompatible datasets that test the edge cases"""
...
Expand Down
Loading