From a00db1bc147fcd510df4fa3fa6c809c4b77a60b4 Mon Sep 17 00:00:00 2001 From: Roman Mankowsky Date: Tue, 16 Jun 2020 13:14:49 +0200 Subject: [PATCH] Added new daq module --- .vscode/settings.json | 5 + eco/.vscode/settings.json | 3 + eco/acquisition/daq_client.py | 158 ++++++++++++++++++ eco/acquisition/epics_data.py | 2 + eco/acquisition/scan.py | 31 ++++ eco/bernina/config.py | 42 +++-- eco/devices_general/.smaract.py.swo | Bin 0 -> 16384 bytes .../bernina_sample_environments.py | 11 +- eco/timing/event_timing.py | 4 +- eco/timing/event_timing_new.py | 18 +- eco/utilities/runtable.py | 49 +++--- eco/xoptics/reflaser_new.py | 2 +- 12 files changed, 286 insertions(+), 39 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 eco/.vscode/settings.json create mode 100644 eco/acquisition/daq_client.py create mode 100644 eco/devices_general/.smaract.py.swo diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..70697c9 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "python.linting.pylintEnabled": true, + "python.linting.enabled": true, + "python.pythonPath": "/sf/bernina/anaconda/bernina_envs/bernina37/bin/python" +} \ No newline at end of file diff --git a/eco/.vscode/settings.json b/eco/.vscode/settings.json new file mode 100644 index 0000000..de288e1 --- /dev/null +++ b/eco/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "python.formatting.provider": "black" +} \ No newline at end of file diff --git a/eco/acquisition/daq_client.py b/eco/acquisition/daq_client.py new file mode 100644 index 0000000..0225740 --- /dev/null +++ b/eco/acquisition/daq_client.py @@ -0,0 +1,158 @@ +import requests +from pathlib import Path +from time import sleep +from ..devices_general.detectors import PvDataStream + + +class Daq: + def __init__( + self, + broker_address="http://sf-daq-1:10002", + timeout=10, + pgroup=None, + pulse_id_adj=None, + event_master=None, + detectors_event_code=None, + instrument=None, + channels_JF=None, + channels_BS=None, + channels_BSCAM=None, + ): + self.channels = {} + if channels_JF: + self.channels["channels_JF"] = channels_JF + if channels_BS: + self.channels["channels_BS"] = channels_BS + if channels_BSCAM: + self.channels["channels_BSCAM"] = channels_BSCAM + self.broker_address = broker_address + self.timeout = timeout + self.pgroup = pgroup + if type(pulse_id_adj) is str: + self.pulse_id = PvDataStream(pulse_id_adj, name="pulse_id") + else: + self.pulse_id = pulse_id_adj + self.running = [] + self._event_master = event_master + self._detectors_event_code = detectors_event_code + + def acquire(self, Npulses, label=None, wait=True, **kwargs): + ix = self.start(label=label, **kwargs) + return self.stop(stop_id=ix, wait=wait) + + def start(self, label=None, **kwargs): + start_id = self.pulse_id.get_current_value() + acq_pars = { + "label": label, + "start_id": start_id, + } + acq_pars.update(kwargs) + self.running.append(acq_pars) + return self.running.index(acq_pars) + + def stop( + self, + stop_id=None, + acq_ix=None, + label=None, + wait=True, + wait_cycle_sleep=0.01, + **kwargs, + ): + if not stop_id: + stop_id = int(self.pulse_id.get_current_value()) + if not acq_ix: + acq_ix = -1 + + acq_pars = self.running.pop(acq_ix) + acq_pars["stop_id"] = stop_id + acq_pars.update(kwargs) + label = acq_pars.pop("label") + if wait: + while int(self.pulse_id.get_current_value()) < stop_id: + sleep(wait_cycle_sleep) + return self.retrieve(**acq_pars) + + def retrieve( + self, + *, + start_id, + stop_id, + directory_relative=None, + channels_CA=None, + channels_JF=None, + channels_BS=None, + channels_BSCAM=None, + pgroup=None, + pgroup_base_path="/sf/bernina/data/{:s}/raw", + filename_format="run_{:06d}", + ): + if not pgroup: + pgroup = self.pgroup + if not pgroup: + raise Exception("a pgroup needs to be defined") + if not directory_relative: + directory_relative = "" + directory_relative = Path(directory_relative) + directory_base = Path(pgroup_base_path.format(pgroup)) / directory_relative + files_extensions = [] + parameters = {"start_pulseid": start_id, "stop_pulseid": stop_id} + if channels_CA: + parameters["pv_list"] = channels_CA + files_extensions.append("CADUMP") + if channels_BS: + parameters["channels_list"] = channels_BS + files_extensions.append("BSREAD") + if channels_JF: + parameters["detectors"] = {tn: {} for tn in channels_JF} + for ch in channels_JF: + files_extensions.append(ch) + if channels_BSCAM: + parameters["camera_list"] = channels_BSCAM + files_extensions.append("BSREAD.CAMERAS") + if directory_relative: + parameters["directory_name"] = directory_relative.as_posix() + + parameters["pgroup"] = pgroup + + runno = validate_response( + requests.post( + f"{self.broker_address}/retrieve_from_buffers", + json=parameters, + timeout=self.timeout, + ).json() + ) + + filenames = [ + (directory_base / Path(filename_format.format(runno))).with_suffix( + f".{ext}.h5" + ) + for ext in files_extensions + ] + + return runno, filenames + + def get_detector_frequency(self): + return self._event_master.get_evtcode_frequency(self._detectors_event_code) + + +def validate_response(resp): + if resp.get("status") == "ok": + return int(resp.get("message")) + + message = resp.get("message", "Unknown error") + msg = "An error happened on the server:\n{}".format(message) + raise Exception(msg) + + +# parameters = { +# "pgroup":"p16584", +# "start_pulseid":12054777413-2000, +# "stop_pulseid":12054777413-1000, +# "channels_list":[ +# "SAR-CVME-TIFALL5:EvtSet" +# ], +# "run_number" +# } + +# r = requests.post(f'{broker_address}/retrieve_from_buffers',json=parameters, timeout=TIMEOUT_DAQ).json() diff --git a/eco/acquisition/epics_data.py b/eco/acquisition/epics_data.py index ae8af50..203d05e 100644 --- a/eco/acquisition/epics_data.py +++ b/eco/acquisition/epics_data.py @@ -16,8 +16,10 @@ class Epicstools: default_channel_list={"listname": []}, default_file_path="%s", elog=None, + name=None, channel_list=None, ): + self.name = name self._default_file_path = default_file_path self._default_channel_list = default_channel_list self._elog = elog diff --git a/eco/acquisition/scan.py b/eco/acquisition/scan.py index 9877fc6..f4737e7 100755 --- a/eco/acquisition/scan.py +++ b/eco/acquisition/scan.py @@ -23,8 +23,10 @@ class Scan: callbackStartStep=None, checker_sleep_time=0.2, return_at_end="question", + run_table=None, ): self.Nsteps = len(values) + self._run_table = run_table self.pulses_per_step = Npulses self.adjustables = adjustables self.values_todo = values @@ -57,6 +59,27 @@ class Scan: tv = adj.get_current_value() self.initial_values.append(adj.get_current_value()) print("Initial value of %s : %g" % (adj.name, tv)) + + if self._run_table: + runname = os.path.basename(fina).split('.')[0] + runno = int(runname.split('run')[1].split('_')[0]) + metadata = { + "type": "scan", + "name": runname.split('_', 1)[1], + + } + for n, adj in enumerate(self.adjustables): + metadata.update({ + f'scan_motor_{n}': adj.name, + f'from_motor_{n}': self.values_todo[0][n], + f'to_motor_{n}': self.values_todo[-1][n] + }) + metadata.update({ + "steps": len(self.values_todo), + "pulses_per_step": Npulses, + "counters": [daq.name for daq in counterCallers], + }) + run_table.append_run(runno, metadata=metadata) def get_filename(self, stepNo, Ndigits=4): fina = os.path.join(self.basepath, Path(self.fina).stem) @@ -181,7 +204,9 @@ class Scans: default_counters=[], checker=None, scan_directories=False, + run_table=None, ): + self._run_table=run_table self.data_base_dir = data_base_dir scan_info_dir = Path(scan_info_dir) if not scan_info_dir.exists(): @@ -242,6 +267,7 @@ class Scans: scan_info_dir=self.scan_info_dir, checker=self.checker, scan_directories=self._scan_directories, + run_table=self._run_table, ) if start_immediately: s.scanAll(step_info=step_info) @@ -275,6 +301,7 @@ class Scans: scan_info_dir=self.scan_info_dir, checker=self.checker, scan_directories=self._scan_directories, + run_table=self._run_table, ) if start_immediately: s.scanAll(step_info=step_info) @@ -309,6 +336,7 @@ class Scans: checker=self.checker, scan_directories=self._scan_directories, return_at_end=return_at_end, + run_table=self._run_table, ) if start_immediately: s.scanAll(step_info=step_info) @@ -344,6 +372,7 @@ class Scans: checker=self.checker, scan_directories=self._scan_directories, return_at_end=return_at_end, + run_table=self._run_table, ) if start_immediately: s.scanAll(step_info=step_info) @@ -382,6 +411,7 @@ class Scans: checker=self.checker, scan_directories=self._scan_directories, return_at_end=return_at_end, + run_table=self._run_table, ) if start_immediately: s.scanAll(step_info=step_info) @@ -419,6 +449,7 @@ class Scans: checker=self.checker, scan_directories=self._scan_directories, return_at_end=return_at_end, + run_table=self._run_table, ) if start_immediately: s.scanAll(step_info=step_info) diff --git a/eco/bernina/config.py b/eco/bernina/config.py index bfa8073..762bb4e 100755 --- a/eco/bernina/config.py +++ b/eco/bernina/config.py @@ -32,6 +32,14 @@ components = [ # 'args' : ['all','the','requires','args'], # 'kwargs': {} # } + { + "args": [config["pgroup"]], + "name": "run_table", + "desc": "run table for data acquisition", + "type": "eco.utilities.runtable:Run_Table", + "kwargs": {"devices": None, "alias_namespace": None}, + "lazy": False, + }, { "type": "eco.utilities.config:append_to_path", "args": config["path_exp"], @@ -467,6 +475,7 @@ components = [ "default_counters": [Component("daq")], "checker": Component("checker"), "scan_directories": True, + "run_table": Component("run_table"), }, }, { @@ -480,6 +489,7 @@ components = [ "default_counters": [Component("epics_daq")], "checker": Component("checker"), "scan_directories": True, + "run_table": Component("run_table"), }, }, { @@ -643,9 +653,15 @@ components = [ "z_und": 142, "desc": "electro optic sampling stages", "type": "eco.endstations.bernina_sample_environments:electro_optic_sampling", - "kwargs": {"Id": "SARES23", - 'pgroup':config['pgroup'], - 'diode_channels': {'d1': 'SARES20-LSCP9-FNS:CH1:VAL_GET','d2': 'SARES20-LSCP9-FNS:CH2:VAL_GET','diff': 'SARES20-LSCP9-FNS:CH3:VAL_GET'}}, + "kwargs": { + "Id": "SARES23", + "pgroup": config["pgroup"], + "diode_channels": { + "d1": "SARES20-LSCP9-FNS:CH1:VAL_GET", + "d2": "SARES20-LSCP9-FNS:CH2:VAL_GET", + "diff": "SARES20-LSCP9-FNS:CH3:VAL_GET", + }, + }, "lazy": True, }, { @@ -658,12 +674,18 @@ components = [ "lazy": True, }, { - "args": [config['pgroup']], - "name": "run_table", - "desc": "run table for data acquisition", - "type": "eco.utilities.runtable:Run_Table", - "kwargs": {'devices':None, 'alias_namespace': None}, - "lazy": False, + "args": [], + "name": "daq_new", + "desc": "server based acquisition new JF stuff", + "type": "eco.acquisition.daq_client:Daq", + "kwargs": { + "instrument": "bernina", + "pgroup": config["pgroup"], + "channels_JF": config["jf_channels"], + "pulse_id_adj": "SLAAR21-LTIM01-EVR0:RX-PULSEID", + "event_master": Component("event_master"), + "detectors_event_code": 50, + }, }, ] @@ -672,5 +694,3 @@ try: print("Did append additional components!") except: print("Could not append components from config.") - - diff --git a/eco/devices_general/.smaract.py.swo b/eco/devices_general/.smaract.py.swo new file mode 100644 index 0000000000000000000000000000000000000000..ecdd4dde4e7503c8ae4d356ff6c08b1848f02c99 GIT binary patch literal 16384 zcmeHOUx*|}8Sf;Q#Hfi9(T6Bf>GQnayV>5oKgr3O0XMsQcS~;F<#uj^?5$1Dbj?iP z^>p`j)y(d4IRip`P@z0KK;3r)4EqZfq@HO%FV`V_6-)HQnS{ z9L7FLZ+Zzoe7q#_s11&t(Nsy;Ag9|3LyeuH-VHSi;#2OI^cEpG?@!;6)! zgDh;bIFU^7sO@?}`m!%rz0PLmtdiP@*z0j2{0?VzW-{a{VzX>!>!ucvQz`b60e1#| z)aNx8cH$)CUKI91De4aS)avrq<*6Fm_Cr~3WPLtWSPXa@Vn6QiOdFV*vkIYY+iX6# z)JJWa5oh?^WWZw?CNTs`iE;}rJ?Up1+>{3nc~oP@n{E;X_0xVN;F)qmm#;}V$QBBd ze#Ckl->?LN`EkI~EJ=ALLoQf5$(SESWlxgkCEk)^=qbZBtQ7sCYd#8n;rKBX?Z+{X z>b7gUmy$4cG{MV3jrr{@P-7J2Zp!QS!n{3dOp)x9HP=s59tV!43cw%b9ZK-qER5kN zbi8)Rqd>T!M#JST>jhaj;4av8?4>goN`5I_$_`b! zjh5o^DCs~#w=s#_9zd{;FM{S<(pe7JQD)CI9^V+Z?NQrJYMxy^w>Y6HWRgy*t~XZK zCRNc<(u68F=EC|?!&_TjpUhy!65AXO1XX!6XoRGSjvH&tH!e)tilW}6?zwX-6NPs7 zQarndV%FS)&6U;0`9~)SRc$$0{^I%Njiqr9&@Oeh9Oy1y>O#{w^ zY*T8D+1;ea14=prPS38cEG^pCYDG7oK2iT@67yjab)c~%jEy1((XD^ci1)k@UDWd& zs}=b|BBmxXQi|+ox?#WXz~aS*Vyb7iAtAhML!6JvP7NLHbsEwwPGQOnUtxO#0&m?M3)iuMugwZFD~^=s)4k z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