|
5 | 5 | import pytest |
6 | 6 | import xarray as xr |
7 | 7 |
|
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 |
9 | 10 | from mpas_tools.logging import LoggingContext |
10 | 11 |
|
11 | 12 | from .util import get_test_data_file |
@@ -59,6 +60,64 @@ def test_write_netcdf_cdf5_format(tmp_path): |
59 | 60 | assert not os.path.exists(tmp_file) |
60 | 61 |
|
61 | 62 |
|
| 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 | + |
62 | 121 | def test_write_netcdf_int64_conversion_and_attr(tmp_path): |
63 | 122 | # Create a dataset with int64 variable and an attribute |
64 | 123 | arr = np.array([1, 2, 3], dtype=np.int64) |
|
0 commit comments