# Horizontal to Equatorial Conversion - Code

Following this tutorial I have been able to convert from Equatorial to Horizontal Coordinates and am now searching for a way to do the reverse.

I understand this kind of question has been asked and answered using mathematics, but I have been unsuccessful in implementing the provided algorithm in code. Below is my C Sharp code for Equatorial -> Horizontal conversion, which I'm looking to reverse.

public static HorizontalCoords EquatorialToHorizontal(EquitorialCoords equatorialCoords, GeographicCoords geographicCoords, double localSiderealTime)
{

double ha = (localSiderealTime - equatorialCoords.rightAscention) * hours2deg;

double x_sid = Cos_d(ha) * Cos_d(equatorialCoords.declination);
double y_sid = Sin_d(ha) * Cos_d(equatorialCoords.declination);
double z_sid = Sin_d(equatorialCoords.declination);

double x_hor = x_sid * Sin_d(geographicCoords.latitude) - z_sid * Cos_d(geographicCoords.latitude);
double y_hor = y_sid;
double z_hor = x_sid * Cos_d(geographicCoords.latitude) + z_sid * Sin_d(geographicCoords.latitude);

double az = Atan2_d(y_hor, x_hor) + 180;
double alt = Atan2_d(z_hor, Sqrt(x_hor * x_hor + y_hor * y_hor));

return new HorizontalCoords(alt, az);
}

public static EquatorialCoords HorizontalToEquatorial(HorizontalCoords horizontalCoords, GeographicCoords geographicCoords, double localSiderealTime)
{
//*hmmmmmm what goes here..
}

• This looks like a case of "DRY": You should probably write a general procedure to transform between spherical coordinates, as much of the code will be repeated. Commented Jul 13, 2020 at 7:52
• For anyone actually trying to take an observed Alt/Az coordinate and convert it into RA/Dec, there are a lot of corrections involved. For high accuracy, objects are only compared to nearby objects of known position, with the Alt/Az coordinates generally being disregarded. Commented May 2, 2023 at 13:07

## 2 Answers

Only took three years, but finally worked it out. This is in rust but the formula is in there.

    pub fn to_equatorial(
&self,
day: Y2000Day,
position: &GeographicCoords,
) -> EquatorialCoords {
let lst = day.lst(position.longitude);

let sin_lat = sin_d(position.latitude);
let cos_lat = cos_d(position.latitude);

let sin_alt = sin_d(self.altitude);
let cos_alt = cos_d(self.altitude);

let ha = (self.azimuth - 180.) * DEG2HOURS;
let sin_ha = sin_h(ha);
let cos_ha = cos_h(ha);

let x_hor = cos_alt * cos_ha;
let y_hor = cos_alt * sin_ha;
let z_hor = sin_alt;

let x_sid = x_hor * sin_lat + z_hor * cos_lat;
let y_sid = y_hor;
let z_sid = -x_hor * cos_lat + z_hor * sin_lat;

let declination = z_sid.asin() * RAD2DEG;
let right_ascention = wrap_hours(lst - y_sid.atan2(x_sid) * RAD2HOURS);

EquatorialCoords {
right_ascention,
declination,
radius: self.distance,
}
}
}

$$$$


HorizontalToEquatorial would be like EquatorialToHorizontal in reverse, something like this:

• (x, y, z)_hor = rectangular form of (alt, az)

• (x, y, z)_sid = (x, y, z)_hor rotated by geographic latitude

• (ha, dec) = spherical form of (x, y, z)_sid

• ra = local sidereal time - ha`

• Ok that makes sense, I guess the part I'm finding challenging is the exact code for the conversion. Commented Jul 14, 2020 at 2:27
• @PeterHayman What have you tried? Commented Dec 11, 2020 at 2:21
• for now im using matricies to invert the calculation Commented Dec 12, 2020 at 3:30