253 lines
9.9 KiB
Python
253 lines
9.9 KiB
Python
#If running from editor
|
|
if (get_exec_pars().source == CommandSource.ui) and (get_exec_pars().script == "Eiger2Img") :
|
|
METHOD = "Two_Pol"
|
|
SWITCHING = "Tune_Detune"
|
|
SEQUENCE = "A"
|
|
MEASUREMENTS = 4
|
|
AUTO_SAVE = True
|
|
DRIFT_CORRECTION = True
|
|
EXPOSURE = 1.0
|
|
AVERAGE = 2.0
|
|
ENERGY_1 = 850.0
|
|
ENERGY_2= 900.0
|
|
SWITCH_POL=False
|
|
NUMBER_SCANS =1
|
|
|
|
print "Starting 2Img: ", METHOD, SWITCHING, SEQUENCE , MEASUREMENTS, AUTO_SAVE, EXPOSURE, AVERAGE, ENERGY_1, ENERGY_2, SWITCH_POL,NUMBER_SCANS
|
|
#SEQUENCE = "A": C+,C-,C-,C+,C+;C-,C-,C+,
|
|
|
|
#SEQUENCE = "B": C+,C-,C+,C-,C+,-;C+,C-.
|
|
|
|
SHOW_IMAGES = True
|
|
SAVE_DIAGS = True
|
|
|
|
ID = get_setting("ID")
|
|
|
|
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")
|
|
|
|
start = time.time()
|
|
threads = []
|
|
image_drift=None
|
|
calculated_shifts={}
|
|
data_path = "{data}/{year}_{month}/{date}/{date}_{name}"
|
|
set_exec_pars(name=METHOD, path = data_path, format="txt", layout="table", open=True)
|
|
|
|
|
|
def get_image_file_name(cycle=-1, frame_index=0, short_name=False, shifted=False):
|
|
global max_index
|
|
if (METHOD == "Two_Pol") and (SEQUENCE == "A") and (frame_index<2) and ((cycle%2)==0): #Swapped
|
|
frame_index = 0 if frame_index else 1
|
|
root = "" if short_name else (data_path + "/")
|
|
abs_index=run_index + frame_index
|
|
max_index= max(max_index, frame_index)
|
|
prefix = Setup.expandPath("i{date}%02d_" + ("%03d" % (abs_index,)))
|
|
|
|
if cycle < 0:
|
|
if (shifted):
|
|
prefix = "c" + prefix
|
|
ret = root + prefix + ".tif"
|
|
else:
|
|
prefix = prefix + ("_corr/c" if shifted else "/")
|
|
ret= root + prefix + "i{date}%02d"+ ("_%03d#%03d" % (abs_index,cycle)) + ".tif"
|
|
return Setup.expandPath(ret)
|
|
|
|
|
|
"""
|
|
def getLEEM():
|
|
global startvoltage,objective, LEEMtemp
|
|
startvoltage=getStartvoltage()
|
|
objective=getObjective()
|
|
LEEMtemp=getLEEMtemp()
|
|
"""
|
|
|
|
#Initialize vartiables
|
|
if not get_dry_run() and str(get_setting("AUTO_SWITCH_VALVE")).lower() == "true":
|
|
open_vg10()
|
|
|
|
if not get_dry_run() and str(get_setting("AUTO_SWITCH_SHUTTER")).lower() == "true":
|
|
caput(FAST_SHTR, 'Open')
|
|
|
|
#rbkEnergy=energy_rbk.read()
|
|
if METHOD == "Two_Pol" or (SWITCH_POL and (METHOD == "Take_Image")):
|
|
init_pol_switch(SWITCHING)
|
|
|
|
def imageinfo(info):
|
|
pass
|
|
|
|
ref_image = None
|
|
def save_image_file(frame, cycle=-1, frame_index=0, processed_shift=False):
|
|
global ref_image
|
|
if frame is None:
|
|
raise Exception("Cannot save frame - None: cycle: " + str(cycle) + " index: " + str(frame_index))
|
|
if SHOW_IMAGES:
|
|
plot_name = get_image_file_name(cycle, frame_index, True)
|
|
plot_data = frame.matrix if (type(frame) == Data) else get_ip_array(frame)
|
|
if plot_data is None:
|
|
raise Exception("Cannot save frame - plot_data is None: cycle: " + str(cycle) + " index: " + str(frame_index))
|
|
plots=plot(plot_data, name = plot_name)
|
|
if (len(plots)==0):
|
|
msg = "Error creating plots - cycle: " + str(cycle) + " index: " + str(frame_index)
|
|
print msg
|
|
log(msg)
|
|
else:
|
|
p=plots[0]
|
|
p.getAxis(p.AxisId.Y).setInverted(True)
|
|
if AUTO_SAVE and (frame is not None):
|
|
filename = get_image_file_name(cycle, frame_index)
|
|
if processed_shift:
|
|
filename = get_image_file_name(cycle, frame_index, shifted=True)
|
|
metadata = get_diags() if SAVE_DIAGS else {}
|
|
|
|
threads.append(save_as_tiff(frame, filename, metadata=metadata))
|
|
|
|
log(filename)
|
|
print filename
|
|
|
|
if DRIFT_CORRECTION and (not processed_shift):
|
|
filename = get_image_file_name(cycle, frame_index, shifted=True)
|
|
#print " *** ", cycle, frame_index
|
|
if cycle>0:
|
|
if ref_image is None:
|
|
if (cycle!=1) or (frame_index!=0):
|
|
raise Exception ("Invalid reference image: cycle=" + str(cycle) + " index="+ str (frame_index))
|
|
ref_image = frame
|
|
threads.append(save_as_tiff(frame, filename, metadata=metadata))
|
|
else:
|
|
if frame_index==2:
|
|
#Done later
|
|
pass
|
|
else:
|
|
threads.append(shift_and_save_as_tiff(frame, filename, ref_image, calc_roi, metadata=metadata))
|
|
|
|
init_eiger(exposure=EXPOSURE)
|
|
|
|
run_index = Context.getDaySequentialNumber()
|
|
print " ---- Run index : %d" %(run_index,)
|
|
max_index=0
|
|
|
|
if DRIFT_CORRECTION:
|
|
roi = IMAGING_ROI
|
|
if roi is None:
|
|
roi = DEFAULT_ROI
|
|
calc_roi = Roi(roi.x,roi.y,roi.width, roi.height)
|
|
ref_image = None # grab_frame(image, None, True)
|
|
|
|
#print "Startup time: " + str(time.time() - start)
|
|
try:
|
|
for scan in range(NUMBER_SCANS):
|
|
#Do the measurement loop
|
|
for cycle in range(1, MEASUREMENTS + 1):
|
|
frames = []
|
|
if MEASUREMENTS > 1:
|
|
log("nround = " + str(cycle) + " / " + str(MEASUREMENTS))
|
|
|
|
|
|
for i in range(2):
|
|
if (cycle == 1) or (METHOD != "Take_Image"):
|
|
assert_status_ok()
|
|
if METHOD == "Two_Energies":
|
|
change_energy(ENERGY[i])
|
|
print "--- Grabing " + str(AVERAGE)+ " frames - cycle: " + str(cycle) + " step: " + str(i)
|
|
av = average_eiger_frames(AVERAGE, roi=None, wait_next=True)
|
|
frames.append(av)
|
|
imageinfo("I")
|
|
save_image_file(frames[i], cycle, i)
|
|
if METHOD == "Take_Image":
|
|
break
|
|
|
|
if METHOD == "Two_Pol":
|
|
if (i==0) or (SEQUENCE == "B"):
|
|
switch_pol()
|
|
|
|
if (METHOD == "Two_Pol") or (METHOD == "Two_Energies"):
|
|
if (METHOD == "Two_Energies") or (SEQUENCE == "B") or ((cycle%2)==1):
|
|
print "Divide first frame by second"
|
|
frames.append(frames[0].copy())
|
|
frames[2].div(frames[1])
|
|
else:
|
|
print "Divide second frame by first"
|
|
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
|
|
|
|
print "Waiting file writing threads to finish..."
|
|
for t in threads:
|
|
join(t)
|
|
|
|
if (MEASUREMENTS > 1) and (AUTO_SAVE == 1):
|
|
if DRIFT_CORRECTION and len(frames)==3:
|
|
print "Dividing drifted images..."
|
|
for cycle in range(1, MEASUREMENTS + 1):
|
|
if (METHOD == "Two_Energies") or (SEQUENCE == "B") or ((cycle%2)==1):
|
|
file1 = get_image_file_name(cycle, 0, shifted=True)
|
|
file2 = get_image_file_name(cycle, 1, shifted=True)
|
|
else:
|
|
file1 = get_image_file_name(cycle, 1, shifted=True)
|
|
file2 = get_image_file_name(cycle, 0, shifted=True)
|
|
print file1
|
|
print file2
|
|
ip1 = open_image(file1)
|
|
ip2 = open_image(file2)
|
|
op_image(ip1, ip2, "divide", float_result=True, in_place=True)
|
|
save_image_file(ip1,cycle, 2, processed_shift=True)
|
|
|
|
print "--- Averaging..."
|
|
for i in range(len(frames)):
|
|
integration = None
|
|
for cycle in range(1, MEASUREMENTS + 1):
|
|
filename = get_image_file_name(cycle, i)
|
|
print "Open " , filename
|
|
ip = open_image(filename)
|
|
if integration is None:
|
|
integration = ip
|
|
else:
|
|
integration =integrate_ips ([integration, ip], as_float=True)
|
|
av=op_const(integration, "divide", float(MEASUREMENTS), in_place=True)
|
|
save_image_file(av, -1, i)
|
|
|
|
if DRIFT_CORRECTION:
|
|
integration = None
|
|
measurements = 0
|
|
for cycle in range(1, MEASUREMENTS + 1):
|
|
filename = get_image_file_name(cycle, i, shifted=True)
|
|
print "Open " , filename
|
|
try:
|
|
ip = open_image(filename)
|
|
if integration is None:
|
|
integration = ip
|
|
else:
|
|
integration =integrate_ips ([integration, ip], as_float=True)
|
|
measurements=measurements+1
|
|
except:
|
|
print "Error integrating frame: ", filename
|
|
if measurements>0:
|
|
av=op_const(integration, "divide", float(measurements), in_place=True)
|
|
save_image_file(av, -1, i, processed_shift=True)
|
|
|
|
if SWITCH_POL and (METHOD == "Take_Image"):
|
|
switch_pol()
|
|
|
|
#TODO: This is a workaround to handle 2-Images, should be removed
|
|
run_index = run_index + 1
|
|
if scan>0:
|
|
max_index=max_index+1
|
|
print " ---- New un index : %d" %(run_index,)
|
|
finally:
|
|
for i in range(max_index):
|
|
Context.incrementDaySequentialNumber()
|
|
if not get_dry_run() and str(get_setting("AUTO_SWITCH_VALVE")).lower() == "true":
|
|
close_vg10()
|
|
if not get_dry_run() and str(get_setting("AUTO_SWITCH_SHUTTER")).lower() == "true":
|
|
caput(FAST_SHTR, 'Close')
|
|
restore_eiger()
|
|
print "Running time: " + str(time.time() - start) |