DistortionCalibrationData.java 258 KB
Newer Older
Andrey Filippov's avatar
Andrey Filippov committed
1
package com.elphel.imagej.calibration;
2 3 4 5 6 7 8
/*
 **
 ** DistortionCalibrationData.java
 **
 ** Copyright (C) 2011-2014 Elphel, Inc.
 **
 ** -----------------------------------------------------------------------------**
9
 **
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 **  DistortionCalibrationData.java is free software: you can redistribute it and/or modify
 **  it under the terms of the GNU General Public License as published by
 **  the Free Software Foundation, either version 3 of the License, or
 **  (at your option) any later version.
 **
 **  This program is distributed in the hope that it will be useful,
 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 **  GNU General Public License for more details.
 **
 **  You should have received a copy of the GNU General Public License
 **  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ** -----------------------------------------------------------------------------**
 **
 */

26
import java.awt.Rectangle;
27 28 29 30 31
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
32
import java.util.Arrays;
33 34
import java.util.Collections;
import java.util.Comparator;
35 36 37 38 39 40
import java.util.List;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.HierarchicalConfiguration;
import org.apache.commons.configuration.XMLConfiguration;

41 42
import com.elphel.imagej.cameras.EyesisCameraParameters;
import com.elphel.imagej.cameras.EyesisSubCameraParameters;
Andrey Filippov's avatar
Andrey Filippov committed
43
import com.elphel.imagej.common.DoubleGaussianBlur;
44
import com.elphel.imagej.common.PolynomialApproximation;
45
import com.elphel.imagej.common.ShowDoubleFloatArrays;
46
import com.elphel.imagej.common.WindowTools;
Andrey Filippov's avatar
Andrey Filippov committed
47 48
import com.elphel.imagej.jp4.JP46_Reader_camera;

49
import Jama.Matrix;
50 51 52 53 54 55 56 57 58 59 60
import ij.IJ;
import ij.ImagePlus;
import ij.ImageStack;
import ij.Prefs;
import ij.gui.GenericDialog;
import ij.io.FileSaver;
import ij.io.Opener;
import ij.process.FloatProcessor;
import ij.process.ImageProcessor;
import ij.text.TextWindow;

