Files
x11ma/script/Users/PEEM/Armin/Functions/SlitCalib.py
gac-x11ma 1d75bc4535
2023-07-18 11:00:54 +02:00

68 lines
2.0 KiB
Python

###################################################################################################
# Function fitting and peak search with mathutils.py
###################################################################################################
from mathutils import fit_polynomial
from mathutils import PolynomialFunction
# Input parameters (example)
#Slit = exit_slit
#Slit_offset = exit_slit_offset
#detector = keithley_1a
#start = 30.0
#end = -20.0
#step_size = 1.0
#final_pos = 20.0
#run('beamline_init/with_x-rays/functions/SlitCalib.py',{"Slit":exit_slit, "Slit_offset":exit_slit_offset, "detector":detector, "start":30, "end":-20.0, "step_size":1.0, "final_pos":20.0})
# Execute the scan
result=lscan(Slit, detector, start, end, float(step_size))
readable_ = result[detector]
positions_ = result[Slit]
print positions_
readable=[]
positions=[]
# execute the fit
fit_threshold=readable_[0]*0.05
#filter the positions with analog values above the threshold
for i in range(8,len(positions_),1):
if(readable_[i]<fit_threshold):
readable.append(readable_[i])
positions.append(positions_[i])
pars_polynomial = (a0, a1) = fit_polynomial(readable, positions,1)
fitted_polynomial_function = PolynomialFunction(pars_polynomial)
print pars_polynomial
resolution = step_size/100
fit_polinomial = []
# from the fit function calculate the values at the positions
for x in frange(end,start,resolution, True):
fit_polinomial.append(fitted_polynomial_function.value(x))
x = frange(end, start+resolution, resolution)
# crate plots
plots = plot([readable_, fit_polinomial] , ["data", "polinomial"], xdata = [positions_,x], title="Data")
p = plot(None,name="Data 1")[0]
p.addSeries(LinePlotSeries("Data2"))
p.getSeries(0).setData(positions_, readable_)
p.getSeries(1).setData(x, fit_polinomial)
#calculate the position where the line crosses zero
k=-pars_polynomial[0]/pars_polynomial[1]
print k
#change the offset
#offset=Slit_offset.read()
#Slit_offset.write(offset+k)
Slit.write(final_pos)