From c10efeb9ccf326f4c686e1d6d0b39d86aa6cbfb1 Mon Sep 17 00:00:00 2001 From: Ivan Usov Date: Tue, 22 Jun 2021 11:23:38 +0200 Subject: [PATCH] Apply UB redefinition from .cami file For #31 --- pyzebra/app/panel_hdf_viewer.py | 12 ++++++++++-- pyzebra/h5.py | 25 +++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/pyzebra/app/panel_hdf_viewer.py b/pyzebra/app/panel_hdf_viewer.py index 2de7e62..d5ddc13 100644 --- a/pyzebra/app/panel_hdf_viewer.py +++ b/pyzebra/app/panel_hdf_viewer.py @@ -51,9 +51,11 @@ IMAGE_PLOT_H = int(IMAGE_H * 2) + 27 def create(): det_data = {} + cami_meta = {} roi_selection = {} def proposal_textinput_callback(_attr, _old, new): + nonlocal cami_meta proposal = new.strip() for zebra_proposals_path in pyzebra.ZEBRA_PROPOSALS_PATHS: proposal_path = os.path.join(zebra_proposals_path, proposal) @@ -69,13 +71,16 @@ def create(): file_list.append((os.path.join(proposal_path, file), file)) file_select.options = file_list + cami_meta = {} + proposal_textinput = TextInput(title="Proposal number:", width=210) proposal_textinput.on_change("value", proposal_textinput_callback) def upload_button_callback(_attr, _old, new): + nonlocal cami_meta with io.StringIO(base64.b64decode(new).decode()) as file: - h5meta_list = pyzebra.parse_h5meta(file) - file_list = h5meta_list["filelist"] + cami_meta = pyzebra.parse_h5meta(file) + file_list = cami_meta["filelist"] file_select.options = [(entry, os.path.basename(entry)) for entry in file_list] upload_div = Div(text="or upload .cami file:", margin=(5, 5, 0, 5)) @@ -182,6 +187,9 @@ def create(): det_data = pyzebra.read_detector_data(new[0]) + if cami_meta and "crystal" in cami_meta: + det_data["ub"] = cami_meta["crystal"]["UB"] + index_spinner.value = 0 index_spinner.high = det_data["data"].shape[0] - 1 index_slider.end = det_data["data"].shape[0] - 1 diff --git a/pyzebra/h5.py b/pyzebra/h5.py index 23d2cff..8620df8 100644 --- a/pyzebra/h5.py +++ b/pyzebra/h5.py @@ -1,6 +1,10 @@ import h5py +import numpy as np +META_MATRIX = ("UB") +META_STR = ("name") + def read_h5meta(filepath): """Open and parse content of a h5meta file. @@ -23,13 +27,30 @@ def parse_h5meta(file): line = line.strip() if line.startswith("#begin "): section = line[len("#begin ") :] - content[section] = [] + if section in ("detector parameters", "crystal"): + content[section] = {} + else: + content[section] = [] elif line.startswith("#end"): section = None elif section: - content[section].append(line) + if section in ("detector parameters", "crystal"): + if "=" in line: + variable, value = line.split("=", 1) + variable = variable.strip() + value = value.strip() + + if variable in META_STR: + content[section][variable] = value + elif variable in META_MATRIX: + ub_matrix = np.array(value.split(",")[:9], dtype=np.float).reshape(3, 3) + content[section][variable] = ub_matrix + else: # default is a single float number + content[section][variable] = float(value) + else: + content[section].append(line) return content