Commit a752b652 authored by Julia Garriga Ferrer's avatar Julia Garriga Ferrer
Browse files

[io][utils] Add overwrite option to write_process function

parent 8843f807
......@@ -117,7 +117,7 @@ def write_process(h5_file, entry, dimensions, W, data, processing_order,
return 'image'
return None
def save_key(path_name, key_path, value):
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('.', '/')
......@@ -129,6 +129,8 @@ def write_process(h5_file, entry, dimensions, W, data, processing_order,
else:
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]
try:
nx[key_path] = value
except TypeError as e:
......@@ -147,8 +149,7 @@ def write_process(h5_file, entry, dimensions, W, data, processing_order,
nx_process = nx_entry.require_group(process_name)
nx_process.attrs['NX_class'] = "NXprocess"
if overwrite:
for key in ('program', 'version', 'date', 'processing_order',
'class_instance', 'ft'):
for key in ('program', 'version', 'date', 'processing_order'):
if key in nx_process:
del nx_process[key]
nx_process['program'] = 'darfix'
......@@ -158,19 +159,18 @@ def write_process(h5_file, entry, dimensions, W, data, processing_order,
nx_parameters = nx_process.require_group("inputs")
nx_parameters.attrs['NX_class'] = "NXparameters"
nx_parameters_path = nx_parameters.name
for key, value in dimensions.items():
save_key(nx_parameters.name, key_path=key, value=value)
results = nx_process.require_group("results")
results.attrs["NX_class"] = "NXcollection"
results_path = results.name
nx_data = nx_entry.require_group("data")
nx_data.attrs["NX_class"] = "NXdata"
nx_data.attrs["signal"] = "components"
source_addr = "entry/" + process_name + "/results/components"
results.attrs["target"] = "components"
save_key(results_path, "W", W)
save_key(results_path, "components", data)
nx_data["components"] = h5f[source_addr]
for key, value in dimensions.items():
save_key(nx_parameters_path, key_path=key, value=value)
save_key(results.name, "W", W)
save_key(results.name, "components", data)
save_key(nx_data.name, "components", h5f[source_addr])
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