215 lines
7.3 KiB
Python
215 lines
7.3 KiB
Python
#Parameters (global variables):
|
|
# ranges: list of RangeSelection havinf args = (step_size, step_time, iterations)
|
|
# pass_energy
|
|
# save_scienta_image
|
|
#
|
|
# skip_iteration: if set to 1 then skips after end of current iteration
|
|
|
|
from ch.psi.pshell.data.LayoutDefault import ATTR_WRITABLE_DIMENSION as ATTR_WRITABLE_DIMENSION
|
|
import org.jfree.chart.axis.NumberAxis as NumberAxis
|
|
import math
|
|
|
|
cur_range = 0
|
|
cur_iteration = 0
|
|
|
|
if Scienta.acquisitionMode != Scienta.AcquisitionMode.Swept:
|
|
Scienta.acquisitionMode = Scienta.AcquisitionMode.Swept
|
|
ret=[]
|
|
SENSORS = ["Scienta.spectrum", "Scienta.dataMatrix"]
|
|
|
|
adjusted_ranges = []
|
|
for cur_range in range(len(ranges)):
|
|
r = ranges[cur_range]
|
|
print r
|
|
print r.vars
|
|
ar = [round(r.min / r.vars[1]) * r.vars[1], round(r.max / r.vars[1]) * r.vars[1]]
|
|
adjusted_ranges.append(ar)
|
|
|
|
set_exec_pars(compression=True)
|
|
set_exec_pars(open = True)
|
|
create_metadata_datasets()
|
|
|
|
#Global arguments
|
|
Scienta.passEnergy = pass_energy
|
|
|
|
names=[]
|
|
names.append("Online Spectrum")
|
|
for i in range(len(ranges)):
|
|
names.append(str(ranges[i]))
|
|
plots = plot(None, names)
|
|
|
|
for p in plots[1:]:
|
|
p.getAxis(p.AxisId.X).label = "kinetic energy"
|
|
p.getAxis(p.AxisId.X2).setLabel("binding energy")
|
|
p.getAxis(p.AxisId.X2).inverted = True
|
|
|
|
p = plots[0]
|
|
be_axis = plots[0].getAxis(p.AxisId.X2)
|
|
be_axis.inverted=True
|
|
be_axis.setLabel("binding energy")
|
|
|
|
spectrum_series = p.getSeries(0)
|
|
|
|
|
|
def get_binding_energy(e):
|
|
ephot = Eph.take(100)
|
|
workfunc = 4.5
|
|
if type(ephot) != float or ephot < 0.:
|
|
ephot = Scienta.highEnergy.take(100)
|
|
return ephot - e - workfunc
|
|
|
|
def get_binding_range(p=None):
|
|
if p is None:
|
|
return get_binding_energy(Scienta.highEnergy.take(100)), get_binding_energy(Scienta.lowEnergy.take(100))
|
|
else:
|
|
ke_range=p.getAxis(p.AxisId.X).getDisplayRange()
|
|
return get_binding_energy(ke_range.max), get_binding_energy(ke_range.min)
|
|
|
|
eb2, eb1 = get_binding_range(p)
|
|
be_axis.setRange(eb2, eb1)
|
|
|
|
|
|
def plot_cur_spectrum():
|
|
try:
|
|
while get_context().state.running:
|
|
try:
|
|
y = Scienta.spectrum.take(100)
|
|
if y is not None:
|
|
x = Scienta.spectrumX
|
|
#x = Scienta.spectrumScale.take(100)
|
|
if len(y)>len(x):
|
|
y=y[:len(x)]
|
|
spectrum_series.setData(x, y)
|
|
|
|
eb2, eb1 = get_binding_range(plots[0])
|
|
if (be_axis.min != eb2) or (be_axis.max != eb1):
|
|
plots[0].resetZoom()
|
|
be_axis.setRange(eb2, eb1)
|
|
except:
|
|
log("Error plotting online spectrum: ", sys.exc_info()[1])
|
|
time.sleep(1.0)
|
|
finally:
|
|
print "Stopping spectrum plotting"
|
|
|
|
task = None
|
|
|
|
|
|
# measurements
|
|
|
|
try:
|
|
for cur_range in range(len(ranges)):
|
|
print "range", cur_range
|
|
cur_iteration = 0
|
|
skip_iteration = False
|
|
vars = ranges[cur_range].vars
|
|
region_name = None
|
|
|
|
#Check if photon energy is defined
|
|
|
|
if len(vars) > 2:
|
|
eph = vars[3]
|
|
if eph and (not math.isnan(eph)):
|
|
Eph.move(eph)
|
|
time.sleep(5.0)
|
|
if len(vars)>4:
|
|
region_name = vars[4]
|
|
if len(vars)>5:
|
|
Scienta.setPassEnergy(int (vars[5]))
|
|
|
|
Scienta.lowEnergy.write(adjusted_ranges[cur_range][0])
|
|
Scienta.highEnergy.write(adjusted_ranges[cur_range][1])
|
|
Scienta.update()
|
|
|
|
Scienta.stepTime.write(vars[0])
|
|
Scienta.stepSize.write(vars[1])
|
|
Scienta.setIterations(1)
|
|
|
|
set_adc_averaging()
|
|
|
|
#iterations done in script
|
|
xdata = None
|
|
ydata = None
|
|
image_data = None
|
|
task = fork(plot_cur_spectrum)
|
|
|
|
path="scan" + str(cur_range+1) + "/"
|
|
for cur_iteration in range(vars[2]):
|
|
print "iteration", cur_iteration
|
|
try:
|
|
p = plots[cur_range+1]
|
|
p.setTitle(str(ranges[cur_range]) + " - iteration " + str(cur_iteration+1))
|
|
except IndexError:
|
|
pass
|
|
|
|
while True:
|
|
wait_beam()
|
|
trig_scienta()
|
|
spectrum_array = Scienta.spectrum.read()
|
|
if beam_ok:
|
|
if image_data is None:
|
|
time.sleep(2.0)
|
|
size = Scienta.getImageSize()
|
|
(_width, _height,) = (size[0], size[1])
|
|
break
|
|
if ydata is None:
|
|
ydata = spectrum_array
|
|
else:
|
|
for k in range (len(spectrum_array)):
|
|
ydata[k] = ydata[k] + spectrum_array[k]
|
|
if xdata is None:
|
|
xdata = Scienta.spectrumX
|
|
p.getSeries(0).setData(xdata, ydata)
|
|
|
|
eb2, eb1 = get_binding_range()
|
|
p.getAxis(p.AxisId.X2).setRange(eb2, eb1)
|
|
|
|
if save_scienta_image:
|
|
image_array = Scienta.dataMatrix.read()
|
|
if _width != len(image_array[0]) or _height != len(image_array):
|
|
err = "Scienta image size changed during the acquisition: " + str((len(image_array[0]), len(image_array))) + " - original: " + str((_width, _height))
|
|
print err
|
|
log(err)
|
|
raise Exception(err)
|
|
|
|
if image_data is None:
|
|
image_data = image_array
|
|
else:
|
|
for k in range (len(image_data)):
|
|
for j in range (len(image_data[0])):
|
|
image_data[k][j] = image_data[k][j] + image_array[k][j]
|
|
if skip_iteration:
|
|
break
|
|
save_dataset(path + "ScientaSpectrum", ydata)
|
|
set_attribute(path, "Iterations",cur_iteration+1)
|
|
if save_scienta_image:
|
|
save_dataset(path + "ScientaImage", image_data, features = {"compression":True})
|
|
if cur_iteration==0:
|
|
save_dataset(path + "ScientaChannels", xdata)
|
|
set_attribute(path + "ScientaChannels", ATTR_WRITABLE_DIMENSION, 1)
|
|
set_attribute(path, "Range Low", adjusted_ranges[cur_range][0])
|
|
set_attribute(path, "Range High", adjusted_ranges[cur_range][1])
|
|
set_attribute(path, "Step Time", vars[0])
|
|
set_attribute(path, "Step Size", vars[1])
|
|
set_attribute(path, "Pass Energy",pass_energy)
|
|
set_attribute(path, "Readables", ["ScientaSpectrum","ScientaImage"] if save_scienta_image else ["ScientaSpectrum",])
|
|
set_attribute(path, "Writables", ["ScientaChannels",])
|
|
if region_name:
|
|
set_attribute(path, "Region Name", region_name)
|
|
create_diag_datasets(path)
|
|
append_diag_datasets(path)
|
|
|
|
plots[cur_range+1].setTitle(str(ranges[cur_range]))
|
|
ret.append((xdata, ydata))
|
|
|
|
|
|
finally:
|
|
cur_range = -1
|
|
if not Scienta.isReady():
|
|
Scienta.stop()
|
|
Scienta.update()
|
|
if task:
|
|
task[0].cancel(True)
|
|
if ENDSCAN:
|
|
after_scan()
|
|
|
|
set_return(to_array(ret,'o')) |