Skip to content

Commit baa16c7

Browse files
committed
Move axis assertion to dedicated function
1 parent 419ce76 commit baa16c7

2 files changed

Lines changed: 11 additions & 7 deletions

File tree

src/parcels/_core/field.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from parcels._core.utils.string import _assert_str_and_python_varname
2222
from parcels._core.utils.time import TimeInterval
2323
from parcels._core.uxgrid import UxGrid
24-
from parcels._core.xgrid import XGrid, _transpose_xfield_data_to_tzyx
24+
from parcels._core.xgrid import XGrid, _transpose_xfield_data_to_tzyx, assert_all_field_dims_have_axis
2525
from parcels._python import assert_same_function_signature
2626
from parcels._reprs import default_repr
2727
from parcels._typing import VectorType
@@ -111,6 +111,7 @@ def __init__(
111111
_assert_compatible_combination(data, grid)
112112

113113
if isinstance(grid, XGrid):
114+
assert_all_field_dims_have_axis(data, grid.xgcm_grid)
114115
data = _transpose_xfield_data_to_tzyx(data, grid.xgcm_grid)
115116

116117
self.name = name

src/parcels/_core/xgrid.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,23 @@ def _drop_field_data(ds: xr.Dataset) -> xr.Dataset:
4848
return ds.drop_vars(ds.data_vars)
4949

5050

51-
def _transpose_xfield_data_to_tzyx(da: xr.DataArray, xgcm_grid: xgcm.Grid) -> xr.DataArray:
52-
"""
53-
Transpose a DataArray of any shape into a 4D array of order TZYX. Uses xgcm to determine
54-
the axes, and inserts mock dimensions of size 1 for any axes not present in the DataArray.
55-
"""
51+
def assert_all_field_dims_have_axis(da: xr.DataArray, xgcm_grid: xgcm.Grid) -> None:
5652
ax_dims = [(get_axis_from_dim_name(xgcm_grid.axes, dim), dim) for dim in da.dims]
57-
5853
for dim in ax_dims:
5954
if dim[0] is None:
6055
raise ValueError(
6156
f'Dimension "{dim[1]}" has no axis attribute. '
6257
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"'
6358
)
6459

60+
61+
def _transpose_xfield_data_to_tzyx(da: xr.DataArray, xgcm_grid: xgcm.Grid) -> xr.DataArray:
62+
"""
63+
Transpose a DataArray of any shape into a 4D array of order TZYX. Uses xgcm to determine
64+
the axes, and inserts mock dimensions of size 1 for any axes not present in the DataArray.
65+
"""
66+
ax_dims = [(get_axis_from_dim_name(xgcm_grid.axes, dim), dim) for dim in da.dims]
67+
6568
if all(ax_dim[0] is None for ax_dim in ax_dims):
6669
# Assuming its a 1D constant field (hence has no axes)
6770
assert da.shape == (1, 1, 1, 1)

0 commit comments

Comments
 (0)