<p>Only took three years, but finally worked it out. This is in rust but the formula is in there.</p>
<pre><code>    pub fn to_equatorial(
        &amp;self,
        day: Y2000Day,
        position: &amp;GeographicCoords,
    ) -&gt; 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,
        }
    }
}


<span class="math-container">```</span>
</code></pre>

CC BY-SA 4.0
2023-05-02T07:34:03.850Z
2023-05-02T07:34:03.850Z
chantey
<p><code>HorizontalToEquatorial</code> would be like <code>EquatorialToHorizontal</code> in reverse, something like this:</p>
<ul>
<li><p><code>(x, y, z)_hor</code> = rectangular form of <code>(alt, az)</code></p>
</li>
<li><p><code>(x, y, z)_sid</code> = <code>(x, y, z)_hor</code> rotated by geographic latitude</p>
</li>
<li><p><code>(ha, dec)</code> = spherical form of <code>(x, y, z)_sid</code></p>
</li>
<li><p><code>ra</code> = local sidereal time - <code>ha</code></p>
</li>
</ul>

CC BY-SA 4.0
2020-07-14T02:27:09.643Z
Ok that makes sense, I guess the part I'm finding challenging is the exact code for the conversion.
chantey
CC BY-SA 4.0
2020-12-11T02:21:28.807Z
@PeterHayman What have you tried?
Mike G
CC BY-SA 4.0
2020-12-12T03:30:20.297Z
for now im using matricies to invert the calculation
chantey
CC BY-SA 4.0
2020-07-13T03:22:28.193Z
2020-07-13T03:22:28.193Z
Mike G
<p>Following <a href="http://www.stjarnhimlen.se/comp/tutorial.html" rel="nofollow noreferrer">this tutorial</a> I have been able to convert from Equatorial to Horizontal Coordinates and am now searching for a way to do the reverse.</p>
<p>I understand this kind of question has been <a href="https://astronomy.stackexchange.com/questions/13067/conversion-from-equatorial-coordinate-to-horizon-coordinates">asked and answered</a> using mathematics, but I have been unsuccessful in implementing the provided algorithm in code. Below is my C Sharp code for Equatorial -&gt; Horizontal conversion, which I'm looking to reverse.</p>
<pre><code>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..
}
</code></pre>

CC BY-SA 4.0
2020-07-13T07:52:35.017Z
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.
James K
CC BY-SA 4.0
2023-05-02T13:07:18.583Z
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.
Greg Miller
<coordinate>
Horizontal to Equatorial Conversion - Code
chantey
# Horizontal to Equatorial Conversion - Code

(Asked by: username_0 on 2020-07-13)

Following [this tutorial](http://www.stjarnhimlen.se/comp/tutorial.html) 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](https://astronomy.stackexchange.com/questions/13067/conversion-from-equatorial-coordinate-to-horizon-coordinates) 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..
}

```

**Tags:** coordinate

> **Comments:**
> **username_1**: 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.  
> **username_2**: 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.  

---

## 2 answers

### Answer by username_0 on 2023-05-02. Score: 2

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,
        }
    }
}

```

```


---

### Answer by username_3 on 2020-07-13. Score: 1

`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`

> **Comments:**
> **username_0**: Ok that makes sense, I guess the part I'm finding challenging is the exact code for the conversion.  
> **username_3**: @PeterHayman What have you tried?  
> **username_0**: for now im using matricies to invert the calculation  

---

