-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheseb.py
More file actions
executable file
·421 lines (372 loc) · 15 KB
/
heseb.py
File metadata and controls
executable file
·421 lines (372 loc) · 15 KB
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#!/usr/bin/python3
import config
import csv
import datetime
import decimal
import json
import os
import re
import sys
import time
import itertools
from common import Common
from detectors.ficus import FICUS
from detectors.ketek import KETEK
from detectors.xflash import XFLASH
from detectors.ic import IC
from detectors.keithley_i0 import KEITHLEY_I0
from detectors.keithley_itrans import KEITHLEY_ITRANS
from SEDSS.SEDSupplements import CLIMessage
from SEDSS.SEDSupport import readFile, dataTransfer, timeModule
from SEDSS.SEDTmuxSession import tmuxSession
# from SEDSS.SEDFileManager import path
# from SEDSS.SEDTransfer import SEDTransfer
import threading
from xdiWriter import XDIWriter
import threading
import log
import shutil
import signal
import subprocess
import math
try:
import PyQt5
import epics
import numpy as np
except ImportError as e:
print("Please make sure the following packages are installed:")
print("PyQt5, epics, numpy")
class HESEB:
def __init__(self,cfg, testingMode = "No"):
#
#epics.PV("SCAN:STOP").put(0)
log.setup_custom_logger("./SED_Scantool.log")
log.info("Start scanning tool")
self.loadPVS("HESEB")
self.PVs["SCAN:Stop"].put(0) # disable stop function
self.PVs["SCAN:pause"].put(0) # flush scan pause pv
self.PVs["Calibration:Energy"].put(1) # disable I0 vs time plotting
self.PVs["I0:TRIGGER"].put(1) # disable I0 vs time plotting
self.PVs["It:TRIGGER"].put(1) # disable It vs time plotting
# self.PVs["Voltage:Validation"].put(0) # enable voltage source
epics.PV("VOLTAGE:VALIDATION").put(0) # enable voltage source
self.KeithelyI0PV = readFile("pvlist/KEITHLEY_I0.json").readJSON()
self.voltageSourcePARAM = []
# get the values of voltage source parameters before reset
self.voltageSourcePARAM.append(epics.PV(self.KeithelyI0PV["PV"]["voltageSourceEnable"]["pvname"]).get())
self.voltageSourcePARAM.append(epics.PV(self.KeithelyI0PV["PV"]["voltageSourceRange"]["pvname"]).get())
self.voltageSourcePARAM.append(epics.PV(self.KeithelyI0PV["PV"]["voltageSourceCurrentLimit"]["pvname"]).get())
self.voltageSourcePARAM.append(epics.PV(self.KeithelyI0PV["PV"]["voltageSourceVoltageRBV"]["pvname"]).get())
self.paths = Common.loadjson("configrations/paths.json")
self.cfg = cfg
self.scanLimites = readFile("configrations/limites.json").readJSON()
#log.info("Experiment configurations: ({})".format(json.dumps(self.cfg, indent=2, sort_keys=True)))
log.info("Experiment scan limites: ({})".format(json.dumps(self.scanLimites, indent=2, sort_keys=True)))
CLIMessage(" Confegrations to be implemented: {}".format(self.cfg), "M")
self.detChosen = None
self.userinfo = Common.loadjson("configrations/userinfo.json")
self.initPaths()
self.initPGM()
self.initDetectors()
# Set ^C interrupt to abort the scan
signal.signal(signal.SIGINT, self.signal_handler)
if testingMode == "No":
log.info("Testing mode: No")
self.runPauseMonitor()
else:
log.info("Testing mode: Yes")
self.tmuxSessionToKill = ['voltageSourceValidation', 'I0_startAcquire']
tmuxSession(self.tmuxSessionToKill).kill()
subprocess.Popen("./voltageSourceValidation.sh")
self.startScan()
def runPauseMonitor(self):
log.info("start pause trigger monitor")
PauseMonitorThread = threading.Thread(target=self.pauseTrigger, args=(), daemon=True)
PauseMonitorThread.start()
def loadPVS(self,name):
log.info("load PVs")
JsonPVlist = Common.loadjson("pvlist/{}.json".format(name))
self.PVs = {}
self.motors = {}
DisconnectedPvs = []
for entry,pvname in JsonPVlist["PV"].items():
pvname=pvname["pvname"]
PVobj = epics.PV(pvname)
if PVobj.get() is None:
CLIMessage("{} : is not connected".format(pvname), "E")
DisconnectedPvs.append("{}\n".format(pvname))
else:
CLIMessage("{} : is connected".format(pvname), "I")
self.PVs[entry] = PVobj
for entry,mtrname in JsonPVlist["Motors"].items():
pvname=mtrname["pvname"]
MTRobj = epics.Motor(pvname)
if MTRobj is None:
CLIMessage("{} : is not connected".format(pvname), "E")
DisconnectedPvs.append("{}\n".format(pvname))
else:
CLIMessage("{} : is connected".format(pvname), "I")
self.motors[entry] = MTRobj
if len(DisconnectedPvs):
log.error("Disconnected PVs: {}".format(DisconnectedPvs))
Common.show_message(PyQt5.QtWidgets.QMessageBox.Critical,"The following PVs are disconnected:\n {}".format(" ".join(DisconnectedPvs)),"scan tool" ,PyQt5.QtWidgets.QMessageBox.Ok)
sys.exit()
def initPaths(self):
log.info("Paths initialization")
self.creationTime = str(time.strftime("%Y%m%dT%H%M%S"))
self.BasePath = "{}/{}-{}".format(self.paths["local_data_path"],self.cfg["DataFileName"],self.creationTime)
self.cfgfilepath = "{}/{}_config_{}.cfg".format(self.BasePath,self.cfg["DataFileName"],self.creationTime)
self.localDataPath = "{}".format(self.BasePath)
if not os.path.exists(self.BasePath):
log.info("Create base directory: {}".format(self.BasePath))
os.makedirs(self.BasePath)
with open(self.cfgfilepath,'w') as cfgfile:
json.dump(self.cfg,cfgfile)
cfgfile.close()
self.dataFileName = "{}-{}.dat".format(self.cfg["DataFileName"], str(datetime.datetime.now()))
self.dataFileFullPath = "{}/{}".format(self.localDataPath, self.dataFileName)
self.expStartTimeDF = str(time.strftime("%Y-%m-%dT%H:%M:%S")) # to be added to xdi file as a content
"""
Writing the data path into file to be available to the plotting tool.
"""
with open("./tmp/SEDPath.txt",'w') as SEDPath:
SEDPath.write(self.localDataPath)
SEDPath.close()
if not os.path.exists(self.localDataPath):
os.makedirs(self.localDataPath)
def drange(self,start,stop,step,prec=10):
log.info("Calculating energy points")
decimal.getcontext().prec = prec
points = []
r= decimal.Decimal(start)
step = decimal.Decimal(step)
while r <=stop:
points.append(float(r))
r += step
return points
def generateScanPoints(self):
log.info("Calculating samples, scans and Intervals")
Samples = range(1,self.cfg["Nsamples"]+1)
Scans = range(1,self.cfg["Nscans"]+1)
Intervals = range(1,self.cfg["NIntervals"]+1)
return itertools.product(Samples,Scans,Intervals)
def initPGM(self):
log.info("PGM initialization")
self.PVs["SCAN:pause"].put(0, wait=True) # set pause flag to Fales
self.motors["PGM:Grating"].put("stop_go",0) # Stop
time.sleep(0.1)
self.motors["PGM:Grating"].put("stop_go",3) # Go
time.sleep(0.1)
self.motors["PGM:M2"].put("stop_go",0) # Stop
time.sleep(0.1)
self.motors["PGM:M2"].put("stop_go",3) # Go
time.sleep(0.1)
self.PVs["PGM:Energy:Reached"].put(1, wait = True)
self.energy0 = self.cfg["Intervals"][0]["Startpoint"]
log.info("Move PGM to initial energy ({})".format(self.energy0))
self.MovePGM(self.energy0)
def MoveSmpX(self,SP):
log.info("Move sample X to: {}".format(SP))
self.motors["SMP:X"].put("stop_go",3) # Go
self.motors["SMP:X"].move(SP)
time.sleep(1)
while not self.motors["SMP:X"].done_moving:
CLIMessage("sample X moving ...", "IG")
time.sleep(1)
def MoveSmpY(self,SP):
log.info("Move sample Y to: {}".format(SP))
self.motors["SMP:Y"].put("stop_go",3) # Go
self.motors["SMP:Y"].move(SP)
time.sleep(1)
while not self.motors["SMP:Y"].done_moving:
CLIMessage("sample Y moving ...", "IG")
time.sleep(1)
def MoveSmpZ(self,SP):
log.info("Move sample Z to: {}".format(SP))
self.motors["SMP:Z"].put("stop_go",3) # Go
self.motors["SMP:Z"].move(SP)
time.sleep(1)
while not self.motors["SMP:Z"].done_moving:
CLIMessage("sample Z moving ...", "IG")
time.sleep(1)
def MoveSmpRot(self,SP):
log.info("Move sample rotation to: {}".format(SP))
self.motors["SMP:Rot"].put("stop_go",3) # Go
self.motors["SMP:Rot"].move(SP)
time.sleep(1)
while not self.motors["SMP:Rot"].done_moving:
CLIMessage("sample Rotation moving ...", "IG")
time.sleep(1)
def clearPlot(self):
log.info("Clear plots PVs and parameters")
self.Energy = []
self.I0 = []
self.It = []
self.It2 = []
self.AbsTr = []
self.AbsTr2 = []
self.If = []
self.AbsFlu = []
self.PVs["PLOT:Energy"].put(self.Energy)
self.PVs["PLOT:I0"].put(self.I0)
self.PVs["PLOT:It"].put(self.It)
self.PVs["PLOT:It2"].put(self.It2)
self.PVs["PLOT:AbsTr"].put(self.AbsTr)
self.PVs["PLOT:AbsTr2"].put(self.AbsTr2)
self.PVs["PLOT:If"].put(self.If)
self.PVs["PLOT:AbsFlu"].put(self.AbsFlu)
def setPlotData(self):
log.info("Setting plots data")
self.PVs["PLOT:Energy"].put(self.Energy)
self.PVs["PLOT:I0"].put(self.I0)
self.PVs["PLOT:It"].put(self.It)
self.PVs["PLOT:It2"].put(self.It2)
self.PVs["PLOT:AbsTr"].put(self.AbsTr[1:])
self.PVs["PLOT:AbsTr2"].put(self.AbsTr2)
self.PVs["PLOT:If"].put(self.If)
self.PVs["PLOT:AbsFlu"].put(self.AbsFlu)
def checkPause(self):
diffTime = 0
pauseFlag = 0
startTime = time.time()
while self.PVs["SCAN:pause"].get():
pauseFlag = 1
diffTime = time.time() - startTime
CLIMessage("Scan is paused | pausing time(sec): {}".format(diffTime), "IO")
time.sleep(0.1)
if pauseFlag == 1:
log.warning("Scan was paused | pausing time(sec): %f ", diffTime)
def pauseTrigger(self):
currentOk = True
photonShutterOk = True
radiationShutterOk = True
KeithelyI0OK = True
ringLowerCurrent = self.scanLimites["SRLowerCurrent"]
ringUpperCurrent = self.scanLimites["SRUpperCurrent"]
KeithelyI0LowerLimit = self.scanLimites["KEITHELY_I0LowerRBV"]
#reading detectors PVs
KeithelyI0ReadOut = epics.PV(self.KeithelyI0PV["PV"]["picoAmmeterI0AcqReadOut"]["pvname"])
"""
setup writing flages to avoid continues writing logs in the log file
"""
currentLogFlag = 0
photonShutterLogFlag = 0
radiationShutterLogFlag = 0
KeithelyI0LogFlag = 0
while True:
photonShutterStatus = self.PVs["photonShutter:Status"].get()
radiationShutterStatus = self.PVs["radiationShutter:Status"].get()
currentCurrent = self.PVs["RING:Current"].get()
KeithelyI0ReadOut = epics.PV(self.KeithelyI0PV["PV"]["picoAmmeterI0AcqReadOut"]["pvname"]).get()
################### Check current parameters ###############
if ringLowerCurrent <= currentCurrent <= ringUpperCurrent:
currentOk = True
if currentLogFlag == 1:
log.warning("SR current is returned to allowed limites, now it is: {} mA."
.format(currentCurrent))
currentLogFlag = 0
else:
currentOk = False
if currentLogFlag == 0:
log.warning("Scan is paused | SR current is: {} mA.".format(currentCurrent))
currentLogFlag = 1
################### Check photonShutter parameters ###############
if photonShutterStatus == 1: # shutter is open 1, undefined 0, 2 closed, 3 interlocked
photonShutterOk = True
if photonShutterLogFlag == 1:
log.warning("Photon Shutter status is returned to allowed limites, now it is: open")
photonShutterLogFlag = 0
else:
photonShutterOk = False
if photonShutterLogFlag == 0:
log.warning("Scan is paused | Photon shutter status is: closed")
photonShutterLogFlag = 1
################### Check radiationShutter parameters ###############
if radiationShutterStatus == 1: # shutter is open 1, undefined 0, 2 closed, 3 interlocked
radiationShutterOk = True
if radiationShutterLogFlag == 1:
log.warning("Radiation shutter status is returned to allowed limites, now it is: open")
radiationShutterLogFlag =0
else:
radiationShutterOk = False
if radiationShutterLogFlag == 0:
log.warning("Scan is paused | Radiation shutter status is: closed")
radiationShutterLogFlag = 1
#################### Check ROIs if current, shutters and stopper are okay ###############
if currentOk and photonShutterOk and radiationShutterOk == True:
#################### Check Keithely_I0 ####################
if KeithelyI0ReadOut >= KeithelyI0LowerLimit:
KeithelyI0OK = True
if KeithelyI0LogFlag == 1:
log.warning("Keithely_I0 value is returned to allowed limites, it is now {}"
.format(KeithelyI0ReadOut))
KeithelyI0LogFlag = 0
else:
KeithelyI0OK = False
if KeithelyI0LogFlag == 0:
log.warning("Scan is paused | Keithely_I0 readout({}) is below the allowed limit ({})"
.format(KeithelyI0ReadOut,KeithelyI0LowerLimit))
KeithelyI0LogFlag = 1
# if any of below is false, pause the scan
if False in (currentOk, photonShutterOk, radiationShutterOk, KeithelyI0OK):
self.PVs["SCAN:pause"].put(1) # 1 pause, 0 release
else:
self.PVs["SCAN:pause"].put(0)
time.sleep(self.scanLimites["checkLimitesEvery"]) # time in seconds
def stopScanning(self):
self.PVs["SCAN:Stop"].put(1) # to make the interlock of voltage source
self.PVs["PGM:Energy:Reached"].put(1, wait=True)
log.warning("Stop button has been pressed, runinig scan is terminated !!")
os.rename("SED_Scantool.log", "SEDScanTool_{}.log".format(self.creationTime))
shutil.move("SEDScanTool_{}.log".format(self.creationTime), "{}/SEDScanTool_{}.log".format(self.localDataPath, self.creationTime))
self.dataTransfer()
sys.exit()
def initDetectors(self):
log.info("Detectors initialization")
self.available_detectors = ["IC","XFLASH"]
self.detectors = []
detlist = self.cfg["detectors"]
log.info("Chosen detectors are {}".format(detlist))
self.detChosen = detlist
for det in detlist:
if det == "XFLASH":
self.detectors.append(XFLASH("XFLASH",self.paths, self.cfg | self.userinfo)) # this sign (|) to combine dicts
elif det == "KEITHLEY_I0":
self.detectors.append(KEITHLEY_I0("KEITHLEY_I0",self.paths,self.userinfo,self.voltageSourcePARAM))
elif det == "KEITHLEY_Itrans":
self.detectors.append(KEITHLEY_ITRANS("KEITHLEY_ITRANS",self.paths,self.userinfo))
elif not det in self.available_detectors:
raise Exception("Unknown detector")
log.error("Unknown detector is Chosen")
def plotting(self):
#################### Plotting ####################
plotList = []
if "KEITHLEY_Itrans" in self.cfg["detectors"]:
plottingGUI = self.paths["HESEB_ScanTool_I0_It"]
plotList.append(plottingGUI)
log.info("HESEB_ScanTool_I0_It plotting started")
if "XFLASH" in self.cfg["detectors"]:
plottingGUI = self.paths["HESEB_ScanTool_XFLASH"]
plotList.append(plottingGUI)
log.info("HESEB_ScanTool_XFLASH")
else:
plottingGUI = self.paths["HESEB_ScanTool_I0"]
log.info("HESEB_ScanTool_I0 started")
plotList.append(plottingGUI)
if "XFLASH" in self.cfg["detectors"]:
plottingGUI = self.paths["HESEB_ScanTool_XFLASH"]
plotList.append(plottingGUI)
log.info("HESEB_ScanTool_XFLASH plotting started")
# self.plot = plotting
for plotGUI in plotList:
subprocess.Popen(plotGUI)
def signal_handler(self, sig, frame):
"""Calls abort_scan when ^C is typed"""
if sig == signal.SIGINT:
self.PVs["SCAN:Stop"].put(1) # to make the interlock of voltage source
self.PVs["PGM:Energy:Reached"].put(1, wait=True)
log.warning("Ctrl + C (^C) has been pressed, runinig scan is terminated !!")
os.rename("SED_Scantool.log", "SEDScanTool_{}.log".format(self.creationTime))
shutil.move("SEDScanTool_{}.log".format(self.creationTime), "{}/SEDScanTool_{}.log".format(self.localDataPath, self.creationTime))
self.dataTransfer()
sys.exit()