Source code for ewoksid13.tasks.edf
from ewokscore import Task
from fabio.edfimage import EdfFrame
import h5py
import numpy as np
[docs]def convert_to_edf(
profile_axis: np.ndarray,
profile_stack: np.ndarray,
output_filename: str,
) -> None:
"""Save an n-D stack of 1D profiles as a 2D stack of 1D profiles in EDF format."""
if profile_axis.ndim != 1:
raise ValueError("Expected profile_axis_data to have 1 dimension.")
if profile_stack.ndim < 2:
raise ValueError("Expected profile_data to have at least 2 dimensions.")
if profile_stack.shape[-1] != profile_axis.size:
raise ValueError(
"Expected profile_axis_data to have {} values.".format(
profile_stack.shape[-1]
)
)
last_axis = profile_stack.ndim - 1
if profile_stack.ndim > 2:
s = profile_stack.shape
profile_stack = profile_stack.reshape((np.product(s[:last_axis]), s[last_axis]))
profile_stack = np.vstack((profile_axis, profile_stack))
# https://github.com/silx-kit/fabio/issues/464
# edf = EdfImage(data=profile_stack, header={"xrdua_1d": True})
# edf.write(output_filename)
frame = EdfFrame(data=profile_stack, header={"xrdua_1d": True})
frame._index = 0
with open(output_filename, mode="wb") as outfile:
outfile.write(frame.get_edf_block())
[docs]class StackToEdf(
Task, input_names=["nxdata_url"], optional_input_names=["output_filename"]
):
"""Convert a pyFAI diffmap to an edf stack"""
[docs] def run(self):
nxdata_url: str = self.inputs.nxdata_url
input_filename, h5_path = nxdata_url.split("::")
output_filename: str = self.get_input_value(
"output_filename", input_filename.replace(".h5", ".edf")
)
with h5py.File(input_filename, "r") as input_file:
if "q" in input_file[h5_path]:
x_axis = "q"
else:
x_axis = "2th"
convert_to_edf(
input_file[f"{h5_path}/{x_axis}"][()],
input_file[f"{h5_path}/intensity"][()],
output_filename,
)