Files
sf-op/script/RFscan/phase_scan_caqtdm_dv.py
2016-06-29 07:57:38 +02:00

73 lines
2.2 KiB
Python

import ch.psi.pshell.epics.ControlledVariable as ControlledVariable
prefix = args[0]
start = caget(prefix + ":SET-SCAN-START")
stop = caget(prefix + ":SET-SCAN-STOP")
step = caget(prefix + ":SET-SCAN-STEP")
lat = caget(prefix + ":SET-SCAN-WAIT-TIME")
bpm_ch = caget(prefix + ":BPM")
print "Prefix = ", prefix
print "Start = ", start
print "Stop = ", stop
print "Step = ", step
print "Latency = ", latency
print "BPM = ", bpm_ch
rf_phase_var = ControlledVariable("Phase", prefix + ":SET-VSUM-PHASE", prefix + ":GET-VSUM-PHASE")
rf_phase_var.config.minValue =-180.0
rf_phase_var.config.maxValue = 180.0
rf_phase_var.config.resolution = 0.001
rf_phase_var.initialize()
rf_ampl_rbk = Channel(prefix + ":GET-VSUM-AMPLT", type = 'd', alias='Amplitude Readback')
bpm_x = Channel(bpm_ch, type = 'd', alias='BPM-X')
#TODO: update the plot dynamically
def after(rec):
pass
try:
r = lscan(rf_phase_var, [rf_ampl_rbk, bpm_x], start, stop, step , latency=lat, after_read = after)
phase = r.getPositions(0)
ampl = r.getReadable(0)
x = r.getReadable(1)
#plot(r.getReadable(1), xdata = r.getPositions(0), title = "data")
phase_fit_max = None
try:
(amplitude, angular_frequency, phase, phase_fit_max) = hfit(x , xdata = phase)
except:
raise Exception("Fit failure")
start,end = min(phase), max(phase)
if not (start <= phase_fit_max <= end):
raise Exception("Fit maximum outside scan range")
rf_phase_var.write(phase_fit_max)
time.sleep(lat)
a0 = rf_ampl_readback.read()
power = 1.0 #TODO
finally:
rf_phase_var.close()
rf_ampl_rbk.close()
print ("------------------------------------")
print ("Valid fit")
energy_gain = amplitude
phase_offset = - phase_fit_max
amplitude_scale = energy_gain / a0
power_scale = power / math.pow(a0,2)
caput(prefix + ":CALC-VSUM-PHASE-OFFSET", phase_offset)
caput(prefix + ":CALC-VSUM-AMPLT-SCALE", amplitude_scale)
caput(prefix + ":CALC-VOLT-POWER-SCALE", power_scale)
set_return("Energy Gain: " + str(energy_gain) +
"\nPhase Offset: " + str(phase_offset) +
"\nAmplitude Scale: " + str(amplitude_scale) +
"\nPower Scale: " + str(power_scale))