Skip to content

Commit 419ce76

Browse files
Raise error when dimensions have no axis
1 parent bba37f4 commit 419ce76

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/parcels/_core/xgrid.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ def _transpose_xfield_data_to_tzyx(da: xr.DataArray, xgcm_grid: xgcm.Grid) -> xr
5555
"""
5656
ax_dims = [(get_axis_from_dim_name(xgcm_grid.axes, dim), dim) for dim in da.dims]
5757

58+
for dim in ax_dims:
59+
if dim[0] is None:
60+
raise ValueError(
61+
f'Dimension "{dim[1]}" has no axis attribute. '
62+
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"'
63+
)
64+
5865
if all(ax_dim[0] is None for ax_dim in ax_dims):
5966
# Assuming its a 1D constant field (hence has no axes)
6067
assert da.shape == (1, 1, 1, 1)

tests/test_xgrid.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import xarray as xr
77
from numpy.testing import assert_allclose
88

9+
from parcels import Field
910
from parcels._core.index_search import (
1011
LEFT_OUT_OF_BOUNDS,
1112
RIGHT_OUT_OF_BOUNDS,
@@ -134,6 +135,13 @@ def test_invalid_depth():
134135
XGrid.from_dataset(ds)
135136

136137

138+
def test_dim_without_axis():
139+
ds = xr.Dataset({"z1d": (["depth"], [0])}, coords={"depth": [0]})
140+
grid = XGrid.from_dataset(ds)
141+
with pytest.raises(ValueError, match='Dimension "depth" has no axis attribute*'):
142+
Field("z1d", ds["z1d"], grid)
143+
144+
137145
@pytest.mark.parametrize(
138146
"ds",
139147
[

0 commit comments

Comments
 (0)