Startup
This commit is contained in:
@@ -10,11 +10,14 @@
|
||||
|
||||
import datetime
|
||||
|
||||
mode = "camtool" # "camtool", "bpm" or "direct"
|
||||
is_panel = get_exec_pars().source != CommandSource.ui #Must be checked before callin "run"
|
||||
|
||||
camera_name = "SINEG01-DSCR190"
|
||||
use_good_region=False
|
||||
do_elog = True
|
||||
|
||||
if get_exec_pars().source == CommandSource.ui:
|
||||
if not is_panel:
|
||||
source = "direct" # "camtool", "bpm" or "direct"
|
||||
I1 = 100.0
|
||||
I2 = 105.0
|
||||
dI = 1.0
|
||||
@@ -22,27 +25,45 @@ if get_exec_pars().source == CommandSource.ui:
|
||||
plot_image = False
|
||||
number_images = 5
|
||||
use_background = True
|
||||
multiple_background = True
|
||||
number_backgrounds = 5
|
||||
do_elog = False
|
||||
centroid_excursion_plot = True
|
||||
else:
|
||||
centroid_excursion_plot = False
|
||||
multiple_background = False
|
||||
number_backgrounds = 5
|
||||
plots = get_plots(title = "Centroid excursion")
|
||||
if len(plots)==0:
|
||||
centroid_plot = plot(None, title = "Centroid excursion")[0]
|
||||
centroid_plot.clear()
|
||||
else:
|
||||
centroid_plot = plots[0]
|
||||
|
||||
|
||||
kill_camtool()
|
||||
check_camtool()
|
||||
centroid_plot_index = 1 if ((len(centroid_plot.getAllSeries()) == 0) or (not globals().has_key("centroid_plot_index"))) else (centroid_plot_index + 1)
|
||||
print "Plot index: ", centroid_plot_index
|
||||
centroid_plot.setLegendVisible(True)
|
||||
centroid_plot.setStyle(centroid_plot.Style.ErrorXY)
|
||||
centroid_plot.getAxis(centroid_plot.AxisId.Y).setLabel("")
|
||||
centroid_plot.getAxis(centroid_plot.AxisId.X).setLabel("Centroid Excursion")
|
||||
scan_series = LinePlotErrorSeries(str(centroid_plot_index))
|
||||
centroid_plot.addSeries(scan_series)
|
||||
scan_series.setLinesVisible(False)
|
||||
scan_series.setPointSize(4)
|
||||
|
||||
#Simulation
|
||||
camera_name = "SLG-LCAM-C041"
|
||||
def laser_on():
|
||||
print "Laser On"
|
||||
def laser_off():
|
||||
print "Laser Off"
|
||||
def switch_off_magnets(magnets = None):
|
||||
pass
|
||||
add_device(DummyPositioner("gun_solenoid"), True)
|
||||
do_elog = False
|
||||
|
||||
laser_was_on = is_laser_on()
|
||||
original_gun_solenoid = gun_solenoid.read()
|
||||
|
||||
|
||||
original_gun_solenoid = gun_solenoid.read()
|
||||
multiple_background = multiple_background and use_background
|
||||
print "Parameters: ", I1, I2, dI, settling_time, plot_image, number_images, use_background, multiple_background, number_backgrounds
|
||||
|
||||
plot_name = datetime.datetime.fromtimestamp(time.time()).strftime('%H%M%S')
|
||||
|
||||
|
||||
if mode == "camtool":
|
||||
if source == "camtool":
|
||||
check_camtool()
|
||||
if use_background:
|
||||
laser_off()
|
||||
if not multiple_background:
|
||||
@@ -50,20 +71,28 @@ if mode == "camtool":
|
||||
camtool.grabBackground(camera_name, number_backgrounds)
|
||||
camtool.start(camera_name, 0, use_background, None, 0.0, None)
|
||||
else:
|
||||
if mode == "bpm":
|
||||
if source == "bpm":
|
||||
run("Devices/BpmStats")
|
||||
add_device(BpmStats("image_stats", camera_name), True)
|
||||
else:
|
||||
add_device(ImageStats(PsiCamera("image_stats", camera_name)), True)
|
||||
multiple_background = False
|
||||
use_background = False
|
||||
multiple_background = False
|
||||
use_background = False
|
||||
else:
|
||||
run("Devices/ImageStats")
|
||||
add_device(ImageStats("image_stats", camera_name), True)
|
||||
add_device(image_stats.source, True)
|
||||
image_stats.enableBackground(use_background)
|
||||
if use_background:
|
||||
laser_off()
|
||||
if not multiple_background:
|
||||
image_stats.grabBackground(number_backgrounds)
|
||||
image_stats.setNumberOfImages(max(number_images,1))
|
||||
|
||||
#switch_off_magnets()
|
||||
|
||||
# add here gun phase setting see wiki page
|
||||
|
||||
def before_sample():
|
||||
if mode == "camtool":
|
||||
def before_sample(position, scan):
|
||||
if source == "camtool":
|
||||
if multiple_background:
|
||||
camtool.stop()
|
||||
camtool.grabBackground(camera_name, number_backgrounds)
|
||||
@@ -71,9 +100,20 @@ def before_sample():
|
||||
laser_on()
|
||||
wait_camtool_message(number_images) #Wait filing the averager cache
|
||||
else:
|
||||
if multiple_background:
|
||||
image_stats.grabBackground(number_backgrounds)
|
||||
laser_on()
|
||||
image_stats.update()
|
||||
|
||||
def after_sample():
|
||||
def after_sample(record, scan):
|
||||
x = record.values[0]
|
||||
y = record.values[1]
|
||||
stdev_x = record.values[2]
|
||||
stdev_y = record.values[3]
|
||||
if source == "camtool":
|
||||
#x, y, stdev_x, stdev_y = x.mean, y.mean, stdev_x.mean, stdev_y.mean
|
||||
x, y, stdev_x, stdev_y = x.mean, y.mean, x.stdev, y.stdev
|
||||
scan_series.appendData(x, y, abs(stdev_x), abs(stdev_y));
|
||||
if multiple_background:
|
||||
laser_off()
|
||||
|
||||
@@ -82,37 +122,60 @@ if not multiple_background:
|
||||
laser_on()
|
||||
|
||||
try:
|
||||
if mode != "camtool":
|
||||
if source != "camtool":
|
||||
sensors = [image_stats.com_x_mean, image_stats.com_y_mean, image_stats.com_x_stdev, image_stats.com_y_stdev]
|
||||
if plot_image and (source == "direct"):
|
||||
sensors.append(image_stats.source.getDataMatrix())
|
||||
else:
|
||||
sensors = get_camtool_stats(number_images, good_region=use_good_region)
|
||||
|
||||
|
||||
sensors = get_camtool_stats(number_images, good_region=use_good_region)
|
||||
if plot_image:
|
||||
sensors.append(camtool.getDataMatrix())
|
||||
r = lscan(gun_solenoid, sensors , I1, I2, dI, settling_time, before_read = before_sample, after_read = after_sample)
|
||||
finally:
|
||||
if mode == "camtool": camtool.stop()
|
||||
if source == "camtool":
|
||||
camtool.stop()
|
||||
else:
|
||||
image_stats.stop()
|
||||
gun_solenoid.write(original_gun_solenoid)
|
||||
if laser_was_on:
|
||||
laser_on()
|
||||
else:
|
||||
laser_off()
|
||||
|
||||
|
||||
# take the result of the scan and generate convex hull plot
|
||||
if centroid_excursion_plot:
|
||||
(hx,hy)=add_convex_hull_plot ("Centroid excursion", r.getReadable(0),r.getReadable(1), plot_name)
|
||||
else:
|
||||
(hx,hy)= convex_hull(x=to_array(r.getReadable(0), 'd'), y=to_array(r.getReadable(1),'d'))
|
||||
hx.append(hx[0]); hy.append(hy[0])
|
||||
(hx,hy)= convex_hull(x=to_array(r.getReadable(0), 'd'), y=to_array(r.getReadable(1),'d'))
|
||||
hx.append(hx[0]); hy.append(hy[0])
|
||||
hx = Convert.toDouble(hx)
|
||||
hy = Convert.toDouble(hy)
|
||||
hull = LinePlotErrorSeries(scan_series.name + "H", scan_series.color);
|
||||
centroid_plot.addSeries(hull);
|
||||
hull.setData(hx, hy);
|
||||
|
||||
#Include metadata do hdf5
|
||||
path = get_exec_pars().scanPath
|
||||
|
||||
set_attribute(path, "Settling time", settling_time)
|
||||
set_attribute(path, "Images", number_images)
|
||||
set_attribute(path, "Background enabled", use_background)
|
||||
set_attribute(path, "Background multiple", multiple_background)
|
||||
set_attribute(path, "Background images", number_backgrounds)
|
||||
set_attribute(path, "Plot index", centroid_plot_index)
|
||||
|
||||
# save the entry in the logbook
|
||||
gsa_log_msg = "Data file: " + get_exec_pars().path
|
||||
gsa_log_msg = gsa_log_msg + "\nImages: " + str(number_images)
|
||||
gsa_log_msg = gsa_log_msg + "\nBackground: enabled=" + str(use_background) + " multiple=" + str(multiple_background) + " number=" + str(number_backgrounds)
|
||||
gsa_log_msg = gsa_log_msg + "\n\n" + r.print()
|
||||
if do_elog:
|
||||
elog("Gun solenoid current scan", gsa_log_msg , get_plot_snapshots())
|
||||
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
|
||||
log_msg = log_msg + "\nI1: " + str(I1)
|
||||
log_msg = log_msg + "\nI2: " + str(I2)
|
||||
log_msg = log_msg + "\ndI: " + str(dI)
|
||||
log_msg = log_msg + "\nSettling time: " + str(settling_time)
|
||||
log_msg = log_msg + "\nImages: " + str(number_images)
|
||||
log_msg = log_msg + "\nBackground: enabled=" + str(use_background) + " multiple=" + str(multiple_background) + " number=" + str(number_backgrounds)
|
||||
log_msg = log_msg + "\nPlot index: " + str(centroid_plot_index)
|
||||
log_msg = log_msg + "\n\n" + r.print()
|
||||
|
||||
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() + "/centroid_excursion.png")
|
||||
centroid_plot.saveSnapshot(file_name , "png")
|
||||
elog("Gun solenoid alignment", log_msg, [file_name,])
|
||||
|
||||
set_return([r, hx, hy])
|
||||
|
||||
@@ -10,11 +10,14 @@
|
||||
|
||||
import datetime
|
||||
|
||||
mode = "camtool" # "camtool", "bpm" or "direct"
|
||||
is_panel = get_exec_pars().source != CommandSource.ui #Must be checked before callin "run"
|
||||
|
||||
camera_name = "SINEG01-DSCR190"
|
||||
use_good_region=False
|
||||
do_elog = True
|
||||
|
||||
if get_exec_pars().source == CommandSource.ui:
|
||||
if not is_panel:
|
||||
source = "camtool" # "camtool", "bpm" or "direct"
|
||||
phi1= 95.0
|
||||
phi2 = 100.0
|
||||
dphi = 1.0
|
||||
@@ -23,23 +26,46 @@ if get_exec_pars().source == CommandSource.ui:
|
||||
number_images = 5
|
||||
use_background = True
|
||||
multiple_background = False
|
||||
number_backgrounds = 5
|
||||
do_elog = False
|
||||
centroid_excursion_plot = True
|
||||
else:
|
||||
centroid_excursion_plot = False
|
||||
number_backgrounds = 5
|
||||
plots = get_plots(title = "Centroid excursion")
|
||||
if len(plots)==0:
|
||||
centroid_plot = plot(None, title = "Centroid excursion")[0]
|
||||
centroid_plot.clear()
|
||||
else:
|
||||
centroid_plot = plots[0]
|
||||
|
||||
kill_camtool()
|
||||
check_camtool()
|
||||
|
||||
centroid_plot_index = 1 if ((len(centroid_plot.getAllSeries()) == 0) or (not globals().has_key("centroid_plot_index"))) else (centroid_plot_index + 1)
|
||||
print "Plot index: ", centroid_plot_index
|
||||
centroid_plot.setLegendVisible(True)
|
||||
centroid_plot.setStyle(centroid_plot.Style.ErrorXY)
|
||||
centroid_plot.getAxis(centroid_plot.AxisId.Y).setLabel("")
|
||||
centroid_plot.getAxis(centroid_plot.AxisId.X).setLabel("Centroid Excursion")
|
||||
scan_series = LinePlotErrorSeries(str(centroid_plot_index))
|
||||
centroid_plot.addSeries(scan_series)
|
||||
scan_series.setLinesVisible(False)
|
||||
scan_series.setPointSize(4)
|
||||
|
||||
#Testing
|
||||
camera_name = "SLG-LCAM-C041"
|
||||
def laser_on():
|
||||
print "Laser On"
|
||||
def laser_off():
|
||||
print "Laser Off"
|
||||
def switch_off_magnets(magnets = None):
|
||||
pass
|
||||
add_device(DummyPositioner("gun_phase"), True)
|
||||
do_elog = False
|
||||
|
||||
laser_was_on = is_laser_on()
|
||||
original_phase = gun_phase.read()
|
||||
multiple_background = multiple_background and use_background
|
||||
print "Parameters: ", phi1, phi2, dphi, settling_time, plot_image, number_images, use_background, multiple_background, number_backgrounds
|
||||
|
||||
plot_name = datetime.datetime.fromtimestamp(time.time()).strftime('%H%M%S')
|
||||
|
||||
if mode == "camtool":
|
||||
if source == "camtool":
|
||||
#kill_camtool()
|
||||
check_camtool()
|
||||
if use_background:
|
||||
laser_off()
|
||||
if not multiple_background:
|
||||
@@ -47,20 +73,28 @@ if mode == "camtool":
|
||||
camtool.grabBackground(camera_name, number_backgrounds)
|
||||
camtool.start(camera_name, 0, use_background, None, 0.0, None)
|
||||
else:
|
||||
if mode == "bpm":
|
||||
if source == "bpm":
|
||||
run("Devices/BpmStats")
|
||||
add_device(BpmStats("image_stats", camera_name), True)
|
||||
multiple_background = False
|
||||
use_background = False
|
||||
else:
|
||||
add_device(ImageStats(PsiCamera("image_stats", camera_name)), True)
|
||||
multiple_background = False
|
||||
use_background = False
|
||||
run("Devices/ImageStats")
|
||||
add_device(ImageStats("image_stats", camera_name), True)
|
||||
add_device(image_stats.source, True)
|
||||
image_stats.enableBackground(use_background)
|
||||
if use_background:
|
||||
laser_off()
|
||||
if not multiple_background:
|
||||
image_stats.grabBackground(number_backgrounds)
|
||||
image_stats.setNumberOfImages(max(number_images,1))
|
||||
|
||||
#switch_off_magnets()
|
||||
|
||||
# add here gun phase setting see wiki page
|
||||
|
||||
def before_sample():
|
||||
if mode == "camtool":
|
||||
def before_sample(position, scan):
|
||||
if source == "camtool":
|
||||
if multiple_background:
|
||||
camtool.stop()
|
||||
camtool.grabBackground(camera_name, number_backgrounds)
|
||||
@@ -68,26 +102,42 @@ def before_sample():
|
||||
laser_on()
|
||||
wait_camtool_message(number_images) #Wait filing the averager cache
|
||||
else:
|
||||
if multiple_background:
|
||||
image_stats.grabBackground(number_backgrounds)
|
||||
laser_on()
|
||||
image_stats.update()
|
||||
|
||||
def after_sample():
|
||||
def after_sample(record, scan):
|
||||
x = record.values[0]
|
||||
y = record.values[1]
|
||||
stdev_x = record.values[2]
|
||||
stdev_y = record.values[3]
|
||||
if source == "camtool":
|
||||
#x, y, stdev_x, stdev_y = x.mean, y.mean, stdev_x.mean, stdev_y.mean
|
||||
x, y, stdev_x, stdev_y = x.mean, y.mean, x.stdev, y.stdev
|
||||
scan_series.appendData(x, y, abs(stdev_x), abs(stdev_y));
|
||||
if multiple_background:
|
||||
laser_off()
|
||||
laser_off()
|
||||
|
||||
r = None
|
||||
if not multiple_background:
|
||||
laser_on()
|
||||
|
||||
try:
|
||||
if mode != "camtool":
|
||||
if source != "camtool":
|
||||
sensors = [image_stats.com_x_mean, image_stats.com_y_mean, image_stats.com_x_stdev, image_stats.com_y_stdev]
|
||||
if plot_image and (source == "direct"):
|
||||
sensors.append(image_stats.source.getDataMatrix())
|
||||
else:
|
||||
sensors = get_camtool_stats(number_images, good_region=use_good_region)
|
||||
if plot_image:
|
||||
sensors.append(camtool.getDataMatrix())
|
||||
r = lscan(gun_phase, sensors , phi1, phi2, dphi, settling_time, before_read = before_sample, after_read = after_sample)
|
||||
finally:
|
||||
if mode == "camtool": camtool.stop()
|
||||
if source == "camtool":
|
||||
camtool.stop()
|
||||
else:
|
||||
image_stats.stop()
|
||||
gun_phase.write(original_phase)
|
||||
if laser_was_on:
|
||||
laser_on()
|
||||
@@ -95,23 +145,45 @@ else:
|
||||
laser_off()
|
||||
|
||||
# take the result of the scan and generate convex hull plot
|
||||
if centroid_excursion_plot:
|
||||
(hx,hy)=add_convex_hull_plot ("Centroid excursion", r.getReadable(0),r.getReadable(1), plot_name)
|
||||
else:
|
||||
(hx,hy)= convex_hull(x=to_array(r.getReadable(0), 'd'), y=to_array(r.getReadable(1),'d'))
|
||||
hx.append(hx[0]); hy.append(hy[0])
|
||||
(hx,hy)= convex_hull(x=to_array(r.getReadable(0), 'd'), y=to_array(r.getReadable(1),'d'))
|
||||
hx.append(hx[0]); hy.append(hy[0])
|
||||
hx = Convert.toDouble(hx)
|
||||
hy = Convert.toDouble(hy)
|
||||
hull = LinePlotErrorSeries(scan_series.name + "H", scan_series.color);
|
||||
centroid_plot.addSeries(hull);
|
||||
hull.setData(hx, hy);
|
||||
|
||||
#Include metadata do hdf5
|
||||
path = get_exec_pars().scanPath
|
||||
set_attribute(path, "phi1", phi1)
|
||||
set_attribute(path, "phi2", phi2)
|
||||
set_attribute(path, "dphi", dphi)
|
||||
set_attribute(path, "Settling time", settling_time)
|
||||
set_attribute(path, "Images", number_images)
|
||||
set_attribute(path, "Background enabled", use_background)
|
||||
set_attribute(path, "Background multiple", multiple_background)
|
||||
set_attribute(path, "Background images", number_backgrounds)
|
||||
set_attribute(path, "Plot index", centroid_plot_index)
|
||||
|
||||
print "Done"
|
||||
# save the entry in the logbook
|
||||
gsa_log_msg = "Data file: " + get_exec_pars().path
|
||||
gsa_log_msg = gsa_log_msg + "\nImages: " + str(number_images) + " Background: enabled=" + str(use_background) + " multiple=" + str(multiple_background) + " number=" + str(number_backgrounds)
|
||||
gsa_log_msg = gsa_log_msg + "\nPhase range: " + str(phi1) + " to " + str(phi2)
|
||||
gsa_log_msg = gsa_log_msg + "\nLaser position on cathod: " + str(caget("SLG-LCAM-C103:FIT-XPOS")) + "/" + str(caget("SLG-LCAM-C103:FIT-YPOS"))
|
||||
gsa_log_msg = gsa_log_msg + "\n\n" + r.print()
|
||||
if do_elog:
|
||||
elog("Laser alignment with phase scan", gsa_log_msg , get_plot_snapshots())
|
||||
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
|
||||
log_msg = log_msg + "\nI1: " + str(I1)
|
||||
log_msg = log_msg + "\nI2: " + str(I2)
|
||||
log_msg = log_msg + "\ndI: " + str(dI)
|
||||
log_msg = log_msg + "\nSettling time: " + str(settling_time)
|
||||
log_msg = log_msg + "\nImages: " + str(number_images)
|
||||
log_msg = log_msg + "\nBackground: enabled=" + str(use_background) + " multiple=" + str(multiple_background) + " number=" + str(number_backgrounds)
|
||||
log_msg = log_msg + "\nPlot index: " + str(centroid_plot_index)
|
||||
log_msg = log_msg + "\n\n" + r.print()
|
||||
|
||||
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() + "/centroid_excursion.png")
|
||||
centroid_plot.saveSnapshot(file_name , "png")
|
||||
elog("Laser alignment with phase scan", log_msg, [file_name,])
|
||||
|
||||
|
||||
set_return([r, hx, hy])
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -72,7 +72,7 @@ class BpmStats(DeviceBase):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
"""
|
||||
if __name__ == "__builtin__":
|
||||
add_device(BpmStats("bpm_com", "SINEG01-DBPM340"), True)
|
||||
bpm_com.setNumberOfImages(5)
|
||||
@@ -81,3 +81,4 @@ if __name__ == "__builtin__":
|
||||
print bpm_com.take(), bpm_com.com_x_mean.read(), bpm_com.com_y_mean.read()
|
||||
time.sleep(1)
|
||||
|
||||
"""
|
||||
@@ -1,6 +1,7 @@
|
||||
from startup import *
|
||||
from ijutils import *
|
||||
from mathutils import *
|
||||
import ch.psi.pshell.imaging.Filter as Filter
|
||||
from ch.psi.pshell.imaging.Overlays import *
|
||||
import ch.psi.pshell.imaging.Pen as Pen
|
||||
import java.awt.Color as Color
|
||||
@@ -12,6 +13,7 @@ from ch.psi.pshell.imaging.Utils import sub
|
||||
REMOVE_BACKGROUND = True
|
||||
PLOT_PROFILE = True
|
||||
|
||||
"""
|
||||
def get_centroid(source):
|
||||
bi = source.getImage()
|
||||
if bi is None:
|
||||
@@ -34,44 +36,8 @@ def get_centroid(source):
|
||||
ov = Crosshairs(Pen(Color.ORANGE), java.awt.Point(int(centroid[0]),int(centroid[1])), java.awt.Dimension(15,15))
|
||||
op.addOverlay(ov)
|
||||
return centroid
|
||||
|
||||
|
||||
|
||||
def arroff(a, value = "mean"):
|
||||
"""Subtract offset to all elemets in series.
|
||||
|
||||
Args:
|
||||
|
||||
a(list, tuple, array ...): subscriptable object containing numbers
|
||||
type(int or str, optional): value to subtract from the array, or "mean" or "min".
|
||||
|
||||
Returns:
|
||||
List
|
||||
|
||||
"""
|
||||
if value=="mean":
|
||||
value = mean(a)
|
||||
elif value=="min":
|
||||
value = min(a)
|
||||
return [x-value for x in a]
|
||||
|
||||
|
||||
def arrsubback(data, a, b):
|
||||
"""Subtract offset to all elemets in series.
|
||||
|
||||
Args:
|
||||
|
||||
a(list, tuple, array ...): subscriptable object containing numbers
|
||||
type(int or str, optional): value to subtract from the array, or "mean" or "min".
|
||||
|
||||
Returns:
|
||||
List
|
||||
|
||||
"""
|
||||
return [y-(a*y+b) for y in a]
|
||||
|
||||
|
||||
import ch.psi.pshell.imaging.Filter as Filter
|
||||
"""
|
||||
|
||||
|
||||
class SimulatedSource(Filter):
|
||||
def process(self, img, data):
|
||||
@@ -94,7 +60,13 @@ class SimulatedSource(Filter):
|
||||
class ImageStats(DeviceBase):
|
||||
def __init__(self, name, source):
|
||||
DeviceBase.__init__(self, name)
|
||||
self.source = source
|
||||
if isinstance(source, basestring):
|
||||
self.source = get_context().getClassByName("SfCamera")(source, source)
|
||||
self.private_source = True
|
||||
self.source.initialize()
|
||||
else:
|
||||
self.private_source = False
|
||||
self.source = source
|
||||
self.com_x_samples, self.com_y_samples = [], []
|
||||
self.rms_x_samples, self.rms_y_samples = [], []
|
||||
self.background = None
|
||||
@@ -122,7 +94,7 @@ class ImageStats(DeviceBase):
|
||||
set_device_alias(self.com_y_mean, name + " com y mean")
|
||||
set_device_alias(self.com_x_stdev, name + " com x stdev")
|
||||
set_device_alias(self.com_y_stdev, name + " com y stdev")
|
||||
self.bg_en = False
|
||||
#self.bg_en = False
|
||||
self.num_images = 5
|
||||
|
||||
class BackgroundSubtractor(Filter):
|
||||
@@ -135,6 +107,7 @@ class ImageStats(DeviceBase):
|
||||
self.backgroundFilter = BackgroundSubtractor(self)
|
||||
|
||||
self.initialize()
|
||||
self.start()
|
||||
|
||||
#class SourceListener (ImageListener):
|
||||
# def __init__(self, dev):
|
||||
@@ -154,17 +127,8 @@ class ImageStats(DeviceBase):
|
||||
self.rms_x_samples, self.rms_y_samples = [], []
|
||||
for i in range(self.num_images):
|
||||
if type(self.source) is not ch.psi.pshell.imaging.FileSource:
|
||||
#print "A"
|
||||
self.source.waitNext(5000)
|
||||
#print "B"
|
||||
#time.sleep(0.2)
|
||||
#print "Ok"
|
||||
#centroid = get_centroid(self.source)
|
||||
#print "cent ", centroid
|
||||
#if centroid is not None:
|
||||
# self.com_x_samples.append(centroid[0])
|
||||
# self.com_y_samples.append(centroid[1])
|
||||
|
||||
|
||||
x_profile = self.source.data.integrateVertically(True)
|
||||
x_profile_x = self.source.data.getRowSelectionX(True)
|
||||
|
||||
@@ -172,7 +136,7 @@ class ImageStats(DeviceBase):
|
||||
y_profile_x = self.source.data.getColSelectionX(True)
|
||||
|
||||
#Remove background
|
||||
if (self.bg_en == False) and REMOVE_BACKGROUND:
|
||||
if (self.source.backgroundEnabled == False) and REMOVE_BACKGROUND:
|
||||
(a, b, amp, com, sigma) = fit_gaussian_linear(x_profile, x_profile_x)
|
||||
x_profile = [x_profile[i]-(a*x_profile_x[i]+b) for i in range(len(x_profile))]
|
||||
(a, b, amp, com, sigma) = fit_gaussian_linear(y_profile, y_profile_x)
|
||||
@@ -187,44 +151,37 @@ class ImageStats(DeviceBase):
|
||||
p = get_plots("Profile")
|
||||
if (p is None) or (len(p)==0):
|
||||
p = plot([x_profile, y_profile], ["x_profile", "yprofile"], [x_profile_x,y_profile_x], title ="Profile")
|
||||
else:
|
||||
if p is not None:
|
||||
p[0].removeMarker(None)
|
||||
p[1].removeMarker(None)
|
||||
p[0].getSeries(0).setData(x_profile_x, x_profile)
|
||||
p[1].getSeries(0).setData(y_profile_x, y_profile)
|
||||
p[0].addMarker(com_x, None, "COM = " + str(com_x), Color.GREEN)
|
||||
p[1].addMarker(com_y, None, "COM = " + str(com_y), Color.GREEN)
|
||||
|
||||
|
||||
def setNumberOfImages(self, value):
|
||||
self.num_images = value
|
||||
|
||||
def enableBackground(self, value):
|
||||
self.bg_en = value
|
||||
self.source.filter = self.backgroundFilter if self.bg_en else None
|
||||
self.source.backgroundEnabled = value
|
||||
|
||||
def captureBackground(self, images):
|
||||
try:
|
||||
self.source.filter = None
|
||||
imgs = []
|
||||
for i in range(images):
|
||||
self.source.waitNext(5000)
|
||||
imgs.append(cam3.output)
|
||||
#TODO:
|
||||
self.background = cam3.output
|
||||
finally:
|
||||
self.enableBackground(self.bg_en)
|
||||
|
||||
|
||||
def grabBackground(self, images):
|
||||
self.source.captureBackground(images,0)
|
||||
|
||||
def doClose(self):
|
||||
print "close"
|
||||
self.source.filter = None
|
||||
#self.source.removeListener(self.listener)
|
||||
if self.private_source:
|
||||
self.source.close()
|
||||
else:
|
||||
self.source.backgroundEnabled = False
|
||||
|
||||
def start(self):
|
||||
pass
|
||||
if self.private_source:
|
||||
self.source.polling = 200
|
||||
self.source.waitNext(2000)
|
||||
|
||||
def stop(self):
|
||||
pass
|
||||
if self.private_source:
|
||||
self.source.polling = 0
|
||||
|
||||
|
||||
def get_simulated_source(img):
|
||||
@@ -235,16 +192,14 @@ def get_simulated_source(img):
|
||||
show_panel(simulated_source)
|
||||
return simulated_source
|
||||
|
||||
|
||||
|
||||
|
||||
"""
|
||||
if __name__ == "__builtin__":
|
||||
#simulated_source = get_simulated_source(image)
|
||||
#print get_centroid(simulated_source)
|
||||
add_device(ImageStats("image_stats", "SLG-LCAM-C041"), True)
|
||||
add_device(image_stats.source, True)
|
||||
#cam3.waitNext(2000)
|
||||
|
||||
add_device(ImageStats("image_stats", cam3), True)
|
||||
cam3.waitNext(2000)
|
||||
image_stats.enableBackground(False)
|
||||
#for i in range (10):
|
||||
# image_stats.update()
|
||||
# print image_stats.take(), image_stats.com_x_mean.read(), image_stats.com_y_mean.read()
|
||||
@@ -253,9 +208,14 @@ if __name__ == "__builtin__":
|
||||
image_stats.setNumberOfImages(3)
|
||||
sensors = [image_stats.com_x_mean, image_stats.com_y_mean, image_stats.com_x_stdev, image_stats.com_y_stdev]
|
||||
|
||||
image_stats.captureBackground(5)
|
||||
#image_stats.enableBackground(True)
|
||||
|
||||
image_stats.grabBackground(1)
|
||||
|
||||
def before_sample():
|
||||
image_stats.update()
|
||||
tscan(sensors, 10, 0.1, before_read = before_sample)
|
||||
image_stats.enableBackground(False)
|
||||
try:
|
||||
tscan(sensors, 10, 0.1, before_read = before_sample)
|
||||
finally:
|
||||
image_stats.enableBackground(False)
|
||||
image_stats.stop()
|
||||
"""
|
||||
@@ -225,16 +225,16 @@ print msg
|
||||
# save the entry in the logbook
|
||||
if do_elog:
|
||||
if get_option("Generated data file:\n" + filename +"\n\n" + msg + "\n\n" + "Save to ELOG?", "YesNo") == "Yes":
|
||||
gsa_log_msg = "Data file: " + filename
|
||||
gsa_log_msg = gsa_log_msg + "\nWire Scanner: " + prefix
|
||||
gsa_log_msg = gsa_log_msg + "\nScan Type: " + str(scan_type)
|
||||
gsa_log_msg = gsa_log_msg + "\nRange: " + str(scan_range)
|
||||
gsa_log_msg = gsa_log_msg + "\nCycles: " + str(cycles)
|
||||
gsa_log_msg = gsa_log_msg + "\nWire Velocity: " + str(velocity)
|
||||
gsa_log_msg = gsa_log_msg + "\nBackground Measures: " + str(bkgrd)
|
||||
gsa_log_msg = gsa_log_msg + "\nBPMs: " + str(bpms)
|
||||
gsa_log_msg = gsa_log_msg + "\nBLMs: " + str(blms)
|
||||
log_msg = "Data file: " + filename
|
||||
log_msg = log_msg + "\nWire Scanner: " + prefix
|
||||
log_msg = log_msg + "\nScan Type: " + str(scan_type)
|
||||
log_msg = log_msg + "\nRange: " + str(scan_range)
|
||||
log_msg = log_msg + "\nCycles: " + str(cycles)
|
||||
log_msg = log_msg + "\nWire Velocity: " + str(velocity)
|
||||
log_msg = log_msg + "\nBackground Measures: " + str(bkgrd)
|
||||
log_msg = log_msg + "\nBPMs: " + str(bpms)
|
||||
log_msg = log_msg + "\nBLMs: " + str(blms)
|
||||
|
||||
gsa_log_msg = gsa_log_msg + "\n" + msg
|
||||
elog("Wire Scan", gsa_log_msg, snapshots)
|
||||
log_msg = log_msg + "\n" + msg
|
||||
elog("Wire Scan", log_msg, snapshots)
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import ch.psi.pshell.epics.ControlledVariable as ControlledVariable
|
||||
|
||||
if get_exec_pars().source == CommandSource.ui:
|
||||
start = -100.0
|
||||
start = -10.0
|
||||
stop = 180.0
|
||||
step = 5.0
|
||||
nb = 3
|
||||
@@ -13,43 +13,41 @@ else:
|
||||
nb = int(args[3])
|
||||
lat = args[4]
|
||||
|
||||
phase = ControlledVariable("Phase", "SINEG01-RSYS:SET-BEAM-PHASE", "SINEG01-RSYS:GET-BEAM-PHASE")
|
||||
#phase = ControlledVariable("Phase", "S10CB09-RSYS:SET-BEAM-PHASE", "S10CB09-RSYS:SET-BEAM-PHASE")
|
||||
phase.config.minValue =-180.0
|
||||
phase.config.maxValue = 360.0
|
||||
phase.config.precision = 3
|
||||
phase.config.resolution = 1.0
|
||||
phase.config.save()
|
||||
phase.initialize()
|
||||
st = Stream("ICTstream", dispatcher)
|
||||
q = st.addScalar("Charge", "SINEG01-DICT215:B1_CHARGE", 1, 0)
|
||||
bphase = ControlledVariable("Beam phase", "SINEG01-RSYS:SET-BEAM-PHASE", "SINEG01-RSYS:GET-BEAM-PHASE")
|
||||
bphase.config.minValue =-180.0
|
||||
bphase.config.maxValue = 360.0
|
||||
bphase.config.precision = 3
|
||||
bphase.config.resolution = 1.0
|
||||
bphase.config.save()
|
||||
bphase.initialize()
|
||||
st = Stream("Schottky", dispatcher)
|
||||
q = st.addScalar("Charge", "SINEG01-DICT215:B1_CHARGE", 1, 0)
|
||||
st.initialize()
|
||||
st.start()
|
||||
#q = Channel("SINEG01-DICT215:B1_CHARGE", type = 'd', alias='ICT-Q')
|
||||
phase0 = phase.read()
|
||||
|
||||
print phase0
|
||||
|
||||
rphase = Channel("SINEG01-RSYS:GET-VSUM-PHASE", type = 'd', alias = 'RF phase')
|
||||
bphase0 = bphase.read()
|
||||
|
||||
try:
|
||||
qb = create_averager(q, nb, 0.100)
|
||||
r = lscan(phase, (qb), start, stop, step, latency=lat)
|
||||
rf_phase = r.getPositions(0)
|
||||
charge = [val.mean for val in r.getReadable(0)]
|
||||
chargerms = [val.stdev for val in r.getReadable(0)]
|
||||
q_averager = create_averager(q, nb, 0.100)
|
||||
rphase_averager = create_averager(rphase, nb, 0.100)
|
||||
r = lscan(bphase, (q_averager, rphase_averager), start, stop, step, latency=lat)
|
||||
beamphase = r.getPositions(0)
|
||||
charge = [val.mean for val in r.getReadable(0)]
|
||||
chargerms = [val.stdev for val in r.getReadable(0)]
|
||||
rfphase = [val.mean for val in r.getReadable(1)]
|
||||
rfphaserms = [val.stdev for val in r.getReadable(1)]
|
||||
finally:
|
||||
phase.write(phase0)
|
||||
phase.close()
|
||||
bphase.write(phase0)
|
||||
bphase.close()
|
||||
st.close()
|
||||
#q.close()
|
||||
q.close()
|
||||
rphase.close()
|
||||
|
||||
#Setting the return value
|
||||
x = rf_phase
|
||||
y = charge
|
||||
index_max = y.index(max(y))
|
||||
phase_ref = x[index_max] - 80
|
||||
index_max = charge.index(max(charge))
|
||||
rphase_ref = rfphase[index_max] - 80
|
||||
|
||||
set_return(phase_ref)
|
||||
set_return(rphase_ref)
|
||||
|
||||
# save the entry in the logbook
|
||||
#if get_option("Generated data file:\n" + get_exec_pars().path +"\n\n" + "Save to ELOG?", "YesNo") == "Yes":
|
||||
|
||||
@@ -1,49 +1,60 @@
|
||||
import ch.psi.pshell.epics.ControlledVariable as ControlledVariable
|
||||
|
||||
if get_exec_pars().source == CommandSource.ui:
|
||||
start = 50.0
|
||||
stop = 230.0
|
||||
start = -10.0
|
||||
stop = 180.0
|
||||
step = 5.0
|
||||
nb = 1
|
||||
lat = 0.100
|
||||
nb = 3
|
||||
lat = 0.300
|
||||
else:
|
||||
start = args[0]
|
||||
stop = args[1]
|
||||
step = args[2]
|
||||
nb = int(args[3])
|
||||
lat = args[4]
|
||||
|
||||
|
||||
phase = ControlledVariable("Phase", "SINEG01-RSYS:SET-VSUM-PHASE-SIM", "SINEG01-RSYS:GET-VSUM-PHASE-SIM")
|
||||
phase.config.minValue =-180.0
|
||||
phase.config.maxValue = 180.0
|
||||
phase.config.resolution = 0.5
|
||||
phase.initialize()
|
||||
bphase = ControlledVariable("Beam phase", "SINEG01-RSYS:SET-VSUM-PHASE-SIM", "SINEG01-RSYS:GET-VSUM-PHASE-SIM")
|
||||
bphase.config.minValue =-180.0
|
||||
bphase.config.maxValue = 360.0
|
||||
bphase.config.precision = 3
|
||||
bphase.config.resolution = 1.0
|
||||
bphase.config.save()
|
||||
bphase.initialize()
|
||||
st = Stream("Schottky stream", dispatcher)
|
||||
#q = st.addScalar("Charge", "SINEG01-DICT215:B1_CHARGE", 1, 0)
|
||||
#rphase = st.addScalar("RF phase", "SINEG01-RSYS:GET-VSUM-PHASE", 1, 0)
|
||||
st.initialize()
|
||||
st.start()
|
||||
q = Channel("SINEG01-DICT215:B1_CHARGE-SIM", type = 'd', alias = 'ICT-Q')
|
||||
rphase = Channel("SINEG01-RSYS:GET-BEAM-PHASE-SIM", type = 'd', alias = 'RF phase')
|
||||
|
||||
#st = Stream("ICTstream", dispatcher)
|
||||
#q = st.addScalar("Charge", "SINEG01-DICT215:B1_CHARGE-SIM", 1, 0)
|
||||
#st.initialize()
|
||||
#st.start(True)
|
||||
q = Channel("SINEG01-DICT215:B1_CHARGE-SIM", type = 'd', alias='ICT-Q')
|
||||
bphase0 = bphase.read()
|
||||
print bphase0
|
||||
print q.read()
|
||||
print rphase.read()
|
||||
|
||||
try:
|
||||
qb = create_averager(q, nb, 0.100)
|
||||
r = lscan(phase, (qb), start, stop, step, latency=lat)
|
||||
rf_phase = r.getPositions(0)
|
||||
charge = [val.mean for val in r.getReadable(0)]
|
||||
chargerms = [val.stdev for val in r.getReadable(0)]
|
||||
q_averager = create_averager(q, nb, 0.100)
|
||||
rphase_averager = create_averager(rphase, nb, 0.100)
|
||||
r = lscan(bphase, (q_averager, rphase_averager), start, stop, step, latency=lat)
|
||||
beamphase = r.getPositions(0)
|
||||
charge = [val.mean for val in r.getReadable(0)]
|
||||
chargerms = [val.stdev for val in r.getReadable(0)]
|
||||
rfphase = [val.mean for val in r.getReadable(1)]
|
||||
rfphaserms = [val.stdev for val in r.getReadable(1)]
|
||||
finally:
|
||||
phase.close()
|
||||
bphase.write(bphase0)
|
||||
bphase.close()
|
||||
st.close()
|
||||
q.close()
|
||||
#st.close()
|
||||
rphase.close()
|
||||
|
||||
#Setting the return value
|
||||
#set_return(50.0)
|
||||
y = charge
|
||||
x = rf_phase
|
||||
index_max = y.index(max(y))
|
||||
phase_ref = x[index_max] - 80
|
||||
phase_offset = - phase_ref
|
||||
caput("SINEG01-RSYS:CALC-VSUM-PHASE-OFFSET", phase_offset)
|
||||
index_max = charge.index(max(charge))
|
||||
rphase_ref = rfphase[index_max] - 80
|
||||
bphase_ref = beamphase[index_max] - 80
|
||||
|
||||
set_return(phase_ref)
|
||||
print rphase_ref
|
||||
print bphase_ref
|
||||
|
||||
set_return(rphase_ref)
|
||||
|
||||
+9
-14
@@ -8,7 +8,7 @@ from mathutils import PolynomialFunction, Gaussian, HarmonicOscillator
|
||||
|
||||
import java.awt.Color as Color
|
||||
|
||||
|
||||
|
||||
LASER_SETTLING_TIME = 3.0
|
||||
|
||||
def laser_on():
|
||||
@@ -165,19 +165,7 @@ def elog(title, message, attachments = [], author = None, category = "Info", dom
|
||||
if (err is not None) and err!="":
|
||||
raise Exception(err)
|
||||
print out
|
||||
|
||||
def get_plot_snapshots(title = None, file_type = "png", temp_path = get_context().setup.getContextPath()):
|
||||
"""
|
||||
Returns list with file names of plots snapshots from a plotting context.
|
||||
"""
|
||||
sleep(0.1) #Give some time to plot to be finished - it is not sync with acquisition
|
||||
ret = []
|
||||
for p in get_plots(title):
|
||||
file_name = os.path.abspath(temp_path + "/" + p.getTitle() + "." + file_type)
|
||||
p.saveSnapshot(file_name , file_type)
|
||||
ret.append(file_name)
|
||||
return ret
|
||||
|
||||
|
||||
|
||||
class Sinusoid(ReadonlyRegisterBase):
|
||||
def doRead(self):
|
||||
@@ -277,11 +265,18 @@ def get_camtool_stats(number_images=1, async = True, interval=-1, good_region =
|
||||
av.monitored = async
|
||||
ret.append(av)
|
||||
"""
|
||||
"""
|
||||
for dev in [CamtoolComX(), CamtoolComY(), CamtoolComErrorX(), CamtoolComErrorY()]:
|
||||
dev.initialize()
|
||||
av = create_averager (dev, number_images, 1/2.5)#(dev, number_images, interval)
|
||||
#av.monitored = async
|
||||
ret.append(av)
|
||||
"""
|
||||
for ident in [prefix+"x_center_of_mass", prefix+"y_center_of_mass", prefix+"x_rms", prefix+"y_rms"]:
|
||||
child = camtool.stream.getChild(ident)
|
||||
av = create_averager(child, number_images, interval)
|
||||
av.monitored = async
|
||||
ret.append(av)
|
||||
return ret
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user