diff --git a/devices/Beam phase.properties b/devices/Beam phase.properties index 8336f35..018d7e0 100644 --- a/devices/Beam phase.properties +++ b/devices/Beam phase.properties @@ -1,9 +1,9 @@ -#Fri Nov 24 07:52:19 CET 2017 +#Mon Nov 27 08:47:47 CET 2017 maxValue=360.0 minValue=-360.0 offset=0.0 precision=3 resolution=0.1 -rotation=false +rotation=true scale=1.0 unit=deg diff --git a/devices/CurrentCamera.properties b/devices/CurrentCamera.properties index 094f805..da9d66e 100644 --- a/devices/CurrentCamera.properties +++ b/devices/CurrentCamera.properties @@ -1,19 +1,19 @@ -#Sat Nov 25 15:14:11 CET 2017 +#Mon Nov 27 10:01:03 CET 2017 \u0000\u0000\u0000\u0000= \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000= \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000= colormap=Flame colormapAutomatic=true -colormapMax=2886.0 -colormapMin=71.0 +colormapMax=14000.0 +colormapMin=0.0 flipHorizontally=false flipVertically=false grayscale=false -imageHeight=600 -imageWidth=800 +imageHeight=1200 +imageWidth=1246 invert=false -regionStartX=16 -regionStartY=16 +regionStartX=304 +regionStartY=8 rescaleFactor=1.0 rescaleOffset=0.0 roiHeight=-1 @@ -24,9 +24,9 @@ rotation=0.0 rotationCrop=false scale=1.0 serverURL=localhost\:10000 -spatialCalOffsetX=-371.495985377343 -spatialCalOffsetY=-65.07336431324175 -spatialCalScaleX=-8.737659474745154 -spatialCalScaleY=-8.834897904579043 +spatialCalOffsetX=-1285.5021743822372 +spatialCalOffsetY=-1063.492325271727 +spatialCalScaleX=-17.12279119040641 +spatialCalScaleY=-17.316017253358037 spatialCalUnits= transpose=false diff --git a/plugins/ScreenPanel.java b/plugins/ScreenPanel.java index 4946b50..0f5b368 100644 --- a/plugins/ScreenPanel.java +++ b/plugins/ScreenPanel.java @@ -16,12 +16,11 @@ import ch.psi.utils.swing.TextEditor; import ch.psi.pshell.bs.PipelineServer; import ch.psi.pshell.bs.StreamValue; import ch.psi.pshell.core.JsonSerializer; -import ch.psi.pshell.device.DescStatsDouble; +import ch.psi.pshell.data.DataManager; import ch.psi.pshell.device.Device; import ch.psi.pshell.epics.ChannelInteger; import ch.psi.pshell.epics.DiscretePositioner; import ch.psi.pshell.epics.Epics; -import ch.psi.pshell.imaging.Calibration; import ch.psi.pshell.imaging.Colormap; import ch.psi.pshell.imaging.ColormapSource; import ch.psi.pshell.imaging.ColormapSource.ColormapSourceConfig; @@ -66,7 +65,6 @@ import java.awt.event.WindowEvent; import java.awt.image.BufferedImage; import java.io.FileInputStream; import java.lang.reflect.Array; -import java.lang.reflect.Field; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; @@ -147,8 +145,7 @@ public class ScreenPanel extends Panel { ImageData() { if (server != null) { - StreamValue cache = server.getStream().take(); - + cache = server.getStream().take(); String prefix = goodRegion ? "gr_" : ""; x_fit_mean = getServerDouble(prefix + "x_fit_mean", cache); y_fit_mean = getServerDouble(prefix + "y_fit_mean", cache); @@ -206,7 +203,8 @@ public class ScreenPanel extends Panel { public double[] x_fit_gauss_function; public double[] y_profile; public double[] y_fit_gauss_function; - PointDouble[] sliceCenters; + public PointDouble[] sliceCenters; + public StreamValue cache; } class Frame extends ImageData { @@ -752,54 +750,54 @@ public class ScreenPanel extends Panel { onChangeColormap(null); checkBackground.setEnabled(true); if (changed) { - comboScreen.setModel(new DefaultComboBoxModel()); - comboFilter.setModel(new DefaultComboBoxModel()); + comboScreen.setModel(new DefaultComboBoxModel()); comboScreen.setEnabled(false); + comboFilter.setModel(new DefaultComboBoxModel()); comboFilter.setEnabled(false); + if (getCameraType(cameraName).equals("ELECTRONS")){ + //Parallelizing initialization + devicesInitTask = new Thread(() -> { + try { + if (cameraName.contains("DSRM")) { + screen = new DiscretePositioner("CurrentScreen", cameraName + ":POSITION_SP", cameraName + ":POSITION"); + } else { + screen = new DiscretePositioner("CurrentScreen", cameraName + ":SET_SCREEN1_POS", cameraName + ":GET_SCREEN1_POS"); + } + screen.setMonitored(true); + screen.initialize(); + DefaultComboBoxModel model = new DefaultComboBoxModel(); + for (String pos : screen.getPositions()) { + model.addElement(pos); + } + comboScreen.setModel(model); + comboScreen.setSelectedItem(screen.read()); - //Parallelizing initialization - devicesInitTask = new Thread(() -> { - try { - if (cameraName.contains("DSRM")) { - screen = new DiscretePositioner("CurrentScreen", cameraName + ":POSITION_SP", cameraName + ":POSITION"); - } else { - screen = new DiscretePositioner("CurrentScreen", cameraName + ":SET_SCREEN1_POS", cameraName + ":GET_SCREEN1_POS"); + } catch (Exception ex) { + comboScreen.setModel(new DefaultComboBoxModel()); + System.err.println(ex.getMessage()); + screen = null; } - screen.setMonitored(true); - screen.initialize(); - DefaultComboBoxModel model = new DefaultComboBoxModel(); - for (String pos : screen.getPositions()) { - model.addElement(pos); - } - comboScreen.setModel(model); - comboScreen.setSelectedItem(screen.read()); + comboScreen.setEnabled(screen != null); + valueScreen.setDevice(screen); - } catch (Exception ex) { - comboScreen.setModel(new DefaultComboBoxModel()); - System.err.println(ex.getMessage()); - screen = null; - } - comboScreen.setEnabled(screen != null); - valueScreen.setDevice(screen); - - try { - filter = new DiscretePositioner("CurrentFilter", cameraName + ":SET_FILTER", cameraName + ":GET_FILTER"); - filter.setMonitored(true); - filter.initialize(); - DefaultComboBoxModel model = new DefaultComboBoxModel(); - for (String pos : filter.getPositions()) { - model.addElement(pos); + try { + filter = new DiscretePositioner("CurrentFilter", cameraName + ":SET_FILTER", cameraName + ":GET_FILTER"); + filter.setMonitored(true); + filter.initialize(); + DefaultComboBoxModel model = new DefaultComboBoxModel(); + for (String pos : filter.getPositions()) { + model.addElement(pos); + } + comboFilter.setModel(model); + comboFilter.setSelectedItem(filter.read()); + } catch (Exception ex) { + System.err.println(ex.getMessage()); + filter = null; } - comboFilter.setModel(model); - comboFilter.setSelectedItem(filter.read()); - } catch (Exception ex) { - System.err.println(ex.getMessage()); - filter = null; - } - comboFilter.setEnabled(filter != null); - valueFilter.setDevice(filter); - }); - devicesInitTask.start(); + comboFilter.setEnabled(filter != null); + valueFilter.setDevice(filter); + }); + devicesInitTask.start(); + } } - } volatile Dimension imageSize; @@ -1622,123 +1620,178 @@ public class ScreenPanel extends Panel { } } - void writeFrameMetadata(String path, Frame frame) throws Exception { - getContext().getDataManager().setAttribute("/", "Camera", String.valueOf(cameraName)); - getContext().getDataManager().setAttribute("/", "Screen", String.valueOf(valueScreen.getLabel().getText())); - getContext().getDataManager().setAttribute("/", "Filter", String.valueOf(valueFilter.getLabel().getText())); - Calibration cal = renderer.getCalibration(); - getContext().getDataManager().setAttribute("/", "Calibration", cal == null ? new double[]{1, 1, 0, 0} - : new double[]{cal.getScaleX(), cal.getScaleY(), cal.getOffsetX(), cal.getOffsetY()}); - getContext().getDataManager().setAttribute(path, "Timestamp", Chrono.getTimeStr(frame.data.getTimestamp(), "HH:mm:ss.SSS")); - if (server != null) { - try { - getContext().getDataManager().setAttribute("/", "ROI", server.getRoi()); - } catch (Exception ex) { - getContext().getDataManager().setAttribute("/", "ROI", new int[]{0, 0, -1, -1}); - } - if (frame != null) { - for (Field f : ImageData.class.getFields()) { - Object value = f.get(frame); - getContext().getDataManager().setAttribute(path, f.getName(), (value == null) ? Double.NaN : value); - } - } - getContext().getDataManager().setAttribute("/", "GoodRegion", goodRegion); - getContext().getDataManager().setAttribute("/", "Slicing", slicing); - for (String name : new String[]{"x_axis", "y_axis", "gr_x_axis", "gr_y_axis"}) { - double[] val = getServerDoubleArray(name); - if (val != null) { - getContext().getDataManager().setAttribute("/", name, val); - } - } - } - } - void saveSnapshot() throws Exception { - getContext().setExecutionPars("snapshot"); - String path = "/data"; - String snapshotFile = null; - synchronized (imageBuffer) { - Frame id = getCurrentFrame(); - if (id == null) { - throw new Exception("No current image"); - } - Object data = id.data.getMatrix(); - getContext().getDataManager().setDataset(path, data, id.data.isUnsigned()); - writeFrameMetadata(path, id); - getContext().getDataManager().closeOutput(); - //Enforce the same timestamp to data & image files. - //snapshotFile = getContext().getSetup().expandPath("{images}/{date}_{time}_snapshot.png", getContext().getExecutionPars().getStart()); - snapshotFile = getContext().getExecutionPars().getPath() + ".png"; - //renderer.saveSnapshot(snapshotFile, "png", true); - ImageBuffer.saveImage(SwingUtils.createImage(renderer), snapshotFile, "png"); - - } - - JPanel panel = new JPanel(); - GridBagLayout layout = new GridBagLayout(); - layout.columnWidths = new int[]{0, 180}; //Minimum width - layout.rowHeights = new int[]{30, 30, 30}; //Minimum height - panel.setLayout(layout); - JComboBox comboLogbook = new JComboBox(new String[]{"SwissFEL commissioning data", "SwissFEL commissioning"}); - JTextField textComment = new JTextField(); - GridBagConstraints c = new GridBagConstraints(); - c.gridx = 0; - c.gridy = 0; - panel.add(new JLabel("Data file:"), c); - c.gridy = 1; - panel.add(new JLabel("Logbook:"), c); - c.gridy = 2; - panel.add(new JLabel("Comment:"), c); - c.fill = GridBagConstraints.HORIZONTAL; - c.gridx = 1; - panel.add(textComment, c); - c.gridy = 1; - panel.add(comboLogbook, c); - c.gridy = 0; - panel.add(new JLabel(getContext().getExecutionPars().getPath()), c); - - if (SwingUtils.showOption(getTopLevel(), "Success", panel, OptionType.OkCancel) == OptionResult.Yes) { - StringBuilder message = new StringBuilder(); - message.append("Camera: ").append(cameraName).append(" ("). - append((server != null) ? "server" : "direct").append(")").append("\n"); - message.append("Screen: ").append(String.valueOf(valueScreen.getLabel().getText())).append("\n"); - message.append("Filter: ").append(String.valueOf(valueFilter.getLabel().getText())).append("\n"); - message.append("Data file: ").append(getContext().getExecutionPars().getPath()).append("\n"); - message.append("Comment: ").append(textComment.getText()).append("\n"); - if ((fitOv != null) /*&& (fitOv.length > 5)*/) { - for (int i=0; i frames = new ArrayList<>(); + frames.add(frame); + this.saveFrames("camera_snapshot", frames); + + //Enforce the same timestamp to data & image files. + snapshotFile = getContext().getExecutionPars().getPath() + ".png"; + ImageBuffer.saveImage(SwingUtils.createImage(renderer), snapshotFile, "png"); + } + + JPanel panel = new JPanel(); + GridBagLayout layout = new GridBagLayout(); + layout.columnWidths = new int[]{0, 180}; //Minimum width + layout.rowHeights = new int[]{30, 30, 30}; //Minimum height + panel.setLayout(layout); + JComboBox comboLogbook = new JComboBox(new String[]{"SwissFEL commissioning data", "SwissFEL commissioning"}); + JTextField textComment = new JTextField(); + GridBagConstraints c = new GridBagConstraints(); + c.gridx = 0; + c.gridy = 0; + panel.add(new JLabel("Data file:"), c); + c.gridy = 1; + panel.add(new JLabel("Logbook:"), c); + c.gridy = 2; + panel.add(new JLabel("Comment:"), c); + c.fill = GridBagConstraints.HORIZONTAL; + c.gridx = 1; + panel.add(textComment, c); + c.gridy = 1; + panel.add(comboLogbook, c); + c.gridy = 0; + panel.add(new JLabel(getContext().getExecutionPars().getPath()), c); + + if (SwingUtils.showOption(getTopLevel(), "Success", panel, OptionType.OkCancel) == OptionResult.Yes) { + StringBuilder message = new StringBuilder(); + message.append("Camera: ").append(cameraName).append(" ("). + append((server != null) ? "server" : "direct").append(")").append("\n"); + message.append("Screen: ").append(String.valueOf(valueScreen.getLabel().getText())).append("\n"); + message.append("Filter: ").append(String.valueOf(valueFilter.getLabel().getText())).append("\n"); + message.append("Data file: ").append(getContext().getExecutionPars().getPath()).append("\n"); + message.append("Comment: ").append(textComment.getText()).append("\n"); + if ((fitOv != null) && (fitOv.length > 5)) { + Overlays.Text text = (Overlays.Text) fitOv[5]; + message.append(text.getText()).append("\n"); + } + elog((String) comboLogbook.getSelectedItem(), "ScreenPanel Snapshot", message.toString(), new String[]{snapshotFile}); } - elog((String) comboLogbook.getSelectedItem(), "ScreenPanel Snapshot", message.toString(), new String[]{snapshotFile}); - } - //SwingUtils.showMessage(getTopLevel(), "Success", "Generated data file:\n" + getContext().getExecutionPars().getPath(), 5000); - //elog("SwissFEL commissioning data", "ScreenPanel Snapshot", message.toString(), new String[]{snapshotFile}); } void saveStack() throws Exception { - getContext().setExecutionPars("snapshot"); - ArrayList x = new ArrayList<>(); - ArrayList y = new ArrayList<>(); - synchronized (imageBuffer) { - for (int i = 0; i < imageBuffer.size(); i++) { - Frame frame = imageBuffer.get(i); - String path = "/data_" + i; - getContext().getDataManager().setDataset(path, frame.data.getMatrix(), frame.data.isUnsigned()); - writeFrameMetadata(path, frame); - x.add(frame.x_fit_mean); - y.add(frame.y_fit_mean); + synchronized (imageBuffer) { + saveFrames("camera_stack", imageBuffer); } + SwingUtils.showMessage(getTopLevel(), "Success", "Generated data file:\n" + getContext().getExecutionPars().getPath(), 5000); + } + + + public static String getCameraType(String name){ + for (String s : new String[]{"LCAM"}){ + if (name.contains(s)){ + return "LCAM"; + } + } + for (String s : new String[]{"DSCR", "DSRM", "DLAC"}){ + if (name.contains(s)){ + return "ELECTRONS"; + } + } + for (String s : new String[]{"PROF", "PPRM", "PSSS", "PSCR", "PSRD"}){ + if (name.contains(s)){ + return "PHOTONICS"; + } + } + return "UNKNOWN"; } - DescStatsDouble xs = new DescStatsDouble(x.toArray(new Double[0]), -1); - DescStatsDouble ys = new DescStatsDouble(y.toArray(new Double[0]), -1); - getContext().getDataManager().closeOutput(); - SwingUtils.showMessage(getTopLevel(), "Success", "Generated data file:\n" + getContext().getExecutionPars().getPath(), 5000); - } + public Map getProcessingParameters(StreamValue value) throws IOException { + return (Map) JsonSerializer.decode(value.getValue("processing_parameters").toString(), Map.class); + } + + void saveFrames(String name, ArrayList frames) throws IOException{ + ArrayList values = new ArrayList<>(); + for (Frame frame : frames){ + values.add(frame.cache); + } + saveImages(name, values); + } + + void saveImages(String name, ArrayList images) throws IOException{ + int depth = images.size(); + if (depth == 0){ + return; + } + StreamValue first = images.get(0); + String pathRoot = "/camera1/"; + String pathImage = pathRoot + "image"; + String pathPid = pathRoot + "pulse_id"; + String pathTimestampStr = pathRoot + "timestamp_str"; + Map processingPars = getProcessingParameters(first); + String camera = (String) processingPars.get("camera_name"); + String type = getCameraType(camera); + + int width = ((Number)first.getValue("width")).intValue(); + int height = ((Number)first.getValue("height")).intValue(); + Class dataType = first.getValue("image").getClass().getComponentType(); + + getContext().setExecutionPars(name); + DataManager dm = getContext().getDataManager(); + + //Create tables + dm.createDataset(pathImage, dataType, new int[]{depth, height, width}); + dm.createDataset(pathPid, Long.class, new int[]{depth}); + dm.createDataset(pathTimestampStr, String.class, new int[]{depth}); + for (String id : first.getIdentifiers()){ + Object val = first.getValue(id); + if (id.equals("image")){ + } else if (id.equals("processing_parameters")){ + Map pars = getProcessingParameters(first); + for (String key : pars.keySet()){ + if ((pars.get(key) != null) && (pars.get(key) instanceof Map)){ + for (Object k : ((Map)pars.get(key)).keySet()){ + Object v = ((Map)pars.get(key)).get(k); + dm.setAttribute(pathImage, key + " " + k, (v == null)? "" : v); + } + } else { + dm.setAttribute(pathImage, key, (pars.get(key) == null)? "" : pars.get(key)); + } + } + } else if (val.getClass().isArray()) { + dm.createDataset(pathRoot + id, Double.class, new int[]{depth, Array.getLength(val)}); + } else { + dm.createDataset(pathRoot + id, val.getClass(), new int[]{depth}); + } + } + + //Add metadata + dm.setAttribute(pathRoot,"Camera", camera); + dm.setAttribute(pathRoot,"Images", depth); + dm.setAttribute(pathRoot,"Interval", -1); + dm.setAttribute(pathRoot,"Type", type); + if (type.equals("ELECTRONS")){ + dm.setAttribute(pathRoot,"Screen", String.valueOf(valueScreen.getLabel().getText())); + dm.setAttribute(pathRoot,"Filter", String.valueOf(valueFilter.getLabel().getText())); + } + + //Save data + for (int index=0; index