Files
x11ma/script/templates/Eiger2Img.py
gac-x11ma e39b1864e7
2020-01-15 09:15:50 +01:00

249 lines
6.8 KiB
Python

#If running from editor
if get_exec_pars().source == CommandSource.ui:
ID = "ID1"
SWITCHING = "Normal"
POL_ID_1 = "Circ_Plus"
ALPHA_ID_1 = 0.0
HARMONIC_ID_1 = 1
OFFSET_ID_1 = 0.0
POL_ID_2 = "Circ_Minus"
ALPHA_ID_2 = 0.0
HARMONIC_ID_2 = 1
OFFSET_ID_2 = 0.0
ROI = {"ROI1": [50,50,200, 100]}
METHOD = "Two_Pol"
MEASUREMENTS = 2
CONTRAST = 0
AUTO_SAVE = True
AS_SEQUENCE = False
EXPOSURE_1 = 1.0
AVERAGE_1 = 2.0
ENERGY_1 = 1000.0
POLARIZATION_1 = "Circ_Plus"
EXPOSURE_2 = 1.0
AVERAGE_2 = 2.0
ENERGY_2= 1000.0
POLARIZATION_2 = "Circ_Minus"
DRY_RUN=True
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")
set_exec_pars(path= path, format="txt", layout="table", open=True)
path = "{data}/{year}_{month}/{date}" + "/" + METHOD + "_{seq}%03d"
data_path = get_context().setup.expandPath(path)
#get_context().incrementFileSequentialNumber()
frames = [None, None, None]
def get_image_file_name(cicle=-1, frame_index=0):
if cicle < 0:
return data_path + "/" + ("%04d" % i) + ".tif"
return data_path + "/" + str(cycle) + "_" + str(MEASUREMENTS) + "_" + ("%04d" % frame_index) + ".tif"
def nextpol():
global current_pol, polID1, polID2
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 activeID==1: return polID2
if activeID==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, OFFSET_ID_1-40) #detuneID1
put_id_offset(2, OFFSET_ID_2) #tuneID2
active_id=2
else:
put_id_offset(2, OFFSET_ID_2-40) #detuneID2
put_id_offset(1, OFFSET_ID_1) #tuneID2
active_id=1
elif SWITCHING == "Chopper":
if active_id == 1:
active_id = 2
set_chopper(1)
else:
active_id = 1
set_chopper(0)
time.sleep(1.0)
def change_energy(v):
if v<91 or v>2500:
raise Exception ("Invalid energy: " + str(v))
if DRY_RUN:
return
put_energy(v)
"""
def getLEEM():
global startvoltage,objective, LEEMtemp
startvoltage=getStartvoltage()
objective=getObjective()
LEEMtemp=getLEEMtemp()
"""
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
active_id = 1
current_pol=None
polID1=None
polID2=None
#rbkEnergy=energy_rbk.read()
if METHOD == "Two_Pol":
global current_pol
if ID == "ID1":
current_pol=POL_IDS[POL_ID_1] #get_id_pol(1)
put_id_pol(1,current_pol)
elif ID == "ID2":
current_pol=POL_IDS[POL_ID_2]#get_id_pol(2)
put_id_pol(2,current_pol)
elif ID == "ID1_ID2":
polID1=POL_IDS[POL_ID_1]
polID2=POL_IDS[POL_ID_2]
put_id_pol(1,polID1)
put_id_pol(2,polID2)
current_pol=polID1
if SWITCHING == "Tune_Detune":
put_id_offset(2, OFFSET_ID_2-40) #detuneID2
put_id_offset(1, OFFSET_ID_1) #tuneID2
elif SWITCHING == "Chopper":
set_chopper(0)
def imageinfo(info):
pass
#Do the measurement loop
for cycle in range(1, MEASUREMENTS + 1):
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[i] = average_frames(ret) if AVERAGE[i] > 1 else ret[0]
imageinfo("I")
if METHOD == "Take_Image":
break
if i == 0:
if METHOD == "Two_Pol":
switch_pol()
if METHOD == "Two_Energies":
change_energy(ENERGY_2)
wait_done()
time.sleep(1)
if METHOD == "Two_Pol":
if (current_pol == 2) or (current_pol==3):
###??? autocontrast???
frames[2] = frames[0].copy()
frames[2].div(frames[1])
else:
frames[2] = frames[1].copy()
frames[2].div(frames[0])
if METHOD == "Two_Energies":
frames[2] = frames[0].copy()
frames[2].div(frames[1])
if AUTO_SAVE:
for i in range(len(frames)):
filename = get_image_file_name(cycle, i)
save_as_tiff(frames[i], filename, check=True)
log(filename)
print filename
# +" "+imagetext)
# log("SV:"+Format(startvoltage,"0.000")+" OB:"+Format(objective,"0.00")+" ST:"+Format(LEEMtemp,"0.0"))
if METHOD == "Take_Image":
break
else:
pass
# 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)
filename = get_image_file_name(-1, i)
save_as_tiff(av, filename, check=True)
print filename
if METHOD == "Take_Image":
break