ScreenPanel

This commit is contained in:
sfop
2017-06-19 11:07:40 +02:00
parent 3524af3ca1
commit 060f262a5a
27 changed files with 666 additions and 178 deletions

View File

@@ -19,6 +19,7 @@ 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;
@@ -63,6 +64,7 @@ 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.net.ServerSocket;
import java.nio.file.Files;
import java.nio.file.Path;
@@ -135,18 +137,18 @@ public class ScreenPanel extends Panel {
ImageData(){
if (camera instanceof Camtool) {
String prefix = goodRegion ? "gr_" : "";
xMean = getCamtoolDouble(prefix + "x_fit_mean");
yMean = getCamtoolDouble(prefix + "y_fit_mean");
xSigma = getCamtoolDouble(prefix + "x_fit_standard_deviation");
ySigma = getCamtoolDouble(prefix + "y_fit_standard_deviation");
x_fit_mean = getCamtoolDouble(prefix + "x_fit_mean");
y_fit_mean = getCamtoolDouble(prefix + "y_fit_mean");
x_fit_standard_deviation = getCamtoolDouble(prefix + "x_fit_standard_deviation");
y_fit_standard_deviation = getCamtoolDouble(prefix + "y_fit_standard_deviation");
x_fit_gauss_function = getCamtoolDoubleArray(prefix +"x_fit_gauss_function");
y_fit_gauss_function = getCamtoolDoubleArray(prefix +"y_fit_gauss_function");
x_profile = getCamtoolDoubleArray("x_profile");
y_profile = getCamtoolDoubleArray("y_profile");
xCom = getCamtoolDouble("x_center_of_mass");
yCom = getCamtoolDouble("y_center_of_mass");
xRms = getCamtoolDouble("x_rms");
yRms = getCamtoolDouble("y_rms");
x_center_of_mass = getCamtoolDouble("x_center_of_mass");
y_center_of_mass = getCamtoolDouble("y_center_of_mass");
x_rms = getCamtoolDouble("x_rms");
y_rms = getCamtoolDouble("y_rms");
if (goodRegion){
double[] gX2 = new double[x_profile.length];
Arrays.fill(gX2, Double.NaN);
@@ -167,18 +169,18 @@ public class ScreenPanel extends Panel {
}
}
}
Double xMean;
Double yMean;
Double xCom;
Double xRms;
Double xSigma;
Double ySigma;
Double yCom;
Double yRms;
double[] x_profile;
double[] x_fit_gauss_function;
double[] y_profile;
double[] y_fit_gauss_function;
public Double x_fit_mean;
public Double y_fit_mean;
public Double x_center_of_mass;
public Double x_rms;
public Double x_fit_standard_deviation;
public Double y_fit_standard_deviation;
public Double y_center_of_mass;
public Double y_rms;
public double[] x_profile;
public double[] x_fit_gauss_function;
public double[] y_profile;
public double[] y_fit_gauss_function;
}
class Frame extends ImageData{
@@ -993,6 +995,34 @@ public class ScreenPanel extends Panel {
Pen penFit = new Pen(new Color(192, 105, 0), 1);
Pen penCross = new Pen(new Color(192, 105, 0), 1);
ImageData getCurrentImageData(){
return getImageData(null);
}
ImageData getImageData(Data data){
ImageData id = null;
if (renderer.isPaused()) {
synchronized (imageBuffer) {
if (data == null){
int index = ((int) pauseSelection.getValue()) - 1;
if (index < imageBuffer.size()) {
data = imageBuffer.get(index).data;
}
}
for (Frame f : imageBuffer) {
if (f.data == data) {
id = f;
break;
}
}
}
} else {
id = new ImageData();
}
return id;
}
Overlay[][] getFitOverlays(Data data) {
Overlays.Polyline hgaussian = null;
@@ -1009,26 +1039,15 @@ public class ScreenPanel extends Panel {
int profileSize = renderer.getProfileSize();
if ((useCamtoolStats) && (camera instanceof Camtool)) {
try {
ImageData id = null;
if (renderer.isPaused()) {
synchronized (imageBuffer) {
for (Frame f : imageBuffer) {
if (f.data == data) {
id = f;
break;
}
}
}
} else {
id = new ImageData();
}
ImageData id = getImageData(data);
if (id==null){
return null;
}
xMean = id.xMean; xSigma = id.xSigma;yMean = id.yMean;ySigma = id.ySigma;
xMean = id.x_fit_mean; xSigma = id.x_fit_standard_deviation;yMean = id.y_fit_mean;ySigma = id.y_fit_standard_deviation;
gX = id.x_fit_gauss_function; gY = id.y_fit_gauss_function;
pX = id.x_profile; pY = id.y_profile;
xCom = id.xCom; xRms = id.xRms; yCom = id.yCom; yRms = id.yRms;
xCom = id.x_center_of_mass; xRms = id.x_rms; yCom = id.y_center_of_mass; yRms = id.y_rms;
profileSize /= 4;
if (pX != null) {
@@ -1539,18 +1558,49 @@ public class ScreenPanel extends Panel {
}
}
}
void writeFrameMetadata(String path, ImageData id) 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()});
if (camera instanceof Camtool){
try{
getContext().getDataManager().setAttribute("/", "ROI", ((Camtool) camera).getRoi());
} catch (Exception ex){
getContext().getDataManager().setAttribute("/", "ROI", new int[]{0,0,-1,-1});
}
if (id!=null){
for (Field f:ImageData.class.getFields()){
Object value = f.get(id);
getContext().getDataManager().setAttribute(path, f.getName(), (value == null) ? Double.NaN : value);
}
}
for (String name : new String[]{"x_axis", "y_axis", "gr_x_axis", "gr_y_axis"}){
double[] val = getCamtoolDoubleArray(name);
getContext().getDataManager().setAttribute("/", "GoodRegion", goodRegion);
if (val!=null){
getContext().getDataManager().setAttribute("/", name, val);
}
}
}
}
void saveSnapshot() throws Exception {
String snapshotFile = getContext().getSetup().expandPath("{images}/{date}_{time}_snapshot.png");
renderer.saveSnapshot(snapshotFile, "png", true);
getContext().setExecutionPars("snapshot");
String path = "/data";
getContext().getDataManager().setDataset(path, renderer.getData().getMatrix(), renderer.getData().isUnsigned());
getContext().getDataManager().setAttribute(path, "Camera", String.valueOf(cameraName));
getContext().getDataManager().setAttribute(path, "Screen", String.valueOf(valueScreen.getLabel().getText()));
getContext().getDataManager().setAttribute(path, "Filter", String.valueOf(valueFilter.getLabel().getText()));
getContext().getDataManager().closeOutput();
String snapshotFile = null;
synchronized (imageBuffer) {
getContext().getDataManager().setDataset(path, renderer.getData().getMatrix(), renderer.getData().isUnsigned());
writeFrameMetadata(path, getCurrentImageData());
getContext().getDataManager().closeOutput();
//Enforce the same timestamp to data & image files.
snapshotFile = getContext().getSetup().expandPath("{images}/{date}_{time}_snapshot.png", getContext().getExecutionPars().getStart());
renderer.saveSnapshot(snapshotFile, "png", true);
}
JPanel panel = new JPanel();
GridBagLayout layout = new GridBagLayout();
layout.columnWidths = new int[]{0, 180}; //Minimum width
@@ -1601,21 +1651,14 @@ public class ScreenPanel extends Panel {
Frame frame = imageBuffer.get(i);
String path = "/data_" + i;
getContext().getDataManager().setDataset(path, frame.data.getMatrix(), frame.data.isUnsigned());
getContext().getDataManager().setAttribute(path, "x_mean", (frame.xMean == null) ? Double.NaN : frame.xMean);
getContext().getDataManager().setAttribute(path, "x_sigma", (frame.xSigma == null) ? Double.NaN : frame.xSigma);
getContext().getDataManager().setAttribute(path, "y_mean", (frame.yMean == null) ? Double.NaN : frame.yMean);
getContext().getDataManager().setAttribute(path, "y_sigma", (frame.ySigma == null) ? Double.NaN : frame.ySigma);
x.add(frame.xMean);
y.add(frame.yMean);
writeFrameMetadata(path, getImageData(frame.data));
x.add(frame.x_fit_mean);
y.add(frame.y_fit_mean);
}
}
DescStatsDouble xs = new DescStatsDouble(x.toArray(new Double[0]), -1);
DescStatsDouble ys = new DescStatsDouble(y.toArray(new Double[0]), -1);
String path = "/";
getContext().getDataManager().setAttribute(path, "x_mean", xs.getMean());
getContext().getDataManager().setAttribute(path, "x_sigma", xs.getStdev());
getContext().getDataManager().setAttribute(path, "y_mean", ys.getMean());
getContext().getDataManager().setAttribute(path, "y_sigma", ys.getStdev());
getContext().getDataManager().closeOutput();
SwingUtils.showMessage(getTopLevel(), "Success", "Generated data file:\n" + getContext().getExecutionPars().getPath(), 5000);
}