diff --git a/plugins/EnergyScan.java b/plugins/EnergyScan.java index 1d740c7..927bd91 100644 --- a/plugins/EnergyScan.java +++ b/plugins/EnergyScan.java @@ -12,7 +12,6 @@ import ch.psi.pshell.plot.LinePlot; import ch.psi.pshell.plot.LinePlotBase; import ch.psi.pshell.plot.LinePlotSeries; import ch.psi.pshell.plot.Plot; -import ch.psi.utils.swing.MonitoredPanel; import ch.psi.pshell.ui.Panel; import ch.psi.utils.Chrono; import ch.psi.utils.State; @@ -29,14 +28,17 @@ import java.util.Properties; import java.util.logging.Level; import javax.swing.DefaultComboBoxModel; import javax.swing.JLabel; -import javax.swing.JPanel; /** * */ public class EnergyScan extends Panel { - - PluginPanel panel; + + public EnergyScan() { + initComponents(); + buttonDefaultsActionPerformed(null); + } + ChannelDoubleArray data; ChannelDoubleArray edata; ChannelDoubleArray idata; @@ -45,14 +47,15 @@ public class EnergyScan extends Panel { double[] offsets = new double[4]; + //Overridable callbacks @Override - protected JPanel create() { - panel = new PluginPanel(); - return panel; + public void onStart() { + super.onStart(); + loadConfig(); } @Override - protected void onInitialize(int runCount) { + public void onInitialize(int runCount) { super.onInitialize(runCount); count = (ChannelInteger) getController().getDevicePool().getByName("count"); @@ -72,59 +75,52 @@ public class EnergyScan extends Panel { } }); update(); - //panel.loadConfig(); + //loadConfig(); } @Override - protected void onStart() { - super.onStart(); - panel.loadConfig(); - } - - @Override - protected void onStateChange(State state, State former) { - getComponent().setEnabled(state == State.Ready); - } - - void stopScan() throws Exception{ - evalAsync("caput('START', 'STOP')"); + public void onStateChange(State state, State former) { + setEnabled(state == State.Ready); } long scanStartTimestamp; - + @Override - protected void onExecutedFile(String fileName, Object result){ + public void onExecutedFile(String fileName, Object result) { try { - switch (fileName){ + switch (fileName) { case "EnergyScan": - if (result instanceof Exception ){ - batchIndex=0; - batch=null; + if (result instanceof Exception) { + batchIndex = 0; + batch = null; stopScan(); - throw ((Exception)result); + throw ((Exception) result); } - if (batch!=null){ + if (batch != null) { batchIndex++; - if (batchIndex>=batch.length){ - batchIndex=0; - batch=null; + if (batchIndex >= batch.length) { + batchIndex = 0; + batch = null; } else { - panel.setMode(batch[batchIndex]); - panel.run(); + setMode(batch[batchIndex]); + run(); } } break; } - } catch (Exception ex) { - getLogger().log(Level.WARNING, null, ex); - SwingUtils.showException(panel, ex); - } + } catch (Exception ex) { + getLogger().log(Level.WARNING, null, ex); + SwingUtils.showException(this, ex); + } } - @Override protected void doUpdate() { - panel.plot(); + plot(); + } + + void stopScan() throws Exception { + evalAsync("caput('START', 'STOP')"); } Path getConfigFile() { @@ -132,248 +128,240 @@ public class EnergyScan extends Panel { } enum Mode { + plus, minus, lh, lv } - + Mode[] batch; int batchIndex; - + LinePlotBase scanPlot; LinePlotSeries scanSeries; - - public class PluginPanel extends MonitoredPanel { - public PluginPanel() { - initComponents(); - buttonDefaultsActionPerformed(null); - } - - @Override - public void setEnabled(boolean value) { - super.setEnabled(value); - buttonExecute.setEnabled(value); - comboSetup.setEnabled(value); - comboRunType.setEnabled(value); - buttonConfigure.setEnabled(value); - textFile.setEnabled(value); - textFolder.setEnabled(value); - buttonDefaults.setEnabled(value); - buttonPlot.setEnabled(value); - checkParameterControls(); - } - - void checkParameterControls(){ - boolean enabled = isEnabled() && (comboRunType.getSelectedIndex() ==0); - for (Component c : panelParameters.getComponents()) { - if (!(c instanceof JLabel)) { - c.setEnabled(enabled); - } - } - spinnerAlpha.setEnabled(enabled && comboMode.getSelectedItem().equals("LINEAR")); - } - - void plot() { - try { - if ((scanPlot!=null) && (scanSeries!=null) && scanPlot.isShowing()&& (count!=null)){ - //buttonGroupPlot.getSelection(). - Integer c = count.take(); - if (c == null) { - scanSeries.clear(); - } else { - double[] ydata = null; - if (radioE.isSelected()) { - data.setSize(c); - ydata = data.read(); - } else if (radioF.isSelected()) { - fdata.setSize(c); - ydata = fdata.read(); - } else if (radioI0.isSelected()) { - idata.setSize(c); - ydata = idata.read(); - } else if (radioTEY.isSelected()) { - data.setSize(c); - idata.setSize(c); - ydata = data.read(); - double[] i0 = idata.read(); - for (int i = 0; i < c; i++) { - ydata[i] = (i0[i] == 0.0) ? Double.NaN : ydata[i] / i0[i]; - } - } else if (radioTFY.isSelected()) { - fdata.setSize(c); - idata.setSize(c); - ydata = fdata.read(); - double[] i0 = idata.read(); - for (int i = 0; i < c; i++) { - ydata[i] = (i0[i] == 0.0) ? Double.NaN : ydata[i] / i0[i]; - } - } - if (ydata == null) { - scanSeries.clear(); - } else { - edata.setSize(c); - double[] xdata = edata.read(); - scanSeries.setData(xdata, ydata); - } - } - } - } catch (Exception ex) { - SwingUtils.showException(this, ex); - ex.printStackTrace(); - } - } - - void loadConfig() { - DefaultComboBoxModel model = (DefaultComboBoxModel) comboSetup.getModel(); - model.removeAllElements(); - try { - for (String line : Files.readAllLines(getConfigFile())) { - if ((line != null) && (!line.trim().isEmpty())) { - String[] tokens = line.split("="); - if (tokens.length > 0) { - model.addElement(tokens[0].trim()); - } - } - } - } catch (Exception ex) { - } - } - - String expandPath(String path) { - long time = System.currentTimeMillis(); - String mode; - if (comboMode.getSelectedIndex() == 0) { - mode = "plus"; - } else if (comboMode.getSelectedIndex() == 1) { - mode = "minus"; - } else { - mode = "lin_" + String.format("%1.0f", (Double) spinnerAlpha.getValue()); - } - - path = path.replaceAll("\\{date\\}", Chrono.getTimeStr(time, "YYYYMMdd")); - path = path.replaceAll("\\{time\\}", Chrono.getTimeStr(time, "HHmmss")); - path = path.replaceAll("\\{year\\}", Chrono.getTimeStr(time, "YYYY")); - path = path.replaceAll("\\{month\\}", Chrono.getTimeStr(time, "MM")); - path = path.replaceAll("\\{day\\}", Chrono.getTimeStr(time, "dd")); - path = path.replaceAll("\\{el\\}", String.valueOf(comboSetup.getSelectedItem())); - path = path.replaceAll("\\{mode\\}", mode); - return path; - } - - void setMode(Mode mode) { - switch (mode) { - case plus: - comboMode.setSelectedIndex(0); - spinnerOffset.setValue(offsets[0]); - return; - case minus: - comboMode.setSelectedIndex(1); - spinnerOffset.setValue(offsets[1]); - return; - case lh: - comboMode.setSelectedIndex(2); - spinnerOffset.setValue(offsets[2]); - spinnerAlpha.setValue(0.0); - return; - case lv: - comboMode.setSelectedIndex(2); - spinnerOffset.setValue(offsets[3]); - spinnerAlpha.setValue(90.0); - return; - } - } - - boolean isManualRun(){ - return (comboRunType.getSelectedIndex() == 0); - } - - void setElement() throws Exception{ - Properties prop = new Properties(); - prop.load(new FileInputStream(getConfigFile().toFile())); - String selection = comboSetup.getSelectedItem().toString(); - String val = prop.getProperty(selection); - String[] tokens = val.split(" "); - if (tokens.length != 8) { - throw new Exception("Invalid file format"); - } - spinnerE1.setValue(Double.valueOf(tokens[0].trim())); - spinnerE2.setValue(Double.valueOf(tokens[1].trim())); - spinnerTime.setValue(Double.valueOf(tokens[2].trim())); - spinnerDelay.setValue(Double.valueOf(tokens[3].trim())); - - offsets[0] = Double.valueOf(tokens[4].trim()); - offsets[1] = Double.valueOf(tokens[5].trim()); - offsets[2] = Double.valueOf(tokens[6].trim()); - offsets[3] = Double.valueOf(tokens[7].trim()); - } - - void setRunType() throws Exception{ - switch (comboRunType.getSelectedIndex()) { - case 0: //Manual - break; - case 1: //+ - setMode(Mode.plus); - break; - case 2: //- - setMode(Mode.minus); - break; - case 3: //LH - setMode(Mode.lh); - break; - case 4: //LV - setMode(Mode.lv); - break; - case 5: //+/- - setMode(Mode.plus); - break; - case 6: //+/-/-/+ - setMode(Mode.plus); - break; - case 7: //LH/LV - setMode(Mode.lh); - break; - case 8: //LH/LV/LV/LH - setMode(Mode.lh); - break; - } - checkParameterControls(); - } - - void run() throws ControllerStateException{ - HashMap args = new HashMap(); - Double e1 = (Double) spinnerE1.getValue(); - Double e2 = (Double) spinnerE2.getValue(); - args.put("E1", e1); - args.put("E2", e2); - args.put("TIME", (Double) spinnerTime.getValue()); - args.put("DELAY", (Double) spinnerDelay.getValue()); - args.put("MODE", comboMode.getSelectedItem().toString()); - args.put("OFFSET", (Double) spinnerOffset.getValue()); - args.put("ALPHA", (Double) spinnerAlpha.getValue()); - - String file = expandPath(textFile.getText()); - args.put("FILE", file); - - String folder = expandPath(textFolder.getText()); - args.put("FOLDER", folder); - scanStartTimestamp = System.currentTimeMillis(); - runAsync("EnergyScan", args); - if (scanPlot!=null){ - scanPlot.getAxis(Plot.AxisId.X).setRange(Math.min(e1, e2), Math.max(e1, e2)); - } - + @Override + public void setEnabled(boolean value) { + super.setEnabled(value); + buttonExecute.setEnabled(value); + comboSetup.setEnabled(value); + comboRunType.setEnabled(value); + buttonConfigure.setEnabled(value); + textFile.setEnabled(value); + textFolder.setEnabled(value); + buttonDefaults.setEnabled(value); + buttonPlot.setEnabled(value); + checkParameterControls(); } - void startPlot() throws Exception{ + void checkParameterControls() { + boolean enabled = isEnabled() && (comboRunType.getSelectedIndex() == 0); + for (Component c : panelParameters.getComponents()) { + if (!(c instanceof JLabel)) { + c.setEnabled(enabled); + } + } + spinnerAlpha.setEnabled(enabled && comboMode.getSelectedItem().equals("LINEAR")); + } + + void plot() { + try { + if ((scanPlot != null) && (scanSeries != null) && scanPlot.isShowing() && (count != null)) { + //buttonGroupPlot.getSelection(). + Integer c = count.take(); + if (c == null) { + scanSeries.clear(); + } else { + double[] ydata = null; + if (radioE.isSelected()) { + data.setSize(c); + ydata = data.read(); + } else if (radioF.isSelected()) { + fdata.setSize(c); + ydata = fdata.read(); + } else if (radioI0.isSelected()) { + idata.setSize(c); + ydata = idata.read(); + } else if (radioTEY.isSelected()) { + data.setSize(c); + idata.setSize(c); + ydata = data.read(); + double[] i0 = idata.read(); + for (int i = 0; i < c; i++) { + ydata[i] = (i0[i] == 0.0) ? Double.NaN : ydata[i] / i0[i]; + } + } else if (radioTFY.isSelected()) { + fdata.setSize(c); + idata.setSize(c); + ydata = fdata.read(); + double[] i0 = idata.read(); + for (int i = 0; i < c; i++) { + ydata[i] = (i0[i] == 0.0) ? Double.NaN : ydata[i] / i0[i]; + } + } + if (ydata == null) { + scanSeries.clear(); + } else { + edata.setSize(c); + double[] xdata = edata.read(); + scanSeries.setData(xdata, ydata); + } + } + } + } catch (Exception ex) { + SwingUtils.showException(this, ex); + ex.printStackTrace(); + } + } + + void loadConfig() { + DefaultComboBoxModel model = (DefaultComboBoxModel) comboSetup.getModel(); + model.removeAllElements(); + try { + for (String line : Files.readAllLines(getConfigFile())) { + if ((line != null) && (!line.trim().isEmpty())) { + String[] tokens = line.split("="); + if (tokens.length > 0) { + model.addElement(tokens[0].trim()); + } + } + } + } catch (Exception ex) { + } + } + + String expandPath(String path) { + long time = System.currentTimeMillis(); + String mode; + if (comboMode.getSelectedIndex() == 0) { + mode = "plus"; + } else if (comboMode.getSelectedIndex() == 1) { + mode = "minus"; + } else { + mode = "lin_" + String.format("%1.0f", (Double) spinnerAlpha.getValue()); + } + + path = path.replaceAll("\\{date\\}", Chrono.getTimeStr(time, "YYYYMMdd")); + path = path.replaceAll("\\{time\\}", Chrono.getTimeStr(time, "HHmmss")); + path = path.replaceAll("\\{year\\}", Chrono.getTimeStr(time, "YYYY")); + path = path.replaceAll("\\{month\\}", Chrono.getTimeStr(time, "MM")); + path = path.replaceAll("\\{day\\}", Chrono.getTimeStr(time, "dd")); + path = path.replaceAll("\\{el\\}", String.valueOf(comboSetup.getSelectedItem())); + path = path.replaceAll("\\{mode\\}", mode); + return path; + } + + void setMode(Mode mode) { + switch (mode) { + case plus: + comboMode.setSelectedIndex(0); + spinnerOffset.setValue(offsets[0]); + return; + case minus: + comboMode.setSelectedIndex(1); + spinnerOffset.setValue(offsets[1]); + return; + case lh: + comboMode.setSelectedIndex(2); + spinnerOffset.setValue(offsets[2]); + spinnerAlpha.setValue(0.0); + return; + case lv: + comboMode.setSelectedIndex(2); + spinnerOffset.setValue(offsets[3]); + spinnerAlpha.setValue(90.0); + return; + } + } + + boolean isManualRun() { + return (comboRunType.getSelectedIndex() == 0); + } + + void setElement() throws Exception { + Properties prop = new Properties(); + prop.load(new FileInputStream(getConfigFile().toFile())); + String selection = comboSetup.getSelectedItem().toString(); + String val = prop.getProperty(selection); + String[] tokens = val.split(" "); + if (tokens.length != 8) { + throw new Exception("Invalid file format"); + } + spinnerE1.setValue(Double.valueOf(tokens[0].trim())); + spinnerE2.setValue(Double.valueOf(tokens[1].trim())); + spinnerTime.setValue(Double.valueOf(tokens[2].trim())); + spinnerDelay.setValue(Double.valueOf(tokens[3].trim())); + + offsets[0] = Double.valueOf(tokens[4].trim()); + offsets[1] = Double.valueOf(tokens[5].trim()); + offsets[2] = Double.valueOf(tokens[6].trim()); + offsets[3] = Double.valueOf(tokens[7].trim()); + } + + void setRunType() throws Exception { + switch (comboRunType.getSelectedIndex()) { + case 0: //Manual + break; + case 1: //+ + setMode(Mode.plus); + break; + case 2: //- + setMode(Mode.minus); + break; + case 3: //LH + setMode(Mode.lh); + break; + case 4: //LV + setMode(Mode.lv); + break; + case 5: //+/- + setMode(Mode.plus); + break; + case 6: //+/-/-/+ + setMode(Mode.plus); + break; + case 7: //LH/LV + setMode(Mode.lh); + break; + case 8: //LH/LV/LV/LH + setMode(Mode.lh); + break; + } + checkParameterControls(); + } + + void run() throws ControllerStateException { + HashMap args = new HashMap(); + Double e1 = (Double) spinnerE1.getValue(); + Double e2 = (Double) spinnerE2.getValue(); + args.put("E1", e1); + args.put("E2", e2); + args.put("TIME", (Double) spinnerTime.getValue()); + args.put("DELAY", (Double) spinnerDelay.getValue()); + args.put("MODE", comboMode.getSelectedItem().toString()); + args.put("OFFSET", (Double) spinnerOffset.getValue()); + args.put("ALPHA", (Double) spinnerAlpha.getValue()); + + String file = expandPath(textFile.getText()); + args.put("FILE", file); + + String folder = expandPath(textFolder.getText()); + args.put("FOLDER", folder); + scanStartTimestamp = System.currentTimeMillis(); + runAsync("EnergyScan", args); + if (scanPlot != null) { + scanPlot.getAxis(Plot.AxisId.X).setRange(Math.min(e1, e2), Math.max(e1, e2)); + } + } + + void startPlot() throws Exception { PlotDescriptor descriptors = new PlotDescriptor("Energy Scan"); - ArrayList plots = getController().plot(new PlotDescriptor[]{descriptors},null); + ArrayList plots = getController().plot(new PlotDescriptor[]{descriptors}, null); scanPlot = (LinePlotBase) plots.get(0); scanSeries = scanPlot.getSeries(0); - } - - + } + @SuppressWarnings("unchecked") // //GEN-BEGIN:initComponents private void initComponents() { @@ -798,7 +786,7 @@ public class EnergyScan extends Panel { try { startPlot(); batchIndex = 0; - switch(comboRunType.getSelectedIndex()){ + switch (comboRunType.getSelectedIndex()) { case 5: batch = new Mode[]{Mode.plus, Mode.minus}; break; @@ -812,7 +800,7 @@ public class EnergyScan extends Panel { batch = new Mode[]{Mode.lh, Mode.lv, Mode.lv, Mode.lh}; break; default: - batch = null; + batch = null; } run(); } catch (Exception ex) { @@ -828,7 +816,7 @@ public class EnergyScan extends Panel { private void comboSetupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboSetupActionPerformed try { setElement(); - if (!isManualRun()){ + if (!isManualRun()) { setRunType(); } } catch (Exception ex) { @@ -849,13 +837,13 @@ public class EnergyScan extends Panel { }//GEN-LAST:event_comboModeActionPerformed private void radioPlotActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioPlotActionPerformed - panel.plot(); + plot(); }//GEN-LAST:event_radioPlotActionPerformed private void comboRunTypeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboRunTypeActionPerformed try { - if (!isManualRun()){ - setElement(); + if (!isManualRun()) { + setElement(); } setRunType(); } catch (Exception ex) { @@ -875,7 +863,7 @@ public class EnergyScan extends Panel { } catch (Exception ex) { SwingUtils.showException(this, ex); } - + }//GEN-LAST:event_buttonPlotActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables @@ -918,5 +906,4 @@ public class EnergyScan extends Panel { private javax.swing.JTextField textFile; private javax.swing.JTextField textFolder; // End of variables declaration//GEN-END:variables - } }