Skip to content

Commit d004bf7

Browse files
committed
Improve
1 parent cc0c0cb commit d004bf7

File tree

1 file changed

+18
-21
lines changed

1 file changed

+18
-21
lines changed

src/parcels/convert.py

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
Xarray dataset so that users can further provide any missing metadata that was unable to
1010
be determined before they pass it to the FieldSet constructor.
1111
"""
12-
1312
from __future__ import annotations
13+
import warnings
1414

1515
import typing
1616

@@ -23,7 +23,7 @@
2323
if typing.TYPE_CHECKING:
2424
import uxarray as ux
2525

26-
_NEMO_EXPECTED_COORDS = ["glamf", "gphif"]
26+
_NEMO_EXPECTED_COORDS = ["glamf", "gphif", "depthw"]
2727

2828
_NEMO_DIMENSION_COORD_NAMES = ["x", "y", "time", "x", "x_center", "y", "y_center", "depth", "depth_center", "glamf", "gphif"]
2929

@@ -95,25 +95,22 @@ def _pick_expected_coords(coords: xr.Dataset, expected_coord_names: list[str]) -
9595

9696

9797
def _maybe_bring_other_depths_to_depth(ds):
98-
if "depth" in ds.coords:
99-
for var in ds.data_vars:
100-
for old_depth, target in [
101-
("depthu", "depth_center"),
102-
("depthv", "depth_center"),
103-
("deptht", "depth_center"),
104-
("depthw", "depth"),
105-
]:
106-
if old_depth in ds[var].dims:
107-
ds[var] = ds[var].rename({old_depth: target})
108-
return ds
109-
110-
111-
def _maybe_create_depth_dim(ds):
98+
for var in ds.data_vars:
99+
for old_depth, target in [
100+
("depthu", "depth_center"),
101+
("depthv", "depth_center"),
102+
("deptht", "depth_center"),
103+
("depthw", "depth"),
104+
]:
105+
if old_depth in ds[var].dims:
106+
ds[var] = ds[var].rename({old_depth: target})
107+
112108
if "depth" not in ds.dims:
109+
warnings.warn("No depth dimension found in your dataset. Assuming no depth (i.e., surface data).", stacklevel=1)
113110
ds = ds.expand_dims({"depth": [0]})
114111
ds["depth"] = xr.DataArray([0], dims=["depth"])
115112
return ds
116-
113+
117114

118115
def _maybe_rename_coords(ds, axis_varnames):
119116
try:
@@ -291,16 +288,16 @@ def nemo_to_sgrid(*, fields: dict[str, xr.Dataset | xr.DataArray], coords: xr.Da
291288
if coords.sizes["time"] != 1:
292289
raise ValueError("Time dimension in coords must be length 1 (i.e., no time-varying grid).")
293290
coords = coords.isel(time=0).drop("time")
294-
if len(coords.dims) == 3:
291+
292+
if len(coords.dims) == 3: #! This should really be looking at the dimensionality of the lons and lats arrays. Currently having 2D lon lat and 1D depth triggers this `if` clause
295293
for dim, len_ in coords.sizes.items():
296294
if len_ == 1:
297295
# TODO: log statement about selecting along z dim of 1
298296
coords = coords.isel({dim: 0})
299-
if len(coords.dims) != 2:
300-
raise ValueError("Expected coordsinates to be 2 dimensional")
297+
# if len(coords.dims) != 2: #! This should really be looking at the dimensionality of the lons and lats arrays. Currently having 2D lon lat and 1D depth triggers this `if` clause
298+
# raise ValueError("Expected coordinates to be 2 dimensional")
301299
ds = xr.merge(list(fields.values()) + [coords])
302300
ds = _maybe_rename_variables(ds, _NEMO_VARNAMES_MAPPING)
303-
ds = _maybe_create_depth_dim(ds)
304301
ds = _maybe_bring_other_depths_to_depth(ds)
305302
ds = _drop_unused_dimensions_and_coords(ds, _NEMO_DIMENSION_COORD_NAMES)
306303
ds = _assign_dims_as_coords(ds, _NEMO_DIMENSION_COORD_NAMES)

0 commit comments

Comments
 (0)