Files
x11ma/script/EnergyScan-withThreading.py
2016-02-05 20:48:43 +01:00

308 lines
8.8 KiB
Python

import os
import traceback
import threading
############################PGM+ID1+ID2###################################################
if str(SET_OFFSETS) == "1":
print "Setting offsets"
if RUNTYPE in ["+/-", "+" , "-"]:
caput(OTF_MODE1,1) # circ + in ID1
caput(OTF_MODE2,2) # circ - in ID
elif RUNTYPE in ["LH/LV", "LH", "LV"]:
caput(OTF_MODE1,0)
caput(OTF_MODE2,0)
wait_channel(OTF_DONE, 1, type = 'i')
caput(OTF_ALPHA1, 0.0) # LH in ID1
caput(OTF_ALPHA2, 90.0) # LV in ID2
wait_channel(OTF_DONE, 1, type = 'i')
else:
raise Exception("Invalid run type: " + RUNTYPE)
caput(OTF_OFF1,OFFSET1)
caput(OTF_OFF2,OFFSET2-40) #detune ID2
wait_channel(OTF_DONE, 1, type = 'i')
print "Offsets are set"
import sys
sys.exit(0)
else:
print "Running full script"
file_prefix = time.strftime("%y%m%d")
input_path = "/sls/X11MA/Data1/public/X11MA/temp/"+file_prefix+"/"
output_path = input_path #+file_prefix+"/" #"/sls/X11MA/Data1/public/e10989/"+file_prefix+"/"
#Parameters
"""
E1 = 680
E2 = 750
TIME = 2 #min
DELAY = 10.0 #s
OFFSET1 = 1.0 #eV
OFFSET2 = -1.0 #eV
PREFIX = 'Data'
RUNTYPE = "+/-"
ROUNDS = 1
PLOT_TYPE = 1
"""
print "\nStarting energy scan - Parameters: ",
print E1,E2,TIME,DELAY,OFFSET1,OFFSET2,RUNTYPE#,ALPHA1,ALPHA2
###############################################################################
# Plotting
###############################################################################
task = None
running = False
def readMCP1():
global MCP1out
MCP1 = []
a1r = []
a1=MCPArray1.read()
for i in a1:
a1r.append(i)
Sum1a1 = 0
Cam1a1 = 0
Sum2a1 = 0
Cam2a1 = 0
for i in range(0,479):
Sum1a1 = Sum1a1 + a1r[i]
for i in range(Cam_start,Cam_end):
Cam1a1 = Cam1a1 + a1r[i]
for i in range(480,959):
Sum2a1 = Sum2a1 + a1r[i]
for i in range(Cam_start+480,Cam_end+480):
Cam2a1 = Cam2a1 + a1r[i]
MCP1.append(line_sep)
MCP1.append(e)
MCP1.append(Sum1a1)
MCP1.append(Cam1a1)
MCP1.append(Sum2a1)
MCP1.append(Cam2a1)
if Save_array_data == 1:
MCP1.append(a1r)
a1r = []
sMCP1 = sep.join(str(x) for x in MCP1) + line_sep # MCP1.write("%s" + sep % i)
MCP1out.write(sep + "rbkenergy" + sep + "Sum1" + sep + "Sum2" + sep + "Cam1" + sep + "Cam2" + sep + "Array" + line_sep + sMCP1)
def readMCP2():
global MCP2out
MCP2 = []
a2r = []
a2=MCPArray2.read()
for j in a2:
a2r.append(j)
Sum1a2 = 0
Cam1a2 = 0
Sum2a2 = 0
Cam2a2 = 0
for j in range(0,479):
Sum1a2 = Sum1a2 + a2r[j]
for j in range(Cam_start,Cam_end):
Cam1a2 = Cam1a2 + a2r[j]
for j in range(480,959):
Sum2a2 = Sum2a2 + a2r[j]
for j in range(Cam_start+480,Cam_end+480):
Cam2a2 = Cam2a2 + a2r[j]
MCP2.append(line_sep)
MCP2.append(e)
MCP2.append(Sum1a2)
MCP2.append(Cam1a2)
MCP2.append(Sum2a2)
MCP2.append(Cam2a2)
if Save_array_data == 1:
MCP2.append(a2r)
a2r = []
sMCP2 = sep.join(str(y) for y in MCP2) + line_sep
MCP2out.write(sep + "rbkenergy" + sep + "Sum1" + sep + "Sum2" + sep + "Cam1" + sep + "Cam2" + sep + "Array" + line_sep + sMCP2)
def _startPlot(type):
global running
sep = "\t"
line_sep = "\r\n"
print "Starting plot: type " + str(type)
running = True
p = plot(None,name="Energy")[0]
s = p.getSeries(0)
cur = 0
time.sleep(3.0)
while running:
try:
if otf_start.read() == 0:
break
e = energy.read()
if (MCP_1 == 1 or MCP_2 == 1):
output_file_MCP1 = output_path+"MCP1"+file_prefix+"_" + suffix + ".dat"
MCP1out = open(output_file_MCP1, "a+")
output_file_MCP2 = output_path+"MCP2"+file_prefix+"_" + suffix + ".dat"
MCP2out = open(output_file_MCP2, "a+")
threadA = threading.Thread(target=readMCP1)
threadB = threading.Thread(target=readMCP2)
threadA.start()
threadB.start()
time.sleep(SamplingTime.read()*0.001)
threadA.join()
threadB.join()
if (abs(e-cur)) > 0.00001:
v = abs((keithley_2a.read() / ((keithley_1a if (type==1) else keithley_3a).read() )))
s.appendData(e,v)
cur = e
# time.sleep(0.2)
except:
pass
print "Done Plotting"
if (MCP_1 == 1 or MCP_2 == 1):
MCP1out.close()
MCP2out.close()
def startPlot(type = 1):
global task
task = fork((_startPlot,(type,)),)
def stopPlot():
global task, running
running = False
ret = join(task)
############################PGM+ID1+ID2###################################################
def switchpol(activeID, runtype):
global pol_str
if activeID == 1:
caput(OTF_OFF1,OFFSET1)
caput(OTF_OFF2,OFFSET2-40) #detune ID2
if runtype in ["+/-", "+"]:
pol_str = "circ +"
elif runtype in ["LH/LV", "LH"]:
pol_str = "Lin. Horizontal"
elif activeID == 2:
caput(OTF_OFF1,OFFSET1-40) #detune ID1
caput(OTF_OFF2,OFFSET2)
if runtype in ["+/-", "-"]:
pol_str = "circ -"
elif runtype in ["LH/LV", "LV"]:
pol_str = "Lin. Vertical"
else:
raise Exception("Invalid parameter")
pol_str = None
polswitch = 1
fid = get_next_fid(input_path, "o" + file_prefix)
###############################################################################
#Prepare scan
###############################################################################
'''if ID1 == 1:
caput ("X11PHS-E:OPT","PGM+ID1")
elif ID2 == 1:
caput ("X11PHS-E:OPT","PGM+ID2")
else:
caput ("X11PHS-E:OPT","PGM+ID1+ID2")
'''
number_of_scans = 1
if RUNTYPE in ["+/-", "+", "-"]:
caput(OTF_MODE1,1) # circ + in ID1
caput(OTF_MODE2,2) # circ - in ID2
wait_channel(OTF_DONE, 1, type = 'i')
if RUNTYPE == "+/-":
number_of_scans = 2 * ROUNDS
else:
number_of_scans = ROUNDS
elif RUNTYPE in ["LH/LV", "LH", "LV"]:
caput(OTF_MODE1,0)
caput(OTF_MODE2,0)
wait_channel(OTF_DONE, 1, type = 'i')
caput(OTF_ALPHA1, 0.0) # LH in ID1
caput(OTF_ALPHA2, 90.0) # LV in ID2
wait_channel(OTF_DONE, 1, type = 'i')
if RUNTYPE == "LH/LV":
number_of_scans = 2 * ROUNDS
else:
number_of_scans = ROUNDS
else:
raise Exception("Invalid run type: " + RUNTYPE)
if RUNTYPE in ["-", "LV"]:
switchpol(2, RUNTYPE) # tune ID2 --> polarization: C- or LV
polswitch = 0
elif RUNTYPE in ["+/-", "+", "LH/LV", "LH"]:
switchpol(1, RUNTYPE) # tune ID1 --> polarization: C+ or LH
time.sleep(1.0)
wait_channel(OTF_DONE, 1, type = 'i')
'''open_vg10()
time.sleep(0.5)
open_vg11()
time.sleep(0.5)
open_vg12()
time.sleep(0.5)
open_vg13()
'''
for scan_no in range(number_of_scans):
suffix = ("%03d" % fid)
input_file = input_path + "o" + file_prefix + "_" + suffix + ".dat"
caput(OTF_E1, E1)
caput(OTF_E2, E2)
caput(OTF_TIME, TIME)
caput(OTF_FTS,file_prefix)
caput(OTF_FID,fid)
time.sleep(2.0)
caput(OTF_ESET, E1)
wait_channel(OTF_DONE, 1, type = 'i')
time.sleep(DELAY)
time.sleep(2.0)
startPlot(PLOT_TYPE)
#Start the OTF scan
#caput(OTF_START, 'GO')
otf_start.write(1)
time.sleep(3.0)
try:
#wait_channel(OTF_START, 'STOP', timeout = int(TIME*60), type = 's')
otf_start.waitValue(0, (15 + int(TIME*60)) *1000)
except:
print "******** OTF STOP TIMEOUT **********"
otf_start.write(0)
finally:
stopPlot()
time.sleep(5.0)
#TODO: wait for file instead of sleep
#Convert file
output_file = output_path+"os"+file_prefix+"_" + suffix + ".dat"
print("Converting data file: " + output_file);
convert_file(input_file, output_file, pol_str)
plot_file(output_file, file_prefix+"_" + suffix) #"Scan " + str(scan_no+1))
if RUNTYPE in ["+/-", "LH/LV"]:
if polswitch == 1:
switchpol(2, RUNTYPE) # tune ID2 --> polarization: C- or LV
polswitch = 0
else:
polswitch = 1
switchpol(1, RUNTYPE) # tune ID1 --> polarization: C+ or LH
else:
print "running in one polarization mode, no switching"
time.sleep(3.0)
fid = fid + 1
caput(OTF_ESET, E1)
close_vg13()
print "Finished Energy scan"
print("Success")