Files
x11ma/script/templates/Eiger2Img.py
gac-x11ma 27152efce8
2020-02-04 12:08:13 +01:00

240 lines
6.9 KiB
Python

#If running from editor
if get_exec_pars().source == CommandSource.ui:
METHOD = "Two_Pol"
SWITCHING = "Tune_Detune"
MEASUREMENTS = 4
CONTRAST = 0
AUTO_SAVE = True
AS_SEQUENCE = False
EXPOSURE_1 = 1.0
AVERAGE_1 = 2.0
ENERGY_1 = 850.0
POLARIZATION_1 = "Circ_Plus"
EXPOSURE_2 = 1.0
AVERAGE_2 = 2.0
ENERGY_2= 900.0
POLARIZATION_2 = "Circ_Minus"
DRY_RUN=True
SHOW_IMAGES = True
ID = get_setting("ID")
EXPOSURE = [EXPOSURE_1, EXPOSURE_2]
AVERAGE = [int(AVERAGE_1), int(AVERAGE_2)]
ENERGY = [ENERGY_1, ENERGY_2]
POLARIZATION = [POLARIZATION_1, POLARIZATION_2]
if METHOD == "Two_Energies":
for en in [ENERGY_1, ENERGY_2]:
if en < 89.9 or en > 2000:
raise Exception("Bad energy argument")
path = "{data}/{year}_{month}/{date}" + "/" + METHOD + "_{seq}%03d"
set_exec_pars(path= path, format="txt", layout="table", open=True)
data_path = get_context().setup.expandPath(path)
#get_context().incrementFileSequentialNumber()
def get_image_file_name(cycle=-1, frame_index=0, short_name=False):
root = "" if short_name else (data_path + "/")
if cycle < 0:
ret = root + "i"+"{seq}%03d" + "_" + ("%d" % frame_index) + ".tif"
else:
ret= root + ("%d/" % frame_index) + "i"+"{seq}%03d" + "_" + str(cycle) + ("_%d" % frame_index) + ".tif"
ret = get_context().setup.expandPath(ret)
return ret
"""
def getLEEM():
global startvoltage,objective, LEEMtemp
startvoltage=getStartvoltage()
objective=getObjective()
LEEMtemp=getLEEMtemp()
"""
set_exec_pars(path= path, format="txt", layout="table", open=True)
data_path = get_context().setup.expandPath(path)
def assert_status_ok():
if DRY_RUN: return
assert_machine_ok()
#assert_beamline_ok()
"""
getbeamline
getLEEM
Rem check status of machine
If checkRing = 3 Then
Close
Exit Sub
End If
Rem check status of the beamline
If checkbeamline = 3 Then
Close
Exit Sub
"""
#Initialize vartiables
if not DRY_RUN:
open_vg10()
active_id = 1
current_pol=None
polID1=get_id_pol(1)
polID2=get_id_pol(2)
id_off_1 = get_id_offset(1)
id_off_2 = get_id_offset(2)
#rbkEnergy=energy_rbk.read()
if METHOD == "Two_Pol":
if ID == "ID1":
current_pol = polID1
elif ID == "ID2":
current_pol = polID2
elif ID == "ID1_ID2":
current_pol=polID1
if SWITCHING == "Tune_Detune":
put_id_offset(2, id_off_2-40) #detuneID2
put_id_offset(1, id_off_1) #tuneID2
wait_channel("X11PHS:alldone", 1)
def imageinfo(info):
pass
#Beamline setting
def nextpol():
global current_pol, polID1, polID2, active_id
if SWITCHING == "Normal":
if current_pol==1: return 2 #circ+ -> circ-
if current_pol==2: return 1 #circ+ -> circ-
if current_pol==3: return 4 #lin hor -> lin vert
if current_pol==4: return 3 #lin vert -> lin hor
if current_pol==5: return 5 #lin rot -> lin rot
elif SWITCHING == "Tune_Detune":
if active_id==1: return polID2
if active_id==2: return polID1
def switch_pol():
global active_id
if DRY_RUN:
return
newpol=nextpol()
if ID == "ID1":
put_id_pol(1,newpol)
elif ID == "ID2":
put_id_pol(2,newpol)
elif ID == "ID1_ID2":
if SWITCHING == "Normal":
put_id_pol(1, newpol)
put_id_pol(2, newpol)
elif SWITCHING == "Tune_Detune":
if active_id ==1:
put_id_offset(1, id_off_1-40) #detuneID1
put_id_offset(2, id_off_2) #tuneID2
active_id=2
else:
put_id_offset(2, id_off_2-40) #detuneID2
put_id_offset(1, id_off_1) #tuneID2
active_id=1
time.sleep(1.0)
wait_channel("X11PHS:alldone", 1)
def change_energy(v):
if v<91 or v>2500:
raise Exception ("Invalid energy: " + str(v))
if DRY_RUN:
return
put_energy(v)
def save_image_file(frame, cycle=-1, frame_index=0):
if SHOW_IMAGES:
plot(frame.matrix if (type(frame) == Data) else get_ip_array(frame), name = get_image_file_name(cycle, frame_index, True))
if AUTO_SAVE and (frame is not None):
filename = get_image_file_name(cycle, frame_index)
save_as_tiff(frame, filename, check=True)
log(filename)
print filename
# log("SV:"+Format(startvoltage,"0.000")+" OB:"+Format(objective,"0.00")+" ST:"+Format(LEEMtemp,"0.0"))
eiger.stop()
eiger.grabMode=eiger.GrabMode.Single
try:
#Do the measurement loop
for cycle in range(1, MEASUREMENTS + 1):
frames = []
if MEASUREMENTS > 1:
log("")
log("nround = " + str(cycle) + " / " + str(MEASUREMENTS))
if METHOD == "Two_Energies":
change_energy(ENERGY_1)
###??? SETAR POLARIZATION?
time.sleep(1)
for i in range(2):
assert_status_ok()
eiger.setExposure(EXPOSURE[i])
if AS_SEQUENCE:
raise Exception("As sequence not implemented")
# TODO
else:
ret = grab_frames(image, AVERAGE[i], roi=None, wait_next=True)
frames.append(average_frames(ret) if AVERAGE[i] > 1 else ret[0])
imageinfo("I")
save_image_file(frames[i], cycle, i)
if METHOD == "Take_Image":
break
if METHOD == "Two_Pol":
switch_pol()
if METHOD == "Two_Energies":
change_energy(ENERGY_2)
time.sleep(1)
if (METHOD == "Two_Pol") or (METHOD == "Two_Energies"):
if (METHOD == "Two_Energies") or ((current_pol == 2) or (current_pol==3)):
###??? autocontrast???
frames.append(frames[0].copy())
frames[2].div(frames[1])
else:
frames.append(frames[1].copy())
frames[2].div(frames[0])
save_image_file(frames[2],cycle, 2)
# objective = getObjective()
# temp = getLEEMtemp()
# log(0 , "Temp : " + Format(temp,"0.00") + " OB : " + Format(objective,"0.00") + " StigmaA : " + " StigmaB : " )
# Auto average and save
if MEASUREMENTS > 1 and AUTO_SAVE == 1:
for i in range(len(frames)):
measures = []
for cycle in range(1, MEASUREMENTS + 1):
filename = get_image_file_name(cycle, i)
ip = open_image(filename)
#measures.append(Data(get_ip_array(ip)))
measures.append(ip)
#av = average_frames(measures) #Result is transposed???
av=average_ips (measures, roi=None, as_float=True)
save_image_file(av, -1, i)
finally:
if not DRY_RUN:
close_vg10()
eiger.grabMode=eiger.GrabMode.Continuous
eiger.start()