Commit 5b945fd2 authored by Julia Garriga Ferrer's avatar Julia Garriga Ferrer
Browse files

[io][utils] Add write_maps function

parent ed639c83
...@@ -71,6 +71,81 @@ def advancement_display(iteration, total, prefix='', suffix='', decimals=1, leng ...@@ -71,6 +71,81 @@ def advancement_display(iteration, total, prefix='', suffix='', decimals=1, leng
if iteration == total: if iteration == total:
print() print()
def write_maps(h5_file, list_of_maps, data_path='/', overwrite=True):
Write a stack of components and its parameters into .h5
:param str h5_file: path to the hdf5 file
:param str entry: entry name
:param dict dimensions: Dictionary with the dimensions names and values
:param numpy.ndarray W: Matrix with the rocking curves values
:param numpy.ndarray data: Stack with the components
:param int processing_order: processing order of treatment
:param str data_path: path to store the data
process_name = 'process_' + str(processing_order)
def get_interpretation(my_data):
"""Return hdf5 attribute for this type of data"""
if isinstance(my_data, numpy.ndarray):
if my_data.ndim == 1:
return 'spectrum'
elif my_data.ndim in (2, 3):
return 'image'
return None
def save_key(path_name, key_path, value, overwrite=True):
"""Save the given value to the associated path. Manage numpy arrays
and dictionaries"""
key_path = key_path.replace('.', '/')
# save if is dict
if isinstance(value, dict):
h5_path = '/'.join((path_name, key_path))
dicttoh5(value, h5file=h5_file, h5path=h5_path,
overwrite_data=True, mode='a')
with h5py.File(h5_file, 'a') as h5f:
nx = h5f.require_group(path_name)
if overwrite and key_path in nx:
del nx[key_path]
nx[key_path] = value
except TypeError as e:
_logger.error('Unable to write', str(key_path), 'reason is', str(e))
interpretation = get_interpretation(value)
if interpretation:
nx[key_path].attrs['interpretation'] = interpretation
with h5py.File(h5_file, 'a') as h5f:
h5f.attrs["default"] = "entry"
nx_entry = h5f.require_group('/'.join((data_path, entry)))
nx_entry.attrs["NX_class"] = "NXentry"
nx_entry.attrs["default"] = "data"
nx_process = nx_entry.require_group(process_name)
nx_process.attrs['NX_class'] = "NXprocess"
if overwrite:
for key in ('program', 'version', 'date', 'processing_order'):
if key in nx_process:
del nx_process[key]
nx_process['program'] = 'darfix'
nx_process['version'] = '0.2'
nx_process['date'] =
nx_process['processing_order'] = numpy.int32(processing_order)
results = nx_process.require_group("results")
results.attrs["NX_class"] = "NXcollection"
for _map in list_of_maps:
nx_data = nx_entry.require_group(_map)
nx_data.attrs["NX_class"] = "NXdata"
nx_data.attrs["signal"] = _map
source_addr = "entry/" + process_name + "/results/" + _map
results.attrs["target"] = write_maps
save_key(, _map, W)
save_key(, _map, h5f[source_addr])
def read_components(h5_file): def read_components(h5_file):
""" """
Supports Markdown
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