|
1 | 1 | import contextlib
|
| 2 | +import gzip |
2 | 3 | import itertools
|
3 | 4 | import math
|
4 | 5 | import os.path
|
5 | 6 | import pickle
|
| 7 | +import re |
6 | 8 | import shutil
|
7 | 9 | import sys
|
8 | 10 | import tempfile
|
|
30 | 32 | save_mfdataset,
|
31 | 33 | )
|
32 | 34 | from xarray.backends.common import robust_getitem
|
| 35 | +from xarray.backends.h5netcdf_ import H5netcdfBackendEntrypoint |
33 | 36 | from xarray.backends.netcdf3 import _nc3_dtype_coercions
|
34 |
| -from xarray.backends.netCDF4_ import _extract_nc4_variable_encoding |
| 37 | +from xarray.backends.netCDF4_ import ( |
| 38 | + NetCDF4BackendEntrypoint, |
| 39 | + _extract_nc4_variable_encoding, |
| 40 | +) |
35 | 41 | from xarray.backends.pydap_ import PydapDataStore
|
| 42 | +from xarray.backends.scipy_ import ScipyBackendEntrypoint |
36 | 43 | from xarray.coding.variables import SerializationWarning
|
37 | 44 | from xarray.conventions import encode_dataset_coordinates
|
38 | 45 | from xarray.core import indexes, indexing
|
@@ -2771,7 +2778,7 @@ def test_open_badbytes(self):
|
2771 | 2778 | with open_dataset(b"garbage", engine="netcdf4"):
|
2772 | 2779 | pass
|
2773 | 2780 | with pytest.raises(
|
2774 |
| - ValueError, match=r"not the signature of a valid netCDF file" |
| 2781 | + ValueError, match=r"not the signature of a valid netCDF4 file" |
2775 | 2782 | ):
|
2776 | 2783 | with open_dataset(BytesIO(b"garbage"), engine="h5netcdf"):
|
2777 | 2784 | pass
|
@@ -2817,7 +2824,11 @@ def test_open_fileobj(self):
|
2817 | 2824 | with open(tmp_file, "rb") as f:
|
2818 | 2825 | f.seek(8)
|
2819 | 2826 | with pytest.raises(ValueError, match="cannot guess the engine"):
|
2820 |
| - open_dataset(f) |
| 2827 | + with pytest.warns( |
| 2828 | + RuntimeWarning, |
| 2829 | + match=re.escape("'h5netcdf' fails while guessing"), |
| 2830 | + ): |
| 2831 | + open_dataset(f) |
2821 | 2832 |
|
2822 | 2833 |
|
2823 | 2834 | @requires_h5netcdf
|
@@ -5161,3 +5172,86 @@ def test_chunking_consintency(chunks, tmp_path):
|
5161 | 5172 |
|
5162 | 5173 | with xr.open_dataset(tmp_path / "test.nc", chunks=chunks) as actual:
|
5163 | 5174 | xr.testing.assert_chunks_equal(actual, expected)
|
| 5175 | + |
| 5176 | + |
| 5177 | +def _check_guess_can_open_and_open(entrypoint, obj, engine, expected): |
| 5178 | + assert entrypoint.guess_can_open(obj) |
| 5179 | + with open_dataset(obj, engine=engine) as actual: |
| 5180 | + assert_identical(expected, actual) |
| 5181 | + |
| 5182 | + |
| 5183 | +@requires_netCDF4 |
| 5184 | +def test_netcdf4_entrypoint(tmp_path): |
| 5185 | + entrypoint = NetCDF4BackendEntrypoint() |
| 5186 | + ds = create_test_data() |
| 5187 | + |
| 5188 | + path = tmp_path / "foo" |
| 5189 | + ds.to_netcdf(path, format="netcdf3_classic") |
| 5190 | + _check_guess_can_open_and_open(entrypoint, path, engine="netcdf4", expected=ds) |
| 5191 | + _check_guess_can_open_and_open(entrypoint, str(path), engine="netcdf4", expected=ds) |
| 5192 | + |
| 5193 | + path = tmp_path / "bar" |
| 5194 | + ds.to_netcdf(path, format="netcdf4_classic") |
| 5195 | + _check_guess_can_open_and_open(entrypoint, path, engine="netcdf4", expected=ds) |
| 5196 | + _check_guess_can_open_and_open(entrypoint, str(path), engine="netcdf4", expected=ds) |
| 5197 | + |
| 5198 | + assert entrypoint.guess_can_open("http://something/remote") |
| 5199 | + assert entrypoint.guess_can_open("something-local.nc") |
| 5200 | + assert entrypoint.guess_can_open("something-local.nc4") |
| 5201 | + assert entrypoint.guess_can_open("something-local.cdf") |
| 5202 | + assert not entrypoint.guess_can_open("not-found-and-no-extension") |
| 5203 | + |
| 5204 | + path = tmp_path / "baz" |
| 5205 | + with open(path, "wb") as f: |
| 5206 | + f.write(b"not-a-netcdf-file") |
| 5207 | + assert not entrypoint.guess_can_open(path) |
| 5208 | + |
| 5209 | + |
| 5210 | +@requires_scipy |
| 5211 | +def test_scipy_entrypoint(tmp_path): |
| 5212 | + entrypoint = ScipyBackendEntrypoint() |
| 5213 | + ds = create_test_data() |
| 5214 | + |
| 5215 | + path = tmp_path / "foo" |
| 5216 | + ds.to_netcdf(path, engine="scipy") |
| 5217 | + _check_guess_can_open_and_open(entrypoint, path, engine="scipy", expected=ds) |
| 5218 | + _check_guess_can_open_and_open(entrypoint, str(path), engine="scipy", expected=ds) |
| 5219 | + with open(path, "rb") as f: |
| 5220 | + _check_guess_can_open_and_open(entrypoint, f, engine="scipy", expected=ds) |
| 5221 | + |
| 5222 | + contents = ds.to_netcdf(engine="scipy") |
| 5223 | + _check_guess_can_open_and_open(entrypoint, contents, engine="scipy", expected=ds) |
| 5224 | + _check_guess_can_open_and_open( |
| 5225 | + entrypoint, BytesIO(contents), engine="scipy", expected=ds |
| 5226 | + ) |
| 5227 | + |
| 5228 | + path = tmp_path / "foo.nc.gz" |
| 5229 | + with gzip.open(path, mode="wb") as f: |
| 5230 | + f.write(contents) |
| 5231 | + _check_guess_can_open_and_open(entrypoint, path, engine="scipy", expected=ds) |
| 5232 | + _check_guess_can_open_and_open(entrypoint, str(path), engine="scipy", expected=ds) |
| 5233 | + |
| 5234 | + assert entrypoint.guess_can_open("something-local.nc") |
| 5235 | + assert entrypoint.guess_can_open("something-local.nc.gz") |
| 5236 | + assert not entrypoint.guess_can_open("not-found-and-no-extension") |
| 5237 | + assert not entrypoint.guess_can_open(b"not-a-netcdf-file") |
| 5238 | + |
| 5239 | + |
| 5240 | +@requires_h5netcdf |
| 5241 | +def test_h5netcdf_entrypoint(tmp_path): |
| 5242 | + entrypoint = H5netcdfBackendEntrypoint() |
| 5243 | + ds = create_test_data() |
| 5244 | + |
| 5245 | + path = tmp_path / "foo" |
| 5246 | + ds.to_netcdf(path, engine="h5netcdf") |
| 5247 | + _check_guess_can_open_and_open(entrypoint, path, engine="h5netcdf", expected=ds) |
| 5248 | + _check_guess_can_open_and_open( |
| 5249 | + entrypoint, str(path), engine="h5netcdf", expected=ds |
| 5250 | + ) |
| 5251 | + with open(path, "rb") as f: |
| 5252 | + _check_guess_can_open_and_open(entrypoint, f, engine="h5netcdf", expected=ds) |
| 5253 | + |
| 5254 | + assert entrypoint.guess_can_open("something-local.nc") |
| 5255 | + assert entrypoint.guess_can_open("something-local.nc4") |
| 5256 | + assert entrypoint.guess_can_open("something-local.cdf") |
| 5257 | + assert not entrypoint.guess_can_open("not-found-and-no-extension") |
0 commit comments