-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdaq_client.py
189 lines (145 loc) · 5.97 KB
/
daq_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import argparse
import json
import requests
broker_address = "http://sf-daq:10002"
TIMEOUT_DAQ = 10
def run():
parser = argparse.ArgumentParser(description="simple daq client example")
parser.add_argument("-p", "--pgroup", help="pgroup, example p12345", default="p18493")
parser.add_argument("-c", "--channels_file", help="TXT file with list channels", default=None)
parser.add_argument("-e", "--epics_file", help="TXT file with list of epics channels to save", default=None)
parser.add_argument("-f", "--file_detectors", help="JSON file with the detector list", default=None)
parser.add_argument("-r", "--rate_multiplicator", type=int, help="rate multiplicator (1(default): 100Hz, 2: 50Hz,)", default=1)
parser.add_argument("-s", "--scan_step_file", help="JSON file with the scan step information", default=None)
parser.add_argument("--start_pulseid", type=int, help="start pulseid", default=None)
parser.add_argument("--stop_pulseid", type=int, help="stop pulseid", default=None)
args = parser.parse_args()
retrieve_data_from_buffer_files(
pgroup=args.pgroup,
channels_file=args.channels_file,
epics_file=args.epics_file,
detectors_file=args.file_detectors,
start_pulseid=args.start_pulseid,
stop_pulseid=args.stop_pulseid,
rate_multiplicator=args.rate_multiplicator,
scan_step_info_file=args.scan_step_file
)
def retrieve_data_from_buffer_files(
pgroup=None,
channels_file=None,
epics_file=None,
detectors_file=None,
start_pulseid=None,
stop_pulseid=None,
rate_multiplicator=1,
scan_step_info_file=None
):
camera_channels = []
bsread_channels = []
channels = []
if channels_file is not None:
with open(channels_file) as input_file:
file_lines = input_file.readlines()
channels = [channel.strip() for channel in file_lines
if not channel.strip().startswith("#") and channel.strip()]
for channel in channels:
if channel.endswith(":FPICTURE"):
camera_channels.append(channel)
else:
bsread_channels.append(channel)
epics_channels = []
if epics_file is not None:
with open(epics_file) as input_file:
file_lines = input_file.readlines()
epics_channels = [channel.strip() for channel in file_lines
if not channel.strip().startswith("#") and channel.strip()]
detectors = None
if detectors_file is not None:
try:
with open(detectors_file) as json_file:
detectors = json.load(json_file)
except Exception as e:
print(f"Cannot read provided detector file, may be not json? due to {e}")
return None
scan_step_info = None
if scan_step_info_file is not None:
try:
with open(scan_step_info_file) as json_file:
scan_step_info = json.load(json_file)
except Exception as e:
print(f"Cannot read provided scan step info file, may be not json? due to {e}")
return None
run_number = retrieve_data_from_buffer(
pgroup=pgroup,
camera_channels=camera_channels,
bsread_channels=bsread_channels,
epics_channels=epics_channels,
detectors=detectors,
start_pulseid=start_pulseid,
stop_pulseid=stop_pulseid,
rate_multiplicator=rate_multiplicator,
scan_step_info=scan_step_info
)
return run_number
def retrieve_data_from_buffer(
pgroup=None,
camera_channels=None,
bsread_channels=None,
epics_channels=None,
detectors=None,
start_pulseid=None,
stop_pulseid=None,
rate_multiplicator=1,
scan_step_info=None
):
camera_channels = camera_channels or []
bsread_channels = bsread_channels or []
epics_channels = epics_channels or []
if pgroup is None:
raise NameError("Provide pgroup")
if start_pulseid is None or stop_pulseid is None:
raise NameError("Provide stop/start pulseid")
parameters = {}
parameters["pgroup"] = pgroup
parameters["start_pulseid"] = start_pulseid
parameters["stop_pulseid"] = stop_pulseid
parameters["rate_multiplicator"] = rate_multiplicator
if len(bsread_channels) > 0:
parameters["channels_list"] = bsread_channels
if len(camera_channels) > 0:
parameters["camera_list"] = camera_channels
if len(epics_channels) > 0:
parameters["pv_list"] = epics_channels
if detectors is not None:
parameters["detectors"] = detectors
if scan_step_info is not None:
parameters["scan_info"] = scan_step_info
try:
r = requests.post(f"{broker_address}/retrieve_from_buffers",json=parameters, timeout=TIMEOUT_DAQ)
except Exception as e:
raise NameError("Cannot connect to daq") from e
run_number = None
response = r.json()
if "status" in response:
if response["status"] == "ok":
message = response.get("message", None)
run_number = response.get("run_number", None)
if run_number is not None:
run_number = int(run_number)
run_number_print = f"{run_number:04}"
else:
run_number_print = None
acq_number = response.get("acquisition_number", None)
unq_acq_number = response.get("unique_acquisition_number", None)
files_daq = response.get("files", [])
print(f"success: {message=} {run_number=} {acq_number=} {unq_acq_number=}")
print(f" these files to expect in raw/{pgroup}/run{run_number_print}/data/ directory : {files_daq}")
else:
message = response.get("message", None)
print(f" Error, reason : {message=}")
print(f" whole response : {response=}")
else:
print("Bad response from request")
return run_number
if __name__ == "__main__":
run()