package com.elphel.imagej.vegetation;

import java.awt.Rectangle;
import java.io.File;
import java.io.FileFilter;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.concurrent.atomic.AtomicInteger;

import com.elphel.imagej.common.DoubleGaussianBlur;
import com.elphel.imagej.common.ShowDoubleFloatArrays;
import com.elphel.imagej.jp4.JP46_Reader_camera;
import com.elphel.imagej.tileprocessor.ImageDtt;
import com.elphel.imagej.tileprocessor.QuadCLT;
import com.elphel.imagej.tileprocessor.TileNeibs;

import Jama.Matrix;
import ij.ImagePlus;
import ij.ImageStack;
import ij.Prefs;
import ij.io.FileSaver;

public class VegetationLMA {
	public static final int TVAO_TERRAIN =          0;
	public static final int TVAO_VEGETATION =       1;
	public static final int TVAO_VEGETATION_ALPHA = 2;
	public static final int TVAO_SCENE_OFFSET =     3;
	public static final String PAR_EXT =            ".par-tiff";

	public final Rectangle    full;
	public final int          image_length;
	public final int          num_scenes;
	public Rectangle          woi =             null;
	public double []          vegetation_average; // full image average vegetation (with nulls)
	public double []          terrain_average;    // full image average terrain (no nulls)
	public double [][]        terrain_rendered;   // terrain rendered for scenes (has nulls)
	public double [][][]      vegetation_offsets; // [num_scenes][pixle]{dx,dy} differential offsets of vegetation to terrain grid
	public boolean            diff_offsets;
	// next 3 arrays are full [image_width*image_height], but will be modified only inside woi
	public double [][]        tvao;            // [0][image_length] - terrain, [1][image_length] - vegetation,

	public VegetationModel vegetationModel = null;
	/*
	// copy from Vegetation model
	public String []     scene_names =               null; // TODO: Implement!
	public String        model_directory;
	public String        model_top_directory;
	public String        reference_scene; // timestamp
	public int           reference_index; // timestamp
	*/
	
	
	
	
//[2][image_length] - alpha, [3][num_scenes] - per-scene offset (mostly for vignetting. MAYBE add per-scene pair of tilts
	public int [][]           par_index;       // indices -  [0..3][full_pixel] - same as for tvao, value - parameter index
	public int [][]           par_rindex;      // parameter -> pair of type (0..3) and full window pixel index
	private int               num_pars_terrain; 
	private int               num_pars_vegetation; 
	private int               num_pars_vegetation_alpha; //== num_pars_vegetation
	private int               ind_pars_terrain =   0;    // index of the first terrain parameter
	private int               ind_pars_vegetation;       // index of the first vegetation parameter
	private int               ind_pars_vegetation_alpha; // index of the first alpha parameter
	private int               ind_pars_scenes;           // index of the first scene parameter
	private int               num_pars_scenes;
	private int [][][]        data_source; // [index][windx][3]:
	                                       // 0:{ scene, full_indx, terr_indx, scale_indx} - always present as parameters
	                                       // 1: [4]/null Z-shape 4 corners vegetation, either >= as parameter index or -1 - (x + image_width*y) of the unmodified full index
	                                       // 2: [4]/null Z-shape 4 corners vegetation alpha, either >= as parameter index or -1 - (x + image_width*y) of the unmodified full index
	private double [][]       corners_weights; // matches data_source, non-nulls specify Z-shape 4 corners of vegetation/alpha weights
	
//	private int               num_used_scenes; // -> num_par_scenes
	private boolean []        used_scenes;    // encode unused as NaN
//	private int []            used_scenes_indices;
	
	
	
	private double []         parameters_vector = null;
	private double []         y_vector = null;
	private double            weight_pure =     0;
	private double []         weights; // normalized so sum is 1.0 for all - samples and extra regularization
	public double             alpha_loss =      0; // not used with cosine alpha
	public double             alpha_offset =    0; // if >0,  start losses above 0.0 and below 1.0;
	public double             alpha_lpf =       0;
	public boolean            alpha_piece_linear = true;
	public double             alpha_scale_avg = 1.0; // 1.1; // scale average alpha (around 0.5) when pulling to it
	public double             alpha_push =      12.0;   // push from alpha==0.5
	public double             alpha_push_neutral = 0.8; // alpha point from which push (closer to opaque)	
	public double             alpha_push_center = 1.5; // weight of center alpha pixel relative to each of the 4 ortho ones
	public double             alpha_mm_hole    = 0.1; // NaN to disable. Local "almost minimum" (lower than this fraction between min and max neighbor) is not subject to alpha_lpf
	public boolean            alpha_en_holes   = true; // Search for small semi-transparent holes, disable diffusion of local alpha minimums
	public double             alpha_diff_hole = 0.01; // minimal alpha difference between min and max neighbor to be considered a hole

	
	public boolean            from_file =       false;

	
	public double             terr_lpf =        0;
	public double             veget_lpf =       0;
	
	// when unsharp mask is applied , pulling to 0 (when alpha is 0 (for vegetation) or 1.0 (for terrain) makes sense
	public double             terr_pull0 =      0;
	public double             veget_pull0 =     0;
	
	
	public double             boost_parallax =  1;
	public double             um_sigma =        0;       // just use in debug image names
	public double             um_weight =       0;
	
	// data used to calculate lpf pull of the alpha pixel to average of four neighbors. Below similar (weaker pull) for terrain and vegetation
	// to smooth areas where there is no data from available images. 
	public int [][]           alpha_neibs;   // corresponds to parameters for alpha (num_pars_vegetation_alpha), each has 4 ortho neibs, -1 - border, >= 0
	// - parameter index, <=-2 -2-full_pixel_index
	public int [][]           terr_neibs;    // corresponds to parameters for terrain, for smoothing undefined terrain, similar to alpha_neibs
	public int [][]           veget_neibs;   // corresponds to parameters for vegetation, for smoothing undefined vegetation, similar to alpha_neibs
	
	
	public double             delta=            1e-5; // 7;
	
	public int                debug_index;
	public double [][]        debug_image;
	public static double []   debug_alpha_scales = {0,50}; // {100,150};
	public String             debug_path = "/media/elphel/SSD3-4GB/lwir16-proc/berdich3/debug/vegetation/lma_um/";
	
	private double []         last_rms =        null; // {rms, rms_pure}, matching this.vector
	private double []         good_or_bad_rms = null; // just for diagnostics, to read last (failed) rms
	private double []         initial_rms =     null; // {rms, rms_pure}, first-calcualted rms
	private double []         last_ymfx =       null;
	private double [][]       last_jt =         null;
	
	public boolean            debug_deriv = true;
	public int                debug_width =     12;
	public int                debug_decimals =  9;

	public VegetationLMA (
			int                width,
			boolean            diff_mode,
//			int height,
//			int scenes,
			double []          terrain_average,    // full image average terrain (no nulls!)
			double []          vegetation_average, // full image average vegetation (with nulls)
			double [][]        terrain_rendered,   // terrain rendered for scenes (has nulls)
			double [][][]      vegetation_offsets  // [num_scenes][pixel]{dx,dy} differential offsets of vegetation to terrain grid
			  ) {
		full = new Rectangle(0,0,width, terrain_average.length/width);
		image_length =  terrain_average.length;
		diff_offsets = diff_mode;
		num_scenes =   vegetation_offsets.length;
		this.vegetation_average = vegetation_average;
		this.terrain_average =    terrain_average;   
		this.terrain_rendered =   terrain_rendered;  
		this.vegetation_offsets = vegetation_offsets;
		tvao = new double[4][];
		tvao[TVAO_TERRAIN] =          this.terrain_average.clone();
		tvao[TVAO_VEGETATION] =       this.vegetation_average.clone();
		tvao[TVAO_VEGETATION_ALPHA] = new double [image_length]; // 0 - use terrain
		tvao[TVAO_SCENE_OFFSET] =     new double [num_scenes];
	}
	
	public VegetationLMA (VegetationModel vegetationModel) {
		full = vegetationModel.full;
		image_length = full.width * full.height;
		num_scenes = vegetationModel.terrain_scenes_render.length;
		vegetation_average =vegetationModel.vegetation_average_render;
		terrain_average =   vegetationModel.terrain_average_render;
		terrain_rendered =   vegetationModel.terrain_scenes_render;  
		vegetation_offsets =vegetationModel.vegetation_warp;
		tvao = new double[4][];
		tvao[TVAO_TERRAIN] =          this.terrain_average.clone();
		tvao[TVAO_VEGETATION] =       this.vegetation_average.clone();
		tvao[TVAO_VEGETATION_ALPHA] = new double [image_length]; // 0 - use terrain
		tvao[TVAO_SCENE_OFFSET] =     new double [num_scenes];
		diff_offsets = vegetationModel.diff_mode;
		this.vegetationModel =        vegetationModel; // to access scene names, directories, reference index
		return;
	}
	
	
	public int prepareLMA(
			final boolean            keep_parameters,
			final Rectangle          woi,
			final int                min_scenes,     // minimal number of scenes (inside woi) vegetation pixel must influence
			final int                min_total_scenes,
			final int                min_samples_scene, // 10
			final int                min_valid_pixels,
			final double             default_alpha,
			final double             reg_weights,    // fraction of the total weight used for regularization
			final double             alpha_loss,     // quadratic loss when alpha reaches -1.0 or 2.0 
			final double             alpha_offset,   // quadratic loss when alpha reaches -1.0 or 2.0
			final double             alpha_lpf,      // pull vegetation alpha to average of 4 neighbors
			final boolean            alpha_piece_linear, // true - piece-linear, false - half-cosine
			final double             alpha_scale_avg, //  = 1.2; // scale average alpha (around 0.5) when pulling to it
			final double             alpha_push,      //  5.0;   // push from alpha==0.5
			final double             alpha_push_neutral, //  = 0.8; // alpha point from which push (closer to opaque)
			final double             alpha_push_center,// 1.5; // weight of center alpha pixel relative to each of the 4 ortho ones
			final boolean            alpha_en_holes, // Search for small semi-transparent holes, disable diffusion of local alpha minimums
			final double             alpha_mm_hole, //     = 0.1; // NaN to disable. Local "almost minimum" (lower than this fraction between min and max neighbor) is not subject to alpha_lpf			
			final double             terr_lpf,       // pull terrain to average of 4 neighbors (very small)
			final double             veget_lpf,      // pull vegetation to average of 4 neighbors (very small - maybe not needed)
			final double             terr_pull0,     // pull terrain to zero (makes sense with UM
			final double             veget_pull0,    // pull vegetation to zero (makes sense with UM
			final double             boost_parallax, // increase weight of scene with maximal parallax relative to the reference scene
			final double             max_parallax,  // do not consider maximal parallax above this (consider it a glitch) 
			final double             um_sigma,       // just use in debug image names
			final double             um_weight,
			String                   parameters_read_path, 
			final int                debugLevel) {
		this.woi =               woi;
		this.alpha_loss =        alpha_loss;
		this.alpha_offset =      alpha_offset;
		this.alpha_lpf =         alpha_lpf;
		this.alpha_scale_avg =   alpha_scale_avg;
		this.alpha_piece_linear= alpha_piece_linear;
		this.alpha_push =        alpha_push; //    5.0;   // push from alpha==0.5
		this.alpha_push_neutral = alpha_push_neutral; // 0.8; // alpha point from which push (closer to opaque)
		this.alpha_push_center = alpha_push_center; // 1.5; // weight of center alpha pixel relative to each of the 4 ortho ones
		this.alpha_en_holes =    alpha_en_holes;
		this.alpha_mm_hole =     alpha_mm_hole;
		this.terr_lpf =          terr_lpf;
		this.veget_lpf =         veget_lpf;
		this.terr_pull0 =        terr_pull0;
		this.veget_pull0 =       veget_pull0;
		this.boost_parallax =    boost_parallax;
		this.um_sigma =          um_sigma;      // just use in debug image names
		this.um_weight =         um_weight;
		
		if ("RESTORE".equals(parameters_read_path)) {
			from_file = false;
			String  restore_dir =    debug_path;
			String  debug_title = getParametersDebugTitle();
			if (!restore_dir.endsWith(Prefs.getFileSeparator())) {
				restore_dir += Prefs.getFileSeparator();
			}
			
			String restore_path=restore_dir + debug_title+"-live.tiff";
			parameters_read_path = restore_path;
		}
		
		
		final double []     scene_weights = setupSceneWeights(
				boost_parallax, // double boost_parallax)
				woi,            // Rectangle woi)
				max_parallax); // final double ceil_parallax)

		int     min_scenes_uses = min_scenes;
		int     min_scenes_used = min_scenes * 4;
		boolean [][] valid_woi = filterValidWoi (
				min_scenes_uses, // int     min_scenes_uses,
				min_scenes_used, // int     min_scenes_used,
				min_valid_pixels, // int     min_valid_pixels,
				debugLevel > 3); // boolean debug_img){ // 4x?
		if (valid_woi == null) {
			System.out.println("Too few valid pixels (< min_valid_pixels="+min_valid_pixels+").");
			return -1;
		}
		
		setupParametersIndices(valid_woi); // needs to know number of used scenes // all but scenes
		
//		alpha_neibs = getAlphaNeighbors();
		alpha_neibs = getNeighbors(
				TVAO_VEGETATION_ALPHA,      // final int tvao,        // TVAO_VEGETATION_ALPHA
				ind_pars_vegetation_alpha,  // final int ind_samples, // ind_pars_vegetation_alpha
				num_pars_vegetation_alpha); // final int num_samples);  // num_pars_vegetation_alpha
		terr_neibs = getNeighbors(
				TVAO_TERRAIN,               // final int tvao,        // TVAO_VEGETATION_ALPHA
				ind_pars_terrain,           // final int ind_samples, // ind_pars_vegetation_alpha
				num_pars_terrain);          // final int num_samples);  // num_pars_vegetation_alpha
		veget_neibs = getNeighbors(
				TVAO_VEGETATION,            // final int tvao,        // TVAO_VEGETATION_ALPHA
				ind_pars_vegetation,        // final int ind_samples, // ind_pars_vegetation_alpha
				num_pars_vegetation);       // final int num_samples);  // num_pars_vegetation_alpha
		
		setupDataSource(
				min_samples_scene); // int min_samples_scene); // condensed
		finishParametersIndices(); // scene-related parameters
		
		if (num_pars_scenes < min_total_scenes) {
			System.out.println("Too few valid scenes="+num_pars_scenes+" < min_total_scenes="+min_total_scenes+").");
			return -1;
		}
		
		
		if (!keep_parameters) {
			setupParametersVector (default_alpha); // areas where both terrain and vegetation are available
		}
		from_file = false;
		if (parameters_read_path != null) {
			 readParametersFromImage(
					 parameters_read_path, // String    path,
						parameters_vector, // double [] vector,
						1) ;// int       gap)
		}
		
//		setupDataSource(
//				min_samples_scene); // int min_samples_scene); // condensed
		setupYVector();
		setupWeights( // after setupParametersIndices
				scene_weights,
				reg_weights);   // final double reg_weights);
		last_jt = new double [parameters_vector.length][];
		return weights.length;
	}
	
