Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion suxarray/core/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def read_schism_nc(grid: Grid, ds_data: xr.Dataset) -> SxDataset:
ds_data = ux.core.utils._map_dims_to_ugrid(ds_data, grid._source_dims_dict, grid)
if "nSCHISM_vgrid_layers" in ds_data.dims:
ds_data = ds_data.swap_dims({"nSCHISM_vgrid_layers": "n_layer"})
sxds = SxDataset(ds_data, sxgrid=grid)
sxds = SxDataset(dict(ds_data.data_vars), sxgrid=grid, attrs=ds_data.attrs)
return sxds


Expand Down
28 changes: 28 additions & 0 deletions tests/test_suxarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,34 @@ def test_read_schism_nc():
assert sxds.time.size == 48


def test_read_schism_nc_preserves_attrs():
"""
Test conservation of dataset-level attrs in read_schism_nc.
"""
p_cur = Path(__file__).parent.absolute()
ds_out2d = xr.open_dataset(str(p_cur / "testdata/out2d_1.nc"))
ds_zcoords = xr.open_dataset(str(p_cur / "testdata/zCoordinates_1.nc"))
grid = sx.read_grid(ds_out2d, ds_zcoords)
ds_data = xr.open_dataset(str(p_cur / "testdata/salinity_1.nc"))

# Attach sample global attributes to the source dataset.
sample_attrs = {
"Conventions": "CF-1.0",
"title": "Bay-Delta SCHISM salinity output",
"source": "SCHISM",
}
ds_data.attrs = sample_attrs

sxds = sx.read_schism_nc(grid, ds_data)

# The global attributes must survive the round trip.
for key, value in sample_attrs.items():
assert sxds.attrs.get(key) == value

# The data variable must still be intact.
assert "salinity" in sxds


def test_read_hgrid_gr3():
"""Test read_hgrid_gr3"""
# Test with a HelloSCHISM v5.10 hgrid.gr3 file
Expand Down
Loading