//deprecated use uk.ac.roe.wfau version
package net.mar;

import java.io.*;
import nom.tam.util.*;
import nom.tam.fits.*;
import nom.tam.image.*;
import uk.ac.starlink.ast.*;
import uk.ac.starlink.util.*;
import uk.ac.starlink.ast.grf.*;
import uk.ac.starlink.ast.FitsChan.*;

public class FitsWcs
{
	public FitsWcs(){
}
  String FitsFile;
  double raDeg;
  double decDeg;
  AstObject  ao;
  int ext;
  double nx=0;
  double ny=0;
  HeaderCard hc;
  FitsChan fc;
public AstObject setFits(FitsChan fchan)
  {
	ao=null;
    fc=fchan;

try {
fc.clear("Card");
hc=new HeaderCard(fc.findFits("NAXIS1",true));
nx=Double.valueOf(hc.getValue()).doubleValue();
fc.clear("Card");
hc=new HeaderCard(fc.findFits("NAXIS2",true));
ny=Double.valueOf(hc.getValue()).doubleValue();
fc.clear("Card");

  ao=fc.read();
}
catch (Exception e){
	ao=null;
	nx=0;
	ny=0;
}

fc=null;

return ao;
}


  public AstObject setFits(String filepath, int extnum)
  {
	  ao=null;
	  System.out.println("taree");
    FitsFile=filepath;
    ext=extnum;
   // raDeg=ra;
  //  decDeg=dec;
    String cards="";
    FitsChan fc;

    Cursor c;
    HeaderCard hc;
    try {
    Fits f = new Fits(FitsFile);

	ImageHDU h= (ImageHDU) f.getHDU(ext);
	Header head=h.getHeader();
	System.out.println(head.getIntValue("NAXIS1"));
	int noc=  head.getNumberOfCards();
	System.out.println(noc);
	fc=new FitsChan();
	c = head.iterator();
	while (c.hasNext()) {
		HeaderCard card = (HeaderCard) c.next();
		//String key = card.getKey();
	//	String val = card.getValue();
	//	String comment = card.getComment();
	//	System.out.println(card.toString());
		fc.putFits(card.toString(),true );
		cards=cards+card.toString();

}
fc.clear("Card");
hc=new HeaderCard(fc.findFits("NAXIS1",true));
nx=Double.valueOf(hc.getValue()).doubleValue();
fc.clear("Card");
hc=new HeaderCard(fc.findFits("NAXIS2",true));
ny=Double.valueOf(hc.getValue()).doubleValue();
fc.clear("Card");

  ao=fc.read();

}
catch (Exception e){
	ao=null;
	nx=0;
	ny=0;
}

return ao;
}


public double getNX() {
	return nx;
}

public double getNY() {
	return ny;
}



public double[] runGetXYSize(){

return getXYSize(Math.round(nx/2),Math.round(ny/2));
}

public double[] runGetXYSize(double xCen, double yCen){

return getXYSize(xCen,yCen);
}
public double[] getXYSize(double centreX, double centreY){
	double xSize=0;
	double ySize=0;
	double [] size = new double[] {0.0,0.0};
	try {
	if (nx > 0 & ny > 0) {
	   double[] xPixCorners = new double[ 4 ];
	   double[] yPixCorners = new double[ 4 ];
/*	   xPixCorners[ 0 ] = Math.round(nx/2);
	   yPixCorners[ 0 ] = Math.round(ny/2);
	   xPixCorners[ 1 ] = Math.round(nx/2);
	   yPixCorners[ 1 ] = 1.0+Math.round(ny/2);
	   xPixCorners[ 2 ] = 1.0+Math.round(nx/2);
	   yPixCorners[ 2 ] = Math.round(ny/2);
       xPixCorners[ 3 ] = 1.0+Math.round(nx/2);
       yPixCorners[ 3 ] = 1.0+Math.round(ny/2);*/
 	   xPixCorners[ 0 ] = centreX;
	   yPixCorners[ 0 ] = centreY;
	   xPixCorners[ 1 ] = centreX;
	   yPixCorners[ 1 ] = 1.0+centreY;
	   xPixCorners[ 2 ] = 1.0+centreX;
	   yPixCorners[ 2 ] = centreY;
       xPixCorners[ 3 ] = 1.0+centreX;
       yPixCorners[ 3 ] = 1.0+centreY;
       FrameSet wcsmap = (FrameSet) ao;
       double[][] result = wcsmap.tran2( 4, xPixCorners, yPixCorners, true );
	   double[] xSkyCorner = result[ 0 ];
	   double[] ySkyCorner = result[ 1 ];
      double [] skyCorner0 = new double[] { xSkyCorner[ 0 ], ySkyCorner[ 0 ] };
      double [] skyCorner1 = new double[] { xSkyCorner[ 1 ], ySkyCorner[ 1 ] };
      double [] skyCorner2 = new double[] { xSkyCorner[ 2 ], ySkyCorner[ 2 ] };
      double [] skyCorner3 = new double[] { xSkyCorner[ 3 ], ySkyCorner[ 3 ] };

		size[0] = wcsmap.getFrame( wcsmap.AST__CURRENT )
                           .distance( skyCorner0, skyCorner2 );
        size[1] = wcsmap.getFrame( wcsmap.AST__CURRENT )
                           .distance( skyCorner0, skyCorner1 );

   }
}
catch (Exception e) {
	size[0]=0.0;
	size[1]=0.0;
	}
	return size;

}
public double[] getXPixYPix(double ra, double dec){
	FrameSet wcs ;
	double [] xypix = new double[] {0.0,0.0};
	try {
	wcs = (FrameSet) ao;
	double[] RAin =  new double[] {ra*Math.PI/180.0};
    double[] Decin = new double[] {dec*Math.PI/180.0};
    double[][] XYout = wcs.tran2( 1, RAin, Decin, false );
    xypix[0]=XYout[0][0];
    xypix[1]=XYout[1][0];
}
catch (Exception e) {
	xypix[0]=0.0;
	xypix[1]=0.0;
}
    return xypix;
}
public double[] getRADec(double xPix, double yPix){
	FrameSet wcs ;
	double [] longLat = new double[] {0.0,0.0};
	try {
	wcs = (FrameSet) ao;
	double[] Xin =  new double[] {xPix};
    double[] Yin = new double[] {yPix};
    double[][] longLatOut = wcs.tran2( 1, Xin, Yin, true );
    longLat[0]=longLatOut[0][0];
    longLat[1]=longLatOut[1][0];
}
catch (Exception e) {
	longLat[0]=0.0;
	longLat[1]=0.0;
}
    return longLat;
}

}
