This repository has been archived by the owner on Aug 31, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
srx-coord-energy-fsi0.py
executable file
·422 lines (394 loc) · 11.1 KB
/
srx-coord-energy-fsi0.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
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
422
#! /usr/bin/env /usr/bin/python2.7
#backwards compatible (i.e., reverts to X raster move Y, hfm, vhm scan if no Z
#values given) three dimensional fast mirror scan. Scan is likely to be very
#fast with respect to motor heating issues. use --wait command line option
#if two transverse stages are scanned with sub-millimeter steps
#add file logging
import signal
import sys
import os
import time
from epics import PV
import math
from optparse import OptionParser
import string
import decimal
import matplotlib.pyplot as plt
import numpy as np
#replace range()
#floating point and inclusive
def frange(start,end=None,inc=None,p=None):
if end == None:
end = start + 0.
start = 0.
if inc == None:
inc = 1.
if p == None:
p = 3
if inc == 0:
count = 1
else:
count = int(math.ceil( (end-start)/inc ))+1
L = [None,] * count
p = pow(10,p)
start = start*p
end = end*p
inc = inc*p
L[0] = float(start)/p
for i in xrange(1,count):
L[i] = L[i-1] + float(inc)/p
return L
#can we signal handle in python??
def sigint_handler(signal,frame):
global simulate
global fp
if simulate != True:
fp.close()
print "!!!!!!!!! stopped"
else:
print "Ctrl-C detected...exiting"
sys.exit()
signal.signal(signal.SIGINT,sigint_handler)
#target array: (xpos, at_position)(ypos, at_position)
tar = []
tar.append([])
tar[0].append(0.)
tar[0].append(1)
tar.append([])
tar[1].append(0.)
tar[1].append(1)
tar.append([])
tar[2].append(0.)
tar[2].append(1)
#deadband in motor units
dbd = .001
#simulate motor moves
simulate=False
global shut_open
shut_open = False
global current
current = False
def cbf_shut(pvname=None,value=None,char_value=None,type=None,enum_strs=None,**kw):
global shut_open
if value==1:
shut_open=True
else:
shut_open=False
def cbf_curr(pvname=None,value=None,char_value=None,type=None,enum_strs=None,**kw):
global current
if value<1.:
current=False
else:
current=True
#callback to check on motor position
def cbfx(pvname=None,value=None,char_value=None,type=None,enum_strs=None,**kw):
if indeadband(float(tar[0][0]),float(value),dbd)==1:
tar[0][1] = 0
else:
tar[0][1] = 1
def cbfy(pvname=None,value=None,char_value=None,type=None,enum_strs=None,**kw):
if indeadband(float(tar[1][0]),float(value),dbd)==1:
tar[1][1] = 0
else:
tar[1][1] = 1
def cbfz(pvname=None,value=None,char_value=None,type=None,enum_strs=None,**kw):
if indeadband(float(tar[2][0]),float(value),dbd)==1:
tar[2][1] = 0
else:
tar[2][1] = 1
def cbf_temp(pvname=None,value=None,char_value=None,type=None,enum_strs=None,**kw):
thresh_temp=80.
while(value > thresh_temp):
print "Temperature is too high. Pausing scan."
time.sleep(60.)
#manual deadband
def indeadband(com,act,dbd):
if math.fabs(math.fabs(com)-math.fabs(act))<float(dbd):
return 1
else:
return 0
def main(argv=None):
global dbd
global simulate
global fp
global shut_open
global current
#parse command line options
usage = "usage: %prog [options]\nData files are written to /data/<year>/<month>/<day>/"
parser = OptionParser(usage)
parser.add_option("--detname", action="store", type="string", dest="detname", help="detector to trigger")
parser.add_option("--wait", action="store", type="float", dest="stall", help="wait at each step [seconds]")
parser.add_option("--config",action="store", type="string", dest="fname", help="name of config file")
parser.add_option("--simulate", action="store_true", dest="sim", default=False, help="simulate motor moves and bursting")
parser.add_option("--checkbeam", action="store_true", dest="checkbeam", default=False, help="only acquire when beam is on")
(options,args) = parser.parse_args()
#open log file
D0=time.localtime()[0]
D1=time.localtime()[1]
D2=time.localtime()[2]
D3=time.localtime()[3]
D4=time.localtime()[4]
cd=os.getcwd()
fstr='/nfs/xf05id1/data/'
if sys.argv[0][0]=='.':
out_filename=fstr+repr(D0)+'/'+repr(D1)+'/'+repr(D2)+'/'+'log_'+repr(D3)+'_'+repr(D4)+'_'+\
string.split(string.strip(sys.argv[0],'./'),'/')[0]+'.txt'
else:
out_filename=fstr+repr(D0)+'/'+repr(D1)+'/'+repr(D2)+'/'+'log_'+repr(D3)+'_'+repr(D4)+'_'+\
string.split(string.strip(sys.argv[0],'./'),'/')[5]+'.txt'
try:
os.chdir(fstr+repr(D0))
except OSError:
try:
os.mkdir(fstr+repr(D0))
except Exception:
print 'cannot create directory: '+fstr+repr(D0)
sys.exit()
try:
os.chdir(fstr+repr(D0)+'/'+repr(D1))
except OSError:
try:
os.mkdir(fstr+repr(D0)+'/'+repr(D1))
except Exception:
print 'cannot create directory: '+fstr+repr(D0)+'/'+repr(D1)
sys.exit()
try:
os.chdir(fstr+repr(D0)+'/'+repr(D1)+'/'+repr(D2))
except OSError:
try:
os.mkdir(fstr+repr(D0)+'/'+repr(D1)+'/'+repr(D2))
except Exception:
print 'cannot create directory: '+fstr+repr(D0)+'/'+repr(D1)+'/'+repr(D2)
sys.exit()
try:
fp=open(out_filename,'a')
except Exception:
print 'cannot open file: '+out_filename
sys.exit()
os.chdir(cd)
fp.write(', '.join(sys.argv))
fp.write('\n')
#open list of scan points
try:
fconfig=open(options.fname)
except Exception:
print "cannot open file containing scan points. Error opening: "+options.fname
sys.exit()
fstr='#a default string'
pN=0
angle=list()
ivu=list()
t2gap=list()
while fstr.rsplit().__len__() > 0:
if (fstr[0] is not '#'):
pN=pN+1
angle.append(fstr.rsplit()[0])
ivu.append(fstr.rsplit()[1])
t2gap.append(fstr.rsplit()[2])
fstr=fconfig.readline()
fconfig.close()
#initialize PVs and callbacks
if options.detname == None:
# detstr='XF:03IDA-BI{FS:1-CAM:1}'
detstr=''
print "must provide detector pv base, e.g., 'XF:28IDA-BI{URL:01}'"
sys.exit()
else:
detstr=options.detname
bmot = PV('XF:05IDA-OP:1{Mono:HDCM-Ax:P}Mtr.VAL')
bmot_cur = PV('XF:05IDA-OP:1{Mono:HDCM-Ax:P}Mtr.RBV')
bmot_stop = PV('XF:05IDA-OP:1{Mono:HDCM-Ax:P}Mtr.STOP')
umot = PV('SR:C5-ID:G1{IVU21:1-Mtr:2}Inp:Pos')
umot_cur = PV('SR:C5-ID:G1{IVU21:1-LEnc}Gap')
umot_go = PV('SR:C5-ID:G1{IVU21:1-Mtr:2}Sw:Go')
gmot = PV('XF:05IDA-OP:1{Mono:HDCM-Ax:X2}Mtr.VAL')
gmot_cur = PV('XF:05IDA-OP:1{Mono:HDCM-Ax:X2}Mtr.RBV')
gmot_stop = PV('XF:05IDA-OP:1{Mono:HDCM-Ax:X2}Mtr.STOP')
traj_o_x=PV('SR:C31-{AI}Aie5-2:Offset-x-Cal')
traj_o_y=PV('SR:C31-{AI}Aie5-2:Offset-y-Cal')
traj_a_x=PV('SR:C31-{AI}Aie5-2:Angle-x-Cal')
traj_a_y=PV('SR:C31-{AI}Aie5-2:Angle-y-Cal')
shut_status=PV('SR:C05-EPS{PLC:1}Shutter:Sum-Sts')
beam_current=PV('SR:C03-BI{DCCT:1}I:Total-I')
bragg_temp=PV('XF:05IDA-OP:1{Mono:HDCM-Ax:P}T-I')
if detstr=='xf05bpm03':
det0 = PV(detstr+':DataRead_Ch1')
det1 = PV(detstr+':DataRead_Ch2')
det2 = PV(detstr+':DataRead_Ch3')
det3 = PV(detstr+':DataRead_Ch4')
else:
det0 = PV(detstr+'Cur:I0-I')
det1 = PV(detstr+'Cur:I1-I')
det2 = PV(detstr+'Cur:I2-I')
det3 = PV(detstr+'Cur:I3-I')
detstr='XF:05IDA-BI:1{FS:1-Cam:1}'
i0_acq = PV(detstr+'Acquire')
i0_exp = PV(detstr+'AcquireTime')
i0_ROI_int = PV(detstr+'Stats1:Total_RBV')
i0_imode = PV(detstr+'ImageMode')
bmot.info
bmot_cur.info
bmot_stop.info
umot.info
umot_cur.info
umot_go.info
gmot.info
gmot_cur.info
gmot_stop.info
det0.info
det1.info
det2.info
det3.info
i0_acq.info
i0_exp.info
i0_ROI_int.info
bragg_temp.info
bmot_cur.add_callback(cbfx)
bmot_cur.run_callbacks()
umot_cur.add_callback(cbfy)
umot_cur.run_callbacks()
gmot_cur.add_callback(cbfz)
gmot_cur.run_callbacks()
shut_status.add_callback(cbf_shut)
beam_current.add_callback(cbf_curr)
shut_status.run_callbacks()
beam_current.run_callbacks()
bragg_temp.add_callback(cbf_temp)
bragg_temp.run_callbacks()
#check command line options
if options.stall == None:
twait = 0.
else:
twait = options.stall
if i0_acq.get() is not 0:
i0_acq.put(1)
i0_imode.put(0)
# display_list = np.zeros((pN+1,2))
count=0
str='Start time is '+time.asctime()
print str
fp.write(str)
fp.write('\n')
if options.sim is True:
str=" -----simulating motor moves and bursts-----"
print str
fp.write(str)
fp.write('\n')
str="Current position [mm]: %(XC)7.3f, %(YC)7.3f, %(ZC)7.3f" %\
{"XC":bmot_cur.get(),"YC":umot_cur.get(),"ZC":gmot_cur.get()}
print str
fp.write(str)
fp.write('\n')
time.sleep(2)
LN=0
IOint=0
for x in range(0,pN):
tar[0][1] = 1
tar[0][0] = float(angle[x])
tar[1][1] = 1
tar[1][0] = float(ivu[x])
tar[2][1] = 1
tar[2][0] = float(t2gap[x])
#if tar[0][0] is the original position, raise "in position" flag
if indeadband(float(tar[0][0]),float(bmot_cur.get()),dbd)==1:
tar[0][1] = 0
if indeadband(float(tar[1][0]),float(umot_cur.get()),dbd)==1:
tar[1][1] = 0
if indeadband(float(tar[2][0]),float(gmot_cur.get()),dbd)==1:
tar[2][1] = 0
if options.sim is False:
bmot.put(tar[0][0])
umot.put(tar[1][0])
gmot.put(tar[2][0])
time.sleep(1)
umot_go.put(0)
else:
tar[0][1]=0
tar[1][1]=0
tar[2][1]=0
try:
ox=traj_o_x.get()
except CA.Client.Exception:
ox=12398
continue
try:
oy=traj_o_y.get()
except CA.Client.Exception:
oy=12398
continue
try:
ax=traj_a_x.get()
except CA.Client.Exception:
ax=12398
continue
try:
ay=traj_a_y.get()
except CA.Client.Exception:
ay=12398
continue
while (tar[0][1] == 1) or (tar[1][1] == 1) or(tar[2][1] == 1):
time.sleep(0.05)
if LN > 400:
umot_go.put(0)
LN=0
else:
LN=LN+1
if options.sim is False:
while ( options.checkbeam and (shut_open == False or beam_current == False)):
print "Stopped. Waiting for beam to return."
time.sleep(60.)
signal0=float(det0.get())
signal1=float(det1.get())
signal2=float(det2.get())
signal3=float(det3.get())
i0_acq.put(1)
if signal0<1e-10:
time.sleep(0.01)
signal0=float(det0.get())
signal1=float(det1.get())
signal2=float(det2.get())
signal3=float(det3.get())
time.sleep(twait)
while (i0_acq.get()==1):
time.sleep(io_exp.get())
I0int=float(i0_ROI_int.get())
else:
while ( options.checkbeam and (shut_open == False or beam_current == False)):
print "Stopped. Waiting for beam to return."
time.sleep(60.)
signal0=0.
signal1=0.
signal2=0.
signal3=0.
if options.sim is False:
str=' B= %(B)8.3f U= %(U)8.3f G= %(G)8.3f : %(C0)10.7e %(C1)10.7e %(C2)10.7e %(C3)10.7e TRAJ %(OX)6.3f %(OY)6.3f %(AX)6.3f %(AY)6.3f %(T)d %(IO)10.7e'%{"B":float(bmot_cur.get()), "U":float(umot_cur.get()), "G":float(gmot_cur.get()), "C0":signal0, "C1":signal1, "C2":signal2, "C3":signal3, "OX":ox,"AX":ax,"OY":oy,"AY":ay, 'T':time.time(), 'IO':I0int}
print str
fp.write(str)
fp.write('\n')
else:
str=' B= %(B)8.3f U= %(U)8.3f G= %(G)8.3f : %(C0)10.7e %(C1)10.7e %(C2)10.7e %(C3)10.7e TRAJ %(OX)6.3f %(OY)6.3f %(AX)6.3f %(AY)6.3f'%{"B":tar[0][0], "U":tar[1][0], "G":tar[2][0], "C0":signal0, "C1":signal1, "C2":signal2, "C3":signal3, "OX":ox,"AX":ax,"OY":oy,"AY":ay}
print str
fp.write(str)
fp.write('\n')
# display_list[count,0] = x
# display_list[count,1] = signal0
count = count+1
#return to starting positions
# if options.sim is False:
# bmot.put(float(angle[0]))
# gmot.put(float(ivu[0]))
# umot.put(float(t2gap[0]))
# time.sleep(2)
# xmot_stop.put(0)
str='End time is '+time.asctime()
print str
fp.write(str)
fp.write('\n')
fp.close()
# plt.figure()
# plt.plot(display_list[:,0],display_list[:,1])
# plt.plot(display_list[:,0],display_list[:,1],'go')
# plt.show()
return 0
if __name__ == "__main__":
sys.exit(main())