import java.io.*;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.*;

import javax.security.auth.login.LoginException;
import javax.servlet.*;
import javax.servlet.http.*;
import net.mar.RetrieverManager;
import net.mar.RetrieverMapper;

import org.apache.log4j.Logger;
import org.astrogrid.community.common.exception.CommunityIdentifierException;
import org.astrogrid.community.common.exception.CommunitySecurityException;
import org.astrogrid.community.common.exception.CommunityServiceException;
import org.astrogrid.community.common.ivorn.CommunityAccountIvornFactory;
import org.astrogrid.community.common.security.data.SecurityToken;
import org.astrogrid.community.resolver.*;
import org.astrogrid.community.resolver.exception.CommunityResolverException;
import org.astrogrid.common.*;
import org.astrogrid.config.Config;
import org.astrogrid.config.SimpleConfig;
import org.astrogrid.registry.RegistryException;
import org.astrogrid.store.Ivorn;

import uk.ac.roe.wfau.DatabaseConnection;
import uk.ac.roe.wfau.FitsWcs;
import uk.ac.starlink.ast.AstObject;
import uk.ac.starlink.ast.FitsChan;


public class Test extends HttpServlet {
  static Logger logger;
  int count=0;
  public void doGet(HttpServletRequest req, HttpServletResponse res)
                               throws ServletException, IOException {
//     res.
   res.setContentType("text/html");
   PrintWriter out = res.getWriter();
  	AstObject newao;
	FitsWcs newfw = new FitsWcs();
	FitsChan fc=new FitsChan();
	fc.putFits("NAXIS   = 2",true); 
    fc.putFits("NAXIS1  = "+"2099",true);
    fc.putFits("NAXIS2  = "+"2100",true);
    fc.putFits("CTYPE1  = '"+"RA---ZPN"+"'",true);
    fc.putFits("CTYPE2  = '"+"DEC--ZPN"+"'",true);
    fc.putFits("CRPIX1  = "+"5.3992046E+03",true);
    fc.putFits("CRVAL1  = "+"8.0745020E+00",true);
    //CRPIX1                                5.3992046E+03                                                         Pixel coordinate at ref point                                
    //CRPIX2                                6.8535361E+03                                                         Pixel coordinate at ref point                                
    //CRVAL1                                8.0745020E+00                                                         Coordinate value at ref pixel                                
    //CRVAL2                                -3.9260456E+01                                                        Coordinate value at ref pixel                                
    
    fc.putFits("CRPIX2  = "+"6.8535361E+03",true);
    fc.putFits("CRVAL2  = "+"-3.9260456E+01",true);
    /*
     * CD1_1                                 -5.9873040E-09                                                        WCS transform matrix element                                 
     * CD1_2                                 9.4715564E-05                                                         WCS transform matrix element                                 
     * CD2_1                                 -9.4739939E-05                                                        WCS transform matrix element                                 
     * CD2_2                                 1.2709141E-07                                                         WCS transform matrix element                                 
     * 
     */
    String CD1_1="CD1_1   = "+"-5.9873040E-09"+ " / from database";
    fc.putFits(CD1_1,true);
    String CD1_2="CD1_2   = "+"9.4715564E-05"+ " / from database";
    fc.putFits(CD1_2,true);
    String  CD2_1="CD2_1   = "+"-9.4739939E-05"+ " / from database";
    fc.putFits(CD2_1,true);
    String  CD2_2="CD2_2   = "+"1.2709141E-07"+ " / from database";
    fc.putFits(CD2_2,true);
    String PROJP1="PV2_1   = "+"1.0000000"+ " / from database";
    fc.putFits(PROJP1,true);
    String PROJP2="PV2_2   = "+"0.0000000"+ " / from database";
    fc.putFits(PROJP2,true);
    String PROJP3="PV2_3   = "+"42.000000"+ " / from database";
    fc.putFits(PROJP3,true);
    String PROJP4="PV2_4   = "+"0.0000000"+ " / from database";
    fc.putFits(PROJP4,true);
    String PROJP5="PV2_5   = "+"-10000.0"+ " / from database";
    fc.putFits(PROJP5,true);
    
//    String PROJP1="PROJP1  = "+"1.0000000"+ " / from database";
//    fc.putFits(PROJP1,true);
//    String PROJP3="PROJP3  = "+"42.00000"+ " / from database";
    fc.putFits(PROJP3,true);  	
    fc.putFits("PCOUNT  =                    0 / required keyword; must = 0                    ",true);
    fc.putFits("GCOUNT  =                    1 / required keyword; must = 1                    ",true);
    fc.putFits("CRUNIT1 = 'deg     '           / Unit of right ascension co-ordinates          ",true);
    fc.putFits("CRUNIT2 = 'deg     '           / Unit of declination co-ordinates              ",true);


    newao = newfw.setFits(fc);
    fc=null;
    double nx=newfw.getNX();
	double ny=newfw.getNY();
//	System.out.println(nx+" "+ny);
	//double [] xypix=newfw.getXPixYPix(267.0853641,6.035505);
	double [] xypix=newfw.getXPixYPix(237.7807101550903,-0.009315087007087119);
	//double [] xypix=newfw.getXPixYPix(237.7802458,-0.010822);
	 
	//System.out.println(xypix[0]+" "+xypix[1]);
	double [] radec=new double[2];
	int i=0;
	    for (int j=0; j<2073; j++){
	        radec=newfw.getRADec((double)i,(double)j);
	        out.println(radec[0]+" "+radec[1]+" "+i+" "+j);
	    }
  }

  void doThis(PrintWriter out){
      out.print("BASE");
  }
  protected void doPost(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException
  {
    // Data received by an HTTP POST is handled the same way
    // as data sent with a HTTP GET request... simply pass on to above...
    doGet(req, res);
    
  }; // END of doPost()

}
