Skip to content

Commit d47af24

Browse files
xylarclaude
andcommitted
Add tests for open_dataset and open_mfdataset
Cover the new wrappers: a basic write/read round trip, opening a NETCDF3_64BIT_DATA (CDF5) file with an explicit engine (exercising the backend-sniffing crash the wrappers work around), resolving the engine from mpas_tools.io.default_engine when engine is None (restoring the global afterward), and a multi-file open_mfdataset smoke test. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent ed1bfd4 commit d47af24

1 file changed

Lines changed: 60 additions & 1 deletion

File tree

conda_package/tests/test_io.py

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
import pytest
66
import xarray as xr
77

8-
from mpas_tools.io import write_netcdf
8+
import mpas_tools.io
9+
from mpas_tools.io import open_dataset, open_mfdataset, write_netcdf
910
from mpas_tools.logging import LoggingContext
1011

1112
from .util import get_test_data_file
@@ -59,6 +60,64 @@ def test_write_netcdf_cdf5_format(tmp_path):
5960
assert not os.path.exists(tmp_file)
6061

6162

63+
def test_open_dataset_basic(tmp_path):
64+
# Write a file then read it back via the wrapper
65+
arr = np.array([1.0, 2.0, 3.0], dtype=np.float32)
66+
ds = xr.Dataset({'foo': (('x',), arr)})
67+
out_file = tmp_path / 'test_open_basic.nc'
68+
write_netcdf(ds, str(out_file))
69+
ds2 = open_dataset(str(out_file))
70+
assert set(ds.dims) == set(ds2.dims)
71+
assert 'foo' in ds2.data_vars
72+
np.testing.assert_array_equal(ds2['foo'].values, arr)
73+
ds2.close()
74+
75+
76+
def test_open_dataset_cdf5(tmp_path):
77+
# Opening a CDF5 (NETCDF3_64BIT_DATA) file with an explicit engine should
78+
# succeed; this exercises the bug the wrapper works around.
79+
arr = np.array([1.0, 2.0, 3.0], dtype=np.float32)
80+
ds = xr.Dataset({'foo': (('x',), arr)})
81+
out_file = tmp_path / 'test_open_cdf5.nc'
82+
write_netcdf(ds, str(out_file), format='NETCDF3_64BIT_DATA')
83+
ds2 = open_dataset(str(out_file), engine='netcdf4')
84+
np.testing.assert_array_equal(ds2['foo'].values, arr)
85+
ds2.close()
86+
87+
88+
def test_open_dataset_default_engine(tmp_path):
89+
# When engine is None, the wrapper should use mpas_tools.io.default_engine
90+
arr = np.array([1.0, 2.0, 3.0], dtype=np.float32)
91+
ds = xr.Dataset({'foo': (('x',), arr)})
92+
out_file = tmp_path / 'test_open_default_engine.nc'
93+
write_netcdf(ds, str(out_file), format='NETCDF3_64BIT_DATA')
94+
saved_engine = mpas_tools.io.default_engine
95+
try:
96+
mpas_tools.io.default_engine = 'netcdf4'
97+
ds2 = open_dataset(str(out_file))
98+
np.testing.assert_array_equal(ds2['foo'].values, arr)
99+
ds2.close()
100+
finally:
101+
mpas_tools.io.default_engine = saved_engine
102+
103+
104+
def test_open_mfdataset(tmp_path):
105+
# Smoke test: write two files along a dimension and open them combined
106+
out_files = []
107+
for index in range(2):
108+
arr = np.array([index], dtype=np.float32)
109+
ds = xr.Dataset({'foo': (('Time',), arr)})
110+
out_file = tmp_path / f'test_open_mf_{index}.nc'
111+
write_netcdf(ds, str(out_file))
112+
out_files.append(str(out_file))
113+
ds2 = open_mfdataset(
114+
out_files, engine='netcdf4', combine='nested', concat_dim='Time'
115+
)
116+
assert ds2.sizes['Time'] == 2
117+
np.testing.assert_array_equal(ds2['foo'].values, [0.0, 1.0])
118+
ds2.close()
119+
120+
62121
def test_write_netcdf_int64_conversion_and_attr(tmp_path):
63122
# Create a dataset with int64 variable and an attribute
64123
arr = np.array([1, 2, 3], dtype=np.int64)

0 commit comments

Comments
 (0)