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


import java.text.*;

public class BesselianEpoch extends CoordinateEpoch {	
	
	public BesselianEpoch() { }

	public BesselianEpoch(double epoch) { super(epoch); }

	public double getBesselianYear() { return year; }

	public double getJulianYear() { return JulianEpoch.getYearForMJD(getMJD()); }

	public JulianEpoch getJulianEpoch() { 
		return new JulianEpoch(getBesselianYear());
	}
	
	//  B = 1900.0 + (JD - 2415020.31352) / 365.242198781
	public void setMJD(double MJD) {
		year = getYearForMJD(MJD);
	}

	@Override
	public double getMJD() {
		return getMJDForYear(year);
	}
	
	public static double getYearForMJD(double MJD) {
		return 1900.0 + (MJD - MJD1900) / julianYear;
	}
	
	public static double getMJDForYear(double year) {
		return (year - 1900.0) * julianYear + MJD1900;
	}
	
	// JD 2415020.31352
	public final static double MJD1900 = 15019.81352;


	public String toString() { return toString(Util.f1); }
	
	public String toString(NumberFormat nf) {
		return "B" + nf.format(year);
	}

	public void parse(String text) throws NumberFormatException, IllegalArgumentException {
		if(text.charAt(0) == 'B') year = Double.parseDouble(text.substring(1));
		else if(text.charAt(0) == 'J') year = getYearForMJD(JulianEpoch.getMJDForYear(Double.parseDouble(text.substring(1))));
		else year = Double.parseDouble(text);
	}	
	
}
