Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: handler for hdf5 files that have variable frames per point #11

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions area_detector_handlers/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,67 @@ class IntegrityError(Exception):
pass


class HDF5VariableFramesHandler(HandlerBase):
"""
Handler for data stored in one Dataset of an HDF5 file.

Parameters
----------
filename : string
path to HDF5 file
swmr : bool, optional
Open the hdf5 file in SWMR read mode. Only used when mode = 'r'.
Default is False.
"""

specs = {"AD_HDF5_v1"} | HandlerBase.specs

def __init__(self, filename):
self._filename = filename
self._key = "/entry/data/data"
self._file = None
self._dataset = None
self._open()

def get_file_list(self, datum_kwarg_gen):
return [self._filename]

def __call__(self, offset, num_frames):
return self._dataset[offset: offset + num_frames]

def _open(self):
if self._file:
return

self._file = h5py.File(self._filename, "r")
self._dataset = self._file[self._key]

def close(self):
super().close()
self._dataset = None
self._file.close()
self._file = None


class HDF5VariableFramesHandlerTS(HDF5VariableFramesHandler):
specs = {"AD_HDF5_TS_v1"} | HandlerBase.specs

def _open(self):
if self._file:
return

self._file = h5py.File(self._filename, "r")
self._dataset1 = self._file["/entry/instrument/NDAttributes/NDArrayEpicsTSSec"]
self._dataset2 = self._file["/entry/instrument/NDAttributes/NDArrayEpicsTSnSec"]

def __call__(self, offset, num_frames):
# Don't read out the dataset until it is requested for the first time.
start, stop = offset, offset + num_frames
rtn = self._dataset1[start:stop].squeeze()
rtn = rtn + (self._dataset2[start:stop].squeeze() * 1e-9)
return rtn


class AreaDetectorSPEHandler(HandlerBase):
specs = {"AD_SPE"} | HandlerBase.specs

Expand Down
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@
"AD_EIGER2 = area_detector_handlers.eiger:EigerHandler",
"AD_EIGER_SLICE = area_detector_handlers.eiger:EigerHandler",
"AD_HDF5 = area_detector_handlers.handlers:AreaDetectorHDF5Handler",
"AD_HDF5_v1 = area_detector_handlers.handlers:HDF5VariableFramesHandler",
"AD_HDF5_SWMR = area_detector_handlers.handlers:AreaDetectorHDF5SWMRHandler",
"AD_HDF5_TS = area_detector_handlers.handlers:AreaDetectorHDF5TimestampHandler",
"AD_HDF5_TS_v1 = area_detector_handlers.handlers:HDF5VariableFramesHandlerTS",
"AD_HDF5_SWMR_TS = area_detector_handlers.handlers:AreaDetectorHDF5SWMRTimestampHandler",
"AD_HDF5_SINGLE = area_detector_handlers.handlers:AreaDetectorHDF5SingleHandler",
"SPECS_HDF5_SINGLE_DATAFRAME = area_detector_handlers.handlers:SpecsHDF5SingleHandlerDataFrame",
Expand Down