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



public class HorizontalCoordinates extends SphericalCoordinates {
	static CoordinateAxis azymuthAxis, elevationAxis, azymuthOffsetAxis, elevationOffsetAxis;
	static CoordinateSystem defaultCoordinateSystem, defaultLocalCoordinateSystem;

	static {
		defaultCoordinateSystem = new CoordinateSystem("Horizontal Coordinates");
		defaultLocalCoordinateSystem = new CoordinateSystem("Horizontal Offsets");

		azymuthAxis = new CoordinateAxis("Azymuth", "ELON");
		elevationAxis = new CoordinateAxis("Elevation", "ELAT");
		azymuthOffsetAxis = new CoordinateAxis("dAZ", azymuthAxis.wcsName);
		elevationOffsetAxis = new CoordinateAxis("dEL", elevationAxis.wcsName);
		
		defaultCoordinateSystem.add(azymuthAxis);
		defaultCoordinateSystem.add(elevationAxis);
		defaultLocalCoordinateSystem.add(azymuthOffsetAxis);
		defaultLocalCoordinateSystem.add(elevationOffsetAxis);
		
		double[] angularUnits = { Unit.deg, Unit.arcmin, Unit.arcsec, Unit.mas };
		AngleFormat af = new AngleFormat(3);
		
		for(CoordinateAxis axis : defaultCoordinateSystem) {
			axis.setFormat(af);
			axis.setTickUnits(angularUnits);
		}
		

	}

	public HorizontalCoordinates() {}

	public HorizontalCoordinates(String text) { super(text); } 

	public HorizontalCoordinates(double az, double el) { super(az, el); }

	public void defaults() {
		coordinateSystem = defaultCoordinateSystem;
		localCoordinateSystem = defaultLocalCoordinateSystem;		
	}

	public double AZ() { return longitude(); }

	public double azymuth() { return longitude(); }

	public double EL() { return latitude(); }

	public double elevation() { return latitude(); }

	public double ZA() { return 90.0 * Unit.deg - latitude(); }

	public double zenithAngle() { return ZA(); }

	public double cosEL() { return cosLat; }



	public void setAZ(double AZ) { setLongitude(AZ); }

	public void setEL(double EL) { setLatitude(EL); }

	public void setZA(double ZA) { setLatitude(90.0 * Unit.deg - ZA); }

	public EquatorialCoordinates toEquatorial(GeodesicCoordinates geodesic, double LST) {
		EquatorialCoordinates equatorial = new EquatorialCoordinates();
		toEquatorial(this, equatorial, geodesic, LST);
		return equatorial;
	}
	
	public void toEquatorial(EquatorialCoordinates toCoords, GeodesicCoordinates geodesic, double LST) { toEquatorial(this, toCoords, geodesic, LST); }

	public static void toEquatorial(HorizontalCoordinates horizontal, EquatorialCoordinates equatorial, GeodesicCoordinates geodesic, double LST) {
		double sinPhi = Math.sin(geodesic.latitude());
		double cosPhi = Math.cos(geodesic.latitude());

		equatorial.setLatitude(asin(horizontal.sinLat * sinPhi + horizontal.cosLat * cosPhi * Math.cos(horizontal.x)));
		double asinH = -Math.sin(horizontal.x) * horizontal.cosLat;
		double acosH = (horizontal.sinLat - equatorial.sinLat * sinPhi) / cosPhi;

		equatorial.setRA(LST * Unit.timeAngle - Math.atan2(asinH, acosH));
	}

	public static void toEquatorial(Vector2D offset, EquatorialCoordinates equatorial, HorizontalCoordinates horizontal, GeodesicCoordinates geodesic) {
		toEquatorialOffset(offset, equatorial.getParallacticAngle(horizontal, geodesic));
	}

	public static void toEquatorialOffset(Vector2D offset, double PA) {
		offset.rotate(PA);
		offset.x *= -1.0;
	}    

}
