Files
x11ma/script/templates/EnergyScan.py
gac-x11ma 329ac0a48f Startup
2019-05-09 15:35:32 +02:00

211 lines
5.3 KiB
Python

#Script imported and adopted from: XTREME beamline
#Parameters
"""
E1 = 710
E2 = 720
TIME = 1 #min
DELAY = 10.0 #s
MODE = 'CIRC +'
OFFSET = -9.0
FOLDER = '2015_04/20150417'
FILE = 'Fe_plus'
ALPHA=0
"""
count.initialize()
data.initialize()
fdata.initialize()
idata.initialize()
edata.initialize()
p=None
def start_plot():
global p
p = plot([None, None, None,None, None], ["TEY", "TFY", "TEYraw", "TFYraw", "I0"])
def update_plot():
global p
c = count.take()
if c is None:
for i in range(len(p)):
p[i].getSeries(0).clear()
else:
data.setSize(c)
fdata.setSize(c)
idata.setSize(c)
edata.setSize(c)
teyr= data.read()
tfyr= fdata.read()
i0 = idata.read()
x = edata.read()
#from operator import truediv
l = lambda x,y: (0.0 if (y==0.0) else x/y) #truediv
tey=map(l, teyr, i0)
tfy=map(l, tfyr, i0)
p[0].getSeries(0).setData(x, to_array(tey,'d'))
p[1].getSeries(0).setData(x, to_array(tfy,'d'))
p[2].getSeries(0).setData(x, to_array(teyr,'d'))
p[3].getSeries(0).setData(x, to_array(tfyr,'d'))
p[4].getSeries(0).setData(x, to_array(i0,'d'))
print "\nStart energy scan..."
if MODE == "LINEAR":
print E1,"eV ->",E2,"eV,",TIME,"min duration,",DELAY,"sec delay,",str(MODE),str(ALPHA),"deg"
else:
print E1,"eV ->",E2,"eV,",TIME,"min duration,",DELAY,"sec delay,",str(MODE)
start = time.localtime()
folder = os.path.expanduser("~/Data1/") + FOLDER + "/";
#print "folder = ",folder
def getNewestFile():
global folder
import glob
try:
# print glob.glob(folder+'/*.txt')
# print max(glob.glob(folder+'/*.txt'), key=os.path.getctime)
return max(glob.glob(folder+'/*.txt'), key=os.path.getctime)
except:
return None
newest = getNewestFile()
#print "newest = ",newest
#Pre-actions
if NO_BEAM_CHECK == False:
wait_beam()
if MODE is not None:
pol_mode.write(MODE) #caput('X07MA-ID:MODE', MODE) #TODO: CAPUT
# time.sleep(0.5) #TODO: Needed?
if MODE == 'LINEAR':
if ALPHA is not None:
pol_angle.write(ALPHA) #TODO: CAPUT
# time.sleep(0.5)
#wait_device(pol_done, "DONE") #TODO: NEEDED?
if OFFSET is not None:
pol_offset.write(OFFSET) #TODO: CAPUT
# time.sleep(0.5)
#wait_device(pol_done, "DONE")
wait_device(pol_done, "DONE") # added 10/02/2017 to try avoid taper error. CP
#################
"""
print "Moving down energy in steps."
while True:
en = caget("X07MA-PGM:CERBK")
if en > 350.0:
estep = 20.0
else:
estep = 5.0
if abs(en - E1) < estep:
estep = abs(en - E1)
if en > float(E1):
set_energy( en - estep )
else:
break
print "It is done."
############
"""
energy.write(float(E1)) # added 10/02/2017 to try avoid taper error. CP
#wait_device(energy_done, 1) # added 10/02/2017 to try avoid taper error. CP. Removed in 17/03 because often script was hanging at this point
caput('E1', E1)
#print "caput E1" #add for debug
caput('E2', E2)
#print "caput E2" #add for debug
caput('TIME', TIME)
#print "caput TIME" #add for debug
caput('FOLDER', FOLDER)
#print "caput FOLDER" #add for debug
#set_energy (E1)
caput('FILE', FILE)
#print "caput FILE" #add for debug
time.sleep(0.1)
#open_valve()
#open_valve(0.0) #TODO: WHY WRITING AGAIN???
#print "before delay" #add for debug
time.sleep(DELAY)
#print "after delay" #add for debug
#print 'ldfkj;fjk'
#if WAIT_STABLE_TEMPERATURE == True:
# wait_temp()
caput('START', '1')
#print "caput START" #add for debug
try:
write_logs()
waiting = True
def plot_task():
global waiting
pos=count.take()
start_plot()
#print "start_plot" # added for debug
while waiting:
if count.take() != pos:
update_plot()
pos = count.take()
time.sleep(2.0)
ret = fork(plot_task)
try:
print "Scanning...",
#Post-actions
wait_channel('START', 'STOP', type = 's')
after_sample() #To call check_id_error()
time.sleep(2.0)
print "Finished Energy scan."
finally:
waiting = False
join(ret)
#print "Joined "
#File convertion
#newName = folder + time.strftime("%Y%m%d_%H%M%S_", start) + FILE + "_0000.txt";
newName = folder + time.strftime("%Y%m%d_%H%M_", start) + FILE + "_0000.txt";
name = getNewestFile()
#print "newest file:"
#print name
#print "newest variable"
#print newest
if name == newest:#TODO: WHY WRITING AGAIN???
print "Risk of overwriting. " + name + " same as " + newest
raise Exception("Data file not created.")
if not os.path.isfile(name):
raise Exception("Data file not found: " + name)
convert_file(name, newName)
log("Created data file: " + newName)
#plot_file(newName)
#print("Success")
#"PlotDomain", "Ecrbk"
except:
print("Aborting...")
while caget('START') == 'START':
caput('START', '0')
time.sleep(0.1)
#if get_exec_pars().source != CommandSource.plugin:
# show_message(str(ex))
raise