From 74dd72f5b086c4172e7698090be9a374f75d96e2 Mon Sep 17 00:00:00 2001 From: e20639 Date: Wed, 13 Sep 2023 17:15:05 +0200 Subject: [PATCH] fix: add saxs_params for user scripts with new layout of motor controller --- bec_plugins/utils/saxs_params_start_stop.py | 74 +++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 bec_plugins/utils/saxs_params_start_stop.py diff --git a/bec_plugins/utils/saxs_params_start_stop.py b/bec_plugins/utils/saxs_params_start_stop.py new file mode 100644 index 0000000..d9c40e0 --- /dev/null +++ b/bec_plugins/utils/saxs_params_start_stop.py @@ -0,0 +1,74 @@ +import csv +from collections import defaultdict +import os +from typing import Callable +import numpy as np + + +class ScanItem: + def __init__(self, offset_xy: Callable) -> None: + self.entry = None + self.offset_xy = offset_xy + + def to_scan_params(self) -> dict: + scan_params = { + "start_x": float(self.entry["X [start]"]) + self.offset_xy()[0], + "start_y": float(self.entry["Y [start]"]) + self.offset_xy()[1], + "end_x": float(self.entry["X [end]"]) + self.offset_xy()[0], + "end_y": float(self.entry["Y [end]"]) + self.offset_xy()[1], + "interval_x": int( + np.round( + np.abs(float(self.entry["X [start]"]) - float(self.entry["X [end]"])) + / (float(self.entry["step_x [mu]"]) * 1e-3) + ) + ), + "interval_y": int( + np.round( + np.abs(float(self.entry["Y [start]"]) - float(self.entry["Y [end]"])) + / (float(self.entry["step_y [mu]"]) * 1e-3) + ) + ), + "exp_time": float(self.entry["exp_time [s]"]), + "readout_time": 3e-3, + "md": {"sample_name": self.entry["sample name"]}, + # "tilt": self.entry["tilt [deg]"] + } + if scan_params["interval_x"] < 1 or scan_params["interval_x"] < 1: + raise ValueError("Bugger off...") + return scan_params + + +class SAXSParams: + def __init__(self, offset: Callable): + self.offset_xy = offset + self.data = defaultdict(lambda: ScanItem(offset)) + + def load_from_csv(self, file_path: str) -> None: + """ + Load the acquisition parameter from a csv file. + """ + + if not os.path.exists(file_path): + raise FileNotFoundError( + f"The specified CSV file could not be found. Please check that the given path is correct: {file_path}." + ) + + with open(os.path.expanduser(file_path), "r") as file: + csv_reader = csv.DictReader(file) + for row in csv_reader: + self.data[row["sample name"]].entry = row + + + +if __name__ == "__main__": + from pprint import pprint + + INPUT_FILE = "/sls/X12SA/data/e21206/Data10/software/test_script.csv" + + def my_offset(): + return [0, 0] + + params = SAXSParams(my_offset) + params.load_from_csv(INPUT_FILE) + for key in params.data: + pprint(params.data[key].to_scan_params())