-
Notifications
You must be signed in to change notification settings - Fork 2
/
client_example.py
166 lines (128 loc) · 5.03 KB
/
client_example.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
import datetime
import os
import signal
import socket
import sys
import time
import daq_client
pulseid = {
"alvra" : "SLAAR11-LTIM01-EVR0:RX-PULSEID",
"bernina" : "SLAAR21-LTIM01-EVR0:RX-PULSEID",
"cristallina" : "SLAAR21-LTIM01-EVR0:RX-PULSEID",
"maloja" : "SLAAR11-LTIM01-EVR0:RX-PULSEID",
"furka" : "SLAAR11-LTIM01-EVR0:RX-PULSEID"
}
def get_beamline():
ip2beamlines = {
"129.129.242": "alvra",
"129.129.243": "bernina",
"129.129.244": "cristallina",
"129.129.246": "maloja",
"129.129.247": "furka"
}
ip=socket.gethostbyname(socket.gethostname())
if ip[:11] in ip2beamlines:
return ip2beamlines[ip[:11]]
try:
import epics
epics_available=True
PV_pulseid=epics.PV(pulseid[get_beamline()])
except ImportError:
epics_available=False
def get_current_pulseid():
if not epics_available:
return get_fake_pulseid()
try:
p = int(PV_pulseid.get())
except Exception:
p = get_fake_pulseid()
return p
def get_fake_pulseid():
#2020-05-08 08:29:52.742737 : 11718049010
# 28.05.2020 - checked compared to "real" pulse-id: 2380 pulses difference
reference_date = datetime.datetime(2020, 5, 8, 8, 29, 52)
now = datetime.datetime.utcnow()
delta = (now-reference_date).total_seconds()*1000
return int(delta/10)+11718049010 + 2361
class BrokerClient:
def __init__(self, pgroup):
self.last_run = 0
self.pgroup = pgroup
self.rate_multiplicator = 1
self.channels_file = None
self.epics_file = None
self.detectors_file = None
self.scan_step_info_file = None
self.start_pulseid = None
beamline=get_beamline()
raw_directory = f"/sf/{beamline}/data/{self.pgroup}/raw/"
if not os.path.isdir(raw_directory):
raise NameError(f"{raw_directory} doesnt exist or accessible")
def configure(
self,
channels_file=None,
epics_file=None,
detectors_file=None,
scan_step_info_file=None,
rate_multiplicator=1
):
self.channels_file = channels_file
self.epics_file = epics_file
self.detectors_file = detectors_file
self.scan_step_info_file = scan_step_info_file
self.rate_multiplicator = rate_multiplicator
try:
beamline=get_beamline()
last_run_file = f"/sf/{beamline}/data/{self.pgroup}/raw/run_info/LAST_RUN"
if os.path.exists(last_run_file):
with open(last_run_file, "r") as run_file:
self.last_run = int(run_file.read())
except Exception:
pass
def start(self):
self.start_pulseid = get_current_pulseid()
def stop(self, stop_pulseid=None):
if self.start_pulseid is not None:
if stop_pulseid is None:
stop_pulseid = get_current_pulseid()
last_run_previous = self.last_run
self.last_run = daq_client.retrieve_data_from_buffer_files(pgroup=self.pgroup,
channels_file=self.channels_file, epics_file=self.epics_file,
detectors_file=self.detectors_file,
start_pulseid=self.start_pulseid, stop_pulseid=stop_pulseid,
rate_multiplicator=self.rate_multiplicator,
scan_step_info_file=self.scan_step_info_file)
if self.last_run is None:
self.last_run = last_run_previous
self.start_pulseid = None
else:
print("Run was not started to stop it")
def status(self):
if self.start_pulseid is not None:
return "running"
return None
def run(self, number_frames=1000):
self.start()
stop_pulseid = int(self.start_pulseid + number_frames*self.rate_multiplicator-1)
last_known_run = int(self.last_run) if self.last_run is not None else -1
def signal_handler(_sig, _frame):
current_pulseid = get_current_pulseid()
print("\nYou pressed Ctrl+C!")
print(f"what do you want me to do with already collected up to now frames (pulseids: {self.start_pulseid}-{current_pulseid})")
answer=input("[s]-save them into; any other key - discard : ")
if answer == "s":
self.stop(stop_pulseid=current_pulseid)
raise NameError("Ctrl-c is called")
signal.signal(signal.SIGINT, signal_handler)
while True:
current_pulseid = get_current_pulseid()
if current_pulseid >= stop_pulseid:
break
time.sleep(0.1)
progress = (current_pulseid-self.start_pulseid) / (stop_pulseid-self.start_pulseid)
block = int(round(20*progress))
text = "\r[{0}] {1}% Run: {2}".format( "#"*block + "-"*(20-block), int(progress*100), last_known_run+1)
sys.stdout.write(text)
sys.stdout.flush()
print()
self.stop(stop_pulseid=stop_pulseid)