Skip to content

Commit

Permalink
modify condition to get t_bounds variable (#122)
Browse files Browse the repository at this point in the history
modify condition to get t_bounds variable
  • Loading branch information
noemieplanat authored Sep 11, 2024
1 parent 3381fc8 commit 2bcca35
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
7 changes: 5 additions & 2 deletions xnemogcm/nemo.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,15 @@ def nemo_preprocess(ds, domcfg, point_type=None):
# rename time and space
# get time_counter bounds
time_b = ds["time_counter"].attrs.get("bounds")
if time_b:
if time_b and time_b in ds:
to_rename.update({"time_counter": "t", time_b: "t_bounds"})
else:
to_rename.update({"time_counter": "t"})
if time_b not in ds:
ds["time_counter"].attrs.pop("bounds")
time_b = None
ds = ds.rename(to_rename)
if time_b:
if time_b and "t_bounds" in ds:
ds["t"].attrs["bounds"] = "t_bounds"
# setting z_c/z_f/x_c/etc to be the same as in domcfg
ds = ds.assign_coords({i: domcfg[i] for i in points})
Expand Down
14 changes: 14 additions & 0 deletions xnemogcm/test/test_nemo.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,20 @@ def test_use_preprocess(data_path):
assert "t" in ds


def test_use_preprocess_no_time_bound(data_path):
"""Test that if the time_bound variable does not exist, no error is raised"""
domcfg = open_domain_cfg(
datadir=data_path / "mesh_mask_1_file",
)
ds_raw = xr.open_dataset(data_path / "nemo/GYRE_1y_00010101_00011230_grid_T.nc")
ds_raw.encoding["source"] = "GYRE_1y_00010101_00011230_grid_T.nc"
ds_raw = ds_raw.drop_vars(ds_raw["time_counter"].attrs.get("bounds"))
ds = nemo_preprocess(ds_raw, domcfg)
assert "x_c" in ds
assert "t" in ds
assert ds["t"].attrs.get("bounds") is None


def test_coordinates_horizontal(data_path):
"""Test that coordinates are added to nemo files"""
domcfg = open_domain_cfg(
Expand Down

0 comments on commit 2bcca35

Please sign in to comment.