diff --git a/script/local.py b/script/local.py index b0635dd..4f8eb87 100755 --- a/script/local.py +++ b/script/local.py @@ -7,7 +7,11 @@ from mathutils import fit_polynomial,fit_gaussian, fit_harmonic, calculate_peaks from mathutils import PolynomialFunction, Gaussian, HarmonicOscillator import java.awt.Color as Color + +################################################################################################### +# Machine utilities +################################################################################################### LASER_SETTLING_TIME = 3.0 @@ -47,9 +51,31 @@ def switch_off_magnets(magnets = None): caput(m + ":I-SET", 0.0) sleep(0.5) for m in magnets: - ccr(m) + pass + +def is_timing_ok(): + return caget("SIN-TIMAST-TMA:SOS-COUNT-CHECK") == 0 + +def get_repetition_rate(): + return caget("SIN-TIMAST-TMA:Evt-15-Freq-I") + +def bsget(channel): + """ + """ + st = Stream(None, dispatcher) + try: + st.addScalar(channel, channel, 10, 0) + st.initialize() + st.start(); + st.waitValueNot(None, 5000) + return st.getValue(channel) + finally: + st.close() +################################################################################################### +# Maths utilities +################################################################################################### def fit(ydata, xdata = None): """ @@ -135,152 +161,6 @@ def hfit(ydata, xdata = None): p.addMarker(max_x, None, "Max="+str(round(max_x ,2)), Color.MAGENTA.darker()) return (amplitude, angular_frequency, phase, False, max_x, fit_x, fit_y) - -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" #get_context().user.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 - - -class Sinusoid(ReadonlyRegisterBase): - def doRead(self): - self.x = self.x + 5.0 if hasattr(self, 'x') else 0.0 - return math.sin(self.x * math.pi / 180.0) - - -#Pseudo devices -""" -add_device(Sinusoid("phase"), True) -add_device(Sinusoid("bpm_q"), True) -add_device(Sinusoid("center_x"), True) -add_device(Sinusoid("center_y"), True) -center_x.setPolling(100) -center_y.setPolling(100) -""" -#Camtool "child" pseudo-devices -class CamtoolValue(Readable): - def __init__(self, channel, alias = None): - self.channel=channel - set_device_alias(self, channel if (alias is None) else alias) - - def read(self): - return camtool.getValue(self.channel) - -class CamtoolArray(ReadableArray): - def __init__(self, channel, alias = None): - self.channel=channel - set_device_alias(self, channel if (alias is None) else alias) - - def read(self): - return camtool.getValue(self.channel) - - def getSize(self): - return len(camtool.getValue(self.channel)) - -class CamtoolImage(ReadableMatrix): - def __init__(self, alias = None): - set_device_alias(self, camtool.getCurrentCamera() + " image" if (alias is None) else alias) - - def read(self): - return camtool.getData().matrix - - def getWidth(self): - return camtool.getData().width - - def getHeight(self): - return camtool.getData().height - -def wait_camtool_message(number_messages = 1, timeout = 10000): - for i in range (number_messages): - if not camtool.stream.waitCacheChange(timeout): raise Exception("Timeout receiving from Camtool") - - -#Temporary wokarounbd - -class CamtoolComX(ReadonlyRegisterBase): - def doRead(self): - v = camtool.getValue("x_profile") - x = camtool.getValue("x_axis") - s = 0 - p = 0 - for i in range(len(v)): - s += x[i]*v[i] - p += v[i] - return s / p - -class CamtoolComY(ReadonlyRegisterBase): - def doRead(self): - v = camtool.getValue("y_profile") - x = camtool.getValue("y_axis") - s = 0 - p = 0 - for i in range(len(v)): - s += x[i]*v[i] - p += v[i] - return s / p - -class CamtoolComErrorX(ReadonlyRegisterBase): - def doRead(self): - return 0.0 - -class CamtoolComErrorY(ReadonlyRegisterBase): - def doRead(self): - return 0.0 - - -def get_camtool_stats(number_images=1, async = True, interval=-1, good_region = False): - #return [CamtoolValue("gr_x_fit_mean"), CamtoolValue("gr_y_fit_mean"), CamtoolValue("gr_x_fit_standard_deviation"), CamtoolValue("gr_y_fit_standard_deviation")] - ret = [] - wait_camtool_message() - prefix = "gr_" if good_region else "" - """ - for ident in [prefix+"x_fit_mean", prefix+"y_fit_mean", prefix+"x_fit_standard_deviation", prefix+"y_fit_standard_deviation"]: - child = camtool.stream.getChild(ident) - av = create_averager(child, number_images, interval) - 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 - - -#Convex hull plots def clear_convex_hull_plot(title): plots = get_plots(title = title) if len(plots)>0: @@ -325,6 +205,57 @@ def add_convex_hull_plot(title, x,y, name=None, clear = False, x_range = None, y hull.setColor(s.color) return [hx,hy] + +################################################################################################### +# Tools +################################################################################################### + +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" #get_context().user.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 + + +################################################################################################### +# Pseudo-devices +################################################################################################### +class Sinusoid(ReadonlyRegisterBase): + def doRead(self): + self.x = self.x + 5.0 if hasattr(self, 'x') else 0.0 + return math.sin(self.x * math.pi / 180.0) + +""" +add_device(Sinusoid("phase"), True) +add_device(Sinusoid("bpm_q"), True) +add_device(Sinusoid("center_x"), True) +add_device(Sinusoid("center_y"), True) +center_x.setPolling(100) +center_y.setPolling(100) import random class ComX(ReadonlyRegisterBase): @@ -341,21 +272,27 @@ comx = ComX(); comx.initialize() comy = ComY(); comy.initialize() avx = create_averager(comx, 5, 0.001) avy = create_averager(comy, 5, 0.001) +""" -#CAS -if get_context().isServerEnabled(): - import ch.psi.pshell.epics.CAS as CAS - #CAS.setServerPort(5062) +################################################################################################### +# Camera server +################################################################################################### +def wait_camtool_message(number_messages = 1, timeout = 10000): + for i in range (number_messages): + if not camtool.stream.waitCacheChange(timeout): + raise Exception("Timeout receiving from Camtool") - class ServerUrl(ReadonlyRegisterBase): - def doRead(self): - return get_context().server.baseURL - d = ServerUrl() - d.initialize() - - cas5 = CAS("PSHELL_OP:SERVER_URL", d, 'string') - +def get_camtool_stats(number_images=1, async = True, interval=-1, good_region = False): + ret = [] + wait_camtool_message() + prefix = "gr_" if good_region else "" + 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 #Managing local camtool server CAMTOOL_SERVER_SCRIPT = "camtool_pshell" #"cam_server" @@ -402,28 +339,24 @@ def check_camtool(): if time.time() - start > 2.0: raise Exception("Error starting camtool process") time.sleep(3.0) + -def bsget(channel): - """ - """ - st = Stream(None, dispatcher) - try: - st.addScalar(channel, channel, 10, 0) - st.initialize() - st.start(); - st.waitValueNot(None, 5000) - return st.getValue(channel) - finally: - st.close() +################################################################################################### +#CAS +################################################################################################### + +if get_context().isServerEnabled(): + import ch.psi.pshell.epics.CAS as CAS + #CAS.setServerPort(5062) + + class ServerUrl(ReadonlyRegisterBase): + def doRead(self): + return get_context().server.baseURL + d = ServerUrl() + d.initialize() + + cas5 = CAS("PSHELL_OP:SERVER_URL", d, 'string') -#Machine utilities -def is_timing_ok(): - return caget("SIN-TIMAST-TMA:SOS-COUNT-CHECK") == 0 -def get_repetition_rate(): - return caget("SIN-TIMAST-TMA:Evt-15-Freq-I") -#Device pool customization -pbpg_mx.setTrustedWrite(False) -pbpg_my.setTrustedWrite(False) \ No newline at end of file