From ba3eab207456464e59a9e20a8d6cb4897012b125 Mon Sep 17 00:00:00 2001 From: Sven Augustin Date: Wed, 15 Sep 2021 22:32:06 +0200 Subject: [PATCH] read, use or compare reference undulator currently used by the machine --- devices/undulator.py | 54 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 47 insertions(+), 7 deletions(-) diff --git a/devices/undulator.py b/devices/undulator.py index f903411..5cef4b8 100644 --- a/devices/undulator.py +++ b/devices/undulator.py @@ -4,30 +4,51 @@ from epics import PV from logzero import logger as log -from slic.core.adjustable import Adjustable -from slic.core.adjustable import PVAdjustable +from slic.core.adjustable import Adjustable, PVAdjustable, PVEnumAdjustable from slic.core.scanner.scanbackend import wait_for_all #, stop_all UND_NAME_FMT = "SATUN{:02}-UIND030" N_UND_CHIC = 14 +N_UNDS = list(range(6, 22+1)) +N_UNDS.remove(N_UND_CHIC) + + class Undulators(Adjustable): + """ + for n_und_ref=None (default), the reference undulator currently used by the machine will be used + """ - def __init__(self, n_unds, n_und_ref, chic_fudge_offset=0, adjust_chic=True, scaled=True, ID="ATHOS_UNDULATORS", name="Athos Undulators", units="eV"): + def __init__(self, n_unds=N_UNDS, n_und_ref=None, chic_fudge_offset=0, adjust_chic=True, scaled=True, ID="ATHOS_UNDULATORS", name="Athos Undulators", units="eV"): super().__init__(ID, name=name, units=units) - self.n_unds = n_unds = list(n_unds) - self.n_und_ref = n_und_ref - if n_und_ref not in n_unds: - raise ValueError(f"the reference undulator ({n_und_ref}) is not in the list of active undulators: {n_unds}") + machine_n_und_ref = get_machine_n_und_ref() + + if n_und_ref is None: + if machine_n_und_ref is None: + raise ValueError(f"could not read reference undulator currently used by the machine, please specify n_und_ref") + n_und_ref = machine_n_und_ref + + if n_und_ref != machine_n_und_ref: + log.warning(f"the chosen reference undulator ({n_und_ref}) is not the reference undulator currently used by the machine ({machine_n_und_ref})") + + + n_unds = list(n_unds) if N_UND_CHIC in n_unds: log.warning(f"the CHIC ({N_UND_CHIC}) is in the list of active undulators: {n_unds}, and will be ignored/removed") n_unds.remove(N_UND_CHIC) + if n_und_ref not in n_unds: + raise ValueError(f"the reference undulator ({n_und_ref}) is not in the list of active undulators: {n_unds}") + + + self.n_unds = n_unds + self.n_und_ref = n_und_ref + self.und_names = und_names = [UND_NAME_FMT.format(n) for n in n_unds] self.und_name_cal = und_name_cal = UND_NAME_FMT.format(n_und_ref) @@ -43,6 +64,7 @@ class Undulators(Adjustable): self.scale = ScalerEK(a) + def set_target_value(self, value, hold=False): k = self.convert.K(value) if np.isnan(k): @@ -200,3 +222,21 @@ class CHIC(PVAdjustable): + + +def get_machine_n_und_ref(): + res = PVEnumAdjustable("SATUN:REF-UND").get() + if not res.startswith("SATUN"): + return None + n = len("SATUN") + res = res[n:] + try: + res = int(res) + except ValueError: + return None + return res + + + + +