diff --git a/config/plugins.properties b/config/plugins.properties index 085b8d3..80d26d8 100644 --- a/config/plugins.properties +++ b/config/plugins.properties @@ -1,2 +1,3 @@ +./home/plugins/test.java=enabled ./home/plugins/HystScan.java=enabled ./home/plugins/EnergyScan.java=enabled diff --git a/plugins/EnergyScan.java b/plugins/EnergyScan.java index 3c508b5..355716e 100644 --- a/plugins/EnergyScan.java +++ b/plugins/EnergyScan.java @@ -14,7 +14,6 @@ import javax.swing.JLabel; import ch.psi.utils.State; import ch.psi.utils.swing.DsvEditor; import ch.psi.utils.swing.Editor.EditorDialog; -import ch.psi.pshell.core.Controller.ControllerStateException; import ch.psi.pshell.data.PlotDescriptor; import ch.psi.pshell.device.Device; import ch.psi.pshell.device.DeviceAdapter; @@ -46,18 +45,19 @@ public class EnergyScan extends Panel { ChannelInteger count; double[] offsets = new double[4]; -/* - //Overridable callbacks - @Override - public void onStart() { - super.onStart(); - loadConfig(); - } -*/ + /* + //Overridable callbacks + @Override + public void onStart() { + super.onStart(); + loadConfig(); + } + */ + @Override public void onInitialize(int runCount) { super.onInitialize(runCount); - + loadConfig(); count = (ChannelInteger) getController().getDevicePool().getByName("count"); @@ -81,35 +81,8 @@ public class EnergyScan extends Panel { setEnabled(state == State.Ready); } - long scanStartTimestamp; - @Override public void onExecutedFile(String fileName, Object result) { - try { - switch (fileName) { - case "EnergyScan": - if (result instanceof Exception) { - batchIndex = 0; - batch = null; - stopScan(); - throw ((Exception) result); - } - if (batch != null) { - batchIndex++; - if (batchIndex >= batch.length) { - batchIndex = 0; - batch = null; - } else { - setMode(batch[batchIndex]); - run(); - } - } - break; - } - } catch (Exception ex) { - getLogger().log(Level.WARNING, null, ex); - showException(ex); - } } @Override @@ -117,8 +90,13 @@ public class EnergyScan extends Panel { plot(); } - void stopScan() throws Exception { - evalAsync("caput('START', 'STOP')"); + void stopScan() { + try{ + getLogger().info("Stopping scan"); + evalAsync("caput('START', 'STOP')"); + } catch (Exception ex){ + getLogger().log(Level.WARNING, null, ex); + } } Path getConfigFile() { @@ -276,10 +254,10 @@ public class EnergyScan extends Panel { for (String line : Files.readAllLines(getConfigFile())) { if ((line != null) && (!line.trim().isEmpty())) { String[] tokens = line.split("="); - if (tokens.length <2) { + if (tokens.length < 2) { throw new Exception("Invalid file format"); } - if (tokens[0].equals(selection)){ + if (tokens[0].equals(selection)) { tokens = tokens[1].trim().split(";"); if (tokens.length != 8) { throw new Exception("Invalid file format"); @@ -294,9 +272,9 @@ public class EnergyScan extends Panel { offsets[2] = Double.valueOf(tokens[6].trim()); offsets[3] = Double.valueOf(tokens[7].trim()); break; - } + } } - } + } } void setRunType() throws Exception { @@ -331,7 +309,7 @@ public class EnergyScan extends Panel { checkParameterControls(); } - void run() throws ControllerStateException { + void run() throws Exception { HashMap args = new HashMap(); Double e1 = (Double) spinnerE1.getValue(); Double e2 = (Double) spinnerE2.getValue(); @@ -347,13 +325,29 @@ public class EnergyScan extends Panel { args.put("FILE", file); String folder = expandPath("{year}_{month}/{date}"); args.put("FOLDER", folder); - scanStartTimestamp = System.currentTimeMillis(); buttonAbort.setEnabled(true); - runAsync("EnergyScan", args).handle((ok, ex) -> { - buttonAbort.setEnabled(false); - if (ex != null) { + runAsync("EnergyScan", args).handle((ok, ex) -> { + try{ + buttonAbort.setEnabled(false); + if (ex != null) { + stopScan(); + throw ex; + } else { + if (batch != null) { + batchIndex++; + if (batchIndex >= batch.length) { + batch = null; + } else { + setMode(batch[batchIndex]); + run(); + } + } + } + } catch (Throwable t){ + batch = null; getLogger().log(Level.WARNING, null, ex); + showException((Exception)ex); } return ok; }); @@ -808,7 +802,7 @@ public class EnergyScan extends Panel { private void comboSetupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboSetupActionPerformed try { - if (comboSetup.getSelectedItem()!=null){ + if (comboSetup.getSelectedItem() != null) { setElement(); if (!isManualRun()) { setRunType(); @@ -833,7 +827,7 @@ public class EnergyScan extends Panel { dlgConfig.addWindowListener(new WindowAdapter() { @Override public void windowClosed(WindowEvent e) { - if (System.currentTimeMillis() - new File(dlgConfig.getEditor().getFileName()).lastModified() < 5000){ + if (System.currentTimeMillis() - new File(dlgConfig.getEditor().getFileName()).lastModified() < 5000) { loadConfig(); } } diff --git a/plugins/HystScan.form b/plugins/HystScan.form index 3844e3e..95125c1 100644 --- a/plugins/HystScan.form +++ b/plugins/HystScan.form @@ -433,13 +433,15 @@ - - - - + + + + + + @@ -477,9 +479,6 @@ - - - diff --git a/plugins/HystScan.java b/plugins/HystScan.java index 290ae89..e12739e 100644 --- a/plugins/HystScan.java +++ b/plugins/HystScan.java @@ -2,11 +2,9 @@ * Copyright (c) 2014 Paul Scherrer Institute. All rights reserved. */ -import ch.psi.pshell.core.Controller; import ch.psi.pshell.ui.Panel; import ch.psi.utils.State; import java.awt.Component; -import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -14,7 +12,6 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.logging.Level; import java.util.logging.Logger; -import javax.script.ScriptException; import javax.swing.DefaultComboBoxModel; import javax.swing.JLabel; import javax.swing.table.DefaultTableModel; @@ -54,14 +51,13 @@ public class HystScan extends Panel { @Override public void setEnabled(boolean value) { super.setEnabled(value); - buttonExecute.setEnabled(value); + buttonExecute.setEnabled(value); tableEnergies.setEnabled(value); for (Component c : panelParameters.getComponents()) { if (!(c instanceof JLabel)) { c.setEnabled(value); } - } - //spinnerE3.setEnabled(value && checkE3.isSelected()); + } } Path getConfigFile() { @@ -107,6 +103,7 @@ public class HystScan extends Panel { Mode[] batch; int batchIndex; double[] offsets = new double[2]; + boolean reverseScan; boolean isManualRun() { @@ -146,18 +143,68 @@ public class HystScan extends Panel { return; } } + + + void run() throws Exception { + String file = expandPath(textFile.getText()); + eval("FILENAME = '" + file + "'"); - void setRunType() throws Exception { - switch (comboRunType.getSelectedIndex()) { - case 0: //Manual - break; - case 1: //2-loop - setMode(Mode.plus); - break; - case 2: //4-loop - setMode(Mode.minus); - break; + HashMap args = new HashMap(); + + ArrayList energies = new ArrayList<>(); + int rows = (Integer) spinnerEnergies.getValue(); + ((DefaultTableModel) tableEnergies.getModel()).setNumRows(rows); + for (int i = 0; i < tableEnergies.getRowCount(); i++) { + energies.add((Double) ((DefaultTableModel) tableEnergies.getModel()).getValueAt(i, 1)); } + + args.put("ENERGIES", energies); + if (reverseScan){ + getLogger().warning("Setting reverse scan"); + args.put("START_FIELD", (Double) spinnerEndField.getValue()); + args.put("END_FIELD", (Double) spinnerStartField.getValue()); + + } else { + args.put("START_FIELD", (Double) spinnerStartField.getValue()); + args.put("END_FIELD", (Double) spinnerEndField.getValue()); + } + args.put("RAMP_RATE", (Double) spinnerRampRate.getValue()); + args.put("ENERGY_CHANGE_SLEEP", (Double) spinnerSleep.getValue()); + args.put("MODE", comboMode.getSelectedItem().toString()); + args.put("OFFSET", (Double) spinnerOffset.getValue()); + buttonAbort.setEnabled(true); + buttonExecute.setEnabled(false); + runAsync("HystScan", args).handle((ok, ex) -> { + try{ + buttonAbort.setEnabled(false); + try { + eval("FILENAME = None"); + } catch (Exception ex1) { + Logger.getLogger(HystScan.class.getName()).log(Level.WARNING, null, ex1); + } + if (ex != null) { + throw ex; + } else { + if (batch != null) { + batchIndex++; + reverseScan = !reverseScan; + if (batchIndex >= batch.length) { + batch = null; + reverseScan = false; + } else { + setMode(batch[batchIndex]); + run(); + } + } + } + } catch (Throwable t){ + batch = null; + reverseScan = false; + getLogger().log(Level.WARNING, null, ex); + showException((Exception)ex); + } + return ok; + }); } @@ -432,11 +479,6 @@ public class HystScan extends Panel { jLabel12.setText("Run Type:"); comboRunType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Manual", "2-loop", "4-loop" })); - comboRunType.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { - comboRunTypeActionPerformed(evt); - } - }); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); @@ -457,12 +499,13 @@ public class HystScan extends Panel { jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() - .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) - .addComponent(comboSetup, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) - .addComponent(jLabel11) + .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel12) - .addComponent(comboRunType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) + .addComponent(comboRunType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(comboSetup, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(jLabel11))) .addContainerGap()) ); @@ -491,53 +534,20 @@ public class HystScan extends Panel { private void buttonExecuteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonExecuteActionPerformed try { - String file = expandPath(textFile.getText()); - eval("FILENAME = '" + file+"'"); - - HashMap args = new HashMap(); - - ArrayList energies = new ArrayList<>(); - int rows = (Integer)spinnerEnergies.getValue(); - ((DefaultTableModel)tableEnergies.getModel()).setNumRows(rows); - for (int i=0; i { - buttonAbort.setEnabled(false); - if (ex != null) { - getLogger().log(Level.WARNING, null, ex); - } - try { - eval("FILENAME = None"); - } catch (Exception ex1) { - Logger.getLogger(HystScan.class.getName()).log(Level.WARNING, null, ex1); - } - return ok; - }); - + run(); } catch (Exception ex) { showException(ex); } @@ -555,26 +565,12 @@ public class HystScan extends Panel { try { if (comboSetup.getSelectedItem()!=null){ setElement(); - if (!isManualRun()) { - setRunType(); - } } } catch (Exception ex) { showException(ex); } }//GEN-LAST:event_comboSetupActionPerformed - private void comboRunTypeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboRunTypeActionPerformed - try { - if (!isManualRun()) { - setElement(); - } - setRunType(); - } catch (Exception ex) { - showException(ex); - } - }//GEN-LAST:event_comboRunTypeActionPerformed - private void spinnerEnergiesStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_spinnerEnergiesStateChanged int rows = (Integer)spinnerEnergies.getValue(); ((DefaultTableModel)tableEnergies.getModel()).setNumRows(rows); diff --git a/script/local.py b/script/local.py index 597f0ec..27eb269 100644 --- a/script/local.py +++ b/script/local.py @@ -6,6 +6,7 @@ FILENAME = None #Uncomment this line to create the simulated devices needed to the tutorial scripts. #run("tutorial/devices") + #The persistence strategy is overriden to adjust file names import ch.psi.pshell.data.ScanPersistenceStrategyTable class Strategy(ch.psi.pshell.data.ScanPersistenceStrategyTable): @@ -63,6 +64,3 @@ class SimulatedEnergyReadback(Readable): sim_energy = SimulatedEnergy() sim_energy_readback = SimulatedEnergyReadback() - - -print getPars("Fe") \ No newline at end of file