Script execution

This commit is contained in:
voulot_d
2017-01-13 16:45:43 +01:00
parent 5898eb0d5c
commit 72aad6cc5a

View File

@@ -1,67 +1,66 @@
import ch.psi.pshell.epics.ControlledVariable as ControlledVariable
if get_exec_pars().source == CommandSource.ui:
prefix = "SINSB04-RSYS"
station = "SINSB04"
else:
prefix = args[0] + "-RSYS"
station = 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")
nb = caget(prefix + ":SET-NUM-AVERAGE")
bpm_ch = caget(prefix + ":DBPM")
disp = caget(bpm_ch + ":DISPERSION")
energy0 = caget(bpm_ch + ":ENERGY")
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")
bpm_ch = caget(station + "-RSYS:DBPM")
disp = caget(bpm_ch + ":DISPERSION")
energy0 = caget(bpm_ch + ":ENERGY")
phase = ControlledVariable("Phase", prefix + ":SET-VSUM-PHASE-SIM", prefix + ":GET-VSUM-PHASE-SIM")
phase = ControlledVariable("Phase", station + "-RSYS:SET-VSUM-PHASE-SIM", station + "-RSYS:GET-VSUM-PHASE-SIM")
phase.config.minValue =-45.0
phase.config.maxValue = 360.0
phase.config.resolution = 0.5
phase.initialize()
V = Channel(prefix + ":GET-VSUM-AMPLT-SIM", type = 'd', alias='Amplitude Readback')
P = Channel(prefix + ":GET-KLY-POWER-SIM", type = 'd', alias='Power Readback')
x = Channel(bpm_ch + ":X1-SIMU", type = 'd', alias='BPM-X')
V = Channel(station + "-RSYS:GET-VSUM-AMPLT-SIM", type = 'd', alias='Amplitude Readback')
P = Channel(station + "-RSYS:GET-KLY-POWER-SIM", type = 'd', alias='Power Readback')
x = Channel(bpm_ch + ":X1-SIMU", type = 'd', alias='BPM-X')
caput(prefix + ":GET-FIT-PHASE-ARRAY", to_array([0.0],'d'))
caput(prefix + ":GET-FIT-ENERGY-ARRAY", to_array([0.0],'d'))
caput(prefix + ":GET-ONCREST-VSUM-PHASE", float('nan'))
caput(prefix + ":GET-ONCREST-VSUM-AMPLT", float('nan'))
caput(prefix + ":GET-ONCREST-E-GAIN", float('nan'))
caput(prefix + ":GET-ONCREST-KLY-POWER", float('nan'))
caput(prefix + ":CALC-VSUM-PHASE-OFFSET", float('nan'))
caput(prefix + ":CALC-VSUM-AMPLT-SCALE" , float('nan'))
caput(prefix + ":CALC-VOLT-POWER-SCALE" , float('nan'))
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'))
caput(station + "-RSYS:CALC-VSUM-PHASE-OFFSET", float('nan'))
caput(station + "-RSYS:CALC-VSUM-AMPLT-SCALE" , float('nan'))
caput(station + "-RSYS:CALC-VOLT-POWER-SCALE" , float('nan'))
#update the plot dynamically
arr_phase,arr_energy = [],[]
def after(rec):
global arrpos, arrval, disp, energy0
arr_phase.append(rec.positions[0])
arr_energy.append(rec.values[1]/1000.0/disp*energy0)
caput(prefix + ":GET-ENERGY-ARRAY", to_array(arr_energy,'d'))
caput(prefix + ":GET-PHASE-ARRAY", to_array(arr_phase,'d'))
arr_energy.append(rec.values[1].mean/1000.0/disp*energy0)
caput(station + "-RSYS:GET-ENERGY-ARRAY", to_array(arr_energy,'d'))
caput(station + "-RSYS:GET-PHASE-ARRAY", to_array(arr_phase, 'd'))
try:
Vb = create_averager(V, nb, lat)
xb = create_averager(x, nb, lat)
r = lscan(phase, [V, x], start, stop, step , latency=lat, after_read = after)
r = lscan(phase, [Vb, xb], start, stop, step , latency=lat, after_read = after)
rf_phase = r.getPositions(0)
energy = [val/1000.0/disp*energy0 for val in r.getReadable(1)]
caput(prefix + ":GET-ENERGY-ARRAY", to_array(energy,'d'))
caput(prefix + ":GET-PHASE-ARRAY", to_array(rf_phase,'d'))
caput(station + "-RSYS:GET-ENERGY-ARRAY", to_array(energy, 'd'))
caput(station + "-RSYS:GET-PHASE-ARRAY", to_array(rf_phase,'d'))
phase_fit_max = None
try:
(energy_max, angular_frequency, phase0, in_range, phase_fit_max, fit_x, fit_y) = hfit(energy , xdata = rf_phase)
except:
raise Exception("Fit failure")
caput(prefix + ":GET-ONCREST-VSUM-PHASE", phase_fit_max)
caput(prefix + ":GET-ONCREST-E-GAIN", energy_max)
caput(prefix + ":GET-FIT-PHASE-ARRAY", fit_x)
caput(prefix + ":GET-FIT-ENERGY-ARRAY", fit_y)
caput(station + "-RSYS:GET-ONCREST-VSUM-PHASE", phase_fit_max)
caput(station + "-RSYS:GET-ONCREST-E-GAIN", energy_max)
caput(station + "-RSYS:GET-FIT-PHASE-ARRAY", fit_x)
caput(station + "-RSYS:GET-FIT-ENERGY-ARRAY", fit_y)
phase_min, phase_max = min(rf_phase), max(rf_phase)
if not (phase_min <= phase_fit_max <= phase_max):
raise Exception("Fit maximum outside scan range")
@@ -69,8 +68,8 @@ try:
time.sleep(lat)
Ampl = V.read()
Power = P.read()
caput(prefix + ":GET-ONCREST-VSUM-AMPLT", Ampl)
caput(prefix + ":GET-ONCREST-KLY-POWER", Power)
caput(station + "-RSYS:GET-ONCREST-VSUM-AMPLT", Ampl)
caput(station + "-RSYS:GET-ONCREST-KLY-POWER", Power)
finally:
phase.close()
V.close()
@@ -85,12 +84,11 @@ phase_offset = 90 - phase_fit_max
amplitude_scale = energy_gain / Ampl
power_scale = Power / math.pow(Ampl,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)
caput(station + "-RSYS:CALC-VSUM-PHASE-OFFSET", phase_offset)
caput(station + "-RSYS:CALC-VSUM-AMPLT-SCALE" , amplitude_scale)
caput(station + "-RSYS:CALC-VOLT-POWER-SCALE" , power_scale)
set_return("\nEnergy Gain: " + str(energy_gain) + "\n" +
"Phase Offset: " + str(phase_offset) + "\n" +
"Amplitude Scale: " + str(amplitude_scale) + "\n" +
"Power Scale: " + str(power_scale))