// (C)2007 Attila Kovacs <attila@submm.caltech.edu>

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

import java.io.*;
import java.text.*;

public class FitsTest {

	public static void main(String[] args) {
		
		float[][] image = new float[80][40];
		
		for(int x=0; x<image.length; x++) image[x][20] = 1.0F;
		
		try {
			Fits fits = new Fits();
			BasicHDU hdu = Fits.makeHDU(image);
			Header header = hdu.getHeader();
		
			header.addLine(new HeaderCard("POSVAL", Math.PI, "A positive double value"));
			header.addLine(new HeaderCard("NEGVAL", -Math.PI, "A negative double value"));
			header.addLine(new HeaderCard("POSLRG", 1000.0 * Math.PI, "A largish positive double value"));
			header.addLine(new HeaderCard("NEGLRG", -1000.0 * Math.PI, "A largish negative double value"));
			header.addLine(new HeaderCard("POSLEXP", 1e8 * Math.PI, "A largish positive double value"));
			header.addLine(new HeaderCard("NEGLEXP", -1e8 * Math.PI, "A largish negative double value"));
			header.addLine(new HeaderCard("POSLEXP", 1e15 * Math.PI, "A largish positive double value"));
			header.addLine(new HeaderCard("NEGLEXP", -1e15 * Math.PI, "A largish negative double value"));
			header.addLine(new HeaderCard("POSSML", Math.PI/1000.0, "A smallish positive double value"));
			header.addLine(new HeaderCard("NEGSML", -Math.PI/1000.0, "A smallish negative double value"));
			header.addLine(new HeaderCard("POSSEXP", Math.PI*1e-8, "A positive double value in exponent form"));
			header.addLine(new HeaderCard("NEGSEXP", -Math.PI*1e-8, "A negative double value in exponent form"));
			header.addLine(new HeaderCard("POSSSEXP", Math.PI*1e-15, "A positive double value in exponent form"));
			header.addLine(new HeaderCard("NEGSSEXP", -Math.PI*1e-15, "A negative double value in exponent form"));
	
			
			/*
			Cursor cursor = header.iterator();
			cursor.setKey("POSVAL");
			cursor.remove();
			cursor.add(new HeaderCard("POSVAL", 1.0, "A positive double value"));
			cursor.setKey("POSVAL");
			cursor.remove();
			cursor.add(new HeaderCard("POSVAL", 2.0, "A positive double value"));
			*/
			
			
			fits.addHDU(hdu);
			fits.write(new BufferedDataOutputStream(new FileOutputStream("test.fits")));
			
			System.err.println("Written " + image.length + "x" + image[0].length);
			
			
			fits = new Fits(new File("test.fits"));	
			image = (float[][]) fits.getHDU(0).getData().getData();
			
			System.err.println("Read " + image.length + "x" + image[0].length);
			
			header = hdu.getHeader();
			Cursor cursor = header.iterator();
			
			cursor.setKey("POSVAL");
			System.err.println(((HeaderCard) cursor.next()).getKey());
				
		} 
		catch(Exception e) {	
			e.printStackTrace();
		}
	}
	
	 public static double fitsDouble(double value) {
			if(value < 0.0 && value > -1.0) {
			    DecimalFormat df = new DecimalFormat("0.0000000000000E0");
			    return Double.parseDouble(df.format(value));
			}
			else if(value > 0.0 && value < 1.0) {
			    DecimalFormat df = new DecimalFormat("0.00000000000000E0");
			    return Double.parseDouble(df.format(value));
			}
			else return value;
	 }
}
