-
Notifications
You must be signed in to change notification settings - Fork 19
/
xfel_interface.py
277 lines (235 loc) · 9.59 KB
/
xfel_interface.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
"""
XFEL machine interface
S.Tomin, 2017
"""
from __future__ import absolute_import, print_function
try:
# in server "doocsdev12" set environment
# $ export PYTHONPATH=/home/ttflinac/user/python-2.7/Debian/
import pydoocs
except:
pass # Show message on Constructor if we try to use it.
import os
import sys
import numpy as np
import subprocess
import base64
from mint.opt_objects import MachineInterface, Device
from collections import OrderedDict
class AlarmDevice(Device):
"""
Devices for getting information about Machine status
"""
def __init__(self, eid=None):
super(AlarmDevice, self).__init__(eid=eid)
class XFELMachineInterface(MachineInterface):
"""
Machine Interface for European XFEL
"""
name = 'XFELMachineInterface'
def __init__(self, args=None):
super(XFELMachineInterface, self).__init__(args)
if 'pydoocs' not in sys.modules:
print('error importing doocs library')
self.logbook_name = "xfellog"
path2root = os.path.abspath(os.path.join(__file__ , "../../../.."))
self.config_dir = os.path.join(path2root, "config_optim_new")
def get_value(self, channel):
"""
Getter function for XFEL.
:param channel: (str) String of the devices name used in doocs
:return: Data from pydoocs.read(), variable data type depending on channel
"""
val = pydoocs.read(channel)
return val["data"]
def set_value(self, channel, val):
"""
Method to set value to a channel
:param channel: (str) String of the devices name used in doocs
:param val: value
:return: None
"""
pydoocs.write(channel, float(val))
return
def get_charge(self):
return self.get_value("XFEL.DIAG/CHARGE.ML/TORA.25.I1/CHARGE.SA1")
def send_to_logbook(self, *args, **kwargs):
"""
Send information to the electronic logbook.
:param args:
Values sent to the method without keywork
:param kwargs:
Dictionary with key value pairs representing all the metadata
that is available for the entry.
:return: bool
True when the entry was successfully generated, False otherwise.
"""
author = kwargs.get('author', '')
title = kwargs.get('title', '')
severity = kwargs.get('severity', '')
text = kwargs.get('text', '')
image = kwargs.get('image', None)
elog = self.logbook_name
# The DOOCS elog expects an XML string in a particular format. This string
# is beeing generated in the following as an initial list of strings.
succeded = True # indicator for a completely successful job
# list beginning
elogXMLStringList = ['<?xml version="1.0" encoding="ISO-8859-1"?>', '<entry>']
# author information
elogXMLStringList.append('<author>')
elogXMLStringList.append(author)
elogXMLStringList.append('</author>')
# title information
elogXMLStringList.append('<title>')
elogXMLStringList.append(title)
elogXMLStringList.append('</title>')
# severity information
elogXMLStringList.append('<severity>')
elogXMLStringList.append(severity)
elogXMLStringList.append('</severity>')
# text information
elogXMLStringList.append('<text>')
elogXMLStringList.append(text)
elogXMLStringList.append('</text>')
# image information
if image:
try:
encodedImage = base64.b64encode(image)
elogXMLStringList.append('<image>')
elogXMLStringList.append(encodedImage.decode())
elogXMLStringList.append('</image>')
except: # make elog entry anyway, but return error (succeded = False)
succeded = False
# list end
elogXMLStringList.append('</entry>')
# join list to the final string
elogXMLString = '\n'.join(elogXMLStringList)
# open printer process
try:
lpr = subprocess.Popen(['/usr/bin/lp', '-o', 'raw', '-d', elog],
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
# send printer job
lpr.communicate(elogXMLString.encode('utf-8'))
except:
succeded = False
return succeded
def get_obj_function_module(self):
from mint.xfel import xfel_obj_function
return xfel_obj_function
def get_preset_settings(self):
"""
Return the preset settings to be assembled as Push Buttons at the user interface for quick load of settings.
:return: (dict) Dictionary with Key being the group name and as value an array of dictionaries following the
format:
{"display": "Text of the PushButton", "filename": "my_file.json"}
"""
presets = {
"SASE Optimization": [
{"display": "1. Launch orbit SASE1", "filename": "sase1_1.json"},
{"display": "2. Match Quads SASE1", "filename": "sase1_2.json"},
],
"Dispersion Minimization": [
{"display": "1. I1 Horizontal", "filename": "disp_1.json"},
{"display": "2. I1 Vertical", "filename": "disp_2.json"},
]
}
return presets
def get_quick_add_devices(self):
"""
Return a dictionary with:
{
"QUADS1" : ["...", "..."],
"QUADS2": ["...", "..."]
}
That is converted into a combobox which allow users to easily populate the devices list
:return: dict
"""
devs = OrderedDict([
("Launch SASE1", ["XFEL.MAGNETS/MAGNET.ML/CFX.2162.T2/CURRENT.SP",
"XFEL.MAGNETS/MAGNET.ML/CFX.2219.T2/CURRENT.SP",
"XFEL.MAGNETS/MAGNET.ML/CFY.2177.T2/CURRENT.SP",
"XFEL.MAGNETS/MAGNET.ML/CFY.2207.T2/CURRENT.SP"]),
("Match Quads SASE1", ["XFEL.MAGNETS/MAGNET.ML/CFX.2162.T2/CURRENT.SP",
"XFEL.MAGNETS/MAGNET.ML/CFX.2219.T2/CURRENT.SP",
"XFEL.MAGNETS/MAGNET.ML/CFY.2177.T2/CURRENT.SP",
"XFEL.MAGNETS/MAGNET.ML/CFY.2207.T2/CURRENT.SP"]),
("I1 Hor. Disp.", ["XFEL.MAGNETS/MAGNET.ML/CBB.62.I1D/KICK_MRAD.SP",
"XFEL.MAGNETS/MAGNET.ML/CIX.90.I1/KICK_MRAD.SP",
"XFEL.MAGNETS/MAGNET.ML/CIX.95.I1/KICK_MRAD.SP",
"XFEL.MAGNETS/MAGNET.ML/CIX.65.I1/KICK_MRAD.SP",
"XFEL.MAGNETS/MAGNET.ML/CIX.51.I1/KICK_MRAD.SP",
"XFEL.MAGNETS/MAGNET.ML/CIX.102.I1/KICK_MRAD.SP",
"XFEL.MAGNETS/MAGNET.ML/CX.39.I1/KICK_MRAD.SP",
"XFEL.MAGNETS/MAGNET.ML/BL.50I.I1/KICK_DEG.SP",
"XFEL.MAGNETS/MAGNET.ML/BL.50II.I1/KICK_DEG.SP"]),
("I1 Ver. Disp.", ["XFEL.MAGNETS/MAGNET.ML/CIY.92.I1/KICK_MRAD.SP",
"XFEL.MAGNETS/MAGNET.ML/CIY.72.I1/KICK_MRAD.SP",
"XFEL.MAGNETS/MAGNET.ML/CY.39.I1/KICK_MRAD.SP"])
])
return devs
# test interface
class TestMachineInterface(XFELMachineInterface):
"""
Machine interface for testing
"""
name = 'TestMachineInterface'
def __init__(self, args):
super(TestMachineInterface, self).__init__(args)
self.data = 1.
pass
def get_alarms(self):
return np.random.rand(4)#0.0, 0.0, 0.0, 0.0]
def get_value(self, device_name):
"""
Testing getter function for XFEL.
:param channel: (str) String of the devices name used in doocs
:return: Data from pydoocs.read(), variable data type depending on channel
"""
#if "QUAD" in device_name:
# return 0
return np.random.rand(1)[0]-0.5 #self.data
def set_value(self, device_name, val):
"""
Testing Method to set value to a channel
:param channel: (str) String of the devices name used in doocs
:param val: value
:return: None
"""
#print("set:", device_name, "-->", val)
self.data += np.sqrt(val**2)
return 0.0
def get_bpms_xy(self, bpms):
"""
Testing method for getting bmps data
:param bpms: list of string. BPMs names
:return: X, Y - two arrays in [m]
"""
X = np.zeros(len(bpms))
Y = np.zeros(len(bpms))
return X, Y
def get_charge(self):
return 0
@staticmethod
def send_to_logbook(*args, **kwargs):
"""
Send information to the electronic logbook.
:param args:
Values sent to the method without keywork
:param kwargs:
Dictionary with key value pairs representing all the metadata
that is available for the entry.
:return: bool
True when the entry was successfully generated, False otherwise.
"""
author = kwargs.get('author', '')
title = kwargs.get('title', '')
severity = kwargs.get('severity', '')
text = kwargs.get('text', '')
elog = kwargs.get('elog', '')
image = kwargs.get('image', None)
# TODO: @sergey.tomin Figure out what to do for logbook at the TestMachineInterface
print('Send to Logbook not implemented for TestMachineInterface.')
return True
def get_obj_function_module(self):
from mint.xfel import xfel_obj_function
return xfel_obj_function