|
9 | 9 | Xarray dataset so that users can further provide any missing metadata that was unable to |
10 | 10 | be determined before they pass it to the FieldSet constructor. |
11 | 11 | """ |
12 | | - |
13 | 12 | from __future__ import annotations |
| 13 | +import warnings |
14 | 14 |
|
15 | 15 | import typing |
16 | 16 |
|
|
23 | 23 | if typing.TYPE_CHECKING: |
24 | 24 | import uxarray as ux |
25 | 25 |
|
26 | | -_NEMO_EXPECTED_COORDS = ["glamf", "gphif"] |
| 26 | +_NEMO_EXPECTED_COORDS = ["glamf", "gphif", "depthw"] |
27 | 27 |
|
28 | 28 | _NEMO_DIMENSION_COORD_NAMES = ["x", "y", "time", "x", "x_center", "y", "y_center", "depth", "depth_center", "glamf", "gphif"] |
29 | 29 |
|
@@ -95,25 +95,22 @@ def _pick_expected_coords(coords: xr.Dataset, expected_coord_names: list[str]) - |
95 | 95 |
|
96 | 96 |
|
97 | 97 | 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 | + |
112 | 108 | 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) |
113 | 110 | ds = ds.expand_dims({"depth": [0]}) |
114 | 111 | ds["depth"] = xr.DataArray([0], dims=["depth"]) |
115 | 112 | return ds |
116 | | - |
| 113 | + |
117 | 114 |
|
118 | 115 | def _maybe_rename_coords(ds, axis_varnames): |
119 | 116 | try: |
@@ -291,16 +288,16 @@ def nemo_to_sgrid(*, fields: dict[str, xr.Dataset | xr.DataArray], coords: xr.Da |
291 | 288 | if coords.sizes["time"] != 1: |
292 | 289 | raise ValueError("Time dimension in coords must be length 1 (i.e., no time-varying grid).") |
293 | 290 | 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 |
295 | 293 | for dim, len_ in coords.sizes.items(): |
296 | 294 | if len_ == 1: |
297 | 295 | # TODO: log statement about selecting along z dim of 1 |
298 | 296 | 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") |
301 | 299 | ds = xr.merge(list(fields.values()) + [coords]) |
302 | 300 | ds = _maybe_rename_variables(ds, _NEMO_VARNAMES_MAPPING) |
303 | | - ds = _maybe_create_depth_dim(ds) |
304 | 301 | ds = _maybe_bring_other_depths_to_depth(ds) |
305 | 302 | ds = _drop_unused_dimensions_and_coords(ds, _NEMO_DIMENSION_COORD_NAMES) |
306 | 303 | ds = _assign_dims_as_coords(ds, _NEMO_DIMENSION_COORD_NAMES) |
|
0 commit comments