Files
x11ma/script/local.py
gac-x11ma e7b60c995b
2020-01-14 17:14:16 +01:00

428 lines
15 KiB
Python

###################################################################################################
# Deployment specific global definitions - executed after startup.py
###################################################################################################
import ntpath
import traceback
"""
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"
"""
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
###################################################################################################
run("devices/eiger")
run("devices/id")
run("devices/chopper")
###################################################################################################
#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):
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")
def assert_bemline_ok():
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 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 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')