Skip to content

Commit

Permalink
This PR adds support for nemo 5.0 (#127)
Browse files Browse the repository at this point in the history
* add data, rm e3t_0 as not always present

* no real need to test for this metric

* better test for depth

* update new vertical dimensions

* no need to use keys, removes a xarray future warning

---------

Co-authored-by: Romain <romain.caneill@zaclys.net>
  • Loading branch information
rcaneill and Romain authored Jan 15, 2025
1 parent 993f627 commit 3de4680
Show file tree
Hide file tree
Showing 52 changed files with 65 additions and 9 deletions.
26 changes: 20 additions & 6 deletions xnemogcm/nemo.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ def _get_point_type(filename, description):
return point_type


def _is_depth_dim(i, attrs):
if "depth" == i[:5]:
return True
if "long_name" not in attrs.keys():
return False
return attrs["long_name"][:8] == "Vertical" and attrs["long_name"][-6:] == "levels"


def nemo_preprocess(ds, domcfg, point_type=None):
"""
Preprocess function for the nemo files.
Expand Down Expand Up @@ -73,17 +81,23 @@ def nemo_preprocess(ds, domcfg, point_type=None):
)

point = akp.Point(point_type)
# get the name of the depth variable e.g. deptht, depthu, etc
try:
z_nme = [i for i in ds.dims if "depth" in i][0]
except IndexError:

# the depth variable name can be either deptht, depthu, etc
# or grid_T_3D_inner, etc
all_z_nme = [i for i in ds.dims if _is_depth_dim(i, ds[i].attrs)]
if len(all_z_nme) >= 1:
z_nme = all_z_nme[0]
ds = ds.swap_dims({i: "depth_tmp_xnemogcm" for i in all_z_nme}).swap_dims(
{"depth_tmp_xnemogcm": z_nme}
)
else:
# This means that there is no depth dependence of the data (surface data)
z_nme = None

# get the name of the dimension along i e.g. x, x_grid_U, x_grid_U_inner etc
x_nme = [i for i in ds.dims.keys() if "x_grid" in i or i == "x"]
x_nme = [i for i in ds.dims if "x_grid" in i or i == "x"]
# get the name of the dimension along j e.g. y, y_grid_U, y_grid_U_inner etc
y_nme = [i for i in ds.dims.keys() if "y_grid" in i or i == "y"]
y_nme = [i for i in ds.dims if "y_grid" in i or i == "y"]

for x in x_nme:
to_rename.update({x: point.x})
Expand Down
2 changes: 1 addition & 1 deletion xnemogcm/test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
TEST_PATH = Path(os.path.dirname(os.path.abspath(__file__)))
DATA_PATH = TEST_PATH / "data"

nemo_versions = ["3.6", "4.0", "4.2.0"]
nemo_versions = ["3.6", "4.0", "4.2.0", "5.0"]


def pytest_generate_tests(metafunc):
Expand Down
1 change: 1 addition & 0 deletions xnemogcm/test/data/5.0/domcfg_mesh_mask/mesh_mask_0000.nc
1 change: 1 addition & 0 deletions xnemogcm/test/data/5.0/domcfg_mesh_mask/mesh_mask_0001.nc
1 change: 1 addition & 0 deletions xnemogcm/test/data/5.0/domcfg_mesh_mask/mesh_mask_0002.nc
1 change: 1 addition & 0 deletions xnemogcm/test/data/5.0/domcfg_mesh_mask/mesh_mask_0003.nc
1 change: 1 addition & 0 deletions xnemogcm/test/data/5.0/mesh_mask_1_file/mesh_mask.nc
1 change: 1 addition & 0 deletions xnemogcm/test/data/5.0/nemo_no_grid_in_filename/T.nc
1 change: 1 addition & 0 deletions xnemogcm/test/data/5.0/nemo_no_grid_in_filename/U.nc
1 change: 1 addition & 0 deletions xnemogcm/test/data/5.0/nemo_no_grid_in_filename/V.nc
1 change: 1 addition & 0 deletions xnemogcm/test/data/5.0/nemo_no_grid_in_filename/W.nc
1 change: 1 addition & 0 deletions xnemogcm/test/data/5.0/open_and_merge/mesh_mask.nc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
2 changes: 0 additions & 2 deletions xnemogcm/test/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ def test_merge_non_linear_free_surface(data_path, request):
nemo_ds = open_nemo(**nemo_kwargs)
ds = _merge_nemo_and_domain_cfg(nemo_ds, domcfg, linear_free_surface=False)
assert "e3t" in ds
if request.node.callspec.id != "3.6":
assert "e3t_0" in ds
assert "t" in ds.e3t.coords
assert "e3f" not in ds
ds2 = open_nemo_and_domain_cfg(
Expand Down
16 changes: 16 additions & 0 deletions xnemogcm/test/test_nemo.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ def test_coordinates_vertical(data_path, request):
pytest.xfail(
"Failing for nemo <= 3.6 as gdept_0 and gdepw_0 are not in mesh mask"
)
if request.node.callspec.id == "5.0":
pytest.xfail(
"Failing for nemo <= 5.0 as gdept_0 and gdepw_0 are not in mesh mask"
)
domcfg = open_domain_cfg(
datadir=data_path / "mesh_mask_1_file",
)
Expand All @@ -189,3 +193,15 @@ def test_coordinates_vertical(data_path, request):
domcfg=domcfg,
)
assert "gdept_0" in nemo_ds.toce.coords


def test_coordinates_vertical_1d(data_path, request):
"""Test that coordinates are added to nemo files"""
domcfg = open_domain_cfg(
datadir=data_path / "mesh_mask_1_file",
)
nemo_ds = open_nemo(
datadir=data_path / "nemo",
domcfg=domcfg,
)
assert "gdept_1d" in nemo_ds.toce.coords

0 comments on commit 3de4680

Please sign in to comment.