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
3 changes: 2 additions & 1 deletion src/parcels/_core/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from parcels._core.utils.string import _assert_str_and_python_varname
from parcels._core.utils.time import TimeInterval
from parcels._core.uxgrid import UxGrid
from parcels._core.xgrid import XGrid, _transpose_xfield_data_to_tzyx
from parcels._core.xgrid import XGrid, _transpose_xfield_data_to_tzyx, assert_all_field_dims_have_axis
from parcels._python import assert_same_function_signature
from parcels._reprs import default_repr
from parcels._typing import VectorType
Expand Down Expand Up @@ -103,6 +103,7 @@ def __init__(
_assert_compatible_combination(data, grid)

if isinstance(grid, XGrid):
assert_all_field_dims_have_axis(data, grid.xgcm_grid)
data = _transpose_xfield_data_to_tzyx(data, grid.xgcm_grid)

self.name = name
Expand Down
10 changes: 10 additions & 0 deletions src/parcels/_core/xgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ def _drop_field_data(ds: xr.Dataset) -> xr.Dataset:
return ds.drop_vars(ds.data_vars)


def assert_all_field_dims_have_axis(da: xr.DataArray, xgcm_grid: xgcm.Grid) -> None:
ax_dims = [(get_axis_from_dim_name(xgcm_grid.axes, dim), dim) for dim in da.dims]
for dim in ax_dims:
if dim[0] is None:
raise ValueError(
f'Dimension "{dim[1]}" has no axis attribute. '
f'HINT: You may want to add an {{"axis": A}} to your DataSet["{dim[1]}"], where A is one of "X", "Y", "Z" or "T"'
)


def _transpose_xfield_data_to_tzyx(da: xr.DataArray, xgcm_grid: xgcm.Grid) -> xr.DataArray:
"""
Transpose a DataArray of any shape into a 4D array of order TZYX. Uses xgcm to determine
Expand Down
7 changes: 7 additions & 0 deletions tests/test_xgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,13 @@ def test_invalid_depth():
XGrid.from_dataset(ds)


def test_dim_without_axis():
ds = xr.Dataset({"z1d": (["depth"], [0])}, coords={"depth": [0]})
grid = XGrid.from_dataset(ds)
with pytest.raises(ValueError, match='Dimension "depth" has no axis attribute*'):
Field("z1d", ds["z1d"], grid, XLinear)


def test_vertical1D_field():
nz = 11
ds = xr.Dataset(
Expand Down