Skip to content

Commit

Permalink
Extract attributes from hdf5 root group.
Browse files Browse the repository at this point in the history
  • Loading branch information
sharkinsspatial committed Apr 22, 2024
1 parent 1dd3370 commit d92c75c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
18 changes: 13 additions & 5 deletions virtualizarr/readers/hdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,14 @@ def _dataset_dims(dataset: h5py.Dataset) -> Union[List[str], List[None]]:
return dims


def _extract_attrs(dataset: h5py.Dataset):
def _extract_attrs(h5obj: Union[h5py.Dataset, h5py.Group]):
"""
Extract attributes from an HDF5 dataset.
Extract attributes from an HDF5 group or dataset.
Parameters
----------
dataset : h5py.Dataset
An HDF5 dataset.
h5obj : h5py.Group or h5py.Dataset
An HDF5 group or dataset.
"""
_HIDDEN_ATTRS = {
"REFERENCE_LIST",
Expand All @@ -140,7 +140,7 @@ def _extract_attrs(dataset: h5py.Dataset):
"_NCProperties",
}
attrs = {}
for n, v in dataset.attrs.items():
for n, v in h5obj.attrs.items():
if n in _HIDDEN_ATTRS:
continue
# Fix some attribute values to avoid JSON encoding exceptions...
Expand Down Expand Up @@ -207,3 +207,11 @@ def virtual_vars_from_hdf(
raise NotImplementedError("Nested groups are not yet supported")

return variables


def attrs_from_root_group(path: str):
fs, file_path = fsspec.core.url_to_fs(path)
open_file = fs.open(path, "rb")
f = h5py.File(open_file, mode="r")
attrs = _extract_attrs(f)
return attrs
8 changes: 8 additions & 0 deletions virtualizarr/tests/test_readers/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ def string_attribute_netcdf4_file(tmpdir):
return filepath


@pytest.fixture
def root_attributes_netcdf4_file(tmpdir):
filepath = f"{tmpdir}/root_attributes.nc"
f = h5py.File(filepath, "w")
f.attrs["attribute_name"] = "attribute_name"
return filepath


@pytest.fixture
def group_netcdf4_file(tmpdir):
filepath = f"{tmpdir}/group.nc"
Expand Down
5 changes: 5 additions & 0 deletions virtualizarr/tests/test_readers/test_hdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ def test_string_attribute(self, string_attribute_netcdf4_file):
attrs = _extract_attrs(ds)
assert attrs["attribute_name"] == "attribute_name"

def test_root_attribute(self, root_attributes_netcdf4_file):
f = h5py.File(root_attributes_netcdf4_file)
attrs = _extract_attrs(f)
assert attrs["attribute_name"] == "attribute_name"


class TestVirtualVarsFromHDF:
def test_variable_with_dimensions(self, chunked_dimensions_netcdf4_file):
Expand Down

0 comments on commit d92c75c

Please sign in to comment.