Files
x11ma/script/local.py
gac-x11ma d6803e82ba
2022-10-05 09:33:25 +02:00

963 lines
36 KiB
Python

###################################################################################################
# Deployment specific global definitions - executed after startup.py
###################################################################################################
import ntpath
import traceback
from rsync import *
from mathutils import estimate_peak_indexes, fit_gaussians, create_fit_point_list, Gaussian
from mathutils import fit_polynomial,fit_gaussian, fit_harmonic, calculate_peaks
from mathutils import PolynomialFunction, Gaussian, HarmonicOscillator
from collections import OrderedDict
run ("imaging/shifts")
ProviderCSV.setDefaultItemSeparator(", ")
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"
FAST_SHTR= "X11MA-VME-ES1:FAST-SHTR"
"""
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"
"
"""
#Device initialization
keithley_1a.setForcedRead(False)
keithley_2a.setForcedRead(False)
keithley_3a.setForcedRead(False)
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)))
###################################################################################################
#Scan commands
###################################################################################################
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})
def otf2(start, end, time, delay = 0.0, mode = None, offset = None, alpha = None, name = None, folder = 'TEST'):
"""
"""
#run("templates/EnergyScan_v2", {"E1":start, "E2":end, "TIME":time, "DELAY":float(delay), "MODE":mode, "OFFSET":(offset), "FOLDER":folder, "FILE":name, "TAG":name, "ALPHA":float(alpha) if alpha is not None else None})
#run("templates/EnergyScan_v3", {"E1":start, "E2":end, "TIME":time, "DELAY":float(delay), "MODE":mode, "OFFSET":(offset), "FOLDER":folder, "FILE":name, "TAG":name, "ALPHA":float(alpha) if alpha is not None else None})
#EnergySCan_v3 was created by CP. Apr/22
run("templates/EnergyScan_v4", {"E1":start, "E2":end, "TIME":time, "DELAY":float(delay), "MODE":mode, "OFFSET":(offset), "FOLDER":folder, "FILE":name, "TAG":name, "ALPHA":float(alpha) if alpha is not None else None})
def otf_img(start, end, time, delay = 0.0, exposure=0.2, roi=None, name = None, save_images=False):
folder = get_context().setup.expandPath("{year}_{month}/{date}");
if is_string(roi):
with open(expand_path("{config}/ROIs/" + roi + ".roi"), 'rb') as f:
roi = OrderedDict()
for r in Serializer.decode(f.read(), Serializer.EncoderType.bin)[0]:
roi[str(r[0])] = [int(r[1]), int(r[2]), int(r[3]), int(r[4])]
run("templates/EnergyScan_img", {"E1":start, "E2":end, "TIME":time, "DELAY":float(delay), "FOLDER":folder, "FILE":name, "ROI":roi, "EXPOSURE":exposure, "SAVE_IMAGES": save_images})
def wait_pol_done(delay=1.0):
#taken from the X-Treme beamline. Used in EnergyScan_v3.py. CP. Apr/22
print "Waiting for pol done"
time.sleep(delay) #Make sure value changed
while True:
try:
if caget("X11MA-ID2:DONE") == "DONE":
break
except:
print "Error reading pol done"
time.sleep(0.5)
print "Done"
def two_pol(switching="Tune_Detune", sequence="A", measurements=1, exposure=1.0, average=1, name = None):
set_exec_pars(open=False)
if name:
set_exec_pars(name=name)
run("templates/Eiger2Img", { \
"METHOD": "Two_Pol", "AUTO_SAVE": True, \
"MEASUREMENTS": int(measurements), "EXPOSURE": float(exposure), "AVERAGE": int(average), \
"SWITCHING": switching, "SEQUENCE": sequence, \
"ENERGY_1":None, "ENERGY_2":None, \
"NUMBER_SCANS":1, "SWITCH_POL": False, \
})
def two_energies(energy_1, energy_2, measurements=1, exposure=1.0, average=1, name = None):
set_exec_pars(open=False)
if name:
set_exec_pars(name=name)
run("templates/Eiger2Img", { \
"METHOD": "Two_Energies", "AUTO_SAVE": True, \
"MEASUREMENTS": int(measurements), "EXPOSURE": float(exposure), "AVERAGE": int(average), \
"SWITCHING": None, "SEQUENCE": None, \
"ENERGY_1":energy_1, "ENERGY_2":energy_2, \
"NUMBER_SCANS":1, "SWITCH_POL": False, \
})
def take_image(scans=1, switch_pol=False, measurements=1, exposure=1.0, average=1, name = None):
set_exec_pars(open=False)
if name:
set_exec_pars(name=name)
run("templates/Eiger2Img", { \
"METHOD": "Take_Image", "AUTO_SAVE": True, \
"MEASUREMENTS": int(measurements), "EXPOSURE": float(exposure), "AVERAGE": int(average), \
"SWITCHING": None, "SEQUENCE": None, \
"ENERGY_1":None, "ENERGY_2":None, \
"NUMBER_SCANS":scans, "SWITCH_POL": switch_pol, \
})
def abs_spec(ranges, roi=None, switch_pol=False, scans=1, exposure=1.0, average=1, name = None):
set_exec_pars(open=False)
if is_string(ranges):
with open(expand_path("{config}/AbsortionSpectrum/" + ranges + ".abs"), 'r') as f:
ranges = Serializer.decode(f.read(), Serializer.EncoderType.bin)[0]
ranges = to_array(ranges,'d')
if is_string(roi):
with open(expand_path("{config}/ROIs/" + roi + ".roi"), 'rb') as f:
roi = OrderedDict()
for r in Serializer.decode(f.read(), Serializer.EncoderType.bin)[0]:
roi[str(r[0])] = [int(r[1]), int(r[2]), int(r[3]), int(r[4])]
if name:
set_exec_pars(name=name)
if roi is None:
if ("DAQ_PANEL_ROIS" in globals()) and (len(DAQ_PANEL_ROIS) > 0):
roi = DAQ_PANEL_ROIS
else:
width, height = eiger.getImageSize()
roi = {"Region1": [0, 0, width, height]}
run("templates/EigerAbsSpec", { \
"ROI": roi, "SAVE_SPECTRUM": True, "SAVE_IMAGES":True, "WITH_I0": True, \
"RANGES": ranges, "SWITCH_POL": switch_pol, \
"NUMBER_SCANS": int(scans), "EXPOSURE": float(exposure), "AVERAGE": int(average), \
})
###################################################################################################
#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 [""])
def beamline_auto(value):
caput("X11MA:AUTO-ONOFF",value)
###################################################################################################
#Devices
###################################################################################################
for dev in ["eiger", "id", "chopper", "LEEM2000", "diag"]:
try:
run("devices/" + dev)
except:
log(sys.exc_info()[1], False)
###################################################################################################
#Beamline and Machine status
###################################################################################################
def assert_machine_ok(wait = True):
print "Checking machine..."
if get_dry_run():
return
ring_status=get_ring_status()
old_ring_status=ring_status
checkRing=0
if ring_status==5:
log ("FB off")
#checkRing=1
if ring_status==0:
log ("Machine down")
checkRing=2
if checkRing>0:
status=False
while not status:
id1_status=get_id_control(1)
id2_status=get_id_control(2)
ring_status=get_ring_status()
if old_ring_status != ring_status:
if ring_status == 0:
log("Machine down")
checkRing=2
elif ring_status == 1:
log("Inj. Stopped")
checkRing=2
elif ring_status == 2:
log("Accumulating.")
checkRing=2
elif ring_status == 3:
log("Accumulating")
checkRing=2
elif ring_status == 4:
log("Top-up ready, Gap still open")
checkRing=2
elif ring_status == 5:
log("Light-Available, no OFB")
checkRing=2
elif ring_status == 6:
log("Light Available")
checkRing=2
old_ring_status=ring_status
status= (ring_status ==6) and (id1_status==1) and (id2_status==1)
if not wait:
raise Exception ("Ring error: " + str(ring_status))
time.sleep(1)
print "Machine ok"
def assert_beamline_ok():
print "Checking beamline..."
if get_dry_run():
return
checkbeamline=0
message=[ "PLC error","Encoder error", "Feedforward error","Operator control", "Moving timeout", "Interlock"]
ID = get_setting("ID")
if ID == "ID1":
id1_status=get_id_status(1)
id2_status=0
elif ID == "ID2":
id1_status=0
id2_status=get_id_status(2)
if ID == "ID1_ID2":
id1_status=get_id_status(1)
id2_status=get_id_status(2)
if id1_status >= 1:
id1_error=get_id_error(1)
for bit in range (5,-1, -1):
if id1_error & (2**bit):
log ("ID1 "+ str(message[bit]))
id1_error=id1_error-(2**bit)
if id2_status >= 1:
id2_error=get_id_error(2)
for bit in range (5,-1, -1):
if id2_error & (2**bit):
log ("ID2 "+ str(message[bit]))
id2_error=id2_error-(2**bit)
if id1_status >= 1:
raise Exception("ID1 error: " + str(id1_status))
if id2_status >= 1:
raise Exception("ID2 error: "+ str(id2_status))
def assert_status_ok(wait = True):
assert_machine_ok(wait)
assert_beamline_ok()
###################################################################################################
#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))
print "Setting energy: " + str(v)
if get_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 = None, renderer=None):
global filter_measurements
if source is None:
source = image
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
###################################################################################################
#Image arrows
###################################################################################################
if eiger.initialized:
run("imaging/fov_arrows")
else:
msg="Cannot initialize arrows: eiger not initialized"
log(msg)
print msg
###################################################################################################
#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:
#TODO: sound does not work when logged as e-account
if not Sys.getUserName().startswith("e"):
log("Error playing sound " + name + ": " + str(sys.exc_info()[1]), False)
###################################################################################################
#Beamline setup
###################################################################################################
def apply_beamline_setup(id,en=None,pol1=None,alp1=None,har1=None,off1=None,pol2=None,alp2=None,har2=None,off2=None,grat=None,order=None,Cff=None):
print "Setting Beamline: ", en,pol1,alp1,har1,off1,pol2,alp2,har2,off2,grat,order,Cff
if get_dry_run():
return
if id=='PGM_ID1':
caput('X11PHS-E:OPT',1)
elif id=='PGM_ID2':
caput('X11PHS-E:OPT',2)
elif id =='PGM_ID1_ID2':
caput('X11PHS-E:OPT',3)
if id == "PGM_ID1":
#current_pol=POL_IDS[pol1] #get_id_pol(1)
if pol1 is not None:
put_id_pol(1,pol1, alp1 if (pol1=="Lin") else None )
caput('X11MA-ID2-GAP:SET',100) #open Gap ID2
if har1 is not None:
id1_harmonic.write(int(har1))
if off1 is not None:
put_id_offset(1, float(off1)) #offset on ID1
elif id == "PGM_ID2":
#current_pol=POL_IDS[pol2]#get_id_pol(2)
if pol2 is not None:
put_id_pol(2,pol2, alp2 if (pol2=="Lin") else None )
caput('X11MA-ID1-GAP:SET',100) #open Gap ID1
if har2 is not None:
id2_harmonic.write(int(har2))
if off2 is not None:
put_id_offset(2, float(off2)) #offset on ID2
elif id == "PGM_ID1_ID2":
#polID1=POL_IDS[pol1]
#polID2=POL_IDS[pol2]
if pol1 is not None:
put_id_pol(1,pol1, alp1 if (pol1=="Lin") else None )
if pol2 is not None:
put_id_pol(2,pol2, alp2 if (pol2=="Lin") else None )
#current_pol=polID1
if har1 is not None:
id1_harmonic.write(int(har1))
if har2 is not None:
id2_harmonic.write(int(har2))
if off1 is not None:
put_id_offset(1, float(off1)) #offset on ID1
if off2 is not None:
put_id_offset(2, float(off2)) #offset on ID2
# Monochromator part
if grat is not None:
if grat=='G1_300':
Grating.write(0)
elif grat=='G2_1200':
Grating.write(1)
elif grat =='G3_600':
Grating.write(2)
if order is not None:
DiffOrd.write(order)
if Cff is not None:
caput("X11MA-PGM:cff.A",Cff)
#wait_channel("X11PHS:alldone", 1)
if en is not None:
energy.write(en)
time.sleep(0.5)
wait_channel("X11PHS:alldone", 1)
def change_pol(id, pol, alpha=None):
print "Changing polarization on id " + str(id), ": " + str(pol) , " - alpha=" + str(alpha)
if get_dry_run():
return
put_id_pol(id, pol, alpha)
time.sleep(0.5)
wait_channel("X11PHS:alldone", 1)
def change_offset(id, offset):
print "Changing offset on id " + str(id), ": " + str(offset)
if get_dry_run():
return
put_id_offset(id, float(offset))
time.sleep(0.5)
wait_channel("X11PHS:alldone", 1)
def set_beamline_setup(id,en=None,pol1=None,alp1=None,har1=None,off1=None,pol2=None,alp2=None,har2=None,off2=None,grat=None,order=None,Cff=None):
set_setting("ID", id)
set_setting("ENERGY", en)
set_setting("POL_ID_1", pol1)
set_setting("ALPHA_ID_1", alp1)
set_setting("HARMONIC_ID_1", har1)
set_setting("OFFSET_ID_1", off1)
set_setting("POL_ID_2", pol2)
set_setting("ALPHA_ID_2", alp2)
set_setting("HARMONIC_ID_2", har2)
set_setting("OFFSET_ID_2", off2)
set_setting("GRATING", grat)
set_setting("DIFF_ORD", order)
set_setting("CFF", Cff)
apply_beamline_setup(id,en,pol1,alp1,har1,off1,pol2,alp2,har2,off2,grat,order,Cff)
#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"))
GRATING = get_setting("GRATING")
DIFF_ORD = float(get_setting("DIFF_ORD"))
CFF =float(get_setting("CFF"))
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,GRATING,DIFF_ORD,CFF)
# peem default setups
def peem_optics_default_15kV():
# read and save the values
objective.write(1475)
obj_stig_a.write(0)
obj_stig_b.write(0)
obj_align_x.write(0)
obj_align_y.write(0)
start_voltage.write(0)
# fov.write(100)
def peem_optics_default_10kV():
objective.write(1220)
obj_stig_a.write(0)
obj_stig_b.write(0)
obj_align_x.write(0)
obj_align_y.write(0)
start_voltage.write(0)
# x-rays default setup
def xrays_default():
exit_slit.write(300.0)
caput ('X11MA-FE-DSAPER', 0) # close FE-slits
set_beamline_setup(id ="ID2", en=800, pol2="Lin_Hor", grat="G3_600", order=1, Cff=2.25)
def neutralize_position(retries = 3):
for i in range(retries):
try:
if abs(manip_x.readback.read()) > 50:
manip_x.write(0.0)
if abs(manip_y.readback.read()) > 50:
manip_y.write(0.0)
break
except:
if i>=(retries-1):
raise
log("neutralize_position error - retrying: " + sys.exc_info()[1])
time.sleep(1)
microscope.home_tilt('H')
microscope.home_tilt('V')
###################################################################################################
#Maths
###################################################################################################
def fit(ydata, xdata = None, limit_to_range=False):
"""
Gaussian fit
"""
if xdata is None:
xdata = frange(0, len(ydata), 1)
#ydata = to_list(ydata)
#xdata = to_list(xdata)
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)
p = plot([ydata],["data"],[xdata], title="Fit" )[0]
gaussians = fit_gaussians(ydata, xdata, [index_max,])
if gaussians[0] is None:
if limit_to_range:
print "Max -> " + str(max_x)
p.addMarker(max_x, None, "Max="+str(round(max_x,2)), Color.MAGENTA.darker())
return (None , max_x, None)
else:
print "Fit error"
return (None, None, None)
(norm, mean, sigma) = gaussians[0]
print " mean:" + str(mean)
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]) < abs((scale_x[0] + scale_x[1])/2):
if limit_to_range and not (scale_x[0] <= mean <= scale_x[1]):
print "Limiting to range ", scale_x, " max=", str(max_x)
p.addMarker(max_x, None, "Max="+str(round(max_x,2)), Color.MAGENTA.darker())
return (None , max_x, None)
print "Mean -> " + str(mean)
p.addMarker(mean, None, "Mean="+str(round(norm,2)), Color.MAGENTA.darker())
return (norm, mean, sigma)
else:
if limit_to_range:
print "Max -> " + str(max_x)
p.addMarker(max_x, None, "Max="+str(round(max_x,2)), Color.MAGENTA.darker())
return (None , max_x, None)
else:
p.addMarker(max_x, None, "Max="+str(round(max_x,2)), Color.GRAY)
print "Invalid gaussian fit: " + str(mean)
return (None, None, None)
###################################################################################################
#Additional initialization
###################################################################################################
run("templates/FocusScan")
def auto_focus(scan_type="all", range_obj=4.0, step_obj=0.2, range_stig=20.0, step_stig=2.0, average=1, runs=2, renderer = None, roi=None, exposure=None):
"""
RANGE_OBJ = 4.0
STEP_OBJ = 0.2
RANGE_STIG = 20.0
STEP_STIG = 2.0
UPDATE_POSITION = True
AVERAGE = 1
RUNS = 2
width, height = eiger.getImageSize()
ROI = Rectangle(width/3, height/3, width/3, height/3,)
"""
run("templates/AutoFocus", { \
"SCAN_TYPE": scan_type, \
"RANGE_OBJ": float(range_obj), \
"STEP_OBJ": float(step_obj), \
"RANGE_STIG": float(range_stig), \
"STEP_STIG": float(step_stig), \
"AVERAGE": int(average), \
"RUNS": int(runs), \
"UPDATE_POSITION": True, \
"RENDERER": renderer, \
"ROI":roi, \
"EXPOSURE":exposure \
})
def scan_intensity(roi=None):
return scan_contrast(girder_x, 0.05, 0.005, roi, average=3)
def auto_intensity(roi=None, exposure=None):
init_eiger(exposure=exposure)
try:
return scan_intensity(roi)
finally:
restore_eiger()