Files
x06da/script/local.py
2015-12-18 11:10:52 +01:00

133 lines
4.4 KiB
Python

###################################################################################################
# Deployment specific global definitions - executed after startup.py
###################################################################################################
dtor = math.pi /180.0
def A2E(t,h=1.0,k=1.0,l=1.0, deg =True, ln = False):
lncorr= 2.e-4 if ln else 0.0
d0 =2 * 5.43102 * (1.0-lncorr) / math.sqrt(h**2+k**2+l**2)
tt = (t * dtor) if (deg or (t>1.0)) else t
return 12.39842 / (d0*math.sin(tt))
def get_energy():
t2 = caget("X06DA-OP-MO1:ROX2.RBV",'d')
e = A2E(abs(t2))
print 'Energy [keV]:'+ str(e) + ' Wavelength [A]:' + str(12.39842/e)
return e
#Fitting
from mathutils import estimate_peak_indexes, fit_gaussians, create_fit_point_list, Gaussian
import java.awt.Color as Color
def fit(ydata, xdata = None):
if xdata is None:
xdata = frange(0, len(ydata), 1)
max_y= max(ydata)
index_max = ydata.index(max_y)
max_x= xdata[index_max]
print "Max index:" + str(index_max),
print " x:" + str(max_x),
print " y:" + str(max_y)
gaussians = fit_gaussians(ydata, xdata, [index_max,])
(norm, mean, sigma) = gaussians[0]
p = plot([ydata],["data"],[xdata], context="Fit" )[0]
fitted_gaussian_function = Gaussian(norm, mean, sigma)
scale_x = [float(min(xdata)), float(max(xdata)) ]
points = max((len(xdata)+1), 100)
resolution = (scale_x[1]-scale_x[0]) / points
fit_y = []
fit_x = frange(scale_x[0],scale_x[1],resolution, True)
for x in fit_x:
fit_y.append(fitted_gaussian_function.value(x))
p.addSeries(LinePlotSeries("fit"))
p.getSeries(1).setData(fit_x, fit_y)
print "---"
print mean, xdata[index_max], abs(mean - xdata[index_max])
print scale_x, (scale_x[0] + scale_x[1])/2, abs((scale_x[0] + scale_x[1])/2)
print abs(mean - xdata[index_max]) < abs((scale_x[0] + scale_x[1])/2)
if abs(mean - xdata[index_max]) < abs((scale_x[0] + scale_x[1])/2):
print "Mean -> " + str(mean)
p.addMarker(mean, None, "Mean="+str(round(norm,2)), Color.MAGENTA.darker())
return (norm, mean, sigma)
else:
p.addMarker(max_x, None, "Max="+str(round(max_x,2)), Color.GRAY)
print "Invalid gaussian fit: " + str(mean)
return (None, None, None)
#fit([1,2,3, 3,3,3,4,5,6,12,33,23,15,3,2,1, 1, 1, 1, 1])
#def rock(axis = None):
# from rock import rock
# rock()
# #run("rock", {"rock_axis":None})
def rock(axis = th1, ffail = False, tt = 0.2, seti0 = False, dx = None, noref = False):
"""
"""
if axis is None:
axis = th1
if noref == False:
caput('X06DA-OP-MO:MONOBEAM', -1 )
e = get_energy()
if dx is None:
dx = 7.5e-2/e
# reinforce theta1 and 2 setting (setting could be RDBD 0.0001 and RTRY 1 when seteq does not finish nicely)
# put 'Retry deadband' to 0.00004 and 'Retries Max' to 5
time.sleep(0.2)
caput('X06DA-OP-MO1:ROX1.RDBD',0.00004)
caput('X06DA-OP-MO1:ROX2.RDBD',0.00004)
caput('X06DA-OP-MO1:ROX1.RTRY',5)
caput('X06DA-OP-MO1:ROX2.RTRY',5)
#time stamp
print time.strftime('%X %x')
#MVR,axis,-dx
#SCAN,axis,0,2*dx,20,/fit,/centre,SILENT = silent,FFAIL = ffail,time=tt,/deriv,$
#data=d
axis_pos = axis.read()
result = lscan(axis, mono_beam, -dx, dx, 20, latency = 0.3, relative = True, context = None, before_read = None, after_read = None)
(ydata, xdata) = (result.getReadable(0), result.getPositions(0))
(norm, mean, sigma) = fit(ydata, xdata)
if (mean is not None) and (mean <= (axis_pos + dx)) and (mean >= (axis_pos - dx)):
axis.move(mean)
else:
print 'fit failed - centering on maximum'
max_x= xdata[ydata.index(max(ydata))]
axis.move(max_x)
if seti0 and not ffail:
time.sleep(2)
run("setI0")
#add after_rock BPM1:SUM as reference for monitoring monochromator thermal drift
if (noref==False) and not ffail:
time.sleep(3) # wait a bit until mono theta finish moving
br = mono_beam.read()
caput('X06DA-OP-MO:MONOBEAM', br)
print 'rock_success new mono beam at ' + str(br) + ' at' + str(e) + ' keV'
#from rock import rock
def fbm():
"""
"""
run("fbm")
def rocknroll():
"""
"""
run("rocknroll")