Skip to content

Commit

Permalink
Merge pull request #62 from kif/ID02
Browse files Browse the repository at this point in the history
Id02
  • Loading branch information
kif authored Feb 22, 2024
2 parents 50a0894 + 22f72c5 commit a164e6e
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 89 deletions.
17 changes: 13 additions & 4 deletions plugins/id02/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
__contact__ = "[email protected]"
__license__ = "MIT"
__copyright__ = "European Synchrotron Radiation Facility, Grenoble, France"
__date__ = "26/08/2021"
__date__ = "18/03/2022"
__status__ = "development"
__version__ = "0.9.2"

Expand All @@ -16,9 +16,12 @@
import json
import numpy
from dahu.plugin import Plugin
from dahu import version as dahu_version
from dahu.utils import get_isotime, fully_qualified_name
import PyTango
try:
import PyTango
except ImportError:
PyTango = None

import logging
logger = logging.getLogger("id02.metadata")
from .common import StringTypes, Nexus, ensure_str
Expand Down Expand Up @@ -237,7 +240,10 @@ def read_c216(self):
"""
Manage the metadata coming from C216 Time Frame Generator
"""
if PyTango is None:
self.log_error("PyTango is needed to connect to C216", do_raise=True)
c216ds = PyTango.DeviceProxy(str(self.c216))
c216ds.set_timeout_millis(5000)
if c216ds.CompStatus("Tango::RUNNING") == "Tango::ON":
msg = "C216 is running while reading counters ... possible issue"
self._logging.append(msg)
Expand Down Expand Up @@ -280,7 +286,10 @@ def read_c216(self):
self.tfg_grp[key].attrs["interpretation"] = "scalar"

# raw scalers:
raw_scalers = c216ds.ReadScalersForNLiveFrames([0, frames - 1])
blocksize = 512
raw_scalers = numpy.concatenate([c216ds.ReadScalersForNLiveFrames([start, min(start + blocksize, frames) - 1])
for start in range(0, frames, blocksize)])

raw_scalers.shape = frames, -1
counters = raw_scalers.shape[1]
self.mcs_grp["HS32C"] = raw_scalers
Expand Down
25 changes: 19 additions & 6 deletions plugins/id02/nexus.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"""Module for writing HDF5 in the Nexus style"""

__author__ = "Jerome Kieffer"
__author__ = "Jérôme Kieffer"
__contact__ = "[email protected]"
__license__ = "MIT"
__copyright__ = "European Synchrotron Radiation Facility, Grenoble, France"
__date__ = "20/02/2020"
__date__ = "29/01/2024"
__status__ = "production"
__docformat__ = 'restructuredtext'

Expand Down Expand Up @@ -85,13 +85,14 @@ class Nexus(object):
TODO: make it thread-safe !!!
"""

def __init__(self, filename, mode=None, creator=None):
def __init__(self, filename, mode=None, creator=None, start_time=None):
"""
Constructor
:param filename: name of the hdf5 file containing the nexus
:param mode: can be 'r', 'a', 'w', '+' ....
:param creator: set as attr of the NXroot
:param start_time: set as attr of the NXroot
"""
self.filename = os.path.abspath(filename)
self.mode = mode
Expand All @@ -105,12 +106,18 @@ def __init__(self, filename, mode=None, creator=None):
self.mode = "r"
else:
self.mode = "a"
self.h5 = h5py.File(self.filename, mode=self.mode)

if self.mode == "r" and h5py.version.version_tuple >= (2, 9):
self.file_handle = open(self.filename, mode=self.mode + "b")
self.h5 = h5py.File(self.file_handle, mode=self.mode)
else:
self.file_handle = None
self.h5 = h5py.File(self.filename, mode=self.mode)
self.to_close = []

if not pre_existing:
if not pre_existing or "w" in mode:
self.h5.attrs["NX_class"] = "NXroot"
self.h5.attrs["file_time"] = get_isotime()
self.h5.attrs["file_time"] = get_isotime(start_time)
self.h5.attrs["file_name"] = self.filename
self.h5.attrs["HDF5_Version"] = h5py.version.hdf5_version
self.h5.attrs["creator"] = creator or self.__class__.__name__
Expand All @@ -125,6 +132,8 @@ def close(self, end_time=None):
entry["end_time"] = end_time
self.h5.attrs["file_update_time"] = get_isotime()
self.h5.close()
if self.file_handle:
self.file_handle.close()

# Context manager for "with" statement compatibility
def __enter__(self, *arg, **kwarg):
Expand All @@ -133,6 +142,10 @@ def __enter__(self, *arg, **kwarg):
def __exit__(self, *arg, **kwarg):
self.close()

def flush(self):
if self.h5:
self.h5.flush()

def get_entry(self, name):
"""
Retrieves an entry from its name
Expand Down
Loading

0 comments on commit a164e6e

Please sign in to comment.