Commit f3be7257 authored by payno's avatar payno
Browse files

Merge branch 'integrate_h5tonx' into 'master'

Integrate dicttinexus

See merge request workflow/est!39
parents e3effb06 9f149455
Pipeline #56148 failed with stages
in 6 minutes and 44 seconds
...@@ -31,7 +31,7 @@ import logging ...@@ -31,7 +31,7 @@ import logging
from datetime import datetime from datetime import datetime
import h5py import h5py
import numpy import numpy
from silx.io.dictdump import dicttoh5, h5todict from silx.io.dictdump import dicttoh5, h5todict, dicttonx
from silx.io.url import DataUrl from silx.io.url import DataUrl
from silx.utils.enum import Enum from silx.utils.enum import Enum
from est.units import ur from est.units import ur
...@@ -42,6 +42,7 @@ from est.io.utils import get_data ...@@ -42,6 +42,7 @@ from est.io.utils import get_data
import silx.io.h5py_utils import silx.io.h5py_utils
from silx.io.h5py_utils import File as HDF5File from silx.io.h5py_utils import File as HDF5File
from est import settings from est import settings
import json
try: try:
from est.io.utils.larch import read_ascii as larch_read_ascii from est.io.utils.larch import read_ascii as larch_read_ascii
...@@ -444,8 +445,6 @@ def write_xas_proc( ...@@ -444,8 +445,6 @@ def write_xas_proc(
with HDF5File(h5_file, "a") as h5f: with HDF5File(h5_file, "a") as h5f:
plot_group = h5f.require_group(plot_path) plot_group = h5f.require_group(plot_path)
plot_group.attrs["NX_class"] = "NXdata"
plot_group.attrs["interpretation"] = "spectrum"
assert plot.signal is not None assert plot.signal is not None
assert plot.axes is not None assert plot.axes is not None
...@@ -478,7 +477,6 @@ def write_xas_proc( ...@@ -478,7 +477,6 @@ def write_xas_proc(
_logger.info("{} is not available".format(plot.signal)) _logger.info("{} is not available".format(plot.signal))
else: else:
link_dataset(dataset_to_link=signal_dataset, name=plot.signal) link_dataset(dataset_to_link=signal_dataset, name=plot.signal)
plot_group.attrs["signal"] = plot.signal
# handle axes # handle axes
for axe in plot.axes: for axe in plot.axes:
try: try:
...@@ -487,7 +485,7 @@ def write_xas_proc( ...@@ -487,7 +485,7 @@ def write_xas_proc(
_logger.info("{} is not available".format(axe)) _logger.info("{} is not available".format(axe))
else: else:
link_dataset(dataset_to_link=axe_dataset, name=axe) link_dataset(dataset_to_link=axe_dataset, name=axe)
plot_group.attrs["axes"] = plot.axes
# handle auxiliary signals # handle auxiliary signals
if plot.auxiliary_signals is not None: if plot.auxiliary_signals is not None:
for aux_sig in plot.auxiliary_signals: for aux_sig in plot.auxiliary_signals:
...@@ -499,17 +497,24 @@ def write_xas_proc( ...@@ -499,17 +497,24 @@ def write_xas_proc(
link_dataset(dataset_to_link=aux_sig_dataset, name=aux_sig) link_dataset(dataset_to_link=aux_sig_dataset, name=aux_sig)
plot_group.attrs["auxiliary_signals"] = plot.auxiliary_signals plot_group.attrs["auxiliary_signals"] = plot.auxiliary_signals
# handle title(s)
if plot.title is not None:
plot_group.attrs["title"] = plot.title
if plot.title_latex is not None:
plot_group.attrs["title_latex"] = plot.title_latex
# handle silx style nexus_dict = {
if plot.silx_style is not None: "@NX_class": "NXdata",
import json "@interpretation": "spectrum",
"@signal": plot.signal,
"@axes": plot.axes,
}
# handle title(s)
if plot.title is not None:
nexus_dict["@title"] = plot.title
if plot.title_latex is not None:
nexus_dict["@title_latex"] = plot.title_latex
# handle silx style
if plot.silx_style is not None:
nexus_dict["@SILX_style"] = json.dumps(plot.silx_style)
plot_group.attrs["SILX_style"] = json.dumps(plot.silx_style) dicttonx(nexus_dict, h5_file, mode="a", h5path=plot_path, update_mode="replace")
# save plots # save plots
for i_plot, plot in enumerate(plots): for i_plot, plot in enumerate(plots):
...@@ -519,10 +524,13 @@ def write_xas_proc( ...@@ -519,10 +524,13 @@ def write_xas_proc(
# default plot will always be the first one # default plot will always be the first one
if len(plots) > 0: if len(plots) > 0:
plots_path = "/".join((entry, process_name, "plots")) plots_path = "/".join((entry, process_name, "plots"))
with HDF5File(h5_file, "a") as h5f: nexus_dict = {
plots_group = h5f.require_group(plots_path) "@NX_class": "NXdata",
plots_group.attrs["NX_class"] = "NXdata" "@default": "plot_0",
plots_group.attrs["default"] = "plot_0" }
dicttonx(
nexus_dict, h5_file, mode="a", h5path=plots_path, update_mode="replace"
)
if process.getConfiguration() is not None: if process.getConfiguration() is not None:
h5_path = "/".join((nx_process_path, "configuration")) h5_path = "/".join((nx_process_path, "configuration"))
...@@ -563,58 +571,38 @@ def write_xas( ...@@ -563,58 +571,38 @@ def write_xas(
:param str title: experiment title :param str title: experiment title
:param str definition: experiment definition :param str definition: experiment definition
""" """
with HDF5File(h5_file, "w") as h5f: h5path = "/".join((data_path, entry))
nx_entry = h5f.require_group("/".join((data_path, entry))) nx_dict = {
nx_entry.attrs["NX_class"] = "NXentry" "@NX_class": "NXentry",
"monochromator": {
# store energy "@NX_class": "NXmonochromator",
nx_monochromator = nx_entry.require_group("monochromator") "energy": energy,
nx_monochromator.attrs["NX_class"] = "NXmonochromator" "energy@interpretation": "spctrum",
if overwrite and "energy" in nx_monochromator: "energy@NX_class": "NXdata",
del nx_monochromator["energy"] "energy@unit": "eV",
nx_monochromator["energy"] = energy },
nx_monochromator["energy"].attrs["interpretation"] = "spectrum" "absorbed_beam": {
nx_monochromator["energy"].attrs["NX_class"] = "NXdata" "@NX_class": "NXdetector",
nx_monochromator["energy"].attrs["unit"] = "eV" "data": mu,
"data@interpretation": "image",
# store absorbed beam "data@NX_class": "NXdata",
nx_absorbed_beam = nx_entry.require_group("absorbed_beam") },
nx_absorbed_beam.attrs["NX_class"] = "NXdetector" "data": {
if overwrite and "data" in nx_absorbed_beam: "@NX_class": "NXdata",
del nx_absorbed_beam["data"] ">energy": "../monochromator/energy",
nx_absorbed_beam["data"] = mu ">absorbed_beam": "../absorbed_beam/data",
nx_absorbed_beam["data"].attrs["interpretation"] = "image" },
nx_absorbed_beam["data"].attrs["NX_class"] = "NXdata" "start_time": start_time,
"title": title,
if sample: "definition": definition,
nx_sample = nx_entry.require_group("sample") }
nx_sample.attrs["NX_class"] = "NXsample" if overwrite:
if overwrite and "name" in nx_sample: mode = "w"
del nx_sample["name"] update_mode = "replace"
nx_sample["name"] = sample.name else:
mode = "a"
nx_data = nx_entry.require_group("data") update_mode = "add"
nx_data.attrs["NX_class"] = "NXdata" dicttonx(nx_dict, h5_file, h5path=h5path, mode=mode, update_mode=update_mode)
# create some link on data
if overwrite and "energy" in nx_data:
del nx_data["energy"]
nx_data["energy"] = h5py.SoftLink(nx_monochromator["energy"].name)
if overwrite and "absorbed_beam" in nx_data:
del nx_data["absorbed_beam"]
nx_data["absorbed_beam"] = h5py.SoftLink(nx_absorbed_beam["data"].name)
if start_time is not None:
if overwrite and "start_time" in nx_entry:
del nx_entry["start_time"]
nx_entry["start_time"] = start_time
if title is not None:
if overwrite and "title" in nx_entry:
del nx_entry["title"]
nx_entry["title"] = title
if definition is not None:
if overwrite and "definition" in nx_entry:
del nx_entry["definition"]
nx_entry["definition"] = definition
def write_spectrum_saving_pt(h5_file, entry, obj, overwrite=True): def write_spectrum_saving_pt(h5_file, entry, obj, overwrite=True):
......
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