package com.elphel.imagej.orthomosaic;

import java.awt.Point;
import java.awt.Rectangle;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import com.elphel.imagej.calibration.CalibrationFileManagement;
import com.elphel.imagej.cameras.CLTParameters;
import com.elphel.imagej.common.DoubleGaussianBlur;
import com.elphel.imagej.common.GenericJTabbedDialog;
import com.elphel.imagej.common.ShowDoubleFloatArrays;
import com.elphel.imagej.gpu.GPUTileProcessor;
import com.elphel.imagej.gpu.TpTask;
import com.elphel.imagej.tileprocessor.ImageDtt;
import com.elphel.imagej.tileprocessor.OpticalFlow;
import com.elphel.imagej.tileprocessor.TDCorrTile;
import com.elphel.imagej.tileprocessor.TileNeibs;

import ij.ImagePlus;
import ij.ImageStack;
import ij.Prefs;
import ij.gui.PointRoi;
import ij.io.FileSaver;
import ij.process.FloatPolygon;
import ij.text.TextWindow;


public class OrthoMapsCollection  implements Serializable{
	private static final long serialVersionUID = 1L;
	public static final int MODE_IMAGE = 0;
	public static final int MODE_ALT =   1;
	public static final int MODE_MASK =  2;
	
	public static final String [] KEY_DIRS= {"rootDirectory", // from EyesisCorrectionParameters
			"sourceDirectory","linkedModels","videoDirectory","x3dDirectory","resultsDirectory","scenesDirectory",
			"kernelsDirectory", "patternsDirectory"};
	public static final String [] NAME_FH = {"full","half"};
	public static final String [] NAME_MO = {"main","other"};

	OrthoMap []                                 ortho_maps;
	transient HashMap<Double,Integer>           map_index;
	transient HashMap<String,Integer>           map_index_string;
	transient ArrayList<AltitudeMismatchKernel> kernels;
	transient ArrayList<GroundObjectPattern>    patterns;
	
	
	private void writeObject(ObjectOutputStream oos) throws IOException { // ?
		oos.defaultWriteObject();
		oos.writeObject(AltitudeMismatchKernel.getKernelsDirectory());
		oos.writeObject(kernels);
		oos.writeObject(GroundObjectPattern.getPatternsDirectory());		
		oos.writeObject(patterns);
//		oos.writeObject(path);
		// ortho_maps is not transient
	}
	@SuppressWarnings("unchecked")
	private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
		ois.defaultReadObject();
		AltitudeMismatchKernel.setKernelsDirectory((String) ois.readObject());
		kernels = (ArrayList<AltitudeMismatchKernel>) ois.readObject();
		GroundObjectPattern.setPatternsDirectory((String) ois.readObject());
		patterns = (ArrayList<GroundObjectPattern>)   ois.readObject();
		
