import java.util.*;


public class SourceExtractor {
    static String version = "22 Jun 2005";
    static double confidenceLevel = 0.95;
    static ReducedData image;

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

	if(arg.length == 0) usage();
	    
	try {
	    image = new ReducedData(arg[arg.length-1]);

	    for(int i=0; i<arg.length-1; i++) option(arg[i]);
	    
	    image.displayUnit = image.janskyPerBeam;
	    image.extract(confidenceLevel);
	}
	catch(Exception e) {}
    }

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

	String key = tokens.nextToken();

	if(key.equalsIgnoreCase("-confidence")) confidenceLevel = Double.parseDouble(tokens.nextToken());
	else if(key.equalsIgnoreCase("-s2n")) {
	    double Q = 1.0 - ConfidenceCalculator.getConfidence(Double.parseDouble(tokens.nextToken()));
	    confidenceLevel = 1.0 - Q * (1.0 + image.countBeams());
	}
	else if(key.equalsIgnoreCase("-help")) usage();
	else return false;

	return true;    
    }

    public static void versionInfo() {
	System.out.println("detect -- Source Detection Utility for SHARC2 maps.");
	System.out.println("          Author: Attila Kovacs <attila@submm.caltech.edu>");
	System.out.println("          Version: " + version);
	System.out.println();
    }

    public static void usage() {
	System.out.println("usage: detect [options] <filename>");
	System.out.println();
	System.out.println("options:");
	System.out.println();
	System.out.println("    -confidence=  Confidence level for detections. (" + confidenceLevel + ")");
	System.out.println("    -s2n=         S/N level for detections.");
	System.out.println("    -help         Provides this help screen.");
	System.out.println();
	
	System.exit(0);
    }

}
