Files
sf-op/script/RFscan/SchottkyScan.py
2017-06-19 11:07:40 +02:00

95 lines
3.3 KiB
Python

import ch.psi.pshell.epics.Positioner as Positioner
import ch.psi.pshell.epics.ChannelDouble as ChannelDouble
dry_run = False
do_elog = True
if get_exec_pars().source == CommandSource.ui:
start = -180.0
stop = 180.0
step = 5.0
nb = 1
lat = 0.100
plt = None
else:
start = args[0]
stop = args[1]
step = args[2]
nb = int(args[3])
lat = args[4]
plt = args[5]
if plt is not None:
plt.setStyle(plt.Style.ErrorY)
plt.addSeries(LinePlotErrorSeries("Values"))
plt.getAxis(plt.AxisId.X).setLabel("Gun Beam Phase (deg)")
plt.getAxis(plt.AxisId.Y).setLabel("SINEG01-DICT215:B1_CHARGE")
if dry_run:
bph = Positioner("Beam phase", "SINEG01-RSYS:SET-BEAM-PHASE-SIM", "SINEG01-RSYS:SET-BEAM-PHASE-SIM")
rph = ChannelDouble('RF phase', "SINEG01-RSYS:SET-VSUM-PHASE-SIM")
q = ChannelDouble('ICT-Q', "SINEG01-DICT215:B1_CHARGE-SIM")
q.initialize()
q.monitored=True
else:
bph = Positioner("Beam phase", "SINEG01-RSYS:SET-BEAM-PHASE", "SINEG01-RSYS:GET-BEAM-PHASE")
rph = ChannelDouble('RF phase', "SINEG01-RSYS:SET-VSUM-PHASE")
st = Stream("ICTstream", dispatcher)
q = st.addScalar("Charge", "SINEG01-DICT215:B1_CHARGE", 1, 0)
st.initialize()
st.start()
st.waitValueChange(10000)
bph.config.minValue =-180.0
bph.config.maxValue = 180.0
bph.config.precision = 3
bph.config.rotation = True
bph.config.resolution = 1.0
bph.config.save()
bph.initialize()
rph.initialize()
rph.monitored=True
rph0 = rph.read()
#Record callback: uptate of output plot
def after_sample(record, scan):
if plt is not None:
plt.getSeries(0).appendData(record.positions[0], record.values[1].mean, record.values[1].stdev)
try:
rph_averager = create_averager(rph, nb, 0.1) # Set polling time to -1 for BS data to get all messages
q_averager = create_averager(q, nb, 0.1)
q_averager.monitored=True
r = lscan(bph, (rph_averager, q_averager), start, stop, step, latency=lat, after_read = after_sample)
beamphase = r.getPositions(0)
rfphase = [val.mean for val in r.getReadable(0)]
rfphaserms = [val.stdev for val in r.getReadable(0)]
charge = [val.mean for val in r.getReadable(1)]
chargerms = [val.stdev for val in r.getReadable(1)]
finally:
rph.write(rph0)
bph.close()
rph.close()
q.close()
#Setting the return value
index_max = charge.index(max(charge))
bph_ref_guess = beamphase[index_max] - 80
rph_ref_guess = rfphase[index_max] - 80
if do_elog:
if get_option("Generated data file:\n" + get_exec_pars().path +"\n\n" + "Save to ELOG?", "YesNo") == "Yes":
log_msg = "Data file: " + get_exec_pars().path + "\n"
log_msg = log_msg + "Beam phase reference: %0.1f" % bph_ref_guess + "deg \n"
log_msg = log_msg + "RF phase reference: %0.1f" % rph_ref_guess + "deg \n"
attachments = []
if plt is not None:
sleep(0.1) #Give some time to plot to be finished - it is not sync with acquisition
file_name = os.path.abspath(get_context().setup.getContextPath() + "/SchottkyScanPlot.png")
plt.saveSnapshot(file_name, "png")
attachments = [file_name,]
elog("Schottky scan", log_msg, attachments)
set_return([bph_ref_guess, rph_ref_guess])