Skip to content

Commit 1fa7fd0

Browse files
committed
Modified HDF Loader
Will now look through the file to work out what's in the file and what shapes these datasets are. This may not be 100% useful in the future...
1 parent 3397b14 commit 1fa7fd0

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/modacor/io/hdf/hdf_loader.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
from ..io_source import IoSource
3131
from ..io_sources import IoSources
3232
from modacor.dataclasses.messagehandler import *
33+
34+
from os.path import abspath
3335
from logging import WARNING
3436
import numpy as np
3537
import h5py
@@ -39,6 +41,10 @@ class HDFLoader(IoSources):
3941
def __init__(self, source_reference: str, source: IoSource, logging_level = WARNING):
4042
super().__init__(source_reference, source)
4143
self.hdf_logger = MessageHandler('hdf5logger', logging_level)
44+
self._file_path = None
45+
self._file_reference = None
46+
self._file_datasets = []
47+
self._file_datasets_shapes = {}
4248

4349

4450
def _open_file(self, file_path = None):
@@ -48,7 +54,9 @@ def _open_file(self, file_path = None):
4854
raise OSError(error)
4955

5056
try:
51-
self.file_reference = h5py.File(file_path, 'r')
57+
self._file_reference = h5py.File(file_path, 'r')
58+
self._file_path = abspath(file_path)
59+
self._file_reference.visititems(self._find_datasets)
5260
except OSError as error:
5361
self.hdf_logger.log.error(error)
5462
raise OSError(error)
@@ -57,6 +65,18 @@ def _open_file(self, file_path = None):
5765
def _close_file(self):
5866
try:
5967
self.file_reference.close()
68+
self._file_path = None
69+
self._file_reference = None
70+
self._file_datasets.clear()
71+
self._file_datasets_shapes.clear()
6072
except OSError as error:
6173
self.hdf_logger.log.error(error)
6274
raise OSError(error)
75+
76+
77+
def _find_datasets(self, path_name, path_object):
78+
"""An internal function to be used to walk the tree of an HDF5 file and return a list of the datasets within"""
79+
80+
if(isinstance(self._file_reference[path_name], h5py._hl.dataset.Dataset)):
81+
self._file_datasets.append(path_name)
82+
self._file_datasets_shapes[path_name] = self._file_reference[path_name].shape

0 commit comments

Comments
 (0)