import nom.tam.fits.*;
import nom.tam.util.*;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;

public class CovarianceBrowser {
    static String version = "18 June 2004";

    static ReducedData arrayView;
    static double[][] covar;
    static int n[][];

    static int pixel = 0;

    public static void main(String[] args) {
	try {
	    Fits fits = new Fits(new File(args[0]));

	    try { covar = (double[][]) fits.getHDU(0).getData().getData(); }
	    catch(ClassCastException e) { 
		float[][] fcovar = (float[][]) fits.getHDU(0).getData().getData(); 
		covar = new double[fcovar.length][fcovar.length];
		for(int i=0; i<fcovar.length; i++) for(int j=0; j<fcovar.length; j++) covar[i][j] = fcovar[i][j];
	    }
		
	    n = (int[][]) fits.getHDU(1).getData().getData();

	    arrayView = new ReducedData();
	    arrayView.altaz = true;
	    arrayView.dRA0 = 1.0 * Unit.ArcSECOND;
	    arrayView.dDEC0 = 1.0 * Unit.ArcSECOND;
	    arrayView.delta = 1.0 * Unit.ArcSECOND;
	    arrayView.setSize(32,12);
	    arrayView.colorSchemes = new Vector();
	    arrayView.colorSchemes.add(new GreyScale());
	    arrayView.colorSchemes.add(new CameraLike());
	    arrayView.displayUnit = new DisplayUnit(0.01, "%");
	    
	    arrayView.show();

	    arrayView.frame.addKeyListener(new KeyAdapter() {
		    public void keyPressed(KeyEvent evt) {
			if(evt.getKeyCode() == KeyEvent.VK_RIGHT) { pixel++; }
			else if(evt.getKeyCode() == KeyEvent.VK_LEFT) { pixel--; }
			else if(evt.getKeyCode() == KeyEvent.VK_UP) { pixel += 32; }
			else if(evt.getKeyCode() == KeyEvent.VK_DOWN) { pixel -= 32; }
			else return;
			
			pixel %= 384;
			if(pixel < 0) pixel += 384;

			pick(pixel);
			arrayView.display.repaint();
		    }
		});
	    					     

	    if(args.length > 1) pick(Integer.parseInt(args[1]));	    
	    else pick(175);

	    arrayView.display.setDefaults();
	    arrayView.display.repaint();

	}
	catch(Exception e) { e.printStackTrace(); }

    }


    public static void pick(int bol) {
	pixel = bol;

	arrayView.clear();
	arrayView.clearRegions();
	arrayView.addRegion((bol%32+1)* Unit.ArcSECOND, (bol/32+1)*Unit.ArcSECOND, 0.5*Unit.ArcSECOND);
	arrayView.sourceName = "Pixel " + (bol/32+1) + "," + (bol%32+1);

	for(int row=0,b2=0; row<12; row++) for(int col=0; col<32; col++,b2++) {	    
	    if(n[bol][b2] > 0) {
		arrayView.clean[col][row] = covar[bol][b2];
		arrayView.weight[col][row] = n[bol][b2];
		arrayView.count[col][row] = n[bol][b2];
		arrayView.rms[col][row] = 1.0 / Math.sqrt(n[bol][b2]);
		arrayView.flag[col][row] = 0;
		arrayView.calcS2N();
	    }
	}
    }

    public static void versionInfo() {
	System.out.println("covarsee -- Visualization Tool for SHARC2 covariance files.");
	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: covarsee <covariance-file>");
	System.out.println();
	
	System.exit(1);
    }
}
    
	
