Skip to content

Commit

Permalink
Add a wrapping to h5py groups so that we can set checksumming
Browse files Browse the repository at this point in the history
  • Loading branch information
GarethCabournDavies committed Jul 31, 2024
1 parent b3f2d35 commit 0e29bd3
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion pycbc/io/hdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,41 @@
logger = logging.getLogger('pycbc.io.hdf')


class HFile(h5py.File):
class HGroup(h5py.Group):
""" Low level extensions to the h5py group object
"""
def create_group(self, name, track_order=None):
"""
Wrapper around h5py's create_group in order to redirect to the
manual HGroup object defined here
"""
if track_order is None:
track_order = h5py.h5.get_config().track_order

# FIXME: use the h5py file locker? with h5py.phil:
name, lcpl = self._e(name, lcpl=True)
gcpl = HGroup._gcpl_crt_order if track_order else None
gid = h5py.h5g.create(self.id, name, lcpl=lcpl, gcpl=gcpl)
return HGroup(gid)


def __setitem__(self, name, obj):
"""
Wrapper around h5py's __setitem__ so that checksums are used
"""
if type(obj) not in [h5py.HLObject, h5py.SoftLink, h5py.ExternalLink, np.dtype]:
name, lcpl = self._e(name, lcpl=True)
print(f"Setting {name} while using fletcher32")
ds = self.create_dataset(None, data=obj, fletcher32=True)
h5py.h5o.link(ds.id, self.id, name, lcpl=lcpl)
else:
h5py.File.__setitem__(self, name, obj)


class HFile(HGroup, h5py.File):
""" Low level extensions to the capabilities of reading an hdf5 File
"""

def select(self, fcn, *args, chunksize=10**6, derived=None, group='',
return_data=True, premask=None):
""" Return arrays from an hdf5 file that satisfy the given function
Expand Down

0 comments on commit 0e29bd3

Please sign in to comment.