/*
 * Created on 26-Sep-2006 - originally in uk.ac.roe.wfau
 * $Author: mar $
 * $Date: 2006-09-26 14:54:42 +0100 (Tue, 26 Sep 2006) $
 * $Locker$
 * $Revision: 394 $
 */
package wfau.coords;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

import net.mar.SLALIB;

/**
 * @author mar
 *
 * Class to provide coverage points (RA/Decs) of UKIDSSS
 * 
 */
public class EQ2Survey {
    static final double surveyCenterRa  =  185.0;
    static final double surveyCenterDec =   32.5;
    static final double deg2Rad=Math.PI/180.0;
    static final double rad2Deg=180.0/Math.PI;
    public static void main(String[] args) {
        double [] tp;
        BufferedWriter out;
        try {
            
        String survey="uds";
        String outFile="u:/"+survey+"Coverage.lis";
        out = new BufferedWriter(new FileWriter(outFile));
        
         
         double []  cGCSRA={56.75,50.5,130.0,266.5,67.5,82.25,242.5,56.25,66.75,186.25};
         double [] cGCSDec={24.116666667,48.6166667,19.666667,5.7166667,25.0,-2.6,-23.0,32.283333,15.8666667,26.1};
         double [] cGCSR={5.0,4.0,3.0,0.5,11.0,10.0,7.0,2.0,9.64,5};
         double [] cDXSRA={ 34.5,165.0,240.0,334.458};
         double [] cDXSDec={-5.0,57.0,57.0,0.4075};
         double [] cDXSSize={2.958,2.958,2.958,2.958}; 
         double [] cUDSRA={ 34.5};
         double [] cUDSDec={-5.0};
         double [] cUDSSize={0.9}; 
         double sRA=0.0;
         double eRA=360.0;
         double sDec=-45.0;
         double eDec=65;
        double decStep=0.25; //	0.5	;///5;///5;
        if (survey.equals("dxs")) {
        decStep=0.05;
        }
        if (survey.equals("uds")) {
            decStep=0.01;
            sRA=30.0;
            eRA=40.0;
            sDec=-7.0;
            eDec=0.0;
            }
        if (survey.equals("gcs")) {
            decStep=0.25;
            }
        double raStep;
        
        for (double d=sDec;d<=eDec;d=d+decStep) {
            raStep=decStep/(Math.cos(d*deg2Rad));
            //System.out.println(raStep+" "+decStep);
            for (double r=sRA;r<=eRA;r=r+raStep) {
                
                if (survey.equals("las")) {
                    tp=getEtaLambda(r,d);
                if (tp[0] > -60. && tp[0] < 53 && tp[1] > -36.25 && tp[1] < -16.25) {                
                out.write(r+" "+d+"\n");
                }
                if (tp[0] > -60. && tp[0] < 53 && tp[1] > 6.25 && tp[1] < 26.25) {
                  
                    out.write(r+" "+d+"\n");
                    }
                if ( ((r > 0 && r <60) || (r < 360 && r  > 335)) && Math.abs(d) < 1.25     ) {
                    out.write(r+" "+d+"\n");
                }
                }
                if (survey.equals("gps")) {
                    tp=SLALIB.EQGAL(r*deg2Rad,d*deg2Rad);
                    tp[0]=tp[0]*rad2Deg;
                    tp[1]=tp[1]*rad2Deg;
                    
                    //if (d <=60 && d > -15 && Math.abs(tp[1]) <=5. && ( (tp[0] >= 15 && tp[0] <=117) || (tp[0] >= 112 && tp[0] <=230)) ) {
                    if (Math.abs(tp[1]) <=5. && ( (tp[0] >= 15 && tp[0] <=107) || (tp[0] >= 142 && tp[0] <=230)) ) {
                        out.write(r+" "+d+" "+tp[0]+" "+tp[1]+"\n");                        
                    }
                    //if (d <=60 && d > -35 && Math.abs(tp[1]) <=2. && ((tp[0] > 358 && tp[0] <=360) || (tp[0] >= 0 && tp[0] <=17.5))) {
                    if ( Math.abs(tp[1]) <=2. && ((tp[0] > 358 && tp[0] <=360) || (tp[0] >= 0 && tp[0] <=15.5))) {
                        out.write(r+" "+d+" "+tp[0]+" "+tp[1]+"\n");
                    }
                    if (tp[1] <= 5 && tp[1] >= 0.0 && Math.abs(tp[0]) < 1) {
                        out.write(r+" "+d+" "+tp[0]+" "+tp[1]+"\n");
                    }
                    
                    if (SLALIB.DSEP(67.5*deg2Rad,25.0*deg2Rad,r*deg2Rad,d*deg2Rad) < 9.77*deg2Rad) {
                        out.write(r+" "+d+" "+tp[0]+" "+tp[1]+"\n");
                    }
                    
                }
                if (survey.equals("gcs")) {
  
                    for (int i=0;i<cGCSRA.length;i++) {
                        if (SLALIB.DSEP(cGCSRA[i]*deg2Rad,cGCSDec[i]*deg2Rad,r*deg2Rad,d*deg2Rad) < cGCSR[i]*deg2Rad) {
                            out.write(r+" "+d+"\n");
                        }
                    }
                    
                }
                if (survey.equals("dxs")) {
                    
                    for (int i=0;i<cDXSRA.length;i++) {
                        tp=SLALIB.DS2TP(r*deg2Rad,d*deg2Rad,cDXSRA[i]*deg2Rad,cDXSDec[i]*deg2Rad);
                        //if (Math.abs(d-cDXSDec[i])<= cDXSSize[i]/2 && Math.abs(tp[0])/deg2Rad <= cDXSSize[i]/2 && Math.abs(tp[1])/deg2Rad <= cDXSSize[i]/2 && tp[2]==0) {
                        if (Math.abs(d-cDXSDec[i])<= cDXSSize[i]/2 && Math.abs(r-cDXSRA[i])<= cDXSSize[i]/2){
                        out.write(r+" "+d+",1\n");
                        }
                        
                    }
                    
                }
                if (survey.equals("uds")) {
                    
                    for (int i=0;i<cUDSRA.length;i++) {
                        tp=SLALIB.DS2TP(r*deg2Rad,d*deg2Rad,cUDSRA[i]*deg2Rad,cUDSDec[i]*deg2Rad);
                        //if (Math.abs(d-cDXSDec[i])<= cDXSSize[i]/2 && Math.abs(tp[0])/deg2Rad <= cDXSSize[i]/2 && Math.abs(tp[1])/deg2Rad <= cDXSSize[i]/2 && tp[2]==0) {
                        if (Math.abs(d-cUDSDec[i])<= cUDSSize[i]/2 && Math.abs(r-cUDSRA[i])<= cUDSSize[i]/2){
                        out.write(r+" "+d+",1\n");
                        }
                        
                    }
                    
                }
                
                
            }
        }
        out.close();
        }
        catch (IOException e) {
            System.out.println(e);
        }
    
    }
    public static double [] getEtaLambda(double ra, double dec) {
  
       
        double node = surveyCenterRa - 90.;
        node = node * deg2Rad;
        double etaPole = surveyCenterDec * deg2Rad;

 

 
        ra = ra*deg2Rad;
        dec = dec*deg2Rad;
        double x1 = Math.cos(ra-node)*Math.cos(dec);
        double y1 = Math.sin(ra-node)*Math.cos(dec);
        double z1 = Math.sin(dec);
        double lambda = -Math.asin( x1 );
        double eta = (Math.atan2( z1, y1 )) - etaPole;
        lambda = lambda * rad2Deg;
        eta = eta * rad2Deg;

        double [] tp=atbound2(lambda,eta);
        
        tp[1]=atbound( tp[1], -180.0, 180.0);
        
        if (tp[1] > (90. - surveyCenterDec)) {
            tp[1]=tp[1]-180.0;
            tp[0]=180.0-tp[0];
        }
        tp[0]=atbound(tp[0],-180.,180.);
        return tp;
       


        
        





    }
    
    
    public static double atbound (double angle, double min ,double max) {
        while (angle <= min) {
            angle=angle+360.;
        }
        
        while (angle >= max) {
            angle=angle-360.;
        }
        
        return angle;

        
    }
    public static double [] atbound2 (double theta,double phi) {
        theta=atbound(theta,-180.0,180.0);
        if (Math.abs(theta) > 90.) {
            theta=180.0-theta;
            phi=phi+180.0;
        }
        theta=atbound(theta,-180.0,180.0);
        phi=atbound(phi,0.0,360.0);
        
        
        if (Math.abs(theta) == 90.0) {
            phi=0.0;
        }
        
        double [] tp={theta,phi};
        return tp;
      
        
    }

}
