diff --git a/pxii_bec/macros/APscripts.py b/pxii_bec/macros/APscripts.py index c3f35aa..37c0f09 100644 --- a/pxii_bec/macros/APscripts.py +++ b/pxii_bec/macros/APscripts.py @@ -9,8 +9,8 @@ import numpy as np import math import matplotlib.pyplot as plt -import sys -import time, datetime +import sys +import time, datetime ################################ ### BASIC BL PROGRAMS @@ -99,7 +99,7 @@ def angle(e, *hkl): def resol(dstart, dend, nsteps): res = (Dstart - Dend) / nsteps print(f"resolution is: {res}") - return res + return res ##################### @@ -208,7 +208,7 @@ def rock(**kwargs): wr.plot(device_x=mot, device_y=det) ##set names/axes first ! s = scans.line_scan(mot, -dx, dx, steps=50, exp_time=time, relative=True) - + if ax == 0: data_x = s.scan.live_data.dcm_pitch.dcm_pitch.val else: @@ -247,12 +247,12 @@ def rock(**kwargs): intercept=intercept ) - + # maxy = max(data_y) # indmax = np.argmax(data_y) # xm = data_x[indmax] # p = model.make_params(amplitude=maxy, center=xm) - + g = model.fit(data_y, p, x=data_x) print(f'Center: {g.params["center"].value:.5f} FWHM: {g.params["fwhm"].value:.5f}') @@ -281,7 +281,7 @@ def rock(**kwargs): ### just reconfigure beaml ############################# def loadmac(): - + bec.macros.load_all_user_macros() # d, planner = init_beamline_environment() @@ -294,7 +294,7 @@ def plotset(y): plt.ion() plt.figure() plt.plot(y) - + ################################ ### FITTING PROGRAMS @@ -325,29 +325,29 @@ def justfit(data_x, data_y, model="gauss", ibg=0): pre='l' model = peak - ypeak = data_y + ypeak = data_y pars = model.guess(data_y, x = data_x) indmax = np.argmax(ypeak) - xm = data_x[indmax] - + xm = data_x[indmax] + if ibg: background = LinearModel(prefix = 'bg_') model = peak + background ### estimate initial params separately for both parts of the model # 1) BG - n = len(data_x) + n = len(data_x) edge= n//10 - idx=np.r_[0:edge, n-edge:n] # first and last part of the range + idx=np.r_[0:edge, n-edge:n] # first and last part of the range #m0, b0 = np.polyfit(data_x[idx], data_y[idx],1) - #ypeak= data_y - (m0*data_x + b0) # subtract linear part + #ypeak= data_y - (m0*data_x + b0) # subtract linear part pars = background.guess(data_y[idx], x = data_x[idx]) - + # 2) Function - BG - ypeak= data_y - (pars['bg_slope']*data_x + pars['bg_intercept'])# subtract linear part + ypeak= data_y - (pars['bg_slope']*data_x + pars['bg_intercept'])# subtract linear part pars.update( peak.guess(ypeak, x= data_x) ) - + g = model.fit(data_y, pars, x=data_x) @@ -405,7 +405,7 @@ def fit_plothist(hindex: int, signal_name: str, model="gauss", ibg=0): prefix='v_' if model == 'gauss': prefix='g_' - + g, xm = justfit(data_x, data_y, model=model, ibg=ibg) plt.ion() @@ -480,29 +480,29 @@ def fit_plot(data_x, data_y, model="gauss", ibg=1, silent=0,fitrange=0, fitclic pre='l_' model = peak - ypeak = data_y + ypeak = data_y pars = model.guess(data_y, x = data_x) indmax = np.argmax(ypeak) - xm = data_x[indmax] - + xm = data_x[indmax] + if ibg: background = LinearModel(prefix = 'bg_') model = peak + background ### estimate initial params separately for both parts of the model # 1) BG - n = len(data_x) + n = len(data_x) edge= n//10 - idx=np.r_[0:edge, n-edge:n] # first and last part of the range + idx=np.r_[0:edge, n-edge:n] # first and last part of the range #m0, b0 = np.polyfit(data_x[idx], data_y[idx],1) - #ypeak= data_y - (m0*data_x + b0) # subtract linear part + #ypeak= data_y - (m0*data_x + b0) # subtract linear part pars = background.guess(data_y[idx], x = data_x[idx]) - + # 2) Function - BG - ypeak= data_y - (pars['bg_slope']*data_x + pars['bg_intercept'])# subtract linear part + ypeak= data_y - (pars['bg_slope']*data_x + pars['bg_intercept'])# subtract linear part pars.update( peak.guess(ypeak, x= data_x) ) - + g = model.fit(data_y, pars, x=data_x) @@ -560,9 +560,9 @@ def fit_plot(data_x, data_y, model="gauss", ibg=1, silent=0,fitrange=0, fitclic print(g.fit_report()) # fit_id = pre+'_center' - + #center = g.pars[fit_id].value - return gcen, xm + return gcen, xm #################################### @@ -595,7 +595,7 @@ def save_data(hindex: int, device_name: str, signal_name: str, isave = 1): plt.ylabel(f"{signal_name} / AU") plt.show() - if isave: + if isave: ans = "n" ans = input("Store data in csv file? y/n ") if ans == "y": @@ -774,7 +774,7 @@ def read_mon(): vbroll = dev.vfm_roll.read()["vfm_roll"]["value"] vfm_x= dev.vfm_x.read()["vfm_x"]["value"] vfm_y= dev.vfm_y.read()["vfm_y"]["value"] - + # KB H hbu = dev.hfm_bu.read()["hfm_bu"]["value"] hbd = dev.hfm_bd.read()["hfm_bd"]["value"] @@ -1047,7 +1047,7 @@ def slitscan(device_location: str, direction: str, range: 1, nsteps=50, centre=0 else: mot = dev.fe_sl_ycen size = dev.fe_sl_ysize - + s_closed = 0.1 s_open = default_v @@ -1128,7 +1128,7 @@ def slitscan(device_location: str, direction: str, range: 1, nsteps=50, centre=0 umv(size, s_closed) wr.plot(device_x=mot, device_y=det) - + wr.x_label = mot.name wr.y_label = det.name @@ -1271,25 +1271,6 @@ def kbfocus(sizex, sizey): return - -######################### -### Status of the BEC -######################### - - -def bstatus(): - # - dock_area = bec.gui.new() - - dbrowser = dock_area.new("device_browser").new(bec.gui.available_widgets.DeviceBrowser) - - dock_area.new("queue").new(bec.gui.available_widgets.BECQueue) - # queue = dock_area.queue.BECQueue # give it a name - # text_box = dock_area.new().new(widget=bec.gui.available_widgets.TextBox) - # text_box.set_plain_text("Hello, World!") - # text_box.set_html_text("
This is an example of displaying HTML text.
") - - ########################### ### move in the X-ray Eye ########################### @@ -1311,7 +1292,7 @@ def detxeye_in(setdetzpos = 200, setdetypos = 213.6): detypos = dev.det_y.user_readback.get() print(f"detector position now: z = {detzpos}, y = {detypos}") - print(f"... moving to detector position: z = {setdetzpos}, y = {setdetypos}") + print(f"... moving to detector position: z = {setdetzpos}, y = {setdetypos}") ans = "n" ans = input("Is it ok to move detector in and up? y/n ") @@ -1355,7 +1336,7 @@ def detxeye_out(): def measure_samcam(zoom=1000): - import sys + import sys scinti_inpos = 36.4 # mm sc_rb = dev.diag_y.user_readback.get() if abs(scinti_inpos - sc_rb) > 0.3: @@ -1552,7 +1533,7 @@ def scan_window(wname="Scan", fit=True): w2 = dock_area.new(bec.gui.available_widgets.ScanControl) # Add signals to the WaveformWidget - w1.plot(device_x = dev.id_gap, device_y = dev.lu_bpmsum) + w1.plot(device_x = dev.id_gap, device_y = dev.lu_bpmsum) ###w1.plot(device_x='id_gap', device_y='bpm') ###dock2 = dock_area.new(name="motor_dock", widget="MotorMap",relative_to=nam, position="right") @@ -1562,9 +1543,9 @@ def scan_window(wname="Scan", fit=True): gstart = 4.5 gend = 5.0 resol = 0.003 # 3 um resol for gap scan - nsteps = round(gend-gstart)/resol - s = scans.line_scan(dev.id_gap, gstart, gend, steps = nsteps, exp_time = 0.1, relative = False) - + nsteps = round(gend-gstart)/resol + s = scans.line_scan(dev.id_gap, gstart, gend, steps = nsteps, exp_time = 0.1, relative = False) + ### if done, remove #w2.remove() ################################################ @@ -1576,21 +1557,21 @@ def scan_window(wname="Scan", fit=True): def readdist(filename): with open(filename, "r") as f: combined_data = np.loadtxt(f, delimiter=",") - + ind1 = 0 ind2 = 1 ind3 = 2 detdist = combined_data[:, ind1] bposx = combined_data[:, ind2] bposy = combined_data[:, ind3] - + plt.ion() plt.figure(1) plt.plot(detdist,bposx, ".") plt.title("Bpos X vs Detectordist") plt.xlabel("dist / mm") plt.ylabel("bpos X in det pix ") - + plt.figure(2) plt.plot(detdist,bposy, ".") plt.title("Bpos Y vs Detectordist") @@ -1598,7 +1579,7 @@ def readdist(filename): plt.ylabel("bpos Y in det pix ") plt.show() - return + return ################################################ ### flux cal @@ -1606,9 +1587,9 @@ def readdist(filename): def read_av(signal, avlen): #signal = dev.lu_bpmsum - + buf = [] - count =0 + count =0 while count < avlen: new_value = signal.read()[signal.name]['value'] #new_value = dev.lu_bpmsum.read()['lu_bpmsum']["value"] @@ -1620,36 +1601,36 @@ def read_av(signal, avlen): def fitpoly(x,y, order): from scipy.optimize import curve_fit - + coeff = np.polyfit(x, y, order) polynomial = np.poly1d(coeff) print("Polynomial coefficients of the fit fct: ", order, coeff) x_fit = np.linspace(min(x), max(x), 100) y_fit = polynomial(x_fit) - + plt.ion() plt.figure() plt.title("X10SA Fl cal") plt.ylabel("fl/bcubpmsum") plt.title("E / keV") - + plt.plot(x, y, "*") plt.plot(x_fit, y_fit, color="blue") - + def prepare_fluxmeas(): # check all slits read_mon() -# +# print("preparing devices ...") ans = "n" ans = input("Is it safe to move det in and up and BS down? y/n ") if ans == "y": detxeye_in() bs_in = d["bs_y"].positions["in"] - #umv(dev.bs_y, bs_in +1) + #umv(dev.bs_y, bs_in +1) return def fluxcal(start_en=13, stop_en=14, step =1): @@ -1657,7 +1638,7 @@ def fluxcal(start_en=13, stop_en=14, step =1): # tstart = time.perf_counter() # time logging avlen= 10 - + dat_en=[] dat_lubpm=[] dat_ssbpm=[] @@ -1670,7 +1651,7 @@ def fluxcal(start_en=13, stop_en=14, step =1): diode_inpos = dev.diag_y.user_parameter["i1"] diode_outpos = dev.diag_y.user_parameter["out"] for ei in range(start_en,stop_en+1, step): - bl_energy(ei*1000, plot = False) # in ev + bl_energy(ei*1000, plot = False) # in ev time.sleep(0.5) #lusum = dev.lu_bpmsum.read()["lu_bpmsum"]["value"] signal = dev.lu_bpmsum # "lu_bpmsum" @@ -1679,26 +1660,26 @@ def fluxcal(start_en=13, stop_en=14, step =1): #sssum = dev.ss_bpmsum.read()["ss_bpmsum"]["value"] signal = dev.ss_bpmsum # "ss_bpmsum" sssum = read_av(signal, avlen) - + #bcusum = dev.bcu_bpmsum.read()["bcu_bpmsum"]["value"] signal = dev.bcu_bpmsum #"bcu_bpmsum" bcusum = read_av(signal, avlen) - + #xi = dev.xidiode.read()["xidiode"]["value"] signal = dev.xidiode xi = read_av(signal, avlen) - + fluxi = flux_x10sa(xi, ei, silent =1) quot = fluxi/bcusum - + #i1diode = dev.i1.read()["i1"]["value"] - umv(dev.diag_y,diode_inpos) + umv(dev.diag_y,diode_inpos) signal = dev.i1 - i1diode = read_av(signal, avlen) + i1diode = read_av(signal, avlen) umv(dev.diag_y,diode_outpos) - + print(f'En {ei}, BCUSUM {bcusum}, xi {xi}, flux {fluxi}') dat_en.append(ei) @@ -1709,9 +1690,9 @@ def fluxcal(start_en=13, stop_en=14, step =1): dat_fluxi.append(fluxi) dat_quot_flbsum.append(quot) dat_i1.append(i1diode) - + combined = np.column_stack((dat_en, dat_lubpm,dat_ssbpm,dat_bcubpm,dat_xi,dat_fluxi,dat_quot_flbsum, dat_i1)) - + # open file for output timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S") dirname = "/sls/x10sa/config/commissioning/Data/" @@ -1722,13 +1703,13 @@ def fluxcal(start_en=13, stop_en=14, step =1): elapsed_time = time.perf_counter()-tstart - print(f"Runtime: {elapsed_time: .6f} seconds, i.e., {elapsed_time/60: .6f} min") + print(f"Runtime: {elapsed_time: .6f} seconds, i.e., {elapsed_time/60: .6f} min") # CLOSE AND MOVE BACK ############ # close shutter ! - + print("close the shutter !") - dev.bcu_shutter.put(0) - + dev.bcu_shutter.put(0) + # move beamstop back ! #umv(dev.bs_y, bs_in)