	public double [] getParametersVector() {
		return parameters_vector;
	}
	
	private double [] setupSceneWeights(
			final double    boost_parallax,
			final Rectangle woi,
			final double    ceil_parallax) {
		// find max parallax
		final double ceil_parallax2 = ceil_parallax * ceil_parallax;
		final double [] scene_weights = new double[vegetation_offsets.length];
		if (boost_parallax == 0) {
			for (int nscene = 0; nscene < vegetation_offsets.length; nscene++) {
				scene_weights[nscene] = 1.0;
			}
		} else {
			final double [] max_parallax2 = new double [vegetation_offsets.length];
			double super_max_parallax2 = 0;
			for (int nscene = 0; nscene < vegetation_offsets.length; nscene++) {
				// only compare for selected window!
				for (int drow = 0; drow < woi.height; drow++) {
					int row = woi.y + drow;
					for (int dcol = 0; dcol < woi.width; dcol++) {
						int col = woi.x + dcol;	
//						int windx =dcol + drow * woi.width;
						int indx = row*full.width + col;
						double [] d =  vegetation_offsets[nscene][indx];
						if (d != null) {
							double d2 = Math.min(ceil_parallax2, d[0]*d[0] + d[1]*d[1]);
							/*
						if (d2 > max_parallax2[nscene]) {
							System.out.println("setupSceneWeights(): nscene="+nscene+", pix =  ("+(i%640)+","+(i/640)+") ="+i+" d2="+d2);
						}
							 */
							max_parallax2[nscene] = Math.max(max_parallax2[nscene], d2);
						}
					}
				}
				super_max_parallax2 = Math.max(super_max_parallax2, max_parallax2[nscene]);
			}
			for (int nscene = 0; nscene < vegetation_offsets.length; nscene++) {
				scene_weights[nscene] = 1.0+(boost_parallax - 1) * Math.sqrt(max_parallax2[nscene]/super_max_parallax2);
			}
		}
		return scene_weights;
	}
	
	private double [] getYminusFxWeighted(
			final double []   fx,
			final double []   rms_fp // null or [2]
			) {
		final Thread[]      threads =     ImageDtt.newThreadArray();
		final AtomicInteger ai =          new AtomicInteger(0);
		final AtomicInteger ati =         new AtomicInteger(0);
		final double []     wymfw =       new double [fx.length];
		double [] swd2 = new double[threads.length];
		for (int ithread = 0; ithread < threads.length; ithread++) { // first sum for pairs
			threads[ithread] = new Thread() {
				public void run() {
					int nthread =  ati.getAndIncrement();
					for (int n = ai.getAndIncrement(); n < y_vector.length; n = ai.getAndIncrement()) {
						double d = y_vector[n] - fx[n]; // - fx[n]; // +y_vector[i]
						double wd = d * weights[n];
						wymfw[n] = wd;
						swd2[nthread] += d * wd;
					}
				}
			};
		}		      
		ImageDtt.startAndJoin(threads);
		double s_rms_pure = 0;
		for (int n = 0; n < swd2.length; n++) {
			s_rms_pure += swd2[n];
		}
//		System.out.println("ai.get()="+ai.get());
		// important to set - after first cycle ai is left 16(number of threads) larger than number of cycles!
		// It is so, because it first increments, then tests if (n < num_pairs)
		
		// Regularization part where y is assumed==0, only fX is used
		
		ai.set(y_vector.length); // not yet used
		ati.set(0);
		for (int ithread = 0; ithread < threads.length; ithread++) {
			threads[ithread] = new Thread() {
				public void run() {
					int nthread =  ati.getAndIncrement();
					for (int n = ai.getAndIncrement(); n < fx.length; n = ai.getAndIncrement()) {
						double d = -fx[n]; // - fx[n]; // +y_vector[i]
						double wd = d * weights[n];
						wymfw[n] = wd;
						swd2[nthread] += d * wd;
					}
				}
			};
		}		      
		ImageDtt.startAndJoin(threads);
		
		double s_rms = 0; // start from scratch
		for (int n = 0; n < swd2.length; n++) {
			s_rms += swd2[n];
		}
		
		if (rms_fp != null) {
			rms_fp[0] = Math.sqrt(s_rms);
			rms_fp[1] = Math.sqrt(s_rms_pure/weight_pure);
		}
		return wymfw;
	}
	
	public int runLma( // <0 - failed, >=0 iteration number (1 - immediately)
			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,
			String dbg_prefix,
			int    debug_level)
	{
		boolean [] rslt = {false,false};
		this.last_rms = null; // remove?
		int iter = 0;
		if (dbg_prefix != null) {
//			 debugStateImage(dbg_prefix+"-initial");
		}
		for (iter = 0; iter < num_iter; iter++) {
			rslt =  lmaStep(
					lambda,
					rms_diff,
					debug_level);
			if (dbg_prefix != null) {
//				 debugStateImage(dbg_prefix+"-step_"+iter);
			}
			
			if (rslt == null) {
				return -1; // false; // need to check
			}
			if (debug_level > -2) { // 1) {
				System.out.println("LMA step"+String.format("%3d",iter)+": {"+rslt[0]+","+rslt[1]+"} full RMS= "+good_or_bad_rms[0]+
						" ("+initial_rms[0]+"), pure RMS="+good_or_bad_rms[1]+" ("+initial_rms[1]+") + lambda="+lambda);
			}
			if (rslt[1]) {
				break;
			}
			if (rslt[0]) { // good
				lambda *= lambda_scale_good;
			} else {
				lambda *= lambda_scale_bad;
				if (lambda > lambda_max) {
					break; // not used in lwir
				}
			}
		}
		if (rslt[0]) { // better
			if (iter >= num_iter) { // better, but num tries exceeded
				if (debug_level > 1) System.out.println("Step "+iter+": Improved, but number of steps exceeded maximal");
			} else {
				if (debug_level > 1) System.out.println("Step "+iter+": LMA: Success");
			}

		} else { // improved over initial ?
			if (last_rms[0] < initial_rms[0]) { // NaN
				rslt[0] = true;
				if (debug_level > 1) System.out.println("Step "+iter+": Failed to converge, but result improved over initial");
			} else {
				if (debug_level > 1) System.out.println("Step "+iter+": Failed to converge");
			}
		}
		boolean show_intermediate = true;
		if (show_intermediate && (debug_level > 0)) {
			System.out.println("LMA: full RMS="+last_rms[0]+" ("+initial_rms[0]+"), pure RMS="+last_rms[1]+" ("+initial_rms[1]+") + lambda="+lambda);
		}
		if (debug_level > 2){ 
			System.out.println("iteration="+iter);
		}
		if (debug_level > 0) {
			if ((debug_level > 1) ||  last_run) { // (iter == 1) || last_run) {
				if (!show_intermediate) {
					System.out.println("LMA: iter="+iter+",   full RMS="+last_rms[0]+" ("+initial_rms[0]+"), pure RMS="+last_rms[1]+" ("+initial_rms[1]+") + lambda="+lambda);
				}
			}
		}
		if ((debug_level > -2) && !rslt[0]) { // failed
			if ((debug_level > 1) || (iter == 1) || last_run) {
				System.out.println("LMA failed on iteration = "+iter);
			}
			System.out.println();
		}

		return rslt[0]? iter : -1;
	}

	private boolean [] lmaStep(
			double lambda,
			double rms_diff,
			int debug_level) {
		boolean [] rslt = {false,false};
		// maybe the following if() branch is not needed - already done in prepareLMA !
		if (this.last_rms == null) { //first time, need to calculate all (vector is valid)
			last_rms = new double[2];
			if (debug_level > 1) {
				System.out.println("lmaStep(): first step");
			}
			double [] fx = getFxDerivs(
					parameters_vector, // double []         vector,
					last_jt,           // final double [][] jt, // should be null or initialized with [vector.length][]
					debug_level);      // final int         debug_level)
			last_ymfx = getYminusFxWeighted(
					fx, // final double []   fx,
					last_rms); // final double []   rms_fp // null or [2]
			this.initial_rms = this.last_rms.clone();
			this.good_or_bad_rms = this.last_rms.clone();

			if (debug_level > -1) { // temporary
				/*
				dbgYminusFxWeight(
						this.last_ymfx,
						this.weights,
						"Initial_y-fX_after_moving_objects");
                */
			}
			if (last_ymfx == null) {
				return null; // need to re-init/restart LMA
			}
			// TODO: Restore/implement
			if (debug_level > 4) {
				double    delta = this.delta;
			 	double delta_err= debugDerivs (
			 			parameters_vector, // double [] vector,
						delta,             // double    delta,
						debug_level, //int       debugLevel,
						false); // boolean   show_img)
				System.out.println("\nMaximal error = "+delta_err);
			}
			if (debug_level > -2) { //
				String  save_dir =    debug_path;
				String  debug_title = getParametersDebugTitle();
				boolean save_all =    (debug_image != null);
				boolean show_this =   debug_level > 3;
				boolean show_all =    debug_level > 4;
				debugImageSaveShow(
						save_dir,     // String  save_dir, // save if not null
						debug_title, // String  title,    // not null
						save_all,    // boolean save_all,
						show_this,   // boolean show_this,
						show_all);   // boolean show_all)
			}
			
		}
		if (debug_level > 3) { // 0) {
			double    delta =  this.delta; // 1E-3;
			if (debug_level > 4) {
				double delta_err= debugDerivs (
						parameters_vector, // double [] vector,
						delta,             // double    delta,
						debug_level, //int       debugLevel,
						false); // boolean   show_img)
				System.out.println("\nMaximal error = "+delta_err);
			}
			int [] wh = new int[2];
			double [] debug_image =  parametersImage (
					parameters_vector, // double [] vector,
					1,                // int       gap)
					wh,
					null); // String    title)
			ShowDoubleFloatArrays.showArrays(
					debug_image,
					wh[0],
					wh[1],
					"parameters.vector-"+debug_index);
		}
		
		
		Matrix y_minus_fx_weighted = new Matrix(this.last_ymfx, this.last_ymfx.length);

		Matrix wjtjlambda = new Matrix(getWJtJlambda(
				lambda, // *10, // temporary
				this.last_jt)); // double [][] jt) // null
		
		if (debug_level>4) {
			System.out.println("JtJ + lambda*diag(JtJ");
			wjtjlambda.print(18, 6);
		}
		Matrix jtjl_inv = null;
		try {
			jtjl_inv = wjtjlambda.inverse(); // check for errors
		} catch (RuntimeException e) {
			rslt[1] = true;
			if (debug_level > -2) {
				System.out.println("Singular Matrix!");
			}

			return rslt;
		}
		if (debug_level>4) {
			System.out.println("(JtJ + lambda*diag(JtJ).inv()");
			jtjl_inv.print(18, 6);
		}
//last_jt has NaNs
		Matrix jty = (new Matrix(this.last_jt)).times(y_minus_fx_weighted);
		if (debug_level>2) {
			System.out.println("Jt * (y-fx)");
			jty.print(18, 6);
		}
		
		
		Matrix mdelta = jtjl_inv.times(jty);
		if (debug_level>2) {
			System.out.println("mdelta");
			mdelta.print(18, 6);
		}

		double scale = 1.0;
		double []  delta =      mdelta.getColumnPackedCopy();
		double []  new_vector = parameters_vector.clone();
		for (int i = 0; i < parameters_vector.length; i++) {
			new_vector[i] += scale * delta[i];
		}
		
		double [] fx = getFxDerivs(
				new_vector, // double []         vector,
				last_jt,           // final double [][] jt, // should be null or initialized with [vector.length][]
				debug_level);      // final int         debug_level)
		double [] rms = new double[2];
		last_ymfx = getYminusFxWeighted(
//				vector_XYS, // final double [][] vector_XYS,
				fx, // final double []   fx,
				rms); // final double []   rms_fp // null or [2]
		if (debug_level > 2) {
			/*
			dbgYminusFx(this.last_ymfx, "next y-fX");
			dbgXY(new_vector, "XY-correction");
			*/
		}

		if (last_ymfx == null) {
			return null; // need to re-init/restart LMA
		}

		this.good_or_bad_rms = rms.clone();
		if (rms[0] < this.last_rms[0]) { // improved
			rslt[0] = true;
			rslt[1] = rms[0] >=(this.last_rms[0] * (1.0 - rms_diff));
			this.last_rms = rms.clone();
			this.parameters_vector = new_vector.clone();
			if (debug_level > -2) { //
				String  save_dir =    debug_path;
				String  debug_title = getParametersDebugTitle();
				boolean save_all =    (debug_image != null);
				boolean show_this =   debug_level > 3;
				boolean show_all =    debug_level > 4;
				debugImageSaveShow(
						save_dir,     // String  save_dir, // save if not null
						debug_title, // String  title,    // not null
						save_all,    // boolean save_all,
						show_this,   // boolean show_this,
						show_all);   // boolean show_all)
			}
		} else { // worsened
			rslt[0] = false;
			rslt[1] = false; // do not know, caller will decide
			// restore state
			fx = getFxDerivs(
					parameters_vector, // double []         vector,
					last_jt,           // final double [][] jt, // should be null or initialized with [vector.length][]
					debug_level);      // final int         debug_level)
			last_ymfx = getYminusFxWeighted(
					fx, // final double []   fx,
					this.last_rms); // final double []   rms_fp // null or [2]
			if (last_ymfx == null) {
				return null; // need to re-init/restart LMA
			}
			if (debug_level > 2) {
				/*
				 dbgJacobians(
							corr_vector, // GeometryCorrection.CorrVector corr_vector,
							1E-5, // double delta,
							true); //boolean graphic)
							*/
			}
		}
		return rslt;
	}
	