		reindex(); // will recreate map_index, map_index_string
		// lla is not transient
	}
	
	public OrthoMapsCollection(
			String path) {
		if (path.endsWith(".list")) {
			String[] scenes0 = new String[1];
			kernels = new ArrayList<AltitudeMismatchKernel>(); 
			patterns = new ArrayList<GroundObjectPattern>();
			
			String[] paths = getPathsAndScenesFromSourceList(
					path,
					scenes0,
					kernels,   // ArrayList<AltitudeMismatchKernel> kernels,
					patterns); // ArrayList<GroundObjectPattern>    patterns);
			
			String scenes_path = scenes0[0];
			double [][] affine = {{1,0,0},{0,1,0}}; // maybe later calculate from mage_enuatr
			ortho_maps = new OrthoMap[paths.length];
			
			for (int n = 0; n < ortho_maps.length; n++) {
				String name = OrthoMap.getNameFromPath(paths[n]);
				ortho_maps[n] = new OrthoMap(paths[n]); // , scene_path);
				ortho_maps[n].setAffine(affine);
			}
		} else {
			System.out.println("OrthoMapsCollection(): path should end with \".list\"");
		}
		Arrays.sort(ortho_maps);
		reindex();
	}
	
	public static double [][] unityAffine() {
		return new double [][] {{1,0,0},{0,1,0}};
	}
	
	// add update kernels, update patterns
	public void updateKernels (
			String path) {
		if (path.endsWith(".list")) {
			kernels = new ArrayList<AltitudeMismatchKernel>(); 
			getPathsAndScenesFromSourceList(
					path,
					null,
					kernels, // ArrayList<AltitudeMismatchKernel> kernels,
					null);   // ArrayList<GroundObjectPattern>    patterns);
		} else {
			System.out.println("updateKernels(): path should end with \".list\"");
		}
	}
	
	public void updatePatterns (
			String path) {
		if (path.endsWith(".list")) {
			patterns = new ArrayList<GroundObjectPattern>();
			getPathsAndScenesFromSourceList(
					path,
					null,
					null, // ArrayList<AltitudeMismatchKernel> kernels,
					patterns);   // ArrayList<GroundObjectPattern>    patterns);
		} else {
			System.out.println("updatePatterns(): path should end with \".list\"");
		}
	}
	
	
	static String getSceneDir(String scenes_path, String name) {
		File [] scene_dirs = (new File(scenes_path)).listFiles(); // may contain non-directories, will be filtered by filterScenes
		//scene_dirs[3].listFiles()
		Arrays.sort(scene_dirs);
		for (int i = scene_dirs.length-1; i>=0; i--) {
			if (scene_dirs[i].isDirectory()) {
				String dirname = scene_dirs[i].getName();
				if (name.compareTo(dirname) >= 0) {
					// verify it has needed scene
					File [] segment = scene_dirs[i].listFiles();
					for (int j = 0; j < segment.length; j++) {
						if (segment[j].getName().equals(name)) {
							return segment[j].toString(); 
						}
					}
				}
			}
		}
		return null;
	}
	
	
	
	public int reindex() {
		map_index = new HashMap<Double,Integer>();
		map_index_string = new HashMap<String,Integer>();
		for (int i = 0; i < ortho_maps.length; i++) {
			map_index.put(ortho_maps[i].ts, i);
			map_index_string.put(ortho_maps[i].name,i);
		}
		return ortho_maps.length;
	}
	
	OrthoMap [] getMaps() {
		return ortho_maps;
	}
	
	public String[] getNames() {
		String [] names = new String [ortho_maps.length];
		for (int n = 0; n < ortho_maps.length; n++) {
			names[n] = ortho_maps[n].getName();
		}
		return names;
	}

	public void getAllTemperatures() {
		for (int n = 0; n < ortho_maps.length; n++) {
			ortho_maps[n].getTemperature();
			ortho_maps[n].getTemperatureAndTelemetry();
		}
	}
	
	public void updateLLa() { // make automatic by saving/testing file modification stamp?
		for (int n = 0; n < ortho_maps.length; n++) {
			ortho_maps[n].updateLLA();
		}
		updateAGL();
	}
	
	public void updateAGL() {
		for (int n = 0; n < ortho_maps.length; n++) {
			ortho_maps[n].agl = Double.NaN;
			ortho_maps[n].agl = ortho_maps[n].getAGL();
		}		
	}
	
	public void updateNumberScenes() {
		for (int n = 0; n < ortho_maps.length; n++) {
			ortho_maps[n].num_scenes = -1;
			ortho_maps[n].num_scenes = ortho_maps[n].getNumberScenes();
		}		
	}	
	
	public void updateSfmGain() {
		for (int n = 0; n < ortho_maps.length; n++) {
			ortho_maps[n].sfm_gain = Double.NaN;
			ortho_maps[n].sfm_gain = ortho_maps[n].getSfmGain();
		}		
	}	
	
	/*
	public static String[] getPathsFromSorceList(
			String files_list) {
		return getPathsAndScenesFromSourceList(files_list, null);
	}
    */
	public static String[] getPathsAndScenesFromSourceList(
			String files_list,
			String[] scenes0,
			ArrayList<AltitudeMismatchKernel> kernels,
			ArrayList<GroundObjectPattern>    patterns) {
		List<String> lines;
		List<String> rel_files = new ArrayList<String>(); 
		Path seq_path = Paths.get(files_list);
		try {
			lines = Files.readAllLines(seq_path, StandardCharsets.UTF_8);
			//                lines.stream().forEach(System.out::println);
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		Path base_path = seq_path.getParent();
		HashMap<String,String> dir_map = new HashMap<String,String>();
		for (String line:lines){
			if (line.split("#").length > 0) {
				String[] tokens = line.split("#")[0].trim().split("[\\s,;=]+");
				if ((tokens.length > 2) && (tokens[0].toUpperCase().equals("SET"))) {
					parse_set:
					{
						for (String dir_name:KEY_DIRS) if (dir_name.equals(tokens[1])) {
							dir_map.put(dir_name,tokens[2]);
							System.out.println("Parsed SET: "+tokens[1]+" in line: "+line);
							break parse_set;
						}
						System.out.println("*********** Unknown SET: "+tokens[1]+" in line: "+line);
					}
				} else if ((tokens.length > 0) && (tokens[0].toUpperCase().equals("KERNEL"))) {
					if ((kernels != null) && (tokens.length > 4)) {
						kernels.add(new AltitudeMismatchKernel(
								tokens[1],
								Integer.parseInt(tokens[2]),
								Double.parseDouble(tokens[3]),
								Double.parseDouble(tokens[4])));
					}
				} else if ((tokens.length > 0) && (tokens[0].toUpperCase().equals("PATTERN"))) {
					if ((patterns != null) && (tokens.length > 4)) {
						patterns.add(new GroundObjectPattern(
								tokens[1],
								Integer.parseInt(tokens[2]),
								tokens[3],
								LocalDateTime.parse(tokens[4])));
					}
				} else if ((tokens.length == 1) && (tokens[0].length() > 0)) {
					rel_files.add(tokens[0]);
				}
			}
		}
		if (dir_map.containsKey("rootDirectory")) {
			base_path=base_path.resolve(Paths.get(dir_map.get("rootDirectory")));
			File base_dir = new File(base_path.toString());
		}
		String sourceDirectory = base_path.toString();
		// set sourceDirectory: 
		if (dir_map.containsKey("sourceDirectory")) {
			sourceDirectory=(base_path.resolve(Paths.get(dir_map.get("sourceDirectory")))).toString();
		}
		Path source_path = Paths.get(sourceDirectory);
		//			File source_dir = new File(source_path.toString());
		String [] paths = new String[rel_files.size()];
		for (int i = 0; i < paths.length; i++) {
			paths[i] = (source_path.resolve(Paths.get(rel_files.get(i)))).toString();
		}
		if (dir_map.containsKey("scenesDirectory")) {
			if (scenes0 != null) {
				scenes0[0]=(base_path.resolve(Paths.get(dir_map.get("scenesDirectory")))).toString();
			}
		}
		
		String kernelsDirectory = base_path.toString();
		// set sourceDirectory: 
		if (dir_map.containsKey("kernelsDirectory")) {
			kernelsDirectory=(base_path.resolve(Paths.get(dir_map.get("kernelsDirectory")))).toString();
		}
		AltitudeMismatchKernel.setKernelsDirectory(kernelsDirectory);
		
		String patternsDirectory = base_path.toString();
		// set sourceDirectory: 
		if (dir_map.containsKey("patternsDirectory")) {
			patternsDirectory=(base_path.resolve(Paths.get(dir_map.get("patternsDirectory")))).toString();
		}
		GroundObjectPattern.setPatternsDirectory(patternsDirectory);
		
		return paths;
	}
	
	
	/**
	 * Get rectified bounds of all provided ortho images relative to the origin (vertical
	 * point) of the first one in pixels at the provided zoom level. Center of the first
	 * image correspond to the {0,0} point from which the bounds are counted
	 * @param zoom_level zoom level - 0 corresponds to 1pix=1cm scale, +1 - to 1pix = 0.5cm
	 * @return {{min_x,max_x},{min_y, max_y}} relative to the origin of the first image
	 */
	public int [][] getBoundsPixels(
			int zoom_level, // maps[0] as a reference
			int [] indices) {
		double [][] bounds_meters = getBoundsMeters(indices);
		double pix_size = OrthoMap.getPixelSizeMeters (zoom_level);
		int [][] bounds_pix = new int[2][2];
		for (int n = 0; n < bounds_pix.length; n++) {
			bounds_pix[n][0] = (int) Math.floor(bounds_meters[n][0]/pix_size);
			bounds_pix[n][1] = (int) Math.ceil (bounds_meters[n][1]/pix_size);
		}
		return bounds_pix;
	}
	public int [][] getBoundsPixels(
			int           zoom_level, // maps[0] as a reference
			int []        indices,
			double [][][] affines) {
		double [][] bounds_meters = (affines == null) ? getBoundsMeters(indices):getBoundsMeters(indices,affines);
		double pix_size = OrthoMap.getPixelSizeMeters (zoom_level);
		int [][] bounds_pix = new int[2][2];
		for (int n = 0; n < bounds_pix.length; n++) {
			bounds_pix[n][0] = (int) Math.floor(bounds_meters[n][0]/pix_size);
			bounds_pix[n][1] = (int) Math.ceil (bounds_meters[n][1]/pix_size);
		}
		return bounds_pix;
	}

	public int [][] getBoundsPixels(
			int zoom_level) { // maps[0] as a reference
		return getBoundsPixels(zoom_level, null);
	}
	//double [][][] affines
	/**
	 * Get rectified bounds of all provided ortho images relative to the origin (vertical
	 * point) of the first one in meters
	 * @return {{min_x,max_x},{min_y,max_y}} bounds that include all provided maps
	 *         relative to the origin (vertical point) of the first image
	 */
	/*
	public double [][] getBoundsMeters(){ // maps[0] as a reference
		double [][] bounds = ortho_maps[0].getBoundsMeters(true);
		for (int nmap = 1; nmap < ortho_maps.length; nmap++) {
			double [][] bounds_other = ortho_maps[nmap].getBoundsMeters(true);
			double [] enuOffset = ortho_maps[0].enuOffsetTo(ortho_maps[nmap]);
			double [] rd = {enuOffset[0], -enuOffset[1]}; // {right,down} of the image 
			for (int n = 0; n < bounds.length; n++) {
				bounds[n][0] = Math.min(bounds[n][0],bounds_other[n][0]+ rd[n]);
				bounds[n][1] = Math.max(bounds[n][1],bounds_other[n][1]+ rd[n]);
			}
		}
		return bounds;
	}
	*/
	public double [][] getBoundsMeters(){ // maps[0] as a reference
		return getBoundsMeters(null);
	}
	/**
	 * Get rectified bounds of all (if indices==null) or selected by indices[] 
	 * provided ortho images relative to the origin (vertical
	 * point) of the first one in meters
	 * @param indices - image indices to process or null to process all images
	 * @return {{min_x,max_x},{min_y,max_y}} bounds that include all provided maps
	 *         relative to the origin (vertical point) of the first image
	 */

	public double [][] getBoundsMeters(int [] indices){ // maps[0] as a reference
		if (indices == null) {
			indices = new int [ortho_maps.length];
			for (int i = 0; i < indices.length; i++) {
				indices[i] = i;
			}
		}
		double [][] bounds = ortho_maps[indices[0]].getBoundsMeters(true);
		for (int imap = 1; imap < indices.length; imap++) {
			int nmap = indices[imap];
			double [][] bounds_other = ortho_maps[nmap].getBoundsMeters(true);
			double [] enuOffset = ortho_maps[indices[0]].enuOffsetTo(ortho_maps[nmap]);
			double [] rd = {enuOffset[0], -enuOffset[1]}; // {right,down} of the image 
			for (int n = 0; n < bounds.length; n++) {
				bounds[n][0] = Math.min(bounds[n][0],bounds_other[n][0]+ rd[n]);
				bounds[n][1] = Math.max(bounds[n][1],bounds_other[n][1]+ rd[n]);
			}
		}
		return bounds;
	}

	public double [][] getBoundsMeters(
			int    [] indices,
			double [][][] affines){ // maps[0] as a reference
		if (indices == null) {
			indices = new int [ortho_maps.length];
			for (int i = 0; i < indices.length; i++) {
				indices[i] = i;
			}
		}
		if (affines == null) {
			affines = new double [ortho_maps.length][][];
			for (int i = 0; i < affines.length; i++) {
				affines[i] = ortho_maps[indices[i]].affine;
			}
		}
		double [][] bounds = ortho_maps[indices[0]].getBoundsMeters(true,affines[0]);
		for (int imap = 1; imap < indices.length; imap++) {
			int nmap = indices[imap];
			double [][] bounds_other = ortho_maps[nmap].getBoundsMeters(true,affines[imap]);
			double [] enuOffset = ortho_maps[indices[0]].enuOffsetTo(ortho_maps[nmap]);
			double [] rd = {enuOffset[0], -enuOffset[1]}; // {right,down} of the image 
			for (int n = 0; n < bounds.length; n++) {
				bounds[n][0] = Math.min(bounds[n][0],bounds_other[n][0]+ rd[n]);
				bounds[n][1] = Math.max(bounds[n][1],bounds_other[n][1]+ rd[n]);
			}
		}
		return bounds;
	}
	
	
	/**
	 * Get rectified overlap bounds of two provided ortho images relative to the origin (vertical
	 * point) of the first one in meters. Use specified affine transforms, not saved with the orto map
	 * @param ref_index - index of the first (reference) map
	 * @param other_index - index of the second (other) map
	 * @param ref_affine - specified affine transform of the reference image (referenced to its vertical_point)
	 * @param other_affine - specified affine transform of the other image (referenced to its vertical_point)
	 * @return {{min_x,max_x},{min_y,max_y}} bounds that include an overlap of the two provided maps
	 *         relative to the origin (vertical point) of the first image.
	 * Returns null if there is no overlap
	 */
	private double [][] getOverlapMeters(
			int ref_index,
			int other_index,
			double [][] ref_affine,
			double [][] other_affine){
		double [][] bounds = ortho_maps[ref_index].getBoundsMeters(true,ref_affine);
		double [][] bounds_other = ortho_maps[other_index].getBoundsMeters(true,other_affine);
		double [] enuOffset = ortho_maps[ref_index].enuOffsetTo(ortho_maps[other_index]);
		double [] rd = {enuOffset[0], -enuOffset[1]}; // {right,down} of the image 
		for (int n = 0; n < bounds.length; n++) {
			bounds[n][0] = Math.max(bounds[n][0],bounds_other[n][0]+ rd[n]);
			bounds[n][1] = Math.min(bounds[n][1],bounds_other[n][1]+ rd[n]);
			if (bounds[n][0] > bounds[n][1]) {
				return null;
			}
		}
		return bounds;
	}
	
	/**
	 * Use object coordinates (now metric) on the composite image to
	 * determine corresponding pixel coordinates of the source images
	 * @param objects ArrayList of objects with the scene name and xy coordinates
	 *                in the composite image. Will fill in pixel coordinates of the objects 
	 * @param zoom_level zoom level of the composite image
	 */
	public void reverseRender(
			ArrayList<ObjectLocation> objects,
			int         zoom_level,
			int []      indices){ // null or selected image indices
		int [][] bounds = getBoundsPixels( // should be for rectified, {-bounds[0][0], -bounds[0][1]} - exact center
				zoom_level,
				indices); 
		int width =  bounds[0][1] - bounds[0][0]; // bounds[x][0] - negative 
		int height = bounds[1][1] - bounds[1][0];
		/*
		int [] indices = new int [ortho_maps.length]; //maybe will get rid of
		for (int i = 0; i < indices.length; i++) {
			indices[i] = i;
		}
		*/
		final int reference_index = (indices == null)?0:indices[0]; 
		for (ObjectLocation ol: objects) {
			int nmap =getIndex(ol.getName());
			final double scale = 1.0/OrthoMap.getPixelSizeMeters(zoom_level);
			final double src_scale = 1.0/OrthoMap.getPixelSizeMeters(ortho_maps[nmap].orig_zoom_level); // pix per meter
			double [][] mbounds = ortho_maps[nmap].getBoundsMeters(true); // keep original bounds
			double [] enu_offset = ortho_maps[reference_index].enuOffsetTo(ortho_maps[nmap]);
			double [] scaled_out_center = { // xy center to apply affine to
					-bounds[0][0] + scale * enu_offset[0],
					-bounds[1][0] - scale * enu_offset[1]};
			double [] metric_center = { // back to meters to match objects
					scaled_out_center[0]/scale,
					scaled_out_center[1]/scale};
			double dX = ol.getMetric()[0]-metric_center[0]; // matching dX, dY in renderMulti()
			double dY = ol.getMetric()[1]-metric_center[1];
			
//			final int [][] obounds = new int [2][2]; // output (rectified, combined) image bounds, relative to thje top-left
//			for (int n = 0; n< 2; n++) {
//				obounds[n][0] = (int) Math.floor(scaled_out_center[n] + scale*mbounds[n][0]);
//				obounds[n][1] = (int) Math.ceil (scaled_out_center[n] + scale*mbounds[n][1]);
//			}
			// Output window size (keep original affine - OK if will not exactly fit)
//			final int ownd_width =  obounds[0][1] - obounds[0][0];
//			final int ownd_height = obounds[1][1] - obounds[1][0];
//			final int ownd_len = ownd_width * ownd_height;
			double [][] src_bounds=ortho_maps[nmap].getBoundsMeters (true); // using original affines
			final double [] src_center = {-src_bounds[0][0],-src_bounds[1][0]}; // x,y center offset in the source image
			final double [][] affine = ortho_maps[nmap].affine; // only here use provided
//			final int src_width =      ortho_maps[nmap].getImageData().width;
//			final int src_height =     ortho_maps[nmap].getImageData().height;
//			final float [] src_img =   ortho_maps[nmap].getImageData().data; // FIXME: will not use
			double [] xy_src = { // pixels of the source image
					src_scale * (affine[0][0]*dX + affine[0][1]*dY + affine[0][2] + src_center[0]),
					src_scale * (affine[1][0]*dX + affine[1][1]*dY + affine[1][2] + src_center[1])};
			ol.setPixels(xy_src);
		}
	}
	
	
	public ImagePlus renderMulti (
			String      title,
			int           mode,    // 0 - regular image, 1 - altitudes, 2 - black/white mask			
			boolean     show_centers,
			int         zoom_level,
			int         temp_mode, // 0 - do nothing, 1 - equalize average,2 - try to correct
			int []      origin){
		return renderMulti (
				title,        // String        title,
				mode, // int           mode,    // 0 - regular image, 1 - altitudes, 2 - black/white mask
				null,         // int []        indices, // null or which indices to use (normally just 2 for pairwise comparison)
				false,        // bounds_to_indices, // boolean       bounds_to_indices,
				temp_mode,    // int           temp_mode, // 0 - do nothing, 1 - equalize average,2 - try to correct
				null,         // double [][][] affines, // null or [indices.length][2][3]
				null,         // FineXYCorr    warp,
				show_centers, // boolean       show_centers,
				zoom_level,   // int           zoom_level,
				origin);      // int []        origin)
	}
	
	public ImagePlus renderMulti (
			String        title,
//			boolean       use_alt,
			int           mode,    // 0 - regular image, 1 - altitudes, 2 - black/white mask
			int []        indices, // null or which indices to use (normally just 2 for pairwise comparison)
			boolean       bounds_to_indices,
			int           temp_mode, // 0 - do nothing, 1 - equalize average,2 - try to correct
			double [][][] affines, // null or [indices.length][2][3] 
			FineXYCorr    warp,    // use for a single pair only
			boolean       show_centers,
			int           zoom_level,
			
			int []        origin){
		int []     wh = new int[2];
		double [][] centers = new double [(indices !=null)? indices.length: ortho_maps.length][];
		float [][] multi = renderMulti (
// 				use_alt,    // boolean     use_alt,
				mode,       // int           mode,    // 0 - regular image, 1 - altitudes, 2 - black/white mask
				indices,    // int []        indices, // null or which indices to use (normally just 2 for pairwise comparison)
				bounds_to_indices, // boolean       bounds_to_indices,
				affines,    // double [][][] affines, // null or [indices.length][2][3]
				warp,       // FineXYCorr    warp,,
				zoom_level, // int         zoom_level,
				wh,         // int []      wh,
				origin,     // int []      origin){ // maps[0] as a reference
				centers);   // double [][] centers)
		int num_images =  (indices != null)? indices.length : ortho_maps.length;
		String [] map_names = new String[num_images + (((num_images==2) && (mode == MODE_IMAGE))? 1 : 0)];
		float [][] extra_multi = new float [map_names.length][];
		for (int n = 0; n <num_images; n++) {
			int mapn= (indices != null)? indices[n] : n;
			map_names[n] = ortho_maps[mapn].getName()+"_"+ortho_maps[mapn].getLocalDateTime().toString().replace("T","_")+"_UTC";
			map_names[n] += String.format(" raw=%7.1f", ortho_maps[mapn].getTemperature());
			extra_multi[n] = multi[n];
		}
		if (map_names.length > num_images) { // inefficient way, only for display
			// correct(offset) pixel values relative to multi[0]
			int map0= (indices != null)? indices[0] : 0;
			if (mode == MODE_IMAGE) {
				for (int n = 1; n < num_images; n++) {
                    //temp_mode, // 0 - do nothing, 1 - equalize average,2 - try to correct
					int mapn= (indices != null)? indices[n] : n;
					double offs = 0;
					switch (temp_mode) {
					case 1:
						offs = ortho_maps[map0].getAveragePixel()-ortho_maps[mapn].getAveragePixel();
						break;
					case 2:
						offs = ortho_maps[mapn].getTemperature()-ortho_maps[mapn].getAveragePixel()
								-(ortho_maps[map0].getTemperature()-ortho_maps[map0].getAveragePixel());
						break;
					}
					float foffs= (float) offs;
					for (int i = 0; i < extra_multi[n].length; i++) if (!Float.isNaN(extra_multi[n][i])) {
						extra_multi[n][i]+= foffs;
					}
				}
			}
			map_names[num_images] = "Diff 0-1";
			extra_multi[num_images] = new float[extra_multi[0].length];
			for (int i = 0; i < extra_multi[num_images].length; i++) {
				extra_multi[num_images][i] = extra_multi[1][i]-extra_multi[0][i];
			}
		}
		ImageStack stack;
		if (mode == MODE_MASK) {
			title +="-MASK";
			byte [][] byte_pix = new byte[multi.length][]; // multi[0].length];
			for (int n = 0; n < multi.length; n++) { // slow
				byte_pix[n] = new byte[multi[n].length];
				for (int i = 0; i < multi[n].length; i++) {
					byte_pix[n][i] = (byte) (Float.isNaN(multi[n][i]) ? 0 : 0xff);
				}
			}
	        stack = ShowDoubleFloatArrays.makeStack( 
	        		byte_pix,
	        		wh[0],
	        		wh[1],
	        		map_names);
		} else {
			stack = ShowDoubleFloatArrays.makeStack( 
        		extra_multi,
        		wh[0],
        		wh[1],
        		map_names,
        		false);
		}
        ImagePlus imp = new ImagePlus(title, stack);
        if (show_centers) {
        	PointRoi roi = new PointRoi();
        	for (int i = 0; i < centers.length; i++) {
        		roi.addPoint(centers[i][0],centers[i][1], i+1);
        	}
        	roi.setOptions("label");
        	imp.setRoi(roi);
        }
        imp.show(); // debugging
        return imp;
        
	}
	
	
	/**
	 * Rectify and render multiple images (as slices) matching vert_meters to
	 * their (vert_meters points) provided IMS coordinates
	 * @param maps array of the ortho images 
	 * @param zoom_level zoom level (0 - 1pix=1cm, 1 - 1pix=0.5cm, -1 - 1pix=2cm
	 * @param wh null or int [2], will return {width, height}
	 * @param origin - null or double [2], will return {x0, y0} in pixels
	 * @param centers - null or double[maps.length][] - will return image centers coordinates
	 * @return
	 */
	public float [][] renderMulti (
			int           mode,    // 0 - regular image, 1 - altitudes, 2 - black/white mask			
			int         zoom_level,
			int []      wh,
			int []      origin, // maps[0] as a reference
			double [][] centers){
		return renderMulti (
//				use_alt,    // boolean       use_alt,
				mode, // int           mode,    // 0 - regular image, 1 - altitudes, 2 - black/white mask
				null,       // int []        indices,
				false,      // boolean       bounds_to_indices,
				null,       // double [][][] affines,
				null,       // FineXYCorr    warp,
				zoom_level, // int           zoom_level,
				wh,         // int []        wh,
				origin,     // int []        origin, // maps[0] as a reference
				centers);   // double [][]   centers)
	}
	
	public float [][] renderMulti (
//			boolean       use_alt,
			int           mode,    // 0 - regular image, 1 - altitudes, 2 - black/white mask			
			int []        indices, // null or which indices to use (normally just 2 for pairwise comparison)
			boolean       bounds_to_indices,
			double [][][] affines, // null or [indices.length][2][3] 
			FineXYCorr    warp,
			int           zoom_level,
			int []        wh,
			int []        origin, // maps[0] as a reference
			double [][]   centers){
		final boolean use_alt = mode == MODE_ALT;
		final int dbg_x=2783, dbg_y=-5228;
		int [][] bounds = getBoundsPixels( // should be for rectified, {-bounds[0][0], -bounds[0][1]} - exact center
				zoom_level,
				bounds_to_indices? indices: null); 
		int width =  bounds[0][1] - bounds[0][0]; // bounds[x][0] - negative 
		int height = bounds[1][1] - bounds[1][0];
		if (wh != null) {
			wh[0] = width;
			wh[1] = height;
		}
		if (origin != null) {
			origin[0] = -bounds[0][0];
			origin[1] = -bounds[1][0];
		}
		if (indices == null) {
			indices = new int [ortho_maps.length];
			for (int i = 0; i < indices.length; i++) {
				indices[i] = i;
			}
		}
		
		final float [][] fpixels = new float [indices.length][width * height];
//		for (int nmap:indices) { // = 0; nmap< ortho_maps.length; nmap++) {
		for (int indx = 0; indx < indices.length; indx++) { //:indices) { // = 0; nmap< ortho_maps.length; nmap++) {
			final int findx= indx;
			final int nmap = indices[indx]; // nmap;
			Arrays.fill(fpixels[findx], Float.NaN);
			final double scale = 1.0/OrthoMap.getPixelSizeMeters(zoom_level);
			final double src_scale = 1.0/OrthoMap.getPixelSizeMeters(ortho_maps[nmap].orig_zoom_level); // pix per meter
		// metric bounds of the rectified image relative to its origin
//			double [][] mbounds = (affines !=null) ?
//					ortho_maps[nmap].getBoundsMeters(true, affines[indx]): // use provided affine
//						ortho_maps[nmap].getBoundsMeters(true);
			double [][] mbounds = ortho_maps[nmap].getBoundsMeters(true); // keep original bounds
			double [] enu_offset = ortho_maps[indices[0]].enuOffsetTo(ortho_maps[nmap]);
			final double [] scaled_out_center = { // xy center to apply affine to
					-bounds[0][0] + scale * enu_offset[0],
					-bounds[1][0] - scale * enu_offset[1]};
			if (centers != null) {
				centers[findx] = scaled_out_center;
			}
			final int [][] obounds = new int [2][2]; // output (rectified, combined) image bounds, relative to thje top-left
			for (int n = 0; n< 2; n++) {
				obounds[n][0] = (int) Math.floor(scaled_out_center[n] + scale*mbounds[n][0]);
				obounds[n][1] = (int) Math.ceil (scaled_out_center[n] + scale*mbounds[n][1]);
			}
			// Output window size (keep original affine - OK if will not exactly fit)
			final int ownd_width =  obounds[0][1] - obounds[0][0];
			final int ownd_height = obounds[1][1] - obounds[1][0];
			final int ownd_len = ownd_width * ownd_height;
//			double [][] src_bounds=(affines !=null) ? 
//					ortho_maps[nmap].getBoundsMeters (true, affines[indx]): // use provided affine
//						ortho_maps[nmap].getBoundsMeters (true);
			double [][] src_bounds=ortho_maps[nmap].getBoundsMeters (true); // using original affines
			final double [] src_center = {-src_bounds[0][0],-src_bounds[1][0]}; // x,y center offset in the source image
			final double [][] affine = (affines !=null) ? affines[indx]: ortho_maps[nmap].affine; // only here use provided
			final int src_width = use_alt? ortho_maps[nmap].getAltData().width: ortho_maps[nmap].getImageData().width;
			final int src_height = use_alt? ortho_maps[nmap].getAltData().height : ortho_maps[nmap].getImageData().height;
			final float [] src_img = use_alt? ortho_maps[nmap].getAltData().data : ortho_maps[nmap].getImageData().data;
			if ((indx==0) && (warp != null)) { // set center from the first image
				warp.setRender(
						zoom_level, // int zoom_lev,
						scaled_out_center[0], // double px0, // in render pixels
						scaled_out_center[1]); // // double py0);
			}
			final Rectangle warp_woi =((indx==1) && (warp != null))? warp.getRenderWOI():null; 
			final Thread[] threads = ImageDtt.newThreadArray();
			final AtomicInteger ai = new AtomicInteger(0);
			for (int ithread = 0; ithread < threads.length; ithread++) {
				threads[ithread] = new Thread() {
					public void run() {
						for (int nPix = ai.getAndIncrement(); nPix < ownd_len; nPix = ai.getAndIncrement()) {
							int opX = nPix % ownd_width + obounds[0][0]; // absolute output pX, pY
							int opY = nPix / ownd_width + obounds[1][0];
							double dX = (opX - scaled_out_center[0]) /scale; // in original image scale 
							double dY = (opY - scaled_out_center[1]) /scale;
							double [] xy_src = { // pixels of the source image
									src_scale * (affine[0][0]*dX + affine[0][1]*dY + affine[0][2] + src_center[0]),
									src_scale * (affine[1][0]*dX + affine[1][1]*dY + affine[1][2] + src_center[1])};
							// limit to the source image
							if ((warp_woi != null) && (warp_woi.contains(opX,opY))) {
								if ((opX==dbg_x) && (opY==dbg_y)) {
									System.out.println("opX="+opX+", opy="+opY);
								}
								double [] dxy = warp.getWarp(opX,opY);
								xy_src[0] += dxy[0];
								xy_src[1] += dxy[1];
							}
							if  ((xy_src[0] >= 0) && (xy_src[0] < (src_width-1)) &&
									(xy_src[1] >= 0) && (xy_src[1] < (src_height-1))) { 
								int [] ixy_src = {(int) Math.floor(xy_src[0]), (int)Math.floor(xy_src[1]) };
								double [] kxy = {xy_src[0]-ixy_src[0], xy_src[1]-ixy_src[1]}; 
								int indx00 = ixy_src[0] + ixy_src[1] * src_width; 
								double d00 = src_img[indx00];
								if (!Double.isNaN(d00)) {
									double d01 = src_img[indx00 + 1];
									double d10 = src_img[indx00 + src_width];
									double d11 = src_img[indx00 + src_width + 1];
									double d = d00*(1.0 - kxy[0])*(1.0 - kxy[1])+
											d01*       kxy[0] *(1.0 - kxy[1])+
											d10*(1.0 - kxy[0])*       kxy[1]+
											d11*       kxy[0] *       kxy[1];
									fpixels[findx][opX + opY*width] = (float) d;
								}
							}
						}
					}
				};
			}		      
			ImageDtt.startAndJoin(threads);
		}
		return fpixels;
	}
	
	
	public double [][] renderMultiDouble ( // should work with just a single image (indice.length ==1)?
			double [][]   ground_planes, // null - images, non-null altitudes. use new double[2][] for old way alt
			int []        indices, // null or which indices to use (normally just 2 for pairwise comparison)
			boolean       bounds_to_indices,
			double [][][] affines, // null or [indices.length][2][3] 
			FineXYCorr    warp,
			int           zoom_level,
			int []        wh,
			int []        origin, // maps[0] as a reference
			double [][]   centers){
		boolean use_alt = ground_planes != null;
		final int dbg_x=707, dbg_y=615;
		int [][] bounds;
		if (affines == null) {
			bounds = getBoundsPixels( // should be for rectified, {-bounds[0][0], -bounds[0][1]} - exact center
					zoom_level,
					bounds_to_indices? indices: null);
		} else {
			bounds = getBoundsPixels( // should be for rectified, {-bounds[0][0], -bounds[0][1]} - exact center
					zoom_level,
					bounds_to_indices? indices: null,
							affines);
		}
		int width =  bounds[0][1] - bounds[0][0]; // bounds[x][0] - negative 
		int height = bounds[1][1] - bounds[1][0];
		if (wh != null) {
			wh[0] = width;
			wh[1] = height;
		}
		if (origin != null) {
			origin[0] = -bounds[0][0];
			origin[1] = -bounds[1][0];
		}
		if (indices == null) {
			indices = new int [ortho_maps.length];
			for (int i = 0; i < indices.length; i++) {
				indices[i] = i;
			}
		}
		
		final double [][] dpixels = new double [indices.length][width * height];
		for (int indx = 0; indx < indices.length; indx++) { //:indices) { // = 0; nmap< ortho_maps.length; nmap++) {
			final int findx= indx;
			final int nmap = indices[indx]; // nmap;
			final double [][] affine = (affines !=null) ? affines[indx]: ortho_maps[nmap].affine; // only here use provided
			Arrays.fill(dpixels[findx], Float.NaN);
			final double scale = 1.0/OrthoMap.getPixelSizeMeters(zoom_level);
			final double src_scale = 1.0/OrthoMap.getPixelSizeMeters(ortho_maps[nmap].orig_zoom_level); // pix per meter
		// metric bounds of the rectified image relative to its origin
///			double [][] mbounds = ortho_maps[nmap].getBoundsMeters(true); // keep original bounds
			double [][] mbounds = ortho_maps[nmap].getBoundsMeters(true,affine); // keep original bounds
			double [] enu_offset = ortho_maps[indices[0]].enuOffsetTo(ortho_maps[nmap]);
			final double [] scaled_out_center = { // xy center to apply affine to in output pixels
					-bounds[0][0] + scale * enu_offset[0],
					-bounds[1][0] - scale * enu_offset[1]};
			if (centers != null) {
				centers[findx] = scaled_out_center;
			}
			final int [][] obounds = new int [2][2]; // output (rectified, combined) image bounds, relative to the top-left
			for (int n = 0; n< 2; n++) { // output pixels
				obounds[n][0] = (int) Math.floor(scaled_out_center[n] + scale*mbounds[n][0]);
				obounds[n][1] = (int) Math.ceil (scaled_out_center[n] + scale*mbounds[n][1]);
			}
			// Output window size (keep original affine - OK if will not exactly fit)
			final int ownd_width =  obounds[0][1] - obounds[0][0];
			final int ownd_height = obounds[1][1] - obounds[1][0];
			final int ownd_len = ownd_width * ownd_height;
///			final double [][] affine = (affines !=null) ? affines[indx]: ortho_maps[nmap].affine; // only here use provided
///			double [][] src_bounds=ortho_maps[nmap].getBoundsMeters (true); // using original affines
			double [][] src_bounds=ortho_maps[nmap].getBoundsMeters (false,affine); // using provided affines
			final double [] src_center = {-src_bounds[0][0],-src_bounds[1][0]}; // x,y center offset in the source image
			final int src_width = use_alt? ortho_maps[nmap].getAltData().width: ortho_maps[nmap].getImageData().width;
			final int src_height = use_alt? ortho_maps[nmap].getAltData().height : ortho_maps[nmap].getImageData().height;
//			final float [] src_img = use_alt? ortho_maps[nmap].getAltData().data : ortho_maps[nmap].getImageData().data;
			if ((indx==0) && (warp != null)) { // set center from the first image
				warp.setRender(
						zoom_level, // int zoom_lev,
						scaled_out_center[0], // double px0, // in render pixels
						scaled_out_center[1]); // // double py0);
			}
			boolean tilt_alt = (ground_planes != null) && (ground_planes[indx] != null);
			final float [] src_img = use_alt?
					(tilt_alt? ortho_maps[nmap].getAltData().data.clone(): ortho_maps[nmap].getAltData().data) :
						ortho_maps[nmap].getImageData().data;
			
			final Rectangle warp_woi =((indx==1) && (warp != null))? warp.getRenderWOI():null; 
			final Thread[] threads = ImageDtt.newThreadArray();
			final AtomicInteger ai = new AtomicInteger(0);
			if (tilt_alt) {
				// apply tilt to the source altitudes								
				double [] vert_meters = ortho_maps[nmap].getVertMeters();
				// altitude at top-left pixel;
				final double top_left_alt = 
						ground_planes[indx][2] - vert_meters[0]*ground_planes[indx][0] - vert_meters[1]*ground_planes[indx][1];
				final double [] tilt_XY = {ground_planes[indx][0]/src_scale, ground_planes[indx][1]/src_scale};
				for (int ithread = 0; ithread < threads.length; ithread++) {
					threads[ithread] = new Thread() {
						public void run() {
							for (int nPix = ai.getAndIncrement(); nPix < src_img.length; nPix = ai.getAndIncrement()) {
								int py = nPix / src_width;
								int px = nPix % src_width;
								src_img[nPix] -= (top_left_alt + px * tilt_XY[0] + py * tilt_XY[1]);
							}
						}
					};
				}		      
				ImageDtt.startAndJoin(threads);
				ai.set(0);
			}
			for (int ithread = 0; ithread < threads.length; ithread++) {
				threads[ithread] = new Thread() {
					public void run() {
						for (int nPix = ai.getAndIncrement(); nPix < ownd_len; nPix = ai.getAndIncrement()) {
							int opX = nPix % ownd_width + obounds[0][0]; // absolute output pX, pY
							int opY = nPix / ownd_width + obounds[1][0];
							double dX = (opX - scaled_out_center[0]) /scale; // in meters
							double dY = (opY - scaled_out_center[1]) /scale;
							double [] xy_src = { // pixels of the source image
									src_scale * (affine[0][0]*dX + affine[0][1]*dY + affine[0][2] + src_center[0]),
									src_scale * (affine[1][0]*dX + affine[1][1]*dY + affine[1][2] + src_center[1])};
							// limit to the source image
							if ((((int) opX)==dbg_x) && (((int) opY)==dbg_y)) {
								System.out.println("opX="+opX+", opy="+opY);
							}
							if ((warp_woi != null) && (warp_woi.contains(opX,opY))) {
								double [] dxy = warp.getWarp(opX,opY);
								xy_src[0] += dxy[0];
								xy_src[1] += dxy[1];
							}
							if  ((xy_src[0] >= 0) && (xy_src[0] < (src_width-1)) &&
									(xy_src[1] >= 0) && (xy_src[1] < (src_height-1))) { 
								int [] ixy_src = {(int) Math.floor(xy_src[0]), (int)Math.floor(xy_src[1]) };
								double [] kxy = {xy_src[0]-ixy_src[0], xy_src[1]-ixy_src[1]}; 
								int indx00 = ixy_src[0] + ixy_src[1] * src_width; 
								double d00 = src_img[indx00];
								if (!Double.isNaN(d00)) {
									double d01 = src_img[indx00 + 1];
									double d10 = src_img[indx00 + src_width];
									double d11 = src_img[indx00 + src_width + 1];
									double d = d00*(1.0 - kxy[0])*(1.0 - kxy[1])+
											d01*       kxy[0] *(1.0 - kxy[1])+
											d10*(1.0 - kxy[0])*       kxy[1]+
											d11*       kxy[0] *       kxy[1];
									dpixels[findx][opX + opY*width] = (float) d;
								}
							}
						}
					}
				};
			}		      
			ImageDtt.startAndJoin(threads);
		}
		return dpixels;
	}

	
	public int getIndex(String sindx) {
		return map_index_string.get(sindx);
	}
	
	
	public float [][] getPaddedPairGPU(
			String [] spair,
			int       zoom_lev){
		int [] pair = new int [spair.length];
		for (int i = 0; i < pair.length; i++) {
			pair[i] = getIndex(spair[i]);
		}
		return getPaddedPairGPU(
				pair, // int [] pair,
				zoom_lev); // int    zoom_lev,
	}

	private float [][] getPaddedPairGPU(
			int [] pair,
			int    zoom_lev){
		float [][] gpu_pair_img = new  float [2][];
		for (int n = 0; n < pair.length; n++) {
			gpu_pair_img[n] = ortho_maps[pair[n]].getPaddedGPU (
							zoom_lev); // int zoom_level,
		}
		return gpu_pair_img;
	}
	
    
	public PairwiseOrthoMatch SpiralMatch (
			CLTParameters    clt_parameters,
			double           frac_remove, //  =        0.25
			double           metric_error,
			boolean          pmtch_use_affine,
			double           max_std,     // maximal standard deviation to limit center area  
			double           min_std_rad, // minimal radius of the central area (if less - fail)
			double           rad_fraction,
			double           max_tile_rad, //  = 30;			
			double           fill_fraction,
			double           fill_fraction_final,
			double           ease_nosfm,
			int []           gpu_pair,
			double [][][]    affines_init,  // here in meters, relative to vertical points
			int              zoom_lev,
			double           pix_step,
			double           pix_range,
			double           good_rms,
			double           max_rms,
			int              num_tries, //  = 5
			int              min_overlap, //  = 5
			boolean          ignore_rms,
			double []        max_rms_iter, //  = {1.0, 0.6};//
			double           overlap,
			int              debugLevel){
		double [][] affine1 = new double [][] {affines_init[1][0].clone(),affines_init[1][1].clone()};
		double [][][] affines = new double [][][] {affines_init[0],affine1};
		boolean  show_vf = false;
		boolean  batch_mode = true;
		double [][] ground_planes = null; // null or double[2] - will return ground planes:
		double pix_size = OrthoMap.getPixelSizeMeters (zoom_lev);
		int nx = 0, ny=0; // number of steps (pix_step size) in hor and vert directions (both pos and neg)
		int nabs = (int) Math.ceil(pix_range/pix_step);
		PairwiseOrthoMatch 	pairwiseOrthoMatch = new PairwiseOrthoMatch(
				affines[1], //  double [][] affine,
				null, // double [][] jtj,
				Double.NaN, // double rms,
				zoom_lev, // int zoom_lev);
				overlap); // double overlap);
//		double           max_std =     0.5;      // maximal standard deviation to limit center area  
//		double           min_std_rad = 2.0;  // minimal radius of the central area (if less - fail)
		int best_nx = -1, best_ny = -1;
		PairwiseOrthoMatch best_pom = null;
		while ((Math.abs(nx) <= nabs) && (Math.abs(ny) <= nabs)) {
			for (int i = 0; i < affines_init.length; i++) {
				System.arraycopy(affines_init[1][i], 0, affine1[i], 0, affine1[i].length);
			}
			affine1[0][2] += pix_step * pix_size * nx;
			affine1[1][2] += pix_step * pix_size * ny;
			pairwiseOrthoMatch.rms = Double.NaN;
			
			correlateOrthoPair(
					clt_parameters,  // CLTParameters    clt_parameters,
					pairwiseOrthoMatch, //PairwiseOrthoMatch pairwiseOrthoMatch, // will return statistics
					min_overlap,     // 			int              min_overlap,
					max_std,         // double           max_std,      // maximal standard deviation to limit center area  
					min_std_rad,     // double           min_std_rad,  // minimal radius of the central area (if less - fail)
					frac_remove,     // double           frac_remove, //  =        0.25
					metric_error,    // 			double           metric_error,
					ignore_rms,      // boolean ignore_prev_rms,
					num_tries,       //  = 5int              num_tries, //  = 5
					false,           // boolean          calc_warp, (will return null if false)
					batch_mode,      // boolean          batch_mode,
					gpu_pair,        // String []        gpu_spair,
					affines,         // double [][][]    affines, // on top of GPS offsets
					null,            // woi,             // Rectangle        woi,
					zoom_lev,        // int              zoom_lev,
					show_vf,         // boolean show_vf,
					ground_planes,   // double [][]      ground_planes, // null or double[2] - will return ground planes
					rad_fraction,    // double           rad_fraction,
					max_tile_rad,    // double           max_tile_rad, //  = 30;
					fill_fraction,   // double           fill_fraction,
					fill_fraction_final, // double           fill_fraction_final,
					ease_nosfm,      // double           ease_nosfm,
					max_rms_iter, // double []        max_rms_iter, //  = {1.0, 0.6};//
					debugLevel-4);     // final int        debugLevel)
			pairwiseOrthoMatch.affine = affines[1]; // modified by correlateOrthoPair
			if (debugLevel > -4) {
				System.out.println(String.format("SpiralMatch(): %3d-%3d nx = %3d, ny=%3d, RMSE=%8.6f",
						gpu_pair[0], gpu_pair[1], nx,ny,pairwiseOrthoMatch.rms)); // if NaN - provide reason
			}
			if (!Double.isNaN(pairwiseOrthoMatch.rms) && ((best_pom == null) || !(best_pom.rms <= pairwiseOrthoMatch.rms))) {
				pairwiseOrthoMatch.nxy = new int [] {nx,ny};
				best_pom = pairwiseOrthoMatch.clone();
				best_nx = nx;
				best_ny = ny;
//				System.out.println("best_pom.rms = "+best_pom.rms);
			}
			if (pairwiseOrthoMatch.rms < good_rms) {
				break;
			}
			// update nx, ny
			if ((nx >   ny) && (nx > -ny)){
				ny++;
			} else if ((nx <=  ny) && (nx > -ny)){
				nx--;
			} else if ((ny <= -nx) && (ny >  nx)){
				ny--;
			} else {
				nx++;
			}
		}
		
		if (pairwiseOrthoMatch.rms < good_rms) { // immediately OK
			return pairwiseOrthoMatch; // pairwiseOrthoMatch.affine will have adjusted affine[1]
		}
		if ((best_pom != null) && (best_pom.rms <= max_rms)) {
			if (debugLevel > -4) {
				System.out.println("SpiralMatch(): best RMSE="+best_pom.rms+" < "+max_rms+
						" for nx = "+best_nx+", ny="+best_ny+", using it.");
			}
			return best_pom;
		}
		if (debugLevel > -4) {
			System.out.println("SpiralMatch(): Failed to find a good match candidate. Best RMSE="+
			((best_pom != null)? best_pom.rms:"N/A")+", max_rms= "+max_rms+"");
		}
		return null; // pairwiseOrthoMatch.affine will have adjusted affine[1]
	}
	
	
/*	
	public FineXYCorr correlateOrthoPair( // not used
			CLTParameters    clt_parameters,
			PairwiseOrthoMatch pairwiseOrthoMatch, // will return statistics			
			double           frac_remove, //  =        0.25
			double           metric_error,
			boolean          ignore_prev_rms,
			int              num_tries, //  = 5
			boolean          calc_warp,
			boolean          batch_mode,
			String []        gpu_spair,
			double [][][]    affines, // here in meters, relative to vertical points
			Rectangle        woi,
			int              zoom_lev,
			boolean          show_vf,
			double [][]      ground_planes, // null or double[2] - will return ground planes
			double           rad_fraction,
			double           fill_fraction,
			double           fill_fraction_final,
			double           ease_nosfm,
			double []        max_rms_iter, //  = {1.0, 0.6};//			
			final int        debugLevel){
		int [] gpu_pair = new int[gpu_spair.length];
		for (int i = 0; i < gpu_pair.length; i++) {
			gpu_pair[i] = getIndex(gpu_spair[i]);
		}
		double           max_std = 1.5;      // maximal standard deviation to limit center area  
		double           min_std_rad = 2.0;  // minimal radius of the central area (if less - fail)
		return correlateOrthoPair(
				clt_parameters,  // CLTParameters    clt_parameters,
				pairwiseOrthoMatch, //PairwiseOrthoMatch pairwiseOrthoMatch, // will return statistics
				0, // 			int              min_overlap,
				max_std,         // double           max_std,      // maximal standard deviation to limit center area  
				min_std_rad,     // double           min_std_rad,  // minimal radius of the central area (if less - fail)
				frac_remove,     // double           frac_remove, //  =        0.25
				metric_error,    // 			double           metric_error,
				ignore_prev_rms, // boolean          ignore_prev_rms,
				num_tries,       //  = 5int              num_tries, //  = 5
				calc_warp,       // boolean          calc_warp,
				batch_mode,      // boolean          batch_mode,
				gpu_pair,        // int []           gpu_pair,
				affines,         // double [][][]    affines, // here in meters, relative to vertical points
				woi,             // Rectangle        woi,
				zoom_lev,        // int              zoom_lev,
				show_vf,         // boolean          show_vf,
				ground_planes,   // double [][]      ground_planes, // null or double[2] - will return ground planes
				rad_fraction,    // double           rad_fraction,
				30,              // double           max_tile_rad, //  = 30;
				fill_fraction,   // double           fill_fraction,
				fill_fraction_final, // double           fill_fraction_final,
				ease_nosfm,      // double           ease_nosfm,
				max_rms_iter, //  = {1.0, 0.6};// double []        max_rms_iter, //  = {1.0, 0.6};//        						
				debugLevel);     // final int        debugLevel)
	}
*/	
	public FineXYCorr correlateOrthoPair(
			CLTParameters    clt_parameters,
			PairwiseOrthoMatch pairwiseOrthoMatch, // will return statistics, may be null if not needed
			int              min_overlap,
			double           max_std,      // maximal standard deviation to limit center area  
			double           min_std_rad,  // minimal radius of the central area (if less - fail)
			double           frac_remove, //  =        0.25
			double           metric_error,
			boolean          ignore_prev_rms,
			int              num_tries, //  = 5
			boolean          calc_warp,
			boolean          batch_mode,
			int []           gpu_pair,
			double [][][]    affines,  // here in meters, relative to vertical points
			Rectangle        woi,
			int              zoom_lev,
			boolean          show_vf,
			double [][]      ground_planes, // null or double[2] - will return ground planes:
			double           rad_fraction,
			double           max_tile_rad, //  = 30;
			double           fill_fraction,
			double           fill_fraction_final,
			double           ease_nosfm,
			double []        max_rms_iter, //  = {1.0, 0.6};//			
			int              debugLevel){
		double vf_drop_big = 0.9; // 0.5? If vector_field threshold drops more, allow RMS to increase
//		double rad_fraction =        0.5; // center circle radius fraction of 0.5* min(width, height) in tiles
//		double fill_fraction =       0.25; // should be populated not less than this
//		double fill_fraction_final = 0.4; // should be populated not less than this
//		double ease_nosfm =          2.0; // ease metric_error when no SfM gain == 0;
//		double max_tile_rad = 30;
		double center_radius_change = 1.5; // if increases by more than this, disregard RMSE worsening
		boolean show_lma_dbg = !batch_mode && (debugLevel > 1); 
		if (woi == null) {
			woi = new Rectangle();
		}
		int min_tiles_overlap = min_overlap/GPUTileProcessor.DTT_SIZE/GPUTileProcessor.DTT_SIZE;
	 	if (pairwiseOrthoMatch != null) {
	 		pairwiseOrthoMatch.rms = Double.NaN; // mark as failed
	 	}
		boolean show_gpu_img = true; // (debugLevel > 1);
		boolean show_tile_centers = false; // true; // (debugLevel > 1);
		if (!batch_mode) {
			debugLevel = 0;
		} else {
			show_gpu_img =      false; // (debugLevel > 1);
			show_tile_centers = false; // true; // (debugLevel > 1);
		}
    	boolean show_vector_field = show_vf || (!batch_mode && (debugLevel>-1)); // true;
		double [][] bounds_overlap_meters = getOverlapMeters(
				gpu_pair[0],  // int ref_index,
				gpu_pair[1],  // int other_index)
				affines[0],   // double [][] ref_affine,
				affines[1]);  // double [][] other_affine
				
		if ((bounds_overlap_meters == null) || (bounds_overlap_meters[0] == null) || (bounds_overlap_meters[1] == null)) {
			if (debugLevel > -4) {
				System.out.println("correlateOrthoPair(): no overlap");
			}
			return null;
		}
		double pix_size = OrthoMap.getPixelSizeMeters (zoom_lev);
	    int [] overlap_wh_pixel = new int [2];
	    for (int i = 0; i < 2; i++) {
	    	overlap_wh_pixel[i] = (
	    			(int) Math.ceil(bounds_overlap_meters[i][1]/pix_size))
	    			- ((int) Math.floor(bounds_overlap_meters[i][0]/pix_size));
	    }
		// convert to pixels,shift top-left to [0,0] (remember offsets, limit w,h,
		// change to pixels last, remember TL in meters?
		// keep center where it was
		// {bounds_overlap_meters[0][0],bounds_overlap_meters[1][0],
		double [] enuOffset = ortho_maps[gpu_pair[0]].enuOffsetTo(ortho_maps[gpu_pair[1]]);
		double [] rd = {enuOffset[0], -enuOffset[1]}; // {right,down} of the image 
		double [][] tlo_rect_metric = new double [2][2]; // top-left of overlap referenced to it's own vertical point (subtract!)
		tlo_rect_metric[0][0] = bounds_overlap_meters[0][0]; // relative to ref vert_meters
		tlo_rect_metric[0][1] = bounds_overlap_meters[1][0]; // vert_meters
		tlo_rect_metric[1][0] = bounds_overlap_meters[0][0] - rd[0];  // relative to other vert_meters
		tlo_rect_metric[1][1] = bounds_overlap_meters[1][0] - rd[1];

		double [][] tlo_src_metric = new double[tlo_rect_metric.length][2]; // relative to it's own vert_meters
		for (int n=0; n <tlo_src_metric.length; n++) {
			for (int i = 0; i < 2; i++) { // subtracting tl_rect_metric[n] (-1)
				tlo_src_metric[n][i] =
						tlo_rect_metric[n][0] * affines[n][i][0] +
						tlo_rect_metric[n][1] * affines[n][i][1] + affines[n][i][2];
			}
		}
		/// referenced to top-left pixel of the gpu image
		double [][] tlo_source_pixel = new double[tlo_src_metric.length][2];
		for (int n=0; n <tlo_source_pixel.length; n++) {
			for (int i = 0; i < 2; i++) {
				tlo_source_pixel[n][i] = (tlo_src_metric[n][i] + ortho_maps[gpu_pair[n]].vert_meters[i])/pix_size;
			}
		}
		
		double [][][] affines_gpu = new double [affines.length][2][3]; // relative to top left corners, in pixels
		float [][] gpu_pair_img = new  float [2][];
		for (int n = 0; n < gpu_pair.length; n++) {
			for (int i = 0; i < 2; i++) {
				for (int j = 0; j < 2; j++) {
					affines_gpu[n][i][j] = affines[n][i][j]; 
				}
				affines_gpu[n][i][2] = tlo_source_pixel[n][i];
			}
			gpu_pair_img[n] = ortho_maps[gpu_pair[n]].getPaddedGPU (zoom_lev); // int zoom_level,
		}
		boolean invert_second = (debugLevel > 1000);
		if (debugLevel > 10) {
			return null;
		}
		if (invert_second) {
			for (int i = 0; i < gpu_pair_img[1].length; i++) {
				gpu_pair_img[1][i]= -gpu_pair_img[1][i];
			}
		}
		if (show_gpu_img) {
			String [] dbg_titles = {ortho_maps[gpu_pair[0]].getName(),ortho_maps[gpu_pair[1]].getName()};
    		ShowDoubleFloatArrays.showArrays(
    				gpu_pair_img,
    				OrthoMap.gpu_width,
    				OrthoMap.gpu_height,
    				true,
    				"gpu_img",
    				dbg_titles);

		}
		woi.width =  overlap_wh_pixel[0];
		woi.height = overlap_wh_pixel[1]; // = new Rectangle(0, 0, overlap_wh_pixel[0], overlap_wh_pixel[1]);
		if (woi.width * woi.height < min_overlap) {
		 	if (debugLevel > -4) {
		 		System.out.println("correlateOrthoPair(): too small overlap: "+
		 				woi.width+" * "+ woi.height+" = "+(woi.width * woi.height)+" < "+min_overlap+"");
		 	}
		 	return null;
		}
		if (woi.width > OrthoMap.gpu_width) {
			if (debugLevel > -3) {
				System.out.println("correlateOrthoPair() correlation woi.width="+woi.width+" > gpu_width="+OrthoMap.gpu_width+". Truncating.");
			}
			woi.width = OrthoMap.gpu_width;
		}
		if (woi.height > OrthoMap.gpu_height) {
			if (debugLevel > -3) {
				System.out.println("correlateOrthoPair() correlation woi.height="+woi.height+" > gpu_height="+OrthoMap.gpu_height+". Truncating.");
			}
			woi.height = OrthoMap.gpu_height;
		}
		
		double tile_rad = rad_fraction * 0.5*Math.min(woi.width, woi.height)/GPUTileProcessor.DTT_SIZE;
		tile_rad = Math.min(tile_rad, max_tile_rad); 
		int min_tiles_rad = (int) (fill_fraction * 4 * tile_rad * tile_rad);
		
		final int  gpu_width = OrthoMap.gpu_width; // static
		final int  gpu_height = OrthoMap.gpu_height; // static
		int tilesX = gpu_width/GPUTileProcessor.DTT_SIZE; 
    	int tilesY =  gpu_height/GPUTileProcessor.DTT_SIZE;
    	Rectangle tile_woi = scaleRectangle (woi, GPUTileProcessor.DTT_SIZE);
		// uses fixed_size gpu image size
		TpTask [][] tp_tasks = new TpTask [2][];
		
//		int num_tries = 5;
		double prev_rms = Double.NaN;
		double rel_improve = 1E-3;
		double [][] elevations = new double [tp_tasks.length][];
		double [][] ground_planes_metric = new double [tp_tasks.length][];
		boolean [][] ground_planes_masks = new boolean[2][];
		double    initial_above = 3; // m
		double    initial_below = 3; // m
		int       num_refine =    3;
		double    frac_above =    0.3;
		double    frac_below =    0.1;
		double [][] jtj = null;
		double rms = Double.NaN;
		double last_center_radius = 0;
		double previous_center_radius = 1.0;
		OrthoPairLMA  orthoPairLMA = null;
		double [][][] vector_field = new double[gpu_pair.length][][];
		double [][]   tile_centers = null;
		double [] ground_planes_weight = null;	
		double previous_vf_threshold = Double.POSITIVE_INFINITY;
		double vf_threshold = 0;
		for (int ntry = 0; ntry < num_tries; ntry++) {
			if (!batch_mode) {
				if (debugLevel>-3) {
					System.out.println("correlateOrthoPair(): ntry="+ntry);
				}
			}
			String dbg_suffix = batch_mode? null: String.format("_%02d", ntry);
			vector_field = 
					ComboMatch.rectilinearVectorField(//rectilinearCorrelate_TD( // scene0/scene1
							clt_parameters,      // final CLTParameters          clt_parameters,
							gpu_pair_img,        // final float  [][]            fpixels, // to check for empty
							gpu_width,           // final int                    img_width,
							woi,                 // Rectangle                    woi, // if null, use full GPU window
							affines_gpu,         // final double [][][]          affine,  // [2][2][3] affine coefficients to translate common to 2 images
							tp_tasks,            // TpTask [][]                  tp_tasks_o,
							batch_mode,          // final boolean                batch_mode,
							dbg_suffix,          //  final String                 dbg_suffix, // for image_names
							debugLevel);         // final int                    debugLevel);
			if (tp_tasks[0].length < min_tiles_overlap) {
				if (pairwiseOrthoMatch != null) {
					pairwiseOrthoMatch.rms = Double.NaN; // mark as failed
				}
				if (debugLevel > -4) {
					System.out.println("correlateOrthoPair(): too small non-null overlap: "+
							tp_tasks[0].length +" < "+min_tiles_overlap+"");
				}
				return null;
			}
			
			// get elevations
//			double ease_nosfm = 3.0;
			int zoom_lev_tiles = zoom_lev-3;
			ground_planes_weight = null;
			for (int num_elevations = 0; num_elevations < 1; num_elevations++) {
				for (int n = 0; n < gpu_pair.length; n++) {
					double sfm_gain = ortho_maps[gpu_pair[n]].getSfmGain();
					double metric_error_adj = metric_error;
					if (sfm_gain == 0.0) {
						metric_error_adj *= ease_nosfm;
						if (debugLevel > -3) {
							System.out.println("SfM gain == 0 for scene #"+gpu_pair[n]+
									", icreasing metric_error to "+metric_error_adj);
						}
					}
					elevations[n] = ortho_maps[gpu_pair[n]].getTileElevations(
							zoom_lev, // final int zoom_level,
							tp_tasks[n], // final TpTask [] tp_tasks, 
							tilesX, // final int tilesX,
							tilesY, // final int tilesY,
							debugLevel); // final int debugLevel)
					String debug_title = (show_vector_field && (ntry==0))? (ortho_maps[gpu_pair[n]].getName()+"_plane"+dbg_suffix):null;
					ground_planes_metric[n] = ortho_maps[gpu_pair[n]].getPlaneMeters (
							zoom_lev_tiles, // int       zoom_lev, // of the data (3 levels down for tiles)
							elevations[n],  // double [] elev,
							tilesX,         // int       width,
							initial_above,  // double    initial_above, // from average
							initial_below,  // double    initial_below, // from average, // positive value
							num_refine,     // int       num_refine,
							frac_above,     // double    frac_above,
							frac_below,     // double    frac_below,
							null, // 			double [][] tile_plane_elev, // null or double[2][] will return per-tile elevations
							debug_title);   // String    debug_title)
					ground_planes_masks[n] = ortho_maps[gpu_pair[n]].removeHighElevationMismatch (
							zoom_lev_tiles,          // int        zoom_lev, // of the data (3 levels down for tiles)
							elevations[n],           // double []  elev,
							tilesX, // int        width,
							ground_planes_metric[n], // double []  plane_metric, // tiltx,tilty, offs - in meters
							metric_error_adj);           // double     metric_error);
				}
				ground_planes_weight = new double[ ground_planes_masks[0].length];
				int num_left = 0; // , num_was = tp_tasks[0].length;
				for (int i = 0; i < ground_planes_weight.length; i++) {
					if (ground_planes_masks[0][i] && ground_planes_masks[1][i]) {
						num_left++;
						ground_planes_weight[i] = 1.0;
					}
				}
				if (debugLevel > -3) {
					System.out.println("correlateOrthoPair(): left "+num_left+" tiles (was "+tp_tasks[0].length+" before filtering). metric_error="+metric_error);
					System.out.println();
				}
				if (num_left < tp_tasks[0].length/4) {
					num_elevations--;
					if (debugLevel > -4) {
						System.out.println("too few tiles remain, try again. metric_error="+metric_error);
					}
					pairwiseOrthoMatch.rms = Double.NaN; // failed
					return null;
				}
				if (num_left < min_tiles_overlap) {
					if (pairwiseOrthoMatch != null) {
						pairwiseOrthoMatch.rms = Double.NaN; // mark as failed
					}
					if (debugLevel > -4) {
						System.out.println("correlateOrthoPair(): too small non-null, filtered by elevations overlap: "+
								num_left +" < "+min_tiles_overlap+"");
					}
					return null;
				}
				
			}
			
	    	double max_err=              7.0;
	    	int num_bins =            1000;
	    	boolean ignore_strength = false;
	    	//double  frac_remove =        0.15;
	    	double [][] vf_error2 = new double [vector_field.length][]; // may be able to skip [0]
	    	double [][][] vector_field_bkp = show_vector_field? new double[vector_field.length][][]:null;
	    	if (vector_field_bkp != null) {
	    		for (int i = 0; i < vector_field_bkp.length; i++) {
	    			vector_field_bkp[i] = vector_field[i].clone(); 
	    		}
	    	}
	    	for (int nvf_mode = 0; nvf_mode < vector_field.length; nvf_mode++) {
	    		vf_error2[nvf_mode] = getVectorFieldError2(
	    				vector_field[nvf_mode], // double [][] vector_field,
	    			    tilesX,                 // int         width, // tilesX
	    			    tile_woi);              // Rectangle   twoi); // in tiles
	    		double threshold = filterVectorField(
	    				vector_field[nvf_mode], // final double [][] vector_field,
	    				vf_error2[nvf_mode],    // final double []   vf_error2,
	    				frac_remove,            // final double      frac_remove,
	    				ignore_strength,        // final boolean     ignore_strength,
	    				num_bins,               // final int         num_bins,
	    				max_err,                // final double      max_err,
	    				tilesX, // final int         width, // tilesX
	    				tile_woi);              //final Rectangle   twoi); // in tiles
	    		if (nvf_mode == 0) {
	    			vf_threshold = threshold; // if it drops significantly - ignore rms increase
	    		}
				if (debugLevel>-3) {
					System.out.println("vector_field layer= "+nvf_mode+" error threshold = "+threshold+" pix.");
				}
	    	}
	    	
	    	// TODO: use crop, keep (0,0) (woi.x+woi.width, woi.y+woi.height 
			if (vector_field_bkp != null) { // show_vector_field) {
				int dbg_width =  tile_woi.x+tile_woi.width;
				int dbg_height = tile_woi.y+tile_woi.height;
				double [][][][] vf_all = {vector_field_bkp,vector_field};
				double [][] dbg_vf = new double [8 * vector_field.length+5][dbg_width * dbg_height];
				String [] dbg_titles = new String[dbg_vf.length];
				String [] prefix= {"single","single_filtered","neibs","neibs_filtered"};
				for (int n = 0; n < 2*vector_field.length; n++) {
					dbg_titles [4*n+0] = prefix[n]+"-vx";	
					dbg_titles [4*n+1] = prefix[n]+"-vy";	
					dbg_titles [4*n+2] = prefix[n]+"-str";	
					dbg_titles [4*n+3] = prefix[n]+"-err";	
				}
				dbg_titles [8 * vector_field.length + 0] = "mask-elev";
				dbg_titles [8 * vector_field.length + 1] = "elev-0";
				dbg_titles [8 * vector_field.length + 2] = "elev-1";
				dbg_titles [8 * vector_field.length + 3] = "mask-0";
				dbg_titles [8 * vector_field.length + 4] = "mask-1";
						
				for (int i = 0; i < dbg_vf.length; i++) {
					Arrays.fill(dbg_vf[i], Double.NaN);
					
				}
				for (int l = 0; l<dbg_vf[0].length; l++) { // local tile
					int lx = l % dbg_width;
					int ly = l / dbg_width;
					int t = lx + ly * tilesX; // full width
					for (int n = 0; n < vector_field.length; n++) {
						for (int m = 0; m < vf_all.length; m++) {
							if (vf_all[m][n][t] != null) {
								for (int k = 0; k < 3; k++) {
									dbg_vf[m*4 + n*8 + k][l] = vf_all[m][n][t][k];
								}
								// will automatically apply filtered
								dbg_vf[m*4 + n*8 + 3][l] = Math.sqrt(vf_error2[n][t]);
							}
						}
					}
					dbg_vf [8 * vector_field.length + 0][l] = ground_planes_weight[t];
					dbg_vf [8 * vector_field.length + 1][l] = elevations[0][t];
					dbg_vf [8 * vector_field.length + 2][l] = elevations[1][t];
					dbg_vf [8 * vector_field.length + 3][l] = ground_planes_masks[0][t]?1.0:0.0;
					dbg_vf [8 * vector_field.length + 4][l] = ground_planes_masks[1][t]?1.0:0.0;
				}
	    		ShowDoubleFloatArrays.showArrays(
	    				dbg_vf,
	    				dbg_width,
	    				dbg_height,
	    				true,
	    				"vector_field"+dbg_suffix,
	    				dbg_titles);
			}

			
			
			
			// may use tl_rect_metric to remap to the original image
			tile_centers = new double [vector_field[0].length][];
			for (TpTask task: tp_tasks[1]) {
				int ti = task.getTileY() * tilesX + task.getTileX();
				tile_centers[ti] = task.getDoubleCenterXY();
			}
			if (show_tile_centers){
				double [][] dbg_img = new double [6][tile_centers.length];
				String [] dbg_titles = {"cX","cY","px0","py0", "px1","py1"};
				for (int i = 0; i< dbg_img.length;i++) Arrays.fill(dbg_img[i], Double.NaN);
				for (int t = 0; t < tp_tasks[0].length; t++) {
					TpTask task0 = tp_tasks[0][t];
					TpTask task1 = tp_tasks[1][t];
					int ti = task0.getTileY() * tilesX + task0.getTileX();
					dbg_img[0][ti] = task0.getDoubleCenterXY()[0]; // same for task0, task1 
					dbg_img[1][ti] = task1.getDoubleCenterXY()[1]; 
					dbg_img[2][ti] = task0.getXY()[0][0];
					dbg_img[3][ti] = task0.getXY()[0][1];
					dbg_img[4][ti] = task1.getXY()[0][0];
					dbg_img[5][ti] = task1.getXY()[0][1];
				} // getXY()
				ShowDoubleFloatArrays.showArrays(
						dbg_img,
						tilesX,
						tile_centers.length/tilesX,
						true,
						"tile_centers"+dbg_suffix,
						dbg_titles);
			}
			if (!batch_mode) {
				if (debugLevel>-3) {
					System.out.println("correlateOrthoPair(): before LMA, ntry="+ntry);
				}
			}
			boolean origin_center = true; // false - old mode

			orthoPairLMA = new OrthoPairLMA(origin_center);
			// vector_field[1] - neighbors
			double lambda =            0.1;
			double lambda_scale_good = 0.5;
			double lambda_scale_bad =  8.0;
			double lambda_max =      100;
			double rms_diff =          0.001;
			int    num_iter =         20;
			boolean last_run =       false;
			int min_good_tiles =      min_tiles_overlap/2;
			
			// show_lma_dbg
			String dbg_lma_prefix = show_lma_dbg? ("LMA_"+gpu_pair[0]+"-"+gpu_pair[1]+"_ntry"+ntry+"_"):null;
			
			int num_good_tiles = orthoPairLMA.prepareLMA(
					// will always calculate relative affine, starting with unity
					tilesX,          // int           width,
					vector_field[1], // double [][]   vector_XYS,    // optical flow X,Y, confidence obtained from the correlate2DIterate()
					tile_centers,    // double [][]   centers,       // tile centers (in pixels)
					ground_planes_weight, // null,            // double []     weights_extra, // optional, may be null 
					true,            // boolean       first_run,
					min_good_tiles,  // int           min_good_tiles,
					max_std,         // double        max_std,      // maximal standard deviation to limit center area  
					min_std_rad,     // double        min_std_rad,  // minimal radius of the central area (if less - fail)
					tile_rad ,       // double        tile_rad,
					min_tiles_rad,   // int           min_tiles_rad,
					debugLevel);     // final int     debug_level)
			last_center_radius = orthoPairLMA.getCenterRadius();
			double center_radius_increase = 
					Double.isInfinite(previous_center_radius)?
							(Double.isInfinite(last_center_radius)?1:0):
								last_center_radius/previous_center_radius;
			if ((ntry == 0) && Double.isInfinite(last_center_radius)) {
				center_radius_increase = 1.0; // if starts with infinity - no increase  
			}
			if ((min_std_rad >0) && (last_center_radius < 1)) {
				if (debugLevel>-3) {
					System.out.println("correlateOrthoPair(): center_radius="+
							orthoPairLMA.getCenterRadius()+" < "+ 1);
				}
				return null;
			}
			
			if (num_good_tiles < min_good_tiles) {
				if (debugLevel>-4) {
					System.out.println("correlateOrthoPair(): num_good_tiles="+
				    num_good_tiles+ " < "+min_good_tiles);
				}
			 	if (pairwiseOrthoMatch != null) {
			 		pairwiseOrthoMatch.rms = Double.NaN; // mark as failed
			 	}
				return null;
			}
			int lma_rslt = orthoPairLMA.runLma( // <0 - failed, >=0 iteration number (1 - immediately)
					lambda,             // double lambda,           // 0.1
					lambda_scale_good,  // double lambda_scale_good,// 0.5
					lambda_scale_bad,   // double lambda_scale_bad, // 8.0
					lambda_max,         // double lambda_max,       // 100
					rms_diff,           // double rms_diff,         // 0.001
					num_iter,           // int    num_iter,         // 20
					last_run,           // boolean last_run,
					dbg_lma_prefix,     // String dbg_prefix,
					debugLevel);        // int    debug_level)
			if (debugLevel > -3) {
				System.out.println("LMA result = "+lma_rslt);
			}
			if (lma_rslt < 0) {
				System.out.println("LMA failed, result="+lma_rslt);
				return null;
			}
			rms = orthoPairLMA.getRms();
			if (debugLevel > -3) {
				System.out.println("RMS= "+rms+" ("+orthoPairLMA.getInitialRms()+")");
			}
			if (max_rms_iter != null) {
				int max_rms_index = Math.min(max_rms_iter.length-1, ntry);
				if (rms > max_rms_iter[max_rms_index]) {
					if (debugLevel > -3) {
						System.out.println("RMS= "+rms+" > max_rms_iter["+
								max_rms_index+"] = "+max_rms_iter[max_rms_index]);
					}
					return null;
				}
			}
			
			if (rms > prev_rms) {
				if (debugLevel > -3) {
					if ((rms-prev_rms)/prev_rms < rel_improve) {
						System.out.println("LMA RMSE worsened, but less than improvement threshold: new "+rms+" ("+ orthoPairLMA.getInitialRms()+"), prev="+prev_rms);
					} else {
						System.out.println("LMA RMSE worsened: new "+rms+" ("+ orthoPairLMA.getInitialRms()+"), prev="+prev_rms);
					}
				}
				if (ignore_prev_rms) {
					if (debugLevel > -3) {
						System.out.println("Will continue, as ignore_prev_rms is set");
					}					
				} else {
					if (center_radius_increase > center_radius_change) {
						if (debugLevel > -3) {
							System.out.println("Will continue, as center_radius increased from "+
									previous_center_radius+" to "+	last_center_radius +" (more than "+
									center_radius_change+"x).");
						}
					} else {
						if (vf_threshold <  (vf_drop_big * previous_vf_threshold)) {
							if (debugLevel > -3) {
								System.out.println("Will continue, as vf_threshold dropped from "+
										previous_vf_threshold+" to "+	vf_threshold +" (more than "+
										vf_drop_big+"x).");
							}
						} else {
							break;
						}
					}
				}
			}
			affines_gpu[1]=OrthoMap.combineAffine(affines_gpu[1], orthoPairLMA.getAffine());
			jtj = orthoPairLMA.getLastJtJ();
			if ((rms <= prev_rms) && ((prev_rms - rms)/prev_rms < rel_improve)) {
				if (debugLevel > -2) {
					System.out.println("LMA relative RMSE improvement = "+((prev_rms - rms)/prev_rms)+" < "+rel_improve+", exiting.");
				}
				if (ignore_prev_rms) {
					if (debugLevel > -3) {
						System.out.println("Will continue, as ignore_prev_rms is set");
					}					
				} else {
					if (center_radius_increase > center_radius_change) {
						if (debugLevel > -3) {
							System.out.println("Will continue, as center_radius increased from "+
									previous_center_radius+" to "+	last_center_radius +" (more than "+
									center_radius_change+"x).");
						}
					} else {
						if (vf_threshold <  (vf_drop_big * previous_vf_threshold)) {
							if (debugLevel > -3) {
								System.out.println("Will continue, as vf_threshold dropped from "+
										previous_vf_threshold+" to "+	previous_vf_threshold +" (more than "+
										vf_drop_big+"x).");
							}
						} else {
							break;
						}
					}
				}
			}

			if (center_radius_increase > center_radius_change) {
				num_tries ++;
				if (debugLevel > -2) {
					System.out.println("Increasing num_tries to "+num_tries+", as center_radius increased from "+
							previous_center_radius+" to "+	last_center_radius +" (more than "+
							center_radius_change+"x).");
				}
			}
			
			
			prev_rms=rms;
			previous_center_radius = last_center_radius;
			previous_vf_threshold = vf_threshold;
			
		} // for (int ntry = 0; ntry < num_tries; ntry++) {
		// recheck here
//		double tile_rad = rad_fraction * 0.5*Math.min(woi.width, woi.height)/GPUTileProcessor.DTT_SIZE;
		if (num_tries > 0) {
			min_tiles_rad = (int) (fill_fraction_final * 4 * tile_rad * tile_rad);
			last_center_radius = orthoPairLMA.getCenterRadius(
					max_std,         // final double      max_std,      // maximal standard deviation to limit center area
					tile_rad,        // final double      min_radius,
					min_tiles_rad,   // final int         min_tiles,
					vector_field[1], // final double [][] vector_XYS,
					ground_planes_weight, //, // null,            // final double []   weights_extra, // null or additional weights (such as elevation-based)
					tile_centers);   // final double [][] centers)

			if ((min_std_rad > 0) && (last_center_radius < min_std_rad)) {
				if (debugLevel>-3) {
					System.out.println("correlateOrthoPair(): center_radius="+
							last_center_radius+" < "+ min_std_rad);
				}
				return null;
			}
		}
		
		if (pairwiseOrthoMatch != null) {
			pairwiseOrthoMatch.jtj = jtj;
			pairwiseOrthoMatch.rms=  rms;
			pairwiseOrthoMatch.zoom_lev = zoom_lev;
		}
		
		double [] tlo_src_metric_other = new double[2]; // check affines[][][] here - 
		for (int i = 0; i < 2; i++) {
			tlo_src_metric_other[i] = affines_gpu[1][i][2] * pix_size - ortho_maps[gpu_pair[1]].vert_meters[i];
			for (int j = 0; j < 2; j++) {
				affines[1][i][j] = affines_gpu[1][i][j];
			}
		}
		for (int i = 0; i < 2; i++) {
			affines[1][i][2] = tlo_src_metric_other[i]  -
					tlo_rect_metric[1][0] * affines[1][i][0] -
					tlo_rect_metric[1][1] * affines[1][i][1];
		}
		if (debugLevel>-3) {
			System.out.println("correlateOrthoPair(): adjusted affines[1]");
			System.out.println("[["+affines[1][0][0]+","+affines[1][0][1]+","+affines[1][0][2]+"],");
			System.out.println(" ["+affines[1][1][0]+","+affines[1][1][1]+","+affines[1][1][2]+"]]");
		}
		
		// modify affines[1]
		
		if ((debugLevel > 1) && (num_tries>0)) {// show result here
			String [] map_names = {ortho_maps[gpu_pair[0]].getName(),ortho_maps[gpu_pair[1]].getName()};
			ShowDoubleFloatArrays.showArrays( 
					gpu_pair_img,
					OrthoMap.gpu_width,
					OrthoMap.gpu_height,
					true,
					"gpu_pair-zoom"+zoom_lev+"-"+ortho_maps[gpu_pair[0]].getName()+"-"+ortho_maps[gpu_pair[1]].getName(),
					map_names);
		}
		if (calc_warp) {
			double scale = 2.0;
			String dbg_suffix = batch_mode? null: "_warp";

		// return only neighbors vector field
			double [][] vf = ComboMatch.rectilinearVectorField(//rectilinearCorrelate_TD( // scene0/scene1
					clt_parameters,    // final CLTParameters          clt_parameters,
					gpu_pair_img,      // final float  [][]            fpixels, // to check for empty
					gpu_width,         // final int                    img_width,
					woi,               // Rectangle                    woi, // if null, use full GPU window
					affines_gpu,       // final double [][][]          affine,  // [2][2][3] affine coefficients to translate common to 2 images
					tp_tasks,          // TpTask [][]                  tp_tasks_o,
					batch_mode,        // final boolean                batch_mode,
					dbg_suffix,        // final String                 dbg_suffix,
					debugLevel)[1];    // final int                    debugLevel);
			double [][] tile_elevs = null;
			if (ground_planes != null) {
				for (int n = 0; n < ground_planes.length; n++) {
					ground_planes[n] = getGroundPlane(
							gpu_pair[n],     // int         scene_num,
							tp_tasks[n],     // TpTask [] tp_tasks,
							zoom_lev,        // int         zoom_lev,
							tilesX,          // int         tilesX,
							tilesY,          // int         tilesY,
							debugLevel);     // int         debugLevel)
				}
			}
			if (show_vf && (num_tries == 0) && (debugLevel > 1)) { // not needed now
				tile_elevs = new double [tp_tasks.length][];
				for (int n = 0; n < tile_elevs.length; n++) {
					String debug_title = (show_vector_field && (debugLevel>0))? (ortho_maps[gpu_pair[n]].getName()+"_plane"+dbg_suffix):null;
					tile_elevs[n] = getTileElevations(
							gpu_pair[n],     // int         scene_num,
							tp_tasks[n],     // TpTask [] tp_tasks,
							zoom_lev,        // int         zoom_lev,
							tilesX,          // int         tilesX,
							tilesY,          // int         tilesY,
							tile_woi,        // Rectangle tile_woi, // only width, height are used top-left corners are the same
							debug_title,     // String      debug_title,
							debugLevel);     // int         debugLevel)
				}
				String [] dbg_titles = new String[tile_elevs.length];
				String dbg_title = "tile_elevations";
				for (int i =0; i < tile_elevs.length; i++) {
					dbg_titles[i] = ortho_maps[gpu_pair[i]].getName();
					dbg_title += "-"+ortho_maps[gpu_pair[i]].getName();
				}
	    		ShowDoubleFloatArrays.showArrays(
	    				tile_elevs,
	    				tile_woi.width,
	    				tile_woi.height,
	    				true,
	    				dbg_title ,
	    				dbg_titles);
				System.out.println();
			}
			
			double [][] vf_interpolated = interpolateVectorField(
					vf,                // final double [][] vector_field, // sparse {vx,vy,strength}
					tp_tasks[1],       // final TpTask []   tp_tasks, // to remove no-overlap tiles
					tilesX,            // final int gpu_tilesX, // 512
					tile_woi,          // final Rectangle tile_woi, // only width, height are used top-left corners are the same
					scale,             // final double scale,
					1);                // final int debugLevel); // vector_filed_filled - use 2
			double [] tl_metric = {bounds_overlap_meters[0][0],bounds_overlap_meters[1][0]};
			
			FineXYCorr warp = new FineXYCorr(
					zoom_lev,          // int zoom_lev,
					tile_woi.width,    // int width,
					tl_metric,         // double [] tl_metric,// relative to the reference image vertical point, normally negative
					vf_interpolated);  // double [][] vf);
			return warp;
		}
		return null;
	}
	
	public double [] getTileElevations(
			int         scene_num,
			TpTask []   tp_tasks,
			int         zoom_lev,
			int         tilesX,
			int         tilesY,
			final Rectangle   tile_woi, // only width, height are used top-left corners are the same
			String      debug_title,
			int         debugLevel) {
		double    initial_above = 3; // m
		double    initial_below = 3; // m
		int       num_refine =    3;
		double    frac_above =    0.3;
		double    frac_below =    0.1;
		int zoom_lev_tiles = zoom_lev-3;
			double [] elevations = ortho_maps[scene_num].getTileElevations(
					zoom_lev, // final int zoom_level,
					tp_tasks, // final TpTask [] tp_tasks, 
					tilesX, // final int tilesX,
					tilesY, // final int tilesY,
					debugLevel); // final int debugLevel)
			double [] tile_plane_elev = new double [elevations.length];
			double [] ground_planes_metric = ortho_maps[scene_num].getPlaneMeters (
					zoom_lev_tiles,  // int       zoom_lev, // of the data (3 levels down for tiles)
					elevations,      // double [] elev,
					tilesX,          // int       width,
					initial_above,   // double    initial_above, // from average
					initial_below,   // double    initial_below, // from average, // positive value
					num_refine,      // int       num_refine,
					frac_above,      // double    frac_above,
					frac_below,      // double    frac_below,
					tile_plane_elev, // 			double [][] tile_plane_elev, // null or double[2][] will return per-tile elevations
					debug_title);    // String    debug_title)
		// tile_woi, //
			
		double [] elev_crop = new double[tile_woi.width*tile_woi.height];
		for (int row = 0; row < tile_woi.height; row++) {
			System.arraycopy(
					tile_plane_elev,
					row * tilesX,
					elev_crop,
					row*tile_woi.width,
					tile_woi.width);
		}
			
		return elev_crop;
	}
	
	/**
	 *  Get planar approximation of the ground
	 * @param scene_num scene index
	 * @param tp_tasks
	 * @param tp_tasks array of non-empty tiles to be processed in the GPU. Integer tx and ty reference
	 *        tile on a tile grid (8x8 pix), center X,Y for the only "sensor" 0 - float coordinates
	 *        corresponding to the current GPU zoom level
	 * @param zoom_lev zoom level of the GPU image, 0 is 1pix=1cm, -1 is 1pix=2cm
	 * @param tilesX
	 * @param tilesY
	 * @param debugLevel
	 * @return plane parameters - all metric, relative to the vertical point:
	 *         {tilt_x (m/m), tilt_y (m/m), offset (elevation at vertical point in meters)      
	 */
	public double [] getGroundPlane(
			int         scene_num,
			TpTask []   tp_tasks,
			int         zoom_lev,
			int         tilesX,
			int         tilesY,
			int         debugLevel) {
		double    initial_above = 3; // m
		double    initial_below = 3; // m
		int       num_refine =    3;
		double    frac_above =    0.3;
		double    frac_below =    0.1;
		int zoom_lev_tiles = zoom_lev-3;
			double [] elevations = ortho_maps[scene_num].getTileElevations(
					zoom_lev, // final int zoom_level,
					tp_tasks, // final TpTask [] tp_tasks, 
					tilesX, // final int tilesX,
					tilesY, // final int tilesY,
					debugLevel); // final int debugLevel)
			double [] ground_plane_metric = ortho_maps[scene_num].getPlaneMeters (
					zoom_lev_tiles,  // int       zoom_lev, // of the data (3 levels down for tiles)
					elevations,      // double [] elev,
					tilesX,          // int       width,
					initial_above,   // double    initial_above, // from average
					initial_below,   // double    initial_below, // from average, // positive value
					num_refine,      // int       num_refine,
					frac_above,      // double    frac_above,
					frac_below,      // double    frac_below,
					null, // 			double [][] tile_plane_elev, // null or double[2][] will return per-tile elevations
					null);    // String    debug_title)
		// tile_woi, //
		return ground_plane_metric;
	}

	
	
	
	
	/**
	 * Project object offset to along and perpendicular to the scenes offset
	 * @param object_offset object center pixel x and y offset between images (y - down) 
	 * @param scene_offset pixel offset between images 
	 * @return A pair of offsets: parallel (to scene offset) and perpendicular. Negative parallel
	 * offsets corresponds to objects above ground  
	 */
	public static double [] projectOffsetOnVector(
			double [] object_offset,
			double [] scene_offset) {
		double l1 = Math.sqrt(scene_offset[0]*scene_offset[0] + scene_offset[1]*scene_offset[1]);
		if (!(l1 > 0.00001)) {
			double [] offs_rot = new double [2];
			offs_rot[0] = ( object_offset[0]*scene_offset[0] + object_offset[1]*scene_offset[1]) / l1;
			offs_rot[1] = (-object_offset[0]*scene_offset[1] + object_offset[1]*scene_offset[0]) / l1;
			return offs_rot;
		}
		return object_offset;
	}
	
	/**
	 * Estimate object height if its offset between image is caused by its elevation
	 * @param object_offset object center pixel x and y offset between images (y - down) 
	 * @param scene_offset pixel offset between images 
	 * @param zoom_level images zoom level
	 * @param agl scene (second) altitude above ground in meters
	 * @return Estimated object elevation above ground in meters
	 */
	public static double offsetToElevation(
			double [] object_offset,
			double [] scene_offset,
			int       zoom_level,
			double    agl) {
		double l1 = Math.sqrt(scene_offset[0]*scene_offset[0] + scene_offset[1]*scene_offset[1]);
		double offs_par = projectOffsetOnVector(object_offset, scene_offset)[0]; // *OrthoMap.getPixelSizeMeters(zoom_level);
		return -agl * offs_par / l1 ;
	}
	
	/**
	 * Calculate (negative) pixel offset parallel to the scene offset corresponding to object height
	 * @param height
	 * @param scene_offset
	 * @param zoom_level
	 * @param agl
	 * @return
	 */
	public static double elevationToParallelOffset(
			double    height,
			double [] scene_offset,
			int       zoom_level,
			double    agl) {
		double l1 = Math.sqrt(scene_offset[0]*scene_offset[0] + scene_offset[1]*scene_offset[1]);
		return -height/agl * l1 ;
	}
	
	
	
	public static Rectangle scaleRectangle(
			Rectangle woi,
			int tile_size){
		int min_tx = woi.x/tile_size;
		int min_ty = woi.y/tile_size;
		int max_tx1 = (woi.x + woi.width +  (tile_size -1)) / tile_size; 
		int max_ty1 = (woi.y + woi.height + (tile_size -1)) / tile_size; 
		return new Rectangle(min_tx,min_ty,max_tx1 - min_tx,max_ty1 - min_ty);
	}
	
	public static double [] getVectorFieldError2(
			double [][] vector_field,
			int         width, // tilesX
			Rectangle   twoi) { // in tiles
		final Rectangle woi = (twoi != null)? twoi: (new Rectangle (0,0,width,vector_field.length/width));
		final int num_tiles = woi.width*woi.height;
		final int tlTile = width * woi.y + woi.x;
		final double [] vf_error2 = new double [vector_field.length];
//		Arrays.fill(vf_error2, Double.NaN);
		final Thread[] threads = ImageDtt.newThreadArray();
		final AtomicInteger ai = new AtomicInteger(0);
		for (int ithread = 0; ithread < threads.length; ithread++) {
			threads[ithread] = new Thread() {
				public void run() {
					for (int nTile = ai.getAndIncrement(); nTile < num_tiles; nTile = ai.getAndIncrement()) {
						int itileX= nTile%woi.width;
						int itileY= nTile/woi.width;
						int tile = tlTile + itileY * width + itileX;
						if (vector_field[tile] != null) {
							double [] xys = vector_field[tile];
							vf_error2[tile] += xys[0]*xys[0]+xys[1]*xys[1]; 
						}
					}
				}
			};
		}		      
		ImageDtt.startAndJoin(threads);
		return vf_error2;
	}
	
	public static double filterVectorField(
			final double [][] vector_field,
			final double []   vf_error2,
			final double      frac_remove,
			final boolean     ignore_strength,
			final int         num_bins,
			final double      max_err,
			final int         width, // tilesX
			final Rectangle   twoi) { // in tiles
		final Rectangle woi = (twoi != null)? twoi: (new Rectangle (0,0,width,vector_field.length/width));
		final int STRENGTH_INDEX=2;
		final double max_err2 = max_err*max_err;
		final double scale = num_bins/max_err2;
		final int num_tiles = woi.width*woi.height;
		final int tlTile = width * woi.y + woi.x;
		final Thread[] threads = ImageDtt.newThreadArray();
		final AtomicInteger ai = new AtomicInteger(0);
		final AtomicInteger ati = new AtomicInteger(0);
		final double [][] hist2 = new double [threads.length][num_bins];
		for (int ithread = 0; ithread < threads.length; ithread++) {
			threads[ithread] = new Thread() {
				public void run() {
					int thread_num = ati.getAndIncrement();
					for (int nTile = ai.getAndIncrement(); nTile < num_tiles; nTile = ai.getAndIncrement()) {
						int itileX= nTile%woi.width;
						int itileY= nTile/woi.width;
						int tile = tlTile + itileY * width + itileX;
						if (!Double.isNaN(vf_error2[tile]) && (vector_field[tile] != null)){ // one test is enough
							int bin = (int) Math.round(vf_error2[tile]*scale);
							if (bin >= num_bins) {
								bin= num_bins - 1;
							}
							hist2[thread_num][bin] += ignore_strength? 1.0 : vector_field[tile][STRENGTH_INDEX]; 
						}
					}
				}
			};
		}		      
		ImageDtt.startAndJoin(threads);
		ai.set(0);
		final double [] hist =    new double [num_bins];
		for (int ithread = 0; ithread < threads.length; ithread++) {
			threads[ithread] = new Thread() {
				public void run() {
					for (int bin = ai.getAndIncrement(); bin < num_bins; bin = ai.getAndIncrement()) {
						for (int i = 0; i < hist2.length; i++) {
							hist[bin]+= hist2[i][bin];
						}
					}
				}
			};
		}		      
		ImageDtt.startAndJoin(threads);
		double sw = 0;
		for (int bin = 0; bin < num_bins; bin++) {
			sw += hist[bin];
		}
		double aremove = sw * frac_remove;
		double sh = 0, shp = 0;
		double t2 = max_err2;
		for (int bin = num_bins-1; bin>=0; bin--) {
			shp = sh;
			sh += hist[bin];
			if (sh > aremove) {
				double r = (sh-aremove)/(sh-shp);
				t2 = (bin + r)/scale;
				break;
			}
		}
		final double threshold2 = t2;
		// remove bad tiles
		ai.set(0);
		for (int ithread = 0; ithread < threads.length; ithread++) {
			threads[ithread] = new Thread() {
				public void run() {
					for (int nTile = ai.getAndIncrement(); nTile < num_tiles; nTile = ai.getAndIncrement()) {
						int itileX= nTile%woi.width;
						int itileY= nTile/woi.width;
						int tile = tlTile + itileY * width + itileX;
						if ((vector_field[tile] != null) && !(vf_error2[tile] <= threshold2)){
							vector_field[tile]= null;
						}
					}
				}
			};
		}		      
		ImageDtt.startAndJoin(threads);
		return Math.sqrt(threshold2);
	}	
	
	
	
	
	
	public static OrthoMapsCollection readOrthoMapsCollection (
			String path) throws IOException, ClassNotFoundException {
		FileInputStream fileInputStream  = new FileInputStream(path);
		ObjectInputStream objectInputStream  = new ObjectInputStream(fileInputStream);
		OrthoMapsCollection orthoMapsCollection = (OrthoMapsCollection) objectInputStream.readObject();
		objectInputStream.close();
		return orthoMapsCollection;
	}
	
	public void writeOrthoMapsCollection (String path) throws IOException {
		FileOutputStream fileOutputStream = new FileOutputStream(path);
		ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
		objectOutputStream.writeObject(this);
	    objectOutputStream.flush();
	    objectOutputStream.close();
	}
	
	public static double [][] interpolateVectorField(
			final double [][] vector_field, // sparse {vx,vy,strength}
			final TpTask []   tp_tasks, // to remove no-overlap tiles
			final int         gpu_tilesX, // 512
			final Rectangle   tile_woi, // only width, height are used top-left corners are the same
			final double      scale,
			final int         debugLevel) {
		final int tiles = tile_woi.width*tile_woi.height;
		final double [][] vf = new double[2][tile_woi.width*tile_woi.height];
		for (int i = 0; i < vf.length; i++) {
			Arrays.fill(vf[i], Double.NaN);
		}
		final boolean [] mask = (tp_tasks==null)? null: new boolean [tiles];
		final Thread[] threads = ImageDtt.newThreadArray();
		final AtomicInteger ai = new AtomicInteger(0);
		for (int ithread = 0; ithread < threads.length; ithread++) {
			threads[ithread] = new Thread() {
				public void run() {
					for (int ipix = ai.getAndIncrement(); ipix < tiles; ipix = ai.getAndIncrement()) {
						int y = ipix / tile_woi.width;
						int x = ipix % tile_woi.width;
						double [] v = vector_field[y*gpu_tilesX+x];
						if (v != null) {
							for (int i = 0; i < vf.length; i++) {
								vf[i][ipix] = scale*v[i];
							}
						}
					}
				}
			};
		}		      
		ImageDtt.startAndJoin(threads);

		if (mask != null) {
			ai.set(0);
			for (int ithread = 0; ithread < threads.length; ithread++) {
				threads[ithread] = new Thread() {
					public void run() {
						for (int iTile = ai.getAndIncrement(); iTile < tp_tasks.length; iTile = ai.getAndIncrement()) {
							TpTask task = tp_tasks[iTile];
							int[] txy = task.getTileXY();
							if ((txy[0] < tile_woi.width) && (txy[1] < tile_woi.height)) {
								mask[txy[0] + txy[1] * tile_woi.width] = true;
							}
						}
					}
				};
			}		      
			ImageDtt.startAndJoin(threads);
		}
        
        // now fill NaNs in each of vf[0], vf[1]
        double [][]  vf_filled = new double[vf.length][];
		int       num_passes = 100;
		double    max_diff = 1E-4;
    	for (int i = 0; i < vf.length; i++) {
    		vf_filled[i] = OpticalFlow.fillGapsDouble(
    				vf[i],                // double []  data,
    				mask,                 // boolean [] mask_in, // do not process if false (may be null)
    				tile_woi.width,       // int       width,
    				0,                    // int       max_grow,
    				num_passes,           // int       num_passes,
    				max_diff,             // double    max_diff,
    				ImageDtt.THREADS_MAX, // int       threadsMax,
    				debugLevel-1);        // int       debug_level)
    	}		
		if (debugLevel > 0) {
			String [] dbg_titles = {"x-raw","x_filled","y-raw","y_filled"};
			double [][] dbg_img = {vf[0], vf_filled[0],vf[1], vf_filled[1]};
    		ShowDoubleFloatArrays.showArrays(
    				dbg_img,
    				tile_woi.width,
    				dbg_img[0].length / tile_woi.width,
    				true,
    				"vector_field_filled",
    				dbg_titles);
		}
		double [][] vf_out = new double [tiles][];
		ai.set(0);
		for (int ithread = 0; ithread < threads.length; ithread++) {
			threads[ithread] = new Thread() {
				public void run() {
					for (int ipix = ai.getAndIncrement(); ipix < tiles; ipix = ai.getAndIncrement()) {
						if (!Double.isNaN(vf_filled[0][ipix]) && !Double.isNaN(vf_filled[1][ipix])) {
							vf_out[ipix] = new double[] {vf_filled[0][ipix],vf_filled[1][ipix]};
						}
					}
				}
			};
		}		      
		ImageDtt.startAndJoin(threads);
		return vf_out;
	}
	
	public ImagePlus patternMatchDualWrap (
			int []        indices, // null or which indices to use (normally just 2 for pairwise comparison)
			double [][][] affines, // null or [indices.length][2][3] 
			FineXYCorr    warp,
			double [][]   ground_planes) { // null or) {    // use for a single pair only
		//indices //getAGL()
		OrthoMapsParameters omp = new OrthoMapsParameters(); // will set defaults

		double scene0_agl = ortho_maps[indices[0]].getAGL();
		// Overwrite some defaults based on AGL
		if (scene0_agl <= ((50+75)/2)) { // add versions for morning/morning or morning/evening
			omp.setDefaults50();
		} else if (scene0_agl < ((75+100)/2)) { // 75m defaults
			omp.setDefaults75();
		} else { // 100m defaults
			omp.setDefaults100();
		}
		omp.setupDialog(
				indices,
				ortho_maps);
		
		return patternMatchDual (
				indices,             // int []        indices, // null or which indices to use (normally just 2 for pairwise comparison)
				affines,             // double [][][] affines, // null or [indices.length][2][3] 
				warp,                // FineXYCorr    warp);
				ground_planes,       // double [][]   ground_planes,
				omp);//OrthoMapsParameters omp );
	}

	public ImagePlus patternMatchDual (
			int []              indices, // null or which indices to use (normally just 2 for pairwise comparison)
			double [][][]       affines, // null or [indices.length][2][3] 
			FineXYCorr          warp,    // use for a single pair only
			double [][]         ground_planes,
			OrthoMapsParameters omp) {
		boolean sort_by_best = true; // false - as was, by selected pattern
		double ease_first_best = 0.9; // before recenter
		String basename = OrthoMapsParameters.getBaseName(
				ortho_maps, // OrthoMap []           ortho_maps,			
				indices);   // int []                indices) 
		int         debugLevel            = omp.debugLevel;		
		
		// sample around + and - 128/spectrum_sample[0]
		// from (128/spectrum_sample[0])/spectrum_sample[1]
		// to (128/spectrum_sample[0])*spectrum_sample[1].
		// Calculate average of FFT power spectrum on both sides, in both directions and get ratio
		double [] spectrum_sample = {14, 1.5, 1.0};
		 
		double search_radius_recenter = 3; // how far to look for updated correlation peak after recenter
		if (warp != null) {
			warp.scale_warp = omp.scale_warp;
		}
		int last_scene = indices.length-1;
    	int min_zoom_lev = ortho_maps[indices[0]].getOriginalZoomLevel();
    	int max_zoom_lev = ortho_maps[indices[0]].getOriginalZoomLevel();

    	for (int i = 0; i < indices.length; i++) {
    		min_zoom_lev = Math.min(min_zoom_lev, ortho_maps[indices[i]].getOriginalZoomLevel());
    		max_zoom_lev = Math.max(max_zoom_lev, ortho_maps[indices[i]].getOriginalZoomLevel());
    	}
		int     zoom_level = min_zoom_lev;
		int []  wh = new int[2];
		int []  origin = new int[2];
		double [][] centers = new double [indices.length][];
		double [][] dmulti = renderMultiDouble (
				null, // double [][]   ground_planes, // null - images, non-null altitudes. use new double[2][3] for old way alt
				indices,    // int []        indices, // null or which indices to use (normally just 2 for pairwise comparison)
				true, // boolean       bounds_to_indices,
				affines,    // double [][][] affines, // null or [indices.length][2][3]
				warp,       // FineXYCorr    warp,,
				zoom_level, // int         zoom_level,
				wh,         // int []      wh,
				origin,     // int []      origin){ // maps[0] as a reference
				centers);   // double [][] centers)
		double [][] dalt = null;
		if (ground_planes != null) {
			dalt = renderMultiDouble (
					ground_planes, // double [][]   ground_planes, // null - images, non-null altitudes. use new double[2][3] for old way alt
					indices,       // int []        indices, // null or which indices to use (normally just 2 for pairwise comparison)
					true,          // boolean       bounds_to_indices,
					affines,       // double [][][] affines, // null or [indices.length][2][3]
					warp,          // FineXYCorr    warp,,
					zoom_level,    // int         zoom_level,
					wh,            // int []      wh,
					origin,        // int []      origin){ // maps[0] as a reference
					null); // centers);      // double [][] centers) // already set with dmulti

		}
		
		
		int width =  wh[0];
		int height = wh[1];
		String [] dbg_titles = new String[dmulti.length];
		for (int i = 0; i < dbg_titles.length; i++) {
			dbg_titles[i] = ""+i;
		}
		ImagePlus img_first = ShowDoubleFloatArrays.makeArrays(
				dmulti,
				width,
				height,
				basename, // ortho_maps[indices[0]].getName(),
				dbg_titles);
		showSaveImagePlus(
				img_first, // ImagePlus imp,
				omp.show_images, // boolean   show,
				omp.save_images, // boolean   save,
				omp.save_dir, // String    save_dir,
				debugLevel); // int       debugLevel)
		if (dalt != null) {
			ImagePlus img_alt = ShowDoubleFloatArrays.makeArrays(
					dalt,
					width,
					height,
					basename + "-alt", // ortho_maps[indices[0]].getName()+"-alt",
					dbg_titles);
			showSaveImagePlus(
					img_alt, // ImagePlus imp,
					omp.show_images, // boolean   show,
					omp.save_images, // boolean   save,
					omp.save_dir, // String    save_dir,
					debugLevel); // int       debugLevel)
		}
		
		
		double [][] src_marks = null;
		
		double [][][] patterns_all =   new double [indices.length][][];
		double [][] kernels_all =      new double [indices.length][];
		
		GroundObjectPattern [] gops =  new GroundObjectPattern[indices.length];
		AltitudeMismatchKernel[] ak =  new AltitudeMismatchKernel[indices.length]; 
		int [] pattern_sizes =         new int[indices.length];
		String [][]  pattern_labels =  new String[indices.length][];
		double [][][] corrs_out =      new double[indices.length][][]; 
		double [][][] corr_patterns =  new double[indices.length][][];
		int    [][][] icorr_patterns=  new int[indices.length][][]; // will only be used for main !
		
		int []        zoomout =        new int[indices.length];
		double [][]   bestcorr =       new double[indices.length][];
		double [][]   fullcorr =       new double[indices.length][];
		int [][]      bestpatt =       new int   [indices.length][];
		double []     min_corrs_full = new double[indices.length];
		ArrayList <ItemMatch> matches_list = new ArrayList <ItemMatch>();
		double []    adv_radii =             new double[indices.length];
		int    []    corr_radius =           new int [indices.length];
		for (int scene_num = 0; scene_num < indices.length; scene_num++) { // will probably just use first one, second - derivative
			gops[scene_num] = GroundObjectPattern.getPattern(
					omp.object_type, // String        object_type,
					ortho_maps[indices[scene_num]].getLocalDateTime(), // LocalDateTime utcDateTime,
					patterns); // ArrayList<GroundObjectPattern> patterns)
			if (gops[scene_num] == null) {
				System.out.println("Failed to find a pattern for object \""+omp.object_type+"\", aborting operation.");
				return null;
			}
			zoomout[scene_num] = 1;
			for (int i = zoom_level; i < gops[scene_num].getZoomLevel();i++) {
				zoomout[scene_num] *= 2;
			}
			adv_radii[scene_num] = omp.adv_radius/zoomout[scene_num];
			corr_radius[scene_num] = (int) (Math.sqrt(0.5)* adv_radii[scene_num]) -1 ;
			
			
			ImagePlus imp_pattern = new ImagePlus(gops[scene_num].getPatternPath());
			pattern_sizes[scene_num] =  imp_pattern.getWidth();
			if (pattern_sizes[scene_num] == 0) {
				System.out.println("testPatternCorrelate(): pattern \""+gops[scene_num].getPatternPath()+"\" is not found.");
				return null;
			}
			ImageStack stack_pattern = imp_pattern.getStack();
			int nSlices =       stack_pattern.getSize();
			patterns_all[scene_num] =   new double[nSlices][];
			pattern_labels[scene_num] = new String[nSlices];
			for (int k = 0; k < patterns_all[scene_num].length; k++) {
				pattern_labels[scene_num][k]=stack_pattern.getShortSliceLabel(k+1);
				float [] fpixels_pattern = (float[]) stack_pattern.getPixels(k+1);
				patterns_all[scene_num][k]=new double[fpixels_pattern.length];
				for (int i = 0; i < fpixels_pattern.length; i++) {
					patterns_all[scene_num][k][i] = fpixels_pattern[i];
				}
			}
			ak[scene_num] = AltitudeMismatchKernel.getKernel(
					gops[scene_num].getZoomLevel(), // zoom_level,       // int                               zoom_level,
					gops[scene_num].getAGL(), // double                            agl_from,
					ortho_maps[indices[scene_num]].getAGL(), // double                            agl_to,
					kernels); // ArrayList<AltitudeMismatchKernel> kernels)
			if (ak[scene_num] != null) {
				String kernel_path = ak[scene_num].getKernelPath();
				ImagePlus imp_kernel = new ImagePlus(kernel_path);
				if (imp_kernel.getWidth() > 0) {
					float [] kernel_pixels = (float[]) imp_kernel.getProcessor().getPixels();
					kernels_all[scene_num] = new double[kernel_pixels.length];
					for (int i = 0; i < kernel_pixels.length; i++) {
						kernels_all[scene_num][i] = kernel_pixels[i];
					}
				}
			}
			corrs_out[scene_num] =     new double[patterns_all[scene_num].length][]; 
			corr_patterns[scene_num] = new double[patterns_all[scene_num].length][];
			
			for (int n = 0; n < patterns_all[scene_num].length; n++) {
				if (omp.convolve_after) {
					corr_patterns[scene_num][n] = OrthoMap.patternZoomCropPad(
							patterns_all[scene_num][n],      // double [] pattern,
							pattern_sizes[scene_num],        // int       pattern_size,
							omp.corr_size,                       // int       size,
							zoomout[scene_num],              // int       zoomout,
							false);                          // out_normalize); // boolean   normalize) 
					if (kernels_all[scene_num] != null) {
						corr_patterns[scene_num][n] = OrthoMap.convolveWithKernel(
								corr_patterns[scene_num][n], // final double [] data,
								kernels_all[scene_num],      // final double [] kernel,
								omp.corr_size);                  // final int width)
					}
				} else {
					double [] pattern = patterns_all[scene_num][n].clone();
					if (kernels_all[scene_num] != null) {
						pattern = OrthoMap.convolveWithKernel(
								pattern,                 // final double [] data,
								kernels_all[scene_num],  // final double [] kernel,
								pattern_sizes[scene_num]);              // final int width)
					}
					corr_patterns[scene_num][n] = OrthoMap.patternZoomCropPad(
							pattern,                     // double [] pattern,
							pattern_sizes[scene_num],    // int       pattern_size,
							omp.corr_size,                   // int       size,
							zoomout[scene_num],          // int       zoomout,
							false);                      // out_normalize); // boolean   normalize) 
				}
			}
			if (scene_num == 0) {
				boolean debug = false; // debugLevel > 0; // 1;
				icorr_patterns[scene_num] =  OrthoMap.getIntPatterns(
						corr_patterns[scene_num], // double [][] patterns,
						omp.abs_edge_frac,                // double edge_frac, // 0.15
						omp.abs_oversize,                 // double oversize,
						debug);                       // boolean debug);
			}
			// so far contrast is only for the main scene
			if ((debugLevel > -1) && (omp.show_images || omp.save_images) && (scene_num == 0)) {
				String [] pattern_titles = new String[corr_patterns[scene_num].length];
				for (int i = 0; i < pattern_titles.length; i++) {
					pattern_titles[i] = "patt-"+i;
				}
				ImagePlus imp = ShowDoubleFloatArrays.makeArrays(
						corr_patterns[scene_num],
						omp.corr_size,
						omp.corr_size,
						"pattern-"+scene_num+"_"+omp.corr_size+"x"+omp.corr_size+(omp.convolve_after?"_convolved_after":"_convolved_before"),
						pattern_titles);
			    showSaveImagePlus(
			    		imp,         // ImagePlus imp,
			    		omp.show_images, // boolean   show,
			    		omp.save_images, // boolean   save,
			    		omp.save_dir,    // String    save_dir,
			            debugLevel); // int       debugLevel)
				double [][] dbg_img = new double [icorr_patterns[scene_num].length][icorr_patterns[scene_num][0].length];
				for (int n = 0; n < dbg_img.length; n++) {
					for (int i= 0; i < dbg_img[n].length; i++) if (icorr_patterns[scene_num][n][i] != 0){
						switch (icorr_patterns[scene_num][n][i]) {
						case 1: dbg_img[n][i] =  1.0; break;
						case 2: dbg_img[n][i] = -1.0; break;
						case 3: dbg_img[n][i] = -2.0; break;
						}
					}
				}
				ImagePlus imp1 = ShowDoubleFloatArrays.makeArrays(
						dbg_img,
						omp.corr_size,
						omp.corr_size,
						"integer_patterns",
						pattern_titles);
			    showSaveImagePlus(
			    		imp1,         // ImagePlus imp,
			    		omp.show_images, // boolean   show,
			    		omp.save_images, // boolean   save,
			    		omp.save_dir,    // String    save_dir,
			            debugLevel); // int       debugLevel)
			}
		}
		
		// Splitting - above done for both scenes, below - just for the main one
		int scene_num = 0;
		double hi_lo_freq = 0.0;
		double [][] hi_freq_arr = null;
		int []  hi_freq_wh = new int[2];
		{
			// Just testing for now
			int shrink_sel = 4;
			double spectrum_sigma = 2.0;
			int blank_xy = (int) Math.round(spectrum_sample[1]);
			double [][] hi_freq = OrthoMap.getHiFreqCirc(
					dmulti[scene_num],  // final double [] data,
					width,              // final int       width,
					omp.corr_size,          // final int       size,      // power of 2, such as 64
					spectrum_sample[0], // final double    center_period,// center frequency is size/center_period
					spectrum_sample[1], // final double    range_period, // ~1.5 - from center/range to center*range
					blank_xy,           // final int       blank_xy, //
					hi_freq_wh,         // final int []    wh,           // result size
					debugLevel);        // final int       debugLevel);
			hi_freq_arr = new double [4][hi_freq.length];
			for (int i = 0; i < hi_freq_arr.length; i++) {
				Arrays.fill(hi_freq_arr[i], Double.NaN);
			}
			for (int i = 0; i < hi_freq.length;i++) if (hi_freq[i] != null) {
				hi_freq_arr[2][i] = hi_freq[i][0];
				hi_freq_arr[3][i] = hi_freq[i][1];
				hi_freq_arr[1][i] = hi_freq[i][1]/hi_freq[i][0];
			}
			
			hi_freq_arr[0] = hi_freq_arr[1].clone();
			TileNeibs tn =  new TileNeibs(hi_freq_wh[0],hi_freq_wh[1]); 
			OrthoMap.fillNaNs(
					hi_freq_arr[0], // double [] data,
					tn, // TileNeibs tn,
					3); // int min_neibs)
			(new DoubleGaussianBlur()).blurDouble(
					hi_freq_arr[0], // double[] pixels,
					hi_freq_wh[0], // int width,
					hi_freq_wh[1], // int height,
					spectrum_sigma, // double sigmaX,
					spectrum_sigma, // double sigmaY,
					0.01); // double accuracy);
			boolean [] mask = new boolean[hi_freq.length];
			for (int i = 0; i < mask.length;i++) {
				mask[i] = hi_freq[i] != null;
			}
			tn.shrinkSelection(
					shrink_sel, // final int        shrink,           // grow tile selection by 1 over non-background tiles 1: 4 directions, 2 - 8 directions, 3 - 8 by 1, 4 by 1 more
					mask,       // final boolean [] tiles,  
					null);      // final boolean [] prohibit)
			double swd = 0.0, sw = 0.0;
			for (int i = 0; i < mask.length;i++) if (mask[i]){
				swd += hi_freq[i][1]/hi_freq[i][0];
				sw+= 1.0;
			}
			hi_lo_freq = swd/sw;
			String [] hi_freq_titles = {"blur","ratio","low","high"};
			ImagePlus img_hi_freq = ShowDoubleFloatArrays.makeArrays(
					hi_freq_arr,
					hi_freq_wh[0],
					hi_freq_wh[1],
//					indices[scene_num]+"-"+ortho_maps[indices[scene_num]].getName()+"-HIGH_FREQ",
					basename+"-HIGH_FREQ", // 
					hi_freq_titles);
			showSaveImagePlus(
					img_hi_freq,         // ImagePlus imp,
					omp.show_images, // boolean   show,
					omp.save_images, // boolean   save,
					omp.save_dir,    // String    save_dir,
					debugLevel); // int       debugLevel)
			
			System.out.println("Average hi_lo_freq="+hi_lo_freq);
			System.out.println();
		}
		
		
		corrs_out[scene_num] = correlateAllPatterns(
				dmulti[scene_num],           // double []    data,
				width,                       // int          width,
				omp.corr_size,                   // int          corr_size,    
				corr_patterns[scene_num],    // double [][]  patterns,  
				false,                       // boolean      convolve, 
				omp.phaseCoeff[scene_num],       // double       phaseCoeff,
				omp.lpf_sigma[scene_num],        //double       lpf_sigma,
				src_marks,                   // double [][]  src_marks,
				ortho_maps[indices[scene_num]].getName(), // String       prefix, // ortho_maps[indices[scene_num]].getName()
				omp.save_dir,                    // String       save_dir,
				omp.show_images,                 // boolean      show,
				omp.save_images,                 // boolean      save,
				debugLevel);                 // int          debugLevel)
		bestcorr[scene_num] = new double [dmulti[scene_num].length];
		fullcorr[scene_num] = new double [dmulti[scene_num].length];
		bestpatt[scene_num] = new int [bestcorr[scene_num].length];
		min_corrs_full[scene_num] = omp.min_corrs[scene_num] * omp.min_corr_full_rel;

		// TODO: scale adv_radius, corr_radius
		ArrayList<Point> plist =OrthoMap.combineDirCorrs (
				corrs_out[scene_num],      // final double [][] corrs,
				width,                     // final int         width,
				fullcorr[scene_num],       // final double []   fullcorr_in,
				bestcorr[scene_num],       // final double []   bestcorr_in
				bestpatt[scene_num],       // final int []      bestpatt_in,
				ease_first_best*omp.min_corrs[scene_num],      // final double      min_corr,
				min_corrs_full[scene_num], // final double      min_corr_full,
				omp.full_preference,           // final double      full_preference,
				omp.max_min_ratio,             // final double      max_min_ratio,
				omp.combine_full[0],           // final boolean     combine_full,    // multiply by normalized full pattern correlation maximum
				adv_radii[scene_num],      // final double      adv_radius,
				corr_radius[scene_num]);   // final int         corr_radius)

		for (Point p : plist) {
			double [] matches = new double [corrs_out[scene_num].length + 1];
			double [] match_xy = {p.x%width, p.x/width}; // here int values
			int    best_patt = p.y + 1;
			matches[0] = bestcorr[scene_num][p.x];
			for (int i = 1; i < matches.length; i++) {
				matches[i] = corrs_out[scene_num][i-1][p.x];
			}
			ItemMatch item_match = new ItemMatch (
					indices.length,
					match_xy); 
			item_match.addPatternMatches(
					gops[scene_num],        // GroundObjectPattern groundObjectPattern,
					matches,    // double [] matches,
					best_patt); // int      best_sub);
			matches_list.add(item_match);
		}
///		int sort_pattern_index = 0; // used in other places, not just for sorting
		int sort_pattern_mode = sort_by_best? -1 : 0; // -1 - best match, regardles off best pointer 0; //combo. 1 - full pattern
		ArrayList<Integer> match_sort =  ItemMatch.sortByMatch(
				matches_list,        // ArrayList<ItemMatch> match_list,
				gops[scene_num],
				false,               // boolean              keep_removed,// GroundObjectPattern groundObjectPattern,
				sort_pattern_mode); // int indx)
		// Print all results
		String log_lines = "\n====="+
		(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(Calendar.getInstance().getTime())+
				"=====\n");
		log_lines += "Match candidates ("+match_sort.size()+
				") that exceed minimal correlation strength of "+omp.min_corrs[scene_num]+"\n";
		
		System.out.println("Match candidates ("+match_sort.size()+
				") that exceed minimal correlation strength of "+omp.min_corrs[scene_num]);
		// remove non-overlapped from the list
		int num_non_overlap = 0;
		for (int i = match_sort.size()-1; i >=0; i--) {
			int indx =match_sort.get(i);
			ItemMatch match = matches_list.get(indx);
			int [] icenter_xy = match.getIntXY();
			for (int sn = 1; sn < indices.length; sn++) {
				if (Double.isNaN(dmulti[sn][icenter_xy[0] + icenter_xy[1] * width])) {
					match.remove();       // mark as removed
					match_sort.remove(i); // remove from index list
					log_lines += "Removed object at x="+icenter_xy[0]+", y="+icenter_xy[1]+
							" as it is not in the scenes overlap area\n";
					if (debugLevel > -1) {
						System.out.println("Removed object at x="+icenter_xy[0]+", y="+icenter_xy[1]+
								" as it is not in the scenes overlap area");
					}
					num_non_overlap++;
					break;
				}
			}
		}

		// near the edge - check both images
		for (int i = match_sort.size()-1; i >=0; i--) {
			int indx =match_sort.get(i);
			ItemMatch match = matches_list.get(indx);
			int [] icenter_xy = match.getIntXY();
			boolean has_NaN = true;
			seach_NaN: {
				int half_size=omp.corr_size/2;
				if  ((icenter_xy[0] < half_size) || (icenter_xy[1] < half_size) ||
						(icenter_xy[0] > (width - half_size)) || (icenter_xy[1] > (height - half_size))) {
					break seach_NaN;								
				}
				for (int y = (icenter_xy[1] - half_size); y < (icenter_xy[1] + half_size); y++) {
					int indx0 = y * width;
					for (int dy =- half_size; dy < half_size; dy++) {
						for (int x = (icenter_xy[0] - half_size); x < (icenter_xy[0] + half_size); x++) {
							int indx1 = indx0 + x;
							for (int sn = 0; sn < indices.length; sn++) {
								if (Double.isNaN(dmulti[sn][indx1])) {
									break seach_NaN;								
								}
							}
						}
					}

				}
				has_NaN = false;
			}
			if (has_NaN) {
				match.remove();       // mark as removed
				match_sort.remove(i); // remove from index list
				log_lines += "Removed object at x="+icenter_xy[0]+", y="+icenter_xy[1]+
						" as it is near the edge of one of the images\n";
				
				if (debugLevel > -1) {
					System.out.println("Removed object at x="+icenter_xy[0]+", y="+icenter_xy[1]+
							" as it is near the edge of one of the images");
				}
				num_non_overlap++;
			}
		}

		if (num_non_overlap > 0) {
			log_lines += "Removed "+num_non_overlap+" objects outside of the scenes overlap, "+
					match_sort.size()+" remain\n";
			System.out.println("Removed "+num_non_overlap+" objects outside of the scenes overlap, "+
					match_sort.size()+" remain");
		}
		
		if (omp.append_log) { // starting log with a date/time separator
			String log_path = omp.save_dir+basename+OrthoMapsParameters.LOG_SUFFIX;
			CalibrationFileManagement.saveStringToFile (
					log_path,      //String path,
					log_lines, // data,
					true);         // boolean append)
		}
		double [] scene_xy_offset =   {centers[last_scene][0]-centers[0][0],centers[last_scene][1]-centers[0][1]};
		double    scene_agl = gops[last_scene].getAGL(); // second scene agl
		
		omp.generateReport( // only goes to log
				null,                        // String                log_line, // should end with \n or be null
				false,                       // boolean               show, // ands with .show_images
				false,                       // boolean               save, // ands with .save_images
				true,                        // boolean               gen_results,
				false,                       // boolean               gen_parameters,
				ortho_maps,                  // OrthoMap []           ortho_maps,			
				new int [] {indices[0]},     // int []                indices,
				match_sort,                  // ArrayList<Integer>    match_sort,
				matches_list,                // ArrayList <ItemMatch> matches_list,
				zoom_level,                  // int                   zoom_level,
				scene_xy_offset,             // double []             scene_xy_offset,
				scene_agl,                   // double                scene_agl,
				corrs_out[scene_num].length, // int                   num_patterns, // corrs_out[scene_num].length
				origin,                      // int []                origin,
				centers,                     // double [][]           centers,
				gops[scene_num],             // GroundObjectPattern   gop,
				hi_lo_freq,                  // double                hi_lo_freq,
				hi_freq_wh,                  // int []                hi_freq_wh,
				hi_freq_arr);                // double [][]           hi_freq_arr,
		if (debugLevel > 0) { 
			omp.printReport(
					true,                        // boolean               gen_results,
					false,                       // boolean               gen_parameters,
					ortho_maps,                  // OrthoMap []           ortho_maps,			
					new int [] {indices[0]},     // int []                indices,
					match_sort,                  // ArrayList<Integer>    match_sort,
					matches_list,                // ArrayList <ItemMatch> matches_list,
					zoom_level,                  // int                   zoom_level,
					scene_xy_offset,             // double []             scene_xy_offset,
					scene_agl,                   // double                scene_agl,
					corrs_out[scene_num].length, // int                   num_patterns, // corrs_out[scene_num].length
					origin,                      // int []                origin,
					centers,                     // double [][]           centers,
					gops[scene_num],             // GroundObjectPattern   gop,
					hi_lo_freq,                  // double                hi_lo_freq,
					hi_freq_wh,                  // int []                hi_freq_wh,
					hi_freq_arr);                // double [][]           hi_freq_arr,
		}
		PointRoi roi = new PointRoi();
		roi.setOptions("nolabel"); // label");
		for (int mn=0; mn < match_sort.size(); mn++) {
			roi.addPoint(omp.extr_size/2,omp.extr_size/2,mn+1); // ,1);
		}

		// Extract main scene areas around candidates
		ItemMatch.setExtractedObjects(
				scene_num,          // int                   scene_num,
				omp.extr_size,          // int                   extr_size,
				gops[scene_num],    //GroundObjectPattern   gop,
				matches_list,       // ArrayList <ItemMatch> matches_list,
				match_sort,         // ArrayList<Integer>    match_sort,
				dmulti[scene_num],  // double[]              data,
				width,              // int                   width,
				debugLevel);        // int                   debugLevel)
		// calculate absolute contrasts for all patterns, update best_sub modes: 0 - keep, 1 - keep type, 2 keep type and if half +/-1,
		// 3 - free, 4 - force round objects. This may change best subpattern and should be reflected by filters. Maybe even reorder candidates?
		if (omp.corr_centered) { // re-evaluate correlations centered, recreate and reorder list
			log_lines = "";
			
			for (int mn=0; mn < match_sort.size(); mn++) {
				int indx =match_sort.get(mn);
				ItemMatch match = matches_list.get(indx);
				double [] center_xy = match.getXY();
				// correlate each extracted object with all patterns, save to corrs_out_centered
				// get correlation for the pattern[0] and re-center if needed
				//						CorrelationPeakStats[] filter_data = new CorrelationPeakStats[2];
				double [] corrs_centered = OrthoMap.correlateWithPattern(
						match.extracted_nodc[scene_num],     // extr_data[mn],                       // final double [] data,
						omp.corr_size,                           // final int       width,
						omp.corr_size,                           // final int       psize,      // power of 2, such as 64
						corr_patterns[scene_num][0], // final double [] pattern,    // [psize*psize]
						false,                               // final boolean   convolve,   // convolve, not correlate
						omp.phaseCoeff[scene_num],               // final double    phaseCoeff,
						omp.lpf_sigma[scene_num],                // final double    lpf_sigma, // 0 - do not filter
						debugLevel);                         // final int       debugLevel) {
				match.setCorrFull(
						scene_num, // int scene_num,
						corr_patterns[scene_num].length, // int num_patt,
						corrs_centered); // double [] corr_data)
				match.filter_data[scene_num][0] = new CorrelationPeakStats( // null pointer
						match.getCorr(scene_num,0),
						// corrs_centered, // double [] data, // square data
						null,                      // double [] cent_xy, // if null, use center of the square
						search_radius_recenter,    // search_radius,            // double    radius, // search for maximum within this radius. Should radius be 0 here?
						omp.filt_frac_max,                  // double    frac_max)
						0,                         // filt_other_rad,           // double    other_radius,
						debugLevel);
				double [] matches = new double [corr_patterns[scene_num].length + 1];
				int best_indx = 0;
				if (Double.isNaN(match.filter_data[scene_num][0].best_d)) {
					log_lines += "Could not find recentered maximum for ["+center_xy[0]+"/"+center_xy[1]+
							"], removing this peak.\n";
					if (debugLevel > -4) {
						System.out.println("Could not find recentered maximum for ["+center_xy[0]+"/"+center_xy[1]+
								"], removing this peak.");
					}
					// will add fake with all 0
				} else {
					//							double [] cent_offs = filter_data[0].cent_offs;
					double [] cent_offs = match.filter_data[scene_num][0].cent_offs;
					int [] old_xy = new int [] {(int) Math.round(center_xy[0]), (int) Math.round(center_xy[1])};
					center_xy[0]+=cent_offs[0];
					center_xy[1]+=cent_offs[1];
					int [] new_xy = new int [] {(int) Math.round(center_xy[0]), (int) Math.round(center_xy[1])};
					match.setComboPXY(center_xy); // update center
					if ((new_xy[0] != old_xy[0]) || (new_xy[1] != old_xy[1])) {
						log_lines += "Centered object position shifted from ["+old_xy[0]+"/"+old_xy[1]+
								"], to ["+new_xy[0]+"/"+new_xy[1]+"], rebuilding extracts.\n";
						if (debugLevel > -4) {
							System.out.println("Centered object position shifted from ["+old_xy[0]+"/"+old_xy[1]+
									"], to ["+new_xy[0]+"/"+new_xy[1]+"], rebuilding extracts.");
						}
						match.extractObject(
								dmulti[scene_num], // double [] data,
								width,             // int       width,
								scene_num,         // int       scene_num,
								omp.extr_size);        // int       extr_size);

						corrs_centered = OrthoMap.correlateWithPattern(
								match.extracted_nodc[scene_num],     // final double [] data,
								omp.corr_size,                           // final int       width,
								omp.corr_size,                           // final int       psize,      // power of 2, such as 64
								corr_patterns[scene_num][0], // final double [] pattern,    // [psize*psize]
								false,                               // final boolean   convolve,   // convolve, not correlate
								omp.phaseCoeff[scene_num],               // final double    phaseCoeff,
								omp.lpf_sigma[scene_num],                // final double    lpf_sigma, // 0 - do not filter
								debugLevel);                         // final int       debugLevel) {
						match.setCorr (
								scene_num,       // int scene_num,
								0,               // int patt_index,
								corrs_centered); // double [] corr_data)


					}
					// calculate correlations for the rest of patterns
					for (int patt_indx = 1; patt_indx < corr_patterns[scene_num].length; patt_indx++) {
						corrs_centered = OrthoMap.correlateWithPattern(
								match.extracted_nodc[scene_num],     // final double [] data,
								omp.corr_size,                           // final int       width,
								omp.corr_size,                           // final int       psize,      // power of 2, such as 64
								corr_patterns[scene_num][patt_indx], // final double [] pattern,    // [psize*psize]
								false,                               // final boolean   convolve,   // convolve, not correlate
								omp.phaseCoeff[scene_num],               // final double    phaseCoeff,
								omp.lpf_sigma[scene_num],                // final double    lpf_sigma, // 0 - do not filter
								debugLevel);                         // final int       debugLevel) {
						match.setCorr (
								scene_num,       // int scene_num,
								patt_indx,       // int patt_index,
								corrs_centered); // double [] corr_data)
					}
					double [][] corrs_all = match.getCorrs(
							scene_num); // int scene_num);
					int cent_indx = (omp.corr_size+1)*omp.corr_size/2;
					double best_v = corrs_all[1][cent_indx]; 
					double worst_v = best_v;
					double full_v = corrs_all[0][cent_indx];
					best_indx=1;
					for (int patt_indx = 2; patt_indx < corrs_all.length; patt_indx++) {
						if (corrs_all[patt_indx][cent_indx] > best_v) {
							best_v=corrs_all[patt_indx][cent_indx];
							best_indx = patt_indx;
						}
						if (corrs_all[patt_indx][cent_indx] < worst_v) {
							worst_v = corrs_all[patt_indx][cent_indx];
						}
					}
					if (full_v * omp.full_preference > best_v) {
						best_indx = 0;
					} else {
						if ((worst_v > 0) && (best_v/worst_v < omp.max_min_ratio)) {
							best_indx = 0;
						}
					}
					matches[0] = corrs_all[best_indx][cent_indx];
					for (int i = 1; i < matches.length; i++) {
						matches[i] = corrs_all[i-1][cent_indx];
					}
				}
				// select best pattern similar to combineDirCorrs(), using center pixel
				// add to a new list (and then replace main list and reorder?)
				//matches_list_centered
				match.addPatternMatches(  // will update existing match too
						gops[scene_num],  // GroundObjectPattern groundObjectPattern,
						matches,          // double [] matches,
						best_indx + 1);   // int      best_sub);

			} // for (int mn=0; mn < match_sort.size(); mn++) {
			// some matches are already marked as removed
			match_sort =  ItemMatch.sortByMatch(
					matches_list,        // ArrayList<ItemMatch> match_list,
					gops[scene_num],     // GroundObjectPattern groundObjectPattern,
					false,               // boolean              keep_removed,// GroundObjectPattern groundObjectPattern,
					sort_pattern_mode);  // int indx)
			// Print all results
			log_lines += "Centered match candidates ("+match_sort.size()+")\n";

			System.out.println("Centered match candidates ("+match_sort.size()+
					")"); // +min_corrs[scene_num]);
			if (omp.append_log) { // starting log with a date/time separator
				String log_path = omp.save_dir+basename+OrthoMapsParameters.LOG_SUFFIX;
				CalibrationFileManagement.saveStringToFile (
						log_path,      //String path,
						log_lines, // data,
						true);         // boolean append)
			}
			
			
			// print updated list
			omp.generateReport( // only goes to log
					null,                        // String                log_line, // should end with \n or be null
					false,                       // boolean               show, // ands with .show_images
					false,                       // boolean               save, // ands with .save_images
					true,                        // boolean               gen_results,
					false,                       // boolean               gen_parameters,
					ortho_maps,                  // OrthoMap []           ortho_maps,			
					new int [] {indices[0]},     // int []                indices,
					match_sort,                  // ArrayList<Integer>    match_sort,
					matches_list,                // ArrayList <ItemMatch> matches_list,
					zoom_level,                  // int                   zoom_level,
					scene_xy_offset,             // double []             scene_xy_offset,
					scene_agl,                   // double                scene_agl,
					corrs_out[scene_num].length, // int                   num_patterns, // corrs_out[scene_num].length
					origin,                      // int []                origin,
					centers,                     // double [][]           centers,
					gops[scene_num],             // GroundObjectPattern   gop,
					hi_lo_freq,                  // double                hi_lo_freq,
					hi_freq_wh,                  // int []                hi_freq_wh,
					hi_freq_arr);                // double [][]           hi_freq_arr,
			if (debugLevel > 0) { 
				omp.printReport(
						true,                        // boolean               gen_results,
						false,                       // boolean               gen_parameters,
						ortho_maps,                  // OrthoMap []           ortho_maps,			
						new int [] {indices[0]},     // int []                indices,
						match_sort,                  // ArrayList<Integer>    match_sort,
						matches_list,                // ArrayList <ItemMatch> matches_list,
						zoom_level,                  // int                   zoom_level,
						scene_xy_offset,             // double []             scene_xy_offset,
						scene_agl,                   // double                scene_agl,
						corrs_out[scene_num].length, // int                   num_patterns, // corrs_out[scene_num].length
						origin,                      // int []                origin,
						centers,                     // double [][]           centers,
						gops[scene_num],             // GroundObjectPattern   gop,
						hi_lo_freq,                  // double                hi_lo_freq,
						hi_freq_wh,                  // int []                hi_freq_wh,
						hi_freq_arr);                // double [][]           hi_freq_arr,
			}
		} else { // reuse already calculated correlations
			for (int mn=0; mn < match_sort.size(); mn++) {
				int indx =match_sort.get(mn);
				ItemMatch match = matches_list.get(indx);
				match.extractCorrs( // copy all 9 layers
						corrs_out[scene_num], // double [][] corr_data,
						width, // int         width,
						scene_num, // int         scene_num,
						omp.extr_size); // int         extr_size)
			}
		}

		setAbsoluteContrasts( // move later than corr_centered
				omp,                       // OrthoMapsParameters   omp
				gops[scene_num],           // GroundObjectPattern   gop,
				matches_list,              // ArrayList <ItemMatch> matches_list,
				match_sort,                // ArrayList<Integer>    match_sort,
				scene_num,                 // int                   scene_num,
				icorr_patterns[scene_num], // int [][]              ipatterns,
				debugLevel); // int                   debugLevel

		for (int mn=0; mn < match_sort.size(); mn++) {
			int indx =match_sort.get(mn);
			ItemMatch match = matches_list.get(indx);
			// should be already updated by setAbsoluteContrasts();
			int best_patt = match.getPatternMatch(gops[scene_num]).getBestSub() - 1;
			//					double [] center_xy = match.getXY();
			match.filter_data[scene_num][0] = new CorrelationPeakStats( // null pointer
					match.getCorr(scene_num,0),// corrs_centered, // double [] data, // square data
					null,                      // double [] cent_xy, // if null, use center of the square
					omp.search_radius,             // search_radius,            // double    radius, // search for maximum within this radius. Should radius be 0 here?
					omp.filt_frac_max,                  // double    frac_max)
					omp.filt_other_rad,            // filt_other_rad,           // double    other_radius,
					debugLevel);

			match.setCorrHalf(
					scene_num, // int scene_num,
					best_patt, // int     best_patt,
					omp.combine_full[0]); // boolean combine_full) {

			match.filter_data[scene_num][1] = new CorrelationPeakStats(
					match.getCorrHalf(scene_num), // double [] data, // square data
					null,                         // double [] cent_xy, // if null, use center of the square
					omp.search_radius,                // double    radius, // search for maximum within this radius. Should radius be 0 here?
					omp.filt_frac_max,                     // double    frac_max)
					omp.filt_other_rad,               // double    other_radius,
					debugLevel);                  // final int       debugLevel) {
		}

		boolean               show_centers = true;
		if (indices.length == 1) { // for a single-scene show before filtering by the first(only) scene
			for (int sn = 0; sn < indices.length; sn++) {
				String prefix = ortho_maps[indices[sn]].getName()+"-prefilter1";
				String suffix0 = "_pc"+omp.phaseCoeff[sn]+"_lpf"+omp.lpf_sigma[sn];
				for (int mode = 0; mode < 4; mode++) { // all 4 image types
					String suffix = (mode <2)? "":suffix0;
					if ((sn == 0) || (mode == 0)) {
						ItemMatch.getImageExtracts(
								prefix,          // String                prefix, // include PC, filter here
								suffix,          // String                suffix, // w/o .tiff
								omp.show_images,     // boolean               show,
								omp.save_images,     // boolean               save,
								false,           // boolean               show_removed,
								omp.save_dir,        // String                save_dir,
								sn,              // int                   scene_num,
								omp.remove_dc,       // boolean               nodc,
								mode,            // int                   mode, // 0 - extract, 1 - masked, 2 - corr full, 3 - corr half
								show_centers,    // boolean               show_centers,
								omp.extr_size,       // int                   extr_size,
								gops[scene_num], // GroundObjectPattern   gop,
								matches_list,    // ArrayList <ItemMatch> matches_list,
								match_sort,      // ArrayList<Integer>    match_sort,
								debugLevel);     // int                   debugLevel)
					}
				}
			}
		}
		if (!omp.filt_atonce) { // first filter using scene 1 only
			boolean[] filt_main_other = new boolean [indices.length]; //{true,false};
			filt_main_other[0] = true;
			int num_removed = filterCandidates(
					omp,                // OrthoMapsParameters   omp,
					basename,           // String                basename,
					filt_main_other,    // new boolean []        filt_main_other,
					gops[scene_num],    // GroundObjectPattern   gop,
					zoom_level,         // int       zoom_level,
					scene_agl,          // double                scene_agl,
					scene_xy_offset,    // double []             scene_xy_offset, //second scene vertical projection offset from the first scene
					matches_list,       // ArrayList <ItemMatch> matches_list,
					match_sort,         // ArrayList<Integer>    match_sort,
					debugLevel);        // int                   debugLevel )
//			if (!filt_keep_pre && (removed.length > match_sort.size())) { // decimate data arrays
//				removed = new boolean [match_sort.size()];
//			}
		}
		if (indices.length > 1) { // for a multi-scene show after filtering by the first scene
			// here extract for the second scene
			for (int sn = 1; sn < indices.length; sn++) {
				ItemMatch.setExtractedObjects(
						sn,           // int                   scene_num,
						omp.extr_size,    // int                   extr_size,
						gops[sn],     // GroundObjectPattern   gop,
						matches_list, // ArrayList <ItemMatch> matches_list,
						match_sort,   // ArrayList<Integer>    match_sort,
						dmulti[sn],   // double[]              data,
						width,        // int                   width,
						debugLevel);  // int                   debugLevel)
			}					

			for (int sn = 0; sn < indices.length; sn++) {
				String prefix = ortho_maps[indices[sn]].getName()+"-postfilter1";
				String suffix0 = "_pc"+omp.phaseCoeff[sn]+"_lpf"+omp.lpf_sigma[sn];
				for (int mode = 0; mode < 4; mode++) { // all 4 image types
					String suffix = (mode <2)? "":suffix0;
					if ((sn == 0) || (mode == 0)) {
						ItemMatch.getImageExtracts(
								prefix,          // String                prefix, // include PC, filter here
								suffix,          // String                suffix, // w/o .tiff
								omp.show_images,     // boolean               show,
								omp.save_images,     // boolean               save,
								false,           // boolean               show_removed,
								omp.save_dir,        // String                save_dir,
								sn,              // int                   scene_num,
								omp.remove_dc,       // boolean               nodc,
								mode,            // int                   mode, // 0 - extract, 1 - masked, 2 - corr full, 3 - corr half
								show_centers,    // boolean               show_centers,
								omp.extr_size,       // int                   extr_size,
								gops[scene_num], // GroundObjectPattern   gop,
								matches_list,    // ArrayList <ItemMatch> matches_list,
								match_sort,      // ArrayList<Integer>    match_sort,
								debugLevel);     // int                   debugLevel)
					}
				}
			}
		}
		// display extracted correlations for the main scene:
		if (indices.length > 1) {
			// print updated table after some scenes may be removed by the filter
			omp.generateReport( // only goes to log
					"\nAfter first scene filtering:\n", // String                log_line, // should end with \n or be null
					false,                       // boolean               show, // ands with .show_images
					false,                       // boolean               save, // ands with .save_images
					true,                        // boolean               gen_results,
					false,                       // boolean               gen_parameters,
					ortho_maps,                  // OrthoMap []           ortho_maps,			
					new int [] {indices[0]},     // int []                indices,
					match_sort,                  // ArrayList<Integer>    match_sort,
					matches_list,                // ArrayList <ItemMatch> matches_list,
					zoom_level,                  // int                   zoom_level,
					scene_xy_offset,             // double []             scene_xy_offset,
					scene_agl,                   // double                scene_agl,
					corrs_out[scene_num].length, // int                   num_patterns, // corrs_out[scene_num].length
					origin,                      // int []                origin,
					centers,                     // double [][]           centers,
					gops[scene_num],             // GroundObjectPattern   gop,
					hi_lo_freq,                  // double                hi_lo_freq,
					hi_freq_wh,                  // int []                hi_freq_wh,
					hi_freq_arr);                // double [][]           hi_freq_arr,
			if (debugLevel > 0) { 
				System.out.println("\nAfter first scene filtering:");
				omp.printReport(
						true,                        // boolean               gen_results,
						false,                        // boolean               gen_parameters,
						ortho_maps,                  // OrthoMap []           ortho_maps,			
						new int [] {indices[0]},     // int []                indices,
						match_sort,                  // ArrayList<Integer>    match_sort,
						matches_list,                // ArrayList <ItemMatch> matches_list,
						zoom_level,                  // int                   zoom_level,
						scene_xy_offset,             // double []             scene_xy_offset,
						scene_agl,                   // double                scene_agl,
						corrs_out[scene_num].length, // int                   num_patterns, // corrs_out[scene_num].length
						origin,                      // int []                origin,
						centers,                     // double [][]           centers,
						gops[scene_num],             // GroundObjectPattern   gop,
						hi_lo_freq,                  // double                hi_lo_freq,
						hi_freq_wh,                  // int []                hi_freq_wh,
						hi_freq_arr);                // double [][]           hi_freq_arr,
			}
		}

		// Correlate second scene only for selected fragments
		for (int scene_other = 1; scene_other < indices.length; scene_other++) { // normally just 1
			for (int mn=0; mn < match_sort.size(); mn++) {
				int indx =match_sort.get(mn);
				ItemMatch match = matches_list.get(indx);
				int best_patt = match.getPatternMatch(gops[scene_num]).getBestSub() - 1;
				// correlate extracted squares of the other scene with full pattern 
				double [] corr_full = OrthoMap.correlateWithPattern(
						match.extracted_nodc[scene_other],   // extr_data[mn],                       // final double [] data,
						omp.corr_size,                           // final int       width,
						omp.corr_size,                           // final int       psize,      // power of 2, such as 64
						corr_patterns[scene_other][0],       // final double [] pattern,    // [psize*psize]
						false,                               // final boolean   convolve,   // convolve, not correlate
						omp.phaseCoeff[scene_other],             // final double    phaseCoeff,
						omp.lpf_sigma[scene_other],              // final double    lpf_sigma, // 0 - do not filter
						debugLevel);                         // final int       debugLevel) {
				// set [9] array of correlations and copy full pattern ([0]) data. Other (1-9) will not be used
				// for scene_other 
				match.setCorrFull(
						scene_other,                         // int scene_num,
						corr_patterns[scene_other].length,   // int num_patt,
						corr_full);                     // double [] corr_data)

				match.filter_data[scene_other][0] = new CorrelationPeakStats( // null pointer
						match.getCorr(scene_other,0),        // double [] data, // square data
						null,                                // double [] cent_xy, // if null, use center of the square
						omp.search_radius,                       // search_radius,            // double    radius, // search for maximum within this radius. Should radius be 0 here?
						omp.filt_frac_max,                            // double    frac_max)
						omp.filt_other_rad,                      // double    other_radius,
						debugLevel);
				if (best_patt > 0) { // not full match - for full match it will copy full correlation
					double [] corr_half = OrthoMap.correlateWithPattern(
							match.extracted_nodc[scene_other],     // extr_data[mn],                       // final double [] data,
							omp.corr_size,                             // final int       width,
							omp.corr_size,                             // final int       psize,      // power of 2, such as 64
							corr_patterns[scene_other][best_patt], // final double [] pattern,    // [psize*psize]
							false,                                 // final boolean   convolve,   // convolve, not correlate
							omp.phaseCoeff[scene_other],               // final double    phaseCoeff,
							omp.lpf_sigma[scene_other],                // final double    lpf_sigma, // 0 - do not filter
							debugLevel);                           // final int       debugLevel) {
					// setting only one correlation of 8 halves 
					match.setCorr (
							scene_other,     // int scene_num,
							best_patt,       // int patt_index,
							corr_half);      // double [] corr_data)
				}
				// Do it in any case, if best_patt==0 it will copy, optionally limit by 0 and square full correlation
				match.setCorrHalf( // D
						scene_other, // int scene_num,
						best_patt, // int     best_patt,
						omp.combine_full[1]); // boolean combine_full) {
				// set statistics for half-correlations of the second scene
				// before it was just a copy of full, regardless of combine_full[1]
				match.filter_data[scene_other][1] = new CorrelationPeakStats(
						match.getCorrHalf(scene_other), // double [] data, // square data
						null,                           // double [] cent_xy, // if null, use center of the square
						omp.search_radius,                  // double    radius, // search for maximum within this radius. Should radius be 0 here?
						omp.filt_frac_max,                       // double    frac_max)
						omp.filt_other_rad,                 // double    other_radius,
						debugLevel);                    // final int       debugLevel) {
			}
			// show correlations for the second scene
			String prefix = ortho_maps[indices[scene_num]].getName()+"-postfilter1";
			String suffix0 = "_pc"+omp.phaseCoeff[scene_other]+"_lpf"+omp.lpf_sigma[scene_other];
			for (int mode = 2; mode < 4; mode++) { // all 2 correlation types (full and half)
				String suffix = (mode < 2)? "":suffix0;
				ItemMatch.getImageExtracts(
						prefix,          // String                prefix, // include PC, filter here
						suffix,          // String                suffix, // w/o .tiff
						omp.show_images,     // boolean               show,
						omp.save_images,     // boolean               save,
						false,           // boolean               show_removed,
						omp.save_dir,        // String                save_dir,
						scene_other,     // int                   scene_num,
						omp.remove_dc,       // boolean               nodc,
						mode,            // int                   mode, // 0 - extract, 1 - masked, 2 - corr full, 3 - corr half
						show_centers,    // boolean               show_centers,
						omp.extr_size,       // int                   extr_size,
						gops[scene_num], // GroundObjectPattern   gop,
						matches_list,    // ArrayList <ItemMatch> matches_list,
						match_sort,      // ArrayList<Integer>    match_sort,
						debugLevel);     // int                   debugLevel)
			}
		}
		// final filter
		boolean[] filt_main_other = new boolean [indices.length]; //{true,false};
		Arrays.fill(filt_main_other, true);
///		int num_removed = 
				filterCandidates( // will filter again if already
				omp,                // OrthoMapsParameters   omp,
				basename,           // String                basename,
				filt_main_other,    // new boolean []            filt_main_other,
				gops[scene_num],    // GroundObjectPattern   gop,
				zoom_level,         // int                   zoom_level,
				scene_agl,          // double                scene_agl,
				scene_xy_offset,    // double []             scene_xy_offset, //second scene vertical projection offset from the first scene
				matches_list,       // ArrayList <ItemMatch> matches_list,
				match_sort,         // ArrayList<Integer>    match_sort,
				debugLevel);        // int                   debugLevel )

		if (!omp.filt_keep) { // show final filtered images again - just extracts (2) and all correlations (4)
			for (int sn = 0; sn < indices.length; sn++) {
				String prefix = ortho_maps[indices[sn]].getName()+"-final";
				String suffix0 = "_pc"+omp.phaseCoeff[sn]+"_lpf"+omp.lpf_sigma[sn];
				for (int mode = 0; mode < 4; mode++) { // all 4 image types
					String suffix = (mode <2)? "":suffix0;
					if ((sn == 0) || (mode != 1)) { // masked - for main scene only
						ItemMatch.getImageExtracts(
								prefix,          // String                prefix, // include PC, filter here
								suffix,          // String                suffix, // w/o .tiff
								omp.show_images,     // boolean               show,
								omp.save_images,     // boolean               save,
								false,           // boolean               show_removed,
								omp.save_dir,        // String                save_dir,
								sn,              // int                   scene_num,
								omp.remove_dc,       // boolean               nodc,
								mode,            // int                   mode, // 0 - extract, 1 - masked, 2 - corr full, 3 - corr half
								show_centers,    // boolean               show_centers,
								omp.extr_size,       // int                   extr_size,
								gops[scene_num], // GroundObjectPattern   gop,
								matches_list,    // ArrayList <ItemMatch> matches_list,
								match_sort,      // ArrayList<Integer>    match_sort,
								debugLevel);     // int                   debugLevel)
					}
				}
			}
		}

		omp.generateReport( // final
				null,                        // String                log_line, // should end with \n or be null
				true,                        // boolean              show, // ands with .show_images
				true,                        // boolean               save, // ands with .save_images
				true,                        // boolean               gen_results,
				true,                        // boolean               gen_parameters,
				ortho_maps,                  // OrthoMap []           ortho_maps,			
				indices,                     // int []                indices,
				match_sort,                  // ArrayList<Integer>    match_sort,
				matches_list,                // ArrayList <ItemMatch> matches_list,
				zoom_level,                  // int                   zoom_level,
				scene_xy_offset,             // double []             scene_xy_offset,
				scene_agl,                   // double                scene_agl,
				corrs_out[scene_num].length, // int                   num_patterns, // corrs_out[scene_num].length
				origin,                      // int []                origin,
				centers,                     // double [][]           centers,
				gops[scene_num],             // GroundObjectPattern   gop,
				hi_lo_freq,                  // double                hi_lo_freq,
				hi_freq_wh,                  // int []                hi_freq_wh,
				hi_freq_arr);                // double [][]           hi_freq_arr,
		if (debugLevel > -3) { 
			omp.printReport(
					true,                        // boolean               gen_results,
					true,                        // boolean               gen_parameters,
					ortho_maps,                  // OrthoMap []           ortho_maps,			
					indices,                     // int []                indices,
					match_sort,                  // ArrayList<Integer>    match_sort,
					matches_list,                // ArrayList <ItemMatch> matches_list,
					zoom_level,                  // int                   zoom_level,
					scene_xy_offset,             // double []             scene_xy_offset,
					scene_agl,                   // double                scene_agl,
					corrs_out[scene_num].length, // int                   num_patterns, // corrs_out[scene_num].length
					origin,                      // int []                origin,
					centers,                     // double [][]           centers,
					gops[scene_num],             // GroundObjectPattern   gop,
					hi_lo_freq,                  // double                hi_lo_freq,
					hi_freq_wh,                  // int []                hi_freq_wh,
					hi_freq_arr);                // double [][]           hi_freq_arr,
		}		
		
		ImagePlus img_final = null;
		{ // Create image with marked mines				
			PointRoi final_roi = new PointRoi();
			final_roi.setOptions("label");
			for (int i = 0; i < match_sort.size(); i++) {
				int indx =match_sort.get(i);
				ItemMatch match = matches_list.get(indx);
				//						double [] match_values = match.getMatchValues(gops[scene_num]);
				double [] center_xy = match.getXY();
				final_roi.addPoint(center_xy[0], center_xy[1]); // ,1);
			}
			String []  final_titles = new String[indices.length];
			double [][] final_map = new double [indices.length][];
			for (int i = 0; i < final_map.length; i++) {
				final_titles[i] = ortho_maps[indices[i]].getName();
				final_map[i] =  dmulti[i];
			}
			String final_title =indices[0]+"";
			for (int i = 1; i < final_map.length; i++) {
				final_title +="-"+indices[i];
			}
			final_title += "_"+final_titles[0];
			for (int i = 1; i < final_map.length; i++) {
				final_title +="-"+final_titles[i];
			}
			final_title += "-MARKED_"+omp.object_type+"_"+match_sort.size()+"_OBJECTS";
			img_final = ShowDoubleFloatArrays.makeArrays(
					final_map,
					width,
					height,
					final_title,				
					final_titles);
			img_final.setRoi(final_roi);
			showSaveImagePlus(
					img_final,            // ImagePlus imp,
					omp.show_final_image, // boolean   show,
					omp.save_images,      // boolean   save,
					omp.save_dir,         // String    save_dir,
					debugLevel);          // int       debugLevel)
		}

		System.out.println("patternMatchDual(): all correlations DONE");
		return img_final;
	}
	
	public static double[][]  correlateAllPatterns(
			double []    data,
			int          width,
			int          corr_size,    
			double [][]  patterns,  
			boolean      convolve, 
			double       phaseCoeff,
			double       lpf_sigma,
			double [][]  src_marks,
			String       prefix, // ortho_maps[indices[scene_num]].getName(). null - do not show
			String       save_dir,
			boolean      show,
			boolean      save,
			int          debugLevel) {
		double [][] corrs_out = new double [patterns.length][];
		for (int n = 0; n < patterns.length; n++) {
			corrs_out[n]= OrthoMap.correlateWithPattern(
					data,        // final double [] data,
					width,       // final int       width,
					corr_size,   // final int       psize,      // power of 2, such as 64
					patterns[n], // final double [] pattern,    // [psize*psize]
					convolve,    // final boolean   convolve,   // convolve, not correlate
					phaseCoeff,  // final double    phaseCoeff,
					lpf_sigma,   // final double    lpf_sigma, // 0 - do not filter
					debugLevel); // final int       debugLevel) {
		}
		if ((prefix != null) && (show || save)) {
			String [] patt_titles = new String[corrs_out.length];
			for (int i = 0; i < patt_titles.length; i++) {
				patt_titles[i] = "patt_"+i;
			}
			int height = data.length/width;
			ImagePlus imp_corr = ShowDoubleFloatArrays.makeArrays(
					corrs_out,
					width,
					height,
					prefix+"-PATTERN_CORRS_PC"+phaseCoeff+"_LPF"+lpf_sigma,				
					patt_titles); // test_titles,
			if (src_marks != null) {
				PointRoi roi = new PointRoi();
				roi.setOptions("label");
				for (int i = 0; i < src_marks.length; i++) {
					roi.addPoint(src_marks[i][0],src_marks[i][1]); // ,1);
				}
				imp_corr.setRoi(roi);
			}
		    showSaveImagePlus(
		    		imp_corr,         // ImagePlus imp,
		            show, // boolean   show,
		            save, // boolean   save,
		            save_dir,    // String    save_dir,
		            debugLevel); // int       debugLevel)
//			imp_corr.show();
		}
		return corrs_out;
	}
	
	/**
	 * Returns boolean array of original length
	 * @param filt_keep
	 * @param filt_main_other
	 * @param min_corrs
	 * @param min_corr_full_rel
	 * @param filt_max_radius
	 * @param filt_elongation
	 * @param filt_dist
	 * @param filt_height
	 * @param filter_data
	 * @param scene_xy_offset
	 * @param matches_list
	 * @param match_sort
	 * @param debugLevel
	 * @return
	 */
	public static int filterCandidates(
			OrthoMapsParameters   omp,
			String                basename,
			boolean []            filt_main_other, // length== number of scenes
			GroundObjectPattern   gop,
			int                   zoom_level,
			double                scene_agl,
			double []             scene_xy_offset, //second scene vertical projection offset from the first scene
			ArrayList <ItemMatch> matches_list,
			ArrayList<Integer>    match_sort,
			int                   debugLevel ) { // >0 - print
		StringBuffer sb =    new StringBuffer();
		int num_scenes = filt_main_other.length;
		int num_removed = 0;
		double [][] min_corrs_other = {
				{omp.min_corrs[0]*omp.min_corr_full_rel, omp.min_corrs[0]},
				{omp.min_corrs[1]*omp.min_corr_full_rel, omp.min_corrs[1]}};
		double offset_from_height = elevationToParallelOffset( // will be negative
				omp.filt_height,     // double    height,
				scene_xy_offset, // double [] scene_offset,
				zoom_level,      // int       zoom_level,
				scene_agl) ;//  double    agl)
		for (int mn=0; mn < match_sort.size(); mn++){
			ItemMatch match = matches_list.get(match_sort.get(mn));
			if (match.isRemoved()) {
				continue; // already removed
			}
			CorrelationPeakStats[][] filter_data = match.filter_data;
			
			int [] ixy = match.getIntXY();
			String name = ixy[0]+"/"+ixy[1];
			double abs_contrast = match.getAbsoluteContrast();
			double roundness = match.getRoundness(gop);
			int best_patt= match.getPatternMatch(gop).getBestSub(); // 1-based
			double best_value = match.getMatchBestValue(gop);
			boolean is_partial = best_patt != 1; // only apply to combined full/half correlations, just full may have nearby maximums near the same size
			double [] strength_full_half_ratio = new double[num_scenes];
			for (int scene_num = 0; scene_num < num_scenes; scene_num++) if (filt_main_other[scene_num] && (filter_data[scene_num] != null)){
					strength_full_half_ratio[scene_num] = filter_data[scene_num][0].best_d / filter_data[scene_num][1].best_d;
			}
			// filt_abs_easepart
			double min_abs_contrast = omp.filt_abs_contrast;
			if (is_partial) {
				min_abs_contrast *= omp.filt_abs_easepart;
			}
			if (abs_contrast < min_abs_contrast) {
				sb.append(name+": filtered out by "+NAME_MO[0]+" scene because its absolute contrast = "+
						abs_contrast+ " < "+min_abs_contrast+(is_partial?(". Eased for partial from "+omp.filt_abs_contrast):""));
				sb.append("\n");
				if (!match.isRemoved()) num_removed++;
				match.remove();
			}
			if (!is_partial && !(roundness >= omp.filt_roundness[0])) { // NaN - also bad
				sb.append(name+": filtered out by "+NAME_MO[0]+" scene because it is full/round, and its roundness = "+
						roundness+ " < "+omp.filt_roundness[0]);
				sb.append("\n");
				if (!match.isRemoved()) num_removed++;
				match.remove();
			}
			// only apply to full patterns, because partial can be filtered without it
			if (!is_partial && !(best_value >= omp.filt_best[0])) { // NaN - also bad
				sb.append(name+": filtered out by "+NAME_MO[0]+
						" scene because it is full-pattern and its best correlation value = "+
						best_value+ " < "+omp.filt_best[0]);
				sb.append("\n");
				if (!match.isRemoved()) num_removed++;
				match.remove();
			}
			
			for (int hf = 0; hf < 2; hf++) {
				for (int scene_num = 0; scene_num < num_scenes; scene_num++) if (filt_main_other[scene_num]){
					if (Double.isNaN(filter_data[scene_num][hf].best_d)) {
						sb.append(name+": filtered out by "+NAME_MO[scene_num]+"/"+NAME_FH[hf]+
								" as correlation maximum is not found");
						sb.append("\n");						
						if (!match.isRemoved()) num_removed++;
						match.remove();
						continue;
					}
					double eff_rad =       filter_data[scene_num][hf].eff_rad;
					double elong =         filter_data[scene_num][hf].elong;
					double corr_val =      filter_data[scene_num][hf].best_d;
					double dist =          filter_data[scene_num][hf].dist;
					double near_max_frac = filter_data[scene_num][hf].max_other;
					double [] second_xy_offsets = filter_data[scene_num][hf].cent_offs;					
					// calculate projection
					double [] scnd_xy_proj =  projectOffsetOnVector(
							second_xy_offsets, // double [] object_offset,
							scene_xy_offset); // double [] scene_offset)
					if (corr_val < min_corrs_other[scene_num][hf]) {
						sb.append(name+": filtered out by "+NAME_MO[scene_num]+" correlation maximum value for "+
								NAME_FH[hf]+ " = "+corr_val+" < "+ min_corrs_other[scene_num][hf]);
						sb.append("\n");						if (scene_num == 0) {
							sb.append(
							"For the main scene it could happen if the sub_pattern index changed after"+
							" the absolute contrast evaluation.");
							sb.append("\n");
						}
						if (!match.isRemoved()) num_removed++;
						match.remove();
					}
					if (is_partial && (hf > 0) && (corr_val < omp.min_corr_half_rel * omp.min_corrs[scene_num])) {
						sb.append(name+": filtered out by "+NAME_MO[scene_num]+
								" because it is partial and correlation maximum value for "+
								NAME_FH[hf]+ " = "+corr_val+" < "+ omp.min_corr_half_rel*min_corrs_other[scene_num][hf]+
								" (increased by "+omp.min_corr_half_rel+" from base "+omp.min_corrs[scene_num]+").");
						sb.append("\n");
						if (!match.isRemoved()) num_removed++;
						match.remove();
					}
					
					if (scene_num > 0) { // other scene
						//	{best_d, eff_rad, elong, dist, cent_offs[0], cent_offs[1]} ;
						if (dist > omp.filt_dist[0]) {
							sb.append(name+": filtered out by "+NAME_MO[scene_num]+" distance from the main scene peak "+
									NAME_FH[hf]+ " = "+dist+" > "+omp.filt_dist[0]);
							sb.append("\n");
							if (!match.isRemoved()) num_removed++;
							match.remove();
						}
						// distance for main only or both?
						if (Math.abs(scnd_xy_proj[1]) > omp.filt_dist[1]) {
							sb.append(name+": filtered out by "+NAME_MO[scene_num]+" distance from the main scene peak perpendicular to the scene offset vector "+
									NAME_FH[hf]+ " = "+scnd_xy_proj[1]+", abs() > "+omp.filt_dist[1]);
							sb.append("\n");
							if (!match.isRemoved()) num_removed++;
							match.remove();
						}
						// offset_from_height
						if (scnd_xy_proj[0] > omp.filt_dist[1]) {
							sb.append(name+": filtered out by "+NAME_MO[scene_num]+
									" distance from the main scene peak parallel to the scene offset vector "+
									NAME_FH[hf]+ " = "+scnd_xy_proj[0]+" > "+omp.filt_dist[1]+
									" it could happen if the object is deep below average ground surface.");
							sb.append("\n");
							if (!match.isRemoved()) num_removed++;
							match.remove();
						}
						if (scnd_xy_proj[0] < (-omp.filt_dist[1]+offset_from_height)) {
							sb.append(name+": filtered out by "+NAME_MO[scene_num]+
									" distance from the main scene peak parallel to the scene offset vector "+
									NAME_FH[hf]+ " = "+scnd_xy_proj[0]+" < "+(-omp.filt_dist[1]+offset_from_height)+
									".\n This limit accounts for objects above ground up to "+omp.filt_height+" meters ("+
									offset_from_height+" pix.");
							sb.append("\n");
							if (!match.isRemoved()) num_removed++;
							match.remove();
						}
					}
					if (eff_rad > omp.filt_max_radius[scene_num][hf]) {
						sb.append(name+": filtered out by "+NAME_MO[scene_num]+" scene radius for "+
								NAME_FH[hf]+ " pattern: "+eff_rad+" > "+ omp.filt_max_radius[scene_num][hf]);
						sb.append("\n");
						if (!match.isRemoved()) num_removed++;
						match.remove();
					}
					if (elong > omp.filt_elongation[scene_num][hf]) {
						sb.append(name+": filtered out by "+NAME_MO[scene_num]+" scene peak cros-section elongation for "+
								NAME_FH[hf]+ " pattern: "+elong+" > "+ omp.filt_elongation[scene_num][hf]);
						sb.append("\n");
						if (!match.isRemoved()) num_removed++;
						match.remove();
					}
					if (is_partial && (omp.filt_other_frac != null) && (hf > 0) && (near_max_frac > omp.filt_other_frac[scene_num])) {
						sb.append(name+": filtered out by "+NAME_MO[scene_num]+
								" because it matches partial pattern ("+best_patt+") and combined full/partial correlation has a near peak "+
								near_max_frac+" > "+ omp.filt_other_frac[scene_num]+" of the used peak.");
						sb.append("\n");
						if (!match.isRemoved()) num_removed++;
						match.remove();
					}
					if (hf==0) {
						if ( strength_full_half_ratio[scene_num] < omp.filt_full_half_frac[scene_num]) {
							sb.append(name+": filtered out by "+NAME_MO[scene_num]+
									" because its correlation with the full patern strengh ratio to the partial pattern one = "+
									strength_full_half_ratio[scene_num]+" < "+ omp.filt_full_half_frac[scene_num]+" (minimum allowed).");
							sb.append("\n");
							if (!match.isRemoved()) num_removed++;
							match.remove();
						}
					}
				}				
			}
		}
		StringBuffer sb1 = new StringBuffer();
		if (! omp.filt_keep) {
			if (num_removed > 0) {
				for (int mn=match_sort.size()-1; mn >=0; mn--) {
					ItemMatch match = matches_list.get(match_sort.get(mn));
					if (match.isRemoved()) {
						match_sort.remove(mn);
					}
				}
				sb1.append("Filtered out "+num_removed+
						" scenes by the filter.\n");
			} else {
				sb1.append("No scenes were filtered out by the filter\n");
			}
			sb1.append(match_sort.size()+" candidate(s) remain\n");
		}
		if (debugLevel > 0) {
			System.out.println(sb.toString());
		}
		if (debugLevel > -3) {
			System.out.println(sb1.toString());
		}
		sb.append(sb1);
		if (omp.append_log && (omp.save_dir!= null)) {
			String log_path = omp.save_dir+basename+OrthoMapsParameters.LOG_SUFFIX;
			CalibrationFileManagement.saveStringToFile (
					log_path,      //String path,
					sb.toString(), // data,
					true);         // boolean append)
		}
		return num_removed;
	}

	/**
	 * Set absolute contrasts for each object, optionally changing sub-pattern index. Absolute contrast
	 * is a difference between average (w/o outliers) data value in zone1 (center area of the pattern) and
	 * the ring around it (zone2).
	 * On return the matches_list data is modified - absolute contrasts are set, and best_pattern may be
	 * updated. 
	 * Gets extracted_objects square arrays for each of the object, so the center of objects is in the
	 * center of the square from ItemMatch instance from the list. Calculated (for debug) masked image is 
	 * also saved there.
	 * 
	 * @param force_round overwrite mode with 4 - force pattern index to full (round) one
	 * @param mode Update of the best sub-pattern index:
	 *             0 - keep current
	 *             1 - keep type (full/half) and allow half direction to change by -1,0, or +1
	 *             2 - keep type (full/half)
	 *             3 - no restrictions - find the best sub-pattern index from scratch,
	 *             4 - force round (full pattern)
	 * @param neg_better    center lower (colder) than around
	 * @param outliers_frac fraction of outliers to remove while calculating averages
	 * @param obscure_warm allow obscurant (that makes half-pattern from a full one only to be warmer
	 *        than the object (defined below fraction between the object center region and peripheral
	 *        ring. For hot objects obscurant should be colder than such fraction.
	 * @param obscure_frac fraction between center average and peripheral ring to compare when obscure_warm
	 *        is set.
	 * @param gop  GroundObjectPattern instance used
	 * @param matches_list list of pattern matches
	 * @param match_sort   sorted list of indices in pattern matches list, so the first is the best match.
	 * @param scene_num - 0 for main, 1 - "other" 
	 * @param ipatterns integer square patterns with the same dimensions as the extracted_objects. Value
	 *        0 - do not use this pixel, 1 - Zone1 (inside the pattern), 2 - Zone2 - reference ring around
	 *        the pattern.
	 * @param debugLevel debug level. >0 - display images, >-4 - print.
	 * @return true
	 */
	public static boolean setAbsoluteContrasts(
			OrthoMapsParameters   omp,
			/*
			boolean               force_round,
			int                   mode, //  0 - keep, 1 keep type and if half +/-1, 2 - keep type, 3 - any
			boolean               neg_better, // more negative is difference - stronger result
			double                outliers_frac,
			boolean               obscure_warm, // =  true; // obscured can only be by warmer objects 
			double                obscure_frac, //  0.25; // obscured threshold between center and outer
			*/ 
			GroundObjectPattern   gop,
			ArrayList <ItemMatch> matches_list,
			ArrayList<Integer>    match_sort,
			int                   scene_num,
			int [][]              ipatterns,
			int                   debugLevel) {
		boolean   debug = debugLevel > 1; 
		int mode = omp.abs_mode;
		for (int mn=0; mn < match_sort.size(); mn++) {
			if (debugLevel > 1) {
				System.out.println("setAbsoluteContrasts(): Calculating absolute contrast for match #"+mn);
			}
			int indx =match_sort.get(mn);
			ItemMatch match = matches_list.get(indx);
			boolean [] try_sub = new boolean [ipatterns.length];
			int best_patt= match.getPatternMatch(gop).getBestSub() - 1; // 0-based
			int num_halves = ipatterns.length - 1; // == 8
			if (omp.abs_force_round) {
				mode = 4;
			}
			switch (mode) {
			case 0: try_sub[best_patt] = true; 	break; // only one
			case 1: // keep type and +-1 for direction
				if (best_patt == 0) {
					try_sub[0] = true;
				} else {
					for (int offs = -1; offs <= 1 ; offs++) {
						int i = best_patt+offs;
						if (i < 1) {
							i += num_halves;
						} else if (i > num_halves) {
							i -= num_halves;
						}
						try_sub[i] = true;
					}
				}
				break;
			case 2: // keep type
				if (best_patt == 0) {
					try_sub[0] = true;
				} else {
					for (int i = 1; i < try_sub.length; i++) {
						try_sub[i] = true;
					}
				}
				break;
			case 3: // any
				for (int i = 0; i < try_sub.length; i++) {
					try_sub[i] = true;
				}
				break;
			case 4: try_sub[0] = true; 	break; // only full pattern
			}
			double [][] dir_contrasts = new double [try_sub.length][];
			int best_index = -1;
			for (int i = 0; i < try_sub.length; i++) if ((i==0) || try_sub[i]) {
				dir_contrasts[i]= OrthoMap.getAbsoluteContrast(
						match.extracted_objects[scene_num], // extracted_objects[mn], // double [] data,
						ipatterns[i],                       // int    [] ipattern,
						omp.abs_outliers_frac,                      // double    outliers_frac,
						omp.abs_obscure_frac,                       // double    obscure_frac, //  0.25; // obscured threshold between center and outer 
						null,
						debug);                             // boolean   debug)				
				if (omp.abs_invert) {
					dir_contrasts[i][0] *= -1;
				}
				boolean bad_obscurant = omp.abs_obscure_warm && (dir_contrasts[i][1] < 0);
				if (try_sub[i] && (dir_contrasts[i][0] > 0)) {
					if (bad_obscurant) {
						if (debugLevel > -4) {
							System.out.println("setAbsoluteContrasts(): removing pattern_sub="+i
									+", match object="+mn+" because of the bad obscurant (cold for cold object)");
						}
					} else {
						best_index = i;
					}
				}
				
			}
			if (best_index < 0) {
				best_index = 0;
				
				if (debugLevel > -3) {
					System.out.println("setAbsoluteContrasts(): no candidates found (probably after removing bad obscurants) #"
							+mn+", giving a chance as full pattern. Was (1 for full):"+
							(best_patt+1)+", new is "+(best_index + 1));
				}
			}
			if (best_index != best_patt) {
				if (debugLevel > -3) {
					System.out.println("setAbsoluteContrasts(): updating best pattern index for match #"
							+mn+". Was (1 for full):"+(best_patt+1)+", new is "+(best_index + 1));
				}
				match.getPatternMatch(gop).setBestSub(best_index + 1);
			}
			match.setAbsoluteContrast(dir_contrasts[best_index][0]); // the higher the better
			if (debugLevel > -3) {
				System.out.println("setAbsoluteContrasts(): Set absolute contrast for match #"+mn+" = "+
						dir_contrasts[best_index][0]+
						", over threshold="+dir_contrasts[best_index][1]+".");
			}
			match.extracted_masked[scene_num] = new double [match.extracted_objects[scene_num].length];
			OrthoMap.getAbsoluteContrast( // calculate and set extracted_masked
					match.extracted_objects[scene_num], // double [] data,
					ipatterns[best_index],              // int    [] ipattern,
					omp.abs_outliers_frac,              // double    outliers_frac,
					omp.abs_obscure_frac,               // double    obscure_frac, //  0.25; // obscured threshold between center and outer 
					match.extracted_masked[scene_num],
					debug);                             // boolean   debug)				
		}		
		return true;
	}
	
	
	public String selectOneScene(
			int num_scene,
			int default_choice,
			int num_choice_lines) {
		String [] fs = {"first","second"};
		String [] lines = new String [ortho_maps.length];
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss.SS zzz");// may be VV instead of zzz
		for (int indx = 0; indx < ortho_maps.length; indx++) {
			OrthoMap map = ortho_maps[indx];
			LocalDateTime ldt = map.getLocalDateTime();
			ZonedDateTime zonedUTC = ldt.atZone(ZoneId.of("UTC")); // that time is UTC
			ZonedDateTime zonedDateTime = zonedUTC.withZoneSameInstant(ZoneId.of("Europe/Kyiv"));
			String sdt = zonedDateTime.format(formatter);
			String name = map.getName();
			double agl = map.getAGL();
			lines[indx]=String.format(
					"%3d %17s %26s %6.2f",indx, name, sdt, agl);
		}
		GenericJTabbedDialog gd = new GenericJTabbedDialog("Select the "+fs[num_scene]+" image from the list ",1200,100);
		gd.addChoice(fs[num_scene]+" image:",
				lines,
				lines[default_choice],
				"Select "+fs[num_scene]+" scene", num_choice_lines);
		gd.showDialog();
		if (gd.wasCanceled()) return null;
		int scene_number= gd.getNextChoiceIndex();
		return ortho_maps[scene_number].getName();
	}

	public String[] selectTwoScenes(
			int default_choice,
			int num_choice_lines) {
		String [] fs = {"first","second"};
		String [] lines = new String [ortho_maps.length];
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss.SS zzz");// may be VV instead of zzz
		for (int indx = 0; indx < ortho_maps.length; indx++) {
			OrthoMap map = ortho_maps[indx];
			LocalDateTime ldt = map.getLocalDateTime();
			ZonedDateTime zonedUTC = ldt.atZone(ZoneId.of("UTC")); // that time is UTC
			ZonedDateTime zonedDateTime = zonedUTC.withZoneSameInstant(ZoneId.of("Europe/Kyiv"));
			String sdt = zonedDateTime.format(formatter);
			String name = map.getName();
			double agl = map.getAGL();
			lines[indx]=String.format(
					"%3d %17s %26s %6.2f",indx, name, sdt, agl);
		}
		GenericJTabbedDialog gd = new GenericJTabbedDialog("Select two image scenes",1200,150);
		for (int num_scene = 0; num_scene < 2; num_scene++) {
			gd.addChoice(fs[num_scene]+" image:",
					lines,
					lines[default_choice],
					"Select "+fs[num_scene]+" scene", num_choice_lines);
		}
		gd.showDialog();
		if (gd.wasCanceled()) return null;
		String [] names = new String[2];
		for (int num_scene = 0; num_scene < 2; num_scene++) {
			int scene_number= gd.getNextChoiceIndex();
			names[num_scene] = ortho_maps[scene_number].getName();
		}		
		return names;
	}

	
	
	public String [] getScenesList() {
		String [] lines = new String [ortho_maps.length];
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss.SS zzz");// may be VV instead of zzz
		for (int indx = 0; indx < ortho_maps.length; indx++) {
			OrthoMap map = ortho_maps[indx];
			LocalDateTime ldt = map.getLocalDateTime();
			ZonedDateTime zonedUTC = ldt.atZone(ZoneId.of("UTC")); // that time is UTC
			ZonedDateTime zonedDateTime = zonedUTC.withZoneSameInstant(ZoneId.of("Europe/Kyiv"));
			String sdt = zonedDateTime.format(formatter);
			String name = map.getName();
			double agl = map.getAGL();
			lines[indx]=String.format(
					"%3d %17s %26s %6.2f",indx, name, sdt, agl);
		}
		return lines;
	}
	
	public int [] getScenesSelection(
			boolean [] pre_selects,
			String purpose) { // " to generate stats", " to generate images"
		String [] lines = getScenesList();
		int [] sel_50 = {125,126,127,129,135,136,137,138,139,140,141,144,145,146,147,148,155,156,157,158};
		int [] sel_75 = {170,177,178,183,187,188,189,192,193,199,200,204};
		int [] sel_100 = {207,208,212,213,214,215,218,220};
		int [] range_50_evn=  {  0, 48};
		int [] range_25_mor=  { 49,124};
		int [] range_50_mor=  {125,164};
		int [] range_75_mor=  {166,204};
		int [] range_100_mor= {205,221};
		int [][] ranges = {null,null,null,null,range_50_evn,range_25_mor,range_50_mor,range_75_mor,range_100_mor};
		
		boolean [] selection = new boolean [lines.length];
		if (pre_selects != null) {
			if (pre_selects[0]) {
				Arrays.fill(selection, true);
			}
			if (pre_selects[1]) {
				for (int i:sel_50) {
					selection[i] = true;
				}
			}
			if (pre_selects[2]) {
				for (int i:sel_75) {
					selection[i] = true;
				}
			}
			if (pre_selects[3]) {
				for (int i:sel_100) {
					selection[i] = true;
				}
			}
			for (int n = 4; n < 9; n++) if (pre_selects[n]){
				for (int i = ranges[n][0]; i<=ranges[n][1]; i++) {
					selection[i] = true;
				}
			}
		}
		pre_selects = new boolean[9];
		GenericJTabbedDialog gd = new GenericJTabbedDialog("Select scenes",1200,1000);
		gd.addCheckbox    ("Select all maps",         pre_selects[0], "Select all scenes, reopen this dialog.");
		gd.addCheckbox    ("Select  50m used maps",   pre_selects[1], "Select 50m scenes, reopen this dialog.");
		gd.addCheckbox    ("Select  75m used maps",   pre_selects[2], "Select 50m scenes, reopen this dialog.");
		gd.addCheckbox    ("Select 100m used maps",   pre_selects[3], "Select 50m scenes, reopen this dialog.");
		gd.addCheckbox    ("Select all 50m evening",  pre_selects[4], "Select all 50m evening scenes, reopen this dialog.");
		gd.addCheckbox    ("Select all 25m morning",  pre_selects[5], "Select all 25m morning scenes, reopen this dialog.");
		gd.addCheckbox    ("Select all 50m morning",  pre_selects[6], "Select all 50m morning scenes, reopen this dialog.");
		gd.addCheckbox    ("Select all 75m morning",  pre_selects[7], "Select all 75m morning scenes, reopen this dialog.");
		gd.addCheckbox    ("Select all 100m morning", pre_selects[8], "Select all 100m morning scenes, reopen this dialog.");
		for (int i = 0; i < lines.length; i++) {
			gd.addCheckbox    (lines[i],   selection[i], "Select scene number "+i);
		}
		gd.showDialog();
		if (gd.wasCanceled()) return null;
		boolean presel_any = false;
		for (int i = 0; i < pre_selects.length; i++) { 
			pre_selects[i]= gd.getNextBoolean();
			presel_any |= pre_selects[i]; 
		}
		if (presel_any) {
			return getScenesSelection(pre_selects, purpose);
		}
		int num_sel = 0;
		for (int i = 0; i < selection.length; i++) {
			selection[i] = gd.getNextBoolean();
			if (selection[i]) {
				num_sel++;
			}
		}
		int [] indices = new int [num_sel];
		int indx=0;
		for (int i = 0; i < selection.length; i++) if (selection[i]) {
			indices[indx++] = i;
		}		
		return indices;
	}
	
	public boolean getIntersectedPairs(
			CLTParameters    clt_parameters,
			String orthoMapsCollection_path) {
		
		int [] indices = getScenesSelection(
				null, // boolean select_all,
				" to find intersects"); // String purpose)
		if (indices == null) {
			return false;
		}
		int     zoom_lev =     clt_parameters.imp.pwise_zoom; // -5;
		double  min_overlap_frac =  clt_parameters.imp.pwise_overlap; // 0.25;
		boolean bounds_to_indices = true;
		int debugLevel = clt_parameters.imp.pwise_debug;
		GenericJTabbedDialog gd = new GenericJTabbedDialog("Get scene intersections",1200,300);
		gd.addNumericField("Zoom level",                 zoom_lev,  0,4,"",
				"Zoom level: +1 - zoom in twice, -1 - zoom out twice");
		gd.addNumericField("Minimal overlap",                min_overlap_frac,    3,7,"",
				"Minimal overlap as a fraction of the smaller image.");
		gd.addNumericField("Pairwise match debug level",  debugLevel, 0,3,"","Debug level during Spiral search.");

		gd.showDialog();
		if (gd.wasCanceled()) return false;
		zoom_lev =     (int) gd.getNextNumber();
		min_overlap_frac =   gd.getNextNumber();
		debugLevel=    (int) gd.getNextNumber();
		int [] wh =     new int[2];
		int [] origin = new int[2];
		double [][] centers =new double [indices.length][];			
		double [][] dmulti = renderMultiDouble (
				null, // double [][]   ground_planes, // null - images, non-null altitudes. use new double[2][3] for old way alt
				indices,    // int []        indices, // null or which indices to use (normally just 2 for pairwise comparison)
				bounds_to_indices, // boolean       bounds_to_indices,
				null, // affines,    // double [][][] affines, // null or [indices.length][2][3]
				null, // warp,       // FineXYCorr    warp,,
				zoom_lev, // int         zoom_level,
				wh,         // int []      wh,
				origin,     // int []      origin){ // maps[0] as a reference
				centers);   // double [][] centers)
		
		double [] overlaps = getFracOverlaps(
				dmulti); // final double [][] dmulti) {
		int num_overlaps_all = 0, num_overlaps_sel = 0;
		boolean [][] intersects = new boolean [dmulti.length][dmulti.length];
		for (int i = 0; i < overlaps.length; i++) if (overlaps[i] > 0){
			num_overlaps_all++;
			if (overlaps[i] >= min_overlap_frac) {
				num_overlaps_sel++;
				int row = i / dmulti.length;
				int col = i % dmulti.length;
				intersects[row][col] = true;
			}
		}
		int [] groups = new int [dmulti.length];
		for (int i = 0; i < groups.length; i++) {
			groups[i] = i;
		}
		for (int i = 0; i< (groups.length-1); i++) {
			for (int j = i+1; j < groups.length; j++) if (intersects[i][j] && (groups[j] != groups[i])){
				int g0 =  groups[i];
				int g1 =  groups[j];
				for (int k = 0; k < groups.length; k++) {
					if (groups[k] == g1) {
						groups[k] = g0;
					}
				}
			}
		}
		HashSet<Integer> hs = new HashSet<Integer>();
		for (int i:groups) {
			hs.add(i);
		}
		System.out.println("getIntersectedPairs(): num_overlaps_all="+num_overlaps_all+
				", num_overlaps_sel="+num_overlaps_sel+", number of disconnected groups="+hs.size());
		if (debugLevel > -4) {
			ShowDoubleFloatArrays.showArrays( 
					overlaps,
					"Overlaps");
		}
		
	boolean matches_ok= generatePairwiseMatches(
			clt_parameters,            // CLTParameters clt_parameters,
			min_overlap_frac,          // double  min_overlap_frac
			indices,                   // int []        indices,
			overlaps,                  // double [] overlaps,
//			intersects,                // boolean [][]  boverlaps)
			orthoMapsCollection_path); // String orthoMapsCollection_path
		return matches_ok;
	}
	
	public boolean generatePairwiseMatches(
			CLTParameters clt_parameters,
			double        min_overlap_frac,
			int []        indices,
			double []     overlaps,			
//			boolean [][]  boverlaps,
			String        orthoMapsCollection_path) {
// gen number of pairs:
		int num_scenes = indices.length;
		int num_pairs = 0;
		int num_new = 0;
		for (int i = 0; i < indices.length-1; i++) {
			for (int j = i+1; j < indices.length; j++) if (overlaps[i * indices.length +j] >= min_overlap_frac){
				num_pairs++;
				int [] ipair = {indices[i], indices[j]};
				if ((ortho_maps[ipair[0]].getMatch(ortho_maps[ipair[1]].getName()) == null) &&
						(ortho_maps[ipair[1]].getMatch(ortho_maps[ipair[0]].getName()) == null)){
					num_new++;
				}
			}
		}
		
		boolean      skip_exist    = clt_parameters.imp.pwise_skip_exist;   // 
		boolean      refine_exist  = clt_parameters.imp.pwise_refine_exist; // if false, start from scratch, true - start from previous
		boolean      delete_failed = clt_parameters.imp.pwise_delete_fail;  // delete existing match if now failed
		boolean      gen_inverse   = clt_parameters.imp.pwise_gen_inverse;  // generate inverse matches
		boolean      save_each     = clt_parameters.imp.pwise_save_each;    // save state file after each match
		boolean      log_append    = clt_parameters.imp.pwise_log_append;   //
		String       log_path      = clt_parameters.imp.pwise_log_path;     // 
		int          debugLevel    = clt_parameters.imp.pwise_debug;        //
		
		//Initial spiral search for image matching
		double  search_step =         clt_parameters.imp.ospir_step;     // 8.0; // pix
		double  search_range =        clt_parameters.imp.ospir_range;    // 50.0; // pix
		double  double_threshold =    clt_parameters.imp.ospir_double;   //
		double  good_rms =            clt_parameters.imp.ospir_good_rms; // 0.27; //
		double  max_rms =             clt_parameters.imp.ospir_max_rms;  // 0.35; //
		int     min_overlap =         clt_parameters.imp.ospir_overlap;  // 3000; // do not try to match if there is too small overlap (scaled pixels)
		int     num_iter_lma =        clt_parameters.imp.ospir_num_iter; // 5;
		double [] max_rms_iter =      clt_parameters.imp.ospir_rms_iter; // {1.0, 0.6};//
		boolean spiral_ignore_rms =   clt_parameters.imp.ospir_ignore_rms; // false
		int     spiral_debug =        clt_parameters.imp.ospir_debug;    // 0;
		
		//Final pairwise scenes matching
		double  frac_remove  =        clt_parameters.imp.pmtch_frac_remove;// 0.15;
		double  metric_err =          clt_parameters.imp.pmtch_metric_err;// 0.05; // 0.02;//  2 cm
		boolean pmtch_use_affine =    clt_parameters.imp.pmtch_use_affine;		
		double  max_std =             clt_parameters.imp.pmtch_max_std;// 1.5;      // maximal standard deviation to limit center area  
		double  min_std_rad =         clt_parameters.imp.pmtch_min_std_rad;// 2.0;  // minimal radius of the central area (if less - fail)
		boolean ignore_prev_rms =     clt_parameters.imp.pmtch_ignore_rms;// true; 
		int     num_tries =           clt_parameters.imp.pmtch_num_iter;// 10;
		
		double rad_fraction =        clt_parameters.imp.pmtch_cent_rad; // center circle radius fraction of 0.5* min(width, height) in tiles
		double max_tile_rad =        clt_parameters.imp.pmtch_max_cent_rad;// maximal center radius in tiles (limit pmtch_cent_rad)
		double fill_fraction =       clt_parameters.imp.pmtch_cent_fill; // should be populated not less than this
		double fill_fraction_final = clt_parameters.imp.pmtch_cent_final; // should be populated not less than this during final pass
		double ease_nosfm =          clt_parameters.imp.pmtch_ease_nosfm; // ease metric_error when no SfM gain == 0;
		
		GenericJTabbedDialog gd = new GenericJTabbedDialog("Pairwise Match Parameters",1200,1000);
		gd.addMessage("Number of scenes - "+num_scenes+
				", number of pairs (w/o inverse) - "+num_pairs+
				", number of new pairs - "+num_new);
	    gd.addCheckbox    ("Skip existing",               skip_exist, "Do not regenerate if match with same or higher resolution exists.");
	    gd.addCheckbox    ("Refine existing",             refine_exist, "Refine existing matches (false - start from scratch with spiral search).");
		gd.addCheckbox    ("Delete failed",               delete_failed, "Delete previous matches if it failed now.");
	    gd.addCheckbox    ("Generate inverse matches",    gen_inverse, "Generate (refine if exist and enabled) inverse matches.");
	    
	    gd.addCheckbox    ("Save state after each match", save_each, "Update state file after each match generation to mitigate possible crashes.");
	    gd.addCheckbox    ("Write log file",              log_append, "Enable writing log file with matching results.");
	    gd.addStringField ("Log file full path",          log_path, 150, "Path of the log file to be appended.");	
		gd.addNumericField("Pairwise match debug level",  debugLevel, 0,3,"","Debug level during Spiral search.");
		
		gd.addMessage  ("Initial spiral search for image matching");
		gd.addNumericField("Spiral search step",          search_step,  3,7,"scaled pix",	"Distance between spiral search probes, in scaled pixels.");
		gd.addNumericField("Spiral search radius",        search_range, 3,7,"scaled pix",	"Maximal radius of the spiral search, in scaled pixels.");
		gd.addNumericField("Mitigate small overlap",      double_threshold, 3,7,"","For small overlaps increase zoom by 1 and range - twice.");
		gd.addNumericField("RMSE to end search",          good_rms,     3,7,"scaled pix",	"Maximal RMSE to consider match, in scaled pixels.");
		gd.addNumericField("Satisfactory RMSE",           max_rms,      3,7,"scaled pix",	"Maximal RMSE to consider match, in scaled pixels.");
		gd.addNumericField("Minimal overlap",             min_overlap,  0,4,"scaled pix ^ 2","Minimal overlap area in square scaled pixels.");
		gd.addNumericField("LMA iterations",              num_iter_lma, 0,2,"",              "Number of LMA iterations during spiral search.");
		gd.addNumericField("RMSE at first iteration",     max_rms_iter[0],  3,7,"scaled pix","Maximal RMSE at first iteration.");
		gd.addNumericField("RMSE at second iteration",    max_rms_iter[1],  3,7,"scaled pix","Maximal RMSE at second iteration.");
		
		gd.addCheckbox    ("Ignore worsening RMSE",       spiral_ignore_rms, "Ignore worsening/not improving RMSE during spiral search.");
		gd.addNumericField("Spiral search debug level",   spiral_debug, 0,3,"","Debug level during Spiral search.");

		gd.addMessage  ("Final pairwise scenes matching");
		gd.addNumericField("Remove fraction of worst matches", frac_remove,  3,7,"",	"When fitting scenes remove this fraction of worst match tiles.");
		gd.addNumericField("Maximal metric error",             metric_err,  3,7,"m",	"Maximal tolerable fitting error caused by elevation variations.");
		gd.addCheckbox    ("Use scenes' affine",               pmtch_use_affine, "Use known scenes' affine matrices, false - start from scratch (unity) ones.");
		gd.addNumericField("Central area standard deviation",  max_std,  3,7,"",	"Central area limit by the standard deviation.");
		gd.addNumericField("Central area minimal radius",      min_std_rad,  3,7,"tile",	"Minimal radius of the central area after all LMA passes.");
		gd.addCheckbox    ("Ignore previous RMSE",             ignore_prev_rms, "Do not exit full fitting cycles if the RMSE worsened/not improved.");
		gd.addNumericField("Number of fitting iterations",     num_tries, 0,3,"","number of full fittng iterations.");
		gd.addNumericField("Central area radius as fraction",  rad_fraction,   3,7,"",	"Central area radius as fraction of half minimal WOI dimension.");
		gd.addNumericField("Maximal central area radius",      max_tile_rad,   3,7,"tiles",	"Absolute limit to the center area radius (eases bad peripheral matching).");
		gd.addNumericField("Central area minimal fill",        fill_fraction,  3,7,"",	"Central area minimal fill for all but the last iteration.");
		gd.addNumericField("Central area minimal fill final",  fill_fraction_final, 3,7,"",	"Central area minimal fill for the last iteration.");
		gd.addNumericField("Relax metric error for no-SfM",    ease_nosfm, 3,7,"",	"Relax metric error for no-SfM scenes (sfm_gain==0).");
		
		
		gd.showDialog();
		if (gd.wasCanceled()) return false;
		skip_exist       = gd.getNextBoolean();
		refine_exist     = gd.getNextBoolean();
		delete_failed    = gd.getNextBoolean();
		gen_inverse      = gd.getNextBoolean();
		save_each        = gd.getNextBoolean();
		log_append       = gd.getNextBoolean();
		log_path         = gd.getNextString();
		debugLevel = (int) gd.getNextNumber(); 	
		
		search_step      =       gd.getNextNumber();        
		search_range     =       gd.getNextNumber();
		double_threshold =       gd.getNextNumber();
		good_rms         =       gd.getNextNumber();
		max_rms          =       gd.getNextNumber();
		min_overlap      = (int) gd.getNextNumber();
		num_iter_lma     = (int) gd.getNextNumber();
		max_rms_iter[0]  =       gd.getNextNumber();
		max_rms_iter[1]  =       gd.getNextNumber();
		spiral_ignore_rms=       gd.getNextBoolean();
		spiral_debug     = (int) gd.getNextNumber();
		                         
		frac_remove      =       gd.getNextNumber();
		metric_err       =       gd.getNextNumber();
		pmtch_use_affine=        gd.getNextBoolean();
		max_std          =       gd.getNextNumber();
		min_std_rad      =       gd.getNextNumber();
		ignore_prev_rms  =       gd.getNextBoolean();
		num_tries        = (int) gd.getNextNumber();		

		rad_fraction =           gd.getNextNumber();
		max_tile_rad =           gd.getNextNumber();		
		fill_fraction =          gd.getNextNumber();  
		fill_fraction_final=     gd.getNextNumber();   
		ease_nosfm =             gd.getNextNumber();
		
		return generatePairwiseMatches(
				clt_parameters,           // CLTParameters clt_parameters,
				min_overlap_frac,          // double  min_overlap_frac,
				indices,                  // int []       indices,
				overlaps,                 // double [] overlaps,
				skip_exist,               // boolean      skip_existing,
				refine_exist,             // boolean      refine_existing, // if false, start from scratch, true - start from previous
				delete_failed,            // boolean       delete_failed,   // if false, start from scratch, true - start from previous
				gen_inverse,              // boolean      gen_inverse,    // generate inverse matches
				save_each,                // boolean      save_each,       // save state file after each match
				log_append,               // boolean      log_append,      //
				log_path,                 // String       log_path,
				orthoMapsCollection_path, // String orthoMapsCollection_path
				search_step,              // double        search_step,
				search_range,             // double        search_range,
				good_rms,                 // double        good_rms,
				max_rms,                  // double        max_rms,
				min_overlap,              // int           min_overlap,
				num_iter_lma,             // int           num_iter_lma,
				spiral_ignore_rms,        // boolean       ignore_rms,
				spiral_debug,             // int           spiral_debug,
				frac_remove,              // double        frac_remove,
				metric_err,               // double        metric_error,
				pmtch_use_affine,         // boolean       pmtch_use_affine,				
				max_std,                  // double        max_std,  
				min_std_rad,              // double        min_std_rad,
				ignore_prev_rms,          // boolean       ignore_prev_rms, 
				num_tries,                // int           num_tries,
				rad_fraction,             // double        rad_fraction,
				max_tile_rad,             // double        max_tile_rad,
				fill_fraction,            // double        fill_fraction,
				fill_fraction_final,      // double        fill_fraction_final,
				ease_nosfm,               // double        ease_nosfm,
				double_threshold,         // double        double_threshold,
				max_rms_iter,             // double []     max_rms_iter,
				debugLevel);              // int          debugLevel
	}
	
	public boolean generatePairwiseMatches(
			CLTParameters clt_parameters,
			double        min_overlap_frac,
			int []        indices,
			double []     overlaps,
			boolean       skip_existing,
			boolean       refine_existing, // if false, start from scratch, true - start from previous
			boolean       delete_failed,   // if false, start from scratch, true - start from previous
			boolean       gen_inverse,     // generate inverse matches
			boolean       save_each,       // save state file after each match
			boolean       log_append,      //
			String        log_path,
			String        orthoMapsCollection_path,
			
			double        search_step,
			double        search_range_in,
			double        good_rms,
			double        max_rms,
			int           min_overlap,
			int           num_iter_lma,
			boolean       ignore_rms,
			int           spiral_debug,
			
			double        frac_remove,
			double        metric_error,
			boolean       pmtch_use_affine,			
			double        max_std,  
			double        min_std_rad,
			boolean       ignore_prev_rms, 
			int           num_tries,
			double        rad_fraction,
			double        max_tile_rad,
			double        fill_fraction,
			double        fill_fraction_final,
			double        ease_nosfm,
			double        double_threshold,
			double []     max_rms_iter,
			int           debugLevel) {
		
		boolean batch_mode = true;
		int num_scenes = indices.length;
		int num_pairs = 0;
		int num_new = 0;
		
		ArrayList<Point> pairs = new ArrayList<Point>();
		for (int i = 0; i < indices.length-1; i++) {
			for (int j = i+1; j < indices.length; j++) if (overlaps[i * indices.length +j] >= min_overlap_frac){
				pairs.add(new Point(i,j));
				if (gen_inverse) {
					pairs.add(new Point(j,i));
				}
				num_pairs++;
				int [] ipair = {indices[i], indices[j]};
				if ((ortho_maps[ipair[0]].getMatch(ortho_maps[ipair[1]].getName()) == null) &&
						(ortho_maps[ipair[1]].getMatch(ortho_maps[ipair[0]].getName()) == null)){
					num_new++;
				}
			}
		}
		
		if (log_append && (log_path != null)) { // assuming directory exists
			StringBuffer sb = new StringBuffer();
			sb.append(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(Calendar.getInstance().getTime())+"\n");
			sb.append("num_scenes\t"+  num_scenes+"\n");
			sb.append("pairs.size()\t"+pairs.size()+"\n");
			sb.append("num_pairs\t"+   num_pairs+"\n");
			sb.append("num_new\t"+     num_new+"\n");
			sb.append(String.format("%4s\t%4s\t%17s\t%17s\t%6s\t%3s\t%4s,\t%4s\t%6s\t%6s\t%7s\n",
					"scn1","scn2","timestamp1","timestamp2","ovrlp","zl","nx","ny","RMS-sp","RMSfin","fzl","removed"));
			CalibrationFileManagement.saveStringToFile (
					log_path,          //String path,
					sb.toString(), // data,
					true); // boolean append)
			if (debugLevel>-3) {
				System.out.print(sb.toString());
			}
		}
		ArrayList<Point> failed_pairs = new ArrayList<Point>();
		ArrayList<Point> new_pairs =    new ArrayList<Point>(); // started from spiral, not from the same or inverse
		for (Point pair:pairs) {
			int [] ipair = {indices[pair.x], indices[pair.y]};
			boolean direct = indices[pair.x] < indices[pair.y];
	    	int min_zoom_lev = ortho_maps[ipair[0]].getOriginalZoomLevel();
	    	int max_zoom_lev = ortho_maps[ipair[0]].getOriginalZoomLevel();
	    	double max_agl =   ortho_maps[ipair[0]].getAGL();
	    	for (int i = 0; i < ipair.length; i++) {
	    		max_agl =      Math.max(max_agl, ortho_maps[ipair[i]].getAGL());	    		
	    		min_zoom_lev = Math.min(min_zoom_lev, ortho_maps[ipair[i]].getOriginalZoomLevel());
	    		max_zoom_lev = Math.max(max_zoom_lev, ortho_maps[ipair[i]].getOriginalZoomLevel());
	    	}
        	double agl_ratio = max_agl/50.0;
        	double metric_error_adj = metric_error * agl_ratio * agl_ratio; // metric_error settings is good for 50m. Increase for higher Maybe squared?
	    	int initial_zoom = max_zoom_lev - 4;  // another algorithm?
	    	// overlaps
	    	int overlap_indx = direct ? (pair.x * indices.length + pair.y) : (pair.y * indices.length + pair.x);
	    	double overlap_frac = overlaps[overlap_indx];
	    	double search_range = search_range_in;
	    	while (overlap_frac < double_threshold) {
	    		overlap_frac *= 2;
	    		initial_zoom += 1;
	    		search_range *= 2;
	    	}
	    	
			PairwiseOrthoMatch direct_match = ortho_maps[ipair[0]].getMatch(ortho_maps[ipair[1]].getName());
			PairwiseOrthoMatch inv_match =  ortho_maps[ipair[1]].getMatch(ortho_maps[ipair[0]].getName());
			boolean use_direct =  false;
			boolean use_inverse = false;
			boolean skip =        false;
			if (!direct && (inv_match != null)) { // prefer inverse
				use_inverse = true;
			} else if (direct_match != null) {
				if (skip_existing) {
					skip = true;
				} else if (refine_existing) {
					use_direct = true;
				} // else will start from scratch
			} else if (inv_match != null) {
				if (direct && refine_existing) {
					use_inverse = true;
				}
			}
			if (skip) {
				if (log_append && (log_path != null)) { // assuming directory exists
					StringBuffer sb = new StringBuffer();
					sb.append(String.format("%4d\t%4d\t%s\t%s\t%6.4f\t%3d\tSKIP\t\t\t%6.4f\t%3d\n",
							ipair[0], ipair[1],
							ortho_maps[ipair[0]].getName(), ortho_maps[ipair[1]].getName(),
							overlaps[overlap_indx],  initial_zoom,
							direct_match.rms,
							direct_match.zoom_lev));
					CalibrationFileManagement.saveStringToFile (
							log_path,          //String path,
							sb.toString(), // data,
							true); // boolean append)
				}
				continue;
			}
			PairwiseOrthoMatch pairwiseOrthoMatch = null;
			
			// unityAffine()
			
			double [][] affine0 = pmtch_use_affine?ortho_maps[ipair[0]].getAffine():unityAffine(); // {{1,0,0},{0,1,0}}; // will always stay the same
			double [][] affine1 = pmtch_use_affine?ortho_maps[ipair[0]].getAffine():unityAffine(); // {{1,0,0},{0,1,0}}; // here (manual mode) start from the center, may use prediction in auto
			double [][][] affines = new double[][][] {affine0,affine1};
			double spiral_rms = Double.NaN;
			if (use_direct) {
				pairwiseOrthoMatch=direct_match;
			} else if (use_inverse) {
				double [] enuOffset = ortho_maps[ipair[0]].enuOffsetTo(ortho_maps[ipair[1]]);
				double [] rd = {enuOffset[0], -enuOffset[1]}; // {right,down} of the image 
				// create inverted pairwiseOrthoMatch
				pairwiseOrthoMatch = inv_match.getInverse(rd);
			} else { // run spiral search
				// now always unity, but after partial adjustment may be non-unity
				// TODO: Not yet tested with intermediate adjustment.
				pairwiseOrthoMatch = SpiralMatch (
						clt_parameters,  // CLTParameters    clt_parameters,
						frac_remove,     // double           frac_remove, //  =        0.25
						metric_error_adj,// double           metric_error,
						pmtch_use_affine, // boolean          pmtch_use_affine,
						max_std,         // double           max_std,     // maximal standard deviation to limit center area  
						min_std_rad,     // double           min_std_rad, // minimal radius of the central area (if less - fail)
						rad_fraction,    // double           rad_fraction,
						max_tile_rad,    // double           max_tile_rad, //  = 30;						
						fill_fraction,   // double           fill_fraction,
						fill_fraction_final, // double           fill_fraction_final,
						ease_nosfm,      // double           ease_nosfm,
						ipair,           // int []           gpu_pair,
						affines,         // double [][][]    affines_init,  // here in meters, relative to vertical points
						initial_zoom,    // int              zoom_lev,
						search_step,     // double           pix_step,
						search_range,    // double           pix_range,
						good_rms,        // double           good_rms,
						max_rms,         // 			double           max_rms,
						num_iter_lma,    // int              num_tries, //  = 5
						min_overlap,     // int              min_overlap, // 3000
						ignore_rms,      // boolean          ignore_rms,
						max_rms_iter,    // double []        max_rms_iter, //  = {1.0, 0.6};//
						overlaps[overlap_indx], // double           overlap,
						spiral_debug);   // int              debugLevel)
				if ((pairwiseOrthoMatch == null) || Double.isNaN(pairwiseOrthoMatch.rms)) {
					if (delete_failed) {
						ortho_maps[ipair[0]].unsetMatch(ortho_maps[ipair[1]].getName());
					}
					String str_failed = delete_failed?"%4d\t%4d\t%s\t%s\t%6.4f\t%3d\tFAILED\tREMOVED\n":"%4d\t%4d\t%s\t%s\t%6.4f\t%3d\tFAILED\n";
					if (log_append && (log_path != null)) { // assuming directory exists
						StringBuffer sb = new StringBuffer();
						sb.append(String.format(str_failed,
								ipair[0], ipair[1], ortho_maps[ipair[0]].getName(), ortho_maps[ipair[1]].getName(),
								overlaps[overlap_indx],  initial_zoom));
						CalibrationFileManagement.saveStringToFile (
								log_path,          //String path,
								sb.toString(), // data,
								true); // boolean append)
					}
					failed_pairs.add(pair);
					continue;
				} else {
					spiral_rms = pairwiseOrthoMatch.rms;
					new_pairs.add(pair); // adjusted with spiral
				}
			}
			// refine match with high resolution
			affines[1] = pairwiseOrthoMatch.getAffine();
			affines[1][0] = affines[1][0].clone(); 
			affines[1][1] = affines[1][1].clone();
			Rectangle woi = new Rectangle(); // used to return actual woi from correlateOrthoPair()			
			correlateOrthoPair(
					clt_parameters,  // CLTParameters    clt_parameters,
					pairwiseOrthoMatch, //PairwiseOrthoMatch pairwiseOrthoMatch, // will return statistics
					0,                // 			int              min_overlap,
					max_std,         // double           max_std,      // maximal standard deviation to limit center area  
					min_std_rad,     // double           min_std_rad,  // minimal radius of the central area (if less - fail)
					frac_remove,     // double           frac_remove, //  =        0.25
					metric_error_adj,// 			double           metric_error,
					ignore_prev_rms, // boolean ignore_prev_rms,
					num_tries,       //  = 5int              num_tries, //  = 5
					false, // ,            // boolean          calc_warp, (will return null if false)
					batch_mode,      // boolean          batch_mode,
					ipair,           // String []        gpu_spair,
					affines,         // double [][][]    affines, // on top of GPS offsets
					woi,             // Rectangle        woi,
					min_zoom_lev,    // int              zoom_lev,
					false, // show_vf,         // boolean show_vf,
					null, // ground_planes,   // double [][]      ground_planes, // null or double[2] - will return ground planes
					rad_fraction,    // double           rad_fraction,
					max_tile_rad,    // double           max_tile_rad, //  = 30;
					fill_fraction,   // double           fill_fraction,
					fill_fraction_final, // double           fill_fraction_final,
					ease_nosfm,      // double           ease_nosfm,
					null,            //  double []        max_rms_iter, //  = {1.0, 0.6};//					
					debugLevel-4);   // final int        debugLevel)
			boolean failed_refine = Double.isNaN(pairwiseOrthoMatch.rms);
			// Create log line and write it
			StringBuffer sb = new StringBuffer();
			sb.append(String.format("%4d\t%4d\t%s\t%s\t%6.4f\t%3d",
					ipair[0], ipair[1], ortho_maps[ipair[0]].getName(), ortho_maps[ipair[1]].getName(),
					overlaps[overlap_indx],  initial_zoom));

			if (use_direct || use_inverse) {
				sb.append("\t\t\t"); 
			} else {
				sb.append(String.format("\t%3d\t%3d\t%6.4f",
						pairwiseOrthoMatch.nxy[0],pairwiseOrthoMatch.nxy[1],spiral_rms)); 
			}
			if (failed_refine) {
				if (delete_failed) {
					ortho_maps[ipair[0]].unsetMatch(ortho_maps[ipair[1]].getName());
				}
				String str_failed = delete_failed?"\tFAILED\t\tREMOVED\n":"\tFAILED\n";

				failed_pairs.add(pair);
				sb.append(String.format(str_failed));
				if (debugLevel > -4) System.out.print("Final adjustment"+str_failed);				
			} else {
				pairwiseOrthoMatch.overlap = overlaps[overlap_indx]; // needed here if refining old/manual w/o overlap 
				sb.append(String.format("\t%6.4f\t%3d\n",pairwiseOrthoMatch.rms,pairwiseOrthoMatch.zoom_lev)); 
				if (debugLevel > -4) System.out.println("Final adjustment RMSE="+pairwiseOrthoMatch.rms+
						", overlap = "+pairwiseOrthoMatch.overlap);				
				ortho_maps[ipair[0]].setMatch(ortho_maps[ipair[1]].getName(),pairwiseOrthoMatch);
				if (save_each && (orthoMapsCollection_path != null)) {
		        	try {
		        		writeOrthoMapsCollection(orthoMapsCollection_path);
		        	} catch (IOException e) {
		        		// TODO Auto-generated catch block
		        		e.printStackTrace();
		        	}
		        	if (debugLevel > -4) {
		        		System.out.println("Saved data to "+ orthoMapsCollection_path);
		        	}
				}
			}
			if (log_append && (log_path != null)) { // assuming directory exists
				CalibrationFileManagement.saveStringToFile (
						log_path,          //String path,
						sb.toString(), // data,
						true); // boolean append)
			}			
			// save if save_each
			// does it have direct or inverted pair
		}
		if (log_append && (log_path != null)) { // assuming directory exists
			StringBuffer sb = new StringBuffer();
			sb.append("\nSUMMARY\n");
			sb.append("good\t"+(pairs.size()-failed_pairs.size())+"\n");
			sb.append("new\t"+ new_pairs.size()+"\n");
			sb.append("failed\t"+failed_pairs.size()+"\n");
			if (!failed_pairs.isEmpty()) {
				for (int i = 0; i < failed_pairs.size(); i++) {
					int [] ipair = {indices[failed_pairs.get(i).x], indices[failed_pairs.get(i).y]};
					sb.append(String.format("%4d\t%4d\t%s\t%s\n",
							ipair[0], ipair[1], ortho_maps[ipair[0]].getName(), ortho_maps[ipair[1]].getName()));
				}
			}
			CalibrationFileManagement.saveStringToFile (
					log_path,          //String path,
					sb.toString(), // data,
					true); // boolean append)
        	if (debugLevel > -4) {
        		System.out.println("Appended log file "+ log_path);
        	}
		}
		if (orthoMapsCollection_path != null) {
        	try {
        		writeOrthoMapsCollection(orthoMapsCollection_path);
        	} catch (IOException e) {
        		// TODO Auto-generated catch block
        		e.printStackTrace();
        	}
        	if (debugLevel > -4) {
        		System.out.println("Saved data to "+ orthoMapsCollection_path);
        	}
		}
		return true;
	}
	
	
	public boolean processComboMap(
			int debugLevel) {
		int [] indices = getScenesSelection(
				null, // boolean select_all,
				" to process"); // String purpose)
		if (indices == null) {
			return false;
		}
		boolean more = true;
		while (more) { // generate multiple images/stats for the same selection
			more = processComboSelection(indices, debugLevel);
		}
		return true;
	}
	public boolean processComboSelection(
			int [] indices,
			int debugLevel) {
		boolean show_map_stats =      false;      
		boolean show_combo_map =      false; // true;
		boolean show_alt_map =        false;
		boolean show_combo_mask =     false; // generate image mas (where it is defined)
		boolean show_frames =         false;
		int     frame_shrink =        4; 
		int     frame_grow =          4;
		double  frame_blur =          1.0;
		double  frame_cut_frac =      0.01;
		int     zoom_lev =           -2; // 0; // +1 - zoom in twice, -1 - zoom out twice
		int     margins =             10;
		boolean show_centers =        true;
		boolean bounds_to_indices =   true;
		boolean merge_layers =        false;  // instead of individuals

		String        save_top_dir =      "/media/elphel/NVME/lwir16-proc/ortho_videos/debug/sept12-13/pattern_match/";
		String        sub_dir =           "combo_maps";
		boolean       show_images =       true;
		boolean       save_images =       true;
		
		GenericJTabbedDialog gd = new GenericJTabbedDialog("Combo map/stats generation",1200,1000);
		gd.addCheckbox    ("Show statistics for ortho images", show_map_stats,  "Generate and show statistics for ortho maps.");
		gd.addCheckbox    ("Show image map",             show_combo_map,  "Generate composite map of images.");
		gd.addCheckbox    ("Show altitudes",             show_alt_map,    "Generate composite process altitude maps.");
		gd.addCheckbox    ("Show image masks",           show_combo_mask, "Generate composite binary image.");
		gd.addCheckbox    ("Show image frames",          show_frames,     "Generate image borders.");
		gd.addNumericField("Shrink inner frames",        frame_shrink,  0,4,"pix","Shrink inner of the frames from the actual image borders.");
		gd.addNumericField("Grow outer frames",          frame_grow,    0,4,"pix","Grow outer of the frames from the actual image borders.");
		gd.addNumericField("Blur frames",                frame_blur,    3,7,"pix","Blur frames for anti-aliasing.");
		gd.addNumericField("Cut threshold",              frame_cut_frac, 3,7,"","After blurring, replace lower values by NaN.");
 		
		gd.addNumericField("Zoom level",                 zoom_lev,  0,4,"",
				"Zoom level: +1 - zoom in twice, -1 - zoom out twice");
		gd.addNumericField("Margins",                    margins,  0,4,"",
				"Add margins around images");

  		gd.addCheckbox    ("Show transformation centers",      show_centers, "Mark verticals from the UAS on the ground.");
		gd.addCheckbox    ("Bounds to selected images", bounds_to_indices, "Set combo image bounds to selected images only. False - all images.");
		gd.addCheckbox    ("Merge layers",                     merge_layers, "Generate composite binary image.");

		gd.addStringField ("Pattern match save directory",      save_top_dir, 120, "Top directory to save combo maps");
		gd.addStringField ("Save subdirectory",                 sub_dir, 80, "Subdirectory for versions of the same scene/pair of scenes");
		gd.addCheckbox    ("Show generated images",             show_images, "Display generated images.");
		gd.addCheckbox    ("Save generated images",             save_images, "Save generated image to the location defined by above..");
		
		gd.showDialog();
		if (gd.wasCanceled()) return false;
		show_map_stats =     gd.getNextBoolean();
		show_combo_map =     gd.getNextBoolean();
		show_alt_map =       gd.getNextBoolean();
		show_combo_mask =    gd.getNextBoolean();
		
		show_frames =        gd.getNextBoolean(); 
		frame_shrink = (int) gd.getNextNumber();
		frame_grow =   (int) gd.getNextNumber();
		frame_blur =         gd.getNextNumber();
		frame_cut_frac =     gd.getNextNumber();
		zoom_lev =     (int) gd.getNextNumber();
		margins =      (int) gd.getNextNumber();
		show_centers =       gd.getNextBoolean();
		bounds_to_indices =  gd.getNextBoolean();
		merge_layers =       gd.getNextBoolean();

		save_top_dir=        gd.getNextString();
		sub_dir=             gd.getNextString();
		show_images=         gd.getNextBoolean();
		save_images=         gd.getNextBoolean();
		
		String save_dir = null;
		if (save_images) {
			if (!save_top_dir.endsWith(Prefs.getFileSeparator())) {
				save_top_dir+= Prefs.getFileSeparator();
			}
			if (indices.length == 1) {
				save_dir=String.format("%s%03d-%s",
						save_top_dir,indices[0],ortho_maps[indices[0]].getName());
			} else if (indices.length == 2) {
				save_dir=String.format("%s%03d-%03d_%s-%s",
						save_top_dir,indices[0],indices[1],ortho_maps[indices[0]].getName(),ortho_maps[indices[1]].getName());
			}
			save_dir=save_top_dir;

			if (!sub_dir.equals("")) {
				save_dir+= Prefs.getFileSeparator()+sub_dir;
			}
			// create if it does not exist
			File  fsave_dir = new File(save_dir);
			fsave_dir.mkdirs();
			save_dir += Prefs.getFileSeparator();
		}
		
		if (show_map_stats) {
			String time_zone_name = "Europe/Kyiv";
			String [] stats = getScenesStats(indices, time_zone_name); // int [] indices)
			TextWindow tw = new TextWindow("Ortho_images_stats", stats[0], stats[1], 1250,1000);
			if (save_images && (save_dir != null)) {
				String path = save_dir+"ortho_images_stats.csv";
				tw.getTextPanel().saveAs(path);
				if (debugLevel > -4) {
					System.out.println("Saved stats to "+path);
				}
			}
		}
		
		double pix_m = OrthoMap.getPixelSizeMeters (zoom_lev);
		if (show_combo_map) {
			int [] wh =     new int[2];
			int [] origin = new int[2];
			double [][] centers = show_centers? (new double [indices.length][]): null;
			double [][] dmulti = renderMultiDouble (
					null, // double [][]   ground_planes, // null - images, non-null altitudes. use new double[2][3] for old way alt
					indices,    // int []        indices, // null or which indices to use (normally just 2 for pairwise comparison)
					bounds_to_indices, // boolean       bounds_to_indices,
					null, // affines,    // double [][][] affines, // null or [indices.length][2][3]
					null, // warp,       // FineXYCorr    warp,,
					zoom_lev, // int         zoom_level,
					wh,         // int []      wh,
					origin,     // int []      origin){ // maps[0] as a reference
					centers);   // double [][] centers)
			if (margins > 0) {
				addMargins(
						dmulti,   // double [][] dmulti,
						wh,       // int width,
						margins); // int margins)
			}
			showSaveMap(
					indices, // int []      indices,
					dmulti,  // double [][] dmulti,
					wh[0],   // int         width,
					
					centers, // int    [][] centers,
					merge_layers, // boolean     merge_layers,
					false,   // boolean     use_max,
					show_images, // boolean     show,
					save_images, // boolean     save,
					save_dir, // String      save_dir,
					"_"+indices.length+"_MAP-"+pix_m, // String      suffix
					debugLevel); // int         debugLevel)			
		}
		if (show_alt_map) {
			int [] wh =     new int[2];
			int [] origin = new int[2];
			double [][] centers = show_centers? (new double [indices.length][]): null;
			double [][] dmulti = renderMultiDouble (
					new double [indices.length][] , // double [][]   ground_planes, // null - images, non-null altitudes. use new double[2][3] for old way alt
					indices,                        // int []        indices, // null or which indices to use (normally just 2 for pairwise comparison)
					bounds_to_indices,              // boolean       bounds_to_indices,
					null, // affines,    // double [][][] affines, // null or [indices.length][2][3]
					null, // warp,       // FineXYCorr    warp,,
					zoom_lev, // int         zoom_level,
					wh,         // int []      wh,
					origin,     // int []      origin){ // maps[0] as a reference
					centers);   // double [][] centers)
			if (margins > 0) {
				addMargins(
						dmulti,   // double [][] dmulti,
						wh,       // int width,
						margins); // int margins)
			}
			showSaveMap(
					indices, // int []      indices,
					dmulti,  // double [][] dmulti,
					wh[0],   // int         width,
					centers, // int    [][] centers,
					merge_layers, // boolean     merge_layers,
					false,   // boolean     use_max,
					show_images, // boolean     show,
					save_images, // boolean     save,
					save_dir, // String      save_dir,
					"_"+indices.length+"_ALT-"+pix_m, // String      suffix
					debugLevel); // int         debugLevel)			
		}
		if (show_combo_mask) {
			int [] wh =     new int[2];
			int [] origin = new int[2];
			double [][] centers = show_centers? (new double [indices.length][]): null;
			double [][] dmulti = renderMultiDouble (
					null, // double [][]   ground_planes, // null - images, non-null altitudes. use new double[2][3] for old way alt
					indices,    // int []        indices, // null or which indices to use (normally just 2 for pairwise comparison)
					bounds_to_indices, // boolean       bounds_to_indices,
					null, // affines,    // double [][][] affines, // null or [indices.length][2][3]
					null, // warp,       // FineXYCorr    warp,,
					zoom_lev, // int         zoom_level,
					wh,         // int []      wh,
					origin,     // int []      origin){ // maps[0] as a reference
					centers);   // double [][] centers)
			if (margins > 0) {
				addMargins(
						dmulti,   // double [][] dmulti,
						wh,       // int width,
						margins); // int margins)
			}
			for (int n = 0; n < dmulti.length; n++) {
				for (int i = 0; i < dmulti[n].length; i++) {
					dmulti[n][i] = Double.isNaN(dmulti[n][i])? 0:1;
				}
			}
			showSaveMap(
					indices, // int []      indices,
					dmulti,  // double [][] dmulti,
					wh[0],   // int         width,
					centers, // int    [][] centers,
					merge_layers, // boolean     merge_layers,
					true,    // boolean     use_max,
					show_images, // boolean     show,
					save_images, // boolean     save,
					save_dir, // String      save_dir,
					"_"+indices.length+"_MASK-"+pix_m, // String      suffix
					debugLevel); // int         debugLevel)			
		}

		if (show_frames) {
			int [] wh =     new int[2];
			int [] origin = new int[2];
			double [][] centers = show_centers? (new double [indices.length][]): null;
			double [][] dmulti = renderMultiDouble (
					null, // double [][]   ground_planes, // null - images, non-null altitudes. use new double[2][3] for old way alt
					indices,    // int []        indices, // null or which indices to use (normally just 2 for pairwise comparison)
					bounds_to_indices, // boolean       bounds_to_indices,
					null, // affines,    // double [][][] affines, // null or [indices.length][2][3]
					null, // warp,       // FineXYCorr    warp,,
					zoom_lev, // int         zoom_level,
					wh,         // int []      wh,
					origin,     // int []      origin){ // maps[0] as a reference
					centers);   // double [][] centers)
			if (margins > 0) {
				addMargins(
						dmulti,   // double [][] dmulti,
						wh,       // int width,
						margins); // int margins)
			}
			double [][] dframes = new double [dmulti.length][];
			for (int n = 0; n < dmulti.length; n++) {
				dframes[n] = createBorderByNaN(
						dmulti[n],       // double [] data,
						wh[0],           // int width,
						frame_shrink,    // int     frame_shrink, 
						frame_grow,      // int     frame_grow,
						frame_blur,      // double  frame_blur,
						frame_cut_frac); // double  cut_frac)
				
//				for (int i = 0; i < dmulti[n].length; i++) {
//					dmulti[n][i] = Double.isNaN(dmulti[n][i])? 0:1;
//				}
			}
			showSaveMap(
					indices, // int []      indices,
					dframes,  // double [][] dmulti,
					wh[0],   // int         width,
					centers, // int    [][] centers,
					merge_layers, // boolean     merge_layers,
					true,   // boolean     use_max,
					show_images, // boolean     show,
					save_images, // boolean     save,
					save_dir, // String      save_dir,
					"_"+indices.length+"_FRAME-"+pix_m, // String      suffix
					debugLevel); // int         debugLevel)			
		}
		
		/*
		show_frames =        gd.getNextBoolean(); 
		frame_shrink = (int) gd.getNextNumber();
		frame_grow =   (int) gd.getNextNumber();
		frame_blur =         gd.getNextNumber();
 
		 */
		//showScenesStats
		//show_combo_mask
		return true;
	}
	public static void addMargins(
			double [][] dmulti,
			int [] wh,
			int margins){
		int width = wh[0];
		int height = dmulti[0].length/width;
		int widthm = width+2*margins;
		int heightm = height + 2* margins;
		int indx0m = (widthm + 1) * margins;
		for (int n = 0; n < dmulti.length; n++) {
			double [] d = new double[widthm*heightm];
			Arrays.fill(d, Double.NaN);
			for (int row = 0; row < height; row++) {
				System.arraycopy(
						dmulti[n],
						row*width,
						d,
						indx0m + widthm * row,
						width);
			}
			dmulti[n] = d;
		}
		wh[0] = widthm;
		wh[1] = heightm;
	}
	
	public static double [] createBorderByNaN(
			double [] data,
			int width,
			int     frame_shrink, 
			int     frame_grow,
			double  frame_blur,
			double  cut_frac) {
		double [] dframe =new double [data.length];
		int height = data.length/width;
		final TileNeibs tnSurface = new TileNeibs(width, height);
        boolean [] mask_in = new boolean[data.length];
        for (int i = 0; i < data.length; i++) {
        	mask_in[i] = !Double.isNaN(data[i]);
        }
		boolean [] mask_out = mask_in.clone();
		tnSurface.shrinkSelection(
				frame_shrink, // shrink,
				mask_in,      // tiles,
				null);        // prohibit);
		tnSurface.growSelection(
				frame_grow,   // grow,
				mask_out,     // tiles,
				null);        // prohibit);
		if (frame_blur > 0) {
			for (int i = 0; i < dframe.length; i++) {
				dframe[i] = (mask_out[i] && !mask_in[i]) ? 1.0 : 0.0;
			}
//			DoubleGaussianBlur gb = new DoubleGaussianBlur();
			(new DoubleGaussianBlur()).blurDouble(
					dframe,
					width,
					height,
					frame_blur,
					frame_blur,
					0.01);
			double max = 0.0;
			for (int i = 0; i < dframe.length; i++) {
				max = Math.max(max, dframe[i]);
			}
			double threshold = max*cut_frac;
			for (int i = 0; i < dframe.length; i++) {
				if (dframe[i] < threshold) {
					dframe[i] = Double.NaN;
				}
			}
		} else {
			for (int i = 0; i < dframe.length; i++) {
				dframe[i] = (mask_out[i] && !mask_in[i]) ? 1.0 : Double.NaN;
			}
		}

		
		return dframe;
		
	}
	
	public void showSaveMap(
			int []      indices,
			double [][] dmulti,
			int         width,
			double [][] centers,
			boolean     merge_layers,
			boolean     use_max,
			boolean     show,
			boolean     save,
			String      save_dir,
			String      suffix, // "_"+indices.length+"_MAP"
			int         debugLevel) {
		boolean     show_centers = centers != null;
		if (merge_layers) {
			dmulti = new double [][] {mergeLayers(dmulti,use_max)};
		}
		String [] titles = new String[dmulti.length];
		for (int i = 0; i < titles.length; i++) {
			titles[i] = String.format("%03d-%s", indices[i], ortho_maps[indices[i]].getName());
		}
		ImagePlus imp = ShowDoubleFloatArrays.makeArrays(
				dmulti,
				width,
				dmulti[0].length/width,
				ortho_maps[indices[0]].getName()+(merge_layers? "_MERGED":"")+suffix+".tiff",
				titles);
        if (show_centers) {
        	PointRoi roi = new PointRoi();
        	for (int i = 0; i < centers.length; i++) {
        		roi.addPoint(centers[i][0],centers[i][1], 1+(merge_layers?0:i));
        	}
        	roi.setOptions("label");
        	imp.setRoi(roi);
        }
		showSaveImagePlus(
				imp,        // ImagePlus imp,
				show, // boolean   show,
				save, // boolean   save,
				save_dir, // String    save_dir,
				debugLevel); // int       debugLevel)
		
	}
	
	
	public static double [] mergeLayers(
			double [][] layers,
			boolean use_max) { // false - use last
		if ((layers == null) || (layers.length == 0)){
			return null;
		}
		double [] merged = layers[0].clone();
		for (int n = 1; n < layers.length; n++) {
			if (use_max) {
				for (int i = 0; i < merged.length; i++) if (!Double.isNaN(layers[n][i])) {
					if (Double.isNaN(merged[i])) {
						merged[i] = layers[n][i];
					} else {
						merged[i] = Math.max(layers[n][i],merged[i]);
					}
				}

			} else {
				for (int i = 0; i < merged.length; i++) if (!Double.isNaN(layers[n][i])) {
					merged[i] = layers[n][i];
				}
			}
		}
		return merged;
	}
	
	
	
	
	public boolean showScenesStats() {
		String time_zone_name = "Europe/Kyiv";
		int [] indices = getScenesSelection(
				null, // boolean select_all,
				" to generate stats"); // String purpose)
		if (indices == null) {
			return false;
		}
		String [] stats = getScenesStats(indices, time_zone_name); // int [] indices)
		new TextWindow("Ortho_images_stats", stats[0], stats[1], 1250,1000);
		return true;
	}
	
	public String[] getScenesStats(int [] indices, String time_zone_name) {
		if (indices == null) {
			indices = new int [ortho_maps.length];
			for (int i = 0; i < indices.length; i++) {
				indices[i] = i;
			}
		}
		String [] stats = new String[2]; // header, body
		StringBuffer sb = new StringBuffer();
//		String head_fmt = "%3s\t%17s\t%26s\t%13s\t%13s\t%7s\t%6s\t%7s\t%6s\t%6s\t%6s\t%6s\n";
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy hh:mm:ss.SS zzz");// may be VV instead of zzz
		//
		stats [0] = String.format(
				"%3s\t%17s\t%26s\t%11s\t%11s\t%7s\t"+
						"%6s\t%7s\t%6s\t%6s\t%6s\t%6s\t"+
						"%6s\t%6s",
						"#", "timestamp", "date/time", "latitude", "longitude", "GND-ASL",
						"AGL","pix(cm)", "width", "height","vert_x","vert_y",
						"scenes", "sfm");
		for (int indx:indices) {
			OrthoMap map = ortho_maps[indx];
			LocalDateTime ldt = map.getLocalDateTime();
			ZonedDateTime zonedUTC = ldt.atZone(ZoneId.of("UTC")); // that time is UTC
			ZonedDateTime zonedDateTime = zonedUTC.withZoneSameInstant(ZoneId.of(time_zone_name));
			String sdt = zonedDateTime.format(formatter);
			String name = map.getName();
			double [] lla = map.getLLA();
			double agl = map.getAGL();
			double pix_size_cm =  100*map.getOrigPixMeters();
			int width = map.getWidth();
			int height = map.getHeight();
			int [] vert_pix = map.getVertPixels();
			double sfm_gain = map.getSfmGain();
			int scenes = map.getNumberScenes();
			sb.append(String.format(
					"%3d\t%17s\t%26s\t%11.6f\t%11.6f\t%7.2f\t"+
							"%6.2f\t%7.4f\t%6d\t%6d\t%6d\t%6d\t"+
							"%6d\t%6.2f\n",
							indx, name, sdt, lla[0], lla[1], lla[2] - agl,//  ground level
							agl,pix_size_cm, width, height,vert_pix[0],vert_pix[1],
							scenes, sfm_gain));
		}
		stats[1] = sb.toString();
		return stats;
	}
	public static void showSaveImagePlus(
			ImagePlus imp,
			boolean   show,
			boolean   save,
			String    save_dir,
			int       debugLevel) {
		if (save && (save_dir != null)) {
			if (!save_dir.endsWith(Prefs.getFileSeparator())) {
				save_dir += Prefs.getFileSeparator();
			}
			String save_path = save_dir+imp.getTitle();
			if (!save_path.endsWith(".tiff")) {
				save_path+=".tiff";
			}
			FileSaver imp_fs = new FileSaver(imp);
			imp_fs.saveAsTiff(save_path);
			if (debugLevel > -4) {
				System.out.println("Saved "+save_path);	
			}
		}
		if (show) {
			imp.show();
		}
	}
	
	public static double [] getFracOverlaps(
			final double [][] dmulti) {
		final double [] overlaps = new double [dmulti.length*dmulti.length];

		final Thread[] threads = ImageDtt.newThreadArray();
		final AtomicInteger ai = new AtomicInteger(0);
		for (int ithread = 0; ithread < threads.length; ithread++) {
			threads[ithread] = new Thread() {
				public void run() {
					for (int nscene = ai.getAndIncrement(); nscene < (dmulti.length-1); nscene = ai.getAndIncrement()) {
						double [] data0 = dmulti[nscene];
						int num0 = 0;
						for (int i = 0; i < data0.length; i++) if (!Double.isNaN(data0[i])) {
							num0++;
						}

						for (int nscene1 = nscene+1; nscene1 < dmulti.length; nscene1++) {
							double [] data1 = dmulti[nscene1];
							int num1 = 0, num_intersect=0; 
							for (int i = 0; i < data0.length; i++) if (!Double.isNaN(data1[i])) {
								num1++;
								if (!Double.isNaN(data0[i])) {
									num_intersect++;
								}
							}
							overlaps[nscene * dmulti.length  + nscene1] = (1.0 * num_intersect)/ Math.min(num0, num1);
						}
					}
				}
			};
		}		      
		ImageDtt.startAndJoin(threads);
		return overlaps;
	}
}
