Files
x07mb/script/test/OTF_cont_sample.py
2022-03-04 14:28:28 +01:00

134 lines
3.3 KiB
Python

#Number of cycles must be small otherwise generates a following error
def before_read(position, scan):
global total_time, steps,d_output
import time
if d_output ==1:
print('enter before_read')
start = time.time()
caput("X07MB-OP2:SMPL",1)
time.sleep(0.75)
if d_output ==1:
print "Waiting..."
cawait ("X07MB-OP2:SMPL-DONE",1, 10000)
print "Done"
t = time.time()-start
if d_output ==1:
print t
if t > (total_time/steps):
print "Before took too long, step = ", (total_time/steps)
if d_output ==1:
print('leave before_read')
# end before_read
print(t)
def after(record, scan):
pass
from mathutils import *
from plotutils import *
#setup_plotting(line_plots = [mca_1])
print('running')
# scan definition
x0=-0.84
x1=-0.875
cycles = 4
total_time=20.
# General definitions
Motor = ScanY
RBV = ScanY_RBV
d_output=0 # 1+ debugging output
# general preactions
caput("X07MB-OP2:START-CSMPL",0) # cont sampling off
time.sleep(0.1)
caput("X07MB-OP2:TOTAL-CYCLES",1)
time.sleep(0.1)
caput("X07MB-OP2:SMPL",1) # start one cycle to get system in defined state
time.sleep(0.1)
#cscan(ScanX, [keith_1] , 18, 18.3, 100, latency = 0.0, time = 2.0, before_read = before_read, after_read = None)
n_cycles=cycles+1
dwell=(n_cycles)*0.2 # time per step
steps=int(total_time/dwell)
speed=(x1-x0)/total_time
#caput(Motor.getChannelName()+".VBAS",speed*.5)
caput("X07MB-OP2:TOTAL-CYCLES",cycles)
print(' parameters for scan ')
print('range',x0,x1)
print('total time',total_time)
print('steps ',steps, ' cycles ' ,cycles)
print('speed',speed)
print(x0,x1,dwell,total_time,steps,speed,speed*0.5)
#ScanX.config.minSpeed = speed*.5
#ScanX.config.save()
# OTF
#ret = cscan(Motor, [keith_1,keith_2,keith_3,RBV] , x0, x1, steps=steps, latency =0.0, time = total_time, before_read = before_read, after_read = None, domain_axis="Time")
#ret = cscan(Motor, [keith_1,keith_2,keith_3,RBV] , x0, x1, steps=steps, latency =0.0, time = total_time, before_read = before_read, after_read = None)
# step by step
ret = lscan(Motor, [keith_1,keith_2, RBV] , x0, x1, steps, latency = 0.0, relative = False, passes = 1, zigzag = False, before_read = before_read, after_read = None, title = None)
#General postactions
caput(Motor.getChannelName()+'.VBAS',0.125)
caput("X07MB-OP2:TOTAL-CYCLES",5)
time.sleep(0.1)
caput("X07MB-OP2:START-CSMPL",1)
time.sleep(0.1)
caput("X07MB-OP2:SMPL",1)
# now plot the data
# read data from data set
y = ret.getReadable(1)
x = ret.getPositions(0)
# create processed tab
p = plot(y, xdata=x, title="Processed")[0]
#function = interpolate(y,x,"cubic")
d = deriv(y,x) # calc derivative
plot_function(p, interpolate(d,x,"cubic"), "Deriv", x)
p.setLegendVisible(True)
(normalization, mean_val, sigma) = fit_gaussian(y, x)
print (' ===================================== ')
print ('RESULT GAUISSIAN FIT')
print ('sigma',sigma)
print ('position',mean_val)
fitted_gaussian_function = Gaussian(normalization, mean_val, sigma)
plot_function(p, fitted_gaussian_function, "Fit", x)
p.addText(min(x), max(y), get_exec_pars().path, Color.RED)
#plots = get_plot_snapshots("Processed")
#p2 = plot(y, xdata=x, title="Processed_2")[0]
#plot_function(p2, interpolate(d,x,"cubic"), "Deriv", x)
plots = get_plot_snapshots("Processed")
print plots