From b93efe4d4d89a2cc37a7139ce3ef1956e5648dd1 Mon Sep 17 00:00:00 2001 From: gac-x11ma Date: Mon, 3 Sep 2018 11:56:26 +0200 Subject: [PATCH] Script execution --- script/EnergyScanTransmission.py | 311 +++++++++++++++++++++++++++++++ 1 file changed, 311 insertions(+) create mode 100644 script/EnergyScanTransmission.py diff --git a/script/EnergyScanTransmission.py b/script/EnergyScanTransmission.py new file mode 100644 index 0000000..171ff23 --- /dev/null +++ b/script/EnergyScanTransmission.py @@ -0,0 +1,311 @@ +import os +import traceback +import thread +import time + +############################PGM+ID1+ID2################################################### + +if str(SET_OFFSETS) == "1": + print "Setting offsets" + #print file_prefix + if RUNTYPE in ["+/-", "+" , "-"]: + caput(OTF_MODE1,1) # circ + in ID1 + caput(OTF_MODE2,2) # circ - in ID2 + 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/data/X11MA/Data1/public/scans/1809_Armin/" +output_path = input_path #+file_Data1/beamtime_oct2017prefix+"/" #"/sls/X11MA/Data1/public/e10989/"+file_prefix+"/" + + +#Parameters + +E1 = 680 +E2 = 750 +TIME = 2 #min +DELAY = 10.0 #s +OFFSET1 = -2.0 #eV +OFFSET2 = -2.9 #eV +PREFIX = 'Data' +RUNTYPE = "+/-" +ROUNDS = 1 +PLOT_TYPE = 1 + +print "\nStarting energy scan - Parameters: ", +print Element,E1,E2,TIME,DELAY,OFFSET1,OFFSET2,RUNTYPE#,ALPHA1,ALPHA2 + +caput(OTF_ESET, E1) + +############################################################################### +# Plotting +############################################################################### + +task = None +running = False + + +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) + TEY = [] + dE1 = 0.0 + dE2 = 0.0 + t_start = time.time() + t_end = time.time() + TIME*60.0 + while running: + try: + if otf_start.read() == 0: + #if (otf_start.read() == 0 or time.time() > t_end): + break + e = energy.read() + if (time.time() <= t_end and OTF_override == 1): + + cur_en = E1+(E2-E1)*(time.time()-t_start)/(TIME*60) + #caput ("X11MA-PHS:E_SP",cur_en) + cur_en1 = cur_en - dE1 + cur_en2 = cur_en - dE2 + caput ("X11MA-ID1:ENERGY",e-0.1) + caput ("X11MA-ID2:ENERGY",e-0.1) + time.sleep(0.0001) + # pass + #time.sleep(0.0001)''' + ID1 = ID1_En.read() + ID2 = ID2_En.read() + #dE1 = ID1_En.read() - energy.read() + #dE2 = ID2_En.read() - energy.read() + aK1=keithley_1a.read() + aK2=keithley_2a.read() + aK3=keithley_3a.read() + aK1raw=Keithley_1_raw.read() + aK2raw=Keithley_2_raw.read() + aK3raw=Keithley_3_raw.read() + aTEYnorm=aK2/aK1 + TEY.append(line_sep) + TEY.append(e) + TEY.append(ID1) + TEY.append(ID2) + TEY.append(aTEYnorm) + TEY.append(aK1) + TEY.append(aK2) + TEY.append(aK3) + TEY.append(pol_str) + TEY.append(aK1raw) + TEY.append(aK2raw) + TEY.append(aK3raw) + if (abs(e-cur)) > 0.1: + v = abs((keithley_3a.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" + output_file_TEY = output_path+"TEY_"+file_prefix+"_" + suffix + ".dat" + TEYout = open(output_file_TEY, "w+") + sTEY = sep.join(str(x) for x in TEY) #+ line_sep # MCP1.write("%s" + sep % i) + sTEYout = sep + "rbkenergy" + sep + "ID1_en" + sep + "ID2_en" + sep + "TEY_norm" + sep + "Keithley1" + sep + "Keithley2" + sep + "Keithley3"+ sep + "Polarisation"+ sep + "Keithley1raw" + sep + "Keithley2raw" + sep + "Keithley3raw" + sTEYout = sTEYout + sTEY + TEYout.write(sTEYout) + TEYout.close() + print "Saved TEY signal data" +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") + +mag_str = None +#mag_str = caget(MAG) +pol_str = None +polswitch = 1 + +#fid = get_next_fid(input_path, "o" + file_prefix) +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" + # Log.append(scan_no+1) + #Log.append(posX) + sep = "\t" + line_sep = "\r\n" + 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) + T_start = Temp.getValue() + startPlot(PLOT_TYPE) + #Start the OTF scan + #caput(OTF_START, 'GO') + otf_start.write(1) + time.sleep(3.0) + print "Running scan " + str(scan_no+1) + " out of " + str(number_of_scans) + 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() + caput(OTF_ESET, E1+5.0) + T_end = Temp.getValue() + 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, mag_str, pol_str) + plot_file(output_file, file_prefix+"_" + suffix) #"Scan " + str(scan_no+1)) + print "Finished scan " + str(scan_no+1) + " out of " + str(number_of_scans) + + if Magnet == 1: + sMag1 = "positive" + sMag2 = "+12A" + else: + sMag1 = "negative" + sMag2 = "-12A" + output_file_Log = output_path+"Log_"+file_prefix+".dat" + Logout = open(output_file_Log, "a+") + sLogout = "Scan_number: "+file_prefix+"_"+suffix+line_sep+"Sample " + Sample + " Magnetized in " + sMag1 +" direction" + line_sep + sLogout = sLogout +"Settings: "+Element+", "+str(E1)+"-"+str(E2)+", "+str(TIME)+" min, "+str(OFFSET1)+", "+str(OFFSET2)+", "+pol_str+", ES="+str(ES.getValue())+", PGM:"+Grating.getValue()+", cff="+str(cff.getValue())+line_sep+"Positions: X=?"+'''str(TRX.getValue())+", Y="+str(TRY.getValue())+", Z="+str(TRZ.getValue())+", THT="+st(THT.getValue())'''+line_sep+"Temperature at start of the scan: "+str(T_start)+" and at the end of the scan: "+str(T_end)+line_sep + sLogout = sLogout + "-------------------------------------------------------------------------------------------------------------"+line_sep + Logout.write(sLogout) + Logout.close() + print "Log file updated" + + output_file_Control = output_path+file_prefix+".dat" + Control = open(output_file_Control, "a+") + sControl = suffix + " " + suffix + " " + Sample + " " + Element + " " + sMag2 + line_sep + Control.write(sControl) + Control.close() + print "Control file updated" + + 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") +