/*******************************************************************************
 * Copyright (c) 2014 Attila Kovacs <attila[AT]sigmyne.com>.
 * All rights reserved. 
 * 
 * This file is part of crushtools.
 * 
 *     crushtools is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     crushtools is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 * 
 *     You should have received a copy of the GNU General Public License
 *     along with crushtools.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Contributors:
 *     Attila Kovacs <attila[AT]sigmyne.com> - initial API and implementation
 ******************************************************************************/

package crushtools;

import java.io.*;
import java.util.*;

public class SkyMapDifferencer {
	static boolean show = false;

	public static void main(String args[]) {
		versionInfo();

		if(args.length == 0) usage();

		try {	    
			SkyMap[] image = new SkyMap[2];
			
			for(int i=0, k=0; i<args.length; i++) if(args[i].charAt(0) != '-') image[k++] = new SkyMap(args[i]);
			
			image[0].fileName = SkyMap.workPath + File.separator + image[0].sourceName + ".diff.fits";
			
	
			ImageTool.image = image[0];			
			for(int i=0; i<args.length; i++) {
				if(args[i].charAt(0) == '-') option(args[i]);
				else ImageTool.image = image[1];
			}
			
			
			SkyMap difference = difference(image[0], image[1]);
			difference.fileName = image[0].fileName;
			
			if(show) {	
				difference.displayUnit = ImageTool.image.getUnit("Jy/beam");
			
				difference.show();
			}
			else difference.write();
		}
		catch(Exception e) { e.printStackTrace(); }
	}

	public static SkyMap difference(SkyMap a, SkyMap b) {
		SkyMap images[] = new SkyMap[] { a, b };
		SkyMap difference = SkyMapCoadder.createCoadded(images, a.projection);	
		difference.clear();
		images = SkyMapCoadder.getIdenticalGridMaps(images, difference);
		directAdd(images[0], images[1], -1.0, difference);
		return difference;
	}
	
	public static void directAdd(SkyMap a, SkyMap b, double scaleb, SkyMap sum) {		
		// make sure same smoothing is applied to both images
		// make sure units are the same
		
		for(int i=0; i<sum.sizeX; i++) for(int j=0; j<sum.sizeY; j++) if(a.flag[i][j] == 0 && b.flag[i][j] == 0) {
			sum.signal[i][j] = a.signal[i][j] + scaleb * b.signal[i][j];
			sum.weight[i][j] = 1.0 / (1.0 / a.weight[i][j] + scaleb * scaleb / b.weight[i][j]);
			sum.count[i][j] = Math.max(a.count[i][j], b.count[i][j]);
			sum.flag[i][j] = 0;
		}
		
		sum.calc();
	}

	public static boolean option(String optionString) {
		StringTokenizer tokens = new StringTokenizer(optionString, "=,");

		String key = tokens.nextToken();

		if(key.equalsIgnoreCase("-help")) usage();
		else if(key.equalsIgnoreCase("-show")) show = true;
		else if(ImageTool.option(optionString)) return true;
		else return false;

		return true;

	}

	public static void versionInfo() {
		System.out.println("difference -- Image Differencing Tool for CRUSH maps and more.");
		System.out.println("              Author: Attila Kovacs <attila[AT]sigmyne.com>");
		System.out.println("              Version: " + SkyMap.version);
		System.out.println();
	}

	public static void usage() {
		System.out.println("Usage: difference [options] <filename1> [options] <filename2>");
		System.out.println();
		System.out.println("Options:");
		System.out.println();
		System.out.println("  All options of 'imagetool' are available. Additionally, 'difference'");
		System.out.println("  defines the following options:");
		System.out.println();
		System.out.println("    -show      Display difference image instead of writing.");
		System.out.println("    -help      Provides this help screen.");
		System.out.println();
		
		//ImageTool.optionsSummary();
		//System.out.println();

		System.exit(0);
	}
}
