This commit is contained in:
root
2017-11-27 10:01:18 +01:00
parent 8d9794c431
commit 8e826e71d6
3 changed files with 223 additions and 170 deletions

View File

@@ -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<fitOv.length; i++ ){
if (fitOv[i] instanceof Overlays.Text){
Overlays.Text text = (Overlays.Text) fitOv[i];
message.append(text.getText()).append("\n");
}
String snapshotFile = null;
synchronized (imageBuffer) {
Frame frame = getCurrentFrame();
if (frame == null) {
throw new Exception("No current image");
}
ArrayList<Frame> 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<Double> x = new ArrayList<>();
ArrayList<Double> 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<String, Object> getProcessingParameters(StreamValue value) throws IOException {
return (Map) JsonSerializer.decode(value.getValue("processing_parameters").toString(), Map.class);
}
void saveFrames(String name, ArrayList<Frame> frames) throws IOException{
ArrayList<StreamValue> values = new ArrayList<>();
for (Frame frame : frames){
values.add(frame.cache);
}
saveImages(name, values);
}
void saveImages(String name, ArrayList<StreamValue> 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<String, Object> 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<String, Object> 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<depth; index++){
StreamValue streamValue = images.get(index);
dm.setItem(pathImage, streamValue.getValue("image"), new long[]{index,0,0}, new int[]{1, height, width});
dm.setItem(pathPid, streamValue.getPulseId(), index);
dm.setItem(pathTimestampStr, Chrono.getTimeStr(streamValue.getTimestamp(), "YYYY-MM-dd HH:mm:ss.SSS") , index);
for (String id : streamValue.getIdentifiers()){
Object val = streamValue.getValue(id);
if (id.equals("image")){
} else if (id.equals("processing_parameters")){
} else if (val.getClass().isArray()) {
dm.setItem(pathRoot + id, val, index);
} else {
dm.setItem(pathRoot + id, val, index);
}
}
}
getContext().getDataManager().closeOutput();
}
StandardDialog calibrationDialolg;