From 39482dc2c4797aa69358cfb9ad8b21b41f768e70 Mon Sep 17 00:00:00 2001 From: gac-x03da Date: Wed, 9 Aug 2023 09:55:13 +0200 Subject: [PATCH] Startup --- script/users/chong/PhotonEnergy.py | 36 +++++ script/users/chong/XPSSpectrum.py | 210 +++++++++++++++++++++++++++++ 2 files changed, 246 insertions(+) create mode 100644 script/users/chong/PhotonEnergy.py create mode 100644 script/users/chong/XPSSpectrum.py diff --git a/script/users/chong/PhotonEnergy.py b/script/users/chong/PhotonEnergy.py new file mode 100644 index 00000000..03d3e190 --- /dev/null +++ b/script/users/chong/PhotonEnergy.py @@ -0,0 +1,36 @@ +""" +Arguments: + +VECTOR (Double[][], Scan vector: Eph,Elow,Ehigh or Eph,Ecenter) +SENSORS (list) +LATENCY (double) +MODE ('fixed' or 'swept') +TYPE ('CIS' or 'CFS') +STEP (double) +""" + +if MODE == "swept": + Scienta.setAcquisitionMode(ch.psi.pshell.epics.Scienta.AcquisitionMode.Swept) +else: + Scienta.setAcquisitionMode(ch.psi.pshell.epics.Scienta.AcquisitionMode.Fixed) + +if len(VECTOR[0]) == 2: + #FIXED + Scienta.centerEnergy.write(VECTOR[0][1]) + writables = (Eph, Scienta.centerEnergy) +else: + #SWEPT + Scienta.lowEnergy.write(VECTOR[0][1]) + Scienta.highEnergy.write(VECTOR[0][2]) + writables = (Eph, Scienta.lowEnergy, Scienta.highEnergy) + +adjust_sensors() +set_adc_averaging() +set_preference(Preference.PLOT_TYPES, {'Scienta spectrum':1}) +set_exec_pars(compression=True) + +try: + vscan(writables, SENSORS, VECTOR, True, LATENCY,False, before_read=before_readout, after_read = after_readout) +finally: + if ENDSCAN: + after_scan() diff --git a/script/users/chong/XPSSpectrum.py b/script/users/chong/XPSSpectrum.py new file mode 100644 index 00000000..4155b742 --- /dev/null +++ b/script/users/chong/XPSSpectrum.py @@ -0,0 +1,210 @@ +#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=[] + +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: + y = Scienta.spectrum.take(100) + #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) + + 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) + (_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) + + 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')) \ No newline at end of file