99 lines
3.6 KiB
Python
99 lines
3.6 KiB
Python
###################################################################################################
|
|
# Deployment specific global definitions - executed after startup.py
|
|
###################################################################################################
|
|
|
|
from mathutils import estimate_peak_indexes, fit_gaussians, create_fit_point_list, Gaussian
|
|
|
|
import java.awt.Color as Color
|
|
|
|
|
|
def fit(ydata, xdata = None):
|
|
"""
|
|
"""
|
|
if xdata is None:
|
|
xdata = frange(0, len(ydata), 1)
|
|
max_y= max(ydata)
|
|
index_max = ydata.index(max_y)
|
|
max_x= xdata[index_max]
|
|
print "Max index:" + str(index_max),
|
|
print " x:" + str(max_x),
|
|
print " y:" + str(max_y)
|
|
gaussians = fit_gaussians(ydata, xdata, [index_max,])
|
|
(norm, mean, sigma) = gaussians[0]
|
|
p = plot([ydata],["data"],[xdata], title="Fit" )[0]
|
|
fitted_gaussian_function = Gaussian(norm, mean, sigma)
|
|
scale_x = [float(min(xdata)), float(max(xdata)) ]
|
|
points = max((len(xdata)+1), 100)
|
|
resolution = (scale_x[1]-scale_x[0]) / points
|
|
fit_y = []
|
|
fit_x = frange(scale_x[0],scale_x[1],resolution, True)
|
|
for x in fit_x:
|
|
fit_y.append(fitted_gaussian_function.value(x))
|
|
p.addSeries(LinePlotSeries("fit"))
|
|
p.getSeries(1).setData(fit_x, fit_y)
|
|
|
|
if abs(mean - xdata[index_max]) < ((scale_x[0] + scale_x[1])/2):
|
|
print "Mean -> " + str(mean)
|
|
p.addMarker(mean, None, "Mean="+str(round(norm,2)), Color.MAGENTA.darker())
|
|
return (norm, mean, sigma)
|
|
else:
|
|
p.addMarker(max_x, None, "Max="+str(round(max_x,2)), Color.GRAY)
|
|
print "Invalid gaussian fit: " + str(mean)
|
|
return (None, None, None)
|
|
|
|
|
|
|
|
def elog(title, message, attachments = [], author = None, category = "Info", domain = "", logbook = "SwissFEL commissioning data", encoding=1):
|
|
"""
|
|
Add entry to ELOG.
|
|
"""
|
|
if author is None:
|
|
author = "pshell" #controller.getUser().name
|
|
typ = "pshell"
|
|
entry = ""
|
|
|
|
cmd = 'G_CS_ELOG_add -l "' + logbook+ '" '
|
|
cmd = cmd + '-a "Author=' + author + '" '
|
|
cmd = cmd + '-a "Type=' + typ + '" '
|
|
cmd = cmd + '-a "Entry=' + entry + '" '
|
|
cmd = cmd + '-a "Title=' + title + '" '
|
|
cmd = cmd + '-a "Category=' + category + '" '
|
|
cmd = cmd + '-a "Domain=' + domain + '" '
|
|
for attachment in attachments:
|
|
cmd = cmd + '-f "' + attachment + '" '
|
|
cmd = cmd + '-n ' + str(encoding)
|
|
cmd = cmd + ' "' + message + '"'
|
|
#print cmd
|
|
#os.system (cmd)
|
|
#print os.popen(cmd).read()
|
|
import subprocess
|
|
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
|
|
(out, err) = proc.communicate()
|
|
if (err is not None) and err!="":
|
|
raise Exception(err)
|
|
print out
|
|
|
|
def get_plot_snapshots(title = None, file_type = "jpg", temp_path = controller.setup.getContextPath()):
|
|
"""
|
|
Returns list with file names of plots snapshots from a plotting context.
|
|
"""
|
|
sleep(0.02) #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):
|
|
self.x = self.x + 1.0 if hasattr(self, 'x') else 0.0
|
|
return math.sin(self.x * math.pi / 180.0)
|
|
|
|
add_device(Sinusoid("sim"), True)
|
|
|
|
add_device(Sinusoid("center_x"), True)
|
|
add_device(Sinusoid("center_y"), True)
|
|
center_x.setPolling(100)
|
|
center_y.setPolling(100) |