/*
 * Created on 30-Aug-2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package uk.ac.roe.wfau;

import java.math.BigInteger;
import java.util.StringTokenizer;

/**
 * @author mar
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class StringToRADec {
    public static String nullStr="null";
    public static double coordStringToDouble (String str, String RADec, boolean limits) throws NumberFormatException {
        StringTokenizer st=new StringTokenizer(str.trim()," ,:");
        int noParts=st.countTokens();
        String[] parts = new String[noParts];
		for ( int i = 0; i < noParts; i++ ) {
		    parts[ i ] = st.nextToken();
		}
        if (noParts == 3) {
            try {
            double strHD=Double.valueOf(parts[0]).doubleValue();
            double strM=Double.valueOf(parts[1]).doubleValue();                
            double strS=Double.valueOf(parts[2]).doubleValue();
            double retValue=(Math.abs(strHD)+(strM+strS/60.0)/60.0);
            if (RADec.equalsIgnoreCase("RA")) {
                retValue=retValue*15.0;
            }
            if (parts[0].indexOf('-') >= 0) {
               retValue=retValue*-1.0;
            }
            if (limits) {
    		if ((retValue >= 0.0 && retValue <= 360.0 && RADec.equalsIgnoreCase("RA")) || 
    		        (retValue >= -90.0 && retValue <= 90.0)) {	
 	           
         return retValue;
 		}
 		else {
 		    throw new  NumberFormatException("Coords out of range") ;
 		}
            }
            else {
                return retValue;
            }
            }
            catch (NumberFormatException nfe) {
                throw new  NumberFormatException("Unable to parse string: "+nfe) ;
            }
        }
        else {
            throw new  NumberFormatException("Unable to parse string: check number of parts") ;
        }
    }
    
    public static Object [] getRADec (String str)  throws NumberFormatException {
        return getRADec (str, true);
    }
    public static Object [] getRADec (String str, boolean IDPresent) throws NumberFormatException {
        Object [] objs = new Object[4];
        objs[3]=nullStr;
        double [] coords = new double [2];
        long l=0;
        StringTokenizer st=new StringTokenizer(str.trim()," ,:");
        int noParts=st.countTokens();
	    String[] parts = new String[st.countTokens()];
			for ( int i = 0; i < noParts; i++ ) {
			    parts[ i ] = st.nextToken();
			}
        if (noParts >=2 && noParts < 6)  {// assume first two degrees
            try {
            coords[0]=Double.valueOf(parts[0]).doubleValue();
            coords[1]=Double.valueOf(parts[1]).doubleValue();
            if (noParts >=3 && IDPresent) {
                l=Long.valueOf(parts[2]).longValue();
            }
            if (noParts >=3 && !IDPresent) {
                objs[3]=parts[2];
            }
            if (noParts >=4 && IDPresent) {
                objs[3]=parts[3];
            }
            
            }
            catch (NumberFormatException nfe) {
                throw new  NumberFormatException("Unable to parse string:" +nfe+" "+IDPresent) ;
            }
        }
        else if (noParts >= 6) {
            try {
                double raH=Double.valueOf(parts[0]).doubleValue();
                double raM=Double.valueOf(parts[1]).doubleValue();                
                double raS=Double.valueOf(parts[2]).doubleValue();
                double decD=Double.valueOf(parts[3]).doubleValue();
                double decM=Double.valueOf(parts[4]).doubleValue();
                double decS=Double.valueOf(parts[5]).doubleValue();
                coords[0]=(raH+(raM+raS/60.0)/60.0)*15.0;
                coords[1]=(Math.abs(decD)+(decM+decS/60.0)/60.0);
                if (parts[3].indexOf('-') >= 0) {
                    coords[1]=coords[1]*-1.0;
                }
                if (noParts >=7 && IDPresent) {
                    l=Long.valueOf(parts[6]).longValue();
                }
                if (noParts >=7 && !IDPresent) {
                    objs[3]=parts[6];
                }
                if (noParts >=8 && IDPresent) {
                    objs[3]=parts[7];
                }
                
            }
            catch (NumberFormatException nfe) {
                throw new  NumberFormatException("Unable to parse string") ;
            }
        }
        else if (noParts == 0) {
            objs[0]=new Double(999.0);
            objs[1]=new Double(999.0);
            objs[2]=new Long(0);
            return objs;
        }
            else {
                throw new  NumberFormatException("Unable to parse string") ;
                
            }           
		if (coords[0] >= 0.0 && coords[0] <= 360.0 && coords[1] >= -90.0 && coords[1] <= 90.0) {	
	           objs[0]=new Double(coords[0]);
	           objs[1]=new Double(coords[1]);
	           objs[2]=new Long(l); 
        return objs;
		}
		else {
		    throw new  NumberFormatException("Unable to parse string into valid coords") ;
		}
    }

}
