diff --git a/devices/CurrentCamera.properties b/devices/CurrentCamera.properties index 7b9bd71..af89cd7 100644 --- a/devices/CurrentCamera.properties +++ b/devices/CurrentCamera.properties @@ -1,5 +1,5 @@ -#Thu Nov 03 11:37:50 CET 2016 -colormap=Grayscale +#Thu Nov 03 15:20:29 CET 2016 +colormap=Temperature colormapAutomatic=true colormapMax=0.0 colormapMin=0.0 diff --git a/plugins/ScreenPanel.java b/plugins/ScreenPanel.java index bd30e24..09da098 100644 --- a/plugins/ScreenPanel.java +++ b/plugins/ScreenPanel.java @@ -19,12 +19,8 @@ import ch.psi.pshell.epics.Camtool; import ch.psi.pshell.core.JsonSerializer; import ch.psi.pshell.device.Device; import ch.psi.pshell.device.DeviceAdapter; -import ch.psi.pshell.device.DeviceListener; import ch.psi.pshell.epics.ArraySource; -import ch.psi.pshell.epics.ChannelDouble; -import ch.psi.pshell.epics.ChannelDoubleArray; import ch.psi.pshell.epics.ChannelInteger; -import ch.psi.pshell.epics.ChannelIntegerArray; import ch.psi.pshell.epics.DiscretePositioner; import ch.psi.pshell.epics.Epics; import ch.psi.pshell.imaging.Colormap; @@ -32,7 +28,6 @@ import ch.psi.pshell.imaging.ColormapSource; import ch.psi.pshell.imaging.ColormapSource.ColormapSourceConfig; import ch.psi.pshell.ui.App; import ch.psi.pshell.imaging.Data; -import ch.psi.pshell.imaging.DimensionDouble; import ch.psi.pshell.imaging.Histogram; import ch.psi.pshell.imaging.Overlay; import ch.psi.pshell.imaging.Overlays; @@ -50,7 +45,6 @@ import ch.psi.pshell.swing.ValueSelection.ValueSelectionListener; import ch.psi.utils.Arr; import ch.psi.utils.ArrayProperties; import ch.psi.utils.Convert; -import ch.psi.utils.Str; import ch.psi.utils.swing.Editor.EditorDialog; import ch.psi.utils.swing.MainFrame; import ch.psi.utils.swing.SwingUtils.OptionResult; @@ -63,7 +57,6 @@ import java.awt.Point; import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.image.BufferedImage; -import java.lang.reflect.Array; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; @@ -87,7 +80,7 @@ import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics; * */ public class ScreenPanel extends Panel { - final boolean USE_CAMTOOL_STATS = true; + boolean useCamtoolStats = true; final String configFolder = "/afs/psi.ch/intranet/SF/Applications/config/camtool"; File[] cameraConfigFiles = new File[0]; ArraySource camera; @@ -132,6 +125,10 @@ public class ScreenPanel extends Panel { ex.printStackTrace(); } } + + if (App.hasArgument("calc")) { + useCamtoolStats = false; + } renderer.setProfileNormalized(true); renderer.setShowProfileLimits(false); @@ -719,19 +716,20 @@ public class ScreenPanel extends Panel { checkHistogram.setSelected((histogramDialog != null) && (histogramDialog.isShowing())); } - Pen fitPen = new Pen(new Color(192, 105, 0), 1); - Pen crossPen = new Pen(new Color(192, 105, 0), 1); + Pen penFit = new Pen(new Color(192, 105, 0), 1); + Pen penCross = new Pen(new Color(192, 105, 0), 1); Overlay[] getFitOverlays(Data data) { - Overlays.Polyline hpoly = null; - Overlays.Polyline vpoly = null; + Overlays.Polyline hgaussian = null; + Overlays.Polyline vgaussian = null; Overlays.Polyline hprofile = null; Overlays.Polyline vprofile = null; Double xMean = null, xSigma = null, xNorm = null; Double yMean = null, ySigma = null, yNorm = null; + Double xCom=null, yCom=null; if (data != null) { int profileSize = renderer.getProfileSize(); - if ((USE_CAMTOOL_STATS) &&(camera instanceof Camtool)){ + if ((useCamtoolStats) &&(camera instanceof Camtool)){ try{ profileSize/=4; double[] gX = ((Camtool)camera).gaussX.read(); @@ -750,8 +748,8 @@ public class ScreenPanel extends Panel { y[i] = (int) (data.getHeight() - 1 - (((gX[i] - minPlot) / rangePlot) * profileSize)); p[i] = (int) (data.getHeight() - 1 - (((pX[i] - minPlot) / rangePlot) * profileSize)); } - vpoly = new Overlays.Polyline(fitPen, x, y); - vprofile = new Overlays.Polyline(fitPen, x, p); + vgaussian = new Overlays.Polyline(penFit, x, y); + vprofile = new Overlays.Polyline(renderer.getPenProfile(), x, p); double[] gY = ((Camtool)camera).gaussY.read(); double[] pY = ((Camtool)camera).profileY.read(); @@ -768,13 +766,15 @@ public class ScreenPanel extends Panel { x[i] = (int) (((gY[i] - minPlot) / rangePlot) * profileSize); p[i] = (int) (((pY[i] - minPlot) / rangePlot) * profileSize); } - hpoly = new Overlays.Polyline(fitPen, x, y); - hprofile = new Overlays.Polyline(fitPen, p, y); + hgaussian = new Overlays.Polyline(penFit, x, y); + hprofile = new Overlays.Polyline(renderer.getPenProfile(), p, y); xMean = ((Camtool)camera).gaussCenterX.read(); yMean = ((Camtool)camera).gaussCenterY.read(); xSigma = ((Camtool)camera).gaussStdDivX.read(); - ySigma = ((Camtool)camera).gaussStdDivY.read(); + ySigma = ((Camtool)camera).gaussStdDivY.read(); + xCom = ((Camtool)camera).posX.read(); + yCom = ((Camtool)camera).posY.read(); } catch (Exception ex){ System.err.println(ex.getMessage()); return null; @@ -789,21 +789,22 @@ public class ScreenPanel extends Panel { return null; } try { - double[] sum = (double[]) Convert.toDouble(data.integrateVertically(true)); + double[] sum = (double[]) Convert.toDouble(data.integrateVertically(true)); + double[] saux = new double[sum.length]; + int[] p = new int[sum.length]; + xCom = getCom(sum); int[] x = Arr.indexesInt(sum.length); - DescriptiveStatistics stats = new DescriptiveStatistics(sum); + DescriptiveStatistics stats = new DescriptiveStatistics(sum); double min = stats.getMin(); for (int i = 0; i < sum.length; i++) { - sum[i] = sum[i] - min; + saux[i] = sum[i] - min; } - double[] gaussian = fitGaussian(sum, x); + double[] gaussian = fitGaussian(saux, x); if (gaussian != null) { if ((gaussian[2] < sum.length * 0.45) && (gaussian[2] > 2) && (gaussian[0] > min * 0.03)) { - //gaussian[0] += min; - xNorm = gaussian[0]; xMean = gaussian[1]; xSigma = gaussian[2]; @@ -811,8 +812,10 @@ public class ScreenPanel extends Panel { int[] y = new int[x.length]; for (int i = 0; i < x.length; i++) { y[i] = (int) (data.getHeight() - 1 - ((((fit[i] + min) / data.getHeight() - minPlot) / rangePlot) * profileSize)); + p[i] = (int) (data.getHeight() - 1 - (((sum[i] / data.getHeight() - minPlot) / rangePlot) * profileSize)); } - vpoly = new Overlays.Polyline(fitPen, x, y); + vgaussian = new Overlays.Polyline(penFit, x, y); + vprofile = new Overlays.Polyline(renderer.getPenProfile(), x, p); } } } catch (Exception ex) { @@ -821,20 +824,22 @@ public class ScreenPanel extends Panel { try { double[] sum = (double[]) Convert.toDouble(data.integrateHorizontally(true)); + double[] saux = new double[sum.length]; + int[] p = new int[sum.length]; + yCom = getCom(sum); int[] x = Arr.indexesInt(sum.length); DescriptiveStatistics stats = new DescriptiveStatistics(sum); double min = stats.getMin(); for (int i = 0; i < sum.length; i++) { - sum[i] = sum[i] - min; + saux[i] = sum[i] - min; } - double[] gaussian = fitGaussian(sum, x); + double[] gaussian = fitGaussian(saux, x); if (gaussian != null) { //Only aknowledge beam fully inside the image and peak over 3% of min if ((gaussian[2] < sum.length * 0.45) && (gaussian[2] > 2) && (gaussian[0] > min * 0.03)) { - //gaussian[0] += min; yNorm = gaussian[0]; yMean = gaussian[1]; ySigma = gaussian[2]; @@ -843,37 +848,66 @@ public class ScreenPanel extends Panel { int[] y = new int[x.length]; for (int i = 0; i < x.length; i++) { y[i] = (int) ((((fit[i] + min) / data.getWidth() - minPlot) / rangePlot) * profileSize); + p[i] = (int) (((sum[i] / data.getWidth() - minPlot) / rangePlot) * profileSize); } - hpoly = new Overlays.Polyline(fitPen, y, x); + hgaussian = new Overlays.Polyline(penFit, y, x); + hprofile = new Overlays.Polyline(renderer.getPenProfile(), p, x); } } } catch (Exception ex) { ex.printStackTrace(); } + if (renderer.getCalibration() != null) { + if (xSigma!=null) xSigma *= renderer.getCalibration().getScaleX(); + if (ySigma!=null) ySigma *= renderer.getCalibration().getScaleY(); + if (xMean!=null) xMean = data.getX((int) Math.round(xMean)); + if (yMean!=null) yMean = data.getY((int) Math.round(yMean)); + if (xCom!=null) xCom = data.getX((int) Math.round(xCom)); + if (yCom!=null) yCom = data.getY((int) Math.round(yCom)); + } } Overlays.Crosshairs cross = null; Overlays.Text text = null; if ((xMean != null) && (yMean != null)) { - cross = new Overlays.Crosshairs(crossPen, - new Point(xMean.intValue(), yMean.intValue()), - new Dimension(2 * xSigma.intValue(), 2 * ySigma.intValue())); - if (renderer.getCalibration() != null) { - xSigma *= renderer.getCalibration().getScaleX(); - ySigma *= renderer.getCalibration().getScaleY(); - } - text = new Overlays.Text(fitPen, - String.format("x = %1.3f " + "\u03C3 = %1.3f \ny = %1.3f " + "\u03C3 = %1.3f", - data.getX((int) Math.round(xMean)), xSigma, data.getY((int) Math.round(yMean)), ySigma), + text = new Overlays.Text(penFit, + String.format("x: m=%1.3f \u03C3=%1.3f c=%1.3f\ny: m=%1.3f \u03C3=%1.3f c=%1.3f", + xMean, xSigma, xCom, + yMean, ySigma, yCom), new Font(Font.MONOSPACED, 0, 14), new Point(20, 20)); text.setFixed(true); text.setAnchor(Overlay.ANCHOR_VIEWPORT_TOP_LEFT); + + Point center = new Point(xMean.intValue(), yMean.intValue()); + if (renderer.getCalibration() != null) { + center = renderer.getCalibration().convertToImagePosition(new PointDouble(xMean, yMean)); + xSigma /= renderer.getCalibration().getScaleX(); + ySigma /= renderer.getCalibration().getScaleY(); + } + cross = new Overlays.Crosshairs(penCross,center, new Dimension(Math.abs(2 * xSigma.intValue()), 2 * Math.abs(ySigma.intValue()))); } - return new Overlay[]{hpoly, vpoly, hprofile, vprofile, cross, text}; + return new Overlay[]{hprofile, vprofile, hgaussian, vgaussian, cross, text}; } return null; } + + double getCom(double[] arr){ + if (arr==null) { + return Double.NaN; + } + int index = 0; + double ret = 0; + double total=0; + for (double v: arr){ + ret += (v * index++); + total +=v; + } + if (total==0){ + return Double.NaN; + } + return ret/total; + } double[] fitGaussianScript(int[] y, int[] x) { ScriptManager sm = Controller.getInstance().getScriptManager(); @@ -918,7 +952,7 @@ public class ScreenPanel extends Panel { } return fit; } - +/* public class Camtool extends ArraySource { final String prefix; @@ -974,10 +1008,10 @@ public class ScreenPanel extends Panel { gaussX = new ChannelDoubleArray(name + " gauss x", dataPrefix + ("x_stats.gauss")); gaussY = new ChannelDoubleArray(name + " gauss y", dataPrefix + ("y_stats.gauss")); - gaussCenterX = new ChannelDouble(name + "gauss center x", dataPrefix + "x_stats.g_center"); - gaussCenterY = new ChannelDouble(name + "gauss center y", dataPrefix + "y_stats.g_center"); - gaussStdDivX = new ChannelDouble(name + "gauss stddiv x", dataPrefix + "x_stats.g_stddiv"); - gaussStdDivY = new ChannelDouble(name + "gauss stddiv t", dataPrefix + "y_stats.g_stddiv"); + gaussCenterX = new ChannelDouble(name + "gauss center x", dataPrefix + "x_stats.g_center_egu"); + gaussCenterY = new ChannelDouble(name + "gauss center y", dataPrefix + "y_stats.g_center_egu"); + gaussStdDivX = new ChannelDouble(name + "gauss stddiv x", dataPrefix + "x_stats.g_stddiv_egu"); + gaussStdDivY = new ChannelDouble(name + "gauss stddiv t", dataPrefix + "y_stats.g_stddiv_egu"); } @Override @@ -1164,7 +1198,8 @@ public class ScreenPanel extends Panel { } } } - +*/ + void setHistogramVisible(boolean value) { if (value) { if ((histogramDialog == null) || (!histogramDialog.isShowing())) { @@ -1957,6 +1992,8 @@ public class ScreenPanel extends Panel { if (!buttonFit.isSelected()) { renderer.removeOverlays(fitOv); fitOv = null; + } else { + renderer.setProfile(Renderer.Profile.None); } } catch (Exception ex) { showException(ex); @@ -2062,7 +2099,7 @@ public class ScreenPanel extends Panel { } renderer.setPenReticle(new Pen(colorReticule)); - renderer.setPenProfile(new Pen(colorReticule)); + renderer.setPenProfile(new Pen(colorReticule,0)); renderer.setPenMarker(new Pen(colorMarker, 2)); renderer.setShowReticle(false); checkReticle();