61 62 63
// stores per pattern image camera/subcamera parameters, filenames, ...
// saves / restores them from a disk file
    public class DistortionCalibrationData{
64 65 66 67 68 69 70 71
    	public static final int INDEX_PX =       0;
    	public static final int INDEX_PY =       1;
    	public static final int INDEX_U =        2;
    	public static final int INDEX_V =        3;
    	public static final int INDEX_CONTRAST = 4;
    	public static final int INDEX_R =        5;
    	public static final int INDEX_G =        6;
    	public static final int INDEX_B =        7;
72
    	public static final double SMALL_FRACTION = 0.8; // consider sensor to be a "small" if average grid period < this fraction of the large
73

Andrey Filippov's avatar
Andrey Filippov committed
74
    	Goniometer.GoniometerParameters goniometerParameters = null;
75
    	public String pathName=null;
76
    	public EyesisCameraParameters eyesisCameraParameters; // has "cartesian"
77 78 79 80
        public int       numSubCameras=1;
        public int       numPointers=4;    // maximal number of pointers to look for
        public int       numMotors  =3;    // maximal number of motors to look for
        public GridImageParameters [] gIP= null; // per-grid image parameters
81
        public GridImageSet []        gIS= null; // sets of images with the same timestamp
82 83
        public boolean [] small_sensors =    null; // set by filter grids
        public double     small_period_frac =   0; // set by filter grids - ratio of small sensor period to large sensor period
84 85 86
        // keep for now?
    	public double [][] pars=null; // for each defined image: set of (22) parameters
    	public double [][] sensorMasks= null; // per-channel (not image) mask
87

88 89 90
    	//pixelsXY, pixelsUV should match, second dimension is variable
    	public boolean updateStatus=true;
    	public int     debugLevel=2;
91
    	private ShowDoubleFloatArrays SDFA_INSTANCE=null; // just for debugging
92 93 94 95
    	public int getNumStations(){
    		return (eyesisCameraParameters==null)?0:eyesisCameraParameters.getNumStations();
    	}

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
		public double getPixelSize(int station, int channel) {
			return this.eyesisCameraParameters.eyesisSubCameras[station][channel].getPixelSize();
		}
		public double getDistortionRadius(int station, int channel) {
			return this.eyesisCameraParameters.eyesisSubCameras[station][channel].getDistortionRadius();
		}
		public double getPixelSize(int imgNumber) {
			return getPixelSize(this.gIP[imgNumber].stationNumber, this.gIP[imgNumber].channel);
		}
		public double getDistortionRadius(int imgNumber) {
			return getDistortionRadius(this.gIP[imgNumber].stationNumber, this.gIP[imgNumber].channel);
		}
        public boolean isTripod() {
        	return (eyesisCameraParameters !=null) && this.eyesisCameraParameters.isTripod();
        }

        public boolean isCartesian() {
        	return (eyesisCameraParameters !=null) && this.eyesisCameraParameters.isCartesian();
        }


117
     	public class GridImageParameters{
118 119 120 121 122
    		public int         imgNumber=-1; // index of this image (for pars[][])
    		private int        setNumber=-1; // long overdue  - will be some inconsistency
    		GridImageSet       gridImageSet=null;
    		public int         stationNumber=0; // changes when camera/goniometer is moved to new position
    		public String      path=null;
123
    		public String      source_path = null; // Full path of the source image this grid was calculated from
124
    		public double [][] laserPixelCoordinates=null; // first index - absolute number of pointer. Each element may be either null or {x,y} pair
125
    		// moving for new files to have laser UV contained in the file
126
    		public double [][] laserUVCoordinates=null;    // first index - absolute number of pointer. Each element may be either null or {u,v} pair - never used??????
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
    		public int         matchedPointers=0;
    		public int         hintedMatch=-1; // -1 - not tried, 0 - no real grid (i.e. double reflection), applied orientation, applied orientation and shift
    		public boolean     enabled=true; //false;  // to mask out some images from all strategy steps (i.e w/o reliable absolute calibration)
    		public boolean     flatFieldAvailable=false; // grid files have flat field data
    		public boolean     newEnabled=false;
    		public int []      motors=null;
    		public ImagePlus   gridImage=null;
    		public double      timestamp=-1;
    		public int         channel=  -1;
    		public double []   intensityRange={255.0,255.0,255.0}; // r,g,b - used to normalize vign*
    		public double []   saturation={255.0,255.0,255.0}; // r,g,b - saturation range read from images
//    		public double  []  pars=null; // set of (22) parameters
    		public double [][] pixelsXY=   null; // for each image, each grid node - a set of of {px,py,contrast,vignR,vignG,vignB} vign* is in the 0..1.0 range
    		public double []   pixelsMask= null; // for each image, each grid node - weight function derived from contrast and 3 parameters
    		public int    [][] pixelsUV=  null; // for each image, each grid node - a pair of {gridU, gridV}
142
    		public boolean  [] badNodes=  null; // if not null, marks node with excessive errors
143
    		public double [][] pixelsXY_extra=  null; // extra data, for nodes that are out of the physical grid (may be needed after re-calibration)
144
    		public int    [][] pixelsUV_extra=  null;
145
    		private double      gridPeriod=0.0;  // average grid period, in pixels (to filter out (double-) reflected images
146 147
    		public boolean     noUsefulPSFKernels=false; // used to mark images w/o good PSF data
    		public double      diameter=0.0;
148
    		public int []      UVShiftRot={0,0,0}; // shift and rotation of the grid
149
    		public Rectangle   woi;
150
    		final int contrastIndex=2;
151 152


153

154
    		public double getGridPeriod() {	return gridPeriod;}
155
    		public void setGridPeriod(double v) {gridPeriod = v;}
Andrey Filippov's avatar
Andrey Filippov committed
156
    		public int getSetNumber(){return this.setNumber;}
157
    		public int getImageNumber() {return this.imgNumber;}
158 159 160
        	public GridImageParameters(int index){
        		this.imgNumber=index;
        	}
161 162 163 164 165 166
            public int [] getUVShiftRot(){
            	return this.UVShiftRot;
            }
            public void setUVShiftRot(int [] UVShiftRot){
            	this.UVShiftRot=UVShiftRot;
            }
167 168 169 170 171 172
        	public int getStationNumber(){ // TODO: make only a single station number - in GridImageSet?
        		return this.stationNumber;
        	}
        	public void setStationNumber(int stationNumber){ // TODO: make only a single station number - in GridImageSet?
        		this.stationNumber=stationNumber;
        	}
173
        	public double [] getGridWeight(){
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
        		return this.pixelsMask;
        	}
        	public void resetMask(){
        			this.pixelsMask=null;
    	    }
        	public void resetBadNodes(){
        		this.badNodes=null;
        	}
        	public void setBadNode(int index){
        		if (this.badNodes==null){
        			this.badNodes=new boolean[this.pixelsXY.length]; // let it throw if null
        			for (int i=0;i<this.badNodes.length;i++)this.badNodes[i]=false;
        		}
        		this.badNodes[index]=true;
        	}
        	public boolean isNodeBad(int index){
        		if (this.badNodes==null) return false;
        		if (index>=this.badNodes.length) {
        			System.out.println("### isNodeBad("+index+") - OOB, as this.badNodes="+this.badNodes.length);
        			return true;
        		}
        		return this.badNodes[index]; //OOB
        	}

        	public int getNumContrastNodes(double minContrast){
199
        	    int num=0;
200 201 202
        		for (int i=0;i<this.pixelsXY.length;i++) if (this.pixelsXY[i][contrastIndex]>=minContrast) num++;
        		return num;
        	}
203 204 205 206 207 208

        	public int getChannel() {
        		return channel;
        	}


209 210 211 212 213 214 215
        	/**
        	 * Calculate "diameter" of the image to be used for image weight
        	 * @param xc image center pixel X
        	 * @param yc image center pixel Y
        	 * @param r0 reference diameter
        	 * @param minContrast minimal contrast to count the node
        	 */
216

217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
        	public void setImageDiameter( // need to get image center px,py. Maybe r0 - use to normalize result diameter
        			double xc,
        			double yc,
        			double r0,
        			double minContrast,
        			int dbgImgNum //>=0 - debug print with image number
        			){
        		boolean debug=(dbgImgNum>=0);
        		// find the farthest point from the center
        		double maxR2=-1;
        		int firstIndex=0;
        		for (int i=0;i<this.pixelsXY.length;i++) if (this.pixelsXY[i][contrastIndex]>=minContrast){
        			double dx=this.pixelsXY[i][0]-xc;
        			double dy=this.pixelsXY[i][1]-yc;
        			double r2=dx*dx+dy*dy;
        			if (r2>maxR2) {
        				maxR2=r2;
        				firstIndex=i;
        			}
        		}
        		if (maxR2<=0) {
        			this.diameter=0.0;
        			return;
        		}
        		double maxDx=this.pixelsXY[firstIndex][0]-xc;
        		double maxDy=this.pixelsXY[firstIndex][1]-yc;

        		if (debug) System.out.print("setImageDiameter("+IJ.d2s(xc,2)+","+IJ.d2s(yc,2)+","+IJ.d2s(r0,2)+","+IJ.d2s(minContrast,4)+","+dbgImgNum+") ---- > ");
        		if (debug) System.out.print(" maxR2="+IJ.d2s(maxR2,2)+" maxDx="+IJ.d2s(maxDx,2)+" maxDy="+IJ.d2s(maxDy,2));
        		double maxAamb=0;
        		double dbgDx=0.0,dbgDy=0.0;
        		for (int i=0;i<this.pixelsXY.length;i++) if (this.pixelsXY[i][contrastIndex]>=minContrast){
//        			double dx=maxDx-this.pixelsXY[i][0];
//        			double dy=maxDy-this.pixelsXY[i][1];
        			double dx=this.pixelsXY[firstIndex][0]-this.pixelsXY[i][0];
        			double dy=this.pixelsXY[firstIndex][1]-this.pixelsXY[i][1];
        			double aAmb=dx*maxDx+dy*maxDy;
        			if (aAmb>maxAamb) {
        				maxAamb=aAmb;
        				dbgDx=this.pixelsXY[i][0]; // debug only !
        				dbgDy=this.pixelsXY[i][1]; // debug only !
        			}
        		}
        		this.diameter=maxAamb/Math.sqrt(maxR2)/r0;
        		if (debug) System.out.println(" maxAamb="+IJ.d2s(maxAamb,2)+" dbgDx="+IJ.d2s(dbgDx,2)+" dbgDy="+IJ.d2s(dbgDy,2)+" --> "+IJ.d2s(this.diameter,2));
        	}
        	/**
        	 * Uzses data calculated by  setImageDiameter();
        	 * @return detected grid diameter (along the radius) to be uses as image weight (in r0 units)
        	 */
        	public double getGridDiameter(){
        		return this.diameter;
        	}

        	public void calculateMask(
        			double minContrast,
        			double shrinkBlurSigma,
        			double shrinkBlurLevel){
275
        		if (this.pixelsMask!=null) return; // need to reset to re-calculate
276 277
        		if (this.pixelsUV==null) {this.pixelsMask=null; return; }
        		if (this.pixelsUV.length==0){ this.pixelsMask=new double[0]; return; }
278

279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
        		this.pixelsMask=new double [this.pixelsUV.length];
        		if (shrinkBlurSigma<=0){
            		for (int i=0;i<this.pixelsUV.length;i++){
            			this.pixelsMask[i]=(this.pixelsXY[i][contrastIndex]>=minContrast)?1.0:0.0;
            		}
            		return;
        		}
        		int minU=this.pixelsUV[0][0],minV=this.pixelsUV[0][1];
        		int maxU=minU,maxV=minV;
        		int margin=(int) (2*shrinkBlurSigma);
        		for (int i=0;i<this.pixelsUV.length;i++){
        			if (this.pixelsUV[i][0]>maxU) maxU=this.pixelsUV[i][0];
        			if (this.pixelsUV[i][0]<minU) minU=this.pixelsUV[i][0];
        			if (this.pixelsUV[i][1]>maxV) maxV=this.pixelsUV[i][1];
        			if (this.pixelsUV[i][1]<minV) minV=this.pixelsUV[i][1];
        		}
295

296 297 298 299 300 301 302 303 304
        		int U0=minU-margin;
        		int V0=minV-margin;
        		int width= (maxU-minU+1+2*margin);
        		int height=(maxV-minV+1+2*margin);
        		double [] mask = new double [width*height];
        		for (int i=0;i<mask.length;i++) mask[i]=-1.0;
        		for (int i=0;i<this.pixelsUV.length;i++){
        			int index=(this.pixelsUV[i][0]-U0)+width*(this.pixelsUV[i][1]-V0);
        			mask[index]=(this.pixelsXY[i][contrastIndex]>=minContrast)?1.0:-1.0; // java.lang.ArrayIndexOutOfBoundsException: 2230
305
        		}
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
        		(new DoubleGaussianBlur()).blurDouble(
							mask,
							width,
							height,
							shrinkBlurSigma,
							shrinkBlurSigma,
							0.01);
        		double k=1.0/(1.0-shrinkBlurLevel);
        		double dbgMax=0.0;
        		for (int i=0;i<this.pixelsUV.length;i++){
        			int index=(this.pixelsUV[i][0]-U0)+width*(this.pixelsUV[i][1]-V0);
        			double d=k*(mask[index]-shrinkBlurLevel);
        			this.pixelsMask[i]=(d>0.0)?(d*d):0.0;
        			if (this.pixelsMask[i]>dbgMax) dbgMax=this.pixelsMask[i];
        		}
 //      		System.out.print(" "+IJ.d2s(dbgMax,2)+" ");
        	}
    	}
    	public class GridImageSet{
325
    		private int numPars=53; // 27;
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
    		private int thisParsStartIndex=6;

    		public int         stationNumber=0; // changes when camera/goniometer is moved to new position
    		public GridImageParameters [] imageSet=null;
//    		public GridImageParameters firstImage=null; // first non-null image in the sert (update to have current parameters?)
    		public double timeStamp;
    		public int [] motors=null;
    		public double goniometerAxial=Double.NaN;
    		public double goniometerTilt=Double.NaN;
    		public double interAxisDistance;    // 8 distance in mm between two goniometer axes
    		public double interAxisAngle;       // 9 angle in degrees between two goniometer axes minus 90. negative if "vertical" axis is rotated
    		public double horAxisErrPhi;        //10 angle in degrees "horizontal" goniometer axis is rotated around target Y axis from target X axis (CW)
    		public double horAxisErrPsi;        //11 angle in degrees "horizontal" goniometer axis is rotated around moving X axis (up)
    		public double entrancePupilForward; //12 common to all lenses - distance from the sensor to the lens entrance pupil
    		public double centerAboveHorizontal;//13 camera center distance along camera axis above the closest point to horizontal rotation axis (adds to height of each
341
    		public double [] GXYZ=new double [3];  //14 (12) coordinates (in mm) of the goniometer horizontal axis closest to the moving one in target system
342 343
//			this.GXYZ[stationNumber][1],              //15 (13)  y
//			this.GXYZ[stationNumber][2],              //16 (14)  z
344
    		public boolean orientationEstimated=true; // orientation is estimated from other stes, not adjusted by LMA
345 346 347 348 349 350 351 352 353 354
    		public double setWeight=0.0; // weight of this set when calculating errors
    		public void setEstimatedFromNonNaN(){
    			this.orientationEstimated= Double.isNaN(this.goniometerTilt) ||  Double.isNaN(this.goniometerAxial);
    		}
    		public int getMinIndex(){
    			return this.thisParsStartIndex;
    		}
    		public int getMaxIndexPlusOne(){
    			return this.thisParsStartIndex+getSetVector().length;
    		}
355

356 357 358 359 360 361 362 363 364 365
    		public double [] getSetVector(){
    			double [] sv={
    		    		this.goniometerTilt,
    		    		this.goniometerAxial,
    		    		this.interAxisDistance,
    		    		this.interAxisAngle,
    		    		this.horAxisErrPhi,
    		    		this.horAxisErrPsi,
    		    		this.entrancePupilForward,
    		    		this.centerAboveHorizontal,
366 367 368
    		    		this.GXYZ[0],
    		    		this.GXYZ[1],
    		    		this.GXYZ[2]
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
    			};
    			return sv;
    		}
    		public void setSetVector(double [] vector){
    			if (vector.length!=getSetVector().length){
    				String msg="Wrong parameter vector length - got:"+vector.length+", expected: "+getSetVector().length;
    				IJ.showMessage("Error",msg);
    				throw new IllegalArgumentException (msg);
    			}
	    		this.goniometerTilt=       vector[ 0];
	    		this.goniometerAxial=      vector[ 1];
	    		this.interAxisDistance=    vector[ 2];
	    		this.interAxisAngle=       vector[ 3];
	    		this.horAxisErrPhi=        vector[ 4];
	    		this.horAxisErrPsi=        vector[ 5];
	    		this.entrancePupilForward= vector[ 6];
	    		this.centerAboveHorizontal=vector[ 7];
386 387 388
	    		this.GXYZ[0]=              vector[ 8];
	    		this.GXYZ[1]=              vector[ 9];
	    		this.GXYZ[2]=              vector[10];
389 390 391 392 393 394 395
    		}

    		public double getParameterValue(int index){
    			int thisIndex=index-this.thisParsStartIndex;
    			double [] sv=getSetVector();
    			if ((thisIndex<0) || (index >sv.length)) return Double.NaN;
    			return sv[thisIndex];
396

397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
    		}
    		public void setParameterValue(int index,
    				double value,
    				boolean updateEstimated){
    			int thisIndex=index-this.thisParsStartIndex;
    			switch (thisIndex){
    			case  0:
    				this.goniometerTilt=       value;
    				setEstimatedFromNonNaN();
    				break;
    			case  1:
    				this.goniometerAxial=      value;
    				setEstimatedFromNonNaN();
    				break;
    			case  2: this.interAxisDistance=    value; break;
    			case  3: this.interAxisAngle=       value; break;
    			case  4: this.horAxisErrPhi=        value; break;
    			case  5: this.horAxisErrPsi=        value; break;
    			case  6: this.entrancePupilForward= value; break;
    			case  7: this.centerAboveHorizontal=value; break;
417 418 419
    			case  8: this.GXYZ[0]=              value; break;
    			case  9: this.GXYZ[1]=              value; break;
    			case 10: this.GXYZ[2]=              value; break;
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
    			}
    		}

    		public double [] updateParameterVectorFromSet(double [] vector){
    			if (vector==null){
    				vector=new double [this.numPars];
    				for (int i=0;i<vector.length;i++) vector[i]=Double.NaN;
    			}
    			if (vector.length!=this.numPars){
    				String msg="Wrong parameter vector length - got:"+vector.length+", expected: "+this.numPars;
    				IJ.showMessage("Error",msg);
    				throw new IllegalArgumentException (msg);
    			}
    			double [] sv=getSetVector();
    			for (int i=0;i<sv.length;i++) if (!Double.isNaN(sv[i])) vector[i+this.thisParsStartIndex]=sv[i];
    			return vector;
    		}
    		public double [] updateParameterVectorFromSet(double [] vector, boolean [] mask){
    			if (vector==null){
    				vector=new double [this.numPars];
    				for (int i=0;i<vector.length;i++) vector[i]=Double.NaN;
    			}
    			if (vector.length!=this.numPars){
    				String msg="Wrong parameter vector length - got:"+vector.length+", expected: "+this.numPars;
    				IJ.showMessage("Error",msg);
    				throw new IllegalArgumentException (msg);
    			}
    			double [] sv=getSetVector();
    			for (int i=0;i<sv.length;i++) if (!Double.isNaN(sv[i]) && mask[this.thisParsStartIndex+ i]) vector[i+this.thisParsStartIndex]=sv[i];
    			return vector;
    		}
451

452 453 454 455 456 457 458 459 460 461 462 463 464 465
    		public void updateSetFromParameterVector(double [] vector){
    			if (vector.length!=this.numPars){
    				String msg="Wrong parameter vector length - got:"+vector.length+", expected: "+this.numPars;
    				IJ.showMessage("Error",msg);
    				throw new IllegalArgumentException (msg);
    			}
	    		this.goniometerTilt=       vector[this.thisParsStartIndex+ 0];
	    		this.goniometerAxial=      vector[this.thisParsStartIndex+ 1];
	    		this.interAxisDistance=    vector[this.thisParsStartIndex+ 2];
	    		this.interAxisAngle=       vector[this.thisParsStartIndex+ 3];
	    		this.horAxisErrPhi=        vector[this.thisParsStartIndex+ 4];
	    		this.horAxisErrPsi=        vector[this.thisParsStartIndex+ 5];
	    		this.entrancePupilForward= vector[this.thisParsStartIndex+ 6];
	    		this.centerAboveHorizontal=vector[this.thisParsStartIndex+ 7];
466 467 468
	    		this.GXYZ[0]=              vector[this.thisParsStartIndex+ 8];
	    		this.GXYZ[1]=              vector[this.thisParsStartIndex+ 9];
	    		this.GXYZ[2]=              vector[this.thisParsStartIndex+10];
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
    		}

    		public void updateSetFromParameterVector(double [] vector, boolean [] mask){
    			if (vector.length!=this.numPars){
    				String msg="Wrong parameter vector length - got:"+vector.length+", expected: "+this.numPars;
    				IJ.showMessage("Error",msg);
    				throw new IllegalArgumentException (msg);
    			}
	    		if (mask[this.thisParsStartIndex+ 0]) this.goniometerTilt=       vector[this.thisParsStartIndex+ 0];
	    		if (mask[this.thisParsStartIndex+ 1]) this.goniometerAxial=      vector[this.thisParsStartIndex+ 1];
	    		if (mask[this.thisParsStartIndex+ 2]) this.interAxisDistance=    vector[this.thisParsStartIndex+ 2];
	    		if (mask[this.thisParsStartIndex+ 3]) this.interAxisAngle=       vector[this.thisParsStartIndex+ 3];
	    		if (mask[this.thisParsStartIndex+ 4]) this.horAxisErrPhi=        vector[this.thisParsStartIndex+ 4];
	    		if (mask[this.thisParsStartIndex+ 5]) this.horAxisErrPsi=        vector[this.thisParsStartIndex+ 5];
	    		if (mask[this.thisParsStartIndex+ 6]) this.entrancePupilForward= vector[this.thisParsStartIndex+ 6];
	    		if (mask[this.thisParsStartIndex+ 7]) this.centerAboveHorizontal=vector[this.thisParsStartIndex+ 7];
485 486 487
	    		if (mask[this.thisParsStartIndex+ 8]) this.GXYZ[0]=              vector[this.thisParsStartIndex+ 8];
	    		if (mask[this.thisParsStartIndex+ 9]) this.GXYZ[1]=              vector[this.thisParsStartIndex+ 9];
	    		if (mask[this.thisParsStartIndex+10]) this.GXYZ[2]=              vector[this.thisParsStartIndex+10];
488 489 490 491 492 493 494 495 496 497
    		}

    		public double getSetWeight(){return this.setWeight;}
        	public int getStationNumber(){ // TODO: make only a single station number - in GridImageSet?
        		return this.stationNumber;
        	}
        	public void setStationNumber(int stationNumber){ // TODO: make only a single station number - in GridImageSet?
        		this.stationNumber=stationNumber;
        	}
    	}
498

499 500 501 502 503 504 505
    	public int index_right; // =     getParameterIndexByName("subcamRight");          // 0
    	public int index_forward; // =   getParameterIndexByName("subcamForward");        // 1
    	public int index_azimuth; // =   getParameterIndexByName("subcamAzimuth");        // 0
    	public int index_heading; // =   getParameterIndexByName("subcamHeading");        // 3
    	public int index_elevation; // = getParameterIndexByName("subcamElevation");      // 4
    	public int index_gh; //=         getParameterIndexByName("goniometerHorizontal"); // 6
    	public int index_ga; //=         getParameterIndexByName("goniometerAxial");      // 7
506

507 508 509 510 511
        public String [][] parameterDescriptionsCartesian ={ // may be shorter, have null rows, shorter rows - will use parameterDescriptions for missing data
        		{"subcamRight",          "Subcamera distance from the vertical rotation axis, positive - right looking to the target","mm","S","E"},                  // 0
        		{"subcamForward",        "Subcamera distance from the vertical rotation axis, positive - towards the target","mm","S","E"},                               // 1
        		null,                                                   // 2
        		{"subcamHeading",        "Optical axis heading (0 - to the target, positive - CW looking from top)","degrees","S","E"}};               // 3
512

513 514 515 516 517 518 519 520
        public String [][] parameterDescriptions ={
        		{"subcamAzimuth",        "Subcamera azimuth, clockwise looking from top","degrees","S","E"},                                    // 0
        		{"subcamDistance",       "Subcamera distance from the axis","mm","S","E"},                                                      // 1
        		{"subcamHeight",         "Subcamera height from the 'equator'","mm","S","E"},                                                   // 2
        		{"subcamHeading",        "Optical axis heading (relative to azimuth)","degrees","S","E"},                                       // 3
        		{"subcamElevation",      "Optical axis elevation (up from equator)","degrees","S","E"},                                         // 4
        		{"subcamRoll",           "Subcamera roll, positive CW looking to the target","degrees","S","E"},                                // 5
    			{"goniometerHorizontal", "Goniometer rotation around 'horizontal' axis (tilting from the target - positive)","degrees","R","E"},// 6
521
    			{"goniometerAxial",      "Rotation around Eyesis main axis (clockwise in plan - positive)","degrees","R","E"},                  // 7
522 523 524 525 526 527
    			{"interAxisDistance",    "Distance between goniometer axes","mm","C","E"},                                                      // 8
    			{"interAxisAngle",       "Angle error between goniometer axes (<0 if vertical axis rotated CW )","degrees","C","E"},            // 9
    			{"horAxisErrPhi",        "Horizontal axis azimuth error (CW in plan)","degrees","C","E"},                                       //10
    			{"horAxisErrPsi",        "Horizontal axis roll error (CW looking to target)","degrees","C","E"},                                //11
    			{"entrancePupilForward", "Distance from the sensor to the lens entrance pupil","mm","C","E"},                              //12
    			{"centerAboveHorizontal","CenterAboveHorizontal","mm","C","E"},                                                            //13
528
    			{"GXYZ0",                "Goniometer reference point position X (target coordinates, left)","mm","T","E"},                      //14 (12)
529 530 531 532 533 534 535 536 537 538 539
    			{"GXYZ1",                "Goniometer reference point position Y (target coordinates, up)","mm","T","E"},                        //15 (13)
    			{"GXYZ2",                "Goniometer reference point position Z (target coordinates, away)","mm","T","E"} ,                     //16 (14)
    			{"subcamFocalLength",    "Lens focal length","mm","S","I"},                                                                     //17 (15)
    			{"subcamPX0",            "Lens axis on the sensor (horizontal, from left edge)","pixels","S","I"},                              //18 (16)
    			{"subcamPY0",            "Lens axis on the sensor (vertical, from top edge)","pixels","S","I"},                                 //19 (17)
    			{"subcamDistortionA8",   "Distortion A8(r^5)","relative","S","I"},                                                              //20 (18)
    			{"subcamDistortionA7",   "Distortion A7(r^5)","relative","S","I"},                                                              //21 (19)
    			{"subcamDistortionA6",   "Distortion A6(r^5)","relative","S","I"},                                                              //22 (20)
    			{"subcamDistortionA5",   "Distortion A5(r^5)","relative","S","I"},                                                              //23 (21)
    			{"subcamDistortionA",    "Distortion A (r^4)","relative","S","I"},                                                              //24 (22)
    			{"subcamDistortionB",    "Distortion B (r^3)","relative","S","I"},                                                              //25 (23)
540
    			{"subcamDistortionC",    "Distortion C (r^2)","relative","S","I"},                                                               //26 (24)
541

542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
        		{"subcamElong_C_o",      "Orthogonal elongation for r^2","relative","S","I"},     // 27 39 (37)
        		{"subcamElong_C_d",      "Diagonal   elongation for r^2","relative","S","I"},     // 28 40 (38)

        		{"subcamEccen_B_x",      "Distortion center shift X for r^3","relative","S","I"}, // 29 27 (25)
        		{"subcamEccen_B_y",      "Distortion center shift Y for r^3","relative","S","I"}, // 30 28 (26)
        		{"subcamElong_B_o",      "Orthogonal elongation for r^3","relative","S","I"},     // 31 41 (39)
        		{"subcamElong_B_d",      "Diagonal   elongation for r^3","relative","S","I"},     // 32 42 (40)

        		{"subcamEccen_A_x",      "Distortion center shift X for r^4","relative","S","I"}, // 33 29 (27)
        		{"subcamEccen_A_y",      "Distortion center shift Y for r^4","relative","S","I"}, // 34 30 (28)
        		{"subcamElong_A_o",      "Orthogonal elongation for r^4","relative","S","I"},     // 35 43 (41)
        		{"subcamElong_A_d",      "Diagonal   elongation for r^4","relative","S","I"},     // 36 44 (42)

        		{"subcamEccen_A5_x",     "Distortion center shift X for r^5","relative","S","I"}, // 37 31 (29)
        		{"subcamEccen_A5_y",     "Distortion center shift Y for r^5","relative","S","I"}, // 38 32 (30)
        		{"subcamElong_A5_o",     "Orthogonal elongation for r^5","relative","S","I"},     // 39 45 (43)
        		{"subcamElong_A5_d",     "Diagonal   elongation for r^5","relative","S","I"},     // 40 46 (44)

        		{"subcamEccen_A6_x",     "Distortion center shift X for r^6","relative","S","I"}, // 41 33 (31)
        		{"subcamEccen_A6_y",     "Distortion center shift Y for r^6","relative","S","I"}, // 42 34 (32)
        		{"subcamElong_A6_o",     "Orthogonal elongation for r^6","relative","S","I"},     // 43 47 (45)
        		{"subcamElong_A6_d",     "Diagonal   elongation for r^6","relative","S","I"},     // 44 48 (46)

        		{"subcamEccen_A7_x",     "Distortion center shift X for r^7","relative","S","I"}, // 45 35 (33)
        		{"subcamEccen_A7_y",     "Distortion center shift Y for r^7","relative","S","I"}, // 46 36 (34)
        		{"subcamElong_A7_o",     "Orthogonal elongation for r^7","relative","S","I"},     // 47 49 (47)
        		{"subcamElong_A7_d",     "Diagonal   elongation for r^7","relative","S","I"},     // 48 50 (48)

        		{"subcamEccen_A8_x",     "Distortion center shift X for r^8","relative","S","I"}, // 49 37 (35)
        		{"subcamEccen_A8_y",     "Distortion center shift Y for r^8","relative","S","I"}, // 50 38 (36)
        		{"subcamElong_A8_o",     "Orthogonal elongation for r^8","relative","S","I"},     // 51 51 (49)
        		{"subcamElong_A8_d",     "Diagonal   elongation for r^8","relative","S","I"}      // 52 52 (50)
574
        };
575

576 577 578 579
        public String [] channelSuffixes={ // natural order (same as array indices, may be modified to camera/subcamera
        		"00","01","02","03","04","05","06","07","08","09",
        		"10","11","12","13","14","15","16","17","18","19",
        		"20","21","22","23","24","25","26","27","28","29"};
580

581 582 583 584 585

        public int getNumSets() {
        	return gIS.length;
        }

586 587 588 589 590 591 592 593
        public void setupIndices(){ // should be always called during initialization !
        	this.index_right =     getParameterIndexByName("subcamRight");          // 0 may be -1 if !cartesian
        	this.index_forward =   getParameterIndexByName("subcamForward");        // 1 may be -1 if !cartesian
        	this.index_azimuth =   getParameterIndexByName("subcamAzimuth");        // 0
        	this.index_heading =   getParameterIndexByName("subcamAzimuth");        // 3
        	this.index_elevation = getParameterIndexByName("subcamElevation");      // 4
        	this.index_gh=         getParameterIndexByName("goniometerHorizontal"); // 6
        	this.index_ga=         getParameterIndexByName("goniometerAxial");      // 7
594 595
        }

596 597 598
        public String descrField(int i,int j){
        	if (
        			(eyesisCameraParameters !=null) &&
599
        			eyesisCameraParameters.isCartesian() &&
600 601 602 603 604 605 606
        			(i < this.parameterDescriptionsCartesian.length) &&
        			(this.parameterDescriptionsCartesian[i]!=null) &&
        			(j<this.parameterDescriptionsCartesian[i].length)){
        		return this.parameterDescriptionsCartesian[i][j];
        	}
    		return this.parameterDescriptions[i][j];
        }
607

608 609 610
        public boolean isNonRadial(int index){
        	return parameterDescriptions[index][0].startsWith("subcamEccen_") || parameterDescriptions[index][0].startsWith("subcamElong_");
        }
611

612
        public int getParameterIndexByName(String name){
613 614 615 616 617
        	if (isCartesian()){
            	for (int i=0;i<this.parameterDescriptionsCartesian.length;i++) if ((this.parameterDescriptionsCartesian[i]!=null) && this.parameterDescriptionsCartesian[i][0].equals(name)){
            		return i;
            	}
        	}
618 619 620 621 622
        	for (int i=0;i<this.parameterDescriptions.length;i++) if (this.parameterDescriptions[i][0].equals(name)){
        		return i;
        	}
        	return -1;
        }
623

624 625 626
        public int getNumDescriptions(){
        	return this.parameterDescriptions.length;
        }
627 628

/**
629
 * Initialize data from scratch using filenames "grid-<timestamp-seconds>_<timestamp-microseconds>-<channel-number>.tiff
630 631 632 633 634 635
 * @param filenames List of grid filenames (2-slice TIFFs)
 */

        public DistortionCalibrationData (
        		String [] filenames,
        		PatternParameters patternParameters,
636
        		EyesisCameraParameters eyesisCameraParameters,
Andrey Filippov's avatar
Andrey Filippov committed
637 638
        		LaserPointer laserPointers,
        		Goniometer.GoniometerParameters goniometerParameters
639 640 641 642 643
        		) {
    	    String [][] stationFilenames={filenames};
        	setupDistortionCalibrationData(
        			stationFilenames,
            		patternParameters,
644
            		eyesisCameraParameters, // debugLevel
Andrey Filippov's avatar
Andrey Filippov committed
645 646
            		laserPointers,
            		goniometerParameters
647 648 649
            		);
        }
        public DistortionCalibrationData (
650 651
        		String []              filenames,
        		PatternParameters      patternParameters,
652
        		EyesisCameraParameters eyesisCameraParameters,
653
        		LaserPointer           laserPointers,
Andrey Filippov's avatar
Andrey Filippov committed
654
        		Goniometer.GoniometerParameters goniometerParameters,
655 656 657 658 659 660 661
        		int debugLevel
        		) {
        	    this.debugLevel=debugLevel;
        	    String [][] stationFilenames={filenames};
        	setupDistortionCalibrationData(
        			stationFilenames,
            		patternParameters,
662
            		eyesisCameraParameters, // debugLevel
Andrey Filippov's avatar
Andrey Filippov committed
663 664
            		laserPointers,
            		goniometerParameters
665 666 667 668 669 670 671
            		);
        }

        public DistortionCalibrationData (
        		String [][] stationFilenames,
        		PatternParameters patternParameters,
        		EyesisCameraParameters eyesisCameraParameters,
672
        		LaserPointer laserPointers,
Andrey Filippov's avatar
Andrey Filippov committed
673
        		Goniometer.GoniometerParameters goniometerParameters,
674 675 676 677 678 679
        		int debugLevel
        		) {
        	    this.debugLevel=debugLevel;
        	setupDistortionCalibrationData(
        			stationFilenames,
            		patternParameters,
680
            		eyesisCameraParameters, // debugLevel
Andrey Filippov's avatar
Andrey Filippov committed
681 682
            		laserPointers,
            		goniometerParameters
683 684 685
            		);
        }

686
        public DistortionCalibrationData (
687
        		String [][] stationFilenames,
688
        		String []                                              source_dirs,      // directories of the source files per station
689 690
        		MultipleExtensionsFileFilter gridFilter,
        		MultipleExtensionsFileFilter sourceFilter,
691 692 693
        		PatternParameters                                      patternParameters,
        		EyesisCameraParameters                                 eyesisCameraParameters,
        		LaserPointer                                           laserPointers, // as a backup if data is not available in the file
Andrey Filippov's avatar
Andrey Filippov committed
694
        		Goniometer.GoniometerParameters                        goniometerParameters,
695 696 697 698 699 700 701 702 703 704 705 706
        		boolean                                                read_grids,
        		int                                                    debugLevel
        		) {
        	    this.debugLevel=debugLevel;
        	    setupDirDistortionCalibrationData(
        			stationFilenames,
            		source_dirs,      // directories of the source files per station
        			gridFilter,
        			sourceFilter,
            		patternParameters,
            		eyesisCameraParameters, // debugLevel
            		laserPointers, // as a backup if data is not available in the file
Andrey Filippov's avatar
Andrey Filippov committed
707
            		goniometerParameters,
708 709 710 711 712 713 714 715 716
            		read_grids
            		);
        }


        public void setupDistortionCalibrationData (
        		String [][]            stationFilenames,
        		PatternParameters      patternParameters,
        		EyesisCameraParameters eyesisCameraParameters,
Andrey Filippov's avatar
Andrey Filippov committed
717 718
        		LaserPointer           laserPointers, // as a backup if data is not available in the file
        		Goniometer.GoniometerParameters goniometerParameters
719
        		) {
Andrey Filippov's avatar
Andrey Filippov committed
720
        	this.goniometerParameters = goniometerParameters;
721
        	setupIndices();
722 723 724 725 726 727 728 729
        	this.eyesisCameraParameters=eyesisCameraParameters;
        	int numSubCameras=(eyesisCameraParameters==null)?1:eyesisCameraParameters.eyesisSubCameras[0].length;
        	this.numSubCameras=numSubCameras;
        	this.eyesisCameraParameters.numStations=stationFilenames.length;
        	int numFiles=0;
        	for (int i=0;i<stationFilenames.length;i++) numFiles+=stationFilenames[i].length;
        	this.gIP=new GridImageParameters[numFiles];

730

731 732 733
        	int numFile=0;
        	for (int numStation=0;numStation<stationFilenames.length;numStation++){
        		for (int index=0;index<stationFilenames[numStation].length;index++){
734

735 736 737 738 739 740 741
        			System.out.println(numFile+" ("+numStation+":"+index+"): "+stationFilenames[numStation][index]);
        			this.gIP[numFile]=new GridImageParameters(numFile);
        			this.gIP[numFile].path=stationFilenames[numStation][index]; //Exception in thread "Run$_AWT-EventQueue-0" java.lang.NullPointerException at Distortions$DistortionCalibrationData.<init>(Distortions.java:5987)
        			this.gIP[numFile].setStationNumber(numStation);
        			int i1=stationFilenames[numStation][index].indexOf('-',stationFilenames[numStation][index].lastIndexOf(Prefs.getFileSeparator()));
        			int i2=stationFilenames[numStation][index].indexOf('-',i1+1);
        			int i3=stationFilenames[numStation][index].indexOf('.',i2+1);
742
        			// Extract timestamp from the filename
743 744 745 746 747
        			if ((i1<0) || (i2<0)) {
        				String msg="invalid file format - '"+stationFilenames[numStation][index]+"', should be '<timestamp-seconds>_<timestamp-microseconds>-<channel-number>.tiff'";
        				IJ.showMessage("Error",msg);
        				throw new IllegalArgumentException (msg);
        			}
748
        			// Extract channel number from the filename
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769

        			this.gIP[numFile].timestamp=Double.parseDouble(stationFilenames[numStation][index].substring(i1+1,i2).replace('_','.'));
        			String channelSuffix=stationFilenames[numStation][index].substring(i2+1,i3);
        			this.gIP[numFile].channel=-1;
        			for (int j=0;j<this.channelSuffixes.length;j++) if (channelSuffix.equals(this.channelSuffixes[j])) {
        				this.gIP[numFile].channel=j;
        				break;
        			}
        			if (this.gIP[numFile].channel<0) {
        				String msg="invalid file format (channel suffix not recognized) - '"+stationFilenames[numStation][index]+"', should be '<timestamp-seconds>_<timestamp-microseconds>-<channel-number>.tiff'";
        				msg+="\nThis channel suffix is "+channelSuffix+", available channel suffixes are:\n";
        				for (int j=0;j<this.channelSuffixes.length;j++) msg+=this.channelSuffixes[j]+", ";
        				IJ.showMessage("Error",msg);
        				throw new IllegalArgumentException (msg);
        			}
        			numFile++;
        		}
        	}
// Create parameters array
        	initPars (this.gIP.length,parameterDescriptions.length);
        	if (this.debugLevel>1) System.out.println("setupDistortionCalibrationData(): Resetting this.gIS");
770
        	this.gIS=null; // so it will be initialized in readAllGrids()
771 772
        	readAllGrids(
        			patternParameters,
773 774 775
        			laserPointers, // prepare grid parameters for LMA
            		true); // boolean keep_images make it configurable parameter?

776
        	// no orientation
777 778 779 780 781 782
        }

// from data organized as image sets
        public void setupDirDistortionCalibrationData (
        		String [][]                                            stationFilenames, // per-station List of image set directories
        		String []                                              source_dirs,      // directories of the source files per station
783 784
        		MultipleExtensionsFileFilter gridFilter,
        		MultipleExtensionsFileFilter sourceFilter,
785 786 787
        		PatternParameters                                      patternParameters,
        		EyesisCameraParameters                                 eyesisCameraParameters,
        		LaserPointer                                           laserPointers, // as a backup if data is not available in the file
Andrey Filippov's avatar
Andrey Filippov committed
788
        		Goniometer.GoniometerParameters goniometerParameters,
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
        		boolean                                                read_grids
        		) {
        	class DirTs{
        		int       station;
        		String [] paths;
        		String [] spaths; // can be null
//        		String    dir;
        		double    ts;
        		int getStation() {return station;}
//        		String getDir() {return dir;}
        		double getTs() {return ts;}
        		String [] getPaths() {return paths;}
        		String [] getSourcePaths() {return spaths;} // may not be null
        		DirTs(int station,
        			  String dir,  // grid image set directory that contains channel files (may be different timestamps)
        			  String sdir, // source super directory that contains image set directories with files
        			  int num_chn,
806 807
        			  MultipleExtensionsFileFilter gridFilter,
        			  MultipleExtensionsFileFilter sourceFilter)
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831
        		{
        			this.station = station;
//        			this.dir = dir;
        			int dot_index = dir.lastIndexOf("_");
        			String digits = "0123456789";
        			int ts_start = dot_index -1;
        			while ((ts_start >=0) && (digits.indexOf(dir.charAt(ts_start)) >= 0)) ts_start--;
        			this.ts = Double.parseDouble(dir.substring(ts_start + 1).replace('_','.'));
        			String [] files = (new File(dir)).list(gridFilter); // are these full files?
        			paths = new String[num_chn];
        			for (String path:files) {
        				int last_dash = path.lastIndexOf('-');
        				int last =      path.lastIndexOf('_');
        				if (last_dash >last) last = last_dash;
        				int last_dot = path.lastIndexOf('.');
        				if (last_dot < 0) {
        					last_dot = path.length();
        				}
        				int chn = Integer.parseInt(path.substring(last+1, last_dot));
        				paths[chn] = (new File(dir,path)).getPath();
        				//grid-elphelimg_1559195695_507621_4.tiff
        			}
    				spaths = new String[num_chn];
        			if (sdir != null) {
832
        				// construct source image set directory name
833 834 835
        				String set_name = (new File(dir)).getName();
        				File set_dir = new File(sdir, set_name );
        				String [] sfiles = set_dir.list(sourceFilter);
836 837 838
        				if (sfiles == null) {
        					System.out.println("sfiles == null");
        				}
839 840 841 842 843 844 845 846 847 848 849 850 851 852
        				for (String spath:sfiles) {
        					int last_dash = spath.lastIndexOf('-');
        					int last =      spath.lastIndexOf('_');
        					if (last_dash >last) last = last_dash;
        					int last_dot = spath.lastIndexOf('.');
        					if (last_dot < 0) {
        						last_dot = spath.length();
        					}
        					int chn = Integer.parseInt(spath.substring(last+1, last_dot));
        					spaths[chn] = (new File(set_dir,spath)).getPath();
        				}
        			}
        		}
        	}
Andrey Filippov's avatar
Andrey Filippov committed
853
    		this.goniometerParameters =  goniometerParameters;
854 855
        	boolean ignore_LWIR_pointers = true; // skip LWIR absolute marks, use them later
        	int max_lwir_width = 1023;  // use LWIR class
856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935
        	setupIndices();
        	this.eyesisCameraParameters=eyesisCameraParameters;
        	int numSubCameras=(eyesisCameraParameters==null)?1:eyesisCameraParameters.eyesisSubCameras[0].length;
        	this.numSubCameras=numSubCameras;
        	this.eyesisCameraParameters.numStations=stationFilenames.length;
//        	int numFiles=0;

//        	DirTs [][] dirTs = new DirTs [stationFilenames.length][];
    		ArrayList<DirTs> dirTsList = new ArrayList<DirTs>();
        	for (int numStation=0;numStation<stationFilenames.length;numStation++){
        		for (int is = 0; is < stationFilenames[numStation].length; is++) {
        			dirTsList.add(new DirTs(
        					numStation,
        					stationFilenames[numStation][is], // 	String dir,
        					source_dirs[numStation],
        					numSubCameras, // int num_chn,
        					gridFilter,
        					sourceFilter));
        		}
        	}
    		// sort list
    		Collections.sort(dirTsList, new Comparator<DirTs>() {
    		    @Override
    		    public int compare(DirTs lhs, DirTs rhs) {
    		        return rhs.ts > lhs.ts ? -1 : (rhs.ts < lhs.ts) ? 1 : 0;
    		    }
    		});
    		int numFiles = 0;
    		for (DirTs dt:dirTsList) {
    			String [] paths = dt.getPaths();
    			for (String p:paths) if (p!=null) numFiles++;
    		}
    		this.gIS=new GridImageSet[dirTsList.size()];
        	this.gIP=new GridImageParameters[numFiles];
        	int numFile=0;
    		Opener opener=new Opener();
    		JP46_Reader_camera jp4_reader= new JP46_Reader_camera(false);
    		ImagePlus imp_grid=null;
    		ImageStack stack;
        	boolean disableNoFlatfield=false;  // true only for processing transitional images - mixture of ff/ no-ff
    		int numOfGridNodes=        0;
    		int numOfGridNodes_extra=  0;
        	for (int nis = 0; nis<this.gIS.length; nis++) {
        		DirTs dt = dirTsList.get(nis);
        		this.gIS[nis]=new GridImageSet();
        		this.gIS[nis].timeStamp= dt.getTs();
        		this.gIS[nis].imageSet=new GridImageParameters [numSubCameras];
    			this.gIS[nis].setStationNumber(dt.getStation());
    			float [][][] set_pixels = new float [numSubCameras][][];
    			int []       set_widths = new int [numSubCameras];
        		String [] paths =  dt.getPaths();
        		String [] spaths = dt.getSourcePaths();
        		int with_pointers = -1;
        		for (int nc = 0; nc < paths.length; nc++) {
        			String p = paths[nc];
        			boolean first_in_set = true;
        			if (p != null) {
        				this.gIP[numFile] =              new GridImageParameters(numFile);
        				this.gIP[numFile].path =         p;
        				this.gIP[numFile].source_path =  spaths[nc];
        				this.gIP[numFile].setStationNumber(dt.getStation());
        				this.gIP[numFile].timestamp =    dt.getTs();
        				this.gIP[numFile].channel =      nc;
        				this.gIP[numFile].setNumber =    nis;
        				this.gIP[numFile].gridImageSet = this.gIS[nis];
            			this.gIS[nis].imageSet[nc]=this.gIP[numFile];

        				//numFile
        				if (first_in_set || read_grids) {
        					if (read_grids) {
        						if (this.updateStatus) IJ.showStatus("Reading grid file "+(numFile+1)+" (of "+(numFiles)+"): "+this.gIP[numFile].path);
        						if (this.debugLevel>-1) System.out.print(numFile+" ("+this.gIP[numFile].getStationNumber()+
        								":"+this.gIP[numFile].setNumber+":"+this.gIP[numFile].channel+"): "+this.gIP[numFile].path);
        					}
                			imp_grid=opener.openImage("", this.gIP[numFile].path);  // or (path+filenames[nFile])
                			if (imp_grid==null) {
                				String msg="Failed to read grid file "+this.gIP[numFile].path;
                				IJ.showMessage("Error",msg);
                				throw new IllegalArgumentException (msg);
                			}
936
                	        // TODO: here - need to decode properties
937
                			jp4_reader.decodeProperiesFromInfo(imp_grid);
938 939 940 941 942
                			this.gIP[numFile].woi = new Rectangle(
                					getImagePlusProperty(imp_grid,"WOI_LEFT",0),
                					getImagePlusProperty(imp_grid,"WOI_TOP",0),
                					getImagePlusProperty(imp_grid,"WOI_WIDTH",  eyesisCameraParameters.getSensorWidth(nc)),
                					getImagePlusProperty(imp_grid,"WOI_HEIGHT", eyesisCameraParameters.getSensorHeight(nc)));
943 944
//                			boolean woi_compensated = getImagePlusProperty(imp_grid,"WOI_COMPENSATED",false);
                			boolean woi_compensated = (this.gIP[numFile].woi.x == 0) && (this.gIP[numFile].woi.y == 0);
945

946 947
                			this.gIP[numFile].gridImage = imp_grid; // Save all images?
                    		this.gIP[numFile].laserPixelCoordinates = MatchSimulatedPattern.getPointersXYUV(imp_grid, laserPointers);
948 949 950 951
                    		this.gIP[numFile].motors =                getMotorPositions(imp_grid, this.numMotors);
                    		this.gIS[nis].motors=                     this.gIP[numFile].motors.clone();
                    		this.gIP[numFile].matchedPointers =       getUsedPonters(imp_grid);
                    		if (this.gIP[numFile].matchedPointers > 0) {
952 953 954 955
                    			// Not using LWIR pointers here!
                    			if (!ignore_LWIR_pointers || (getImagePlusProperty(imp_grid,"WOI_TOP",0) > max_lwir_width)) {
                    				with_pointers = numFile;
                    			}
956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
                    		}
                    		double [] saturations=new double [4];
                    		for (int i=0;i<saturations.length;i++) {
                    			saturations[i]=Double.NaN;
                    			if (imp_grid.getProperty("saturation_" + i) !=null) saturations[i]=Double.parseDouble((String) imp_grid.getProperty("saturation_" + i));
                    		}
                    		if (!Double.isNaN(saturations[1])) this.gIP[numFile].saturation[0]=saturations[1];
                    		if (!Double.isNaN(saturations[2])) this.gIP[numFile].saturation[2]=saturations[2];
                    		if (!Double.isNaN(saturations[0]) && !Double.isNaN(saturations[3])) {
                    			this.gIP[numFile].saturation[1]=0.5*(saturations[0]+saturations[3]);
                    		} else {
                        		if (!Double.isNaN(saturations[0])) this.gIP[numFile].saturation[1]=saturations[0];
                        		if (!Double.isNaN(saturations[3])) this.gIP[numFile].saturation[1]=saturations[3];
                    		}
                    		if (read_grids) {
                    			stack=imp_grid.getStack();
                    			if ((stack==null) || (stack.getSize()<4)) {
                    				String msg="Expected a 8-slice stack in "+this.gIP[numFile].path;
                    				IJ.showMessage("Error",msg);
                    				throw new IllegalArgumentException (msg);
                    			}
                    			float [][] pixels=new float[stack.getSize()][]; // now - 8 (x,y,u,v,contrast, vignR,vignG,vignB
978 979 980 981 982 983 984 985 986 987 988 989 990
                    			for (int i=0;i<pixels.length;i++) pixels[i]= (float[]) stack.getPixels(i+1); // pixel X : negative - no grid here
                    			if (!woi_compensated) {
                    				System.out.print(" woi_compensation-a "+numFile+" ");
                    				for (int i = 0; i < pixels[0].length; i++) {
                    					pixels[0][i] += this.gIP[numFile].woi.x;
                    					pixels[1][i] += this.gIP[numFile].woi.y;
                    				}
                    				this.gIP[numFile].woi.width += this.gIP[numFile].woi.x;
                    				this.gIP[numFile].woi.x = 0;
                    				this.gIP[numFile].woi.height += this.gIP[numFile].woi.y;
                    				this.gIP[numFile].woi.y = 0;
                    				woi_compensated = true;
                    			}
991 992
                    			set_pixels[nc] = pixels;
                    			set_widths[nc] = imp_grid.getWidth();
993 994


995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
                    			int numBadNodes = 0;
                    			if (this.eyesisCameraParameters.badNodeThreshold>0.0){
                    				boolean thisDebug =false;
                    				//                            		thisDebug|=        (fileNumber== 720); // chn 25
                    				numBadNodes=fixBadGridNodes(
                    						pixels,
                    						stack.getWidth(),
                    						this.eyesisCameraParameters.badNodeThreshold,
                    						this.eyesisCameraParameters.maxBadNeighb,
                    						this.debugLevel+(thisDebug?3:0),
                    						thisDebug?("fixBad-"+numFile):null
                    						);
                    			}
                    			this.gIP[numFile].flatFieldAvailable=pixels.length>=8;
                            	if (disableNoFlatfield && !this.gIP[numFile].flatFieldAvailable) this.gIP[numFile].enabled=false; // just to use old mixed data

                            	int [][] shiftRotMatrix= MatchSimulatedPattern.getRemapMatrix(this.gIP[numFile].getUVShiftRot());
                            	int [] sizeSizeExtra=setGridsWithRemap(
                            			numFile,
                                		shiftRotMatrix, // int [][] reMap,
                                		pixels,
                                		patternParameters);
                            	numOfGridNodes+=sizeSizeExtra[0];
                            	numOfGridNodes_extra+=sizeSizeExtra[1];

                				if (this.debugLevel>-1) {
                					if (this.gIP[numFile].pixelsUV != null) {
1022
                						System.out.print(" ["+ this.gIP[numFile].pixelsUV.length+"+"+this.gIP[numFile].pixelsUV_extra.length+"]");
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
                					} else {
                						System.out.print(" [null]");
                					}
                					if (numBadNodes>0)
                						System.out.print("  -- replaced "+numBadNodes+" bad grid nodes");
                					int [] uvrot=this.gIP[numFile].getUVShiftRot();
                					System.out.println(" shift:rot="+uvrot[0]+"/"+uvrot[1]+":"+uvrot[2]+
                							" enabled="+this.gIP[numFile].enabled+" hintedMatch="+this.gIP[numFile].hintedMatch);

                				}
1033
                            	calcGridPeriod(numFile, true); // may be not absolutely calibrated, use_extra out-of-pattern nodes will be used to filter out reflections
1034 1035 1036 1037 1038 1039 1040 1041 1042
                //System.out.println ("pixelsXY["+fileNumber+"]length="+pixelsXY[fileNumber].length);
                    		} //if (read_grids)
                    		// not reading the grid itself
                    		first_in_set = false;
        				}
        				numFile++;

        			}
        		}
1043
        		if (with_pointers < 0) { // no matching pointers, will try to match selected channel with the pattern
1044 1045
        			int main_channel = 4; // one of the EO channels to match with the pattern
//        			boolean [] sensor_mask = null; // later may be used to limit scope to EO-only
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
        			int extra_search = 2;
//        			int base_channel = this.gIP[with_pointers].channel;
        			if (this.gIS[nis].imageSet[main_channel] != null) {
        				int imgNum =  this.gIS[nis].imageSet[main_channel].imgNumber;
        				boolean invert_color = (main_channel & 4) == 0; // first 4 - LWIR

						if (this.updateStatus) IJ.showStatus("Matching with the pattern, grid file "+(imgNum+1)+" (of "+(numFiles)+"): "+this.gIP[imgNum].path);
						if (this.debugLevel>-1) System.out.print(imgNum+">("+this.gIP[imgNum].getStationNumber()+
								":"+this.gIP[imgNum].setNumber+":"+this.gIP[imgNum].channel+"): "+this.gIP[imgNum].path);

    					double [] sensor_wh = {
    							this.gIP[imgNum].woi.width +  this.gIP[imgNum].woi.x,
    							this.gIP[imgNum].woi.height + this.gIP[imgNum].woi.y};

        				int [] uv_shift_rot = correlateWithPattern(
        		        		patternParameters,
    							set_widths[main_channel], // 		int        test_width,
    							set_pixels[main_channel], //		float [][] test_pixels,
        		        		invert_color,
        		        		extra_search,
        		        		5.0, // double     sigma,
        		        		sensor_wh, // test set pixels width/height pair to reduce weight near the margins (or null)
        		        		false // true // boolean    bdebug
        		        		);
    					System.out.print(" {"+uv_shift_rot[0]+":"+uv_shift_rot[1]+"]");

    					this.gIS[nis].imageSet[main_channel].setUVShiftRot(uv_shift_rot);

                    	int [][] shiftRotMatrix= MatchSimulatedPattern.getRemapMatrix(this.gIS[nis].imageSet[main_channel].getUVShiftRot());
                    	setGridsWithRemap( // null immediately
                    			imgNum,
                    			shiftRotMatrix, // int [][] reMap,
                    			set_pixels[main_channel],
                    			patternParameters);
                    	calcGridPeriod(imgNum, false);  // centered, can skip _extra

    					if (this.gIP[imgNum].pixelsUV != null) {
    						System.out.println(" ["+ this.gIP[imgNum].pixelsUV.length+"+"+this.gIP[imgNum].pixelsUV_extra.length+"]");
    					} else {
    						System.out.println(" [null]");
    					}

    					with_pointers = imgNum; // no adjust all other channels by this one
        			}
        		}

1092
        		if (with_pointers >= 0) { // set initial grids offset from the grid files in the same image set that do not have absolute calibration
1093
        			boolean [] sensor_mask = null; // later may be used to limit scope to EO-only
1094 1095 1096 1097
        			int extra_search = 1;
        			int base_channel = this.gIP[with_pointers].channel;
        			for (int nc = 0; nc < this.gIS[nis].imageSet.length; nc++) if ((sensor_mask == null) || sensor_mask[nc]) {
        				boolean invert_color = ((base_channel ^ nc) & 4) != 0;
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
        				if ((this.gIS[nis].imageSet[nc].matchedPointers <= 0) && (nc != base_channel)) { // Later add non-laser conditions
        					int imgNum =  this.gIS[nis].imageSet[nc].imgNumber; // with_pointers - base_channel + nc;
    						if (this.updateStatus) IJ.showStatus("Re-reading grid file "+(imgNum+1)+" (of "+(numFiles)+"): "+this.gIP[imgNum].path);
    						if (this.debugLevel>-1) System.out.print(imgNum+"*("+this.gIP[imgNum].getStationNumber()+
    								":"+this.gIP[imgNum].setNumber+":"+this.gIP[imgNum].channel+"): "+this.gIP[imgNum].path);

        					double [] sensor_wh = {
        							this.gIP[imgNum].woi.width +  this.gIP[imgNum].woi.x,
        							this.gIP[imgNum].woi.height + this.gIP[imgNum].woi.y};

        					int [] uv_shift_rot = correlateGrids(
        							set_widths[base_channel], // int        base_width,
        							set_pixels[base_channel], //		float [][] base_pixels,
        							set_widths[nc], // 		int        test_width,
        							set_pixels[nc], //		float [][] test_pixels,
        							invert_color,
        							extra_search,
        							5.0, // 2.0, // sigma
        							sensor_wh,
        							false); // true);
1118
        					System.out.print(" {"+uv_shift_rot[0]+":"+uv_shift_rot[1]+"->");
1119 1120 1121 1122 1123 1124
                        	int [] combinedUVShiftRot=MatchSimulatedPattern.combineUVShiftRot(
                        			this.gIS[nis].imageSet[base_channel].getUVShiftRot(),
                        			uv_shift_rot);

        					this.gIS[nis].imageSet[nc].setUVShiftRot(combinedUVShiftRot); // uv_shift_rot);
        					System.out.print(combinedUVShiftRot[0]+":"+combinedUVShiftRot[1]+"}");
1125 1126 1127 1128 1129 1130 1131

                        	int [][] shiftRotMatrix= MatchSimulatedPattern.getRemapMatrix(this.gIS[nis].imageSet[nc].getUVShiftRot());
                        	setGridsWithRemap( // null immediately
                        			imgNum,
                        			shiftRotMatrix, // int [][] reMap,
                        			set_pixels[nc],
                        			patternParameters);
1132 1133 1134 1135 1136 1137
                        	calcGridPeriod(imgNum, false); // centered, can skip _extra
        					if (this.gIP[imgNum].pixelsUV != null) {
        						System.out.println(" ["+ this.gIP[imgNum].pixelsUV.length+"+"+this.gIP[imgNum].pixelsUV_extra.length+"]");
        					} else {
        						System.out.println(" [null]");
        					}
1138 1139
                		}
        			}
1140
        		} else {
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176

        		}
        	} // for (int nis = 0; nis<this.gIS.length; nis++)


        	initPars (this.gIP.length,parameterDescriptions.length);
        	// readAllGrids(patternParameters); // prepare grid parameters for LMA

        	// Create parameters array
        	///        	initPars (this.gIP.length,parameterDescriptions.length);
        	///        	if (this.debugLevel>1) System.out.println("setupDistortionCalibrationData(): Resetting this.gIS");
        	///        	this.gIS=null; // so it will be initialized in readAllGrids()
        	///        	readAllGrids(patternParameters); // prepare grid parameters for LMA
        	// no orientation
        	if (read_grids) {
        		if (this.debugLevel>3) {
        			System.out.println("setupDirDistortionCalibrationData(), numFiles="+numFiles);
        			for (int n=0;n<this.gIP.length;n++) {
        				System.out.println(n+": length="+this.gIP[n].pixelsXY.length);
        				System.out.println("pixelsUV[][][0]/pixelsUV[][][1] pixelsXY[][][0]/pixelsXY[][][1]");
        				for (int i=0;i<this.gIP[n].pixelsXY.length;i++){
        					System.out.println(n+":"+i+"  "+
        							this.gIP[n].pixelsUV[i][0]+"/"+
        							this.gIP[n].pixelsUV[1][1]+"  "+
        							IJ.d2s(this.gIP[n].pixelsXY[i][0], 2)+"/"+
        							IJ.d2s(this.gIP[n].pixelsXY[i][1], 2)
        							);
        				}
        			}
        		}
        		if (this.debugLevel>0) {
        			System.out.println("setupDirDistortionCalibrationData(), numFiles="+numFiles+", total number of grid nodes="+numOfGridNodes+", unused nodes "+numOfGridNodes_extra);
        		}
        		/// buildImageSets(this.gIS!=null); // already done
        		 // probably - do not need to verify that this.gIS is null - should do that anyway. UPDATE: no, now reading config file creates gIS
        	}
1177

1178
        }
1179

1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
        public boolean initialSetLwirFromEO( //
        		int               num_set,
        		boolean           invert_unmarked_grid,
        		int               extra_search,         // 2
        		double            sigma,                 //5.0
        		PatternParameters patternParameters,
        		boolean           bdebug
        		) {
        	// see if there is any LWIR in the system is sensor and throw if there is none
        	if (!hasSmallSensors()) {
        		String msg="This system does not have any LWIR or other dependent sub-cameras";
        		IJ.showMessage("Error",msg);
        		throw new IllegalArgumentException (msg);
        	}
        	if ((this.gIS[num_set] == null) || (this.gIS[num_set].imageSet == null)) {
        		return false;
        	}
        	// See if any of the LWIR subcameras has a mark (absolute grid) in this set
        	int lwir_mark = -1;
        	for (int ns = 0; ns <  this.gIS[num_set].imageSet.length; ns++) {
        		if (this.gIS[num_set].imageSet[ns]!=null) {
        			int imgNum = this.gIS[num_set].imageSet[ns].imgNumber;
        			if (isSmallSensor(imgNum) && (this.gIP[imgNum].matchedPointers > 0)) {
        				lwir_mark = ns;
        				break;
        			}
        		}
        	}
        	int master_sub = getEo0(); //  lowest number EO channel, use as a reference
        	if (lwir_mark >=0) { // some LWIR grid already has mark
        		master_sub = lwir_mark;
        		if (bdebug) {
        			System.out.println ("Aligning to marked LWIR grid image rather than to EO one");
        		}
//        		return false;
        	}
        	if (this.gIS[num_set].imageSet[master_sub] == null) {
        		return false; // master EO is not available // may try to search other channels
        	}
        	int imgMaster = this.gIS[num_set].imageSet[master_sub].imgNumber;
        	// get EO image and pixels
        	ImagePlus imp_master_grid = null;
    		if (this.gIP[imgMaster].gridImage!=null){ // use in-memory grid images instead of the files
    			imp_master_grid=this.gIP[imgMaster].gridImage;
    		} else if (this.gIP[imgMaster].path != null) {
    			imp_master_grid=(new Opener()).openImage("", this.gIP[imgMaster].path);
    			if (imp_master_grid==null) {
    				String msg="Failed to read grid file "+this.gIP[imgMaster].path;
    				IJ.showMessage("Error",msg);
    				throw new IllegalArgumentException (msg);
    			}
    			(new JP46_Reader_camera()).decodeProperiesFromInfo(imp_master_grid);
    		} else {
    			System.out.println("EO grid is not in memory, file path is not specified");
    			return false;
    		}
			ImageStack stack_master=imp_master_grid.getStack();
			if ((stack_master==null) || (stack_master.getSize()<4)) {
				String msg="Expected a 8-slice stack";
				IJ.showMessage("Error",msg);
				throw new IllegalArgumentException (msg);
			}
1242 1243 1244 1245 1246
//			boolean woi_compensated_master= getImagePlusProperty(imp_master_grid,"WOI_COMPENSATED",false);
			boolean woi_compensated_master = (getImagePlusProperty(imp_master_grid,"WOI_TOP",0) == 0) &&
					(getImagePlusProperty(imp_master_grid,"WOI_LEFT",0) == 0);


1247 1248
			float [][] pixels_master =new float[stack_master.getSize()][]; // now - 8 (x,y,u,v,contrast, vignR,vignG,vignB
        	for (int i=0;i<pixels_master.length;i++) pixels_master[i]= (float[]) stack_master.getPixels(i+1); // pixel X : negative - no grid here
1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
			if (!woi_compensated_master) {
				for (int i = 0; i < pixels_master[0].length; i++) {
					pixels_master[0][i] += this.gIP[imgMaster].woi.x;
					pixels_master[1][i] += this.gIP[imgMaster].woi.y;
				}
				this.gIP[imgMaster].woi.width += this.gIP[imgMaster].woi.x;
				this.gIP[imgMaster].woi.x = 0;
				this.gIP[imgMaster].woi.height += this.gIP[imgMaster].woi.y;
				this.gIP[imgMaster].woi.y = 0;
				woi_compensated_master = true;
			}

1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301
        	int width_master = imp_master_grid.getWidth();
        	// If master is LWIR - reset it's shift to zero
        	if (lwir_mark >=0) {
    			// reset master sub UV shift
    			int [] zero_uvr = {0,0,0};
	            this.gIS[num_set].imageSet[master_sub].setUVShiftRot(zero_uvr); // uv_shift_rot);
	            int [][] shiftRotMatrix= MatchSimulatedPattern.getRemapMatrix(this.gIS[num_set].imageSet[master_sub].getUVShiftRot());
	            setGridsWithRemap( // null immediately
	            		imgMaster,
	                    shiftRotMatrix, // int [][] reMap,
	                    pixels_master,
	                    patternParameters);
        	}


        	for (int ns = 0; ns <  this.gIS[num_set].imageSet.length; ns++) {
        		if ((this.gIS[num_set].imageSet[ns]!=null) &&(ns != lwir_mark)) {
        			int imgNum = this.gIS[num_set].imageSet[ns].imgNumber;
        			if (isSmallSensor(imgNum)) { // repeat for all target grid images in the image set
        	        	// get target EO image and pixels
        	        	ImagePlus imp_grid = null;
        	    		if (this.gIP[imgNum].gridImage!=null){ // use in-memory grid images instead of the files
        	    			imp_grid=this.gIP[imgNum].gridImage;
        	    		} else if (this.gIP[imgNum].path != null) {
        	    			imp_grid=(new Opener()).openImage("", this.gIP[imgNum].path);
        	    			if (imp_grid==null) {
        	    				String msg="Failed to read grid file "+this.gIP[imgNum].path;
        	    				IJ.showMessage("Error",msg);
        	    				throw new IllegalArgumentException (msg);
        	    			}
        	    			(new JP46_Reader_camera()).decodeProperiesFromInfo(imp_grid);
        	    		} else {
        	    			System.out.println("EO grid is not in memory, file path is not specified");
        	    			return false;
        	    		}
        				ImageStack stack = imp_grid.getStack();
        				if ((stack==null) || (stack.getSize()<4)) {
        					String msg="Expected a 8-slice stack";
        					IJ.showMessage("Error",msg);
        					throw new IllegalArgumentException (msg);
        				}
1302 1303 1304
//        				boolean woi_compensated = getImagePlusProperty(imp_grid,"WOI_COMPENSATED",false);
            			boolean woi_compensated = (this.gIP[imgNum].woi.x == 0) && (this.gIP[imgNum].woi.y == 0);

1305 1306
        				float [][] pixels =new float[stack.getSize()][]; // now - 8 (x,y,u,v,contrast, vignR,vignG,vignB
        	        	for (int i=0;i<pixels.length;i++) pixels[i]= (float[]) stack.getPixels(i+1); // pixel X : negative - no grid here
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
            			if (!woi_compensated) {
            				System.out.print(" woi_compensation-b "+imgNum+" ");
            				for (int i = 0; i < pixels[0].length; i++) {
            					pixels[0][i] += this.gIP[imgNum].woi.x;
            					pixels[1][i] += this.gIP[imgNum].woi.y;
            				}
            				this.gIP[imgNum].woi.width += this.gIP[imgNum].woi.x;
            				this.gIP[imgNum].woi.x = 0;
            				this.gIP[imgNum].woi.height += this.gIP[imgNum].woi.y;
            				this.gIP[imgNum].woi.y = 0;
            				woi_compensated = true;
            			}

1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
        	        	int width_lwir = imp_grid.getWidth();
    					double [] sensor_wh = {
    							this.gIP[imgNum].woi.width +  this.gIP[imgNum].woi.x,
    							this.gIP[imgNum].woi.height + this.gIP[imgNum].woi.y};
        	            int [] uv_shift_rot = correlateGrids(
        	                    width_master, // int        base_width,
        	                    pixels_master, //		float [][] base_pixels,
        	                    width_lwir, // 		int        test_width,
        	                    pixels, //		float [][] test_pixels,
        	                    invert_unmarked_grid,
        	                    extra_search,
        	                    sigma,
        	                    sensor_wh,
        	                    false); // true); //bdebug

        	            // combined rotation - first this, next what is applied to EO channel

        	            int [] combinedUVShiftRot=MatchSimulatedPattern.combineUVShiftRot(
        	            		uv_shift_rot,
        	                    this.gIS[num_set].imageSet[master_sub].getUVShiftRot()
        	                    );
        	            if (bdebug) {
        	            	System.out.print(imgNum+": calculated uv_shift_rot= ["+uv_shift_rot[0]+":"+uv_shift_rot[1]+"], ");
        	            	System.out.print(imgNum+": EO uv_shift_rot= ["+this.gIS[num_set].imageSet[master_sub].getUVShiftRot()[0]+
        	            			":"+this.gIS[num_set].imageSet[master_sub].getUVShiftRot()[1]+"], ");
        	            	System.out.println(imgNum+": combined uv_shift_rot= ["+combinedUVShiftRot[0]+":"+combinedUVShiftRot[1]+"]");
        	            }

        	            this.gIS[num_set].imageSet[ns].setUVShiftRot(combinedUVShiftRot); // uv_shift_rot);
        	            int [][] shiftRotMatrix= MatchSimulatedPattern.getRemapMatrix(this.gIS[num_set].imageSet[ns].getUVShiftRot());
        	            setGridsWithRemap( // null immediately
        	                    imgNum,
        	                    shiftRotMatrix, // int [][] reMap,
        	                    pixels,
        	                    patternParameters);
        			}
        		}
        	}
        	return true; // OK
        }


1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
        // provide image set index for the same station that has at least one marked image
        // non_estimated - disregard images with estimated orientation
        public int getMarkedSet(int num_set, boolean non_estimated) {
        	int station = this.gIS[num_set].stationNumber;
        	for (int ns = 0; ns < this.gIS.length; ns++) {
        		if (	(this.gIS[ns].stationNumber == station) &&
        				(!non_estimated || !this.gIS[ns].orientationEstimated)) {
                	for (int n=0;n<this.gIS[ns].imageSet.length;n++){
                		if ((this.gIS[ns].imageSet[n]!=null) && (this.gIS[ns].imageSet[n].matchedPointers > 0)){
                			return ns;
                		}
                	}
        		}

        	}
        	return -1; // none found
        }

        // get XYZ for this set's station from marked grid
        public double [] getXYZFromMarked(int num_set, boolean non_estimated) {
        	int ns = getMarkedSet(num_set, non_estimated);
        	if (ns < 0) return null;
        	return this.gIS[ns].GXYZ;
        }

1387 1388 1389 1390 1391 1392
        public double [] getXYZ(int num_img) {
        	int ns = gIP[num_img].getSetNumber();
        	return this.gIS[ns].GXYZ;
        }


1393 1394 1395 1396 1397 1398 1399
        // suggest set grid offset by comparing with known (by mark) set.
        // Wrong Grid UV should cause parallel shift - same Z, different XY
        public int [] suggestOffset (
        		int num_img,
        		boolean non_estimated,
        		boolean even,
        		PatternParameters patternParameters) {
1400
        	int num_set = this.gIP[num_img].getSetNumber();
1401 1402 1403 1404 1405 1406 1407
        	double [] ref_xyz = getXYZFromMarked(num_set, non_estimated);
        	if (ref_xyz == null) {
        		System.out.println("Error: Could not find reference goniometer XYZ for set "+num_set);
        		return null;
        	}
        	double [] diff_xyz = this.gIS[num_set].GXYZ.clone();
        	for (int i = 0; i < diff_xyz.length; i++) diff_xyz[i]-=ref_xyz[i];
1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421
        	return suggestOffset (
        			num_img,
        			diff_xyz, // z is not used, may ne just[2]
        			even,
        			patternParameters);
        }

        public int [] suggestOffset (
        		int num_img,
        		double [] diff_xyz, // This XYZ minus reference XYZ  z is not used, may be just[2]
        		boolean even,
        		PatternParameters patternParameters) {
        	int num_set = this.gIP[num_img].setNumber;
        	int station = this.gIS[num_set].stationNumber;
1422 1423 1424 1425 1426 1427
        	int [][] pixelsUV =  this.gIP[num_img].pixelsUV ; // null; // for each image, each grid node - a pair of {gridU, gridV}
        	if ((pixelsUV == null) || ((pixelsUV.length <3 ))) {
        		System.out.println("No/too few pixelsUV data for image "+num_img);
        		return null;
        	}
        	double [][][] data =new double [pixelsUV.length][3][];
1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446
        	for (int i=0; i < pixelsUV.length; i++){
        		data[i][0]=new double[2];
        		data[i][1]=new double[2];
        		data[i][2]=new double[1];
        		double [] xyzm = patternParameters.getXYZM(
        				pixelsUV[i][0],
        				pixelsUV[i][1],
        				false, // boolean verbose,
        				station); // int station)
        		data[i][0][0]=xyzm[0];// pixelsXY[i][0];
        		data[i][0][1]=xyzm[1];// pixelsXY[i][1];
        		data[i][1][0]=pixelsUV[i][0];
        		data[i][1][1]=pixelsUV[i][1];
        		data[i][2][0]=xyzm[3];// mask
        	}
        	double [][] coeff=new PolynomialApproximation(this.debugLevel).quadraticApproximation(data, true); // force linear
        	double [] dUV = {
        			-(coeff[0][0]* diff_xyz[0] + coeff[0][1]* diff_xyz[1]),
        			-(coeff[1][0]* diff_xyz[0] + coeff[1][1]* diff_xyz[1])};
1447 1448
        	int [] idUV = {(int) Math.round(dUV[0]), (int) Math.round(dUV[1]), 0}; // 0 - no rot
        	int parity = (idUV[0]+idUV[1] + (even?0:1)) & 1;
1449
        	double [] UV_err = {dUV[0]-idUV[0], dUV[1]-idUV[1]};
1450 1451
        	if (parity !=0) {
        		if (UV_err[1] > UV_err[0]) {
1452 1453
        			if (UV_err[1] > -UV_err[0]) idUV[1]++;
        			else             			idUV[0]--;
1454
        		} else {
1455 1456
        			if (UV_err[1] > -UV_err[0]) idUV[0]++;
        			else	                    idUV[1]--;
1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470
        		}
        		UV_err[0] = dUV[0] - idUV[0];
        		UV_err[1] = dUV[1] - idUV[1];
        	}
        	System.out.println(String.format("Errors U/V = %.3f:%.3f",UV_err[0],UV_err[1]));
        	return idUV;
        }


        public int [] offsetGrid(
        		int img_num,
        		int [] uv_shift_rot,
        		PatternParameters patternParameters) {
        	ImagePlus imp_grid = null;
1471
        	boolean woi_compensated = true;
1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483
    		if (this.gIP[img_num].gridImage!=null){ // use in-memory grid images instead of the files
    			int numGridImg=img_num;
    			if (numGridImg>=this.gIP.length) numGridImg=this.gIP.length-1;
    			imp_grid=this.gIP[numGridImg].gridImage;
    		}else if (this.gIP[img_num].path != null) {
    			imp_grid=(new Opener()).openImage("", this.gIP[img_num].path);
    			if (imp_grid==null) {
    				String msg="Failed to read grid file "+this.gIP[img_num].path;
    				IJ.showMessage("Error",msg);
    				throw new IllegalArgumentException (msg);
    			}
    			(new JP46_Reader_camera()).decodeProperiesFromInfo(imp_grid);
1484 1485 1486
    			woi_compensated = (getImagePlusProperty(imp_grid,"WOI_TOP",0) == 0) &&
    					(getImagePlusProperty(imp_grid,"WOI_LEFT",0) == 0);
//    			woi_compensated = getImagePlusProperty(imp_grid,"WOI_COMPENSATED",false);
1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497
    		} else {
    			System.out.println("Grid is not in memory, file path is not specified");
    			return null;
    		}
			ImageStack stack=imp_grid.getStack();
			if ((stack==null) || (stack.getSize()<4)) {
				String msg="Expected a 8-slice stack";
				IJ.showMessage("Error",msg);
				throw new IllegalArgumentException (msg);
			}
			float [][] pixels=new float[stack.getSize()][]; // now - 8 (x,y,u,v,contrast, vignR,vignG,vignB
1498

1499
        	for (int i=0;i<pixels.length;i++) pixels[i]= (float[]) stack.getPixels(i+1); // pixel X : negative - no grid here
1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514

			if (!woi_compensated) { // in memory - always compensated
				System.out.print(" woi_compensation-c "+img_num+" ");
				for (int i = 0; i < pixels[0].length; i++) {
					pixels[0][i] += this.gIP[img_num].woi.x;
					pixels[1][i] += this.gIP[img_num].woi.y;
				}
				this.gIP[img_num].woi.width += this.gIP[img_num].woi.x;
				this.gIP[img_num].woi.x = 0;
				this.gIP[img_num].woi.height += this.gIP[img_num].woi.y;
				this.gIP[img_num].woi.y = 0;
				woi_compensated = true;
			}

        	int [] combinedUVShiftRot=MatchSimulatedPattern.combineUVShiftRot(
1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528
                    this.gIP[img_num].getUVShiftRot(),
                    uv_shift_rot);
            this.gIP[img_num].setUVShiftRot(combinedUVShiftRot); // uv_shift_rot);
            int [][] shiftRotMatrix= MatchSimulatedPattern.getRemapMatrix(this.gIP[img_num].getUVShiftRot());
            setGridsWithRemap( // null immediately
            		img_num,
                    shiftRotMatrix, // int [][] reMap,
                    pixels,
                    patternParameters);
            calcGridPeriod(img_num, false); // centered, can skip _extra
            return this.gIP[img_num].getUVShiftRot();
        }


1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554
        public static int getImagePlusProperty(ImagePlus imp, String name, int dflt) {
           	try {
           		dflt = Integer.parseInt((String) (imp.getProperty(name)));
           	} catch (Exception e) {

           	}
           	return dflt;
        }

        public static double getImagePlusProperty(ImagePlus imp, String name, double dflt) {
           	try {
           		dflt = Double.parseDouble((String) (imp.getProperty(name)));
           	} catch (Exception e) {

           	}
           	return dflt;
        }

        public static boolean getImagePlusProperty(ImagePlus imp, String name, boolean dflt) {
           	try {
           		dflt = Boolean.parseBoolean((String) (imp.getProperty(name)));
           	} catch (Exception e) {

           	}
           	return dflt;
        }
1555

1556 1557 1558 1559 1560 1561 1562
        public static String getImagePlusProperty(ImagePlus imp, String name, String dflt) {
        	Object obj = imp.getProperty(name);
        	if (obj != null) {
        		dflt =  (String) obj;
        	}
           	return dflt;
        }
1563 1564


1565
        public DistortionCalibrationData (
Andrey Filippov's avatar
Andrey Filippov committed
1566 1567
        		EyesisCameraParameters eyesisCameraParameters,
        		Goniometer.GoniometerParameters goniometerParameters
1568
        		) {
1569
        	setupIndices();
Andrey Filippov's avatar
Andrey Filippov committed
1570
        	this.goniometerParameters = goniometerParameters;
1571
        	int numSubCameras=(eyesisCameraParameters==null)?1:eyesisCameraParameters.eyesisSubCameras[0].length;
1572

1573 1574 1575 1576
        	this.numSubCameras=numSubCameras;
        	this.eyesisCameraParameters=eyesisCameraParameters;
        }

1577

1578
        public DistortionCalibrationData (
1579 1580 1581
        		ImagePlus []           images, // images in the memory
        		PatternParameters      patternParameters,
        		EyesisCameraParameters eyesisCameraParameters,
Andrey Filippov's avatar
Andrey Filippov committed
1582 1583
        		LaserPointer           laserPointers,
        		Goniometer.GoniometerParameters goniometerParameters
1584
        		) {
1585
        	setupIndices();
Andrey Filippov's avatar
Andrey Filippov committed
1586
        	this.goniometerParameters = goniometerParameters;
1587 1588 1589
        	int numSubCameras=(eyesisCameraParameters==null)?1:eyesisCameraParameters.eyesisSubCameras[0].length;
        	this.numSubCameras=numSubCameras;
        	this.eyesisCameraParameters=eyesisCameraParameters;
1590 1591 1592 1593
        	setImages(
        			images,
        			patternParameters,
        			laserPointers);
1594
        }
1595

1596 1597 1598 1599 1600 1601 1602
        public int get_gIS_index(int numImg){
        	if (this.gIS==null) return -1;
        	for (int i=0;i<this.gIS.length;i++)
        		if (this.gIS[i].imageSet!=null)
        			for (int j=0;j<this.gIS[i].imageSet.length;j++)
        				if ((this.gIS[i].imageSet[j]!=null) &&(this.gIS[i].imageSet[j].imgNumber==numImg)) return i;
        	return -1;
1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616

        }

        public void listCameraParameters(boolean xcam){
        	int numSubCameras=getNumSubCameras();
        	if (this.gIP!=null) {
        		int maxChn=0;
        		for (int i=0;i<this.gIP.length;i++) if ((this.gIP[i]!=null) && (this.gIP[i].channel>maxChn)){
        			maxChn=this.gIP[i].channel;
        		}
        		numSubCameras=maxChn+1;
        	}

        	if (xcam && (numSubCameras == 4)) {
1617
//           	if (xcam) {
1618 1619 1620 1621
        		listCameraParametersXcam();
        	} else {
        		listCameraParameters();
        	}
1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634
        }
        public void listCameraParameters(){
            int numSubCameras=getNumSubCameras();
            if (this.gIP!=null) {
            	int maxChn=0;
            	for (int i=0;i<this.gIP.length;i++) if ((this.gIP[i]!=null) && (this.gIP[i].channel>maxChn)){
            		maxChn=this.gIP[i].channel;
            	}
            	numSubCameras=maxChn+1;
            }
        	String header="Name\tUnits";
        	StringBuffer sb = new StringBuffer();
        	for (int i=0;i<numSubCameras;i++) header+="\t"+i;
1635 1636 1637 1638
        	for (int stationNumber=0;stationNumber<this.eyesisCameraParameters.numStations;stationNumber++){
        		if (this.eyesisCameraParameters.numStations>1){
        			sb.append("Station "+stationNumber+" W="+(100*this.eyesisCameraParameters.stationWeight[stationNumber])+"%");  for (int i=-1;i<numSubCameras;i++) sb.append("\t===");  sb.append("\n");
        		}
1639

1640 1641 1642 1643 1644
        		int [] lensDistortionModels=new int [numSubCameras];
        		for (int i=0;i<numSubCameras;i++) lensDistortionModels[i]=eyesisCameraParameters.getLensDistortionModel(stationNumber,i);
        		sb.append("Lens Distortion Model\t");
        		for (int i=0;i<numSubCameras;i++) sb.append("\t"+lensDistortionModels[i]);
        		sb.append("\n");
1645 1646 1647 1648 1649 1650 1651 1652 1653 1654

        		sb.append("Sensor width\t");
        		for (int i=0;i<numSubCameras;i++) sb.append("\t"+eyesisCameraParameters.getSensorWidth(i));
        		sb.append("\n");

        		sb.append("Sensor height\t");
        		for (int i=0;i<numSubCameras;i++) sb.append("\t"+eyesisCameraParameters.getSensorHeight(i));
        		sb.append("\n");


1655
        		double [][] cameraPars=new double [numSubCameras][];
1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725

            	for (int i=0;i<numSubCameras;i++) cameraPars[i]=eyesisCameraParameters.getParametersVector(stationNumber,i);
            	// parameters same order as in this
            	for (int n=0;n<cameraPars[0].length;n++) if (isSubcameraParameter(n) && isIntrinsicParameter(n)){
            		sb.append(getParameterName(n)+"\t"+getParameterUnits(n));
            		for (int i=0;i<numSubCameras;i++) sb.append("\t"+IJ.d2s(cameraPars[i][n],3));
            		sb.append("\n");
            	}
            	sb.append("---");  for (int i=-1;i<numSubCameras;i++) sb.append("\t");  sb.append("\n");

            	for (int n=0;n<cameraPars[0].length;n++) if (isSubcameraParameter(n) && !isIntrinsicParameter(n)){
            		sb.append(getParameterName(n)+"\t"+getParameterUnits(n));
            		for (int i=0;i<numSubCameras;i++) sb.append("\t"+IJ.d2s(cameraPars[i][n],3));
            		sb.append("\n");
            	}
            	sb.append("---");  for (int i=-1;i<numSubCameras;i++) sb.append("\t");  sb.append("\n");
            	for (int n=0;n<cameraPars[0].length;n++) if (
            			!isSubcameraParameter(n)&&
            			!isLocationParameter(n)&&
            			!isOrientationParameter(n)){
            		sb.append(getParameterName(n)+"\t"+getParameterUnits(n));
            		sb.append("\t"+IJ.d2s(cameraPars[0][n],3));
            		for (int i=1;i<numSubCameras;i++) sb.append("\t---");
            		sb.append("\n");
            	}
            	sb.append("---");  for (int i=-1;i<numSubCameras;i++) sb.append("\t");  sb.append("\n");
            	for (int n=0;n<cameraPars[0].length;n++) if (isLocationParameter(n)){
            		sb.append(getParameterName(n)+"\t"+getParameterUnits(n));
            		sb.append("\t"+IJ.d2s(cameraPars[0][n],3));
            		for (int i=1;i<numSubCameras;i++) sb.append("\t---");
            		sb.append("\n");
            	}
            	sb.append("---");  for (int i=-1;i<numSubCameras;i++) sb.append("\t");  sb.append("\n");
            	for (int n=0;n<cameraPars[0].length;n++) if (isOrientationParameter(n)){
            		sb.append(getParameterName(n)+"\t"+getParameterUnits(n));
            		sb.append("\t"+IJ.d2s(cameraPars[0][n],3));
            		for (int i=1;i<numSubCameras;i++) sb.append("\t---");
            		sb.append("\n");
            	}
            }
     	    new TextWindow("Camera parameters", header, sb.toString(), 85*(numSubCameras+3),600);
         }

        public void listCameraParametersXcam(){ // getNumSubCameras() should be 4!
        	double rollDegPerTurn =  -0.45/33.5*180/Math.PI; //  -0.769644799429464 deg/turn, CW screw increases roll, degrees per 1 screw turn
        	double headDegPerTurn = 0.45/34.5*180/Math.PI; //  0.7473362545184652  deg/turn, both screws CW decreases heading (degree/turn)
        	double elevDegPerTurn = 0.45/14*180/Math.PI; //  1.8416500557776463 deg/turn, top CW, bottom CCW decreases elevation (degree/turn)


            int numSubCameras=getNumSubCameras();
            if (this.gIP!=null) {
            	int maxChn=0;
            	for (int i=0;i<this.gIP.length;i++) if ((this.gIP[i]!=null) && (this.gIP[i].channel>maxChn)){
            		maxChn=this.gIP[i].channel;
            	}
            	numSubCameras=maxChn+1;
            }
        	String header="Name\tUnits";
        	StringBuffer sb = new StringBuffer();
        	for (int i=0;i<numSubCameras;i++) header+="\t"+i;
        	for (int stationNumber=0;stationNumber<this.eyesisCameraParameters.numStations;stationNumber++){
        		if (this.eyesisCameraParameters.numStations>1){
        			sb.append("Station "+stationNumber+" W="+(100*this.eyesisCameraParameters.stationWeight[stationNumber])+"%");  for (int i=-1;i<numSubCameras;i++) sb.append("\t===");  sb.append("\n");
        		}

        		int [] lensDistortionModels=new int [numSubCameras];
        		for (int i=0;i<numSubCameras;i++) lensDistortionModels[i]=eyesisCameraParameters.getLensDistortionModel(stationNumber,i);
//        		sb.append("Lens Distortion Model\t");
//        		for (int i=0;i<numSubCameras;i++) sb.append("\t"+lensDistortionModels[i]);
//        		sb.append("\n");
1726 1727 1728 1729 1730 1731 1732 1733 1734

        		sb.append("Sensor width\t");
        		for (int i=0;i<numSubCameras;i++) sb.append("\t"+eyesisCameraParameters.getSensorWidth(i));
        		sb.append("\n");

        		sb.append("Sensor height\t");
        		for (int i=0;i<numSubCameras;i++) sb.append("\t"+eyesisCameraParameters.getSensorHeight(i));
        		sb.append("\n");

1735 1736
        		double [][] cameraPars=new double [numSubCameras][];

1737
            	for (int i=0;i<numSubCameras;i++) cameraPars[i]=eyesisCameraParameters.getParametersVector(stationNumber,i);
1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788

            	// calculate average height average right
            	double [] subcamRight =      new double[numSubCameras];
            	double [] subcamHeight =     new double[numSubCameras];
            	double [] subcamCorrRight =  new double[numSubCameras]; // in rotated C.S.
            	double [] subcamCorrHeight = new double[numSubCameras]; // in rotated C.S.
            	double [] subcamHeading =    new double[numSubCameras];
            	double [] subcamElevation =  new double[numSubCameras];
            	double subcamRightCenter =     0.0;
            	double subcamHeightCenter =    0.0;
            	double subcamHeadingCenter =   0.0;
            	double subcamElevationCenter = 0.0;
            	double [] subcamRelRot =        new double[numSubCameras];
            	double [] subcamRelHeading =    new double[numSubCameras];
            	double [] subcamRelElevation =  new double[numSubCameras];

            	double [] rollCorrTurns =       new double[numSubCameras];
            	double [] topCorrTurns =        new double[numSubCameras];
            	double [] botCorrTurns =        new double[numSubCameras];
            	for (int i=0;i<numSubCameras;i++) {
            		subcamRight[i] =     cameraPars[i][getParameterIndexByName("subcamRight")];
            		subcamHeight[i] =    cameraPars[i][getParameterIndexByName("subcamHeight")];
            		subcamHeading[i] =   cameraPars[i][getParameterIndexByName("subcamHeading")];
            		subcamElevation[i] = cameraPars[i][getParameterIndexByName("subcamElevation")];
            		subcamRightCenter +=     subcamRight[i];
            		subcamHeightCenter +=    subcamHeight[i];
            		subcamHeadingCenter +=   subcamHeading[i];
            		subcamElevationCenter += subcamElevation[i];
            	}
            	subcamRightCenter /= numSubCameras;
            	subcamHeightCenter /= numSubCameras;
            	subcamHeadingCenter /= numSubCameras;
            	subcamElevationCenter /= numSubCameras;
            	double [] subcamNominalDirs = {135.0,45.0, -135.0, -45.0};
            	double [] subcamDirsDeg = new double[numSubCameras];
            	double commonRot = 0.0;
            	for (int i=0;i<numSubCameras;i++) {
            		subcamDirsDeg[i]=180.0/Math.PI*Math.atan2(subcamHeight[i]-subcamHeightCenter, subcamRight[i]-subcamRightCenter);
            		commonRot += subcamNominalDirs[i]-subcamDirsDeg[i];
            	}
            	commonRot /= numSubCameras;
            	for (int i=0;i<numSubCameras;i++) {
            		subcamRelRot[i] =       cameraPars[i][getParameterIndexByName("subcamRoll")] - commonRot;
            		subcamRelHeading[i] =   subcamHeading[i] - subcamHeadingCenter;
            		subcamRelElevation[i] =   subcamElevation[i] - subcamElevationCenter;

            		double r = Math.sqrt((subcamHeight[i]-subcamHeightCenter)*(subcamHeight[i]-subcamHeightCenter)+
            				(subcamRight[i]-subcamRightCenter)*(subcamRight[i]-subcamRightCenter));
            		subcamCorrRight[i] = r*Math.cos(Math.PI/180.0*(subcamDirsDeg[i]+commonRot));
            		subcamCorrHeight[i] = r*Math.sin(Math.PI/180.0*(subcamDirsDeg[i]+commonRot));

1789 1790 1791
            		rollCorrTurns[i] = subcamRelRot[i] / rollDegPerTurn;
            		topCorrTurns[i] =  subcamRelHeading[i] / headDegPerTurn + subcamRelElevation[i] / elevDegPerTurn;
            		botCorrTurns[i] =  subcamRelHeading[i] / headDegPerTurn - subcamRelElevation[i] / elevDegPerTurn;
1792 1793 1794

            	}
/*
1795 1796 1797 1798 1799 1800
            	// parameters same order as in this
            	for (int n=0;n<cameraPars[0].length;n++) if (isSubcameraParameter(n) && isIntrinsicParameter(n)){
            		sb.append(getParameterName(n)+"\t"+getParameterUnits(n));
            		for (int i=0;i<numSubCameras;i++) sb.append("\t"+IJ.d2s(cameraPars[i][n],3));
            		sb.append("\n");
            	}
1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850
*/
//            	sb.append("---");  for (int i=-1;i<numSubCameras;i++) sb.append("\t");  sb.append("\n");
            	int flindex =getParameterIndexByName("subcamFocalLength");
        		sb.append(getParameterName(flindex)+"\t"+getParameterUnits(flindex));
        		for (int i=0;i<numSubCameras;i++) sb.append("\t"+IJ.d2s(cameraPars[i][flindex],3));
        		sb.append("\n");

            	sb.append("Camera roll"+"\t"+"degrees"+"\t"+IJ.d2s(commonRot,3));
            	for (int i=1;i<numSubCameras;i++) sb.append("\t");  sb.append("\n");

            	sb.append("Camera heading"+"\t"+"degrees"+"\t"+IJ.d2s(subcamHeadingCenter,3));
            	for (int i=1;i<numSubCameras;i++) sb.append("\t");  sb.append("\n");

            	sb.append("Camera elevation"+"\t"+"degrees"+"\t"+IJ.d2s(subcamElevationCenter,3));
            	for (int i=1;i<numSubCameras;i++) sb.append("\t");  sb.append("\n");

            	sb.append("Rel roll"+"\t"+"degrees");
            	for (int i=0;i<numSubCameras;i++) sb.append("\t"+IJ.d2s(subcamRelRot[i],3));
            	sb.append("\n");


            	sb.append("Rel heading"+"\t"+"degrees");
            	for (int i=0;i<numSubCameras;i++) sb.append("\t"+IJ.d2s(subcamRelHeading[i],3));
            	sb.append("\n");

            	sb.append("Rel elevation"+"\t"+"degrees");
            	for (int i=0;i<numSubCameras;i++) sb.append("\t"+IJ.d2s(subcamRelElevation[i],3));
            	sb.append("\n");

            	sb.append("Corr right"+"\t"+"mm");
            	for (int i=0;i<numSubCameras;i++) sb.append("\t"+IJ.d2s(subcamCorrRight[i],3));
            	sb.append("\n");

            	sb.append("Corr height"+"\t"+"mm");
            	for (int i=0;i<numSubCameras;i++) sb.append("\t"+IJ.d2s(subcamCorrHeight[i],3));
            	sb.append("\n");

            	sb.append("---");  for (int i=-1;i<numSubCameras;i++) sb.append("\t");  sb.append("\n");
            	sb.append("Screw roll"+"\t"+"turns CW");
            	for (int i=0;i<numSubCameras;i++) sb.append("\t"+IJ.d2s(rollCorrTurns[i],2));
            	sb.append("\n");

            	sb.append("Screw top"+"\t"+"turns CW");
            	for (int i=0;i<numSubCameras;i++) sb.append("\t"+IJ.d2s(topCorrTurns[i],2));
            	sb.append("\n");

            	sb.append("Screw bottom"+"\t"+"turns CW");
            	for (int i=0;i<numSubCameras;i++) sb.append("\t"+IJ.d2s(botCorrTurns[i],2));
            	sb.append("\n");

1851 1852 1853 1854 1855 1856 1857 1858
            	sb.append("---");  for (int i=-1;i<numSubCameras;i++) sb.append("\t");  sb.append("\n");

            	for (int n=0;n<cameraPars[0].length;n++) if (isSubcameraParameter(n) && !isIntrinsicParameter(n)){
            		sb.append(getParameterName(n)+"\t"+getParameterUnits(n));
            		for (int i=0;i<numSubCameras;i++) sb.append("\t"+IJ.d2s(cameraPars[i][n],3));
            		sb.append("\n");
            	}
            	sb.append("---");  for (int i=-1;i<numSubCameras;i++) sb.append("\t");  sb.append("\n");
1859
            	/*
1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882
            	for (int n=0;n<cameraPars[0].length;n++) if (
            			!isSubcameraParameter(n)&&
            			!isLocationParameter(n)&&
            			!isOrientationParameter(n)){
            		sb.append(getParameterName(n)+"\t"+getParameterUnits(n));
            		sb.append("\t"+IJ.d2s(cameraPars[0][n],3));
            		for (int i=1;i<numSubCameras;i++) sb.append("\t---");
            		sb.append("\n");
            	}
            	sb.append("---");  for (int i=-1;i<numSubCameras;i++) sb.append("\t");  sb.append("\n");
            	for (int n=0;n<cameraPars[0].length;n++) if (isLocationParameter(n)){
            		sb.append(getParameterName(n)+"\t"+getParameterUnits(n));
            		sb.append("\t"+IJ.d2s(cameraPars[0][n],3));
            		for (int i=1;i<numSubCameras;i++) sb.append("\t---");
            		sb.append("\n");
            	}
            	sb.append("---");  for (int i=-1;i<numSubCameras;i++) sb.append("\t");  sb.append("\n");
            	for (int n=0;n<cameraPars[0].length;n++) if (isOrientationParameter(n)){
            		sb.append(getParameterName(n)+"\t"+getParameterUnits(n));
            		sb.append("\t"+IJ.d2s(cameraPars[0][n],3));
            		for (int i=1;i<numSubCameras;i++) sb.append("\t---");
            		sb.append("\n");
            	}
1883
*/
1884 1885 1886 1887 1888 1889 1890
            }
     	    new TextWindow("Camera parameters", header, sb.toString(), 85*(numSubCameras+3),600);
         }


        public void setImages(
        		ImagePlus [] images,  // images in the memory
1891 1892
        		PatternParameters patternParameters,
        		LaserPointer laserPointers){
1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905
        	this.gIP=new GridImageParameters[images.length];
        	for (int i=0;i<images.length;i++){
        		this.gIP[i]=new GridImageParameters(i);
        		this.gIP[i].path=      images[i].getTitle(); // not real path?
        		this.gIP[i].timestamp= getImageTimestamp(images[i]);
        		System.out.println(i+": "+this.gIP[i].path+" - timestamp="+this.gIP[i].timestamp);
        		this.gIP[i].channel=   getImageChannel(images[i]);
            	this.gIP[i].gridImage=images[i]; // free later?

        	}
// Create parameters array
        	initPars (this.gIP.length,parameterDescriptions.length);
        	this.gIS=null; // so it will be created in readAllGrids()
1906 1907
        	readAllGrids(
        			patternParameters, // prepare grid parameters for LMA
1908 1909
        			laserPointers, // prepare grid parameters for LMA
            		true); // boolean keep_images make it configurable parameter?
1910 1911
        	// no orientation
        }
1912

1913
        public void listImageSet(){
1914
        	listImageSet(0, null,null, null,null,null);
1915 1916
        }

1917
        public void listImageSet(
1918
        		int    mode,
1919
        		int [] numPoints, // All arrays may be twice long, then 1 - EO, second - LWIR
1920
        		double [] setRMS,
1921 1922 1923 1924
        		boolean [] hasNaNInSet,
    			int [][] numImgPoints,
    			double [][] rmsPerImg
        		){
1925 1926 1927
        	if ((this.gIS==null) || (this.gIS.length==0)){
        		return;
        	}
1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938
        	boolean showXYZ = true;
    		boolean hasLwir = false;
    		if (numPoints!=null) {
        		hasLwir = numPoints.length > this.gIS.length; // twice longer
    		} else if (setRMS != null) {
        		hasLwir = setRMS.length > this.gIS.length; // twice longer
    		} else if (hasNaNInSet != null) {
        		hasLwir = hasNaNInSet.length > this.gIS.length; // twice longer
    		}


1939 1940
        	String header="#\ttimestamp";
        	if (this.eyesisCameraParameters.numStations>1) header+="\tStation";
1941
//        	header+="\tAxial\tTilt\thorPhi\thorPsi\tX\tY\tZ\tMotor2\tMotor3";
1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953
        	header+="\tAxial\tTilt\tdTilt\tInter";
        	if (showXYZ) {
            	header+="\tGXY0\tGXY1\tGXY2";
        	}
        	header+="\tMotor2\tMotor3";
        	if (numPoints!=null) {
        		if (hasLwir) {
            		header+="\tNumPointsEO\tNumPointsLWIR";
        		} else {
        			header+="\tNumPoints";
        		}
        	}
1954
        	header+="\tEnabled\tMatched";
1955 1956 1957 1958 1959 1960 1961
        	if (setRMS!=null) {
        		if (hasLwir) {
            		header+="\tRMS-EO\tRMS-LWIR\tWeight";
        		} else {
            		header+="\tRMS\tWeight";
        		}
        	}
1962 1963
        	for (int n=0;n<this.gIS[0].imageSet.length;n++) header+="\t"+n;
    		StringBuffer sb = new StringBuffer();
1964

1965
    		for (int i=0;i<this.gIS.length;i++){
1966 1967 1968 1969 1970 1971 1972 1973 1974 1975
    			double axial_corr_sign=this.gIS[i].goniometerAxial; // correct sign of rotation beyond +/-180 according to motor steps
    			if (this.gIS[i].motors != null) {
    				if (this.gIS[i].motors[1] > 0){
    					if (axial_corr_sign < -90.0) {
    						axial_corr_sign += 360.0;
    					}
    				} else {
    					if (axial_corr_sign > 90.0) {
    						axial_corr_sign -= 360.0;
    					}
1976

1977 1978 1979 1980 1981 1982 1983
    				}
    			}
    			// calculate average tilt for this tilt motor and difference of the current tilt from average
    			double dTilt=Double.NaN;
    			if (!Double.isNaN(this.gIS[i].goniometerTilt) && (this.gIS[i].motors != null)){
    				int i_low,i_high;
    				for (i_low=i-1;i_low>=0;i_low--){
1984
    					if ((this.gIS[i_low].motors != null) && (this.gIS[i_low].motors[2] != this.gIS[i].motors[2])) break;
1985 1986 1987
    				}
    				i_low++;
    				for (i_high=i+1;i_high < this.gIS.length;i_high++){
1988
    					if ((this.gIS[i_high].motors != null) && (this.gIS[i_high].motors[2] != this.gIS[i].motors[2])) break;
1989 1990 1991 1992 1993 1994 1995 1996 1997
    				}
    				int num_avg=0;
    				double sum_avg=0.0;
    				for (int i_avg=i_low;i_avg < i_high; i_avg++){
    					if (!Double.isNaN(this.gIS[i_avg].goniometerTilt)){
    						num_avg++;
    						sum_avg += this.gIS[i_avg].goniometerTilt;
    					}
    				}
1998 1999
    				if (num_avg>0) dTilt = this.gIS[i].goniometerTilt - (sum_avg/num_avg);

2000 2001 2002
    			}
    			double firstInterAxisAngle=Double.NaN;
    			firstInterAxisAngle = this.gIS[i].interAxisAngle;
2003

2004
    			sb.append(i+"\t"+IJ.d2s(this.gIS[i].timeStamp,6));
2005
    			if (this.eyesisCameraParameters.numStations>1)	sb.append("\t"+ this.gIS[i].getStationNumber());
2006
    			sb.append("\t"+(Double.isNaN(this.gIS[i].goniometerAxial)?"---":((this.gIS[i].orientationEstimated?"(":"")+IJ.d2s(axial_corr_sign,3)+(this.gIS[i].orientationEstimated?")":""))));
2007
    			sb.append("\t"+(Double.isNaN(this.gIS[i].goniometerTilt)?"---":((this.gIS[i].orientationEstimated?"(":"")+IJ.d2s(this.gIS[i].goniometerTilt,3)+(this.gIS[i].orientationEstimated?")":""))));
2008 2009 2010

    			sb.append("\t"+(Double.isNaN(dTilt)?"---":IJ.d2s(dTilt,3)));
    			sb.append("\t"+(Double.isNaN(firstInterAxisAngle)?"---":IJ.d2s(firstInterAxisAngle,3)));
2011

2012 2013 2014 2015 2016 2017
            	if (showXYZ) {
                	sb.append(String.format("\t%.1f\t%.1f\t%.1f", this.gIS[i].GXYZ[0], this.gIS[i].GXYZ[1], this.gIS[i].GXYZ[2]));
            	}



2018 2019 2020 2021 2022
    			if (this.gIS[i].motors==null) {
    				sb.append("\t"+"bug"+"\t"+"bug");
    			} else {
    				sb.append("\t"+this.gIS[i].motors[1]+"\t"+this.gIS[i].motors[2]); // null pointer here????
    			}
2023 2024 2025 2026 2027 2028 2029 2030
            	if (numPoints!=null) {
            		if (hasLwir) {
                		sb.append("\t"+numPoints[2 * i + 0]);
                		sb.append("\t"+numPoints[2 * i + 1]);
            		} else {
                		sb.append("\t"+numPoints[i]);
            		}
            	}
2031 2032 2033
            	int numEnImages=0;
            	for (int n=0;n<this.gIS[i].imageSet.length;n++)if (this.gIS[i].imageSet[n]!=null){
            		if (this.gIS[i].imageSet[n].enabled) numEnImages++;
2034
            	}
2035 2036 2037 2038
            	sb.append("\t"+numEnImages);
            	int matchedPointersInSet=0;
            	for (int n=0;n<this.gIS[i].imageSet.length;n++){
            		if (this.gIS[i].imageSet[n]!=null){
2039
            			matchedPointersInSet+=this.gIS[i].imageSet[n].matchedPointers;
2040 2041 2042 2043
            		}
            	}
            	sb.append("\t"+matchedPointersInSet);
            	if (setRMS!=null) {
2044 2045 2046 2047 2048 2049 2050 2051
            		if (hasLwir) {
                		sb.append("\t"+(((hasNaNInSet!=null) && hasNaNInSet[2 * i + 0])?"*":"")+IJ.d2s(setRMS[2 * i + 0],3));
                		sb.append("\t"+(((hasNaNInSet!=null) && hasNaNInSet[2 * i + 1])?"*":"")+IJ.d2s(setRMS[2 * i + 1],3)); //393
                		sb.append("\t"+IJ.d2s(this.gIS[i].setWeight,3));
            		} else {
                		sb.append("\t"+(((hasNaNInSet!=null) && hasNaNInSet[i])?"*":"")+IJ.d2s(setRMS[i],3));
                		sb.append("\t"+IJ.d2s(this.gIS[i].setWeight,3));
            		}
2052
            	}
2053 2054 2055 2056 2057 2058 2059 2060 2061 2062
            	switch (mode) {
            	case 0:
            		for (int n=0;n<this.gIS[i].imageSet.length;n++){
            			sb.append("\t");
            			if (this.gIS[i].imageSet[n]!=null){
            				int numPointers=0; // count number of laser pointers
            				if (this.gIS[i].imageSet[n].laserPixelCoordinates!=null){
            					for (int j=0;j<this.gIS[i].imageSet[n].laserPixelCoordinates.length;j++) {
            						if (this.gIS[i].imageSet[n].laserPixelCoordinates[j]!=null) numPointers++;
            					}
2063
            				}
2064 2065 2066 2067 2068
            				if (!this.gIS[i].imageSet[n].enabled) sb.append("(");
            				sb.append(numPointers+"("+this.gIS[i].imageSet[n].matchedPointers+"):"+this.gIS[i].imageSet[n].hintedMatch +
            						" "+IJ.d2s(this.gIS[i].imageSet[n].getGridPeriod(),1));
            				if (!this.gIS[i].imageSet[n].enabled) sb.append(")");

2069
            			}
2070 2071 2072 2073 2074 2075 2076 2077
            		}
            		break;
            	case 1:
            		for (int n=0;n<this.gIS[i].imageSet.length;n++){
            			sb.append("\t");
            			if (this.gIS[i].imageSet[n]!=null){
            				int [] uvrot = this.gIS[i].imageSet[n].getUVShiftRot();
            				sb.append(uvrot[0]+":"+uvrot[1]+"("+uvrot[2]+")");
2078 2079
            			} else {
            				sb.append("\t---");
2080 2081 2082 2083 2084 2085 2086 2087
            			}
            		}
            		break;
            	case 2:
            		for (int n=0;n<this.gIS[i].imageSet.length;n++){
            			sb.append("\t");
            			if (this.gIS[i].imageSet[n]!=null){
            				sb.append(this.gIS[i].imageSet[n].pixelsXY.length+"+"+this.gIS[i].imageSet[n].pixelsXY_extra.length);
2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103
            			} else {
            				sb.append("\t---");
            			}
            		}
            		break;
            	case 3:
            		for (int n=0;n<this.gIS[i].imageSet.length;n++){
            			sb.append("\t");
            			if ((this.gIS[i].imageSet[n]!=null) && (numImgPoints!=null) && (rmsPerImg !=null)){
            				if (Double.isNaN(rmsPerImg[i][n])) {
                				sb.append("NaN ("+numImgPoints[i][n]+")");
            				} else {
                				sb.append(String.format("%.3f (%d)",rmsPerImg[i][n],numImgPoints[i][n]));
            				}
            			} else {
            				sb.append("---");
2104
            			}
2105
            		}
2106
            		break;
2107 2108 2109
            	}
            	sb.append("\n");
    		}
2110
			new TextWindow("Image calibration state (pointers/hinted state)", header, sb.toString(), 1400, 900);
2111
        }
2112 2113


2114
        /**
2115
         * create list of image indices per image set
2116
         * @return array of image indices for each image set
2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130
         */
        public int [][] listImages(boolean enabledOnly){
        	int [][] imageSets = new int [this.gIS.length][];
    		for (int i=0;i<this.gIS.length;i++){
    			int setSize=0;
    			for (int n=0;n<this.gIS[i].imageSet.length;n++) if ((this.gIS[i].imageSet[n]!=null) && (this.gIS[i].imageSet[n].imgNumber>=0) && (!enabledOnly || this.gIS[i].imageSet[n].enabled)) setSize++;
    			imageSets[i]=new int [setSize];
    		}
    		for (int i=0;i<this.gIS.length;i++){
    			int index=0;
    			for (int n=0;n<this.gIS[i].imageSet.length;n++) if ((this.gIS[i].imageSet[n]!=null) && (this.gIS[i].imageSet[n].imgNumber>=0) && (!enabledOnly || this.gIS[i].imageSet[n].enabled)) imageSets[i][index++]=this.gIS[i].imageSet[n].imgNumber;
    		}
        	return imageSets;
        }
2131

2132 2133 2134 2135
        /**
         * Filter images (grids) by calibration status with laser pointers and "hinted" from the camera orientation
         * buildImageSets may be needed to be re-ran (if it was ran with all=false)
         * @param resetHinted - if true - reset status of "hinted" calibration to undefined
2136
         * @param minPointers minimal number of laser pointers considered to be enough (usually 2, as mirror/non-mirror is aPriori known
2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155
         * @parame minGridPeriod - minimal detected grid period as a fraction of the maximal (filtering reflected grids)
         * @return number of enabled images
         */
        public int [] filterImages(
        		boolean resetHinted,
        		int minPointers,
        		double minGridPeriodFraction,
        		boolean disableNoVignetting,
        		int minGridNodes){
        	int notEnoughNodes=0;
        	int numEnabled=0;
        	int newEnabled=0;
        	int maxPeriod=100;
        	int periodSubdivide=10;
        	int numBins=maxPeriod*periodSubdivide;
        	double [] periodHistogram=new double[numBins];
        	double [] medianGridPeriod=new double [this.eyesisCameraParameters.numStations];
        	double [] maxGridPeriod=new double [this.eyesisCameraParameters.numStations];
        	double [] minGridPeriod=new double [this.eyesisCameraParameters.numStations];
2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191
        	// With different sensors different channels wil have different periods
        	int numChannels = getNumSubCameras(); // this.eyesisCameraParameters.getNumChannels();
        	double [][] sw = new double [numChannels][2];
    		for (int i=0;i<this.gIP.length;i++) if (getNumNodes(i, false) >0) {
    			double period = this.gIP[i].getGridPeriod();
    			if (!Double.isNaN(period) && !Double.isInfinite(period) ) {
    				int chn = this.gIP[i].getChannel();
    				sw[chn][0] += getNumNodes(i, false); // weight
    				sw[chn][1] += getNumNodes(i, false) * period; // weight
    			}
    		}

    		double [] avg_periods =  new double [numChannels];
    		double [] ravg_periods = new double [numChannels];
    		this.small_sensors =     new boolean [numChannels];
    		double max_per = 0;
    		for (int i = 0; i < numChannels; i++) {
    			avg_periods[i] =sw[i][1] / sw[i][0];
    			if (max_per < avg_periods[i])  max_per = avg_periods[i];
    		}
    		double [][] sw1 = new double [2][2];
    		for (int i = 0; i < numChannels; i++) {
    			ravg_periods[i] = avg_periods[i] / max_per;
    			small_sensors[i] = ravg_periods[i] < SMALL_FRACTION;
    			sw1[small_sensors[i]?1:0][0] += sw[i][0];
    			sw1[small_sensors[i]?1:0][1] += sw[i][0] * ravg_periods[i];
    		}
    		this.small_period_frac = (sw1[1][0] == 0) ? 0.0 : (sw1[1][1] * sw1[0][0] / (sw1[1][0] * sw1[0][1]));
    		if (small_period_frac > 0.0) {
    			System.out.println(String.format("2 types of sensors are detected, lowres has %5.2f%% resolution",100*small_period_frac));
    			System.out.print("Sensor map: ");
    			for (int i = 0; i < numChannels; i++) {
    				System.out.print(i+":"+(small_sensors[i]?"low-res":"high-res")+", ");
    			}
    			System.out.println();
    		}
2192 2193 2194 2195
        	for (int stationNumber=0;stationNumber<this.eyesisCameraParameters.numStations;stationNumber++){
        		for (int i=0;i<numBins;i++) periodHistogram[i]=0.0;
        		int numSamples=0;
        		for (int i=0;i<this.gIP.length;i++) if (this.gIP[i].getStationNumber()==stationNumber){
2196 2197 2198 2199 2200
        			double period = getEffectivePeriod(i);
//        			if (!Double.isNaN(this.gIP[i].getGridPeriod())) {
        			if (!Double.isNaN(period)) {
//        				int iPeriod=(int) Math.round(this.gIP[i].getGridPeriod()*periodSubdivide);
        				int iPeriod=(int) Math.round(period*periodSubdivide);
2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220
        				if (iPeriod>=numBins) iPeriod=numBins-1;
        				else if (iPeriod<0) iPeriod=0; // does not count NaN
        				if (iPeriod>0) {
        					periodHistogram[iPeriod]++;
        					numSamples++;
        				}
        			}
        		}
        		int sumLess=0;
        		medianGridPeriod[stationNumber]=0.0;
        		for (int i=0;i<numBins;i++){
        			sumLess+=periodHistogram[i];
        			if (sumLess>(numSamples/2)) {
        				medianGridPeriod[stationNumber]=(1.0*i)/periodSubdivide;
        				break;
        			}
        		}

        		maxGridPeriod[stationNumber]=0.0;
        		for (int i=0;i<this.gIP.length;i++) if (this.gIP[i].getStationNumber()==stationNumber){
2221 2222 2223 2224 2225 2226
//        			if (this.gIP[i].getGridPeriod()>maxGridPeriod[stationNumber]) {
//        				maxGridPeriod[stationNumber]=this.gIP[i].getGridPeriod();
//        			}
        			if (getEffectivePeriod(i) > maxGridPeriod[stationNumber]) {
        				maxGridPeriod[stationNumber]=getEffectivePeriod(i);
        			}
2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241
        		}
        		minGridPeriod[stationNumber]=medianGridPeriod[stationNumber]*minGridPeriodFraction;
            	System.out.print("Station "+stationNumber+ ": maximal grid period="+maxGridPeriod[stationNumber]+" minimal grid period="+minGridPeriod[stationNumber]+" median grid period="+medianGridPeriod[stationNumber]+" numSamples="+numSamples);
            	if (minGridPeriodFraction>0.0) maxGridPeriod[stationNumber]=medianGridPeriod[stationNumber]/minGridPeriodFraction;
            	System.out.println(" new maximal grid period="+maxGridPeriod[stationNumber]);
        	}
        	// set which image set each image belongs
        	int [] gIS_index=new int [this.gIP.length];
        	for (int i=0;i<gIS_index.length;i++)gIS_index[i]=-1;
        	if (this.gIS!=null){
            	for (int i=0;i<this.gIS.length;i++) if (this.gIS[i].imageSet!=null)for (int j=0;j<this.gIS[i].imageSet.length;j++) if (this.gIS[i].imageSet[j]!=null){
            		gIS_index[this.gIS[i].imageSet[j].imgNumber]=i;
            	}
        	}
        	int numNoVignetting=0;
2242
        	int disabledNoLaser=0;
2243 2244
        	for (int i=0;i<this.gIP.length;i++){
        		int stationNumber=this.gIP[i].getStationNumber();
2245
        		boolean enableNoLaser=this.eyesisCameraParameters.getEnableNoLaser(stationNumber,this.gIP[i].channel);
2246 2247
        		boolean wasEnabled=this.gIP[i].enabled;
        		if (resetHinted) this.gIP[i].hintedMatch=-1; // undefined
2248 2249 2250 2251 2252
///        		if (Double.isNaN(this.gIP[i].getGridPeriod()) ||
///        				((minGridPeriodFraction>0) && ((this.gIP[i].getGridPeriod()<minGridPeriod[stationNumber]) || (this.gIP[i].getGridPeriod()>maxGridPeriod[stationNumber])))){
           		if (Double.isNaN(getEffectivePeriod(i)) ||
           				((minGridPeriodFraction>0) && ((getEffectivePeriod(i)<minGridPeriod[stationNumber]) ||
           						(getEffectivePeriod(i) > maxGridPeriod[stationNumber])))){
2253
        			this.gIP[i].hintedMatch=0; // is it needed?
2254 2255
        			this.gIP[i].enabled=false; // failed against minimal grid period (too far) - probably double reflection in the windows
        		}
2256 2257 2258
        		if (this.gIP[i].hintedMatch==0) {
        			this.gIP[i].enabled=false; // failed against predicted grid
        		} else {
2259 2260 2261
        			if (
        					(this.gIP[i].matchedPointers>=minPointers) ||
        					((this.gIP[i].matchedPointers>0) && (this.gIP[i].hintedMatch>0)) || // orientation and one pointer
2262
        					((this.gIP[i].hintedMatch>1) && enableNoLaser)) { // do not use bottom images w/o matched pointers
2263
        				// before enabling - copy orientation from gIS
2264
        				if (!this.gIP[i].enabled && (gIS_index[i]>=0)){ // FIXME - is it correct to use set index 0?
2265 2266 2267 2268
        					if (!Double.isNaN(this.gIS[gIS_index[i]].goniometerTilt))	setGH(i,this.gIS[gIS_index[i]].goniometerTilt );
        					if (!Double.isNaN(this.gIS[gIS_index[i]].goniometerAxial))	setGA(i,this.gIS[gIS_index[i]].goniometerAxial );
        				}
        				this.gIP[i].enabled=true;
2269 2270 2271
        			} else {
        				this.gIP[i].enabled=false;
        			}
2272 2273
        			if ((this.gIP[i].hintedMatch>1) && !enableNoLaser && (this.gIP[i].matchedPointers==0)){
        				disabledNoLaser++;
2274 2275
        			}
        		}
2276

2277 2278 2279 2280
        		if (disableNoVignetting) {
        			if (this.gIP[i].enabled &!this.gIP[i].flatFieldAvailable) numNoVignetting++;
        			this.gIP[i].enabled &= this.gIP[i].flatFieldAvailable;
        		}
2281 2282 2283 2284 2285 2286 2287 2288
        		if (this.gIP[i].motors==null) { // only disable if any other set has motors
        			boolean hasMotors=false;
                	for (int j=0;j<this.gIP.length;j++){
                		if (this.gIP[j].motors != null) {
                			hasMotors=true;
                			break;
                		}
                	}
2289 2290 2291
                	if (hasMotors) {
                		this.gIP[i].enabled=false; // got some no-motor images made without scanning
                	}
2292
        		}
2293

2294 2295 2296 2297 2298
        		/* Disable no-pointer, new, number of points less than required */
        		if (this.gIP[i].enabled && !wasEnabled && (this.gIP[i].matchedPointers==0) && (this.gIP[i].pixelsXY.length<minGridNodes)){
        			this.gIP[i].enabled=false;
        			notEnoughNodes++;
        		}
2299

2300 2301 2302 2303 2304
            	if (this.gIP[i].enabled) numEnabled++;
            	this.gIP[i].newEnabled=this.gIP[i].enabled&&!wasEnabled;
            	if (this.gIP[i].newEnabled) newEnabled++;
        	}
        	// may need buildImageSets
2305
        	int [] result={numEnabled,newEnabled,numNoVignetting,notEnoughNodes,disabledNoLaser};
2306 2307 2308 2309 2310 2311 2312
        	return result;
        }
// TODO:
        // 1 -  Filter by lasers/hint state
        // 2 - recalculate hinted
        // connect "enabled" to strategies (not done yet)
      //  applyHintedGrids90 - moved to the parent class
2313 2314


2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371
        /**
         * Create array of image sets ("panoramas"), sorted by timestamps
         * @return number of sets
         */
        public int buildImageSets(boolean preserveSet){
        	if (this.debugLevel>0) {
        		System.out.println("buildImageSets("+preserveSet+")");
        	}
        	if (!preserveSet){
        		List <Double> timeStampList=new ArrayList<Double>(this.gIP.length);
        		int numChannels=0;
        		for (int i=0;i<this.gIP.length;i++) {
        			if (this.gIP[i].channel>numChannels) numChannels=this.gIP[i].channel;
        			int j=0;
        			Double ts=this.gIP[i].timestamp;
        			if (!timeStampList.contains(ts)){
        				for (;(j<timeStampList.size()) && (ts>timeStampList.get(j));j++);
        				timeStampList.add(j,ts);
        			}
        		}
        		numChannels++;
        		this.gIS=new GridImageSet[timeStampList.size()];
        		for (int i=0;i<this.gIS.length;i++){
        			this.gIS[i]=new GridImageSet();
        			this.gIS[i].timeStamp=timeStampList.get(i);
        			this.gIS[i].imageSet=new GridImageParameters [numChannels];
        			for (int j=0;j<numChannels;j++) this.gIS[i].imageSet[j]=null;

        		}
        		for (int i=0;i<this.gIP.length;i++) {
        			Double ts=this.gIP[i].timestamp;
        			int iIS=timeStampList.indexOf(ts);
        			this.gIS[iIS].setStationNumber(this.gIP[i].getStationNumber());
        			this.gIS[iIS].imageSet[this.gIP[i].channel]=this.gIP[i];
//        			if (this.gIP[i].motors!=null) this.gIS[iIS].motors=this.gIP[i].motors;
        			this.gIP[i].setNumber=iIS;
        			this.gIP[i].gridImageSet=this.gIS[iIS];
        		}
        		// verify that station number is the same for the same timestamp
        		for (int i=0;i<this.gIP.length;i++) {
        			Double ts=this.gIP[i].timestamp;
        			int iIS=timeStampList.indexOf(ts);
        			if (this.gIS[iIS].getStationNumber()!=this.gIP[i].getStationNumber()){
        				String msg="Inconsistent station number for timestamp "+ts+": this.gIS[iIS].getStationNumber()="+this.gIS[iIS].getStationNumber()+
        				" this.gIP[i].getStationNumber()="+this.gIP[i].getStationNumber()+", using "+this.gIS[iIS].getStationNumber();
        				System.out.println(msg);
        				IJ.showMessage("Error:",msg);
        				this.gIP[i].setStationNumber(this.gIS[iIS].getStationNumber());
        			}
        		}
        	}
    		for (int i=0;i<this.gIP.length;i++) {
    			int iIS=this.gIP[i].setNumber;
    			if (this.gIP[i].motors!=null) this.gIS[iIS].motors=this.gIP[i].motors.clone();
    		}
        	return this.gIS.length;
        }
2372

2373 2374 2375 2376 2377
        /**
         * Create array of image sets ("panoramas"), sorted by timestamps
         * @param all // use all images (false - only enabled)
         * @return number of sets
         */
2378

2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411
        public int buildImageSetsOld(boolean all){
        	List <Double> timeStampList=new ArrayList<Double>(this.gIP.length);
        	int numChannels=0;
        	for (int i=0;i<this.gIP.length;i++) if (all || this.gIP[i].enabled){
        		if (this.gIP[i].channel>numChannels) numChannels=this.gIP[i].channel;
        		int j=0;
        		Double ts=this.gIP[i].timestamp;
        		if (!timeStampList.contains(ts)){
        			for (;(j<timeStampList.size()) && (ts>timeStampList.get(j));j++);
        			timeStampList.add(j,ts);
        		}
        	}
        	numChannels++;
        	this.gIS=new GridImageSet[timeStampList.size()];
        	for (int i=0;i<this.gIS.length;i++){
        		this.gIS[i]=new GridImageSet();
        		this.gIS[i].timeStamp=timeStampList.get(i);
        		this.gIS[i].imageSet=new GridImageParameters [numChannels];
        		for (int j=0;j<numChannels;j++) this.gIS[i].imageSet[j]=null;

        	}
        	for (int i=0;i<this.gIP.length;i++) if (all || this.gIP[i].enabled){
        		Double ts=this.gIP[i].timestamp;
        		int iIS=timeStampList.indexOf(ts);
        		this.gIS[iIS].imageSet[this.gIP[i].channel]=this.gIP[i];
        		if (this.gIP[i].motors!=null) this.gIS[iIS].motors=this.gIP[i].motors;
        	}
        	return this.gIS.length;
        }

        /**
         * Set goniometer initial orientation from the image with maximal number of laser pointers (make averaging later?)
         * Needed before LMA to have some reasonable initial orientation
2412
         * @param overwriteAll if true, overwrite orientation data even if it is already not NaN, false -skip those that have orientation set
2413
         */
2414 2415 2416
        public void setInitialOrientation(
        		PatternParameters  patternParameters,
        		boolean            overwriteAll) {
2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432
			if (this.debugLevel>0) {
				System.out.println("setInitialOrientation("+overwriteAll+"), debugLevel= "+this.debugLevel);
			}

        	for (int i=0; i<this.gIS.length;i++){
        		int stationNumber=this.gIS[i].getStationNumber();
        		int bestRating=-1;
        		int bestChannel=-1;
        		for (int j=0;j<this.gIS[i].imageSet.length;j++) if ((this.gIS[i].imageSet[j]!=null) && this.gIS[i].imageSet[j].enabled){
        			int thisRating=this.gIS[i].imageSet[j].matchedPointers+((this.gIS[i].imageSet[j].hintedMatch>0)?1:0); // rate hintedMatch 2 higher?
        			if (thisRating>bestRating) {
        				bestRating=thisRating;
        				bestChannel=j;
        			}
        		}
        		if (bestRating>0){
2433
        			EyesisSubCameraParameters esp = this.eyesisCameraParameters.eyesisSubCameras[stationNumber][bestChannel];
2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497
        			double [] uv_center = getGridUVfromXY(
        	        		esp.px0, // final double px,
        	        		esp.py0, // final double py,
        	        		this.gIS[i].imageSet[bestChannel].getImageNumber(), //  final int fileNumber,
        	        		true); // boolean use_extra)
// find UV of the center of the image getImageNumber
        			if (uv_center == null) {
        				if (this.debugLevel>0) {
        					System.out.println("Center UV = NULL");
        				}
        			} else {
        				if (this.debugLevel>0) {
        					System.out.println("Center UV = "+uv_center[0]+","+uv_center[1]);
        				}
        				double [] patt_xyz = 		patternParameters.getXYZ(
        						uv_center, // double [] uv,
        						false, // boolean verbose,
        						this.gIS[i].getStationNumber()); // int station); // u=0,v=0 - center!

            			if (patt_xyz == null) {
            				if (this.debugLevel>0) {
            					System.out.println("Center UV = NULL");
            				}
            			} else {
            				if (this.debugLevel>0) {
            					System.out.println("Center XYZ = "+patt_xyz[0]+","+patt_xyz[1]+","+patt_xyz[2]);
            				}
// Calculate position relative to the view point on the target
            				double [] aview = {
            						patt_xyz[0]- this.gIS[i].GXYZ[0],
            						-patt_xyz[1]+ (this.gIS[i].GXYZ[1] - this.gIS[i].centerAboveHorizontal),
            						-patt_xyz[2]+ this.gIS[i].GXYZ[2]
            				};
            				Matrix mview = new Matrix(aview,3);
            				double phi = -Math.PI/180.0*this.gIS[i].horAxisErrPhi;
            				double cp = Math.cos(phi);
            				double sp = Math.sin(phi);
            				double [][] aphi = {
            						{ cp, 0.0, sp},
            						{0.0, 1.0, 0.0},
            						{-sp, 0.0, cp}};
            				Matrix mphi = new Matrix(aphi);
            				Matrix mview_gon = mphi.times(mview); // view point on the target from the goniometer
            				double tilt = -Math.atan2(mview_gon.get(1, 0), mview_gon.get(2, 0)); // y pointed up
            				double ct =  Math.cos(tilt);
            				double st = Math.sin(tilt);

            				double [][] atilt = {
            						{1.0, 0.0, 0.0},
            						{0.0,  ct,  st},
            						{0.0, -st,  ct}};
            				Matrix mtilt = new Matrix(atilt);
            				Matrix mview_tilt = mtilt.times(mview_gon); // view point on the target from the tilted goniometer
            				double az = Math.atan2(mview_tilt.get(0, 0), mview_tilt.get(2, 0)); // x pointed right

            				double tilt_deg = tilt/Math.PI*180;
            				double az_deg =   az/Math.PI*180;
            				if (this.debugLevel>0) {
            					System.out.println("Tilt = "+tilt_deg+", az = "+az_deg);
            					System.out.print("");
            				}
            			}
        			}

2498 2499
        			if (overwriteAll || Double.isNaN(this.gIS[i].goniometerAxial)){
 //       				System.out.println("setInitialOrientation("+overwriteAll+"),  Double.isNaN(this.gIS["+i+"].goniometerAxial)="+Double.isNaN(this.gIS[i].goniometerAxial));
2500

2501 2502
        				double subcam_heading = (esp.heading + (esp.cartesian? 0: esp.azimuth));
        				this.gIS[i].goniometerAxial=-subcam_heading;
2503 2504 2505
        				for (int j=0;j<this.gIS[i].imageSet.length;j++) if (this.gIS[i].imageSet[j]!=null) setGA(this.gIS[i].imageSet[j].imgNumber,this.gIS[i].goniometerAxial);
            			this.gIS[i].orientationEstimated=true;
            			if (this.debugLevel>1) {
2506 2507 2508
            				System.out.print(String.format("Setting goniometerAxial for the image set #%4d (%18.6f) to ", i, this.gIS[i].timeStamp));
            				System.out.println(""+this.gIS[i].goniometerAxial+" +++++ orientationEstimated==true +++++");
//            				System.out.println("Setting goniometerAxial for the image set #"+i+" ("+this.gIS[i].timeStamp+") to "+this.gIS[i].goniometerAxial+" +++++ orientationEstimated==true +++++");
2509 2510 2511 2512
            			}
        			}
        			if (overwriteAll || Double.isNaN(this.gIS[i].goniometerTilt )){
//        				System.out.println("setInitialOrientation("+overwriteAll+"),  Double.isNaN(this.gIS["+i+"].goniometerTilt)="+Double.isNaN(this.gIS[i].goniometerTilt));
2513
        				this.gIS[i].goniometerTilt= -esp.theta;
2514 2515 2516
        				for (int j=0;j<this.gIS[i].imageSet.length;j++) if (this.gIS[i].imageSet[j]!=null) setGH(this.gIS[i].imageSet[j].imgNumber,this.gIS[i].goniometerTilt);
            			this.gIS[i].orientationEstimated=true;
            			if (this.debugLevel>1) {
2517 2518
            				System.out.print(String.format("Setting goniometerTilt  for the image set #%4d (%18.6f) to ", i, this.gIS[i].timeStamp));
            				System.out.println(""+this.gIS[i].goniometerTilt+" ===== orientationEstimated==true =====");
2519 2520 2521 2522 2523 2524
            			}
        			}
        		}
        	}
        }
        /**
2525
         * update image set (panorama, set of simultaneous images) goniometer orientation from the image parameters, do after running LMA
2526 2527 2528 2529 2530 2531 2532
         * @param selectedImages boolean array of selected images (in current strategy) or null (all selected)
         */
// TODO: potential problem here if only some images were enabled in the strategy -- FIXED
// TODO: Add other extrinsic parameters here to sets?
        /**
         * Updated version - only flag as orientationEstimated if no enabled images exist in the set or any of the angles is NaN
         * Temporarily duplicate  image parameters from those of the set (should not be needed)
2533
         * selectedImages will not be used
2534 2535 2536 2537 2538 2539 2540
         */
        public void updateSetOrientation(boolean [] selectedImages){ // if selectedImages[] is not null will set orientationEstimated for unselected images
        	if (this.gIS==null){
        		String msg="Image set is not initilaized";
        		System.out.println(msg);
        		IJ.showMessage(msg);
        	}
2541

2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554
        	for (int i=0; i<this.gIS.length;i++){
        		this.gIS[i].orientationEstimated=true;
        		if (!Double.isNaN(this.gIS[i].goniometerAxial) && !Double.isNaN(this.gIS[i].goniometerTilt)) {
        			for (int j=0;j<this.gIS[i].imageSet.length;j++) if ((this.gIS[i].imageSet[j]!=null) && this.gIS[i].imageSet[j].enabled){
        				if ((selectedImages==null) || selectedImages[this.gIS[i].imageSet[j].imgNumber]) {
        					this.gIS[i].goniometerAxial-=360.0*Math.floor((this.gIS[i].goniometerAxial+180.0)/360.0);
        					this.gIS[i].orientationEstimated=false;
        					break; // set from the first non-null, enabled image
        				}
        			}
        		}
        		if (!this.gIS[i].orientationEstimated){
        			// now fill that data to all disabled images of the same set (just for listing RMS errors and debugging)
2555
        			for (int j=0;j<this.gIS[i].imageSet.length;j++) if (this.gIS[i].imageSet[j]!=null) { // fill even those that are enabled
2556 2557 2558 2559 2560 2561 2562 2563 2564 2565
        				setGA(this.gIS[i].imageSet[j].imgNumber,this.gIS[i].goniometerAxial );
        				setGH(this.gIS[i].imageSet[j].imgNumber,this.gIS[i].goniometerTilt );
        			}
        		} else {
        			this.gIS[i].goniometerAxial=Double.NaN;
        			this.gIS[i].goniometerTilt= Double.NaN;
        			System.out.println("updateSetOrientation(): imageSet "+i+" orientationEstimated == true");
        		}
        	}
        }
2566

2567 2568 2569 2570 2571 2572 2573
        public void updateSetOrientationOld(boolean [] selectedImages){
        	if (this.gIS==null){
        		String msg="Image set is not initilaized";
        		System.out.println(msg);
        		IJ.showMessage(msg);
        	}
        	for (int i=0; i<this.gIS.length;i++){
2574
        		if (selectedImages==null){ // if all selected - remove orientation if there are no enabled images (i.e. after removeOutliers)
2575 2576 2577
    				this.gIS[i].goniometerAxial=Double.NaN;
    				this.gIS[i].goniometerTilt= Double.NaN;
    				this.gIS[i].orientationEstimated=true;
2578

2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590
        		}
        		for (int j=0;j<this.gIS[i].imageSet.length;j++) if ((this.gIS[i].imageSet[j]!=null) && this.gIS[i].imageSet[j].enabled){
        			if ((selectedImages==null) || selectedImages[this.gIS[i].imageSet[j].imgNumber]) {
        				this.gIS[i].goniometerAxial=getGA(this.gIS[i].imageSet[j].imgNumber);  //update - most likely will do nothing (if set has non-NaN)
        				this.gIS[i].goniometerTilt= getGH(this.gIS[i].imageSet[j].imgNumber);
        				this.gIS[i].goniometerAxial-=360.0*Math.floor((this.gIS[i].goniometerAxial+180.0)/360.0);
        				this.gIS[i].orientationEstimated=false;
        				break; // set from the first non-null, enabled image
        			}
        		}
        		// now fill that data to all disabled images of the same set (just for listing RMS errors and debugging)
        		if (!Double.isNaN(this.gIS[i].goniometerAxial) && !Double.isNaN(this.gIS[i].goniometerTilt)){
2591
        			for (int j=0;j<this.gIS[i].imageSet.length;j++) if (this.gIS[i].imageSet[j]!=null) { // fill even those that are enabled
2592 2593 2594 2595 2596 2597
        				setGA(this.gIS[i].imageSet[j].imgNumber,this.gIS[i].goniometerAxial );
        				setGH(this.gIS[i].imageSet[j].imgNumber,this.gIS[i].goniometerTilt );
        			}
        		}
        	}
        }
2598

2599 2600 2601 2602 2603 2604 2605
        public boolean isEstimated(int imgNum){
        	if (this.gIS==null) {
            	String msg="Image sets are not initialized";
        		IJ.showMessage("Error",msg);
        		throw new IllegalArgumentException (msg);
        	}
        	if (this.gIP[imgNum].gridImageSet!=null) return this.gIP[imgNum].gridImageSet.orientationEstimated;
2606
        	// should not get here
2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631
        	System.out.println("FIXME: isEstimated("+imgNum+"): this.gIP["+imgNum+"].gridImageSet==null");
        	for (int i=0;i<this.gIS.length;i++)	if (this.gIS[i].imageSet!=null){
        		for (int j=0;j<this.gIS[i].imageSet.length;j++) if ((this.gIS[i].imageSet[j]!=null) && (this.gIS[i].imageSet[j].imgNumber==imgNum)){
        			return this.gIS[i].orientationEstimated;
        		}
        	}
        	String msg="Image with index "+imgNum+" is not in the image set";
    		IJ.showMessage("Error",msg);
    		throw new IllegalArgumentException (msg);
        }
        public boolean isEstimatedOld(int imgNum){
        	if (this.gIS==null) {
            	String msg="Image sets are not initialized";
        		IJ.showMessage("Error",msg);
        		throw new IllegalArgumentException (msg);
        	}
        	for (int i=0;i<this.gIS.length;i++)	if (this.gIS[i].imageSet!=null){
        		for (int j=0;j<this.gIS[i].imageSet.length;j++) if ((this.gIS[i].imageSet[j]!=null) && (this.gIS[i].imageSet[j].imgNumber==imgNum)){
        			return this.gIS[i].orientationEstimated;
        		}
        	}
        	String msg="Image with index "+imgNum+" is not in the image set";
    		IJ.showMessage("Error",msg);
    		throw new IllegalArgumentException (msg);
        }
Andrey Filippov's avatar
Andrey Filippov committed
2632
        public int getNumberOfEstimated(boolean enabledOnly) {
2633 2634 2635 2636 2637
        	int numEstimated=0;
        	if (this.gIS==null) return 0;
        	for (int i=0;i<this.gIS.length;i++)	if (this.gIS[i].imageSet!=null){
        		for (int j=0;j<this.gIS[i].imageSet.length;j++) if (this.gIS[i].imageSet[j]!=null) {
        			if ((!enabledOnly || this.gIS[i].imageSet[j].enabled) && this.gIS[i].orientationEstimated) numEstimated++;
2638

2639 2640 2641 2642 2643
        		}
        	}
        	return numEstimated;
        }

Andrey Filippov's avatar
Andrey Filippov committed
2644
        public int [] getNumberOfEstimatedPerStation(boolean enabledOnly) {
2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656
        	int [] numEstimated=new int [this.eyesisCameraParameters.numStations];
        	for (int i=0;i<numEstimated.length;i++) numEstimated[i]=0;
        	if (this.gIS!=null){
        		for (int i=0;i<this.gIS.length;i++)	if (this.gIS[i].imageSet!=null){
        			for (int j=0;j<this.gIS[i].imageSet.length;j++) if (this.gIS[i].imageSet[j]!=null) {
        				if ((!enabledOnly || this.gIS[i].imageSet[j].enabled) && this.gIS[i].orientationEstimated) numEstimated[this.gIS[i].getStationNumber()]++;
        			}
        		}
        	}
        	return numEstimated;
        }

2657

Andrey Filippov's avatar
Andrey Filippov committed
2658
        public int getNumEnabled(){
2659 2660 2661 2662 2663
        	int num=0;
        	for (int i=0;i<this.gIP.length;i++) if ((this.gIP[i]!=null) && this.gIP[i].enabled) num++;
        	return num;
        }

Andrey Filippov's avatar
Andrey Filippov committed
2664
        public int getNumNewEnabled(){
2665 2666 2667 2668 2669
        	int num=0;
        	for (int i=0;i<this.gIP.length;i++) if ((this.gIP[i]!=null) && this.gIP[i].enabled && this.gIP[i].newEnabled) num++;
        	return num;
        }

Andrey Filippov's avatar
Andrey Filippov committed
2670
        public int [] getNumNewEnabledPerStation(){
2671 2672 2673 2674 2675 2676
        	int [] numEnabled=new int [this.eyesisCameraParameters.numStations];
        	for (int i=0;i<numEnabled.length;i++) numEnabled[i]=0;
        	for (int i=0;i<this.gIP.length;i++) if ((this.gIP[i]!=null) && this.gIP[i].enabled && this.gIP[i].newEnabled) numEnabled[this.gIP[i].getStationNumber()]++;//  OOB 837
        	return numEnabled;
        }

Andrey Filippov's avatar
Andrey Filippov committed
2677
        public int [] getStations(){
2678 2679 2680 2681
        	int [] result = new int [this.gIP.length];
        	for (int i=0;i<result.length;i++) result[i]=(this.gIP[i]!=null)?this.gIP[i].stationNumber:-1;
        	return result;
        }
Andrey Filippov's avatar
Andrey Filippov committed
2682
        public int [] getChannels(){
2683 2684 2685 2686
        	int [] result = new int [this.gIP.length];
        	for (int i=0;i<result.length;i++) result[i]=(this.gIP[i]!=null)?this.gIP[i].channel:-1;
        	return result;
        }
Andrey Filippov's avatar
Andrey Filippov committed
2687
        public int [] getMatchedPointers(){
2688 2689 2690 2691
        	int [] result = new int [this.gIP.length];
        	for (int i=0;i<result.length;i++) result[i]=(this.gIP[i]!=null)?this.gIP[i].matchedPointers:0;
        	return result;
        }
Andrey Filippov's avatar
Andrey Filippov committed
2692
        public int [] getHintedMatch(){
2693 2694 2695 2696
        	int [] result = new int [this.gIP.length];
        	for (int i=0;i<result.length;i++) result[i]=(this.gIP[i]!=null)?this.gIP[i].hintedMatch:-1;
        	return result;
        }
2697

Andrey Filippov's avatar
Andrey Filippov committed
2698
        public boolean [] selectNewEnabled () {
2699 2700 2701 2702 2703
        	boolean [] newEnabled=new boolean [this.gIP.length] ;
        	for (int i=0;i<this.gIP.length;i++) newEnabled[i]= (this.gIP[i]!=null) && this.gIP[i].enabled && this.gIP[i].newEnabled;
        	return newEnabled;
        }

Andrey Filippov's avatar
Andrey Filippov committed
2704
        public boolean [] selectEnabled () {
2705 2706 2707 2708 2709
        	boolean [] enabled=new boolean [this.gIP.length] ;
        	for (int i=0;i<this.gIP.length;i++) enabled[i]= (this.gIP[i]!=null) && this.gIP[i].enabled;
        	return enabled;
        }

Andrey Filippov's avatar
Andrey Filippov committed
2710
        public boolean [] selectEstimated (boolean enabledOnly) {
2711
        	boolean [] estimated=new boolean [getNumImages()];
2712 2713 2714
        	if (this.gIS==null) {
            	String msg="Image sets are not initialized";
        		IJ.showMessage("Error",msg);
2715 2716 2717
//        		throw new IllegalArgumentException (msg);
        		Arrays.fill(estimated, true);
            	return estimated;
2718
        	}
2719

2720 2721 2722 2723
        	for (int i=0;i<estimated.length;i++) estimated[i]=false;
        	for (int i=0;i<this.gIS.length;i++)	if (this.gIS[i].imageSet!=null){
        		for (int j=0;j<this.gIS[i].imageSet.length;j++) if (this.gIS[i].imageSet[j]!=null) {
        			if ((!enabledOnly || this.gIS[i].imageSet[j].enabled) ) estimated[this.gIS[i].imageSet[j].imgNumber]= this.gIS[i].orientationEstimated;
2724

2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748
        		}
        	}
        	return estimated;
        }
        public void enableSelected(boolean [] selected){
        	for (int i=0;i<this.gIP.length  ;i++) if (this.gIP[i]!=null){
        		int i1=i;
        		if (i1>=selected.length) i1=selected.length-1;
        		this.gIP[i].enabled = selected[i1];
        	}
        }
        /**
         * Calculate goniometer orientation for one of the "known" images/grids
         * @param imgNum grid image number
         * @return pair of {goniometerHorizontal, goniometerAxial} (in angular degrees)
         */
        public double [] getImagesetTiltAxial(int imgNum){
        	return getImagesetTiltAxial(this.gIP[imgNum].timestamp);
        }
        /**
         * Return pair of {goniometerHorizontal, goniometerAxial} for the specified timestamp
         * updateSetOrientation() should be called after LMA or other updates to camera parameters
         * @param timeStamp - double timestamp identifying imageset (image does not need to be a part of selected grid files)
         * @return null if no images set has the specified timestamp, may contain Double.NaN if the orientation was not set.
2749
         * Now 3-rd term - interAxisAngle - with goniometerTilt it is used for correction of non-pure axial movement of the camera.
2750 2751
         */
        public double [] getImagesetTiltAxial(double timeStamp){
2752
        	int mAxial=1;     // m2
2753 2754 2755 2756 2757 2758 2759 2760
        	int mHorizontal=2;// m3
        	// this is probably already set
        	for (int i=0;i<this.gIS.length;i++){
        		if ((this.gIS[i].imageSet!=null) && (this.gIS[i].imageSet.length>0) && (this.gIS[i].imageSet[0]!=null)) this.gIS[i].setStationNumber(this.gIS[i].imageSet[0].getStationNumber());
            }
        	for (int i=0;i<this.gIS.length;i++)
        		if (this.gIS[i].timeStamp==timeStamp) {
    				int iBest=i;
2761
        			if (Double.isNaN(this.gIS[i].goniometerTilt) || Double.isNaN(this.gIS[i].goniometerAxial)  || Double.isNaN(this.gIS[i].interAxisAngle)) {
2762 2763 2764
// find the closest one (by motors)
        				if (this.gIS[i].motors==null) {
                			if (this.debugLevel>0) System.out.println("getImagesetTiltAxial("+timeStamp+"): No motor data");
2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775
                			if (this.debugLevel>0) System.out.println("Looking for closest timestamps in the same station, image set = "+i);
                			int early_set=-1;
                			int late_set=-1;
                			for (int j=0; j<this.gIS.length;j++) {
                    			if (Double.isNaN(this.gIS[j].goniometerTilt) || Double.isNaN(this.gIS[j].goniometerAxial)  || Double.isNaN(this.gIS[j].interAxisAngle)) continue;
                    			if (this.gIS[j].timeStamp > timeStamp){
                    				if ((late_set<0) || (this.gIS[j].timeStamp < this.gIS[late_set].timeStamp)) late_set = j;
                    			} else {
                    				if ((early_set<0) || (this.gIS[j].timeStamp >this.gIS[early_set].timeStamp)) early_set = j;
                    			}
                			}
2776

2777 2778 2779 2780
                			if ((late_set <0) && (early_set<0)) {
                    			if (this.debugLevel>0) System.out.println("Failed to find any known orientation");
                    			return null;
                			}
Andrey Filippov's avatar
Andrey Filippov committed
2781 2782 2783 2784 2785
                			if       (late_set <0) {
                				iBest= early_set;
                			} else if  (early_set <0) {
                				iBest= late_set;
                			} else {
2786 2787 2788 2789 2790 2791 2792 2793 2794 2795
                				// interpolate
            					double axialEarly=this.gIS[early_set].goniometerAxial;
            					double axialLate= this.gIS[late_set].goniometerAxial;
            					axialEarly-=360.0*Math.floor((axialEarly+180.0)/360.0); // convert to range +/-180
            					axialLate-= 360.0*Math.floor((axialLate+ 180.0)/360.0);
            					double axialCenter= 0.5*(axialEarly+axialLate);
            					if (Math.abs(axialEarly-axialLate)>180) {
            						if (axialCenter>0) axialCenter-=180.0;
            						else axialCenter+=180.0;
            					}
2796

2797 2798 2799 2800 2801 2802 2803 2804 2805
            					double interEarly=this.gIS[early_set].interAxisAngle;
            					double interLate= this.gIS[late_set].interAxisAngle;
            					interEarly-=360.0*Math.floor((interEarly+180.0)/360.0);
            					interLate-= 360.0*Math.floor((interLate+ 180.0)/360.0);
            					double interCenter= 0.5*(interEarly+interLate);
            					if (Math.abs(interEarly-interLate)>180) {
            						if (interCenter>0) interCenter-=180.0;
            						else interCenter+=180.0;
            					}
2806

2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837
            					double tiltEarly=this.gIS[early_set].goniometerTilt;
            					double tiltLate= this.gIS[late_set].goniometerTilt;
            					tiltEarly-=360.0*Math.floor((tiltEarly+180.0)/360.0);
            					tiltLate-= 360.0*Math.floor((tiltLate+ 180.0)/360.0);
            					double tiltCenter= 0.5*(tiltEarly+tiltLate);
            					if (Math.abs(tiltEarly-tiltLate)>180) {
            						if (tiltCenter>0) tiltCenter-=180.0;
            						else tiltCenter+=180.0;
            					}
            					this.gIS[i].goniometerTilt= tiltCenter;
            					this.gIS[i].goniometerAxial=axialCenter;
            					this.gIS[i].interAxisAngle=interCenter;
                				if (this.debugLevel>2) System.out.println("getImagesetTiltAxial("+timeStamp+"):"+
                						" axialEarly - "+ axialEarly+
                						" axialLate - "+  axialLate+
                						" axialCenter - "+axialCenter+
                						" tiltEarly - "+  tiltEarly+
                						" tiltLate - "+   tiltLate+
                						" tiltCenter - "+ tiltCenter+
                						" interEarly - "+ interEarly+
                						" interLate - "+  interLate+
                						" interCenter - "+interCenter);
                			}
                			if (iBest!=i) {
                			// use closest
                				this.gIS[i].goniometerTilt= this.gIS[iBest].goniometerTilt;
                				this.gIS[i].goniometerAxial=this.gIS[iBest].goniometerAxial;
                				this.gIS[i].interAxisAngle=this.gIS[iBest].interAxisAngle;
                			}
            				this.gIS[i].orientationEstimated=true;
                			double [] result = {
Andrey Filippov's avatar
Andrey Filippov committed
2838 2839 2840 2841 2842 2843
//                					this.gIS[iBest].goniometerTilt,
//                					this.gIS[iBest].goniometerAxial,
///                					this.gIS[iBest].interAxisAngle
                					this.gIS[i].goniometerTilt,
                					this.gIS[i].goniometerAxial,
                					this.gIS[i].interAxisAngle
2844 2845
                			};
        					return result;
2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863
        				}
// Maybe later use both motors, for now - just the axial. It seems to have <0.5 degree error (but accumulates gradually as there are friction rollers involved).
        				int thisMotorHorizontal=this.gIS[i].motors[mHorizontal];
        				int thisMotorAxial=     this.gIS[i].motors[mAxial];
        				int stationNumber=      this.gIS[i].getStationNumber();
            			ArrayList<Integer> setList=new ArrayList<Integer>(100);
            			for (int j=0;j<this.gIS.length;j++) {
            				if (this.gIS[j]==null){
            					System.out.println("BUG?: getImagesetTiltAxial("+timeStamp+"): this.gIS["+j+"]==null");
            					continue;
            				}
            				if (this.gIS[j].motors==null){
            					System.out.println("BUG?: getImagesetTiltAxial("+timeStamp+"): this.gIS["+j+"].motors==null");
            					continue;
            				}
            				if ( //   (j!=i)  && // not needed - this set does not have orientation
            						(this.gIS[j].getStationNumber()==stationNumber) &&
            						(this.gIS[j].motors[mHorizontal]==thisMotorHorizontal) &&
Andrey Filippov's avatar
Andrey Filippov committed
2864
            						!this.gIS[j].orientationEstimated && // new
2865
            						!Double.isNaN(this.gIS[j].goniometerTilt) &&
2866 2867
            						!Double.isNaN(this.gIS[j].goniometerAxial) &&
            						!Double.isNaN(this.gIS[j].interAxisAngle)){
Andrey Filippov's avatar
Andrey Filippov committed
2868
            					System.out.println("Found image set "+j+" with timestamp "+ timeStamp + " with known attitude to estimate this attitude");
2869 2870 2871 2872
            					setList.add(new Integer(j));
            				}
            			}
            			if (setList.size()>=2){
Andrey Filippov's avatar
Andrey Filippov committed
2873 2874
//            				if (this.debugLevel>2) System.out.println("getImagesetTiltAxial("+timeStamp+"): estimating orientation for set # "+i+": this.debugLevel="+this.debugLevel);
            				if (this.debugLevel > -1) System.out.println("getImagesetTiltAxial("+timeStamp+"): estimating orientation for set # "+i+": this.debugLevel="+this.debugLevel);
2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885
            				// find the closest one
            				int indexClosest=setList.get(0);
            				double dClosest=Math.abs(this.gIS[indexClosest].motors[mAxial]-thisMotorAxial);
            				for (int j=1;j<setList.size();j++) if (Math.abs(this.gIS[setList.get(j)].motors[mAxial]-thisMotorAxial)<dClosest){
            					indexClosest=setList.get(j);
            					dClosest=Math.abs(this.gIS[indexClosest].motors[mAxial]-thisMotorAxial);
            				}
            				// try to get the second on the other side than the closest first
            				int indexSecond=-1;
            				for (int j=0;j<setList.size();j++) {
            					if (((this.gIS[indexClosest].motors[mAxial]-thisMotorAxial)*
2886
            							(this.gIS[setList.get(j)].motors[mAxial]-thisMotorAxial)<0) && // different side
2887 2888 2889 2890
            							((indexSecond<0) || (Math.abs(this.gIS[setList.get(j)].motors[mAxial]-thisMotorAxial)<dClosest))){
            						indexSecond=setList.get(j);
            						dClosest=Math.abs(this.gIS[indexSecond].motors[mAxial]-thisMotorAxial);
            					}
2891
            				}
2892 2893 2894 2895 2896 2897 2898 2899
            				if (this.debugLevel>2) System.out.println("indexSecond="+indexSecond);
            				if (indexSecond<0){ // no sets on the opposite side from the indexClosest, use second closest on the same side as indexClosest
                				for (int j=0;j<setList.size();j++) {
                					if ((setList.get(j)!=indexClosest) &&
                							((indexSecond<0) || (Math.abs(this.gIS[setList.get(j)].motors[mAxial]-thisMotorAxial)<dClosest))){
                						indexSecond=setList.get(j);
                						dClosest=Math.abs(this.gIS[indexSecond].motors[mAxial]-thisMotorAxial);
                					}
2900 2901
                				}

2902 2903 2904 2905 2906 2907 2908
            				}
            				if (indexSecond<0){ // no second sets at all
            					System.out.println("getImagesetTiltAxial("+timeStamp+") - this is a BUG ");
            				} else {
            					// now linear interpolate axail between theses two sets: indexClosest and indexSecond. (resolve/ guess crossing 360
            					double axialClosest=this.gIS[indexClosest].goniometerAxial;
            					double axialSecond= this.gIS[indexSecond].goniometerAxial;
2909 2910
            					double interClosest=this.gIS[indexClosest].interAxisAngle;
            					double interSecond= this.gIS[indexSecond].interAxisAngle;
2911 2912 2913 2914 2915 2916
            					axialClosest-=360.0*Math.floor((axialClosest+180.0)/360.0);
            					axialSecond-= 360.0*Math.floor((axialSecond+ 180.0)/360.0);
                				if (this.debugLevel>2) System.out.println("getImagesetTiltAxial("+timeStamp+"):"+
                						" same tilt - "+setList.size()+
                						" axialClosest="+axialClosest+
                						" axialSecond="+axialSecond+
2917 2918
                						" interClosest="+interClosest+
                						" interSecond="+interSecond+
2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931
                						" motor closest="+this.gIS[indexClosest].motors[mAxial]+
                						" motor second="+this.gIS[indexSecond].motors[mAxial]);
            					// axial motor has the same sign/direction as the axial angle
            					if (this.gIS[indexSecond].motors[mAxial]>this.gIS[indexClosest].motors[mAxial]){
            						if (axialSecond<axialClosest) axialSecond+=360.0;
            					} else {
            						if (axialSecond>axialClosest) axialClosest+=360.0;
            					}
            					this.gIS[i].goniometerAxial=
            						axialClosest+
            						(axialSecond-axialClosest)*
            						(thisMotorAxial-this.gIS[indexClosest].motors[mAxial])/
            						(this.gIS[indexSecond].motors[mAxial]-this.gIS[indexClosest].motors[mAxial]);
2932 2933 2934 2935 2936
            					this.gIS[i].interAxisAngle=
            							interClosest+
                						(interSecond-interClosest)*
                						(thisMotorAxial-this.gIS[indexClosest].motors[mAxial])/
                						(this.gIS[indexSecond].motors[mAxial]-this.gIS[indexClosest].motors[mAxial]);
2937 2938 2939 2940 2941
            					this.gIS[i].goniometerTilt=
            						this.gIS[indexClosest].goniometerTilt+
            						(this.gIS[indexSecond].goniometerTilt-this.gIS[indexClosest].goniometerTilt)*
            						(thisMotorAxial-this.gIS[indexClosest].motors[mAxial])/
            						(this.gIS[indexSecond].motors[mAxial]-this.gIS[indexClosest].motors[mAxial]);
2942 2943 2944 2945 2946
            					// 06/06/2015 Andrey: Was missing setting estimated orientation. Was it a bug?
                    			this.gIS[i].orientationEstimated=true;
                    			if (this.debugLevel>0) System.out.println("Orientation for set # "+i+" timestamp "+IJ.d2s(this.gIS[i].timeStamp,6)+
                    					") is not defined, using interpolated between sets # "+indexClosest+" (timestamp "+IJ.d2s(this.gIS[indexClosest].timeStamp,6)+") "+
                    					"and # "+indexSecond+" (timestamp "+IJ.d2s(this.gIS[indexSecond].timeStamp,6)+")");
2947
            				}
Andrey Filippov's avatar
Andrey Filippov committed
2948 2949 2950 2951
//            			} else if (setList.size() >= 1){
//            				if (this.debugLevel > -1) System.out.println("getImagesetTiltAxial("+timeStamp+
//            						"): estimating orientation for set # "+i+" from a single set "+setList.get(0)+": this.debugLevel="+this.debugLevel);

2952
            			} else { // old way
Andrey Filippov's avatar
Andrey Filippov committed
2953 2954
            				// first try for the same station number only:
            				iBest = -1;
2955
            				double d2Min=-1;
Andrey Filippov's avatar
Andrey Filippov committed
2956 2957 2958 2959 2960 2961 2962 2963
            				int station_number = this.gIS[i].getStationNumber();
            				for (int j=0;j<this.gIS.length;j++) if ((j!=i) &&
            						(this.gIS[j].getStationNumber() == station_number) &&
            						!this.gIS[j].orientationEstimated &&
            						(this.gIS[j].motors!=null) &&
            						!Double.isNaN(this.gIS[j].goniometerTilt) &&
            						!Double.isNaN(this.gIS[j].goniometerAxial )  &&
            						!Double.isNaN(this.gIS[j].interAxisAngle)) {
2964 2965 2966 2967 2968 2969 2970 2971 2972 2973
            					double d2=0;
            					for (int k=0;k<this.gIS[j].motors.length;k++){
            						d2+=1.0*(this.gIS[j].motors[k]-this.gIS[i].motors[k])*
            						(this.gIS[j].motors[k]-this.gIS[i].motors[k]);
            					}
            					if ((d2Min<0) || (d2Min>d2)) {
            						d2Min=d2;
            						iBest=j;
            					}
            				}
Andrey Filippov's avatar
Andrey Filippov committed
2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018
            				if (iBest < 0) {
                				d2Min=-1;
                				for (int j=0;j<this.gIS.length;j++) if ((j!=i) &&
                						(this.gIS[j].motors!=null) &&
                						!this.gIS[j].orientationEstimated &&
                						!Double.isNaN(this.gIS[j].goniometerTilt) &&
                						!Double.isNaN(this.gIS[j].goniometerAxial )  &&
                						!Double.isNaN(this.gIS[j].interAxisAngle)) {
                					double d2=0;
                					for (int k=0;k<this.gIS[j].motors.length;k++){
                						d2+=1.0*(this.gIS[j].motors[k]-this.gIS[i].motors[k])*
                						(this.gIS[j].motors[k]-this.gIS[i].motors[k]);
                					}
                					if ((d2Min<0) || (d2Min>d2)) {
                						d2Min=d2;
                						iBest=j;
                					}
                				}
                				if (iBest < 0) {
                    				d2Min=-1;
                    				for (int j=0;j<this.gIS.length;j++) if ((j!=i) &&
                    						(this.gIS[j].motors!=null) &&
                    						!Double.isNaN(this.gIS[j].goniometerTilt) &&
                    						!Double.isNaN(this.gIS[j].goniometerAxial )  &&
                    						!Double.isNaN(this.gIS[j].interAxisAngle)) {
                    					double d2=0;
                    					for (int k=0;k<this.gIS[j].motors.length;k++){
                    						d2+=1.0*(this.gIS[j].motors[k]-this.gIS[i].motors[k])*
                    						(this.gIS[j].motors[k]-this.gIS[i].motors[k]);
                    					}
                    					if ((d2Min<0) || (d2Min>d2)) {
                    						d2Min=d2;
                    						iBest=j;
                    					}
                    				}
                    				System.out.println("Used any station numer with even estimated orientation, iBest = "+iBest);
                				} else {
                    				System.out.println("Used different station numer, iBest = "+iBest);
                				}
            				} else {
                				System.out.println("Used the same station number, iBest = "+iBest);

            				}


3019 3020
            			}
        			}
Andrey Filippov's avatar
Andrey Filippov committed
3021 3022 3023 3024 3025
//        			double [] result = {
//        					this.gIS[iBest].goniometerTilt,
//        					this.gIS[iBest].goniometerAxial,
//        					this.gIS[iBest].interAxisAngle
//        			};
3026
        			if (iBest!=i){
Andrey Filippov's avatar
Andrey Filippov committed
3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038
        				boolean usable_tilt  = (this.gIS[i].motors != null);
        				boolean usable_axial = usable_tilt && (this.gIS[i].getStationNumber() == this.gIS[iBest].getStationNumber());
        				double diff_axial = usable_axial? (this.gIS[i].motors[mAxial]-this.gIS[iBest].motors[mAxial])/
        						goniometerParameters.goniometerMotors.stepsPerDegreeAxial : 0.0;
        				double diff_horizontal = usable_tilt? (this.gIS[i].motors[mHorizontal]-this.gIS[iBest].motors[mHorizontal])/
        						goniometerParameters.goniometerMotors.stepsPerDegreeTilt: 0.0;
        				this.gIS[i].goniometerTilt =  this.gIS[iBest].goniometerTilt + diff_horizontal;
        				this.gIS[i].goniometerAxial = this.gIS[iBest].goniometerAxial + diff_axial;

        				this.gIS[i].goniometerTilt-=360.0*Math.floor((this.gIS[i].goniometerTilt+180.0)/360.0);
        				this.gIS[i].goniometerAxial-=360.0*Math.floor((this.gIS[i].goniometerAxial+180.0)/360.0);

3039
            			if (this.debugLevel>0) System.out.println("Orientation for set # "+i+" timestamp "+IJ.d2s(this.gIS[i].timeStamp,6)+
Andrey Filippov's avatar
Andrey Filippov committed
3040
            					") is not defined, estimating from  # "+iBest+" (timestamp "+IJ.d2s(this.gIS[iBest].timeStamp,6)+")" );
3041
            			this.gIS[i].orientationEstimated=true;
Andrey Filippov's avatar
Andrey Filippov committed
3042 3043
//    					this.gIS[i].goniometerTilt= this.gIS[iBest].goniometerTilt;
//    					this.gIS[i].goniometerAxial=this.gIS[iBest].goniometerAxial;
3044
    					this.gIS[i].interAxisAngle=this.gIS[iBest].interAxisAngle;
3045
        			}
Andrey Filippov's avatar
Andrey Filippov committed
3046 3047 3048 3049 3050 3051
        			double [] result = {
        					this.gIS[i].goniometerTilt,
        					this.gIS[i].goniometerAxial,
        					this.gIS[i].interAxisAngle
        			};

3052 3053 3054 3055
       				return result; // may have Double.NaN
        	}
        	return null;
        }
3056

3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085
        public double getImageTimestamp(ImagePlus image){
        	if ((image.getProperty("timestamp")==null) || (((String) image.getProperty("timestamp")).length()==0)) {
        		(new JP46_Reader_camera(false)).decodeProperiesFromInfo(image);
        	}
        	return Double.parseDouble((String) image.getProperty("timestamp"));
        }

        public int getImageChannel(ImagePlus image){
        	if ((image.getProperty("channel")==null) || (((String) image.getProperty("channel")).length()==0)) {
        		(new JP46_Reader_camera(false)).decodeProperiesFromInfo(image);
        	}

        	String channelSuffix=(String) image.getProperty("channel");
        	int channel=-1;
        	for (int j=0;j<this.channelSuffixes.length;j++){
//        		System.out.println("== j="+j);
//        		System.out.println("channelSuffix="+channelSuffix);
//        		System.out.println("this.channelSuffixes[j]="+this.channelSuffixes[j]);
        		if (channelSuffix.equals(this.channelSuffixes[j])) {
        			channel=j;
        			break;
        		}
        	}
        	if (channel<0) {
        		String msg="Channel not recognized) - this channel suffix is "+channelSuffix+", available channel suffixes are:\n";
        		for (int j=0;j<this.channelSuffixes.length;j++) msg+=this.channelSuffixes[j]+", ";
        		IJ.showMessage("Error",msg);
        		throw new IllegalArgumentException (msg);
        	}
3086
        	return channel;
3087
        }
3088

3089 3090 3091 3092 3093 3094 3095
        /**
         * initialize image data with camera defaults
         * @param distortionCalibrationData grid distortionCalibrationData
         * @param eyesisCameraParameters deafault camera parameters
         * @return
         */
        // Used in Goniometer
3096
        public void initImageSet(
3097 3098 3099 3100 3101 3102 3103
        		EyesisCameraParameters eyesisCameraParameters) {
        	for (int i=0;i<this.getNumImages();i++){
        		int subCam=this.getImageSubcamera(i);
        		int stationNumber=this.getImageStation(i);
        		this.setParameters(eyesisCameraParameters.getParametersVector(stationNumber,subCam), i);
        	}
        }
3104 3105 3106



3107
// constructor from XML file
3108

3109 3110 3111 3112 3113
        public DistortionCalibrationData (
        		boolean smart,
        		String defaultPath,
        		PatternParameters patternParameters,
        		EyesisCameraParameters eyesisCameraParameters,
3114
    			EyesisAberrations.AberrationParameters aberrationParameters,
3115
    			LaserPointer laserPointers,
Andrey Filippov's avatar
Andrey Filippov committed
3116
    			Goniometer.GoniometerParameters goniometerParameters,
3117
				ImagePlus[] gridImages  ){ // null - use specified files
Andrey Filippov's avatar
Andrey Filippov committed
3118
        	this.goniometerParameters = goniometerParameters;
3119
        	setupIndices();
3120
			String [] extensions={".dcal-xml","-distcal.xml"};
3121
			MultipleExtensionsFileFilter parFilter = new MultipleExtensionsFileFilter("",extensions,"Distortion calibration *.dcal-xml files");
3122 3123 3124 3125 3126 3127 3128 3129 3130
			String pathname=CalibrationFileManagement.selectFile(
					smart,
					false,
					"Restore Calibration Parameters",
					"Restore",
					parFilter,
					defaultPath); //String defaultPath
			if ((pathname==null) || (pathname=="")) return;
//			setGridImages(gridImages);
3131 3132
//TODO: these images will be overwritten by setFromXML !!!!!!!!!
			this.gIS=null; // So readAllGrids will create it
3133 3134
        	setFromXML(
        			pathname,
3135 3136
            		eyesisCameraParameters,
        			aberrationParameters);
3137 3138 3139
			if (gridImages!=null) {
//				this.pathName="";  // modified, keep the path anyway
// overwrite saved paths with the provided images, number of images{ should match
3140 3141 3142 3143 3144 3145 3146
				if (this.gIP.length == gridImages.length){
					for (int i=0;i<this.gIP.length;i++){
						this.gIP[i].gridImage=gridImages[i];
						this.gIP[i].path=null; // not needed, just in case
						this.gIP[i].enabled=true;// enable all (actually just one) acquired images
					}
				} else {
3147 3148
					String msg="Number of provided images ("+gridImages.length+") does not match parameters restored from the "+pathname+" ("+this.gIP.length+")";
		    		IJ.showMessage("Error",msg);
3149 3150 3151 3152 3153 3154 3155 3156 3157 3158
//		    		throw new IllegalArgumentException (msg);
					for (int i=0; i<this.gIP.length ; i++){
						this.gIP[i].path=null; // not needed, just in case
						this.gIP[i].enabled=true;// enable all (actually just one) acquired images
						if (i < gridImages.length) {
							this.gIP[i].gridImage=gridImages[i];
						} else {
							this.gIP[i].gridImage=null;
						}
					}
3159 3160 3161
				}
//				setGridImages(gridImages);
			}
3162 3163
        	readAllGrids(
        			patternParameters, // prepare grid parameters for LMA
3164 3165
        			laserPointers, // prepare grid parameters for LMA
            		true); // boolean keep_images make it configurable parameter?
3166 3167 3168
			updateSetOrientation(null); // update orientation of image sets (built in readAllGrids() UPDATE - not anymore)

        }
3169

3170
        public void setFromXML(String pathname,
3171
        		EyesisCameraParameters eyesisCameraParameters, // should have cartesian set
3172
    			EyesisAberrations.AberrationParameters aberrationParameters) {
3173 3174 3175 3176 3177 3178 3179 3180 3181
        	this.eyesisCameraParameters=eyesisCameraParameters;

        	XMLConfiguration hConfig=null;
        	try {
				hConfig=new XMLConfiguration(pathname);
			} catch (ConfigurationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
3182
    		this.numSubCameras=Integer.parseInt(hConfig.getString("subcameras","1"));
3183 3184 3185 3186 3187 3188
        	System.out.println("Number of subcameras is "+this.numSubCameras);
			int num=hConfig.getMaxIndex("file");
			num++;
        	this.gIP=new GridImageParameters[num];
        	this.pars=new double[num][parameterDescriptions.length];
        	System.out.println("Number of pattern grid images in "+pathname+" is "+num);
3189

3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201
        	int numSets=hConfig.getMaxIndex("set")+1; // see if it returns -1 for none
        	System.out.println("Number of image sets in "+pathname+" is "+numSets);
        	if (numSets>0){
            	this.gIS=new GridImageSet[numSets];
            	for (int i=0;i<numSets;i++) {
            		HierarchicalConfiguration sub = hConfig.configurationAt("set("+i+")");
            		int index=Integer.parseInt(sub.getString("index"));
            		this.gIS[index]=new GridImageSet();
            		this.gIS[index].timeStamp=     Double.parseDouble(sub.getString("timestamp"));
            		this.gIS[index].stationNumber= Integer.parseInt(sub.getString("stationNumber"));
                	int minIndex=       this.gIS[index].getMinIndex();
                	int maxIndexPlusOne=this.gIS[index].getMaxIndexPlusOne();
3202 3203 3204 3205 3206
                	for (int j=minIndex;j<maxIndexPlusOne;j++){
//                		if (sub.getString(parameterDescriptions[j][0])!=null) {
                   		if (sub.getString(descrField(j,0)) != null) {
                			this.gIS[index].setParameterValue(j,Double.parseDouble(sub.getString(descrField(j,0))), false);
                		}
3207
                	}
3208

3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219
            		if (sub.getString("orientationEstimated")!=null) {
            			this.gIS[i].orientationEstimated=Boolean.parseBoolean(sub.getString("orientationEstimated"));
/*                    	System.out.println(i+": restored orientationEstimated="+this.gIS[i].orientationEstimated+
                    			" tilt="+this.gIS[i].goniometerTilt+
                    			" axial="+this.gIS[i].goniometerAxial);*/
            		} else {
                		this.gIS[i].setEstimatedFromNonNaN();
/*                    	System.out.println(i+": guessed. orientationEstimated="+this.gIS[i].orientationEstimated+
                    			" tilt="+this.gIS[i].goniometerTilt+
                    			" axial="+this.gIS[i].goniometerAxial);*/
            		}
3220

3221 3222 3223 3224 3225
            	}

        	} else {
        		this.gIS=null; // has to be build later
        	}
3226

3227 3228 3229 3230
        	for (int i=0;i<num;i++) {
        		this.gIP[i]=new GridImageParameters(i);
        		HierarchicalConfiguration sub = hConfig.configurationAt("file("+i+")");
        		this.gIP[i].imgNumber=i;
3231
        		this.gIP[i].path=sub.getString("name");
3232
        		this.gIP[i].source_path=sub.getString("source_path");
3233
        		this.gIP[i].timestamp=Double.parseDouble(sub.getString("timestamp"));
3234 3235 3236 3237 3238
        		this.gIP[i].channel=Integer.parseInt(sub.getString("channel"));
        		if (sub.getString("stationNumber")!=null) this.gIP[i].setStationNumber(Integer.parseInt(sub.getString("stationNumber")));
        		else this.gIP[i].setStationNumber(0);
        		if (sub.getString("enabled")!=null) this.gIP[i].enabled=Boolean.parseBoolean(sub.getString("enabled"));
        		if (sub.getString("noUsefulPSFKernels")!=null) this.gIP[i].noUsefulPSFKernels=Boolean.parseBoolean(sub.getString("noUsefulPSFKernels"));
3239 3240 3241 3242 3243 3244
        		this.gIP[i].setNumber=sub.getInt("setNumber",-1);
// new
        		this.gIP[i].hintedMatch=sub.getInt("hintedMatch",-1);
        		this.gIP[i].enabled=sub.getBoolean("enabled",false);
//        		if (aberrationParameters.trustEnabled && this.gIP[i].enabled) this.gIP[i].hintedMatch=2; // trusted
        		if (aberrationParameters.trustEnabled) this.gIP[i].hintedMatch= this.gIP[i].enabled?2:-1; // trusted and only trusted to enabled
3245

3246
        		for (int j=0;j<this.parameterDescriptions.length;j++){
3247 3248 3249
//            		if (sub.getString(parameterDescriptions[j][0])!=null)
               		if (sub.getString(descrField(j,0))!=null)
        				this.pars[i][j] = Double.parseDouble(sub.getString(descrField(j,0)));
3250 3251 3252 3253 3254 3255
        			else
        				if (isNonRadial(j)){
        					this.pars[i][j] = 0.0; // old calibration files without non-radial parameters
        				} else {
        					this.pars[i][j] = Double.NaN;
        				}
3256
        		}
3257 3258 3259 3260 3261 3262
        		int [] shiftRot={
        				sub.getInt("gridShiftX", 0),
        				sub.getInt("gridShiftY", 0),
        				sub.getInt("gridRotate", 0)};
        		this.gIP[i].setUVShiftRot(shiftRot);
//        		getInt(String key, int defaultValue)
3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274
        	}
        	if (this.gIS!=null){
            	System.out.println("Using stored image set data");
        		for (int is=0;is<this.gIS.length;is++){
            		this.gIS[is].imageSet=new GridImageParameters [this.numSubCameras];
            		for (int j=0;j<this.numSubCameras;j++) this.gIS[is].imageSet[j]=null;
        		}
        		for (int ip=0;ip<this.gIP.length;ip++) if (this.gIP[ip].setNumber>=0) {
        			this.gIS[this.gIP[ip].setNumber].imageSet[this.gIP[ip].channel]=this.gIP[ip];
        			this.gIP[ip].gridImageSet=this.gIS[this.gIP[ip].setNumber];
        			//this.gIP[i].channel
        		}
3275

3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300
        	} else {
            	System.out.println("Re-creating image set data from individual images (old format)");
            	System.out.println("WARNING: Some parameters may get from unused images and so have wrong values");
            	buildImageSets(false); // from scratch
            	// copying only parameters that have the same values for all images in a set
            	for (int is=0;is<this.gIS.length;is++){
            		int minIndex=       this.gIS[is].getMinIndex();
            		int maxIndexPlusOne=this.gIS[is].getMaxIndexPlusOne();
            		for (int pi=minIndex;pi<maxIndexPlusOne;pi++){
                		double parVal=Double.NaN;
                		boolean differs=false;
            			for (int j=0;j<this.gIS[is].imageSet.length;j++) if (this.gIS[is].imageSet[j]!=null) {
            				int imgNum=this.gIS[is].imageSet[j].imgNumber;
            				if (!Double.isNaN(this.pars[imgNum][pi])){
            					if (!Double.isNaN(parVal) && (parVal!=this.pars[imgNum][pi])){
            						differs=true;
            						break;
            					} else {
            						parVal=this.pars[imgNum][pi];
            					}
            				}
            				if (!differs && !Double.isNaN(parVal)){
            					this.gIS[is].setParameterValue(pi,parVal,false);
            				}
            				if (differs){
3301 3302
//            					System.out.println("ImageSet #"+is+": "+parameterDescriptions[j][0] +" has different values for individual images, skipping");
            					System.out.println("ImageSet #"+is+": "+descrField(j,0) +" has different values for individual images, skipping");
3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324
            				}
            			}
            		}
            		this.gIS[is].setEstimatedFromNonNaN();
            		//orientationEstimated
            		System.out.println(is+": tilt="+this.gIS[is].goniometerTilt+" axial="+this.gIS[is].goniometerAxial+" estimated="+this.gIS[is].orientationEstimated);

            	}
            	System.out.println("setFromXML("+pathname+",eyesisCameraParameters) 1 -> this.gIS.length="+this.gIS.length);
        	}
//        	System.out.println("setFromXML("+pathname+",eyesisCameraParameters) 2 -> this.gIS.length="+((this.gIS==null)?"null":this.gIS.length));
        	this.pathName=pathname; // where this instance was created from
        }
  //http://commons.apache.org/configuration/userguide/howto_xml.html
        public String getPath(){
        	return this.pathName;
        }
        public String selectAndSaveToXML(boolean smart, String defaultPath){
        	return selectAndSaveToXML(smart, defaultPath, null);
        }
        public String selectAndSaveToXML(boolean smart, String defaultPath, String comment){
			String [] extensions={".dcal-xml","-distcal.xml"};
3325
			MultipleExtensionsFileFilter parFilter = new MultipleExtensionsFileFilter("",extensions,"Distortion calibration *.dcal-xml files");
3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338
			String pathname=CalibrationFileManagement.selectFile(
					smart,
					true,
					"Save Calibration Parameters",
					"Save",
					parFilter,
					(defaultPath==null)?this.pathName:defaultPath); //String defaultPath
			if (pathname!=null) saveToXML(pathname,comment);
			return pathname;
        }
        public boolean saveTimestampedToXML(String pathname, String comment) {
        	return saveToXML(pathname+"_"+IJ.d2s(0.000001*(System.nanoTime()/1000),6).replace('.', '_')+".dcal-xml",      // full path or null
        			null);
3339
        }
3340 3341
        public boolean saveToXML(String pathname) {
        	return saveToXML(pathname,null);
3342
        }
3343 3344 3345 3346 3347 3348 3349 3350 3351
        public boolean saveToXML(String pathname, String comment) {
        	XMLConfiguration hConfig=new XMLConfiguration();
        	if (comment!=null) hConfig.addProperty("comment",comment);
        	hConfig.setRootElementName("distortionCalibrationParameters");
        	hConfig.addProperty("subcameras",this.numSubCameras);
        	for (int i=0;i<this.gIP.length;i++){
            	hConfig.addProperty("file","");
            	hConfig.addProperty("file.setNumber",this.gIP[i].setNumber);
            	hConfig.addProperty("file.name",this.gIP[i].path);
3352
            	hConfig.addProperty("file.source_path",this.gIP[i].source_path);
3353
            	hConfig.addProperty("file.enabled",this.gIP[i].enabled);
3354
            	hConfig.addProperty("file.hintedMatch",this.gIP[i].hintedMatch); // new
3355 3356 3357 3358
            	hConfig.addProperty("file.timestamp",IJ.d2s(this.gIP[i].timestamp,6));
            	hConfig.addProperty("file.channel",this.gIP[i].channel);
            	hConfig.addProperty("file.stationNumber",this.gIP[i].getStationNumber());
            	hConfig.addProperty("file.noUsefulPSFKernels",this.gIP[i].noUsefulPSFKernels);
3359 3360 3361 3362
            	int [] UVShiftRot=this.gIP[i].getUVShiftRot();
            	hConfig.addProperty("file.gridShiftX",UVShiftRot[0]);
            	hConfig.addProperty("file.gridShiftY",UVShiftRot[1]);
            	hConfig.addProperty("file.gridRotate",UVShiftRot[2]);
3363
            	for (int j=0;j<this.parameterDescriptions.length;j++){
3364
                	hConfig.addProperty("file."+descrField(j,0),this.pars[i][j]);
3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375
            	}
        	}
// save image sets
        	for (int i=0;i<this.gIS.length;i++){
            	hConfig.addProperty("set","");
            	hConfig.addProperty("set.index",i);
            	hConfig.addProperty("set.stationNumber",this.gIS[i].stationNumber);
            	hConfig.addProperty("set.timestamp",    IJ.d2s(this.gIS[i].timeStamp,6));
            	hConfig.addProperty("set.orientationEstimated",this.gIS[i].orientationEstimated);
            	double [] vector = this.gIS[i].updateParameterVectorFromSet(null); // unused parameters will be NaN
            	for (int j=0;j<vector.length;j++) if (!Double.isNaN(vector[j])){
3376
            		hConfig.addProperty("set."+descrField(j,0),vector[j]);
3377 3378
            	}
        	}
3379

3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395
//        	hConfig.addProperty("grids","");
        	File file=new File (pathname);
        	BufferedWriter writer;
			try {
				writer = new BufferedWriter(new FileWriter(file));
	        	hConfig.save(writer);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ConfigurationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			this.pathName=pathname;
        	return true;
        }
3396

3397
//    		public double      gridPeriod=0.0;  // average grid period, in pixels (to filter out (double-) reflected images
3398 3399 3400 3401 3402 3403 3404 3405 3406 3407
        public double calcGridPeriod(
        		int fileNumber,
        		boolean use_extra){ // use out-of grid nodes (can be w/o absolute matching
        	use_extra &= (this.gIP[fileNumber].pixelsXY_extra !=null);
        	int len = (this.gIP[fileNumber].pixelsXY==null)? 0 : this.gIP[fileNumber].pixelsXY.length;
        	int len0 = len;
        	if (use_extra ) {
        		len += this.gIP[fileNumber].pixelsXY_extra.length;
        	}
        	if (len<3) {
3408
        		this.gIP[fileNumber].setGridPeriod(Double.NaN);
3409
        	} else {
3410 3411
//        		double [][][] data =new double [this.gIP[fileNumber].pixelsXY.length][2][2];
        		double [][][] data =new double [len][2][2];
3412
        		// U(x,y), v(x,y)
3413
        		for (int i=0; i < this.gIP[fileNumber].pixelsXY.length; i++){
3414 3415 3416 3417 3418
        			data[i][0][0]=this.gIP[fileNumber].pixelsXY[i][0];
        			data[i][0][1]=this.gIP[fileNumber].pixelsXY[i][1];
        			data[i][1][0]=this.gIP[fileNumber].pixelsUV[i][0];
        			data[i][1][1]=this.gIP[fileNumber].pixelsUV[i][1];
        		}
3419 3420 3421 3422 3423 3424 3425 3426
        		if (use_extra) {
            		for (int i=0; i < this.gIP[fileNumber].pixelsXY_extra.length; i++){
            			data[i + len0][0][0]=this.gIP[fileNumber].pixelsXY_extra[i][0];
            			data[i + len0][0][1]=this.gIP[fileNumber].pixelsXY_extra[i][1];
            			data[i + len0][1][0]=this.gIP[fileNumber].pixelsUV_extra[i][0];
            			data[i + len0][1][1]=this.gIP[fileNumber].pixelsUV_extra[i][1];
            		}
        		}
3427 3428 3429 3430 3431 3432
        		if (this.debugLevel>3) {
        			System.out.println("calcGridPeriod("+fileNumber+"), debugLevel="+this.debugLevel+":");
            		for (int i=0;i<data.length;i++)System.out.println(i+": {{"+data[i][0][0]+","+data[i][0][1]+"},{"+data[i][1][0]+","+data[i][1][1]+"}}");
        		}
         	   double [][] coeff=new PolynomialApproximation(this.debugLevel).quadraticApproximation(data, true); // force linear
         	   if (coeff!=null) {
3433
         	     this.gIP[fileNumber].setGridPeriod(2.0/Math.sqrt(coeff[0][0]*coeff[0][0]+coeff[0][1]*coeff[0][1]+coeff[1][0]*coeff[1][0]+coeff[1][1]*coeff[1][1]));
3434 3435 3436 3437
         	     if (this.debugLevel>3) {
         	    	System.out.println("coeff[][]={{"+coeff[0][0]+","+coeff[0][1]+"},{"+coeff[1][0]+","+coeff[1][1]+"}}");
         	     }
         	   } else {
3438
        		  this.gIP[fileNumber].setGridPeriod(Double.NaN);
3439 3440 3441
         	   }
        	}
    		if (this.debugLevel>3) {
3442
    			System.out.println("calcGridPeriod("+fileNumber+") => "+this.gIP[fileNumber].getGridPeriod());
3443
    		}
3444
        	return this.gIP[fileNumber].getGridPeriod();
3445 3446

        }
3447

3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534
        public double [] getGridUVfromXY(
        		final double px,
        		final double py,
        		final int fileNumber,
        		boolean use_extra){ // use out-of grid nodes (can be w/o absolute matching
        	use_extra &= (this.gIP[fileNumber].pixelsXY_extra !=null);
        	int len = (this.gIP[fileNumber].pixelsXY==null)? 0 : this.gIP[fileNumber].pixelsXY.length;
        	final int len0 = len;
        	if (use_extra ) {
        		len += this.gIP[fileNumber].pixelsXY_extra.length;
        	}
        	if (len<3) {
        		return null;
        	}
        	final double [][] all_xy = new double [len][2];
        	final int    [][] all_uv = new int   [len][2];
        	if (len0 > 0) {
        		System.arraycopy(this.gIP[fileNumber].pixelsXY, 0, all_xy, 0, len0);
        		System.arraycopy(this.gIP[fileNumber].pixelsUV, 0, all_uv, 0, len0);
        	}
        	if (len > len0) {
        		System.arraycopy(this.gIP[fileNumber].pixelsXY_extra, 0, all_xy, len0, len-len0);
        		System.arraycopy(this.gIP[fileNumber].pixelsUV_extra, 0, all_uv, len0, len-len0);
        	}

    		ArrayList<Integer> neibs = new ArrayList<Integer>();
    		for (int i = 0; i < len; i++) neibs.add(i);
    		Collections.sort(neibs, new Comparator<Integer>() {
    		    @Override
    		    public int compare(Integer lhs, Integer rhs) {
//    		    	double [] xy_lhs = (lhs < len0)? gIP[fileNumber].pixelsXY[lhs] : gIP[fileNumber].pixelsXY_extra[lhs-len0];
//    		    	double [] xy_rhs = (rhs < len0)? gIP[fileNumber].pixelsXY[rhs] : gIP[fileNumber].pixelsXY_extra[rhs-len0];
    		    	double x_lhs = all_xy[lhs][0] - px;
    		    	double y_lhs = all_xy[lhs][1] - py;
    		    	double x_rhs = all_xy[rhs][0] - px;
    		    	double y_rhs = all_xy[rhs][1] - py;
    		    	double l2_lhs = x_lhs*x_lhs + y_lhs*y_lhs;
    		    	double l2_rhs = x_rhs*x_rhs + y_rhs*y_rhs;
    		        return l2_rhs > l2_lhs ? -1 : (l2_rhs < l2_lhs) ? 1 : 0;
    		    }
    		});
    		// now list neibs start with closest to px,py node. Get first with non-collinearU,V
    		int dlen = -1;
    		int [] i01 = {neibs.get(0), neibs.get(1)};
    		int [] duv0 = {all_uv[i01[1]][0]-all_uv[i01[0]][0], all_uv[i01[1]][1]-all_uv[i01[0]][1]};
    		for (int ii = 2;ii < len; ii++) {
    			int i = neibs.get(ii);
//        		int [] duv = {all_uv[i][0]-all_uv[0][0], all_uv[i][1]-all_uv[0][1]};
        		int idet = (duv0[0] *  (all_uv[i][1]-all_uv[i01[0]][1]))
        				-  (duv0[1] *  (all_uv[i][0]-all_uv[i01[0]][0]));
        		if (idet != 0) {
        			dlen = i + 1;
        			break;
        		}
    		}
    		if (dlen < 0) {
    			return null;
    		}
    		double [][][] data =new double [dlen][2][2];
    		// U(x,y), v(x,y)
    		for (int i=0; i < dlen; i++){
    			int indx = neibs.get(i);
    			data[i][0][0]=all_xy[indx][0];
    			data[i][0][1]=all_xy[indx][1];
    			data[i][1][0]=all_uv[indx][0];
    			data[i][1][1]=all_uv[indx][1];
    		}
      	   double [][] coeff=new PolynomialApproximation(this.debugLevel).quadraticApproximation(data, true); // force linear
     	   if (coeff == null) {
     		   return null;
     	   }
     	   double [] uv0 =
     		   {       (coeff[0][0] * px + coeff[0][1] * py + coeff[0][2]),
     				   (coeff[1][0] * px + coeff[1][1] * py + coeff[1][2])};

     	   int [][] reMap= MatchSimulatedPattern.getRemapMatrix(this.gIP[fileNumber].getUVShiftRot());
//     	   double [] uv = { reMap[0][0]*uv0[0] + reMap[0][1]* uv0[1] + reMap[0][2], // u
//     			   (        reMap[1][0]*uv0[0] + reMap[1][1]* uv0[1] + reMap[1][2])}; // v;
//     	   Sign?
     	   double [] uv = { reMap[0][0]*uv0[0] + reMap[0][1]* uv0[1] - reMap[0][2], // u
     			   (        reMap[1][0]*uv0[0] + reMap[1][1]* uv0[1] - reMap[1][2])}; // v;

        	return uv;
        }



3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545
        public int [] setGridsWithRemap(
        		int fileNumber,
        		int [][] reMap,
        		float [][] pixels,
        		PatternParameters patternParameters){
    		int sensorWidth=this.eyesisCameraParameters.getSensorWidth(this.gIP[fileNumber].channel);
    		int sensorHeight=this.eyesisCameraParameters.getSensorHeight(this.gIP[fileNumber].channel);
        	int station=this.gIP[fileNumber].getStationNumber();
        	int size=0;
        	int size_extra=0;
        	for (int i=0;i<pixels[0].length;i++) if ((pixels[0][i]>=0) && (pixels[1][i]>=0) && (pixels[0][i]<sensorWidth) && (pixels[1][i]<sensorHeight)){
3546 3547
        		int u=Math.round(pixels[2][i]);
        		int v=Math.round(pixels[3][i]);
3548 3549 3550 3551 3552
    			int u1= reMap[0][0]*u + reMap[0][1]*v + reMap[0][2]; // u
    			int v1= reMap[1][0]*u + reMap[1][1]*v + reMap[1][2]; // v;
        		if (patternParameters.getXYZM(u1,v1,false,station)!=null) size++; // already assumes correct uv?
        		else size_extra++;
        	}
3553 3554


3555 3556 3557 3558 3559 3560 3561 3562 3563
        	this.gIP[fileNumber].resetMask();
        	this.gIP[fileNumber].pixelsXY=new double [size][6];
        	this.gIP[fileNumber].pixelsUV=new int    [size][2];
        	this.gIP[fileNumber].pixelsXY_extra=new double [size_extra][6];
        	this.gIP[fileNumber].pixelsUV_extra=new int    [size_extra][2];

        	int index=0;
        	int index_extra=0;
        	for (int i=0;i<pixels[0].length;i++) if ((pixels[0][i]>=0) && (pixels[1][i]>=0) && (pixels[0][i]<sensorWidth) && (pixels[1][i]<sensorHeight)) {
3564 3565
        		int u=Math.round(pixels[2][i]);
        		int v=Math.round(pixels[3][i]);
3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599
    			int u1= reMap[0][0]*u + reMap[0][1]*v + reMap[0][2]; // u
    			int v1= reMap[1][0]*u + reMap[1][1]*v + reMap[1][2]; // v;

        		if (patternParameters.getXYZM(u1,v1,false,station)!=null) {
        			this.gIP[fileNumber].pixelsXY[index][0]=pixels[0][i];
        			this.gIP[fileNumber].pixelsXY[index][1]=pixels[1][i];
        			this.gIP[fileNumber].pixelsUV[index][0]= u1; // u
        			this.gIP[fileNumber].pixelsUV[index][1]= v1; // v;
        			if (this.gIP[fileNumber].flatFieldAvailable){
        				this.gIP[fileNumber].pixelsXY[index][2]=pixels[4][i];
        				for (int n=0;n<3;n++) this.gIP[fileNumber].pixelsXY[index][n+3]=pixels[n+5][i]/this.gIP[fileNumber].intensityRange[n];
        			} else {
        				for (int n=0;n<4;n++)this.gIP[fileNumber].pixelsXY[index][n+2]=1.0;
        			}
        			index++;
        		} else {
        			this.gIP[fileNumber].pixelsXY_extra[index_extra][0]=pixels[0][i];
        			this.gIP[fileNumber].pixelsXY_extra[index_extra][1]=pixels[1][i];
        			this.gIP[fileNumber].pixelsUV_extra[index_extra][0]= u1; // u
        			this.gIP[fileNumber].pixelsUV_extra[index_extra][1]= v1; // v;
        			if (this.gIP[fileNumber].flatFieldAvailable){
        				this.gIP[fileNumber].pixelsXY_extra[index_extra][2]=pixels[4][i];
        				for (int n=0;n<3;n++){
        					this.gIP[fileNumber].pixelsXY_extra[index_extra][n+3]=pixels[n+5][i]/this.gIP[fileNumber].intensityRange[n];
        				}
        			} else {
        				for (int n=0;n<4;n++)this.gIP[fileNumber].pixelsXY_extra[index_extra][n+2]=1.0;
        			}
        			index_extra++;
        		}
        	}
        	int [] result = {size,size_extra};
        	return result;
        }
3600 3601


3602 3603
        public boolean readAllGrids(
        		PatternParameters patternParameters,
3604 3605
        		LaserPointer      laserPointers, // as a backup if data is not available in the file
        		boolean keep_images
3606
            ){
3607 3608
        	boolean disableNoFlatfield=false;  // true only for processing transitional images - mixture of ff/ no-ff
			System.out.println("readAllGrids(), this.debugLevel="+this.debugLevel+" this.gIS is "+((this.gIS==null)?"null":"not null"));
3609 3610 3611 3612 3613 3614 3615 3616
        	int numImages=getNumImages();
    		Opener opener=new Opener();
    		JP46_Reader_camera jp4_reader= new JP46_Reader_camera(false);
    		ImagePlus imp_grid=null;
    		ImageStack stack;
    		int numOfGridNodes=0;
    		int numOfGridNodes_extra=0;
        	for (int fileNumber=0;fileNumber<numImages;fileNumber++){
3617
        		boolean woi_compensated = true;
3618 3619 3620 3621 3622 3623 3624
        		if (this.gIP[fileNumber].gridImage!=null){ // use in-memory grid images instead of the files
        			int numGridImg=fileNumber;
        			if (numGridImg>=this.gIP.length) numGridImg=this.gIP.length-1;
        			if (this.updateStatus) IJ.showStatus("Using in-memory grid image "+(fileNumber+1)+" (of "+(numImages)+"): "+
        					this.gIP[numGridImg].gridImage.getTitle());
        			if (this.debugLevel>1) System.out.print((fileNumber+1)+": "+this.gIP[numGridImg].gridImage.getTitle());
        			imp_grid=this.gIP[numGridImg].gridImage;
3625
        			// in memory grid pixels are always compensated
3626 3627
        		} else {
        			if (this.updateStatus) IJ.showStatus("Reading grid file "+(fileNumber+1)+" (of "+(numImages)+"): "+this.gIP[fileNumber].path);
3628
        			if (this.debugLevel>-1) System.out.print(fileNumber+" ("+this.gIP[fileNumber].getStationNumber()+"): "+this.gIP[fileNumber].path);
3629 3630 3631 3632 3633 3634 3635 3636
        			imp_grid=opener.openImage("", this.gIP[fileNumber].path);  // or (path+filenames[nFile])
        			if (imp_grid==null) {
        				String msg="Failed to read grid file "+this.gIP[fileNumber].path;
        				IJ.showMessage("Error",msg);
        				throw new IllegalArgumentException (msg);
        			}
// TODO: here - need to decode properties
        			jp4_reader.decodeProperiesFromInfo(imp_grid);
3637 3638 3639 3640
//        			woi_compensated = getImagePlusProperty(imp_grid,"WOI_COMPENSATED",false);
        			woi_compensated = (getImagePlusProperty(imp_grid,"WOI_TOP",0) == 0) &&
        					(getImagePlusProperty(imp_grid,"WOI_LEFT",0) == 0);

3641 3642 3643
        			if (keep_images) {
        				this.gIP[fileNumber].gridImage = imp_grid;
        			}
3644
        		}
3645 3646 3647 3648 3649
    			this.gIP[fileNumber].woi = new Rectangle(
    					getImagePlusProperty(imp_grid,"WOI_LEFT",0),
    					getImagePlusProperty(imp_grid,"WOI_TOP",0),
    					getImagePlusProperty(imp_grid,"WOI_WIDTH",  eyesisCameraParameters.getSensorWidth(this.gIP[fileNumber].getChannel())),
    					getImagePlusProperty(imp_grid,"WOI_HEIGHT", eyesisCameraParameters.getSensorHeight(this.gIP[fileNumber].getChannel())));
3650
        		this.gIP[fileNumber].laserPixelCoordinates=MatchSimulatedPattern.getPointersXYUV(imp_grid, laserPointers);
3651 3652 3653 3654 3655
        		this.gIP[fileNumber].motors=getMotorPositions(imp_grid, this.numMotors);
        		this.gIP[fileNumber].matchedPointers=getUsedPonters(imp_grid);
        		double [] saturations=new double [4];
        		for (int i=0;i<saturations.length;i++) {
        			saturations[i]=Double.NaN;
3656
        			if (imp_grid.getProperty("saturation_" + i) !=null) saturations[i]=Double.parseDouble((String) imp_grid.getProperty("saturation_" + i));
3657 3658 3659 3660 3661 3662 3663 3664
        		}
        		if (!Double.isNaN(saturations[1])) this.gIP[fileNumber].saturation[0]=saturations[1];
        		if (!Double.isNaN(saturations[2])) this.gIP[fileNumber].saturation[2]=saturations[2];
        		if (!Double.isNaN(saturations[0]) && !Double.isNaN(saturations[3])) this.gIP[fileNumber].saturation[1]=0.5*(saturations[0]+saturations[3]);
        		else {
            		if (!Double.isNaN(saturations[0])) this.gIP[fileNumber].saturation[1]=saturations[0];
            		if (!Double.isNaN(saturations[3])) this.gIP[fileNumber].saturation[1]=saturations[3];
        		}
3665

3666 3667 3668 3669 3670 3671 3672 3673
                stack=imp_grid.getStack();
            	if ((stack==null) || (stack.getSize()<4)) {
            		String msg="Expected a 8-slice stack in "+this.gIP[fileNumber].path;
            		IJ.showMessage("Error",msg);
            		throw new IllegalArgumentException (msg);
            	}
        		float [][] pixels=new float[stack.getSize()][]; // now - 8 (x,y,u,v,contrast, vignR,vignG,vignB
            	for (int i=0;i<pixels.length;i++) pixels[i]= (float[]) stack.getPixels(i+1); // pixel X : negative - no grid here
3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684
    			if (!woi_compensated) {
    				System.out.print(" woi_compensation-d "+fileNumber+" ");
    				for (int i = 0; i < pixels[0].length; i++) {
    					pixels[0][i] += this.gIP[fileNumber].woi.x;
    					pixels[1][i] += this.gIP[fileNumber].woi.y;
    				}
    				this.gIP[fileNumber].woi.width += this.gIP[fileNumber].woi.x;
    				this.gIP[fileNumber].woi.x = 0;
    				this.gIP[fileNumber].woi.height += this.gIP[fileNumber].woi.y;
    				this.gIP[fileNumber].woi.y = 0;
    				woi_compensated = true;
3685

3686
    			}
3687

3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698
            	if (this.eyesisCameraParameters.badNodeThreshold>0.0){
            		boolean thisDebug =false;
//            		thisDebug|=        (fileNumber== 720); // chn 25
                 int numBadNodes=fixBadGridNodes(
                		pixels,
                		stack.getWidth(),
                		this.eyesisCameraParameters.badNodeThreshold,
                		this.eyesisCameraParameters.maxBadNeighb,
                		this.debugLevel+(thisDebug?3:0),
                		thisDebug?("fixBad-"+fileNumber):null
                		);
3699
                 if (this.debugLevel>-1) {
3700 3701
                  if (numBadNodes>0)
                	  System.out.print("  -- replaced "+numBadNodes+" bad grid nodes");
3702 3703 3704
                  int [] uvrot=this.gIP[fileNumber].getUVShiftRot();
                  System.out.println(" shift:rot="+uvrot[0]+"/"+uvrot[1]+":"+uvrot[2]+
                		  " enabled="+this.gIP[fileNumber].enabled+" hintedMatch="+this.gIP[fileNumber].hintedMatch);
3705 3706
                 }
            	}
3707

3708 3709
    			this.gIP[fileNumber].flatFieldAvailable=pixels.length>=8;
            	if (disableNoFlatfield && !this.gIP[fileNumber].flatFieldAvailable) this.gIP[fileNumber].enabled=false; // just to use old mixed data
3710 3711

            	int [][] shiftRotMatrix= MatchSimulatedPattern.getRemapMatrix(this.gIP[fileNumber].getUVShiftRot());
3712 3713 3714 3715 3716 3717 3718
            	int [] sizeSizeExtra=setGridsWithRemap(
                		fileNumber,
                		shiftRotMatrix, // int [][] reMap,
                		pixels,
                		patternParameters);
            	numOfGridNodes+=sizeSizeExtra[0];
            	numOfGridNodes_extra+=sizeSizeExtra[1];
3719

3720
            	calcGridPeriod(fileNumber,true); // use _extra (out-of-pattern nodes) will be used to filter out reflections
3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741
//System.out.println ("pixelsXY["+fileNumber+"]length="+pixelsXY[fileNumber].length);
        	}
    		if (this.debugLevel>3) {
    			System.out.println("readAllGrids(), numImages="+numImages);
    			for (int n=0;n<this.gIP.length;n++) {
					System.out.println(n+": length="+this.gIP[n].pixelsXY.length);
	    			System.out.println("pixelsUV[][][0]/pixelsUV[][][1] pixelsXY[][][0]/pixelsXY[][][1]");
					for (int i=0;i<this.gIP[n].pixelsXY.length;i++){
    					System.out.println(n+":"+i+"  "+
    							this.gIP[n].pixelsUV[i][0]+"/"+
    							this.gIP[n].pixelsUV[1][1]+"  "+
    							IJ.d2s(this.gIP[n].pixelsXY[i][0], 2)+"/"+
    							IJ.d2s(this.gIP[n].pixelsXY[i][1], 2)
    					);
    				}
    			}
    		}
    		if (this.debugLevel>0) {
    			System.out.println("readAllGrids(), numImages="+numImages+", total number of grid nodes="+numOfGridNodes+", unused nodes "+numOfGridNodes_extra);
    		}
    		 // probably - do not need to verify that this.gIS is null - should do that anyway. UPDATE: no, now reading config file creates gIS
3742
    		buildImageSets(this.gIS!=null); // with non-null just copies motors from any of the images that has it
3743

3744 3745 3746
        	return true;
        }
        /**
3747
         * Sometimes "Process grid files" generates outliers (by 0.1..5 pixels) TODO: find the bug
3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838
         * This program replaces the "bad" ones with predicted by 8 neighbors using 2-nd order interpolation
         * @param fPixels stack of pX,pY,target-U,target-V,contrast (some bad pixels have low contrast), red,green,blue
         * @param width grid width
         * @param tolerance maximal tolerated difference between the predicted by 8 neigbors and center pixels
         * @parame maxBadNeighb - maximal number of bad cells among 8 neighbors
         * @parame gebugLevel debug level
         * @return number of fixed nodes
         * Neighbors of bad pixels can be reported bad, so they have to be re-tried with the worst removed
         */
        public int fixBadGridNodes(
        		float [][] fpixels,
        		int width,
        		double tolerance,
        		int maxBadNeighb,
        		int debugLevel,
        		String dbgTitle){
        	int debugThreshold=3;
        	double tolerance2=tolerance*tolerance;
        	double tolerance2Final=10.0*tolerance2; // final pass - fix even if the surronding are not that good
        	int [][] dirs8=   {{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}};
        	int [] dirs8Index={1,width+1,width,width-1,-1,-width-1,-width,-width+1};
        	double [] diffs2=new double [fpixels[0].length];
        	int height=diffs2.length/width;
        	for (int i=0;i<diffs2.length;i++) diffs2[i]=-1.0; // no nodes
        	double [][][] data=new double [8][3][];
        	for (int i=0;i<data.length;i++){
        		data[i][0]=new double[2];
        		data[i][1]=new double[2];
        		data[i][2]=new double[1];
        	}
        	PolynomialApproximation polynomialApproximation=new PolynomialApproximation(0); // do not report linear
        	double maxDiff2=0.0;
        	for (int y=1; y<(height-1);y++) for (int x=1;x<(width-1);x++) {
        		int index=y*width+x;
        		if (fpixels[0][index]>=0.0){
        			int numNonZero=0;
        			for (int iDir=0;iDir<dirs8.length;iDir++){
        				int index1=index+dirs8[iDir][1]*width+dirs8[iDir][0];
        				data[iDir][0][0]=dirs8[iDir][0];
        				data[iDir][0][1]=dirs8[iDir][1];
        				data[iDir][1][0]=fpixels[0][index1];
        				data[iDir][1][1]=fpixels[1][index1];
        				if ((fpixels[0][index1]<0) || (fpixels[1][index1]<0)){
        					data[iDir][2][0]=0.0;
        				} else {
        					data[iDir][2][0]=1.0;
        					numNonZero++;
        				}
        			}
        			if (numNonZero<8) continue; // should all be defined
        			double [][] coeff=polynomialApproximation.quadraticApproximation(
        					data,
        					false); // boolean forceLinear  // use linear approximation
        			if (coeff!=null) {
        				if ((coeff[0].length<6) || (coeff[1].length<6)){
        					if (debugLevel>0){
            					System.out.println("fixBadGridNodes() linear interpolate for x="+x+", y="+y);
            					for (int j=0;j<data.length;j++){
            						System.out.println(j+" "+data[j][0][0]+"/"+data[j][0][1]+" - "+data[j][1][0]+"/"+data[j][1][1]+" : "+data[j][2][0]);
            					}
            				}
        				}
        				double dx=coeff[0][coeff[0].length-1] - fpixels[0][index];
        				double dy=coeff[1][coeff[1].length-1] - fpixels[1][index];
        				diffs2[index]=dx*dx+dy*dy;
        				if (diffs2[index]>maxDiff2) maxDiff2=diffs2[index];
        			} else {
        				if (debugLevel>0){
        					System.out.println("fixBadGridNodes() failed for x="+x+", y="+y);
        				}
        			}
        		}
        	}
        	if (maxDiff2<=tolerance2) return 0; // nothing to fix
        	// here - first debug show?
        	boolean [] localWorst=new boolean[diffs2.length];
        	int numBad=0;
        	for (int i=0;i<localWorst.length;i++){
        		if (diffs2[i]<tolerance2){
        			localWorst[i]=false;
        		} else {
        			localWorst[i]=true;
        			for (int iDir=0;iDir<dirs8Index.length;iDir++) if (diffs2[i+dirs8Index[iDir]] > diffs2[i]){
        				localWorst[i]=false;
        				break;
        			}
        			if (localWorst[i]) numBad++;
        		}
        	}
        	if (numBad==0) {
				System.out.println("fixBadGridNodes() BUG - should not get here.");
3839
        		return 0; // should not get here -
3840 3841 3842 3843 3844 3845
        	}
        	double [][] dbgData=null;
			if (debugLevel>debugThreshold){
				dbgData=new double[9][];
				dbgData[0]=diffs2.clone();
				dbgData[2]=dbgData[0].clone();
3846
				for (int i=0;i< dbgData[2].length;i++) if (!localWorst[i]) dbgData[2][i]=-1.0;
3847
//				(new showDoubleFloatArrays()).showArrays(diffs2, width, height,  "diffs2");
3848
			}
3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929
        	// Trying to eliminate all non local worst (may that is just extra as there anot too many bad nodes)
        	int numStillBad=0;
        	for (int i=0;i<localWorst.length;i++) if (localWorst[i]){
        		for (int iDir0=0;iDir0<dirs8Index.length;iDir0++) if (diffs2[i+dirs8Index[iDir0]] > tolerance2){ // don't bother with not-so-bad
        			int index=i+dirs8Index[iDir0]; // will never be on the border as diffs2 is <=0.0 there
        			int numNonZero=0;
        			for (int iDir=0;iDir<dirs8.length;iDir++){
        				int index1=index+dirs8[iDir][1]*width+dirs8[iDir][0];
        				data[iDir][0][0]=dirs8[iDir][0];
        				data[iDir][0][1]=dirs8[iDir][1];
        				data[iDir][1][0]=fpixels[0][index1];
        				data[iDir][1][1]=fpixels[1][index1];
        				if ((data[iDir][1][0]<0) || (data[iDir][1][1]<0) || localWorst[index1]){
            				data[iDir][2][0]=0.0;
        				} else {
        					data[iDir][2][0]=1.0;
        					numNonZero++;
        				}

        			}
    				if (debugLevel>3){
    					System.out.print("+++ fixBadGridNodes() trying to fix for x="+(index%width)+", y="+(index/width)+", iDir0="+iDir0+" numNonZero="+numNonZero+" maxBadNeighb="+maxBadNeighb);
    				}

        			if (numNonZero<(data.length-maxBadNeighb-1)) continue;
        			double [][] coeff=polynomialApproximation.quadraticApproximation(
        					data,
        					false); // boolean forceLinear  // use linear approximation
        			if (coeff!=null) {
        				double dx=coeff[0][coeff[0].length-1] - fpixels[0][index];
        				double dy=coeff[1][coeff[1].length-1] - fpixels[1][index];
        				if (debugLevel>3){
        					System.out.print("fixBadGridNodes() old diffs2["+index+"]="+diffs2[index]);
        				}
        				diffs2[index]=dx*dx+dy*dy; // updated value
        				if (debugLevel>3){
        					System.out.print(" new diffs2["+index+"]="+diffs2[index]);
        				}
        				if (diffs2[index]>tolerance2) {
        					numStillBad++;
            				if (debugLevel>3){
            					System.out.print(" --- BAD");
            				}
        				} else if (debugLevel>3){
        					System.out.print(" --- GOOD");
        				}
        				if ((coeff[0].length<6) || (coeff[1].length<6)){
        					if (debugLevel>3){
            					System.out.print("fixBadGridNodes() 2 linear interpolate for x="+(index%width)+", y="+(index/width));
            					for (int j=0;j<data.length;j++){
            						System.out.println(j+" "+data[j][0][0]+"/"+data[j][0][1]+" - "+data[j][1][0]+"/"+data[j][1][1]+" : "+data[j][2][0]);
            					}
            				}
        				}
        			} else {
        				if (debugLevel>3){
        					System.out.println("fixBadGridNodes() failed for x="+(index%width)+", y="+(index/width)+", iDir0="+iDir0);
        				}
        			}
        			if (debugLevel>3) System.out.println();
        		}
        	}
        	if (numStillBad>0){
        		if (debugLevel>3){
        			System.out.println("fixBadGridNodes(): numStillBad="+numStillBad+" > 0 - probably near the border, just make sure  OK.");
        		}
        	}
			if (debugLevel>debugThreshold){
				dbgData[1]=diffs2.clone();
				for (int i=0;i< dbgData[1].length;i++) if (localWorst[i]) dbgData[1][i]=0.0;
				dbgData[3]=new double[dbgData[0].length];
				for (int i=0;i< dbgData[3].length;i++)  dbgData[3][i]=0.0;
				dbgData[4]=dbgData[3].clone();
				dbgData[5]=dbgData[3].clone();
				dbgData[6]=dbgData[3].clone();
				dbgData[7]=dbgData[3].clone();
				dbgData[8]=dbgData[3].clone();
				for (int i=0;i< dbgData[3].length;i++)  {
					dbgData[3][i]=fpixels[0][i];
					dbgData[4][i]=fpixels[1][i];
			    }
3930 3931 3932
			}

// TODO - try to fix some around pixels first?
3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004

// Actually patching locally worst nodes
        	for (int index=0;index<localWorst.length;index++) if (localWorst[index]){
        		int numNonZero=0;
    			for (int iDir=0;iDir<dirs8.length;iDir++){
    				int index1=index+dirs8[iDir][1]*width+dirs8[iDir][0];
    				data[iDir][0][0]=dirs8[iDir][0];
    				data[iDir][0][1]=dirs8[iDir][1];
    				data[iDir][1][0]=fpixels[0][index1];
    				data[iDir][1][1]=fpixels[1][index1];
    				if (diffs2[index1]>tolerance2Final){ // increased tolerance for the final correction
    					data[iDir][2][0]=0.0; // do not count neighbors who are bad themselves
    				} else {
    					data[iDir][2][0]=1.0;
    					numNonZero++;
    				}
    			}
    			if (numNonZero<(data.length-maxBadNeighb)){
    				if (debugLevel>3){
    					System.out.println("fixBadGridNodes() failed x="+(index%width)+", y="+(index/width)+", number of good neighbors="+numNonZero);
    				}
    				continue; // do not fix anything
    			}
    			double [][] coeff=polynomialApproximation.quadraticApproximation(
    					data,
    					false); // boolean forceLinear  // use linear approximation
    			if (coeff!=null) {
    				if ((coeff[0].length<6) || (coeff[1].length<6)){
    					if (debugLevel>3){
        					System.out.println("fixBadGridNodes() linear interpolate for x="+(index%width)+", y="+(index/width));
        					for (int j=0;j<data.length;j++){
        						System.out.println(j+" "+data[j][0][0]+"/"+data[j][0][1]+" - "+data[j][1][0]+"/"+data[j][1][1]+" : "+data[j][2][0]);
        					}
        					for (int n=0;n<coeff.length;n++){
        						for (int j=0;j<coeff[n].length;j++){
        							System.out.print(coeff[n][j]+" ");
        						}
        						System.out.println();
        					}
        				}
    				} else if (debugLevel>3){
    					System.out.println("fixBadGridNodes() qudratic interpolate for x="+(index%width)+", y="+(index/width));
    					for (int j=0;j<data.length;j++){
    						System.out.println(j+" "+data[j][0][0]+"/"+data[j][0][1]+" - "+data[j][1][0]+"/"+data[j][1][1]+" : "+data[j][2][0]);
    					}
    					for (int n=0;n<coeff.length;n++){
    						for (int j=0;j<coeff[n].length;j++){
    							System.out.print(coeff[n][j]+" ");
    						}
    						System.out.println();
    					}
    					if (((index%width)==19) && ((index/width)==57)){
    						coeff=(new PolynomialApproximation(4)).quadraticApproximation(
    		    					data,
    		    					false);
    					}
    				}
    				fpixels[0][index]=(float) coeff[0][coeff[0].length-1];
    				fpixels[1][index]=(float) coeff[1][coeff[1].length-1];
    			} else {
    				if (debugLevel>3){
    					System.out.println("fixBadGridNodes() failed for x="+(index%width)+", y="+(index/width)+", last pass");
    				}
    			}
        	}
			if (debugLevel>debugThreshold){
				for (int i=0;i< dbgData[3].length;i++)  {
					dbgData[5][i]=fpixels[0][i];
					dbgData[6][i]=fpixels[1][i];
					dbgData[7][i]=dbgData[3][i]-fpixels[0][i];
					dbgData[8][i]=dbgData[4][i]-fpixels[1][i];
			    }
4005

4006
				String [] dbgTitles={"diff20","diff2Mod","localWorst", "old-X", "old-Y", "new-X", "new-Y","old-new-X","old-new-Y"};
4007
				if (dbgTitle!=null) (new ShowDoubleFloatArrays()).showArrays(dbgData, width, height, true,  dbgTitle, dbgTitles);
4008
			}
4009 4010
        	return numBad;
        }
4011

4012
// TODO: Move all custom image properties (including encode/decode from JP4_reader_camera) to a separate class.
4013
// below is a duplicatie from MatchSimulatedPattern
4014
        @Deprecated
4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034
        public double[][] getPointersXY(ImagePlus imp, int numPointers){
			   // read image info to properties (if it was not done yet - should it?
			   if ((imp.getProperty("timestamp")==null) || (((String) imp.getProperty("timestamp")).length()==0)) {
				   JP46_Reader_camera jp4_instance= new JP46_Reader_camera(false);
				   jp4_instance.decodeProperiesFromInfo(imp);
			   }
			   double [][] pointersXY=new double[numPointers][];
			   int numPointerDetected=0;
			   for (int i=0;i<pointersXY.length;i++) {
				   pointersXY[i]=null;
				   if ((imp.getProperty("POINTER_X_"+i)!=null) && (imp.getProperty("POINTER_Y_"+i)!=null)) {
					   pointersXY[i]=new double[2];
					   pointersXY[i][0]=Double.parseDouble((String) imp.getProperty("POINTER_X_"+i));
					   pointersXY[i][1]=Double.parseDouble((String) imp.getProperty("POINTER_Y_"+i));
					   numPointerDetected++;
				   }
			   }
			   if (numPointerDetected>0) return pointersXY;
			   else return null;
		   }
4035

4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053
        public int [] getMotorPositions(ImagePlus imp, int numMotors){
        	// read image info to properties (if it was not done yet - should it?
        	if ((imp.getProperty("timestamp")==null) || (((String) imp.getProperty("timestamp")).length()==0)) {
        		JP46_Reader_camera jp4_instance= new JP46_Reader_camera(false);
        		jp4_instance.decodeProperiesFromInfo(imp);
        	}
        	int [] motorPos=new int [numMotors];
        	int numMotorsDetected=0;
        	for (int i=0;i<motorPos.length;i++) {
        		motorPos[i]=0;
        		if (imp.getProperty("MOTOR"+(i+1))!=null) {
        			motorPos[i]=Integer.parseInt((String) imp.getProperty("MOTOR"+(i+1)));
        			numMotorsDetected++;
        		}
        	}
        	if (numMotorsDetected>0) return motorPos;
        	else return null;
        }
4054

4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065
        public int  getUsedPonters(ImagePlus imp){
        	// read image info to properties (if it was not done yet - should it?
        	if ((imp.getProperty("timestamp")==null) || (((String) imp.getProperty("timestamp")).length()==0)) {
        		JP46_Reader_camera jp4_instance= new JP46_Reader_camera(false);
        		jp4_instance.decodeProperiesFromInfo(imp);
        	}
        	if (imp.getProperty("USED_POINTERS")!=null) {
        		return Integer.parseInt((String) imp.getProperty("USED_POINTERS"));
        	}
        	return 0;
        }
4066 4067


4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082
        // get "effective" grid period scaled for low-res (as LWIR) sensors
    	public double getEffectivePeriod(int numImg) {
    		double period = this.gIP[numImg].getGridPeriod();
    		int chn =       this.gIP[numImg].getChannel();
    		if ((this.small_sensors != null) && this.small_sensors[chn]) period /= small_period_frac;
    		return period;
    	}

    	public boolean hasSmallSensors() {
    		return small_period_frac > 0.0;
    	}
    	public boolean [] getSmallSensors() {
    		return small_sensors;
    	}
    	public boolean isSmallSensor(int numImg) {
4083
    		if ((this.gIP != null) && (numImg >= 0) &&   (numImg < this.gIP.length) && (small_sensors != null)){
4084 4085 4086 4087
    			return small_sensors[this.gIP[numImg].getChannel()];
    		}
    		return false;
    	}
4088 4089


4090 4091 4092 4093 4094 4095 4096
    	public double getSmallPeriodFrac() {
    		return small_period_frac;
    	}
//        public boolean [] small_sensors =    null; // set by filter grids
//        public double     small_period_frac =   0; // set by filter grids - ratio of small sensor period to large sensor period

    	// depending on camera type, return group, groups, group name
4097
    	// camera type: eyesis26, lwir/eo (2 resolutions) , single, other
4098 4099 4100 4101 4102 4103 4104 4105 4106 4107
    	//getNumSubCameras()
    	public int getNumLwir() {
    		if (hasSmallSensors()) {
    			int n = 0;
    			for (int i = 0; i < small_sensors.length; i++) if (small_sensors[i]) n++;
    			return n;
    		} else {
    			return 0;
    		}
    	}
4108
    	public int getNumEo() {
4109 4110
    		return getNumSubCameras() - getNumLwir();
    	}
4111
    	public int getEo0() {
4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134
    		if (hasSmallSensors()) {
    			for (int i = 0; i < small_sensors.length; i++) if (!small_sensors[i]) return i;
    			return -1; // should not happen
    		}
    		return 0;
    	}

    	public int getLwir0() {
    		if (hasSmallSensors()) {
    			for (int i = 0; i < small_sensors.length; i++) if (small_sensors[i]) return i;
    			return -1; // should not happen
    		}
    		return -1;
    	}

    	// Get number of different subcameras for adjustments (to share adjustment types)
    	public int getSubGroups() {
    		int num_sub = getNumSubCameras();
    		if (num_sub == 1)  return 1; // single
    		if (num_sub == 26) return 3; // eyesis4pi-26
    		int n = 2;
    		if (hasSmallSensors()) {
    			if (getNumLwir() > 1) n++;
4135
    			if (getNumEo() > 1) n++;
4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154
    		}
    		return n;
    	}

    	// Get subcamera adjustment group
    	// May be modified to use other subcamera as zero (eyesis in the middle row)
    	public int getSubGroup(int chn) {
    		int groups = getSubGroups();
    		if (groups == 1) return 0; // single camera
    		int num_sub = getNumSubCameras();
    		if (num_sub == 26) { // eyesis4pi-26
    			if (chn == 0) return 0;
    			if (chn < 24) return 1;
    			return 2;
    		}
			int n = 0;
    		if (hasSmallSensors()) {
    			if (small_sensors[chn]) { // current is LWIR
    				n = 1;
4155
    				if (getNumEo() > 1) n = 2;
4156
    				if (chn != getLwir0()) n++;
4157
    			} else {  // current is EO
4158
    				n = 0;
4159
    				if (chn != getEo0()) n++;
4160 4161 4162 4163
    			}
    		}
			return n;
    	}
4164
    	// check if the channel is the first in group that represents settings
4165 4166
    	public boolean firstInGroup(int chn) {
    		if (chn >= 24) return (chn == 24);
4167
    		if ((chn == getEo0()) || (chn == getLwir0())) return true;
4168 4169 4170 4171 4172 4173 4174 4175
    		int [] num = {0,0};
    		for (int i = 0; i < chn; i++) {
    			if ((small_sensors != null) && small_sensors[i]) num[1]++;
    			else num[0]++;
    		}
    		if ((small_sensors != null) && small_sensors[chn]) return (num[1] == 1);
    		else                                               return (num[0] == 1);
    	}
4176

4177 4178 4179 4180 4181 4182 4183 4184
    	// can be a clone of the previous channel (same value)
    	public int sourceToCopy(int chn) {
    		if ((chn == getEo0()) || (chn == getLwir0())) return -1; // no prototype to copy
    		return chn -1; // copy from previous
    	}



4185
    	public String getSubName(int chn, boolean full) {
4186
    		int groups = getSubGroups();
4187
    		if (groups == 1) return full?"Single subcamera":"sub"; // single camera
4188 4189
    		int num_sub = getNumSubCameras();
    		if (num_sub == 26) { // eyesis4pi-26
4190 4191 4192 4193
    			if (chn == 0) return full?"Camera head subcamera 0":"sub-head-0";
    			if (chn < 24) return full?"Camera head other subcameras":"sub-head-other";
    			if (chn == 24) return full?"Camera bottom 0":"sub-bottom-0";
    			return                full?"Camera bottom other":"sub-bottom-other";
4194 4195 4196
    		}
    		if (hasSmallSensors()) {
    			if (small_sensors[chn]) { // current is LWIR
4197 4198 4199
    				if (chn != getLwir0()) return full?"Subcamera LWIR other":"sub-lwir-other";
    				if (getNumLwir() > 1) return  full?"Subcamera LWIR 0":"sub-lwir0";
    				return                        full?"Subcamera LWIR":"sub-lwir";
4200 4201 4202 4203
    			} else {  // current is EO
    				if (chn != getEo0()) return full?"Subcamera EO other":"sub-eo-other";
    				if (getNumEo() > 1) return  full?"Subcamera EO 0":"sub-eo0";
    				return                        full?"Subcamera EO":"sub-eo";
4204 4205
    			}
    		}
4206
			if (chn != getEo0()) return full?"Subcamera other":"sub-other";
4207
			return                        full?"Subcamera 0":"sub0";
4208 4209
    	}

4210 4211 4212 4213 4214 4215 4216 4217
    	// get subcamera index this one should copy parameters from (-1 if it is first in group
    	public int masterSub(int chn) {
    		if (firstInGroup(chn)) return -1;
    		for (int mchn = chn -1; mchn >= 0; mchn--) {
    			if (firstInGroup(mchn)) return mchn;
    		}
    		return -1; // should never get here
    	}
4218 4219 4220



4221 4222 4223
        public int getImageNumPoints(int numImg){
        	return this.gIP[numImg].pixelsUV.length;
        }
4224

4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281
        public void initPars(int numImages, int numPars) {
        	this.pars=new double [numImages][numPars];
        	for (int i=0;i<numImages;i++) for (int j=0;j<numPars;j++) this.pars[i][j]=Double.NaN;
        }
        public double getParameterValue(int numImg, int numPar){
        	if ((numImg<0) || (numImg>=this.pars.length)) {
        		String msg="There are only "+this.pars.length+" images defined, requested #"+numImg;
        		IJ.showMessage("Error",msg);
        		throw new IllegalArgumentException (msg);
        	}
        	if ((numPar<0) || (numPar>=this.pars[numImg].length)) {
        		String msg="There are only "+this.pars[numImg].length+" parameters defined, requested #"+numPar;
        		IJ.showMessage("Error",msg);
        		throw new IllegalArgumentException (msg);
        	}
        	double par=(this.gIP[numImg].gridImageSet!=null)?this.gIP[numImg].gridImageSet.getParameterValue(numPar):Double.NaN;
        	if (Double.isNaN(par)) par=this.pars[numImg][numPar];
        	return par;
        }
        public void setParameterValue(int numImg, int numPar, double value, boolean updateEstimated){
        	if ((numImg<0) || (numImg>=this.pars.length)) {
        		String msg="There are only "+this.pars.length+" images defined, requested #"+numImg;
        		IJ.showMessage("Error",msg);
        		throw new IllegalArgumentException (msg);
        	}
        	if ((numPar<0) || (numPar>=this.pars[numImg].length)) {
        		String msg="There are only "+this.pars[numImg].length+" parameters defined, requested #"+numPar;
        		IJ.showMessage("Error",msg);
        		throw new IllegalArgumentException (msg);
        	}
        	this.pars[numImg][numPar]=value;
        	if (this.gIP[numImg].gridImageSet!=null) this.gIP[numImg].gridImageSet.setParameterValue(numPar,value,updateEstimated);
        }

        public void setParameters(double [] parameters, int numImg){
        	if ((numImg<0) || (numImg>=this.pars.length)) {
        		String msg="There are only "+this.pars.length+" images defined, requested #"+numImg;
        		IJ.showMessage("Error",msg);
        		throw new IllegalArgumentException (msg);
        	}
        	this.pars[numImg]=parameters.clone();
        	if (this.gIP[numImg].gridImageSet!=null) this.gIP[numImg].gridImageSet.updateSetFromParameterVector(parameters);
        }

        public int getParametersLength(int numImg){
        	return this.pars[numImg].length;
        }
        public double [] getParameters(int numImg){
        	if ((numImg<0) || (numImg>=this.pars.length)) {
        		String msg="There are only "+this.pars.length+" images defined, requested #"+numImg;
        		IJ.showMessage("Error",msg);
        		throw new IllegalArgumentException (msg);
        	}
        	double [] parameters=this.pars[numImg].clone();
        	if (this.gIP[numImg].gridImageSet!=null) this.gIP[numImg].gridImageSet.updateParameterVectorFromSet(parameters);
        	return parameters;
        }
4282 4283 4284 4285 4286 4287
        public int [] getUVShiftRot(int numImg){
        	return this.gIP[numImg].getUVShiftRot();
        }
        public GridImageParameters getGridImageParameters(int numImg){
        	return this.gIP[numImg];
        }
4288 4289

        // next is just for goniometer - use elevation and heading for cartesian mode?
4290
        public double [] getHeadEl(int imgNum){ // get sensor heading +(azimuth) and elevation
4291 4292 4293 4294 4295
        	if ((imgNum<0) || (imgNum>=this.pars.length)) {
        		String msg="There are only "+this.pars.length+" images defined, requested #"+imgNum;
        		IJ.showMessage("Error",msg);
        		throw new IllegalArgumentException (msg);
        	}
4296 4297 4298 4299 4300 4301 4302
        	double [] headel={this.pars[imgNum][index_heading],this.pars[imgNum][index_elevation]};
        	if (!isCartesian()) {
        		headel[0] += this.pars[imgNum][index_azimuth];
        	}
        	System.out.println("getHeadEl("+imgNum+") "+isCartesian()+" -> "+headel[0]+"/"+ headel[1]+", "+this.pars[imgNum][index_azimuth]+","+
        			this.pars[imgNum][index_heading]+", "+this.pars[imgNum][index_elevation]);
        	return headel;
4303
        }
4304
        // set goniometer horizontal axis angle and goniometer axial angles in all images
4305 4306 4307 4308 4309 4310 4311 4312
        public void setGHGA(double gh, double ga){
        	for (int imgNum=0;imgNum<this.pars.length;imgNum++) setGHGA( imgNum, gh,ga);
        }
        public void setGHGA(int imgNum, double gh, double ga){
        	setGH(imgNum, gh);
        	setGA(imgNum, ga);
        }
        public void setGH(int numImg, double gh){
4313
        	this.pars[numImg][index_gh]=gh;
4314 4315 4316
        	if (this.gIP[numImg].gridImageSet!=null) this.gIP[numImg].gridImageSet.goniometerTilt= gh;
        }
        public void setGA(int numImg,  double ga){
4317
        	this.pars[numImg][index_ga]=ga;
4318 4319 4320 4321
        	if (this.gIP[numImg].gridImageSet!=null) this.gIP[numImg].gridImageSet.goniometerAxial= ga;
        }
        public double getGH(int numImg){
        	if (this.gIP[numImg].gridImageSet!=null) return this.gIP[numImg].gridImageSet.goniometerTilt;
4322
        	return this.pars[numImg][index_gh];
4323
        }
4324

4325 4326
        public double getGA(int numImg){
        	if (this.gIP[numImg].gridImageSet!=null) return this.gIP[numImg].gridImageSet.goniometerAxial;
4327
        	return this.pars[numImg][index_ga];
4328
        }
4329

4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347
        public void setParameters(double [] parameters, int numImg, boolean[] mask){
        	if ((numImg<0) || (numImg>=this.pars.length)) {
        		String msg="There are only "+this.pars.length+" images defined, requested #"+numImg;
        		IJ.showMessage("Error",msg);
        		throw new IllegalArgumentException (msg);
        	}
        	if ((this.pars[numImg].length!=parameters.length) || (this.pars[numImg].length!=mask.length)) {
        		String msg="Vector lengths for image #"+numImg+
        		" mismatch: this.pars["+numImg+"].length="+this.pars[numImg].length+
        		" parameters.length="+parameters.length+
        		" mask.length="+mask.length;
        		IJ.showMessage("Error",msg);
        		throw new IllegalArgumentException (msg);
        	}
        	for (int i=0;i<mask.length;i++) if (mask[i])this.pars[numImg][i]=parameters[i];
        	if (this.gIP[numImg].gridImageSet!=null) this.gIP[numImg].gridImageSet.updateSetFromParameterVector(parameters,mask);

        }
4348

4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380
        public void setIntrinsicParameters(double [] parameters, int num){
        	if ((num<0) || (num>=this.pars.length)) {
        		String msg="There are only "+this.pars.length+" images defined, requested #"+num;
        		IJ.showMessage("Error",msg);
        		throw new IllegalArgumentException (msg);
        	}
        	if (this.pars[num].length!=parameters.length) {
        		String msg="Vector lengths for image #"+num+
        		" mismatch: this.pars["+num+"].length="+this.pars[num].length+
        		" parameters.length="+parameters.length;
        		IJ.showMessage("Error",msg);
        		throw new IllegalArgumentException (msg);
        	}
        	for (int i=0;i<parameters.length;i++) if (isIntrinsicParameter(i))this.pars[num][i]=parameters[i];
        	// no need to update image sets
        }
        public void setSubcameraParameters(double [] parameters, int num){
        	if ((num<0) || (num>=this.pars.length)) {
        		String msg="There are only "+this.pars.length+" images defined, requested #"+num;
        		IJ.showMessage("Error",msg);
        		throw new IllegalArgumentException (msg);
        	}
        	if (this.pars[num].length!=parameters.length) {
        		String msg="Vector lengths for image #"+num+
        		" mismatch: this.pars["+num+"].length="+this.pars[num].length+
        		" parameters.length="+parameters.length;
        		IJ.showMessage("Error",msg);
        		throw new IllegalArgumentException (msg);
        	}
        	for (int i=0;i<parameters.length;i++) if (isSubcameraParameter(i))this.pars[num][i]=parameters[i];
        	// no need to update image sets
        }
4381 4382


4383 4384 4385 4386 4387 4388
        public String getParameterName(int num){
        	if ((num<0) || (num>=this.parameterDescriptions.length)) {
        		String msg="There are only "+this.parameterDescriptions.length+" parameters defined, requested #"+num;
        		IJ.showMessage("Error",msg);
        		throw new IllegalArgumentException (msg);
        	}
4389
        	return descrField(num,0);
4390

4391 4392 4393 4394 4395 4396 4397
        }
        public String getParameterDescription(int num){
        	if ((num<0) || (num>=this.parameterDescriptions.length)) {
        		String msg="There are only "+this.parameterDescriptions.length+" parameters defined, requested #"+num;
        		IJ.showMessage("Error",msg);
        		throw new IllegalArgumentException (msg);
        	}
4398
        	return descrField(num,1);
4399

4400 4401 4402 4403 4404 4405 4406
        }
        public String getParameterUnits(int num){
        	if ((num<0) || (num>=this.parameterDescriptions.length)) {
        		String msg="There are only "+this.parameterDescriptions.length+" parameters defined, requested #"+num;
        		IJ.showMessage("Error",msg);
        		throw new IllegalArgumentException (msg);
        	}
4407
        	return descrField(num,2);
4408 4409 4410 4411 4412 4413 4414
        }
        public boolean isSubcameraParameter(int num){
        	if ((num<0) || (num>=this.parameterDescriptions.length)) {
        		String msg="There are only "+this.parameterDescriptions.length+" parameters defined, requested #"+num;
        		IJ.showMessage("Error",msg);
        		throw new IllegalArgumentException (msg);
        	}
4415
        	return (descrField(num,3).equals("S"));
4416

4417 4418 4419 4420 4421 4422 4423
        }
        public boolean isLocationParameter(int num){ //X,Y or Z location of the camera
        	if ((num<0) || (num>=this.parameterDescriptions.length)) {
        		String msg="There are only "+this.parameterDescriptions.length+" parameters defined, requested #"+num;
        		IJ.showMessage("Error",msg);
        		throw new IllegalArgumentException (msg);
        	}
4424
        	return (descrField(num,3).equals("T"));
4425 4426 4427 4428 4429 4430 4431 4432
        }

        public boolean isOrientationParameter(int num){ //one of the 2 goniometer orientation angles
        	if ((num<0) || (num>=this.parameterDescriptions.length)) {
        		String msg="There are only "+this.parameterDescriptions.length+" parameters defined, requested #"+num;
        		IJ.showMessage("Error",msg);
        		throw new IllegalArgumentException (msg);
        	}
4433
        	return (descrField(num,3).equals("R"));
4434 4435 4436 4437 4438 4439 4440 4441
        }

        public boolean isIntrinsicParameter(int num){ // updated from image calibration file
        	if ((num<0) || (num>=this.parameterDescriptions.length)) {
        		String msg="There are only "+this.parameterDescriptions.length+" parameters defined, requested #"+num;
        		IJ.showMessage("Error",msg);
        		throw new IllegalArgumentException (msg);
        	}
4442
        	return (descrField(num,4).equals("I"));
4443

4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477
        }
        public String getImagePath(int num) {
        	if ((num<0) || (num>=this.gIP.length)) {
        		String msg="There are only "+this.gIP.length+" images defined, requested #"+num;
        		IJ.showMessage("Error",msg);
        		throw new IllegalArgumentException (msg);
        	}
        	return this.gIP[num].path;
        }
        public int getImageSubcamera(int num) {
        	if ((num<0) || (num>=this.gIP.length)) {
        		String msg="There are only "+this.gIP.length+" images defined, requested #"+num;
        		IJ.showMessage("Error",msg);
        		throw new IllegalArgumentException (msg);
        	}
        	return this.gIP[num].channel;
        }
        public int getImageStation(int num) {
        	if ((num<0) || (num>=this.gIP.length)) {
        		String msg="There are only "+this.gIP.length+" images defined, requested #"+num;
        		IJ.showMessage("Error",msg);
        		throw new IllegalArgumentException (msg);
        	}
        	return this.gIP[num].getStationNumber();
        }
        public double getImageTimestamp(int num) {
        	if ((num<0) || (num>=this.gIP.length)) {
        		String msg="There are only "+this.gIP.length+" images defined, requested #"+num;
        		IJ.showMessage("Error",msg);
        		throw new IllegalArgumentException (msg);
        	}
        	return this.gIP[num].timestamp;
        }
        public int getNumImages() {
4478
        	if (this.gIP == null) return 0;
4479 4480 4481
        	return this.gIP.length;
        }
        public int getNumParameters() {
4482
        	if (this.parameterDescriptions == null) return 0;
4483 4484 4485 4486 4487
        	return this.parameterDescriptions.length;
        }
        public int getNumSubCameras() {
        	return this.numSubCameras;
        }
4488 4489 4490 4491 4492 4493 4494
        public int getNumNodes(int num, boolean use_extra) {
        	if ((this.gIP == null) || (num >= this.gIP.length) || (this.gIP[num].pixelsXY == null)) return 0;
        	int len = this.gIP[num].pixelsXY.length;
        	if (use_extra && (this.gIP[num].pixelsXY_extra != null)) len += this.gIP[num].pixelsXY_extra.length;
        	return len;
        }

4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507
        public int getImageNumber(int set_number, int sub_number) {
        	if ((this.gIS== null) || (this.gIS.length <= set_number) || (this.gIS[set_number] == null)) return -1;
        	if (sub_number >= getNumChannels()) return -1;
        	if ((this.gIS[set_number].imageSet == null) || (this.gIS[set_number].imageSet[sub_number] == null)) return -1;
        	return this.gIS[set_number].imageSet[sub_number].imgNumber;
        }

        public int getImageSet(int img_number) {
        	if ((this.gIP== null) || (this.gIP.length <= img_number) || (img_number <  0) ||(this.gIP[img_number] == null)) return -1;
        	return this.gIP[img_number].setNumber;
        }


4508
        /**
4509
         *
4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539
         * @param imgNumber number of grid image to edit parameters (location, distortion) for
         * @return <2 - canceled, -1 - done, els - number of the next image to edit
         */
        public int editImageParameters(int imgNumber){
        	if ((this.gIP==null) || (imgNumber<0) ||(imgNumber>=this.gIP.length)) return -3;
			int sub=getImageSubcamera(imgNumber);
       	    String sTS=IJ.d2s(getImageTimestamp(imgNumber),6);
    		GenericDialog gd = new GenericDialog("Manually editing per-image parameters, timestamp="+sTS+
    				", subchannel-"+sub+" "+getImagePath(imgNumber));
    	    for (int i=0;i<getNumParameters();i++){
    	    	gd.addNumericField(
    	    			i+": "+getParameterDescription(i)+"["+ getParameterName(i)+"] "+
    	    			(isSubcameraParameter(i)?"S ":"  "),
//    	    			this.pars[imgNumber][i],5,10, getParameterUnits(i));
    	    			this.getParameterValue(imgNumber,i),5,10, getParameterUnits(i));
    	    }
    	    gd.addNumericField("Next image to edit (0.."+this.pars.length+", -1 - none) ", imgNumber+1,0);
   	        gd.enableYesNoCancel("OK", "Done");
    	    WindowTools.addScrollBars(gd);
    	    gd.showDialog();
    	    if (gd.wasCanceled()) return -2;
    	    for (int i=0;i<getNumParameters();i++){
//    	    	this.pars[imgNumber][i]= gd.getNextNumber();
    	    	this.setParameterValue(imgNumber,i, gd.getNextNumber(),true);
    	    }
    	    imgNumber= (int) gd.getNextNumber();
    	    if ((imgNumber<0) || (imgNumber>=getNumImages())) return -1;
    		if (!gd.wasOKed()) return -1; // pressed Done (no need to ask for the next number)
            return imgNumber;
        }
4540 4541
        @Deprecated
        public void setMaskFromImageStack(String path){ // can not work with different size senors
4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555
    		Opener opener=new Opener();
			if (this.debugLevel>1) System.out.println("Opening "+path+" as a stack of sensor masks");
			ImagePlus imp=opener.openImage("", path);
        	if (imp==null) {
        		String msg="Failed to read sensors mask file "+path;
        		IJ.showMessage("Error",msg);
        		throw new IllegalArgumentException (msg);
        	}
        	(new JP46_Reader_camera(false)).decodeProperiesFromInfo(imp);
        	if (imp.getProperty("shrinkGridForMask")!=null)
        		eyesisCameraParameters.shrinkGridForMask=Integer.parseInt((String) imp.getProperty("shrinkGridForMask"));
            	if (imp.getProperty("maskBlurSigma")!=null)
            		eyesisCameraParameters.maskBlurSigma=Double.parseDouble((String) imp.getProperty("maskBlurSigma"));
            	if (imp.getProperty("decimateMasks")!=null)
4556
            		eyesisCameraParameters.setDecimateMasks(Integer.parseInt((String) imp.getProperty("decimateMasks")));
4557
            	if (imp.getProperty("sensorWidth")!=null)
4558
            		eyesisCameraParameters.setSensorWidth(Integer.parseInt((String) imp.getProperty("sensorWidth")));
4559
            	if (imp.getProperty("sensorHeight")!=null)
4560
            		eyesisCameraParameters.setSensorHeight(Integer.parseInt((String) imp.getProperty("sensorHeight")));
4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571
        	setMaskFromImageStack(imp);
        }
        /**
         * Find number of channels in this camera
         * @return maximal number of channel used plus one
         */
        public int getNumChannels(){
        	int nChn=-1;
        	for (int i=0;i<this.gIP.length;i++) if (this.gIP[i].channel>nChn) nChn=this.gIP[i].channel;
        	return nChn+1;
        }
4572

4573

4574
        public double getMask(int chnNum, double px, double py){
4575 4576 4577 4578
        	int width= eyesisCameraParameters.getSensorWidth(chnNum)/eyesisCameraParameters.getDecimateMasks(chnNum);
        	int height=eyesisCameraParameters.getSensorHeight(chnNum)/eyesisCameraParameters.getDecimateMasks(chnNum);
        	int iPX= ((int) Math.round(px))/eyesisCameraParameters.getDecimateMasks(chnNum);
        	int iPY= ((int) Math.round(py))/eyesisCameraParameters.getDecimateMasks(chnNum);
4579 4580 4581 4582
        	if ((iPX<0) || (iPY<0) || (iPX>=width) || (iPY>=height)) return 0.0;
        	if ((this.sensorMasks==null) || (this.sensorMasks[chnNum]==null)) return 1.0;
        	return this.sensorMasks[chnNum][iPY*width+iPX];
        }
4583 4584
        @Deprecated
        public double getMask(double[] mask, double px, double py){ // problems with different size sensors
4585
        	if (mask==null) return 0;
4586 4587 4588 4589
        	int width= eyesisCameraParameters.getSensorWidth()/eyesisCameraParameters.getDecimateMasks();
        	int height=eyesisCameraParameters.getSensorHeight()/eyesisCameraParameters.getDecimateMasks();
        	int iPX= ((int) Math.round(px))/eyesisCameraParameters.getDecimateMasks();
        	int iPY= ((int) Math.round(py))/eyesisCameraParameters.getDecimateMasks();
4590 4591 4592
        	if ((iPX<0) || (iPY<0) || (iPX>=width) || (iPY>=height)) return 0.0;
        	return mask[iPY*width+iPX];  // null ponter
        }
4593 4594


4595
        @Deprecated
4596 4597 4598 4599 4600 4601
        public void setMaskFromImageStack(ImagePlus imp){
        	if (imp == null){
        		String msg="sensors mask image is null";
        		IJ.showMessage("Error",msg);
        		throw new IllegalArgumentException (msg);
        	}
4602 4603 4604 4605 4606
        	if (imp.getProperty("decimateMasks")!=null) eyesisCameraParameters.setDecimateMasks(Integer.parseInt((String) imp.getProperty("decimateMasks")));
        	eyesisCameraParameters.setSensorWidth(imp.getWidth()*eyesisCameraParameters.getDecimateMasks());
        	eyesisCameraParameters.setSensorHeight(imp.getHeight()*eyesisCameraParameters.getDecimateMasks());
        	if (imp.getProperty("sensorWidth")!=null)	eyesisCameraParameters.setSensorWidth(Integer.parseInt((String) imp.getProperty("sensorWidth")));
        	if (imp.getProperty("sensorHeight")!=null)  eyesisCameraParameters.setSensorHeight(Integer.parseInt((String) imp.getProperty("sensorHeight")));
4607

4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625
    		if (this.sensorMasks==null) {
    			this.sensorMasks=new double[getNumChannels()][];
    			for (int i=0;i<this.sensorMasks.length;i++) this.sensorMasks[i]=null;
    		}
    		int numChannels=imp.getStackSize();
    		float [][] pixels =new float[numChannels][];
    		if (numChannels==1){
    			pixels[0]=(float[]) imp.getProcessor().getPixels();
    		} else {
        		ImageStack stack = imp.getStack();
            	if (stack==null) {
            		String msg="Expected a image stack with masks";
            		IJ.showMessage("Error",msg);
            		throw new IllegalArgumentException (msg);
            	}
            	for (int i=0;i<numChannels;i++) pixels[i]= (float[]) stack.getPixels(i+1);
    		}
    		for (int numChn=0;(numChn<numChannels) && (numChn<this.sensorMasks.length);numChn++){
4626
    			//Make sure masks contain non-zero (>0.0) pixels, otherwise skip those
4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650
        		boolean defined=false;
        		for (int i=0;i<pixels[numChn].length;i++) if (pixels[numChn][i]>0.0){
        			defined=true;
        			break;
        		}
    			if (defined) {
    				this.sensorMasks[numChn]=new double [pixels[numChn].length];
    				for (int i=0;i<this.sensorMasks[numChn].length;i++) this.sensorMasks[numChn][i]=pixels[numChn][i];
    			}
    		}
        }
        public ImagePlus saveMaskAsImageStack(String title, String path){
        	ImagePlus imp=getMaskAsImageStack(title);
        	if (imp==null) return null;
	   				FileSaver fs=new FileSaver(imp);
	   				if (updateStatus) IJ.showStatus("Saving masks "+path);
	   				if (this.debugLevel>0) System.out.println("Saving masks "+path);
	   				if (imp.getStackSize()>1)
	   					fs.saveAsTiffStack(path);
	   				else
	   					fs.saveAsTiff(path);
        	return imp;
        }

4651
        @Deprecated
4652 4653 4654 4655 4656 4657
        public ImagePlus getMaskAsImageStack(String title){
        	if (this.sensorMasks==null){
        		String msg="Sensor mask array does not exist, nothing to convert";
        		IJ.showMessage("Error",msg);
        		throw new IllegalArgumentException (msg);
        	}
4658 4659
        	int width= eyesisCameraParameters.getSensorWidth()/eyesisCameraParameters.getDecimateMasks();
        	int height=eyesisCameraParameters.getSensorHeight()/eyesisCameraParameters.getDecimateMasks();
4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674
        	float [][]pixels=new float [getNumChannels()][width*height];
        	ImagePlus imp=null;
        	for (int numChn=0;numChn<getNumChannels();numChn++){
        		if (this.sensorMasks[numChn]==null) for (int i=0;i<pixels[numChn].length;i++)pixels[numChn][i]=0.0F;
        		else for (int i=0;i<pixels[numChn].length;i++)pixels[numChn][i]=(float) this.sensorMasks[numChn][i];
        	}
        	if (this.sensorMasks.length>0){
        		ImageStack stack=new ImageStack(width,height);
        		for (int numChn=0;numChn<pixels.length;numChn++)  stack.addSlice("chn-"+numChn,    pixels[numChn]);
        		imp = new ImagePlus(title, stack);
        	} else {
        		ImageProcessor  ip =new FloatProcessor(width,height);
        		ip.setPixels(pixels[0]);
        		imp=new ImagePlus(title, ip);
        	}
4675
// TODO: add more properties here (MAC+channel)? preserve other properties?
4676 4677
        	imp.setProperty("sensorWidth", ""+eyesisCameraParameters.getSensorWidth());
        	imp.setProperty("sensorHeight", ""+eyesisCameraParameters.getSensorHeight());
4678 4679
        	imp.setProperty("shrinkGridForMask", ""+eyesisCameraParameters.shrinkGridForMask);
        	imp.setProperty("maskBlurSigma", ""+eyesisCameraParameters.maskBlurSigma);
4680
        	imp.setProperty("decimateMasks", ""+eyesisCameraParameters.getDecimateMasks());
4681

4682 4683 4684 4685 4686 4687 4688 4689 4690 4691
        	(new JP46_Reader_camera(false)).encodeProperiesToInfo(imp);
        	imp.getProcessor().resetMinAndMax();
        	return imp;
        }
        /**
         * Generate low-vignetting sensor mask for flat-field calculation
         * @param sensorMask sensor mask, decimated array
         * @param width  sensor width, pixels
         * @param height sensor height, pixels
         * @param shrink shrink sensor mask by this amount (sensor, non-decimated pixels)
4692 4693 4694
         * @param radius radial mask - zero if farther than radius, 0.5*(cos(pi*r/radius)+1.0) if less
         * @param minimalAlpha - zero mask below this threshold
         * @return returns arrray with the same size as sensorMask that corresponds to low-vignetting areas of the sensor/lens
4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728
         */

        public double [] nonVignettedMask(
        		double [] sensorMask,
        		int width,
        		int height,
        		double x0,     // lens center X (sensor, non-decimated pix)
        		double y0,     // lens center Y (sensor, non-decimated pix)
        		double shrink,
        		double radius,
        		double minimalAlpha){

        	int decimate= (int) Math.round(Math.sqrt(width*height/sensorMask.length));
        	int dcmWidth= width/decimate;
        	int dcmHeight=height/decimate;
        	double [] mask= sensorMask.clone();
        	if (shrink>0){
        		(new DoubleGaussianBlur() ).blurDouble(mask, dcmWidth, dcmHeight, shrink/decimate, shrink/decimate, 0.01);
        		for (int i=0;i<mask.length;i++){
        			double d=2*(mask[i]-0.5);
        			mask[i]=(d>0)?(d*d):(0.0);
        		}
        	}
        	if (radius>0.0){
        		int index=0;
        		for (int iy=0; iy<dcmHeight;iy++) for (int ix=0; ix<dcmWidth;ix++){
        			double r=Math.sqrt((iy*decimate-y0)*(iy*decimate-y0)+(ix*decimate-x0)*(ix*decimate-x0))/radius;
        			double k=(r>1.0)?0.0:(0.5*(Math.cos(Math.PI*r)+1.0));
        			mask[index++]*=k;
        		}
        	}
        	if (minimalAlpha>0.0) for (int i=0;i<mask.length;i++) if (mask[i]<minimalAlpha) mask[i]=0.0;
        	return mask;
        }
4729 4730 4731 4732 4733 4734 4735 4736 4737
        @Deprecated
        public double [][] calculateSensorMasksOld() {
        	return calculateSensorMasks(
        			eyesisCameraParameters.getDecimateMasks(),
        			eyesisCameraParameters.getSensorWidth(),
        			eyesisCameraParameters.getSensorHeight(),
        			eyesisCameraParameters.shrinkGridForMask,
        			eyesisCameraParameters.maskBlurSigma);
        }
4738

4739 4740 4741 4742 4743
        public double [][] calculateSensorMasks() {
        	return calculateSensorMasks(
        			eyesisCameraParameters.shrinkGridForMask,
        			eyesisCameraParameters.maskBlurSigma);
        }
4744

4745
        /**
4746
         *
4747 4748 4749
         * @param width image width, in pixels (pixel X coordinates are between 0 and width-1, inclusive)
         * @param height image height, in pixels (pixel Y coordinates are between 0 and height-1, inclusive)
         * @param shrinkGridForMask shrink detected grids by this number of nodes in each direction before bluring
4750
         * @param sigmaUV Gaussian sigma for blurring of the sensor mask (if negative - in grid inter-node distances)
4751 4752
         * @return array of pixel arrays (or nulls) for each camera subchannel (also keeps it in the class instance)
         */
4753
        @Deprecated
4754 4755 4756 4757 4758 4759
        public double [][] calculateSensorMasks( int decimate, int width, int height, int shrinkGridForMask, double sigmaUV) {
        	int dWidth=  (width -1)/decimate+1;
        	int dHeight= (height-1)/decimate+1;
        	int numChannels=getNumChannels();
        	this.sensorMasks=new double [numChannels][];
        	DoubleGaussianBlur gb=new DoubleGaussianBlur();
4760
        	if ((this.debugLevel>1) && (SDFA_INSTANCE==null)) SDFA_INSTANCE=new ShowDoubleFloatArrays();
4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786
			if (this.debugLevel>2)System.out.println("calculateSensorMasks("+width+","+height+","+shrinkGridForMask+","+sigmaUV+")");
        	for (int chNum=0;chNum<numChannels; chNum++){
        		this.sensorMasks[chNum]=new double[dWidth*dHeight];
        		for (int i=0;i<this.sensorMasks[chNum].length;i++) this.sensorMasks[chNum][i]=0.0;
        		double rAverage=0.0;
        		double rAverageNum=0.0;
        		for (int imgNum=0;imgNum<this.gIP.length;imgNum++) if (this.gIP[imgNum].channel==chNum){ // image is for this this channel
        	        double [][] preMask=preCalculateSingleImageMask(imgNum, decimate, width, height, shrinkGridForMask);
        	        if (preMask==null) continue; //nothing in this channel
            		rAverage+=preMask[0][0];
            		rAverageNum+=preMask[0][1];
       			    for (int i=0;i<this.sensorMasks[chNum].length;i++) if (preMask[1][i]>0.0) this.sensorMasks[chNum][i]=1.0;
        		}
        		if (rAverageNum==0.0) continue; // nothing to blur/process for this channel
        		rAverage/=rAverageNum; // average distance to the fartherst node from the current
        		double      sigma=sigmaUV;
        		if(sigma<0) sigma*=-rAverage;
        		gb.blurDouble(this.sensorMasks[chNum], dWidth, dHeight, sigma/decimate, sigma/decimate, 0.01);

        		//    this.sensorMasks[chNum] now contains 0.0/1.0 mask. Blur it
        		//	    		gb.blurDouble(pointedBayer[bayerR], halfWidth, halfHeight, this.lowpassSigma, this.lowpassSigma, 0.01);
        		//	    		if (debugLevel>2) sdfra_instance.showArrays(pointedBayer[bayerR].clone(), halfWidth, halfHeight, title+"-smooth");

        	}
        	return this.sensorMasks;
        }
4787

4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823
        public double [][] calculateSensorMasks(int shrinkGridForMask, double sigmaUV) {
        	int numChannels=getNumChannels();
        	this.sensorMasks=new double [numChannels][];
        	DoubleGaussianBlur gb=new DoubleGaussianBlur();
        	if ((this.debugLevel>1) && (SDFA_INSTANCE==null)) SDFA_INSTANCE=new ShowDoubleFloatArrays();
			if (this.debugLevel>2)System.out.println("calculateSensorMasks("+shrinkGridForMask+","+sigmaUV+")");
        	for (int chNum=0;chNum<numChannels; chNum++){
        		int decimate = eyesisCameraParameters.getDecimateMasks(chNum);
        		int width = eyesisCameraParameters.getSensorWidth(chNum);
        		int height = eyesisCameraParameters.getSensorHeight(chNum);
            	int dWidth=  (width -1)/decimate+1;
            	int dHeight= (height-1)/decimate+1;

        		this.sensorMasks[chNum]=new double[dWidth*dHeight];
        		for (int i=0;i<this.sensorMasks[chNum].length;i++) this.sensorMasks[chNum][i]=0.0;
        		double rAverage=0.0;
        		double rAverageNum=0.0;
        		for (int imgNum=0;imgNum<this.gIP.length;imgNum++) if (this.gIP[imgNum].channel==chNum){ // image is for this this channel
        	        double [][] preMask=preCalculateSingleImageMask(imgNum, decimate, width, height, shrinkGridForMask);
        	        if (preMask==null) continue; //nothing in this channel
            		rAverage+=preMask[0][0];
            		rAverageNum+=preMask[0][1];
       			    for (int i=0;i<this.sensorMasks[chNum].length;i++) if (preMask[1][i]>0.0) this.sensorMasks[chNum][i]=1.0;
        		}
        		if (rAverageNum==0.0) continue; // nothing to blur/process for this channel
        		rAverage/=rAverageNum; // average distance to the fartherst node from the current
        		double      sigma=sigmaUV;
        		if(sigma<0) sigma*=-rAverage;
        		gb.blurDouble(this.sensorMasks[chNum], dWidth, dHeight, sigma/decimate, sigma/decimate, 0.01);
        	}
        	return this.sensorMasks;
        }




4824

4825 4826 4827 4828 4829 4830 4831 4832
        /**
         * Create round mask inside the actual one, with the provided center. Blur result with the same sigma as original
         * @param chn sensor number
         * @param xCenter X of the center (before decimation)
         * @param yCenter Y of the center (before decimation)
         * @return this channel mask, also sets the round mask instead of the original
         */
        public double [] roundOffMask(int chn, double xCenter, double yCenter){
4833 4834
        	int dWidth=  (eyesisCameraParameters.getSensorWidth(chn) -1)/eyesisCameraParameters.getDecimateMasks(chn)+1;
        	int dHeight= (eyesisCameraParameters.getSensorHeight(chn)-1)/eyesisCameraParameters.getDecimateMasks(chn)+1;
4835 4836
        	DoubleGaussianBlur gb=new DoubleGaussianBlur();

4837 4838
        	int iXC=(int) Math.round(xCenter/eyesisCameraParameters.getDecimateMasks(chn));
        	int iYC=(int) Math.round(yCenter/eyesisCameraParameters.getDecimateMasks(chn));
4839 4840 4841 4842 4843 4844 4845 4846 4847
        	double r0=iXC;
        	r0 = Math.min(r0, iYC);
        	r0 = Math.min(r0, dWidth - iXC);
        	r0 = Math.min(r0, dHeight - iYC);
        	int ir02=(int) Math.round(r0*r0);
        	System.out.println("iXC="+iXC);
        	System.out.println("iYC="+iYC);
        	System.out.println("initial ir02="+ir02+ "("+Math.sqrt(ir02)+")");
        	for (int i = 0; i < this.sensorMasks[chn].length; i++) if (this.sensorMasks[chn][i]<0.5) {
4848
        		int ix = (i % dWidth) - iXC;
4849 4850 4851 4852 4853 4854 4855
        		int iy = (i / dWidth) - iYC;
        		int ir2=ix*ix + iy*iy;
        		if (ir2 < ir02) ir02 = ir2;
        	}
        	System.out.println("second ir02="+ir02+ "("+Math.sqrt(ir02)+")");
        	double [] mask= new double[this.sensorMasks[chn].length];
        	for (int i = 0; i < mask.length; i++) {
4856
        		int ix = (i % dWidth) - iXC;
4857 4858 4859 4860 4861
        		int iy = (i / dWidth) - iYC;
        		mask[i]=((ix*ix + iy*iy) > ir02)?0.0:1.0;
        	}
        	// blur result
        	double [][] preMask=preCalculateSingleImageMask(chn,
4862 4863 4864
        			eyesisCameraParameters.getDecimateMasks(chn),
        			eyesisCameraParameters.getSensorWidth(chn),
        			eyesisCameraParameters.getSensorHeight(chn),
4865 4866 4867 4868 4869 4870 4871 4872 4873
        			eyesisCameraParameters.shrinkGridForMask);

        	if (preMask==null) return null; //nothing in this channel
        	double rAverage=preMask[0][0];
        	double rAverageNum=preMask[0][1];
        	if (rAverageNum==0.0) return null; // nothing to blur/process for this channel
        	rAverage/=rAverageNum; // average distance to the fartherst node from the current
        	double      sigma = eyesisCameraParameters.maskBlurSigma;
        	if(sigma<0) sigma*=-rAverage;
4874
        	gb.blurDouble(mask, dWidth, dHeight, sigma/eyesisCameraParameters.getDecimateMasks(chn), sigma/eyesisCameraParameters.getDecimateMasks(chn), 0.01);
4875 4876 4877 4878 4879
        	for (int i=0;i < mask.length;i++){
        		this.sensorMasks[chn][i] = Math.min(this.sensorMasks[chn][i],mask[i]);
        	}
        	return this.sensorMasks[chn];
        }
4880 4881


4882
        public double [] calculateImageGridMask(int imgNum) {
4883
        	int chn = this.gIP[imgNum].channel; // getChannel()
4884 4885
        	return calculateImageGridMask(
        			imgNum,
4886 4887 4888
        			eyesisCameraParameters.getDecimateMasks(chn),
        			eyesisCameraParameters.getSensorWidth(chn),
        			eyesisCameraParameters.getSensorHeight(chn),
4889 4890 4891 4892 4893 4894 4895 4896
        			eyesisCameraParameters.shrinkGridForMask,
        			eyesisCameraParameters.maskBlurSigma);
        }

        public double [] calculateImageGridMask(int imgNum, int decimate, int width, int height, int shrinkGridForMask, double sigmaUV) {
        	int dWidth=  (width -1)/decimate+1;
        	int dHeight= (height-1)/decimate+1;
        	DoubleGaussianBlur gb=new DoubleGaussianBlur();
4897
        	if ((this.debugLevel>1) && (SDFA_INSTANCE==null)) SDFA_INSTANCE=new ShowDoubleFloatArrays();
4898 4899 4900 4901 4902 4903 4904 4905 4906
        	if (this.debugLevel>2)System.out.println("calculateSensorMasks("+width+","+height+","+shrinkGridForMask+","+sigmaUV+")");
        	double [][] preMask=preCalculateSingleImageMask(imgNum, decimate, width, height, shrinkGridForMask);
        	if (preMask==null) return null; //nothing in this channel
        	double rAverage=preMask[0][0];
        	double rAverageNum=preMask[0][1];
        	if (rAverageNum==0.0) return null; // nothing to blur/process for this channel
        	rAverage/=rAverageNum; // average distance to the fartherst node from the current
        	double      sigma=sigmaUV;
        	if(sigma<0) sigma*=-rAverage;
4907
// old version, trying new - will influence all sensor masks!!
4908 4909 4910 4911 4912 4913 4914 4915 4916
//        	gb.blurDouble(preMask[1], dWidth, dHeight, sigma/decimate, sigma/decimate, 0.01);
        	double [] mask0=preMask[1].clone();
        	gb.blurDouble(preMask[1], dWidth, dHeight, sigma/decimate, sigma/decimate, 0.01);
        	for (int i=0;i<preMask[1].length;i++){
				double d=2.0*(preMask[1][i]-0.5);
				preMask[1][i]=((mask0[i]>0) && (d>0))?(d*d):0.0;
        	}
        	return preMask[1];
        }
4917 4918 4919



4920
        /**
4921
         *
4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980
         * @param imgNum number of image to process
         * @param decimate - reduce image resolution for the mask
         * @param width - image width (actual will be divided by decimate
         * @param height- image height (actual will be divided by decimate
         * @param shrinkGridForMask shrink defined grid before bluring
         * @return array of 2 rows - [0] has just rAverage and rAverageNum for the average radius of the grid [1] - mask (1.0/0.0)
         *         or null if there are no grid nodes at all;
         */
        public double [][] preCalculateSingleImageMask(
        		int imgNum,
        		int decimate,
        		int width,
        		int height,
        		int shrinkGridForMask){
        	if (!this.gIP[imgNum].enabled) return null; // this image is disabled, ignore it
        	int [][] dirs={{-1,0},{1,0},{0,-1},{0,1}};
        	double rAverage=0.0;
        	double rAverageNum=0.0;
        	int i;
        	int dWidth=  (width -1)/decimate+1;
        	int dHeight= (height-1)/decimate+1;
        	double [] mask = new double [dWidth*dHeight];
        	boolean hasGrid=false;
        	for (i=0;i<this.gIP[imgNum].pixelsXY.length;i++) if ((this.gIP[imgNum].pixelsXY[i]!=null) &&(this.gIP[imgNum].pixelsXY[i][0]>=0)) {
        		hasGrid=true;
        		break;
        	}
        	if (!hasGrid) return null; // image has no grid nodes
        	int minU=this.gIP[imgNum].pixelsUV[i][0];
        	int minV=this.gIP[imgNum].pixelsUV[i][1];
        	int maxU=minU;
        	int maxV=minV;
        	for (;i<this.gIP[imgNum].pixelsXY.length;i++) if ((this.gIP[imgNum].pixelsXY[i]!=null) &&(this.gIP[imgNum].pixelsXY[i][0]>=0)) {
        		if (this.gIP[imgNum].pixelsUV[i][0]<minU) minU=this.gIP[imgNum].pixelsUV[i][0];
        		if (this.gIP[imgNum].pixelsUV[i][1]<minV) minV=this.gIP[imgNum].pixelsUV[i][1];
        		if (this.gIP[imgNum].pixelsUV[i][0]>maxU) maxU=this.gIP[imgNum].pixelsUV[i][0];
        		if (this.gIP[imgNum].pixelsUV[i][1]>maxV) maxV=this.gIP[imgNum].pixelsUV[i][1];
        	}
        	if (this.debugLevel>2)System.out.println("calculateSensorMasks, imgNum="+imgNum+", minU="+minU+", maxU="+maxU+", minV="+minV+", maxV="+maxV);
        	// restore the grid rectangle for u,v ->pixel-x,pixel-y
        	double [][][] pXY=new double[maxV-minV+1][maxU-minU+1][2];
        	int [][]iMask=new int [pXY.length][pXY[0].length];
        	for (int v=0;v<pXY.length;v++) for (int u=0;u<pXY[0].length;u++) {
        		pXY[v][u][0]=-1;
        		iMask[v][u]=0;
        	}
        	if (this.debugLevel>2)System.out.println("calculateSensorMasks, pXY.length="+pXY.length+", pXY[0].length="+pXY[0].length);
        	for (i=0;i<this.gIP[imgNum].pixelsXY.length;i++) if ((this.gIP[imgNum].pixelsXY!=null) &&(this.gIP[imgNum].pixelsXY[i][0]>=0)) {
        		int v=this.gIP[imgNum].pixelsUV[i][1]-minV;
        		int u=this.gIP[imgNum].pixelsUV[i][0]-minU;
        		pXY[v][u][0]=this.gIP[imgNum].pixelsXY[i][0]; // out of bounds 22
        		pXY[v][u][1]=this.gIP[imgNum].pixelsXY[i][1];
        		//    			if (this.debugLevel>2)System.out.println("calculateSensorMasks, i="+i+", pXY["+v+"]["+u+"]={"+pXY[v][u][0]+","+pXY[v][u][1]+"}");
        		iMask[v][u]=1;
        	}
        	if (this.debugLevel>3){
        		double [][] testArray=new double[3][pXY.length*pXY[0].length];
        		int index=0;
        		for (int v=0;v<iMask.length;v++) for (int u=0;u<iMask[0].length;u++){
4981 4982
        			testArray[0][index]=pXY[v][u][0];
        			testArray[1][index]=pXY[v][u][1];
4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006
        			testArray[2][index++]=iMask[v][u];

        		}
        		String [] dbgTitles={"X","Y","iMask"};
        		this.SDFA_INSTANCE.showArrays(testArray, pXY[0].length, pXY.length,  true, "original", dbgTitles);

        	}
        	// shrink the grid
        	int vMax=iMask.length-1;
        	int uMax=iMask[0].length-1;
        	if (this.debugLevel>2)System.out.println("calculateSensorMasks, uMax="+uMax+", vMax="+vMax);
        	for (int n=0;n<shrinkGridForMask;n++) {
        		for (int v=0;v<iMask.length;v++) for (int u=0;u<iMask[0].length;u++) if (iMask[v][u]>0){

        			if ((v==0) || (v==vMax) || (u==0) || (u==uMax) ||
        					(iMask[v-1][u]==-n) || (iMask[v+1][u]==-n) ||(iMask[v][u-1]==-n) || (iMask[v][u+1]==-n)) {
        				iMask[v][u]=-n-1;
        			}
        		}
        	}
        	if (this.debugLevel>3){
        		double [][] testArray1=new double[3][pXY.length*pXY[0].length];
        		int index=0;
        		for (int v=0;v<iMask.length;v++) for (int u=0;u<iMask[0].length;u++){
5007 5008
        			testArray1[0][index]=pXY[v][u][0];
        			testArray1[1][index]=pXY[v][u][1];
5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034
        			testArray1[2][index++]=iMask[v][u];

        		}
        		String [] dbgTitles={"X","Y","iMask"};
        		this.SDFA_INSTANCE.showArrays(testArray1, pXY[0].length, pXY.length,  true, "shrank", dbgTitles);
        	}

        	// now in remaining grid nodes iMask[v][u]>0 (0 and negative - no grid)
        	// accumulate pixels around the grid points
        	for (int v=0;v<iMask.length;v++) for (int u=0;u<iMask[0].length;u++)if (iMask[v][u]>0){
        		// find the radius - distance to the fartherst of the 4 (existent) neighbors (if none exist - disregard the node)
        		double r2Max=0;
        		for (int d=0;d<dirs.length;d++){
        			int u1=u+dirs[d][0];
        			int v1=v+dirs[d][1];
        			double r2;
        			if ((v1>=0) && (v1<=vMax) && (u1>=0) && (u1<=uMax)){
        				r2=(pXY[v1][u][0]-pXY[v][u][0])*(pXY[v1][u][0]-pXY[v][u][0])+
        				(pXY[v][u1][0]-pXY[v][u][0])*(pXY[v][u1][0]-pXY[v][u][0]);
        				if (r2Max<r2) r2Max=r2;
        			}
        		}
        		if (r2Max==0.0) continue; // nothing around - skip this node
        		// calculate average radius (for bluring)
        		double r=Math.sqrt(r2Max);
        		rAverage+=r;
5035
        		rAverageNum++;
5036 5037 5038 5039 5040 5041 5042 5043 5044

        		int iR= (int) Math.round(r);
        		int iX0= (int) Math.round (pXY[v][u][0]);
        		int iY0= (int) Math.round (pXY[v][u][1]);
        		int xLowLim=iX0-iR;
        		int xHighLim=iX0+iR;
        		int yLowLim=iY0-iR;
        		int yHighLim=iY0+iR;
        		if (xLowLim<0)       xLowLim=0;
5045
// decimation apply below
5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060
        		if (xHighLim>=width) xHighLim=width-1;
        		if (yLowLim<0)       yLowLim=0;
        		if (yHighLim>=height)yHighLim=height-1;
        		for (int iY=yLowLim;iY<=yHighLim;iY+=decimate)for (int iX=xLowLim;iX<=xHighLim;iX+=decimate){
        			double r2=(iX-pXY[v][u][0])*(iX-pXY[v][u][0])+(iY-pXY[v][u][1])*(iY-pXY[v][u][1]);
        			if (r2<=r2Max) {
        				if (decimate==1) mask[iY*width+iX]=1.0;
        				else mask[(iY/decimate)*dWidth+(iX/decimate)]=1.0;
        			}
        		}
        	}
        	double [][] result= {{rAverage,rAverageNum},mask};
        	return  result;

        }
5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079
// Methods related to Talon (instructor/student) systems
        // calculates UV offset (no rotations) for test grid by best contrast match to  known grid
//        int [] correlateGrids(int base_grid_num, int test_grid_num, boolean invert_color) {
// returns
        int [] correlateGrids(
        		int        base_width,
        		float [][] base_pixels,
        		int        test_width,
        		float [][] test_pixels,
        		boolean    invert_color,
        		int        extra_search) {
        	int base_height = base_pixels[0].length/base_width;
        	int test_height = test_pixels[0].length/test_width;
        	int search_rad = Math.max(
        			(Math.max(base_width,  test_width)- Math.min(base_width,  test_width) + 1) /2,
        			(Math.max(base_height, test_height)-Math.min(base_height, test_height) + 1) /2) + extra_search;
        	int offs_x = base_width/2 -  test_width/2;  // subtract from test.x
        	int offs_y = base_height/2 - test_height/2; // subtract from test.y
        	double [] corr = new double [(2*search_rad + 1)*(2*search_rad + 1)];
5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242
        	for (int dy = -search_rad; dy <= search_rad; dy++) {
        		for (int dx = -search_rad; dx <= search_rad; dx++) {
        			double sum = 0;
        			for (int y0 = 0; y0 < base_height; y0++) {
        				int y1 = y0 - offs_y - dy;
        				if ((y1 >= 0) && (y1 < test_height)) {

        					for (int x0 = 0; x0 < base_width; x0++) {
        						int x1 = x0 - offs_x - dx;
        						if ((x1 >= 0) && (x1 < test_width)) {
        							sum+= base_pixels[INDEX_CONTRAST][y0*base_width + x0] * test_pixels[INDEX_CONTRAST][y1*test_width + x1];
        						}
        					}
        				}
        			}
        			corr[(2*search_rad + 1)*(search_rad + dy) +(search_rad + dx)] = sum;
        		}
        	}
        	int [] indx_max_even_odd = {0,0};
        	for (int i = 1; i < corr.length; i++) {
        		int parity = ((i /(2*search_rad + 1)) +  (i %(2*search_rad + 1))) & 1;
        		if (corr[indx_max_even_odd[parity]] <corr[i]) {
        			indx_max_even_odd[parity] = i;
        		}
        	}
        	// find first non-zero matching cell
        	int indx0=-1,indx1=-1;
        	int [] rslt = new int[3];
        	for (int parity = 0; parity < 2; parity++) {
        		int dy = indx_max_even_odd[parity] / (2*search_rad + 1) - search_rad;
        		int dx = indx_max_even_odd[parity] % (2*search_rad + 1) - search_rad;

        		first_nonzero:

        			for (int y0 = 0; y0 < base_height; y0++) {
        				int y1 = y0 - offs_y - dy;
        				if ((y1 >= 0) && (y1 < test_height)) {
        					for (int x0 = 0; x0 < base_width; x0++) {
        						int x1 = x0 - offs_x - dx;
        						if ((x1 >= 0) && (x1 < test_width)) {
        							indx0 = y0*base_width + x0;
        							indx1 = y1*test_width + x1;
        							if ((base_pixels[INDEX_CONTRAST][indx0] > 0 )&&
        									(test_pixels[INDEX_CONTRAST][indx1] > 0)) {
        								break first_nonzero;
        							}
        						}
        					}
        				}
        			}
        		// test grid with index indx1 matches base grid with indx0
        		rslt[0] = Math.round(base_pixels[INDEX_U][indx0] - test_pixels[INDEX_U][indx1]);
        		rslt[1] = Math.round(base_pixels[INDEX_V][indx0] - test_pixels[INDEX_V][indx1]);
        		rslt[2] = 0; // rotation
        		if (((rslt[0] + rslt[1] + (invert_color?1:0)) & 1) == 0) break;
        	}
        	return rslt;
        }

        int [] correlateWithPattern(
        		PatternParameters patternParameters,
        		int        test_width,
        		float [][] test_pixels,
        		boolean    invert_color,
        		int        extra_search,
        		double     sigma,
        		double []  sensor_wh, // test set pixels width/height pair to reduce weight near the margins (or null)
        		boolean    bdebug
        		) {
        	int base_height = patternParameters.gridGeometry.length;
        	int base_width =  patternParameters.gridGeometry[0].length;
        	int index_mask =  3;
        	float [][] base_pixels = new float [INDEX_CONTRAST+1][base_height*base_width];
    		int indx = 0;
        	for (int iv = 0; iv < base_height; iv++) {
            	for (int iu = 0; iu < base_width; iu++) {
            		base_pixels[INDEX_U][indx] =        iu - patternParameters.U0;
            		base_pixels[INDEX_V][indx] =        iv - patternParameters.V0;
            		base_pixels[INDEX_CONTRAST][indx] = (float) patternParameters.gridGeometry[iv][iu][index_mask];
            		indx++;
            	}
        	}
        	return correlateGrids(
            		base_width,
            		base_pixels,
            		test_width,
            		test_pixels,
            		invert_color,
            		extra_search,
            		sigma,
            		sensor_wh, // test set pixels width/height pair to reduce weight near the margins (or null)
            		bdebug
            		) ;
        }

        int [] correlateGrids(
        		int        base_width,
        		float [][] base_pixels,
        		int        test_width,
        		float [][] test_pixels,
        		boolean    invert_color,
        		int        extra_search,
        		double     sigma,
        		double []  sensor_wh, // test set pixels width/height pair to reduce weight near the margins (or null)
        		boolean    bdebug
        		) { // Gaussian blur sigma to subtract
        	int base_height = base_pixels[0].length/base_width;
        	int test_height = test_pixels[0].length/test_width;
        	int search_rad = Math.max(
        			(Math.max(base_width,  test_width)- Math.min(base_width,  test_width) + 1) /2,
        			(Math.max(base_height, test_height)-Math.min(base_height, test_height) + 1) /2) + extra_search;
        	int offs_x = base_width/2 -  test_width/2;  // subtract from test.x
        	int offs_y = base_height/2 - test_height/2; // subtract from test.y
        	double [] corr = new double [(2*search_rad + 1)*(2*search_rad + 1)];
        	double [] base_contrast = new double [base_pixels[INDEX_U].length];
        	double [] test_contrast = new double [test_pixels[INDEX_U].length];
        	for (int i = 0; i < base_contrast.length; i++) {
        		base_contrast[i] = base_pixels[INDEX_CONTRAST][i];
        	}
        	for (int i = 0; i < test_contrast.length; i++) {
        		test_contrast[i] = test_pixels[INDEX_CONTRAST][i];
        	}
        	double [] tmp_b = base_contrast.clone();
        	DoubleGaussianBlur gb = new DoubleGaussianBlur();
        	gb.blurDouble(tmp_b, base_width, base_height, sigma, sigma, 0.01);
        	// subtract DC
        	double s = 0.0;
        	for (int i = 0; i < base_contrast.length; i++) {
        		base_contrast[i] -= tmp_b[i];
        		s+=base_contrast[i];
        	}
        	s/=base_contrast.length;
        	for (int i = 0; i < base_contrast.length; i++) {
        		base_contrast[i] -= s;
        	}

        	tmp_b = test_contrast.clone();
        	gb.blurDouble(tmp_b, test_width, test_height, sigma, sigma, 0.01);
        	// subtract DC
        	s = 0.0;
        	for (int i = 0; i < test_contrast.length; i++) {
        		test_contrast[i] -= tmp_b[i];
        		s+=test_contrast[i];
        	}
        	s/=test_contrast.length;
        	for (int i = 0; i < test_contrast.length; i++) {
        		test_contrast[i] -= s;
        	}

        	if (sensor_wh != null) {
        		double x0 = sensor_wh[0]/2;
        		double y0 = sensor_wh[1]/2;
        		for (int i = 0; i < test_contrast.length; i++) {
        			double x = (test_pixels[INDEX_PX][i] - x0)/x0;
        			double y = (test_pixels[INDEX_PY][i] - y0)/y0;
        			test_contrast[i] *= (1.0 - x*x)* (1.0 - y*y);
        		}
        	}
        	if (bdebug) {
        		(new ShowDoubleFloatArrays()).showArrays(base_contrast, base_width, base_height, "base_sigma-"+sigma);
        		(new ShowDoubleFloatArrays()).showArrays(test_contrast, test_width, test_height, "test_sigma-"+sigma);
        	}

5243 5244 5245 5246 5247 5248 5249 5250 5251 5252
        	for (int dy = -search_rad; dy <= search_rad; dy++) {
            	for (int dx = -search_rad; dx <= search_rad; dx++) {
            		double sum = 0;
            		for (int y0 = 0; y0 < base_height; y0++) {
            			int y1 = y0 - offs_y - dy;
            			if ((y1 >= 0) && (y1 < test_height)) {

                    		for (int x0 = 0; x0 < base_width; x0++) {
                    			int x1 = x0 - offs_x - dx;
                    			if ((x1 >= 0) && (x1 < test_width)) {
5253
                    				sum+= base_contrast[y0*base_width + x0] * test_contrast[y1*test_width + x1];
5254 5255 5256 5257 5258 5259 5260
                    			}
                    		}
            			}
            		}
            		corr[(2*search_rad + 1)*(search_rad + dy) +(search_rad + dx)] = sum;
            	}
        	}
5261 5262 5263 5264
           	if (bdebug) {
        		(new ShowDoubleFloatArrays()).showArrays(corr, "corr_sigma-"+sigma);
           	}
        	int [] indx_max_even_odd = {-1,-1};
5265 5266
        	for (int i = 1; i < corr.length; i++) {
        		int parity = ((i /(2*search_rad + 1)) +  (i %(2*search_rad + 1))) & 1;
5267
        		if ((indx_max_even_odd[parity] < 0) || (corr[indx_max_even_odd[parity]] < corr[i])) {
5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303
        			indx_max_even_odd[parity] = i;
        		}
        	}
    		// find first non-zero matching cell
    		int indx0=-1,indx1=-1;
    		int [] rslt = new int[3];
        	for (int parity = 0; parity < 2; parity++) {
        		int dy = indx_max_even_odd[parity] / (2*search_rad + 1) - search_rad;
        		int dx = indx_max_even_odd[parity] % (2*search_rad + 1) - search_rad;

        		first_nonzero:

        			for (int y0 = 0; y0 < base_height; y0++) {
        				int y1 = y0 - offs_y - dy;
        				if ((y1 >= 0) && (y1 < test_height)) {
        					for (int x0 = 0; x0 < base_width; x0++) {
        						int x1 = x0 - offs_x - dx;
        						if ((x1 >= 0) && (x1 < test_width)) {
        							indx0 = y0*base_width + x0;
        							indx1 = y1*test_width + x1;
        							if ((base_pixels[INDEX_CONTRAST][indx0] > 0 )&&
        									(test_pixels[INDEX_CONTRAST][indx1] > 0)) {
        								break first_nonzero;
        							}
        						}
        					}
        				}
        			}
            	// test grid with index indx1 matches base grid with indx0
            	rslt[0] = Math.round(base_pixels[INDEX_U][indx0] - test_pixels[INDEX_U][indx1]);
            	rslt[1] = Math.round(base_pixels[INDEX_V][indx0] - test_pixels[INDEX_V][indx1]);
            	rslt[2] = 0; // rotation
            	if (((rslt[0] + rslt[1] + (invert_color?1:0)) & 1) == 0) break;
        	}
        	return rslt;
        }
5304 5305


5306
        // Set initial shifts for the grids that do not have absolute match from the one in the same set that does
5307

5308
        // end of class DistortionCalibrationData
5309 5310 5311 5312 5313 5314 5315


    // Set initial shifts for the grids that do not have absolute match from the one in the same set that does

    // end of class DistortionCalibrationData
}