#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 cur_range = 0 cur_iteration = 0 if Scienta.acquisitionMode != Scienta.AcquisitionMode.Swept: Scienta.acquisitionMode = Scienta.AcquisitionMode.Swept ret=[] 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(open = True) #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 """ eb_axis = NumberAxis("binding energy") eb_axis.inverted = True jf = p.chart.plot jf.setDomainAxis(1, eb_axis) jf.getDomainAxis(1).labelPaint = jf.getDomainAxis(0).labelPaint jf.getDomainAxis(1).labelFont = jf.getDomainAxis(0).labelFont jf.getDomainAxis(1).tickLabelPaint = jf.getDomainAxis(0).tickLabelPaint jf.getDomainAxis(1).tickLabelFont = jf.getDomainAxis(0).tickLabelFont """ # online spectrum """ p = plots[0] spectrum_series = p.getSeries(0) jf = p.chart.plot eb_axis = NumberAxis("binding energy") jf.setDomainAxis(1, eb_axis) jf.getDomainAxis(1).labelPaint = jf.getDomainAxis(0).labelPaint jf.getDomainAxis(1).labelFont = jf.getDomainAxis(0).labelFont jf.getDomainAxis(1).tickLabelPaint = jf.getDomainAxis(0).tickLabelPaint jf.getDomainAxis(1).tickLabelFont = jf.getDomainAxis(0).tickLabelFont def set_online_spectrum_x2_range(): spectrum_eb_axis = jf.getDomainAxis(1) # adjust the binding energy scale # if Eph returns an invalid number, we take energy_high as photon energy ephot = Eph.take(100) elo = Scienta.lowEnergy.take(100) ehi = Scienta.highEnergy.take(100) workfunc = 4.5 if ephot is not float or ephot < 0.: ephot = ehi eb1 = ephot - elo - workfunc eb2 = ephot - ehi - workfunc spectrum_eb_axis.setRange(eb2, eb1) set_online_spectrum_x2_range() """ p = plots[0] spectrum_series = p.getSeries(0) p.getAxis(p.AxisId.X2).setLabel("binding energy") ephot = Eph.take(100) elo = Scienta.lowEnergy.take(100) ehi = Scienta.highEnergy.take(100) workfunc = 4.5 if ephot is not float or ephot < 0.: ephot = ehi eb1 = ephot - elo - workfunc eb2 = ephot - ehi - workfunc plots[0].getAxis(p.AxisId.X2).setRange(eb2, eb1) # #def plot_cur_spectrum_eb(): # """ # plot online spectrum function for forked task. # with binding energy scale. # issue: this should not set the axis while the plot is zoomed! # """ # global spectrum_series # global spectrum_eb_axis # try: # while get_context().state.running: # y = Scienta.spectrum.take(100) # x = Scienta.spectrumX # spectrum_series.setData(x, y) # # # adjust the binding energy scale # # if Eph returns an invalid number, we take energy_high as photon energy # ephot = Eph.take(100) # elo = Scienta.lowEnergy.take(100) # ehi = Scienta.highEnergy.take(100) # workfunc = 4.5 # if ephot is not float or ephot < 0.: # ephot = ehi # eb1 = ephot - elo - workfunc # eb2 = ephot - ehi - workfunc # spectrum_eb_axis.setRange(eb2, eb1) # # time.sleep(1.0) # finally: # print "Stopping spectrum plotting" # def plot_cur_spectrum(): try: while get_context().state.running: y = Scienta.spectrum.take(100) x = Scienta.spectrumX spectrum_series.setData(x, y) #set_online_spectrum_x2_range() time.sleep(1.0) finally: print "Stopping spectrum plotting" task = None # measurements try: for cur_range in range(len(ranges)): cur_iteration = 0 skip_iteration = False Scienta.lowEnergy.write(adjusted_ranges[cur_range][0]) Scienta.highEnergy.write(adjusted_ranges[cur_range][1]) Scienta.update() vars = ranges[cur_range].vars 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]): p = plots[cur_range+1] p.setTitle(str(ranges[cur_range]) + " - iteration " + str(cur_iteration+1)) while True: wait_beam() trig_scienta() spectrum_array = Scienta.spectrum.read() if beam_ok: if image_data is None: time.sleep(2.0) (_width, _height) = Scienta.getImageSize() 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) # adjust the binding energy scale # if Eph returns an invalid number, we take energy_high as photon energy #eb_axis = p.chart.plot.getDomainAxis(1) ephot = Eph.take(100) elo = Scienta.lowEnergy.take(100) ehi = Scienta.highEnergy.take(100) workfunc = 4.5 if ephot is not float or ephot < 0.: ephot = ehi eb1 = ephot - elo - workfunc eb2 = ephot - ehi - workfunc #eb_axis.setRange(eb2, eb1) 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",]) 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() task[0].cancel(True) if ENDSCAN: after_scan() set_return(to_array(ret,'o'))