Files
sf-op/script/RFscan/phase_scan_caqtdm.py
gobbo_a 8beeb7dbc7
2023-08-17 15:16:51 +02:00

163 lines
6.8 KiB
Python
Executable File

import ch.psi.pshell.epics.Positioner as Positioner
import ch.psi.pshell.epics.ChannelDouble as ChannelDouble
dry_run = False
do_elog = True
is_panel = get_exec_pars().source != CommandSource.ui
if is_panel:
station = args[0]
bpm_ch = args[1]
bpm_field = args[2]
else:
station = "SATCB01"
bpm_ch = "SATBD02-DBPM010"
bpm_field = "Y2"
#station = "S30CB13"
#bpm_ch = "SARCL02-DBPM110"
#bpm_field = "X1"
#station = "STEST01"
#bpm_ch = "SINBC02-DBPM140"
#bpm_field = "X1"
start = caget(station + "-RSYS:SET-SCAN-START")
stop = caget(station + "-RSYS:SET-SCAN-STOP")
step = caget(station + "-RSYS:SET-SCAN-STEP")
lat = caget(station + "-RSYS:SET-SCAN-WAIT-TIME")
nb = caget(station + "-RSYS:SET-NUM-AVERAGE")
disp = caget(bpm_ch + ":DISPERSION-OP")
def mbnd(bpm_ch):
return {
'SINLH02-DBPM210': 'SINLH02-MBND100',
'SINLH02-DBPM240': 'SINLH02-MBND100',
'SINBC02-DBPM140': 'SINBC02-MBND100',
'SINBC02-DBPM320': 'SINBC02-MBND100',
'S10BD01-DBPM020': 'S10DI01-MBND100',
'S10BC02-DBPM140': 'S10BC02-MBND100',
'S10BC02-DBPM320': 'S10BC02-MBND100',
'SARCL02-DBPM110': 'SARCL02-MBND100',
'SARCL02-DBPM220': 'SARCL02-MBND100',
'SARCL02-DBPM260': 'SARCL02-MBND100',
'SARCL02-DBPM330': 'SARCL02-MBND100',
'SARCL02-DBPM470': 'SARCL02-MBND100',
'SATBD02-DBPM010': 'SATBD01-MBND200'
}[bpm_ch]
p0 = caget(mbnd(bpm_ch) + ":P-READ")
energy0 = p0 - 0.511
A = energy0 / (disp * 1000)
B = energy0
phase = Positioner("Phase", station + "-RSYS:SET-VSUM-PHASE", station + "-RSYS:GET-VSUM-PHASE")
phase.config.minValue =-90.0
phase.config.maxValue = 360.0
phase.config.precision = 4
phase.config.resolution = 0.3
phase.config.rotation = True
phase.initialize()
V = ChannelDouble(station + " Amplitude", station + "-RSYS:GET-VSUM-AMPLT")
P = ChannelDouble(station + " Klystron Power", station + "-RSYS:GET-KLY-POWER-GATED")
V.initialize()
P.initialize()
if dry_run:
x = ChannelDouble("BPM-X averager", bpm_ch + ":X1-SIMU")
else:
x = ChannelDouble("BPM-X averager", bpm_ch + ":" + bpm_field)
x.initialize()
phase0 = phase.read() % 360.0
caput(station + "-RSYS:GET-FIT-PHASE-ARRAY", to_array([0.0],'d'))
caput(station + "-RSYS:GET-FIT-ENERGY-ARRAY", to_array([0.0],'d'))
caput(station + "-RSYS:GET-ONCREST-VSUM-PHASE", float('nan'))
caput(station + "-RSYS:GET-ONCREST-VSUM-AMPLT", float('nan'))
caput(station + "-RSYS:GET-ONCREST-E-GAIN", float('nan'))
caput(station + "-RSYS:GET-ONCREST-KLY-POWER", float('nan'))
#update the plot dynamically
arr_phase,arr_energy = [],[]
def after(rec):
global A, B
arr_phase.append(rec.positions[0])
arr_energy.append(A * rec.readables[0].mean + B)
caput(station + "-RSYS:GET-PHASE-ARRAY", to_array(arr_phase, 'd'))
caput(station + "-RSYS:GET-ENERGY-ARRAY", to_array(arr_energy,'d'))
#scan and plot
try:
phase.write(start)
time.sleep(5.0)
x_averager = create_averager(x, nb, lat)
r = lscan(phase, x_averager, start, stop, step, latency=lat, after_read = after)
rf_phase = r.getPositions(0)
if start < 0:
rf_phase = [((ph + 90) % 360) -90 for ph in rf_phase ]
energy = [A * val.mean + B for val in r.getReadable(0)]
caput(station + "-RSYS:GET-ENERGY-ARRAY", to_array(energy, 'd'))
caput(station + "-RSYS:GET-PHASE-ARRAY", to_array(rf_phase,'d'))
try:
run("CPython/wrapper")
(fit_amplitude, fit_phase_deg, fit_offset, ph_crest, fit_x, fit_y) = hfitoff(energy , rf_phase)
except:
raise Exception("Fit failure")
plt = plot(None,name="phase scan")[0]
if plt is not None:
plt.getSeries(0).setData(to_array(rf_phase,'d'), to_array(energy,'d'))
plt.getSeries(0).setPointSize(6)
plt.getSeries(0).setLinesVisible(False)
plt.addSeries(LinePlotSeries("fit"))
plt.getSeries(1).setData(fit_x, fit_y)
plt.getSeries(1).setPointsVisible(False)
plt.setLegendVisible(True)
ph_crest = ph_crest % 360
phase.write(ph_crest)
time.sleep(lat)
Ampl = V.read()
Power = P.read()
caput(station + "-RSYS:GET-ONCREST-VSUM-PHASE", ph_crest)
caput(station + "-RSYS:GET-ONCREST-E-GAIN", fit_amplitude)
caput(station + "-RSYS:GET-ONCREST-VSUM-AMPLT", Ampl)
caput(station + "-RSYS:GET-ONCREST-KLY-POWER", Power)
caput(station + "-RSYS:GET-FIT-PHASE-ARRAY", fit_x)
caput(station + "-RSYS:GET-FIT-ENERGY-ARRAY", fit_y)
finally:
phase.write(phase0)
phase.close()
V.close()
P.close()
x.close()
phase_corr = caget(station + "-RSYS:GET-VSUM-PHASE-OFFSET-CORR")
phase_offset = 90.0 - ph_crest - phase_corr
amplitude_scale = fit_amplitude / Ampl if Ampl != 0 else 0.0
power_scale = Power / fit_amplitude**2 if fit_amplitude != 0 else 0.0
caput(station + "-RSYS:SET-VSUM-PHASE-OFFSET-BASE-CALC", phase_offset)
caput(station + "-RSYS:SET-VSUM-AMPLT-SCALE-CALC", amplitude_scale)
caput(station + "-RSYS:SET-VOLT-POWER-SCALE-CALC", power_scale)
#Saving metadata
save_dataset ("experiment/Station" , station )
save_dataset ("scan 1/processed/Energy" , energy )
save_dataset ("scan 1/processed/Energy gain" , fit_amplitude )
save_dataset ("scan 1/processed/On-crest VS-phase" , ph_crest )
save_dataset ("scan 1/processed/VS-phase offset" , phase_offset )
save_dataset ("scan 1/processed/Amplitude scale" , amplitude_scale )
save_dataset ("scan 1/processed/Power scale" , power_scale )
save_dataset ("scan 1/processed/Dispersion" , disp )
set_attribute("scan 1/processed/Energy" , "Unit", "MeV" )
set_attribute("scan 1/processed/Energy gain" , "Unit", "MeV" )
set_attribute("scan 1/processed/On-crest VS-phase" , "Unit", "deg" )
set_attribute("scan 1/processed/VS-phase offset" , "Unit", "deg" )
set_attribute("scan 1/processed/Amplitude scale" , "Unit", "MV" )
set_attribute("scan 1/processed/Power scale" , "Unit", "MW/MV^2" )
set_attribute("scan 1/processed/Dispersion" , "Unit", "mm" )
#Elog entry
if do_elog:
title = "Phase scan " + station
log_msg = "Data file: " + get_exec_pars().path + "\n"
log_msg = log_msg + "On-crest VS phase: %0.2f" % ph_crest + " deg \n"
log_msg = log_msg + "Energy gain: %0.3f" % fit_amplitude + " MeV \n"
log_msg = log_msg + "VS-phase offset: %0.2f" % phase_offset + " deg \n"
log_msg = log_msg + "Amplitude scale: %0.3f" % amplitude_scale + " MV \n"
log_msg = log_msg + "Power scale: %0.7f" % power_scale + " MW/MV^2 \n"
log_msg = log_msg + "Dispersion: %0.4f" % disp + " mm"
attachments = get_plot_snapshots(size=(600,400))
elog(title, log_msg, attachments)