75 lines
2.4 KiB
Python
75 lines
2.4 KiB
Python
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())
|