#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')) """ #open shutter print "\n try open shutter" caput('X07MA-FE-PH1:CLOSE4BL',1) cawait('X07MA-FE-PH1:MS2', 1) print "\n shutter is open" """ print "\nStart energy scan..." #print E1,E2,TIME,DELAY,str(MODE) ,str(OFFSET) ,str(FOLDER) ,str(FILE) ,str(ALPHA) 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: print "Set polarization" pol_mode.write(MODE) if MODE == 'LINEAR': if ALPHA is not None: pol_angle.write(ALPHA) time.sleep(0.5) # TIME SLEEP IS NECESSARY. OTHERWISE NEXT COMMAND WILL NOT WAIT FOR POLARIZATION SETTING. ADDED BY CP ON 07.02.2020 wait_device(pol_done, "DONE") # Oct. 2019 added wait to try avoid taper error print "Polarization setting done" print "Set Energy" if OFFSET is not None: pol_offset.write(OFFSET) #TODO: CAPUT #time.sleep(0.5) #wait_device(pol_done, "DONE") energy.write(float(E1)) # no need to add wait command. This commands sets and waits. print "Energy setting done" ################# """ 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." ############ """ 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() try: while True: # if ABORT_ON_ID_ERROR == False, waits reading with no ID error caput('START', '1') #print "caput START" #add for debug 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') time.sleep(2.0) print "Finished Energy scan." finally: waiting = False join(ret) #print "Joined " if after_sample(): #Repeat if id error break #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??? # commented on June 20th 2019 #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) mail #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