Files
x11ma/script/old_scripts/EnergyScanXtreme.py
gac-x11ma 0f8956cafe
2025-09-03 16:52:53 +02:00

144 lines
3.4 KiB
Python

#Script imported from: MgE_plus.xml
#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 "\nStarting energy scan - Parameters: ",
print E1,E2,TIME,DELAY,str(MODE) ,str(OFFSET) ,str(FOLDER) ,str(FILE) ,str(ALPHA)
print pol_mode
start = time.localtime()
folder = os.path.expanduser("~/Data1/") + FOLDER + "/";
def getNewestFile():
global folder
import glob
try:
return max(glob.iglob(folder+'/*.txt'), key=os.path.getctime)
except:
return None
newest = getNewestFile()
#Pre-actions
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")
caput('E1', E1)
caput('E2', E2)
caput('TIME', TIME)
caput('FOLDER', FOLDER)
set_energy (E1)
caput('FILE', FILE)
time.sleep(0.1)
#open_valve()
#open_valve(0.0) #TODO: WHY WRITING AGAIN???
time.sleep(DELAY)
caput('START', '1')
try:
write_logs()
waiting = True
def plot_task():
global waiting
pos=count.take()
start_plot()
while waiting:
if count.take() != pos:
update_plot()
pos = count.take()
time.sleep(2.0)
ret = fork(plot_task)
try:
#Post-actions
wait_channel('START', 'STOP', type = 's')
time.sleep(2.0)
print "Finished Energy scan"
finally:
waiting = False
join(ret)
#File convertion
newName = folder + time.strftime("%Y%m%d_%H%M%S_", start) + FILE + "_0000.txt";
name = getNewestFile()
if name == newest:#TODO: WHY WRITING AGAIN???
raise Exception("Data file not created")
if not os.path.isfile(name):
raise Exception("Data file not found: " + name)
convert_file(name, newName)
#plot_file(newName)
print("Success")
except:
while caget('START') == 'START':
print("Aborting...")
caput('START', '0')