	public String getParametersDebugTitle() {
		// alpha_en_holes -> alpha_mm_hole = NaN;
		String  debug_title = "parameters_vector-x"+woi.x+"-y"+woi.y+"-w"+woi.width+"-h"+woi.height;
		debug_title += "-al"+alpha_loss+"-alo"+alpha_offset+"-alp"+alpha_lpf+"-als"+alpha_scale_avg+(alpha_piece_linear?"-alin":"-acos");
		debug_title += "-ap"+alpha_push+"-apn"+alpha_push_neutral+"-apc"+alpha_push_center+"-amm"+alpha_mm_hole;
		debug_title += "-tl"+terr_lpf+"-vl"+veget_lpf+"-tp"+terr_pull0+"-vp"+veget_pull0;
		debug_title += "-bp"+boost_parallax;
		debug_title += from_file?"-file":"-new";
		if (um_weight > 0) {
			debug_title +="-um"+um_sigma+"_"+um_weight;
		}
		return debug_title;
	}
	
	
	public void debugImageSaveShow(
			String  save_dir, // save if not null
			String  title,    // not null
			boolean save_all,
			boolean show_this,
			boolean show_all) {
		int [] wh = new int [2];
		double [] debug_img =  parametersImage (
				parameters_vector, // double [] vector,
				1,                // int       gap)
				wh,
				null); // title);
		String title_all = title+"-live";
		if ((debug_image != null) && (debug_index < debug_image.length)) {
			title += "-"+debug_index;
			debug_image[debug_index++] = debug_img;
		}
		
		if (show_this) {
			ShowDoubleFloatArrays.showArrays(
					debug_img,
					wh[0],
					wh[1],
					title);
		}
		if (save_all || show_all) {
			double [][] dbg_img = new double [debug_index][];
			String [] titles = new String[debug_index];
			for (int i = 0; i < debug_index; i++) {
				titles[i] = ""+i;
				dbg_img[i] = debug_image[i];
			}
			if (show_all) {
				ShowDoubleFloatArrays.showArrays(
						debug_image,
						wh[0],
						wh[1],
						true,
						title_all,
						titles);
			}
			if (save_all) {
				String file_path = save_dir+title_all+".tiff";
				ImageStack imageStack = ShowDoubleFloatArrays.makeStack(dbg_img, wh[0], wh[1], titles);
				ImagePlus imp = new ImagePlus( title, imageStack);
				FileSaver fs=new FileSaver(imp);
				fs.saveAsTiff(file_path);
				System.out.println("saveDoubleArrayInModelDirectory(): saved "+file_path);
			}
		}
	}
	
	
	private double [][] getWJtJlambda(
			final double      lambda,
			final double [][] jt)
	{
		final int num_pars = jt.length;
		final int num_pars2 = num_pars * num_pars;
		final int nup_points = jt[0].length;
		final double [][] wjtjl = new double [num_pars][num_pars];
		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 indx = ai.getAndIncrement(); indx < num_pars2; indx = ai.getAndIncrement()) {
						int i = indx / num_pars;
						int j = indx % num_pars;
						if (j >= i) {
							double d = 0.0;
							for (int k = 0; k < nup_points; k++) {
								if (jt[i][k] != 0) {
									d+=0;
								}
								d += weights[k]*jt[i][k]*jt[j][k];
							}
							wjtjl[i][j] = d;
							if (i == j) {
								wjtjl[i][j] += d * lambda;
							} else {
								wjtjl[j][i] = d;
							}
						}
					}
				}
			};
		}		      
		ImageDtt.startAndJoin(threads);
		return wjtjl;
	}
	
	
	public double [] getRms() {
		return last_rms;
		
	}

	public double [] getInitialRms() {
		return initial_rms;
	}	
	
	public void updateFromParameters(
			double [] vector) {
		
		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 pindx = ai.getAndIncrement(); pindx < vector.length; pindx = ai.getAndIncrement()) {
						int [] indices = par_rindex[pindx];
						tvao[indices[0]][indices[1]] = vector[pindx];
					}
				}
			};
		}
		ImageDtt.startAndJoin(threads);
	}
	
	public void showResults(
			String      title,
			double []   vector,
			double      threshold_terrain,
			double      min_max_terrain, //0.1
			double      min_terrain, //0.001
			double      min_vegetation) { // 0.5
		double [][] tvas = getTVASWoi(
				vector);    // final double [] vector)
		double [][] terrain_weights_max = getTerrainWeights(
				threshold_terrain, // final double      alpha_threshold, // discard images with too low transparency
				vector);           // final double []   vector)
		double [] terrain_masked =     tvas[0].clone();
		double [] vegetation_masked =  tvas[1].clone();
		for (int i = 0; i < terrain_masked.length; i++) {
			if (!(terrain_weights_max[0][i] >= min_terrain))      terrain_masked[i] = Double.NaN;
			if (!(terrain_weights_max[1][i] >= min_max_terrain)) terrain_masked[i] = Double.NaN;
			if (!(tvas[2][i] >=      min_vegetation)) vegetation_masked[i] = Double.NaN;
		}
		double [][] tvww = {terrain_masked, vegetation_masked, tvas[0], tvas[1], terrain_weights_max[0], terrain_weights_max[1],tvas[2]}; // no scene offsets
		String [] titles = {"terr>"+min_terrain,"veget>"+min_vegetation,"terr","veget","terr_str","terr_max","alpha"};
		
		ShowDoubleFloatArrays.showArrays(
				tvww,
				woi.width,
				woi.height,
				true,
				title,
				titles);
		return;
	}
	
	public double [][] getTVASWoi(
			final double [] vector){
		final int woi_length = woi.width*woi.height;
		double [][] tvas = new double [4][];
		for (int i = 0; i < 3; i++) tvas[i] = new double [woi_length];
		tvas[3] = new double [num_scenes];
		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 windx = ai.getAndIncrement(); windx < woi_length; windx = ai.getAndIncrement()) {
						int x = windx % woi.width + woi.x;
						int y = windx / woi.width + woi.y;
						int indx = x + y * full.width;
						for (int i = 0; i < 3; i++) {
							tvas[i][windx] = tvao[i][indx]; // non-adjusted parameters
						}
					}
				}
			};
		}
		ImageDtt.startAndJoin(threads);
		
		
		ai.set(0);
		for (int ithread = 0; ithread < threads.length; ithread++) {
			threads[ithread] = new Thread() {
				public void run() {
					for (int pindx = ai.getAndIncrement(); pindx < vector.length; pindx = ai.getAndIncrement()) {
						int [] indices = par_rindex[pindx];
						if (indices[0] <3) {
							int wx = indices[1] % full.width - woi.x;
							int wy = indices[1] / full.width - woi.y;
							int windx = wx + wy * woi.width;
							tvas[indices[0]][windx] = vector[pindx]; // all adjusted parameters, including offsets
						} else {
							tvas[indices[0]][ indices[1]] = vector[pindx]; // all adjusted parameters, including offsets
						}
					}
				}
			};
		}
		ImageDtt.startAndJoin(threads);
		return tvas;
	}
	
	public static double [] laplacian(
			final boolean   gaussian,
			final double [] data_in,
			final int       width,
			final double    weight_diag) {
		final int height = data_in.length/width;
		final double [] data_out = new double [data_in.length];
		Arrays.fill(data_out, Double.NaN);
		final Thread[] threads = ImageDtt.newThreadArray();
		final AtomicInteger ai = new AtomicInteger(0);
		final double [] weights8 = new double [8];
		weights8[0] = 0.25/(weight_diag+1);
		weights8[1] = weights8[0]*weight_diag;
		for (int i = 2; i < 8;i++) {
			weights8[i] = weights8[i % 2];
		}
		
		ai.set(0);
		for (int ithread = 0; ithread < threads.length; ithread++) {
			threads[ithread] = new Thread() {
				public void run() {
					TileNeibs tn = new TileNeibs(width,height);
					for (int nPix = ai.getAndIncrement(); nPix < data_in.length; nPix = ai.getAndIncrement()) if(!Double.isNaN(data_in[nPix])){
						double sw = 0, swd = 0;
						for (int dir = 0; dir < 8; dir++) {
							int nPix1= tn.getNeibIndex(nPix, dir);
							if ((nPix1 >= 0) && !Double.isNaN(data_in[nPix1])) {
								sw  += weights8[dir];
								swd += data_in[nPix1]*weights8[dir];
							}
						}
						if (sw != 0) {
							swd /= sw;
							data_out[nPix] = gaussian? swd : (data_in[nPix] - swd); 
						}
					}
				}
			};
		}
		ImageDtt.startAndJoin(threads);
		return data_out;
		
	}
	
	
	
	public double [][] getTerrainWeights(
			final double      alpha_threshold, // discard images with too low transparency
			final double []   vector)
	{
		final int woi_length = woi.width*woi.height;
		final double [][] terrain_scene_weights = new double [num_scenes][woi_length];
		final double [][] terrain_scene_max = new double [num_scenes][woi_length];
		final double [][] full_scene_weights = new double [num_scenes][woi_length];
        final double [][] terrain_weights = new double [2][woi_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() {
					double [] alpha =      new double [4];
					for (int n = ai.getAndIncrement(); n < y_vector.length; n = ai.getAndIncrement()) {
						int nscene =        data_source[n][0][0];
						int indx =          data_source[n][0][1];
						int x =             indx % full.width - woi.x;
						int y =             indx / full.width - woi.y;
						int windx =         x + y * woi.width;
						double [] cw =      corners_weights[n]; 
						int [] indx_alpha = data_source[n][2];
						double sum_a =0;
						if (cw != null) {
							for (int i = 0; i < 4; i++ ) {
								int ia=indx_alpha[i];
								alpha[i] =      cw[i] * ((ia >= 0) ? vector[ia]: tvao[TVAO_VEGETATION_ALPHA][-1-ia]);
								sum_a += alpha[i];
							}
							double tw = Math.max(0, 1.0 - sum_a);
							terrain_scene_max[nscene][windx] = tw;
							if (tw >= alpha_threshold) {
								terrain_scene_weights[nscene][windx] = weights[n] * tw;
							}
							full_scene_weights[nscene][windx] =  weights[n];
						}
					}
				}
			};
		}
		ImageDtt.startAndJoin(threads);
		// sum for all scenes, divide (undefined - also 0)
		Arrays.fill(terrain_weights[0], Double.NaN);
		Arrays.fill(terrain_weights[1], Double.NaN);
		ai.set(0);
		for (int ithread = 0; ithread < threads.length; ithread++) {
			threads[ithread] = new Thread() {
				public void run() {
					for (int windx = ai.getAndIncrement(); windx < woi_length; windx = ai.getAndIncrement()) {
						double sum_w = 0, sum_wt = 0, max_t = 0;
						for (int nscene = 0; nscene < num_scenes;nscene++) {
							sum_wt += terrain_scene_weights[nscene][windx];
							sum_w +=  full_scene_weights[nscene][windx];
							max_t = Math.max(max_t, terrain_scene_max[nscene][windx]);
						}
						if (sum_w > 0) {
							terrain_weights[0][windx] = sum_wt/sum_w;
							terrain_weights[1][windx] = max_t;
						}
					}
				}
			};
		}
		ImageDtt.startAndJoin(threads);
		
		// regularization weights and derivatives
		return terrain_weights;
    }
	
	
	private double [] getFxDerivs(
			final double []   vector,
			final double [][] jt, // should be null or initialized with [vector.length][]
			final int         debug_level)	{
		// using 0.5*(1-cos(alpha/2pi) instead of alpha. alpha < 0 -> 0, alpha > 1 -> 1. Depends on other terms for stability 
		double [] fX = new double [weights.length]; // num_pairs + vector.length];
		if (jt != null) {
			for (int i = 0; i < jt.length; i++) {
				jt[i] = new double [weights.length]; // weights.length];
			}
		}
		final int dbg_n = -69992;
		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() {
					double [] vegetation = new double [4]; 
					double [] alpha =      new double [4];
					for (int n = ai.getAndIncrement(); n < y_vector.length; n = ai.getAndIncrement()) {
						if (n == dbg_n) {
							System.out.println("getFxDerivs(): n="+n);
						}
//						int nscene =   data_source[n][0][0];
//						int indx =     data_source[n][0][1];
						double terrain = vector[data_source[n][0][2]];
						double [] cw = corners_weights[n]; 
						double d; 
						int [] indx_vegetation = data_source[n][1]; 
						int [] indx_alpha =      data_source[n][2];
						double sum_v =0, sum_a =0;
						if (cw != null) {
							for (int i = 0; i < 4; i++ ) {
								int iv = indx_vegetation[i], ia=indx_alpha[i];
								vegetation[i] = cw[i] * ((iv >= 0) ? vector[iv]: tvao[TVAO_VEGETATION][-1-iv]);
								alpha[i] =      cw[i] * ((ia >= 0) ? vector[ia]: tvao[TVAO_VEGETATION_ALPHA][-1-ia]);
								sum_v += vegetation[i];
								sum_a += alpha[i];
							}
							double k;
							if (alpha_piece_linear) {
								k = (sum_a < 0)? 0: ((sum_a > 1) ? 1.0: sum_a);	
							} else {
								k = (sum_a < 0)? 0: ((sum_a > 1) ? 1.0: 0.5 * (1.0 - Math.cos(sum_a*Math.PI)));	
							}
							
//							d = terrain * (1.0 - sum_a) + sum_v * sum_a;
							d = terrain * (1.0 - k) + sum_v * k;
							
							if (jt != null) {
								jt[data_source[n][0][2]][n] = 1 - k;// sum_a; // d/dterrain
								for (int i = 0; i < 4; i++ ) {
									if (indx_vegetation[i] >= 0) {
										jt[data_source[n][1][i]][n] = cw[i] * k; // sum_a; // d/dvegetation[i]
									}
									if ((indx_alpha[i] >= 0) && (sum_a > 0) && (sum_a < 1.0)) {
									    if (alpha_piece_linear) {
									    	jt[data_source[n][2][i]][n] = cw[i] * (sum_v - terrain); // d/dalpha[i]
									    } else {
									    	jt[data_source[n][2][i]][n] = cw[i] * (sum_v - terrain) *0.5*Math.PI *Math.sin(sum_a*Math.PI); // d/dalpha[i]
									    }
									}
								}								
							}

						} else {
							d = terrain; 
							if (jt != null) {
								jt[data_source[n][0][2]][n] = 1;  // d/dterrain
							}
						}
						if (data_source[n][0][3] >= 0) {
							double scene_offs = vector[data_source[n][0][3]];
							fX[n] = d + scene_offs;
							if (jt != null) {
								jt[data_source[n][0][3]][n] = 1;
							}
						} 
					}
				}
			};
		}
		ImageDtt.startAndJoin(threads);
		// regularization weights and derivatives
		// splitting alpha_lpf from alpha_loss+alpha_push
		int ind_next = y_vector.length;
		
		if ((alpha_loss > 0) || (alpha_push > 0)) {
			int dbg_nx = -76340;
			final int ind_y_alpha_loss = ind_next;
			ind_next += num_pars_vegetation_alpha;
			ai.set(0);
			for (int ithread = 0; ithread < threads.length; ithread++) {
				threads[ithread] = new Thread() {
					public void run() {
						for (int n = ai.getAndIncrement(); n < num_pars_vegetation_alpha; n = ai.getAndIncrement()) {
							int np = ind_pars_vegetation_alpha + n; // index of the alpha parameter
							int nx = n + ind_y_alpha_loss; //  y_vector.length; // x - index
							if (nx == dbg_nx) {
								System.out.println("getFxDerivs(): n="+n+", nx="+nx);
							}
							double d = 0;
							fX[nx] = 0.0;
							if (alpha_loss > 0) {
								double alpha = vector[np];
								if (alpha < alpha_offset) {
									d = alpha- alpha_offset;
								} else if (alpha > (1 - alpha_offset)) {
									d = alpha - (1.0 - alpha_offset);
								}
								if (d != 0) {
									fX[nx] = d * d * alpha_loss;
									if (jt != null) {
										jt[np][nx] = 2 * alpha_loss * d; // d/dalpha[i]		
									}							
								}
							}
							double avg = 0;
							int nn = 0;
							double neib_min = Double.POSITIVE_INFINITY, neib_max = Double.NEGATIVE_INFINITY;
							for (int i = 0; i < alpha_neibs[n].length; i++) { // now 4, may be increased
								int di =  alpha_neibs[n][i];
								d=0;
								if (di >= 0) {
									d = vector[di];  // d - full parameter index
									avg+=d;
									if (d < neib_min) neib_min = d; 
									if (d > neib_max) neib_max = d; 
									nn++;
								} else if (di < -1) {
									d = tvao[TVAO_VEGETATION_ALPHA][-di - 2]; 
									avg+=d;
									if (d < neib_min) neib_min = d; 
									if (d > neib_max) neib_max = d; 
									nn++;
								}
							}
							avg /= nn; // average
							if (alpha_push > 0) {
								/*
								 * p = alpha_push
								 * n = alpha_push_neutral
								 * a0 =        p/(2 * n)
								 * a1 =        p/(2 * (1 - n))
								 * f1(x) =     p/(2 * n)* x * (1 - x/(2*n))
								 * f2(x) =     p * (x - 2*n + 1) * (1 - x) / (4 * (1-n)*(1-n) 
								 * df1(x)/dx = p/(2 * n^2 )*( n - x)
								 * df2(x)/dx = p* (n-x)/(2*(1-n)^2)
								 */
								// average including center:
								double sum_w = (nn + alpha_push_center);
								double avg_c = (avg * nn + vector[np] * alpha_push_center) / sum_w;
								double one_minus_n = 1.0 - alpha_push_neutral;
								double one_minus_p = 1.0 - alpha_push;
								double x_minus_p = avg_c - alpha_push;
								if ((avg_c > 0) || (avg_c < 1.0)) {
//									fX[nx] += alpha_push * avg_c * (1.0 - avg_c);
									if (avg_c <= alpha_push_neutral) {
										fX[nx] += alpha_push / (2 * alpha_push_neutral) * avg_c * (1 - avg_c / (2 * alpha_push_neutral));
									} else {
										fX[nx] += alpha_push * (avg_c - 2 * alpha_push_neutral + 1) * (1 - avg_c) / (4 *one_minus_n*one_minus_n); 
									}
									if (jt != null) {
										//									double dd = alpha_push / sum_w * (1 - 2 * avg_c);
										double dd;
										if (avg_c <= alpha_push_neutral) {
											dd = alpha_push / (sum_w * 2 * alpha_push_neutral * alpha_push_neutral ) * (alpha_push_neutral  - avg_c);
										} else {
											dd = alpha_push / (sum_w * 2 * one_minus_n * one_minus_n)  * (alpha_push_neutral  - avg_c); 
										}
										jt[np][nx] += dd * alpha_push_center; // 2 * alpha_loss * d; // d/dalpha[i]		
										for (int i = 0; i < alpha_neibs[n].length; i++) { // now 4, may be increased
											int di =  alpha_neibs[n][i];
											if (di >= 0) {
												jt[di][nx] += dd; // 2 * alpha_loss * d; // d/dalpha[i]
											}
										}
									}								
								}
							}
						}
					}
				};
			}
			ImageDtt.startAndJoin(threads);
		} // if ((alpha_loss > 0) || (alpha_push > 0)){
		
		
		
		
		if (alpha_lpf >= 0) {
			int dbg_nx = -76340;
			final int ind_y_alpha_lpf = ind_next;
			ind_next += num_pars_vegetation_alpha;
			ai.set(0);
			for (int ithread = 0; ithread < threads.length; ithread++) {
				threads[ithread] = new Thread() {
					public void run() {
						for (int n = ai.getAndIncrement(); n < num_pars_vegetation_alpha; n = ai.getAndIncrement()) {
							int np = ind_pars_vegetation_alpha + n; // index of the alpha parameter
							int nx = n + ind_y_alpha_lpf; //  y_vector.length; // x - index
							if (nx == dbg_nx) {
								System.out.println("getFxDerivs(): n="+n+", nx="+nx);
							}
							double d = 0;
							fX[nx] = 0.0;
							double avg = 0;
							int nn = 0;
							double neib_min = Double.POSITIVE_INFINITY, neib_max = Double.NEGATIVE_INFINITY;
							for (int i = 0; i < alpha_neibs[n].length; i++) { // now 4, may be increased
								int di =  alpha_neibs[n][i];
								d=0;
								if (di >= 0) {
									d = vector[di];  // d - full parameter index
									avg+=d;
									if (d < neib_min) neib_min = d; 
									if (d > neib_max) neib_max = d; 
									nn++;
								} else if (di < -1) {
									d = tvao[TVAO_VEGETATION_ALPHA][-di - 2]; 
									avg+=d;
									if (d < neib_min) neib_min = d; 
									if (d > neib_max) neib_max = d; 
									nn++;
								}
							}
							avg /= nn; // average
							// add cost for difference between this alpha and average of 4 neighbors (when they exist
							// applies to alpha before cosine, so it will pull borders even when alpha<0 or alpha > 1 (zero derivatives)
							//alpha_scale_avg
							double mm = neib_min + (neib_max-neib_min) * alpha_mm_hole;
							double effective_alpha_lpf = alpha_lpf; 
							if (alpha_en_holes && !Double.isNaN(alpha_mm_hole) && (vector[np] <= mm) && ((neib_max-neib_min) >= alpha_diff_hole)) {
								effective_alpha_lpf = 0.0; // disable alpha_lpf
							}
							// disable pull to average of neighbors for small holes in vegetation (local "almost minimum")
							///								if (Double.isNaN(alpha_mm_hole) || (vector[np] > mm) || ((neib_max-neib_min) < alpha_diff_hole)) {
							//		this.alpha_mm_hole =     alpha_mm_hole;
							if (alpha_scale_avg == 1) {
								fX[nx] = effective_alpha_lpf * (vector[np] - avg);
								if (jt != null) {
									jt[np][nx] += effective_alpha_lpf;
									for (int i = 0; i < alpha_neibs[n].length; i++) { // now 4, may be increased
										int di =  alpha_neibs[n][i];
										if (di >= 0) {
											jt[di][nx] -= effective_alpha_lpf/nn;
										}
									}
								}
							} else {
								if ((avg > 0) && (avg < 1)) { 
									double savg = 0.5 + (avg - 0.5) * alpha_scale_avg;
									fX[nx] = effective_alpha_lpf * (vector[np] - savg);
									if (jt != null) {
										//											jt[np][nx] += effective_alpha_lpf;
										for (int i = 0; i < alpha_neibs[n].length; i++) { // now 4, may be increased
											int di =  alpha_neibs[n][i];
											if (di >= 0) {
												jt[di][nx] -= effective_alpha_lpf/nn * alpha_scale_avg;
											}
										}
									}
								} else {
									if (avg <= 0) {
										fX[nx] = effective_alpha_lpf * (vector[np] + 0.5*(alpha_scale_avg - 1.0));
									} else { // avg > 1
										fX[nx] = effective_alpha_lpf * (vector[np] - 0.5*(alpha_scale_avg + 1.0));
									}
								}
								if (jt != null) {
									jt[np][nx] += effective_alpha_lpf;
								}
							}	
						}
					}
				};
			}
			ImageDtt.startAndJoin(threads);
		} // if (alpha_lpf >= 0) {
		
		if (terr_lpf >= 0) {
			final int ind_y_terr = ind_next;
			ind_next += num_pars_terrain;
			ai.set(0);
			for (int ithread = 0; ithread < threads.length; ithread++) {
				threads[ithread] = new Thread() {
					public void run() {
						for (int n = ai.getAndIncrement(); n < num_pars_terrain; n = ai.getAndIncrement()) {
							int np = ind_pars_terrain + n; // index of the alpha parameter
							int nx = n + ind_y_terr; //  y_vector.length; // x - index
							double d = 0;
							if (terr_lpf > 0) {
								double avg = 0;
								int nn = 0;
								for (int i = 0; i < terr_neibs[n].length; i++) { // now 4, may be increased
									int di =  terr_neibs[n][i];
									d=0;
									if (di >= 0) {
										d = vector[di];  // d - full parameter index
										avg+=d;
										nn++;
									} else if (di < -1) {
										d = tvao[TVAO_TERRAIN][-di - 2]; 
										avg+=d;
										nn++;
									}
								}
								avg /= nn; // average
								fX[nx] += terr_lpf * (vector[np] - avg) + terr_pull0 * vector[np];
								if (jt != null) {
									jt[np][nx] += terr_lpf + terr_pull0;
									for (int i = 0; i < terr_neibs[n].length; i++) { // now 4, may be increased
										int di =  terr_neibs[n][i];
										if (di >= 0) {
											jt[di][nx] -= terr_lpf/nn;
										}
									}								
								}							
							}
						}
					}
				};
			}
			ImageDtt.startAndJoin(threads);
		}
		if (veget_lpf >= 0) {
			final int ind_y_veget = ind_next;
			ind_next += num_pars_vegetation;
			ai.set(0);
			
			for (int ithread = 0; ithread < threads.length; ithread++) {
				threads[ithread] = new Thread() {
					public void run() {
						for (int n = ai.getAndIncrement(); n < num_pars_vegetation; n = ai.getAndIncrement()) {
							int np = ind_pars_vegetation + n; // index of the alpha parameter
							int nx = n + ind_y_veget; //  y_vector.length; // x - index
							double d = 0;
							if (veget_lpf > 0) {
								double avg = 0;
								int nn = 0;
								for (int i = 0; i < veget_neibs[n].length; i++) { // now 4, may be increased
									int di =  veget_neibs[n][i];
									d=0;
									if (di >= 0) {
										d = vector[di];  // d - full parameter index
										avg+=d;
										nn++;
									} else if (di < -1) {
										d = tvao[TVAO_VEGETATION][-di - 2]; 
										avg+=d;
										nn++;
									}
								}
								avg /= nn; // average
								fX[nx] += veget_lpf * (vector[np] - avg)  + veget_pull0 * vector[np];
								if (jt != null) {
									jt[np][nx] += veget_lpf + veget_pull0;
									for (int i = 0; i < veget_neibs[n].length; i++) { // now 4, may be increased
										int di =  veget_neibs[n][i];
										if (di >= 0) {
											jt[di][nx] -= veget_lpf/nn;
										}
									}								
								}							
							}
						}
					}
				};
			}
			ImageDtt.startAndJoin(threads);
		}
		return fX;		
	}
	
	
	private double [][] getFxDerivsDelta(
			double []         vector,
			final double      delta,
			final int         debug_level) {
		int dbg_n = -2486;
		double [][] jt =  new double [vector.length][weights.length];
		for (int nv = 0; nv < vector.length; nv++) {
			if (nv == dbg_n) {
				System.out.println("getFxDerivsDelta(): nv="+nv);
			}
			double [] vpm = vector.clone();
			vpm[nv]+= 0.5*delta;
			double [] fx_p =  getFxDerivs(
					vpm,
					null, // final double [][] jt, // should be null or initialized with [vector.length][]
					debug_level);
			vpm[nv]-= delta;
			double [] fx_m =  getFxDerivs(
					vpm,
					null, // final double [][] jt, // should be null or initialized with [vector.length][]
					debug_level);
			for (int i = 0; i < weights.length; i++) if (weights[i] > 0) {
				jt[nv][i] = (fx_p[i]-fx_m[i])/delta;
			}
		}
		return jt;
	}

	private int [] getParamDebugIndices(
			int gap) {
		int woi_width3=woi.width*3 + 2 * gap;
//		int num_scene_rows = (int) Math.ceil(1.0*par_index[3].length/woi_width3);
		int num_scene_rows = (int) Math.ceil(1.0*num_scenes/woi_width3); // some scenes have NaN
		int [] indices = new int [woi_width3 * (woi.height + gap+ num_scene_rows)];
		Arrays.fill(indices, -1);
		for (int h = 0; h < woi.height; h++) {
			for (int n = 0; n < 3; n++) {
				for (int w = 0; w < woi.width; w++) {
					int indx = (h + woi.y) * full.width + (w + woi.x);
					indices [(h * woi_width3) + (woi.width + gap) * n + w] = par_index[n][indx]; // h * woi.width + w]; 
				}
			}
		}
		for (int i = 0; i < par_index[3].length; i++) {
			indices [((woi.height + gap) * woi_width3) + i] = par_index[3][i]; // some will be -1
		}
		return indices;
	}
	
	public void showYfX(
			double [] vector,
			String title) {
		if (vector == null) {
			vector = parameters_vector;
		}
		double [][][] img_data = new double [3][num_scenes][woi.width*woi.height];
		for (int n = 0; n < img_data.length; n++) {
			for (int nscene = 0; nscene < num_scenes; nscene++) {
				Arrays.fill(img_data[n][nscene], Double.NaN);
			}
		}
		double [] fX = getFxDerivs( // longer than y_vector
				vector, // final double []   vector,
				null,   // final double [][] jt, // should be null or initialized with [vector.length][]
				-1);    // final int         debug_level)
		final Thread[] threads = ImageDtt.newThreadArray(QuadCLT.THREADS_MAX);
		final AtomicInteger ai = new AtomicInteger(0);
		for (int ithread = 0; ithread < threads.length; ithread++) {
			threads[ithread] = new Thread() {
				public void run() {
					for (int ny = ai.getAndIncrement(); ny < data_source.length; ny = ai.getAndIncrement()) {
						int nscene =   data_source[ny][0][0];
						int indx =     data_source[ny][0][1];
						int wx = (indx % full.width) - woi.x; 
						int wy = (indx / full.width) - woi.y;
						int windx = wx + wy * woi.width;
						img_data[0][nscene][windx] = y_vector[ny];
						img_data[1][nscene][windx] = fX[ny];
						img_data[2][nscene][windx] = y_vector[ny]-fX[ny];
					}
				}
			};
		}		      
		ImageDtt.startAndJoin(threads);
		String [] top_titles = {"Y", "fX", "Y-fX"};
		String [] scene_titles = new String[num_scenes];
		for (int i = 0; i < num_scenes; i++) {
			scene_titles[i] = ""+i;
		}
		ShowDoubleFloatArrays.showArraysHyperstack(
				img_data,           // double[][][] pixels, 
				woi.width,                // int          width, 
				title, // "terrain_vegetation_render.tiff", // String       title, "time_derivs-rt"+diff_time_rt+"-rxy"+diff_time_rxy,
				scene_titles,                     // String []    titles, // all slices*frames titles or just slice titles or null
				top_titles,                     // String []    frame_titles, // frame titles or null
				true);                            // boolean      show)

		return;
		
	}
	
	public double [] parametersImage (
			double [] vector,
			int       gap,
			int []    wh,
			String    title) {
		int [] indices = getParamDebugIndices (gap);
		int woi_width3=woi.width*3 + 2 * gap;
		double [] dbg_img = new double [indices.length];
		for (int i = 0; i < indices.length; i++) {
			if (indices[i] < 0) {
				dbg_img[i] = Double.NaN;
			} else {
				dbg_img[i]= vector[indices[i]];
				// scale alpha for the same image
				int w = i % woi_width3;
				int h = i / woi_width3;
				if ((w > 2 *( woi.width+ gap)) && (h < woi.height))  {
					dbg_img[i] = debug_alpha_scales[0] + (debug_alpha_scales[1] - debug_alpha_scales[0]) * dbg_img[i];  
				}
			}
		}
		if (wh != null) {
			wh[0] = woi_width3;
			wh[1] = indices.length/woi_width3;
		}
		if (title != null) {
			ShowDoubleFloatArrays.showArrays(
					dbg_img,
					woi_width3,
					indices.length/woi_width3,
					title);
		}
		return dbg_img;
	}
	
	
	
	public static String getSavePath(
			String dir,
			String title) { // no .par-tiff
		return getSavePath(dir,title,PAR_EXT);
	}

	public static String getSavePath(
			String dir,
			String title,
			String ext) {
		if (!dir.endsWith(Prefs.getFileSeparator())) {
			dir+=Prefs.getFileSeparator();
		}
		return dir+title+ext;
	}

	
	
	public void saveParametersFile(
			String dir,
			String title, // no .par-tiff
			double [] vector) {
		int woi_length = woi.width*woi.height;
		int ext_length = woi_length; 
		if (vector == null) {
			vector = parameters_vector;
		}
		
		int height = woi.height;
		
		if (num_scenes > woi_length) { // very unlikely but still may happen
			height = (num_scenes + woi.width - 1) / woi.width;
			ext_length = woi.width * height; 
		}
		// assuming
		String [] titles = {"terrain","vegetation","alpha","offset","terrain-index","vegetation-index","alpha-index","offset-index"};
		double [][] data = new double [titles.length][ext_length];
		for (int i = 0; i < 4; i++) Arrays.fill(data[i],Double.NaN);
		int [][] indices = new int [4][ext_length];
		for (int n = 0; n < 4; n++) {
			for (int i = 0; (i < indices[n].length) && (i < par_index[n].length); i++) {
				int indx = i;
				if (n < TVAO_SCENE_OFFSET) {
					int w = i % woi.width;
					int h = i / woi.width;
					indx = (h + woi.y) * full.width + (w + woi.x);
				}
				int pindx = par_index[n][indx];
				indices[n][i] =  pindx;
				if (pindx < 0) {
					if (n == TVAO_SCENE_OFFSET) {
						data[n][i] = Double.NaN;
					} else {
						data[n][i] = tvao[n][indx];
					}
				} else {
					data[n][i] = vector[pindx];
				}
				data[n+4][i] = indices[n][i];
			}
		}
		ImagePlus imp = ShowDoubleFloatArrays.makeArrays(
				data,
				woi.width,
				height,
				title, // +"-objects_"+scene_num,				
				titles); // test_titles,
		imp.setProperty("WOI_X",                     ""+woi.x);
		imp.setProperty("WOI_Y",                     ""+woi.y);
		imp.setProperty("WOI_WIDTH",                 ""+woi.width);
		imp.setProperty("WOI_HEIGHT",                ""+woi.height);
		imp.setProperty("FULL_WIDTH",                ""+full.width);
		imp.setProperty("FULL_HEIGHT",               ""+full.height);
		
		imp.setProperty("NUM_SCENES",                ""+num_scenes);
		imp.setProperty("DIFF_OFFSETS",              ""+diff_offsets);
		imp.setProperty("NUM_PARS_TERRAIN",          ""+num_pars_terrain);
		imp.setProperty("NUM_PARS_VEGETATION",       ""+num_pars_vegetation);
		imp.setProperty("NUM_PARS_VEGETATION_ALPHA", ""+num_pars_vegetation_alpha);
		imp.setProperty("NUM_PARS_SCENES",           ""+num_pars_scenes);
		imp.setProperty("IND_PARS_TERRAIN",          ""+ind_pars_terrain);
		imp.setProperty("IND_PARS_VEGETATION",       ""+ind_pars_vegetation);
		imp.setProperty("IND_PARS_VEGETATION_ALPHA", ""+ind_pars_vegetation_alpha);
		imp.setProperty("IND_PARS_SCENES",           ""+ind_pars_scenes);
		
		imp.setProperty("ALPHA_LOSS",                ""+alpha_loss);
		imp.setProperty("ALPHA_OFFSET",              ""+alpha_offset);
		imp.setProperty("ALPHA_LPF",                 ""+alpha_lpf);
		imp.setProperty("ALPHA_PIECE_LINEAR",        ""+alpha_piece_linear);
		imp.setProperty("ALPHA_SCALE_AVG",           ""+alpha_scale_avg);
		imp.setProperty("ALPHA_PUSH",                ""+alpha_push);
		imp.setProperty("ALPHA_PUSH_NEUTRAL",        ""+alpha_push_neutral);
		imp.setProperty("ALPHA_PUSH_CENTER",         ""+alpha_push_center );
		imp.setProperty("ALPHA_EN_HOLES",            ""+alpha_en_holes );
		imp.setProperty("ALPHA_MM_HOLE",             ""+alpha_mm_hole);
		imp.setProperty("ALPHA_DIFF_HOLE",           ""+alpha_diff_hole);
		imp.setProperty("TERR_LPF",                  ""+terr_lpf);
		imp.setProperty("VEGET_LPF",                 ""+veget_lpf);
		imp.setProperty("TERR_PULL0",                ""+terr_pull0);
		imp.setProperty("VEGET_PULL0",               ""+veget_pull0);
		imp.setProperty("BOOST_PARALLAX",            ""+boost_parallax);
		imp.setProperty("UM_SIGMA",                  ""+um_sigma);
		imp.setProperty("UM_WEIGHT",                 ""+um_weight);
		String path = getSavePath(
				dir, // String dir,
				title); // String title)
		JP46_Reader_camera.encodeProperiesToInfo(imp);			
		FileSaver fs=new FileSaver(imp);
		fs.saveAsTiff(path);
		System.out.println("saveParametersFile(): saved "+path); // dir+title+PAR_EXT);
		return;
	}
	
	public double [][] restoreParametersFile( //FIXME: Not finished for real import ! 
			String    path,
			Rectangle file_woi) {//  if not null, will return woi data and not input parameters to this instance
		ImagePlus imp_pars = new ImagePlus (path);
		if (imp_pars.getWidth()==0) {
			throw new IllegalArgumentException("Could not read "+path);
		}
		int num_slices = imp_pars.getStack().getSize();
		if (num_slices != 8) {
			throw new IllegalArgumentException("Expecting an 8-slice file, got "+num_slices);
		}
		int width = imp_pars.getWidth();
		int height = imp_pars.getHeight();
		double [][] data = new double[4][width*height];
		int [][] indices = new int[4][width*height];
		for (int n = 0; n < 4; n++) {
			float [] pixels = (float[]) imp_pars.getStack().getPixels(n+1);
			for (int i = 0; i < data[n].length; i++) {
				data[n][i] = pixels[i];
			}
		}
		for (int n = 0; n < 4; n++) {
			float [] pixels = (float[]) imp_pars.getStack().getPixels(n+5);
			for (int i = 0; i < data[n].length; i++) {
				indices[n][i] = (int) pixels[i];
			}
		}
		JP46_Reader_camera.decodeProperiesFromInfo(imp_pars);
		Rectangle full = new Rectangle (
				0,
				0,
				Integer.parseInt((String) imp_pars.getProperty("FULL_WIDTH")),
				Integer.parseInt((String) imp_pars.getProperty("FULL_HEIGHT")));
		if (!full.equals(this.full)) {
			throw new IllegalArgumentException("Full image window differes, this is ("+this.full.width+" x "+this.full.height+"), file contains ("+
					+full.width+" x "+full.height+")");
		}
		int num_scenes = Integer.parseInt((String) imp_pars.getProperty("NUM_SCENES"));
		
		if (num_scenes != this.num_scenes) {
			throw new IllegalArgumentException("Number of scenes can not be changed, this has "+this.num_scenes+", file has "+
					+num_scenes+".");
		}
		

		
		Rectangle woi = new Rectangle (
				Integer.parseInt((String) imp_pars.getProperty("WOI_X")),
				Integer.parseInt((String) imp_pars.getProperty("WOI_Y")),
				Integer.parseInt((String) imp_pars.getProperty("WOI_WIDTH")),
				Integer.parseInt((String) imp_pars.getProperty("WOI_HEIGHT")));
		
		if (file_woi != null) {
			file_woi.x = woi.x;
			file_woi.y = woi.y;
			file_woi.width =  woi.width;
			file_woi.height = woi.height;
// Remove undefined values that were not adjusted for this woi			
			for (int n = 0; n < 4; n++) {
				for (int i = 0; i < data[n].length; i++) {
					if (indices[n][i] < 0) {
						data[n][i] = Double.NaN;
					}
				}
			}
			// Trim data to actiual
//			int num_pars_scenes =           Integer.parseInt((String) imp_pars.getProperty("NUM_PARS_SCENES"));
//			double [] scene_offsets = new double [num_pars_scenes];
			double [] scene_offsets = new double [num_scenes];
			System.arraycopy(data[TVAO_SCENE_OFFSET], 0, scene_offsets, 0, scene_offsets.length);
			// temporary patch, later they will be NaNs already - Already done !
			for (int i = 0; i < scene_offsets.length; i++) {
				if (indices[TVAO_SCENE_OFFSET][i] < 0) {
					data[TVAO_SCENE_OFFSET][i] = Double.NaN;
				}
			}
			
			for (int n = 0; n < 3; n++) {
				if (woi.width * woi.height < data[n].length ) {
					double [] d = new double [woi.width * woi.height];
					System.arraycopy(data[n], 0, d, 0, d.length);
					data[n] = d;
				}
			}
			data[3] = scene_offsets;
		} else {
			this.woi = woi;
			diff_offsets =              Boolean.parseBoolean((String) imp_pars.getProperty("DIFF_OFFSETS"));
			alpha_piece_linear =        Boolean.parseBoolean((String) imp_pars.getProperty("ALPHA_PIECE_LINEAR"));
			num_pars_terrain =          Integer.parseInt((String) imp_pars.getProperty("NUM_PARS_TERRAIN"));
			num_pars_vegetation =       Integer.parseInt((String) imp_pars.getProperty("NUM_PARS_VEGETATION"));
			num_pars_vegetation_alpha = Integer.parseInt((String) imp_pars.getProperty("NUM_PARS_VEGETATION_ALPHA"));
			num_pars_scenes =           Integer.parseInt((String) imp_pars.getProperty("NUM_PARS_SCENES"));
			ind_pars_terrain =          Integer.parseInt((String) imp_pars.getProperty("IND_PARS_TERRAIN"));
			ind_pars_vegetation =       Integer.parseInt((String) imp_pars.getProperty("IND_PARS_VEGETATION"));
			ind_pars_vegetation_alpha = Integer.parseInt((String) imp_pars.getProperty("IND_PARS_VEGETATION_ALPHA"));
			ind_pars_scenes =           Integer.parseInt((String) imp_pars.getProperty("IND_PARS_SCENES"));
			alpha_loss =                Double.parseDouble((String) imp_pars.getProperty("ALPHA_LOSS"));
			alpha_offset =              Double.parseDouble((String) imp_pars.getProperty("ALPHA_OFFSET"));
			alpha_lpf =                 Double.parseDouble((String) imp_pars.getProperty("ALPHA_LPF"));
			alpha_scale_avg =           Double.parseDouble((String) imp_pars.getProperty("ALPHA_SCALE_AVG"));
			alpha_push =                Double.parseDouble((String) imp_pars.getProperty("ALPHA_PUSH"));
			alpha_push_neutral =        Double.parseDouble((String) imp_pars.getProperty("ALPHA_PUSH_NEUTRAL"));
			alpha_push_center =         Double.parseDouble((String) imp_pars.getProperty("ALPHA_PUSH_CENTER"));
			
			if (imp_pars.getProperty("ALPHA_EN_HOLES") != null) alpha_en_holes = Boolean.parseBoolean((String) imp_pars.getProperty("ALPHA_EN_HOLES"));
			
			alpha_mm_hole =             Double.parseDouble((String) imp_pars.getProperty("ALPHA_MM_HOLE"));
			alpha_diff_hole =           Double.parseDouble((String) imp_pars.getProperty("ALPHA_DIFF_HOLE"));
			terr_lpf =                  Double.parseDouble((String) imp_pars.getProperty("TERR_LPF"));
			veget_lpf =                 Double.parseDouble((String) imp_pars.getProperty("VEGET_LPF"));
			terr_pull0 =                Double.parseDouble((String) imp_pars.getProperty("TERR_PULL0"));
			veget_pull0 =               Double.parseDouble((String) imp_pars.getProperty("VEGET_PULL0"));
			boost_parallax =            Double.parseDouble((String) imp_pars.getProperty("BOOST_PARALLAX"));
			um_sigma =                  Double.parseDouble((String) imp_pars.getProperty("UM_SIGMA"));
			um_weight =                 Double.parseDouble((String) imp_pars.getProperty("UM_WEIGHT"));
// TODO: build other data structures and make instance consistent, without it this does not allow reading from file			
			
		}
		return data;
	}

	public void combineSegments(
			VegetationSegment [] segments,
			int                  width,
			final boolean        render_open,      // render open areas (no vegetation offset)
			final boolean        render_no_alpha,  // render where no opacity is available
			final double         alpha_min,        // below - completely transparent vegetation
			final double         alpha_max,        // above - completely opaque
			final double         weight_opaque,    // render through completely opaque vegetation
			final double         boost_parallax,   // increase weights of scenes with high parallax relative to the reference one
			final double         max_parallax,			
			final int            num_exaggerate) { // show amplified difference from filtering
		final Thread[] threads = ImageDtt.newThreadArray(QuadCLT.THREADS_MAX);
		final AtomicInteger ai = new AtomicInteger(0);
		double ksigma = 1.0; 
		double min_sw = 1e-4;
		int full_length = full.width*full.height;
		int accum_indx = segments.length;
		double [][][] preview_data = new double [3][accum_indx+1][full_length];
		String [] top_titles = {"terrain","vegetation","alpha"};
		String [] titles = new String[accum_indx+1];
		for (int ns = 0; ns < segments.length; ns++) {
			Rectangle woi = segments[ns].woi;
			titles[ns] = woi.toString();
			int woi_length = woi.width * woi.height;  
			double [][] woi_tva = segments[ns].tva;
			for (int t = 0; t < 3; t++) {
				Arrays.fill(preview_data[t][ns], Double.NaN);
				for (int windx = 0; windx < woi_length; windx++) {
					int x = windx % woi.width + woi.x;
					int y = windx / woi.width + woi.y;
					int indx = x + y * full.width;
					preview_data[t][ns][indx] = woi_tva[t][windx];
				}
			}
		}
		titles[accum_indx] = "accum";
		double [][] sum_w =  new double [preview_data.length][full_length];
		double [][] sum_wd = new double [preview_data.length][full_length];
		for (int ns = 0; ns < segments.length; ns++) {
			Rectangle woi = segments[ns].woi;
			int woi_length = woi.width*woi.height;
			double [][] woi_tva = segments[ns].tva;			
			double [] wnd = overlapCosineWindow (woi, width);
			for (int windx = 0; windx < woi_length; windx++) {
				int x = windx % woi.width + woi.x;
				int y = windx / woi.width + woi.y;
				int indx = x + y * full.width;
				for (int t = 0; t < preview_data.length; t++) {
					if (!Double.isNaN(woi_tva[t][windx])) {
						double w = wnd[windx];
						sum_w [t][indx] += w;
						sum_wd[t][indx] += w * woi_tva[t][windx] ;
					}
				}				
			}
		}
		
		for (int t = 0; t < preview_data.length; t++) {
			for (int i = 0; i < full_length; i++) {
				if (sum_w[t][i] == 0) {
					preview_data[t][accum_indx][i] = Double.NaN;
				} else {
					preview_data[t][accum_indx][i] = sum_wd[t][i]/sum_w[t][i];
				}
			}
		}
		// Combine offsets per scene

		double [][] combined_offsets = new double [num_scenes][full_length];
		String [] titles_scenes = new String [num_scenes];
		for (int ithread = 0; ithread < threads.length; ithread++) {
			threads[ithread] = new Thread() {
				public void run() {
					double [] sw =  new double [full_length];
					for (int nscene = ai.getAndIncrement(); nscene < num_scenes; nscene = ai.getAndIncrement()) {
						titles_scenes[nscene] = "scene "+nscene;
						double [] swd = combined_offsets[nscene];
						Arrays.fill(sw, 0);
						for (int ns = 0; ns < segments.length; ns++) {
							Rectangle woi = segments[ns].woi;
							int woi_length = woi.width*woi.height;
							double scene_offset = segments[ns].scene_offsets[nscene];
							if (!Double.isNaN(scene_offset)) {
								double [] wnd = overlapCosineWindow (woi, width);
								for (int windx = 0; windx < woi_length; windx++) {
									int x = windx % woi.width + woi.x;
									int y = windx / woi.width + woi.y;
									int indx = x + y * full.width;
									for (int t = 0; t < preview_data.length; t++) {
										double w = wnd[windx];
										sw [indx] += w;
										swd[indx] += w * scene_offset ;
									}				
								}
							}
						}
						(new DoubleGaussianBlur()).blurDouble(
								sw,          //
								full.width,
								full.height,
								ksigma*width,              // double sigmaX,
								ksigma*width,              // double sigmaY,
								0.01);                 // double accuracy)
						(new DoubleGaussianBlur()).blurDouble(
								swd,          //
								full.width,
								full.height,
								ksigma*width,              // double sigmaX,
								ksigma*width,              // double sigmaY,
								0.01);                 // double accuracy)

						for (int i = 0; i < full_length; i++) {
							if (sw[i] < min_sw) {
								swd[i] = Double.NaN;
							} else {
								swd[i] /= sw[i];
							}
						}
					}
				}
			};
		}		      
		ImageDtt.startAndJoin(threads);
		
		double [][] tva = {preview_data[0][accum_indx],preview_data[1][accum_indx],preview_data[2][accum_indx]};
		vegetationModel.setTVA(tva);

	//				new String[] {"full average", "filtered average"});
	
		String ref_scene = vegetationModel.reference_scene;
		String render_title = "render-ro_"+render_open+"-rna_"+render_no_alpha+"-amn"+alpha_min+"-amx"+alpha_max+"-wo"+weight_opaque+"-bp"+boost_parallax+".tiff"; 
		
		
		double [] render = renderWithAlpha(
				preview_data[2][accum_indx], // final double []   alpha,
				combined_offsets,            // final double [][] scene_offsets, // subtract if not null
				render_open,                 // final boolean     render_open,      // render open areas (no vegetation offset)
				render_no_alpha,             // final boolean     render_no_alpha,  // render where no opacity is available
				alpha_min,                   // final double      alpha_min,        // below - completely transparent vegetation
				alpha_max,                   // final double      alpha_max,        // above - completely opaque
				weight_opaque,               // final double      weight_opaque,    // render through completely opaque vegetation
				boost_parallax,              // final double      boost_parallax) {    // increase weights of scenes with high parallax relative to the reference one
				max_parallax);               // final double      max_parallax)
		
		// num_exaggerate
		double [][] result_renders = new double [1+num_exaggerate][];
		String []   result_titles = new String [result_renders.length];
		result_titles[0] = "full average";
		result_titles[1] = "filtered average";
		result_renders[0] = terrain_average;
		result_renders[1] = render;
		for (int n = 2; n <=  num_exaggerate; n++) {
			result_titles[n]="exaggerate x "+n;
			result_renders[n] = new double [full_length];
			for (int i = 0; i < full_length; i++) {
				result_renders[n][i] = terrain_average[i] + n * (render[i] - terrain_average[i]);
			}
		}
		
		ShowDoubleFloatArrays.showArrays(
				combined_offsets,
				full.width,
				full.height,
				true,
				ref_scene+"-scene_offsets",
				titles_scenes);
		
		ShowDoubleFloatArrays.showArraysHyperstack(
				preview_data,              // double[][][] pixels, 
				full.width,                // int          width, 
				ref_scene+"-preview_segments",        // String       title, "time_derivs-rt"+diff_time_rt+"-rxy"+diff_time_rxy,
				titles,                    // String []    titles, // all slices*frames titles or just slice titles or null
				top_titles,                // String []    frame_titles, // frame titles or null
				true);                     // boolean      show)
		
		ShowDoubleFloatArrays.showArrays(
				result_renders,
				full.width,
				full.height,
				true,
				ref_scene+"-"+render_title,
				result_titles);
		String [] synt_titles = {"render","synthetic", "synt_terrain", "synt vegetation","masked render", "render"};
		final double [][][] synt_data = new double [synt_titles.length][num_scenes][];
		final double      terrain_max = alpha_min;
		final double      vegetation_min = 0.5; // alpha_max;
		String synt_name = "synthetic-terr"+terrain_max+"-veg"+vegetation_min;
		synt_data[0] = terrain_rendered;
		synt_data[5] = terrain_rendered;
		double [][][] synt_tv= renderSynthetic(
				preview_data[2][accum_indx], // final double []   alpha,
				combined_offsets,            // final double [][] scene_offsets, // subtract if not null
				terrain_max,                 // final double      terrain_max,      // below - terrain is visible
				vegetation_min,              // final double      vegetation_min,   // above - vegetation is visible
				boost_parallax,              // final double      boost_parallax,   // increase weights of scenes with high parallax relative to the reference one
				max_parallax);               //final double      max_parallax) {
		synt_data[2] = synt_tv[0];
		synt_data[3] = synt_tv[1];
/*		
		for (int nscene = 0; nscene < num_scenes; nscene++) { // use threads
			synt_data[1][nscene] = synt_tv[0][nscene].clone(); 
			synt_data[4][nscene] = terrain_rendered[nscene].clone();
			double [] veg_scene = synt_tv[1][nscene];
			for (int i = 0; i < veg_scene.length; i++) {
				if (!Double.isNaN(veg_scene[i])) {
					synt_data[1][nscene][i] = veg_scene[i];
					synt_data[4][nscene][i] = Double.NaN;
				}
			}
		}
*/
		ai.set(0);
		for (int ithread = 0; ithread < threads.length; ithread++) {
			threads[ithread] = new Thread() {
				public void run() {
					for (int nscene = ai.getAndIncrement(); nscene < num_scenes; nscene = ai.getAndIncrement()) {
						synt_data[1][nscene] = synt_tv[0][nscene].clone(); 
						synt_data[4][nscene] = terrain_rendered[nscene].clone();
						double [] veg_scene = synt_tv[1][nscene];
						for (int i = 0; i < veg_scene.length; i++) {
							if (!Double.isNaN(veg_scene[i])) {
								synt_data[1][nscene][i] = veg_scene[i];
								synt_data[4][nscene][i] = Double.NaN;
							}
						}
						
					}
				}
			};
		}		      
		ImageDtt.startAndJoin(threads);
		
		
		
		
		ShowDoubleFloatArrays.showArraysHyperstack(
				synt_data,              // double[][][] pixels, 
				full.width,                // int          width, 
				ref_scene+"-"+synt_name,        // String       title, "time_derivs-rt"+diff_time_rt+"-rxy"+diff_time_rxy,
				titles_scenes,                    // String []    titles, // all slices*frames titles or just slice titles or null
				synt_titles,                // String []    frame_titles, // frame titles or null
				true);                     // boolean      show)
		
		
		
		return;
		
	}
	
	public double [][][] renderSynthetic(
			final double []   alpha,
			final double [][] scene_offsets, // subtract if not null
			final double      terrain_max,      // below - terrain is visible
			final double      vegetation_min,   // above - vegetation is visible
			final double      boost_parallax,   // increase weights of scenes with high parallax relative to the reference one
			final double      max_parallax) {			
		final int full_length = full.width*full.height;
		final double [][] tva = vegetationModel.getTVA();
		final double [] terrain_dflt = vegetationModel.terrain_average_render;
		final double [] vegetation_dflt = vegetationModel.vegetation_average_render;
		final int INDX_TERR = 0, INDX_VEG=1;
		final double [][][] synthetic = new double [2][num_scenes][full_length];
		for (int t = 0; t < synthetic.length; t++) {
			for (int nscene = 0; nscene < synthetic[t].length; nscene++) {
				Arrays.fill(synthetic[t][nscene], Double.NaN);
			}
		}
		final double [][] synthetic_terrain =    synthetic[INDX_TERR];
		final double [][] synthetic_vegetation = synthetic[INDX_VEG];
		final Thread[] threads = ImageDtt.newThreadArray(QuadCLT.THREADS_MAX);
		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 < num_scenes; nScene = ai.getAndIncrement()) {
						for (int npix = 0; npix < full_length; npix++) {
							int x = npix % full.width;
							int y = npix / full.width;
							double d0= 0;
							if (scene_offsets != null) {
								double offs = scene_offsets[nScene][npix];
								if (!Double.isNaN(offs)) d0 = offs;
							}
							double [] veg_xy = vegetation_offsets[nScene][npix];
							double terrain_pix = (Double.isNaN(tva[TVAO_TERRAIN][npix])) ? terrain_dflt[npix] : (tva[TVAO_TERRAIN][npix] + d0);  // added offset to adjusted only
							if (veg_xy == null) {
								synthetic_terrain[nScene][npix] = terrain_pix;
							}  else { 
								veg_xy = veg_xy.clone();
								if (diff_offsets) {
									veg_xy[0] += x;
									veg_xy[1] += y;
								}
								int x0 = (int) Math.floor(veg_xy[0]);
								int y0 = (int) Math.floor(veg_xy[1]);
								if (    full.contains(x0,  y0)   &&
										full.contains(x0+1,y0)   &&
										full.contains(x0,  y0+1) &&
										full.contains(x0+1,y0+1)) {
									double fx = veg_xy[0] - x0;
									double fy = veg_xy[1] - y0;
									double [] corn_w = {(1-fx)*(1-fy), fx*(1-fy), (1-fx)*fy, fx*fy};
									int veg_indx = x0+full.width*y0;
									int [] veg_indx4 = {veg_indx, veg_indx+1, veg_indx+full.width, veg_indx+full.width+1};
									double saw = 0, sawd = 0, sawv = 0;
									for (int i = 0; i < 4; i++) {
										double a = alpha[veg_indx4[i]];
										double v = tva[TVAO_VEGETATION][veg_indx4[i]];//vegetation_dflt
										if (Double.isNaN(v)) v = vegetation_dflt[veg_indx4[i]];
										if (!Double.isNaN(a)) {
											saw +=  corn_w[i]; 
											sawd += corn_w[i] * a; 
											sawv += corn_w[i] * v; 
										}
									}
									if (saw > 0) {
										sawd /= saw; // here it is interpolated alpha
										sawv /= saw;
										if (sawd <= terrain_max) {
											synthetic_terrain[nScene][npix] = terrain_pix;									
										}
										if (sawd >=  vegetation_min) {
											synthetic_vegetation[nScene][npix] = sawv + d0; // added offset										
										}
									} else { // no alpha
										synthetic_terrain[nScene][npix] =terrain_pix;
									}
								} else { // offset points outside of the image - unlikely
									synthetic_terrain[nScene][npix] = terrain_pix;
								}
							}
						}
					}
				}
			};
		}		      
		ImageDtt.startAndJoin(threads);
		return synthetic;
	}
	
	
	public double [] renderWithAlpha(
			final double []   alpha,
			final double [][] scene_offsets, // subtract if not null
			final boolean     render_open,      // render open areas (no vegetation offset)
			final boolean     render_no_alpha,  // render where no opacity is available
			final double      alpha_min,        // below - completely transparent vegetation
			final double      alpha_max,        // above - completely opaque
			final double      weight_opaque,    // render through completely opaque vegetation
			final double      boost_parallax,   // increase weights of scenes with high parallax relative to the reference one
			final double      max_parallax) {			
		final int full_length = full.width*full.height;
		final double [] render = new double [full_length];
		Arrays.fill(render, Double.NaN);
		final double []     scene_weights = setupSceneWeights(
				boost_parallax, // double boost_parallax)
				full,           // Rectangle woi)
				max_parallax);  //final double    ceil_parallax)				
		final Thread[] threads = ImageDtt.newThreadArray(QuadCLT.THREADS_MAX);
		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 < full_length; nPix = ai.getAndIncrement()) {
						int x = nPix % full.width;
						int y = nPix / full.width;
						double sw = 0, swd = 0;
						for (int nscene = 0; nscene < num_scenes; nscene++) if (!Double.isNaN(terrain_rendered[nscene][nPix])) { 
							double scene_weight = scene_weights[nscene];
							double [] veg_xy = vegetation_offsets[nscene][nPix];
							double terrain = terrain_rendered[nscene][nPix];
							if (scene_offsets != null) {
								double offs = scene_offsets[nscene][nPix];
								if (!Double.isNaN(offs)) {
									terrain -= offs;
								}
								
							}
							if (veg_xy == null) {
								if (render_open) {
									sw += scene_weight;
									swd += scene_weight * terrain;
								}
							} else { // veg_xy !== null
								veg_xy = veg_xy.clone();
								if (diff_offsets) {
									veg_xy[0] += x;
									veg_xy[1] += y;
								}
								int x0 = (int) Math.floor(veg_xy[0]);
								int y0 = (int) Math.floor(veg_xy[1]);
								if (    full.contains(x0,  y0)   &&
										full.contains(x0+1,y0)   &&
										full.contains(x0,  y0+1) &&
										full.contains(x0+1,y0+1)) {
									double fx = veg_xy[0] - x0;
									double fy = veg_xy[1] - y0;
									double [] corn_w = {(1-fx)*(1-fy), fx*(1-fy), (1-fx)*fy, fx*fy};
									int veg_indx = x0+full.width*y0;
									int [] veg_indx4 = {veg_indx, veg_indx+1, veg_indx+full.width, veg_indx+full.width+1};
									double saw = 0, sawd = 0;
									for (int i = 0; i < 4; i++) {
										double a = alpha[veg_indx4[i]];
										if (!Double.isNaN(a)) {
											saw +=  corn_w[i]; 
											sawd += corn_w[i] * a; 
										}
									}
									if (saw > 0) {
										sawd /= saw; // here it is interpolated alpha
										double k =  weight_opaque + (1- weight_opaque)*Math.min(1.0, Math.max(0, (alpha_max - sawd)/(alpha_max-alpha_min)));
										sw += scene_weight * k;
										swd += scene_weight * k * terrain;
									} else {
										if (render_no_alpha) { // include no-alpha data is available (TODO: reduce weight?)
											sw += scene_weight;
											swd += scene_weight * terrain;
										}
									}
								} else if (render_open) { // offset points outside of the image - unlikely
									sw += scene_weight;
									swd += scene_weight * terrain;
								}
							}
						}
						if (sw == 0) {
							render[nPix] = Double.NaN;
						} else {
							render[nPix] = swd/sw;
						}
					}
				}
			};
		}		      
		ImageDtt.startAndJoin(threads);
		return render;
		
	}
	
	
	public static double [] overlapCosineWindow(
			Rectangle woi,
			int       width){
		if (width > woi.width/2) {
			width = woi.width/2;
		}
		if (width > woi.height/2) {
			width = woi.height/2;
		}
		double [] window = new double [woi.width*woi.height];
		double [] wnd_x = new double  [woi.width];
		double [] wnd_y = new double  [woi.height];
		Arrays.fill(wnd_x, 1.0);
		Arrays.fill(wnd_y, 1.0);
		for (int i = 0; i < width; i++) {
			double w = 0.5* (1 - Math.cos(Math.PI * (i + 0.5) / width));
			wnd_x[i] = w;
			wnd_x[woi.width - i - 1] = w;
			wnd_y[i] = wnd_x[i];
			wnd_y[woi.height - i - 1] = w;
		}
		int indx = 0;
		for (int y = 0; y < woi.height; y++) {
			for (int x = 0; x < woi.width; x++) {
				window[indx++] = wnd_x[x] * wnd_y[y];
			}
		}
		return window;
	}
	
	
	
	public VegetationSegment [] readAllSegments(
			String dir_path) {
		
		FileFilter parFileFilter = new FileFilter() 
		{
			//Override accept method
			public boolean accept(File file) {

				//if the file extension is .log return true, else false
				if (file.isFile() && file.getName().endsWith(PAR_EXT)) {
					return true;
				}
				return false;
			}
		};
		File dir = new File(dir_path);
		File [] par_files = dir.listFiles(parFileFilter);
		VegetationSegment [] segments = new VegetationSegment [par_files.length];
		for (int n = 0; n < par_files.length; n++) {
			Rectangle woi = new Rectangle();
			double [][] tvao = restoreParametersFile( // already trimmed to actual lengths FIXME: Not finished for real import ! 
					par_files[n].getPath(), //  String    path,
					woi); // Rectangle file_woi)
			segments[n] = new VegetationSegment(
					woi, // Rectangle   woi,
					tvao[TVAO_SCENE_OFFSET], // datadouble []   scene_offsets,
					new double [][] {tvao[0],tvao[1],tvao[2]}); // double [][]  tva) {} {}
		}
		// sort in line-scan order
		Arrays.sort(segments, new Comparator<VegetationSegment>() {
			@Override
			public int compare(VegetationSegment lhs, VegetationSegment rhs) {
				return (rhs.woi.y > lhs.woi.y) ? -1 : (rhs.woi.y < lhs.woi.y) ? 1 :
					((rhs.woi.x > lhs.woi.x) ? -1 : (rhs.woi.x < lhs.woi.x) ? 1 : 0); // increasing
			}
		});
		
		
		
		
		return segments;
	}
	
	
	public void readParametersFromImage(
			String    path,
			double [] vector,
			int       gap) {
		int [] indices = getParamDebugIndices (gap);
		ImagePlus imp_pars = new ImagePlus (path);
		if (imp_pars.getWidth()==0) {
			throw new IllegalArgumentException("Could not read "+path);
		}
		int woi_width3=woi.width*3 + 2 * gap;
		int height = indices.length/woi_width3;
		if ((woi_width3 != imp_pars.getWidth()) || (height != imp_pars.getHeight())) {
			throw new IllegalArgumentException("Read image size does not match required: ("+imp_pars.getWidth()+"x"+imp_pars.getHeight()+
					") != ("+woi_width3+"x"+height+")");
		}
        // float [] pixels = (float[]) imp_pars.getProcessor().getPixels();

		int num_slices = imp_pars.getStack().getSize();
		float [] pixels = (float[]) imp_pars.getStack().getPixels(num_slices); // last slice
		
		for (int i = 0; i < indices.length; i++) {
			if (indices[i] >= 0) {
				double d = pixels[i];
				// scale alpha for the same image
				int w = i % woi_width3;
				int h = i / woi_width3;
				if ((w > 2 *( woi.width+ gap)) && (h < woi.height))  {
					d = (d- debug_alpha_scales[0])/(debug_alpha_scales[1] - debug_alpha_scales[0]);
				}
				vector[indices[i]] = d;
			}
		}
		from_file = true;
		return;
	}
	
	
	
	
	
	public double debugDerivs (
			double [] vector,
			double    delta,
			int       debugLevel,
			boolean   show_img) {
		double  [][] jt_delta =  getFxDerivsDelta (
				vector, // double []         vector,
				delta,  // final double      delta,
				debugLevel); // final int         debug_level)
		double [][] jt = new double [jt_delta.length][];
		getFxDerivs(
				vector,      // final double []   vector,
				jt,          // final double [][] jt, // should be null or initialized with [vector.length][]
				debugLevel); // final int         debug_level)
		double [][] jt_diff = new double [jt_delta.length][jt_delta[0].length];
		double max_err=0;
		for (int n = 0; n < jt_diff.length; n++) {
			for (int w = 0; w < jt_diff[0].length; w++) {
				jt_diff[n][w] = jt[n][w] - jt_delta[n][w];
				if (Math.abs(jt_diff[n][w]) > max_err) {
					System.out.println("debugDerivs(): n="+n+", w = "+w +", diff="+jt_diff[n][w]);
				}
				max_err = Math.max(max_err, Math.abs(jt_diff[n][w]));
			}
		}
		System.out.println("delta = "+delta+", max_err = "+max_err);
		// think of visualization
		/**/
		if (show_img) {
			String [] frame_titles = {"jt","jt_delta", "jt_diff"}; 
			double [][][] debug_img = new double [frame_titles.length][][];
		}
		/**/
		return max_err;
	}
	
	
	private void setupWeights( // after setupParametersIndices
			final double [] scene_weights,
			final double reg_weights) {
//		int extra_samples = num_pars_vegetation_alpha; // in the future may be more regularization
		int extra_samples = 0;
		// using >=0 no use 0 as NOP but reserve space, <0 - do not reserve space
		
		//(alpha_loss > 0)
		if ((alpha_loss > 0) || (alpha_push > 0)) extra_samples+= num_pars_vegetation_alpha; // need to split loss (always positive) from alpha_lpf
		if (alpha_lpf >= 0) extra_samples+=	num_pars_vegetation_alpha;
		if (terr_lpf >= 0)  extra_samples+=	num_pars_terrain;
		if (veget_lpf >= 0) extra_samples+=	num_pars_vegetation;
		
		double reg_sample_weight = reg_weights/extra_samples; // weight of each regularization sample
		final double [] sw =  (scene_weights != null) ? scene_weights : new double [num_scenes];
		if (scene_weights == null) {
			Arrays.fill (sw, 1.0);
		}
		weights = new double [data_source.length + extra_samples];
		double s = 0;
		for (int ny = 0; ny < data_source.length; ny++) {
			int nscene =   data_source[ny][0][0];
//			int indx =     data_source[ny][0][1];
			s += sw[nscene];
		}
		final double s0 = s;
		s *= (1+ reg_weights);
		final double k = 1.0/s;
		weight_pure = s0/s;
//		for (int i = 0; i < extra_samples; i++) {
//			s+=extra_weights;
//		}
		final Thread[] threads = ImageDtt.newThreadArray(QuadCLT.THREADS_MAX);
		final AtomicInteger ai = new AtomicInteger(0);
		for (int ithread = 0; ithread < threads.length; ithread++) {
			threads[ithread] = new Thread() {
				public void run() {
					for (int nw = ai.getAndIncrement(); nw < weights.length; nw = ai.getAndIncrement()) {
						if (nw  < data_source.length) {
							weights[nw] = sw[data_source[nw][0][0]] * k;
						} else {
							weights[nw] = reg_sample_weight;
						}
					}
				}
			};
		}		      
		ImageDtt.startAndJoin(threads);
	}
	
	private void setupYVector() {
//		double [] 
		y_vector = new double [data_source.length];
		final Thread[] threads = ImageDtt.newThreadArray(QuadCLT.THREADS_MAX);
		final AtomicInteger ai = new AtomicInteger(0);
		for (int ithread = 0; ithread < threads.length; ithread++) {
			threads[ithread] = new Thread() {
				public void run() {
					for (int ny = ai.getAndIncrement(); ny < data_source.length; ny = ai.getAndIncrement()) {
						int nscene =   data_source[ny][0][0];
						int indx =     data_source[ny][0][1];
						y_vector[ny] = terrain_rendered[nscene][indx];
					}
				}
			};
		}		      
		ImageDtt.startAndJoin(threads);
		return;
	}
	
	@SuppressWarnings("unused")
	private int [][] getAlphaNeighbors(){
		final int [][] alpha_neibs = new int[num_pars_vegetation_alpha][4];
		final Thread[] threads = ImageDtt.newThreadArray(QuadCLT.THREADS_MAX);
		final AtomicInteger ai = new AtomicInteger(0);
		
		for (int ithread = 0; ithread < threads.length; ithread++) {
			threads[ithread] = new Thread() {
				TileNeibs tn = new TileNeibs(full.width, full.height);
				public void run() {
					for (int i = ai.getAndIncrement(); i < num_pars_vegetation_alpha; i = ai.getAndIncrement()) {
						int pindx = ind_pars_vegetation_alpha + i; // full parameter index
						int indx = par_rindex[pindx][1]; // full pixel index
						for (int dir4 = 0; dir4 < 4; dir4++) { // ortho only
							int dir = 2 * dir4;
							int nindx = tn.getNeibIndex(indx, dir);
							if (nindx < 0) {
								alpha_neibs[i][dir4] = -1;
							} else if (par_index[TVAO_VEGETATION_ALPHA][nindx] >= 0) {
								alpha_neibs[i][dir4] = par_index[TVAO_VEGETATION_ALPHA][nindx];
							} else { // pull to fixed alpha value
								alpha_neibs[i][dir4] = -2 -indx; // OFFSET by 2 !
							}
						}
					}
				}
			};
		}		      
		ImageDtt.startAndJoin(threads);
		//ind_pars_vegetation_alpha
		return alpha_neibs;
	}


	private int [][] getNeighbors(
			final int tvao,        // TVAO_VEGETATION_ALPHA
			final int ind_samples, // ind_pars_vegetation_alpha
			final int num_samples  // num_pars_vegetation_alpha
			){
		final int [][] alpha_neibs = new int[num_samples][4];
		final Thread[] threads = ImageDtt.newThreadArray(QuadCLT.THREADS_MAX);
		final AtomicInteger ai = new AtomicInteger(0);
		
		for (int ithread = 0; ithread < threads.length; ithread++) {
			threads[ithread] = new Thread() {
				TileNeibs tn = new TileNeibs(full.width, full.height);
				public void run() {
					for (int i = ai.getAndIncrement(); i < num_samples; i = ai.getAndIncrement()) {
						int pindx = ind_samples + i; // full parameter index
						int indx = par_rindex[pindx][1]; // full pixel index
						for (int dir4 = 0; dir4 < 4; dir4++) { // ortho only
							int dir = 2 * dir4;
							int nindx = tn.getNeibIndex(indx, dir);
							if (nindx < 0) {
								alpha_neibs[i][dir4] = -1;
							} else if (par_index[tvao][nindx] >= 0) {
								alpha_neibs[i][dir4] = par_index[tvao][nindx];
							} else { // pull to fixed alpha value
								alpha_neibs[i][dir4] = -2 -indx; // OFFSET by 2 !
							}
						}
					}
				}
			};
		}		      
		ImageDtt.startAndJoin(threads);
		//ind_pars_vegetation_alpha
		return alpha_neibs;
	}

	
	
	private void setupDataSource(int min_samples_scene) {
		final int woi_length = woi.width*woi.height;
		final int [][][][] data_src = new int [num_scenes][woi_length][][];
		final double [][][] corn_w = new double [num_scenes][woi_length][]; // null where no vegetation
		final int [] scene_samples = new int [num_scenes];
		final Thread[] threads = ImageDtt.newThreadArray(QuadCLT.THREADS_MAX);
		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 < num_scenes; nScene = ai.getAndIncrement()) {
						for (int drow = 0; drow < woi.height; drow++) {
							int row = woi.y + drow;
							for (int dcol = 0; dcol < woi.width; dcol++) {
								int col = woi.x + dcol;	
								int windx =dcol + drow * woi.width;
								int indx = row*full.width + col;
								if (!Double.isNaN(terrain_rendered[nScene][indx]) && (par_index[TVAO_TERRAIN][indx] >= 0)) { //
									scene_samples[nScene]++;
									data_src[nScene][windx] = new int[3][];
									// maybe use windx instead of indx below?
//									data_src[nScene][windx][0] = new int [] {nScene, indx, par_index[TVAO_TERRAIN][indx],par_index[TVAO_SCENE_OFFSET][nScene]};
									data_src[nScene][windx][0] = new int [] {nScene, indx, par_index[TVAO_TERRAIN][indx],nScene};
									// next 3 not needed - they are already nulls
									data_src[nScene][windx][1] = null;
									data_src[nScene][windx][2] = null;
									corn_w[nScene][windx] = null;
									double [] veg_xy = vegetation_offsets[nScene][indx];
									if (veg_xy != null) {
										veg_xy = veg_xy.clone();
										if (diff_offsets) {
											veg_xy[0] += col;
											veg_xy[1] += row;
										}
										int x0 = (int) Math.floor(veg_xy[0]);
										int y0 = (int) Math.floor(veg_xy[1]);
										if (    full.contains(x0,  y0)   &&
												full.contains(x0+1,y0)   &&
												full.contains(x0,  y0+1) &&
												full.contains(x0+1,y0+1)) {

											double fx = veg_xy[0] - x0;
											double fy = veg_xy[1] - y0;
											corn_w[nScene][windx] = new double []{(1-fx)*(1-fy), fx*(1-fy), (1-fx)*fy, fx*fy};
											data_src[nScene][windx][1] = new int [4];
											data_src[nScene][windx][2] = new int [4];

											int veg_indx = x0+full.width*y0;
											int [] veg_indx4 = {veg_indx, veg_indx+1, veg_indx+full.width, veg_indx+full.width+1};
											for (int i = 0; i < 4; i++ ) {
												data_src[nScene][windx][1][i] = (par_index[TVAO_VEGETATION][veg_indx4[i]] >= 0)?
														par_index[TVAO_VEGETATION][veg_indx4[i]] : (-1-veg_indx4[i]);
												data_src[nScene][windx][2][i] = (par_index[TVAO_VEGETATION_ALPHA][veg_indx4[i]] >= 0)?
														par_index[TVAO_VEGETATION_ALPHA][veg_indx4[i]] : (-1-veg_indx4[i]);
											}
										}
									}
								}
							}
						}
					}
				}
			};
		}		      
		ImageDtt.startAndJoin(threads);
		// now condense removing nulls in data_src (cornw may still have nulls)
		int num_samples = 0;
		used_scenes = new boolean [num_scenes];
		final int [] used_scene_indices = new int [num_scenes];
		int num_used_scenes = 0;
		for (int nscene = 0; nscene < num_scenes; nscene++) {
			int num_prev = num_samples;
			int defined = scene_samples[nscene];
			used_scene_indices[nscene] = num_used_scenes;
			if (defined >= min_samples_scene) {
				used_scenes[nscene] = true;
				num_samples+= scene_samples[nscene];
				scene_samples[nscene] = num_prev; // start index
				num_used_scenes++;
			}

		}
		/*
		 * 
		used_scenes_indices = new int [num_used_scenes];
		int indx = 0;
		for (int nscene = 0; nscene < num_scenes; nscene++) {
			if (used_scenes[nscene]) {
				used_scenes_indices[indx++] = nscene;
			}
		}
		*/
		data_source = new int [num_samples][][];
		corners_weights = new double[num_samples][];
		ai.set(0);
		for (int ithread = 0; ithread < threads.length; ithread++) {
			threads[ithread] = new Thread() {
				public void run() {
					for (int nScene = ai.getAndIncrement(); nScene < num_scenes; nScene = ai.getAndIncrement()) if (used_scenes[nScene]){
						int out_indx = scene_samples[nScene];
						for (int windx = 0; windx < woi_length; windx++) {
							if (data_src[nScene][windx] != null) {
								corners_weights[out_indx] =   corn_w[nScene][windx];
								int [][] dsrc = data_src[nScene][windx];
								dsrc[0][3] = ind_pars_scenes + used_scene_indices[dsrc[0][3]]; // replace scene number with the corresponding parameter index
								data_source    [out_indx++] = dsrc; // data_src[nScene][windx];
							}
						}
					}
				}
			};
		}		      
		ImageDtt.startAndJoin(threads);
		return;
	}
	
	private void setupParametersVector(
			double default_alpha) { // areas where both terrain and vegetation are available
		for (int i = 0; i < tvao[TVAO_VEGETATION_ALPHA].length; i++) {
			if (!Double.isNaN(tvao[TVAO_VEGETATION][i])) {
				tvao[TVAO_VEGETATION_ALPHA][i] = default_alpha;
			}
		}
		for (int drow = 0; drow < woi.height; drow++) {
			int row = woi.y + drow;
			for (int dcol = 0; dcol < woi.width; dcol++) {
				int col = woi.x + dcol;	
//				int windx =dcol + drow * woi.width;
				int indx = row*full.width + col;
				if (par_index[TVAO_TERRAIN][indx] >= 0) {
					parameters_vector[par_index[TVAO_TERRAIN][indx]] =    tvao[TVAO_TERRAIN][indx];
				}
				if (par_index[TVAO_VEGETATION][indx] >= 0) {
					parameters_vector[par_index[TVAO_VEGETATION][indx]] = tvao[TVAO_VEGETATION][indx];
				}
				if (par_index[TVAO_VEGETATION_ALPHA][indx] >= 0) {
					parameters_vector[par_index[TVAO_VEGETATION_ALPHA][indx]] = default_alpha;
				}
			}
		}
		for (int i = 0; i <  par_index[TVAO_SCENE_OFFSET].length; i++) if (par_index[TVAO_SCENE_OFFSET][i] >= 0){
			parameters_vector[par_index[TVAO_SCENE_OFFSET][i]]= tvao[TVAO_SCENE_OFFSET][i];
		}
	}
	
	
	private void setupParametersIndices(boolean [][] valid_woi) {
		par_index = new int [4][];
		par_index[TVAO_TERRAIN] =          new int [image_length];
		par_index[TVAO_VEGETATION] =       new int [image_length];
		par_index[TVAO_VEGETATION_ALPHA] = new int [image_length];
		par_index[TVAO_SCENE_OFFSET] =     new int [num_scenes];
		if (valid_woi == null) {
			valid_woi = new boolean [2][woi.width*woi.height];
			Arrays.fill(valid_woi[0],true);
			Arrays.fill(valid_woi[1],true);
		}
		int max_pars = 0;
		for (int i = 0; i < par_index.length; i++) {
			max_pars += par_index[i].length;
		}
		int [][] par_rindex = new int [max_pars][2];
		
		Arrays.fill(par_index[TVAO_TERRAIN],          -1);
		Arrays.fill(par_index[TVAO_VEGETATION],       -1);
		Arrays.fill(par_index[TVAO_VEGETATION_ALPHA], -1);
		int par_num = 0;
		ind_pars_terrain = 0;
		for (int drow = 0; drow < woi.height; drow++) {
			int row = woi.y + drow;
			for (int dcol = 0; dcol < woi.width; dcol++) {
				int col = woi.x + dcol;	
				int windx =dcol + drow * woi.width;
				if (valid_woi[0][windx]) {
					int indx = row*full.width + col;
					par_rindex[par_num][0] = 0;
					par_rindex[par_num][1] = indx;
					par_index[TVAO_TERRAIN][indx] = par_num++;
				}
			}
		}
		num_pars_terrain =    par_num;
		ind_pars_vegetation = par_num;
		for (int drow = 0; drow < woi.height; drow++) {
			int row = woi.y + drow;
			for (int dcol = 0; dcol < woi.width; dcol++) {
				int col = woi.x + dcol;	
				int windx =dcol + drow * woi.width;
				if (valid_woi[1][windx]) {
					int indx = row*full.width + col;
					par_rindex[par_num][0] = 1;
					par_rindex[par_num][1] = indx;
					par_index[TVAO_VEGETATION][indx] = par_num++;
				}
			}
		}
		ind_pars_vegetation_alpha = par_num;
		num_pars_vegetation = par_num - ind_pars_vegetation;
		for (int drow = 0; drow < woi.height; drow++) {
			int row = woi.y + drow;
			for (int dcol = 0; dcol < woi.width; dcol++) {
				int col = woi.x + dcol;	
				int windx =dcol + drow * woi.width;
				if (valid_woi[1][windx]) {
					int indx = row*full.width + col;
					par_rindex[par_num][0] = 2;
					par_rindex[par_num][1] = indx;
					par_index[TVAO_VEGETATION_ALPHA][indx] = par_num++;
				}
			}
		}
		num_pars_vegetation_alpha = par_num - ind_pars_vegetation_alpha; // normally should be == num_pars_vegetation
//		int num_pars_pixel = par_num;
		ind_pars_scenes =    par_num;
		
		
		for (int i = 0; i < par_index[TVAO_SCENE_OFFSET].length; i++) {
			par_rindex[par_num][0] = 3;
			par_rindex[par_num][1] = i;
			par_index[TVAO_SCENE_OFFSET][i] = par_num++;
		}
		// temporarily assign full rindex, will finalize in finishParametersIndices()
		this.par_rindex = par_rindex;
		/*
		
		this.par_rindex = new int [par_num][2];
		System.arraycopy(par_rindex, 0, this.par_rindex, 0, par_num);
		num_pars_scenes = par_num - ind_pars_scenes;
		parameters_vector = new double [par_num];
		*/
	}
	
	private void finishParametersIndices() {
		int par_num = ind_pars_scenes;
		int [][] par_rindex = this.par_rindex;
		for (int i = 0; i < par_index[TVAO_SCENE_OFFSET].length; i++) {
			if (used_scenes[i]) {
				par_rindex[par_num][0] = 3;
				par_rindex[par_num][1] = i;
				par_index[TVAO_SCENE_OFFSET][i] = par_num++;
			} else {
				par_index[TVAO_SCENE_OFFSET][i] = -1;
			}
		}		
		this.par_rindex = new int [par_num][2];
		System.arraycopy(par_rindex, 0, this.par_rindex, 0, par_num);
		num_pars_scenes = par_num - ind_pars_scenes;
		parameters_vector = new double [par_num];
	}

	
	
	private boolean [][] filterValidWoi (
			int     min_scenes_uses,
			int     min_scenes_used,
			int     min_valid_pixels,
			boolean debug_img){ // 4x?
		int max_iter = 100;
		boolean [][] valid_pix = new boolean [2][woi.width*woi.height];
		Arrays.fill(valid_pix[0],true);
		Arrays.fill(valid_pix[1],true);
		int [] num_valid = {valid_pix[0].length,valid_pix[1].length};
		int [] num_valid_prev =num_valid.clone();
		int [][] num_uses;
		for (int iter = 0; iter < max_iter; iter++) {
			num_uses = numVegUses(valid_pix);
			num_valid = new int[2];
			for (int i = 0; i < valid_pix[0].length; i++) {
				valid_pix[0][i] = num_uses[0][i] > min_scenes_uses;
				valid_pix[1][i] = num_uses[1][i] > min_scenes_used;
				if (valid_pix[0][i]) num_valid[0]++;
				if (valid_pix[1][i]) num_valid[1]++;
			}
			System.out.println("Iteration "+(iter+1)+", valid pixels: terrain "+num_valid[0]+", vegetation "+num_valid[1]+" (was "+num_valid_prev[0]+", "+num_valid_prev[1]+")");
			if ((num_valid[0] < min_valid_pixels) || (num_valid[1] < min_valid_pixels)) {
//				System.out.println("Too few valid pixels (< min_valid_pixels="+min_valid_pixels+").");
				return null;
			}
			
			
			if ((num_valid[0] == num_valid_prev[0]) && (num_valid[1] == num_valid_prev[1])) {
				break;
			}
			num_valid_prev =num_valid.clone();
			if (debug_img) {
				double [][] debug_uses = new double [4][num_uses[0].length];
				double max = 0;
				for (int i = 0; i < num_uses[0].length; i++) {
					debug_uses[0][i] = num_uses[0][i];
					debug_uses[1][i] = num_uses[1][i] *0.25;
					max = Math.max(max, debug_uses[0][i]);
					max = Math.max(max, debug_uses[1][i]);
				}
				for (int i = 0; i < num_uses[0].length; i++) {
					debug_uses[2][i] = valid_pix[0][i]? max:0;
					debug_uses[3][i] = valid_pix[1][i]? max:0;
				}
			ShowDoubleFloatArrays.showArrays(
					debug_uses,
					woi.width,
					woi.height,
					true,
					"number_of_uses_valid-"+iter+".tiff",
					new String[] {"uses_veg","veg_used","valid_terr","valid_veg"});
			}
			
		}
		
		return valid_pix; // [0] - valid terrain for adjustment, [1] valid vegetation
	}
	
	/**
	 * In how many scenes this pixel depends on vegetation in all 4 corners around it
	 * @return {number of scenes where this pixel depends on vegetation, 4x number of scenes where this vegetation pixel influences composite image.
	 */
	private int [][] numVegUses(
			boolean [][] valid_vegetation) { // 0 this pixel has adjustable terrain (depends on enough vegetation), 1 - has adjustable vegetation
		final AtomicInteger [] auses_veg = new AtomicInteger[woi.width*woi.height]; // this pixel depends on vegetation
		final AtomicInteger [] aused_veg = new AtomicInteger[woi.width*woi.height]; // this vegetation pixel influences composite image
		for (int i = 0; i < auses_veg.length; i++) {
			auses_veg[i] = new AtomicInteger(0);
			aused_veg[i] = new AtomicInteger(0);
		}
		final Thread[] threads = ImageDtt.newThreadArray(QuadCLT.THREADS_MAX);
		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 < num_scenes; nScene = ai.getAndIncrement()) {
						for (int drow = 0; drow < woi.height; drow++) {
							int row = woi.y + drow;
							for (int dcol = 0; dcol < woi.width; dcol++) {
								int col = woi.x + dcol;	
								int windx =dcol + drow * woi.width;
								int indx = row*full.width + col;
								if (!Double.isNaN(terrain_rendered[nScene][indx]) && ((valid_vegetation== null) || valid_vegetation[0][windx])) {
//									int indx = row*full.width + col;
									double [] veg_xy = vegetation_offsets[nScene][indx];
									if (veg_xy != null) {
										veg_xy = veg_xy.clone();
										if (diff_offsets) {
											veg_xy[0] += col;
											veg_xy[1] += row;
										}
										int x0 = (int) Math.floor(veg_xy[0]);
										int y0 = (int) Math.floor(veg_xy[1]);
										if (woi.contains(x0, y0) && woi.contains(x0+1, y0) && woi.contains(x0, y0+1) && woi.contains(x0+1,y0+1)) {
											if     ((vegetation_offsets[nScene][indx + 1] != null) &&
													(vegetation_offsets[nScene][indx + full.width] != null) &&
													(vegetation_offsets[nScene][indx + full.width + 1] != null)) {
												int veg_dindx0 = (x0-woi.x) + (y0-woi.y)*woi.width;
												if ((valid_vegetation== null) ||
														(		valid_vegetation[1][veg_dindx0                ] &&
																valid_vegetation[1][veg_dindx0 +             1] &&
																valid_vegetation[1][veg_dindx0 + woi.width    ] &&
																valid_vegetation[1][veg_dindx0 + woi.width + 1])) {
													auses_veg[windx].getAndIncrement();
													aused_veg[veg_dindx0                ].getAndIncrement();
													aused_veg[veg_dindx0 +             1].getAndIncrement();
													aused_veg[veg_dindx0 + woi.width    ].getAndIncrement();
													aused_veg[veg_dindx0 + woi.width + 1].getAndIncrement();
												}
											}
										}
									}
								}
							}
						}
					}
				}
			};
		}		      
		ImageDtt.startAndJoin(threads);
		int [][] num_uses = new int [2][auses_veg.length];
		for (int i = 0; i < num_uses[0].length; i++) {
			num_uses[0][i] = auses_veg[i].get();
			num_uses[1][i] = aused_veg[i].get();
		}
		return num_uses;
	}

	
	
	
	
}
