Files
x11ma/script/local.py
2020-12-09 15:06:55 +01:00

675 lines
24 KiB
Python

###################################################################################################
# Deployment specific global definitions - executed after startup.py
###################################################################################################
import ntpath
import traceback
from rsync import *
get_context().dataManager.createLogs=False
"""
OTF_START = "X11MA-OTF:GO"
OTF_E1 = "X11MA-OTF:E1"
OTF_E2 = "X11MA-OTF:E2"
OTF_TIME = "X11MA-OTF:TIME"
OTF_FID = "X11MA-OTF:FID"
OTF_FTS = "X11MA-OTF:FTSTAMP"
OTF_FILE = "X11MA-OTF:FNAME"
ID1_MODE = "X11MA-ID1:MODE"
ID2_MODE = "X11MA-ID2:MODE"
ID1_ALPHA = "X11MA-ID1:ALPHA"
ID2_ALPHA = "X11MA-ID2:ALPHA"
ID1_OFF = "X11MA-ID1:ENERGY-OFFS"
ID2_OFF = "X11MA-ID2:ENERGY-OFFS"
ENERGY_SP = "X11MA-PHS:E_SP"f
"""
ALL_DONE = "X11PHS:alldone"
VG10_SET = "X11MA-EPS-VG10:SET"
VG10_GET = "X11MA-OP-VG10:OPEN"
VG11_SET = "X11MA-EPS-VG11:SET"
VG11_GET = "X11MA-OP-VG11:OPEN"
VG12_SET = "X11MA-EPS-VG12:SET"
VG12_GET = "X11MA-OP-VG12:OPEN"
VG13_SET = "X11MA-EPS-VG13:SET"
VG13_GET = "X11MA-OP-VG13:OPEN"
"""
LPP_DELAY = "X11MA-ES2-4CHT:SET2-DELAY"
LPP_FINE_DELAY = "X11MA-ES2-4CHT:SET2-FINE"
TIME_DELAY_SET = "X11MA-ES2:Theta-New.A"
TIME_DELAY_VAL = "X11MA-ES2:Theta-Cur.VAL"
TIME_DELAY_START = "X11MA-ES2:SCAN-START"
TIME_DELAY_COMPLETE = "X11MA-ES2-scan1.SMSG"
TBT_MAG = "X11MA-LSCI632:MFIELD"
"
"""
def get_next_fid(folder, prefix):
try:
import glob
files = glob.glob(folder + prefix + '*_*.dat')
last = max(files)
index = int (last[last.rfind('_')+1 : last.rfind('.')]) + 1
return index
except:
return 0
def wait_channel(name, value, timeout =None, type='s'):
print "Waiting " + str(name) + " = " + str(value)
cawait(name, value, timeout = timeout, type=type)
print "Done"
def wait_device(dev, value, timeout=-1):
timeout = int(timeout *1000) if timeout>0 else timeout
dev.waitValue(value,timeout)
def open_vg10():
if caget (VG10_GET,'i') != 1:
caput(VG10_SET, 0)
time.sleep(0.1)
caput(VG10_SET, 1)
def close_vg10():
if caget (VG10_GET,'i') == 1:
caput(VG10_SET, 0)
time.sleep(0.1)
caput(VG10_SET, 1)
def open_vg11():
if caget (VG11_GET,'i') != 1:
caput(VG11_SET, 0)
time.sleep(0.1)
caput(VG11_SET, 1)
def close_vg11():
if caget (VG11_GET,'i') == 1:
caput(VG11_SET, 0)
time.sleep(0.1)
caput(VG11_SET, 1)
def open_vg12():
if caget (VG12_GET,'i') != 1:
caput(VG12_SET, 0)
time.sleep(0.1)
caput(VG12_SET, 1)
def close_vg12():
if caget (VG12_GET,'i') == 1:
caput(VG12_SET, 0)
time.sleep(0.1)
caput(VG12_SET, 1)
def open_vg13():
if caget (VG13_GET,'i') != 1:
caput(VG13_SET, 0)
time.sleep(0.1)
caput(VG13_SET, 1)
def close_vg13():
if caget (VG13_GET,'i') == 1:
caput(VG13_SET, 0)
time.sleep(0.1)
caput(VG13_SET, 1)
def plot_file(file_name, ctxt = None):
"""
"""
sep = "\t"
with open(file_name) as f:
header = f.readline()[1:].split(sep)
table = Table.loadRaw(file_name, sep, '#',header)
plots = plot(table, title = ctxt)
def has_beam():
"""
"""
return beam_status.readback.read() == "Light Available"
def is_id_error():
return (id_error.read()==0)
def check_id_error():
if is_id_error():
raise Exception ("ID error: check ID status")
###################################################################################################
#Default scan callbacks
###################################################################################################
def before_sample():
pass
def after_sample():
check_id_error()
##################### Convert_File function #############################
"""
def convert_file(input_file_name, output_file_name):
sep = "\t"
line_sep = "\r\n"
field = caget('X11MA-XMCD:Ireadout')
with open(input_file_name) as inp:
lines = inp.readlines()
with open(output_file_name, "wb") as out:
out.write("Energy" + sep + "Io" + sep + "CADC2" + sep + "CADC3" + sep + "Mag" + line_sep)
s = sep + " " #File format has a space before numeric values
for line in lines[1:]:
line = line.strip()
if line=="": break
try:
(Ecrbk,CADC1, CADC2, NORM, CADC3, CADC4, MCurr, cffrbk, ID1Erbk, ID2Erbk, vTime) = line.split(" ")
out.write(Ecrbk + s + CADC1 + s + CADC2 + s + CADC3 + s + str(field) +line_sep)
except:
traceback.print_exc()
"""
def convert_file(input_file_name, output_file_name, pol = None):
print "Converting data file: " + input_file_name + " to " + output_file_name
#print "File converted to: ",output_file_name
sep = "\t"
line_sep = "\n"
MODE = pol_mode.read()
if pol is None:
pol = pol_angle.read() if (MODE == "LINEAR") else pol_mode.readback.read()
with open(input_file_name) as inp:
lines = inp.readlines()
with open(output_file_name, "wb") as out:
(db, st) = ("java.lang.Double", "java.lang.String")
out.write("#Energy" + sep + "CADC1" + sep + "CADC2" + sep + "CADC3" + sep + "NORMtey" + sep + "NORMdiode" + line_sep)
out.write("#"+ db + sep + db + sep + db + sep + db + sep + db + sep + db + line_sep)
s = sep
for line in lines[1:]:
line = line.strip()
if line=="": break
try:
(Ecrbk, CADC1, CADC2, CADC3, CADC4, MCurr, cffrbk, IDErbk, time) = line.split(" ")
normtey=repr( float(CADC2)/float(CADC1))
normdiode=repr(float(CADC3)/float(CADC1))
#field=caget(MAG)
out.write(Ecrbk + s + CADC1 + s + CADC2 + s + CADC3 + s + normtey + s + normdiode + line_sep)
except:
traceback.print_exc()
#os.rename(input_file_name, get_context().setup.expandPath("{data}/OTF/" + ntpath.basename(input_file_name)))
def convert_file_full(input_file_name, output_file_name, pol = None):
print "Converting data file: " + input_file_name + " to " + output_file_name
#print "File converted to: ",output_file_name
sep = "\t"
line_sep = "\n"
MODE = pol_mode.read()
if pol is None:
pol = pol_angle.read() if (MODE == "LINEAR") else pol_mode.readback.read()
with open(input_file_name) as inp:
lines = inp.readlines()
with open(output_file_name, "wb") as out:
(db, st) = ("java.lang.Double", "java.lang.String")
out.write("#Energy" + sep + "CADC1" + sep + "CADC2" + sep + "CADC3" + sep + "CADC4" + sep + "CADC5" + sep + "MCurr" + sep + "Time" + sep + "FieldX" + sep + "FieldZ" + sep + "Pol" + sep + "Temperature" + sep + "NORMtey" + sep + "NORMdiode" + line_sep)
out.write("#"+ db + sep + db + sep + db + sep + db + sep + db + sep + db + sep + db + sep + db + sep + db + sep + db + sep + st + sep + db + sep + db + sep + db + line_sep)
s = sep
for line in lines[1:]:
line = line.strip()
if line=="": break
try:
(Ecrbk, CADC1, CADC2, CADC3, CADC4, MCurr, cffrbk, IDErbk, time) = line.split(" ")
normtey=repr( float(CADC2)/float(CADC1))
normdiode=repr(float(CADC3)/float(CADC1))
CADC5=repr(0.0)
mcurr=repr(machine_cur.read())
fieldx=repr(field.read())
fieldz=repr(0.0)
temp=repr(Temp.read())
out.write(Ecrbk + s + CADC1 + s + CADC2 + s + CADC3 + s + CADC4 + s + CADC5 + s + mcurr + s + time + s + fieldx + s + fieldz + s + str(pol) + s + temp + s + normtey + s + normdiode + line_sep)
except:
traceback.print_exc()
#os.rename(input_file_name, get_context().setup.expandPath("{data}/OTF/" + ntpath.basename(input_file_name)))
###################################################################################################
#OTF function
###################################################################################################
def otf(start, end, time, delay = 0.0, mode = None, offset = None, alpha = None, name = None, folder = 'TEST'):
"""
"""
if name is None:
name = get_exec_pars().name
#folder = get_context().setup.expandPath("{year}_{month}/{date}");
run("templates/EnergyScan", {"E1":start, "E2":end, "TIME":time, "DELAY":float(delay), "MODE":mode, "OFFSET":(offset), "FOLDER":folder, "FILE":name, "ALPHA":float(alpha) if alpha is not None else None})
###################################################################################################
#Devices
###################################################################################################
for dev in ["eiger", "id", "chopper", "LEEM2000", "diag"]:
try:
run("devices/" + dev)
except:
log(sys.exc_info()[1], False)
###################################################################################################
#ID and Machine status
###################################################################################################
def get_id_status(id):
# 0 = ok
# 1 = Warning
# 2 = Error
# 3 = Not available
if id<1 or id>2:
raise Exception("Invalid id")
return caget("X11MA-ID1-USER:STATUS" if (id==1) else "X11MA-ID2-USER:STATUS")
def get_id_error(id):
# B0 (1) = PLC (Taper, Motor controller, Limitswitch....)
# B1 (2) = Encoders (cabel, not referenced)
# B2 (4) = FeedForward (PS off, PS defect, FF ausgeschaltet)
# B3 (8) = Operator COntrol
# B4 (16)= Moving Timeout (moving longer then 120 sec)
# B5 (32)= Interlock (Orbit, Temperature...)\
if id<1 or id>2:
raise Exception("Invalid id")
return caget("X11MA-ID1-USER:ERROR-SOURCE" if (id==1) else "X11MA-ID2-USER:ERROR-SOURCE")
def get_id_control(id):
#0 = SLS control, 1 = experiment
if id<1 or id>2: raise Exception("Invalid id")
return caget("X11MA-ID1-GAP:SCTRL" if id==1 else "X11MA-ID2-GAP:SCTRL")
def get_ring_status():
#ACOAU-ACCU:OP-MODE kann Werte zwischen 0 und 5 annehmen:
# rule from 24.8.04
#If (PCT beam current <0.09) -> State 0: "Machine Down"
#Else If (GUN Trigger OFF) -> State 1: "Inj. Stopped"
#Else If (INJ_MODE == Normal) -> State 2: "Accumulating."
#Else If !(reached Top-Up Current) -> State 3: "Accumulating"
#Else If (OP-READY == Wait) -> State 4: "Top-up ready"
#Else If (OFB off) -> State 5: "Light-Available" (former State 4)
#Else -> State 6: "Light Available" (former State 5)
return caget("ACOAU-ACCU:OP-MODE", 'i')
def assert_machine_ok(wait = True):
print "Checking machine..."
ID1status=get_id_control(1)
ID2status=get_id_control(2)
Ringstatus=get_ring_status()
oldRingstatus=Ringstatus
checkRing=0
if Ringstatus==5:
log ("FB off")
#checkRing=1
if Ringstatus==0:
log ("Machine down")
checkRing=2
if checkRing>0:
status=False
while not status:
ID1status=get_id_control(1)
ID2status=get_id_control(2)
Ringstatus=get_ring_status()
if oldRingstatus != Ringstatus:
if Ringstatus == 0:
log("Machine down")
checkRing=2
oldRingstatus=Ringstatus
elif Ringstatus == 1:
log("Inj. Stopped")
checkRing=2
oldRingstatus=Ringstatus
elif Ringstatus == 2:
log("Accumulating.")
checkRing=2
oldRingstatus=Ringstatus
elif Ringstatus == 3:
log("Accumulating")
checkRing=2
oldRingstatus=Ringstatus
elif Ringstatus == 4:
log("Top-up ready, Gap still open")
checkRing=2
oldRingstatus=Ringstatus
elif Ringstatus == 5:
log("Light-Available, no OFB")
checkRing=2
oldRingstatus=Ringstatus
elif Ringstatus == 6:
log("Light Available")
checkRing=2
oldRingstatus=Ringstatus
status= (Ringstatus ==6) and (ID1status==1) and (ID2status==1)
if not wait:
raise Exception ("Ring error: " + str(Ringstatus))
time.sleep(1)
if checkRing==2:
log("wait 120 s")
time.sleep(120)
log("continue")
elif checkRing==2:
log("wait 10 s")
time.sleep(10)
log("continue")
print "Machine ok"
def assert_bemline_ok():
print "Checking beamline..."
checkbeamline=0
message=[ "PLC error","Encoder error", "Feedforward error","Operator control", "Moving timeout", "Interlock"]
if ID == "ID1":
ID1status=get_id_status(1)
ID2status=0
elif ID == "ID2":
ID1status=0
ID2status=get_id_status(2)
if ID == "ID1_ID2":
ID1status=get_id_status(1)
ID2status=get_id_status(2)
if ID1status >= 1:
ID1error=get_id_error(1)
for bit in range (5,-1, -1):
if ID1error & (2**bit):
log ("ID1 "+ str(message[bit]))
ID1error=ID1error-(2**bit)
if ID2status >= 1:
ID2error=get_id_error(2)
for bit in range (5,-1, -1):
if ID2error & (2**bit):
log ("ID2 "+ str(message[bit]))
ID2error=ID2error-(2**bit)
if ID1status >= 1:
raise Exception("ID1 error")
if ID2status >= 1:
raise Exception("ID2 error")
###################################################################################################
#Manual log file
###################################################################################################
"""
def get_log_file():
return data_path + "/logs.txt"
def write_logfile(msg):
log(msg, True)
if not os.path.exists(os.path.dirname(get_log_file())):
os.makedirs(os.path.dirname(get_log_file()))
with open( get_log_file(), "a") as myfile:
myfile.write(msgv + "\n")
"""
###################################################################################################
#Energy
###################################################################################################
"""
def put_energy(v):
if get_dry_run(): return
if v>91 and v<2500:
caput("X11PHS:alldone",0)
caput("X11PHS-E:GO.A",v)
else:
log("Energy out of range (91-2500) :" + str(v))
def is_done():
#1: done move
#0: moving
return True if caget("X11PHS:alldone") else False
def wait_done():
if get_dry_run(): return
time.sleep(1.0) #in order to reduce trafic on the IOC
wait_channel("X11PHS:alldone", 1)
"""
def put_energy(v):
energy.write(float(v))
time.sleep(0.5)
wait_channel(ALL_DONE, 1, type = 'i')
def change_energy(v):
if v<91 or v>2500:
raise Exception ("Invalid energy: " + str(v))
if DRY_RUN:
return
put_energy(v)
###################################################################################################
#Image measurements
###################################################################################################
from ijutils import get_measurement, load_array
import ch.psi.pshell.imaging.Filter as Filter
from ch.psi.pshell.imaging.Overlays import Text
import ch.psi.pshell.imaging.Pen as Pen
class MeasurementsFilter(Filter):
def __init__(self, measurements):
self.overlay = Text(Pen(java.awt.Color.GREEN.darker()), "", \
java.awt.Font("Verdana", java.awt.Font.PLAIN, 12), java.awt.Point(20,20))
self.measurements = measurements
self.source = None
self.renderer = None
def process(self, image, data):
try:
ip = load_array(data.array, data.width, data.height)
msg = ""
if self.measurements is not None:
for measurement in self.measurements:
val = get_measurement(ip,measurement)
msg = msg + "%s = %1.4f\n" % (measurement,val)
self.overlay.update(msg)
except:
self.overlay.update(str(sys.exc_info()[1]))
return image
def start(self, source, renderer=None):
self.stop()
self.source = source
self.renderer = renderer if (renderer is not None) else show_panel(source)
self.source.setFilter(self)
self.renderer.addOverlay(self.overlay)
def stop(self):
if self.renderer is not None:
self.renderer.removeOverlay(self.overlay)
if self.source is not None:
self.source.setFilter(None)
self.source = None
self.renderer = None
filter_measurements = None
def start_measurements(measurements=["StdDev"], source = image, renderer=None):
global filter_measurements
stop_measurements()
filter_measurements = MeasurementsFilter(measurements)
filter_measurements.start(string_to_obj(source), string_to_obj(renderer))
def stop_measurements():
global filter_measurements
if filter_measurements is not None:
filter_measurements.stop()
filter_measurements = None
###################################################################################################
#Rsync
###################################################################################################
def _check_sync_user_data(path):
rsync_user = get_setting("RSYNC_USER");
rsync_path = get_setting("RSYNC_PATH");
rsync_host = get_setting("RSYNC_HOST");
rsync_del = str(get_setting("RSYNC_DEL")).lower() == "true"
if rsync_user:
if not rsync_path:
rsync_path = "~/Data1"
else:
rsync_path = "~/Data1/" + rsync_path
if not rsync_host:
rsync_host = "localhost"
if path is not None:
sync_user_data(rsync_user, path, rsync_path, host=rsync_host, remove_local_folder=rsync_del, remove_local_files=False)
time.sleep(5.0)
_FORK_SYNC_USER = True
def check_sync_user_data(path, do_fork=_FORK_SYNC_USER):
if (do_fork):
fork((_check_sync_user_data, (path,)), )
else:
_check_sync_user_data(path)
_CURRENT_DATA_PATH = None
def on_change_data_path(path):
print "on_change_data_path: " + str(path)
global _CURRENT_DATA_PATH
if path is None:
print "Close data path"
if _CURRENT_DATA_PATH is not None:
check_sync_user_data(_CURRENT_DATA_PATH)
else:
print "Open data path: " + str(path)
_CURRENT_DATA_PATH = path
fork(write_metadata)
###################################################################################################
#Sounds
###################################################################################################
def on_command_started(info):
if not get_context().isLocalMode():
if info.script and not info.background:
play_sound("start")
def on_command_finished(info):
if not get_context().isLocalMode():
if info.script and not info.background:
if (info.isError()):
if not info.isAborted():
play_sound("error")
else:
play_sound("success")
import ch.psi.utils.Audio as Audio
import java.io.File as File
def play_sound(name):
try:
Audio.playFile(File(get_context().setup.expandPath("{home}/sounds/" + name + ".wav")), False)
except:
log("Error playing sound " + name + ": " + str(sys.exc_info()[1]), False)
###################################################################################################
#Beamline setup
###################################################################################################
def apply_beamline_setup(ID,ENERGY,POL_ID_1,ALPHA_ID_1,HARMONIC_ID_1,OFFSET_ID_1,POL_ID_2,ALPHA_ID_2,HARMONIC_ID_2,OFFSET_ID_2):
if get_dry_run():
return
if ID=='ID1':
caput('X11PHS-E:OPT',1)
elif ID=='ID2':
caput('X11PHS-E:OPT',2)
elif ID =='ID1_ID2':
caput('X11PHS-E:OPT',3)
if ID == "ID1":
#current_pol=POL_IDS[POL_ID_1] #get_id_pol(1)
put_id_pol(1,POL_ID_1, ALPHA_ID_1 if (POL_ID_1=="Lin") else None )
caput('X11MA-ID2-GAP:SET',100) #open Gap ID2
id1_harmonic.write(int(HARMONIC_ID_1))
put_id_offset(1, float(OFFSET_ID_1)) #offset on ID1
elif ID == "ID2":
#current_pol=POL_IDS[POL_ID_2]#get_id_pol(2)
put_id_pol(2,POL_ID_2, ALPHA_ID_2 if (POL_ID_2=="Lin") else None )
caput('X11MA-ID1-GAP:SET',100) #open Gap ID1
id2_harmonic.write(int(HARMONIC_ID_2))
put_id_offset(2, float(OFFSET_ID_2)) #offset on ID2
elif ID == "ID1_ID2":
#polID1=POL_IDS[POL_ID_1]
#polID2=POL_IDS[POL_ID_2]
put_id_pol(1,POL_ID_1, ALPHA_ID_1 if (POL_ID_1=="Lin") else None )
put_id_pol(2,POL_ID_2, ALPHA_ID_2 if (POL_ID_2=="Lin") else None )
#current_pol=polID1
id1_harmonic.write(int(HARMONIC_ID_1))
id2_harmonic.write(int(HARMONIC_ID_2))
put_id_offset(1, float(OFFSET_ID_1)) #offset on ID1
put_id_offset(2, float(OFFSET_ID_2)) #offset on ID2
#wait_channel("X11PHS:alldone", 1)
energy.write(ENERGY)
wait_channel("X11PHS:alldone", 1)
def set_beamline_setup(ID,ENERGY,POL_ID_1,ALPHA_ID_1,HARMONIC_ID_1,OFFSET_ID_1,POL_ID_2,ALPHA_ID_2,HARMONIC_ID_2,OFFSET_ID_2):
set_setting("ID", ID)
set_setting("ENERGY", ENERGY)
set_setting("POL_ID_1", POL_ID_1)
set_setting("ALPHA_ID_1", ALPHA_ID_1)
set_setting("HARMONIC_ID_1", HARMONIC_ID_1)
set_setting("OFFSET_ID_1", OFFSET_ID_1)
set_setting("POL_ID_2", POL_ID_2)
set_setting("ALPHA_ID_2", ALPHA_ID_2)
set_setting("HARMONIC_ID_2", HARMONIC_ID_2)
set_setting("OFFSET_ID_2", OFFSET_ID_2)
apply_beamline_setup(ID,ENERGY,POL_ID_1,ALPHA_ID_1,HARMONIC_ID_1,OFFSET_ID_1,POL_ID_2,ALPHA_ID_2,HARMONIC_ID_2,OFFSET_ID_2)
#Restore beamline initial config
def restore_beamline_setup():
ID = get_setting("ID")
ENERGY = float(get_setting("ENERGY"))
POL_ID_1 = get_setting("POL_ID_1")
ALPHA_ID_1 = float(get_setting("ALPHA_ID_1"))
HARMONIC_ID_1 = get_setting("HARMONIC_ID_1")
OFFSET_ID_1 = float(get_setting("OFFSET_ID_1"))
POL_ID_2 = get_setting("POL_ID_2")
ALPHA_ID_2 = float(get_setting("ALPHA_ID_2"))
HARMONIC_ID_2 = get_setting("HARMONIC_ID_2")
OFFSET_ID_2 = float(get_setting("OFFSET_ID_2"))
apply_beamline_setup(ID,ENERGY,POL_ID_1,ALPHA_ID_1,HARMONIC_ID_1,OFFSET_ID_1,POL_ID_2,ALPHA_ID_2,HARMONIC_ID_2,OFFSET_ID_2)
###################################################################################################
#Settings
###################################################################################################
def set_dry_run(value):
set_setting("DRY_RUN", bool(value))
def get_dry_run():
return str(get_setting("DRY_RUN")).lower() == "true"
def set_outliers_threshold(value):
set_setting("OUTLIERS_THRESHOLD", int(value))
def get_outliers_threshold():
try:
return int(get_setting("OUTLIERS_THRESHOLD"))
except:
return 0
def get_outliers_mask_file():
return get_setting("OUTLIERS_MASK_FILE")
def write_metadata():
for name in ["proposer", "proposal", "pgroup", "sample"]:
value = get_setting(name)
set_attribute("/", name, value)
setting = get_setting("authors")
set_attribute("/", "authors", setting.split("|") if setting is not None else [""])