3 """Useful utilites not related to calibration.
5 Currently just a framework for consistently saving/loading calibration
11 from h5config.storage.hdf5 import HDF5_Storage as _HDF5_Storage
12 from h5config.storage.hdf5 import h5_create_group as _h5_create_group
15 from . import LOG as _LOG
18 class SaveSpec (object):
19 def __init__(self, item=None, relpath='/', key=None, config=False,
20 array=False, units=None, deviation=None):
22 self.relpath = relpath
27 self.deviation = deviation
29 def save(filename=None, group='/', specs=tuple()):
31 storage = _HDF5_Storage()
33 if isinstance(group, str):
34 f = _h5py.File(filename, 'a')
35 group = _h5_create_group(f, group)
39 cwg = _h5_create_group(group, spec.relpath)
41 storage.save(config=spec.item, group=cwg)
43 assert spec.units, spec.item
52 cwg['data'] = spec.item
53 cwg['units'] = spec.units
54 if spec.deviation is not None:
55 cwg['standard-deviation'] = spec.deviation
60 def load(filename=None, group='/', specs=tuple()):
63 storage = _HDF5_Storage()
65 if isinstance(group, str):
66 f = _h5py.File(filename, 'a')
67 group = _h5_create_group(f, group)
70 cwg = group[spec.relpath]
74 for n in spec.key[:-1]:
79 d[spec.key[-1]] = spec.config(storage=_HDF5_Storage(group=cwg))
80 d[spec.key[-1]].load()
82 assert spec.units, spec.key
85 d[spec.key[-1]] = cwg['data'][...]
87 d[spec.key[-1]] = float(cwg['data'][...])
91 _LOG.warn('while loading {} from {}: {}'.format(
92 spec.key, cwg['data'], e))
95 units_ = cwg['units'][...]
96 assert units_ == spec.units, (units_, spec.units)
97 if spec.deviation is not None:
99 d[spec.deviation] = float(
100 cwg['standard-deviation'][...])