From d18302c62005d77e6a0e0d4c22d2a78cf27e6474 Mon Sep 17 00:00:00 2001 From: Alexandre Georges Date: Tue, 7 Jul 2026 10:34:53 -0700 Subject: [PATCH 1/2] Fix: pass ds_data as dictionary in read_schism_nc to circumvent xarray guard on xr.Dataset parameter --- suxarray/core/api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/suxarray/core/api.py b/suxarray/core/api.py index 6acb997..131e765 100644 --- a/suxarray/core/api.py +++ b/suxarray/core/api.py @@ -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) return sxds From 8b7d30a931424f16dd55368b6a137872c290b250 Mon Sep 17 00:00:00 2001 From: Alexandre Georges Date: Tue, 7 Jul 2026 14:38:55 -0700 Subject: [PATCH 2/2] Fix: preserve dataset-level attributes in read_schism_nc function Building the SxDataset from ``dict(ds_data.data_vars)`` drops the source Dataset's global attributes unless they are forwarded explicitly. This forwards the attributes and test makes sure attrs exist and come back the same. --- suxarray/core/api.py | 2 +- tests/test_suxarray.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/suxarray/core/api.py b/suxarray/core/api.py index 131e765..c82391c 100644 --- a/suxarray/core/api.py +++ b/suxarray/core/api.py @@ -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(dict(ds_data.data_vars), sxgrid=grid) + sxds = SxDataset(dict(ds_data.data_vars), sxgrid=grid, attrs=ds_data.attrs) return sxds diff --git a/tests/test_suxarray.py b/tests/test_suxarray.py index 61dc8ee..7fc72cf 100644 --- a/tests/test_suxarray.py +++ b/tests/test_suxarray.py @@ -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