Skip to content

Commit 95fce11

Browse files
authored
Vendor kerchunk netCDF3 reader (#397)
1 parent 1160f3a commit 95fce11

File tree

8 files changed

+456
-1
lines changed

8 files changed

+456
-1
lines changed

docs/releases.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ Documentation
6060
Internal Changes
6161
~~~~~~~~~~~~~~~~
6262

63+
- Vendor netCDF3 reader from kerchunk. (:pull:`397`) By `Tom Nicholas <https://github.com/TomNicholas>`_.
64+
6365
.. _v1.2.0:
6466

6567
v1.2.0 (5th Dec 2024)

virtualizarr/readers/netcdf3.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def open_virtual_dataset(
2626
virtual_backend_kwargs: Optional[dict] = None,
2727
reader_options: Optional[dict] = None,
2828
) -> Dataset:
29-
from kerchunk.netCDF3 import NetCDF3ToZarr
29+
from virtualizarr.vendor.kerchunk.netCDF3 import NetCDF3ToZarr
3030

3131
if virtual_backend_kwargs:
3232
raise NotImplementedError(
@@ -38,6 +38,9 @@ def open_virtual_dataset(
3838
loadable_variables,
3939
)
4040

41+
if reader_options is None:
42+
reader_options = {}
43+
4144
refs = NetCDF3ToZarr(filepath, inline_threshold=0, **reader_options).translate()
4245

4346
# both group=None and group='' mean to read root group

virtualizarr/tests/test_readers/conftest.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import pathlib
12
import warnings
23

34
import h5py # type: ignore
@@ -322,3 +323,13 @@ def root_coordinates_hdf5_file(tmpdir, np_uncompressed_int16):
322323
f.create_dataset(name="lon", data=data)
323324
f.attrs.create(name="coordinates", data="lat lon")
324325
return filepath
326+
327+
328+
@pytest.fixture
329+
def netcdf3_file(tmp_path: pathlib.Path) -> pathlib.Path:
330+
ds = xr.Dataset({"foo": ("x", np.array([1, 2, 3]))})
331+
332+
filepath = tmp_path / "file.nc"
333+
ds.to_netcdf(filepath, format="NETCDF3_CLASSIC")
334+
335+
return filepath
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import numpy as np
2+
import xarray as xr
3+
import xarray.testing as xrt
4+
5+
from virtualizarr import open_virtual_dataset
6+
from virtualizarr.manifests import ChunkManifest, ManifestArray
7+
from virtualizarr.tests import requires_scipy
8+
from virtualizarr.zarr import ZArray
9+
10+
11+
@requires_scipy
12+
def test_read_netcdf3(netcdf3_file):
13+
filepath = str(netcdf3_file)
14+
vds = open_virtual_dataset(filepath)
15+
16+
assert isinstance(vds, xr.Dataset)
17+
assert list(vds.variables.keys()) == ["foo"]
18+
assert isinstance(vds["foo"].data, ManifestArray)
19+
20+
expected_manifest = ChunkManifest(
21+
entries={"0": {"path": filepath, "offset": 80, "length": 12}}
22+
)
23+
expected_zarray = ZArray(dtype=np.dtype(">i4"), shape=(3,), chunks=(3,))
24+
expected_ma = ManifestArray(chunkmanifest=expected_manifest, zarray=expected_zarray)
25+
expected_vds = xr.Dataset({"foo": xr.Variable(data=expected_ma, dims=["x"])})
26+
27+
xrt.assert_identical(vds, expected_vds)
28+
29+
30+
# TODO test loading data against xarray backend, see issue #394 for context
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Intake
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

virtualizarr/vendor/kerchunk/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)