Commit 0a0c2e23 authored by Thomas Vincent's avatar Thomas Vincent

update string read/write in hdf5 files

parent e6d8a0a5
......@@ -37,6 +37,7 @@ import h5py as _h5py
import numpy as _np
from .XsocsH5Base import XsocsH5Base
from ._utils import str_to_h5_utf8, find_NX_class
class InvalidEntryError(Exception):
......@@ -88,12 +89,7 @@ class XsocsH5(XsocsH5Base):
def _update_entries(self):
with self._get_file() as h5_file:
# TODO : this isnt pretty but for some reason the attrs.get() fails
# when there is no attribute NX_class (should return the default
# None)
self.__entries = sorted([key for key in h5_file
if ('NX_class' in h5_file[key].attrs and
h5_file[key].attrs['NX_class'].decode() == 'NXentry')])
self.__entries = sorted(find_NX_class(h5_file, 'NXentry'))
def entries(self):
if self.__entries is None:
......@@ -196,16 +192,20 @@ class XsocsH5(XsocsH5Base):
def scan_positions(self, entry):
path = self.measurement_tpl.format(entry)
params = self.scan_params(entry)
m0 = '/{0}'.format(MOTORCOLS[params['motor_0'].decode()])
m1 = '/{0}'.format(MOTORCOLS[params['motor_1'].decode()])
motors = [m.decode() if hasattr(m, 'decode') else m
for m in (params['motor_0'], params['motor_1'])]
m0 = '/{0}'.format(MOTORCOLS[motors[0]])
m1 = '/{0}'.format(MOTORCOLS[motors[1]])
n_0 = params['motor_0_steps']
n_1 = params['motor_1_steps']
x_pos = self._get_array_data(path + m0)
y_pos = self._get_array_data(path + m1)
return ScanPositions(motor_0=params['motor_0'],
return ScanPositions(motor_0=motors[0],
pos_0=x_pos,
motor_1=params['motor_1'],
motor_1=motors[1],
pos_1=y_pos,
shape=(n_0, n_1))
......@@ -373,11 +373,11 @@ class XsocsH5Writer(XsocsH5):
delay,
**kwargs):
params = OrderedDict([('motor_0', _np.string_(motor_0)),
params = OrderedDict([('motor_0', str_to_h5_utf8(motor_0)),
('motor_0_start', float(motor_0_start)),
('motor_0_end', float(motor_0_end)),
('motor_0_steps', int(motor_0_steps)),
('motor_1', _np.string_(motor_1)),
('motor_1', str_to_h5_utf8(motor_1)),
('motor_1_start', float(motor_1_start)),
('motor_1_end', float(motor_1_end)),
('motor_1_steps', int(motor_1_steps)),
......@@ -390,29 +390,29 @@ class XsocsH5Writer(XsocsH5):
def create_entry(self, entry):
with self._get_file() as h5_file:
entry_grp = h5_file.require_group(entry)
entry_grp.attrs['NX_class'] = _np.string_('NXentry')
entry_grp.attrs['NX_class'] = str_to_h5_utf8('NXentry')
# creating mandatory groups and setting their Nexus attributes
grp = entry_grp.require_group('measurement/image')
grp.attrs['interpretation'] = _np.string_('image')
grp.attrs['interpretation'] = str_to_h5_utf8('image')
# setting the nexus classes
# entry_grp.attrs['NX_class'] = _np.string_('NXentry')
# entry_grp.attrs['NX_class'] = str_to_h5_utf8('NXentry')
grp = entry_grp.require_group('instrument')
grp.attrs['NX_class'] = _np.string_('NXinstrument')
grp.attrs['NX_class'] = str_to_h5_utf8('NXinstrument')
grp = entry_grp.require_group('instrument/detector')
grp.attrs['NX_class'] = _np.string_('NXdetector')
grp.attrs['NX_class'] = str_to_h5_utf8('NXdetector')
grp = entry_grp.require_group('instrument/positioners')
grp.attrs['NX_class'] = _np.string_('NXcollection')
grp.attrs['NX_class'] = str_to_h5_utf8('NXcollection')
grp = entry_grp.require_group('measurement')
grp.attrs['NX_class'] = _np.string_('NXcollection')
grp.attrs['NX_class'] = str_to_h5_utf8('NXcollection')
grp = entry_grp.require_group('measurement/image')
grp.attrs['NX_class'] = _np.string_('NXcollection')
grp.attrs['NX_class'] = str_to_h5_utf8('NXcollection')
# creating some links
grp = entry_grp.require_group('measurement/image')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment