Celestial Programming : Convert RA/DEC to Alt/Az

You will likely come across different sets of equations for the RA/Dec to Alt/Az conversion. I have included several below from different sources. They are all mathematically equivelant, but one may be easier to use depending on the form you are given the Ra/Dec coordinates in. The matrix form is likely the most useful for most applications since you will likely need to perform the conversion to rectagular coordinates for other purposes. But, when supplied an Ra/Dec coordinate, my preferred method is the ATAN2 method as use of the atan2() function eliminates the awkward quadrant checking.

Be aware, that even though these equations are exact, Alt/Az coordinates are usually given in Apparent coordinates, and it is very common for Ra/Dec to be expressed in Astrometric coordinates. If you're having trouble reproducing results obtained elsewhere, it is likely that you need to apply transformations such as precession, nutation, stellar abberation, refraction, etc.

The implementation below uses the ATAN2, but an implementation of the other is available in the set of test data which validates all sets of equations, and can be used as an example of how to implement the others.

For all equations below:

\(h\) is the hour angle
\(\delta\) is the declination
\(A_{z}\) is azimuth
\(a\) is altitude
\(\phi\) is the lattitude.

Explanatory Supplement eq 7.16

$$ \begin{align*} \cos \mathit{a} \sin A_{z} &= -\cos \delta \sin \mathit{h} & (eq 1) \\ \cos \mathit{a} \cos A_{z} &= \sin \delta \cos \phi - \cos \delta \cos \mathit{h} \sin \phi & (eq 2) \\ \sin \mathit{a} &= \sin \delta \sin \phi + \cos \delta \cos \mathit{h} \cos \phi & (eq 3) \end{align*} $$

You will need to check the signs of \(\cos A_z\) and \(\sin A_z\) to determine the propper quadrant:

if \(\cos A_z\) > 0 and \(\sin A_z\) > 0: use either eq1 or eq2

if \(\cos A_z\) > 0 and \(\sin A_z\) < 0: use eq1

if \(\cos A_z\) < 0 and \(\sin A_z\) < 0: use \(A_z\) = 360 - eq2

if \(\cos A_z\) < 0 and \(\sin A_z\) > 0: use eq2

ATAN2 Method

Since \(\tan A_z = \frac{\sin A_z}{\cos A_z}\) we can rearrange the azimuth equations above so that the atan2() function built in to most modern programming languages can be used. This function performs the awkward quadrant checking needed above, so it doesn't have to be done explicitly. This is similar to Astronomical Algorithms eq 13.5, 13.6, but altered so that Longitudes to the West are negative (like GPS coordinates), and so that 0 Azimuth is North (like a compass).

$$ \begin{aligned} \tan A_z &= \dfrac{\sin h}{\cos h \sin \phi - \tan \delta \cos \phi} \\~\\ \sin a &= \sin \phi \sin \delta + \cos \phi \cos \delta \cos h \end{aligned} $$

Where the symbols have the same meaning as in the Explanatory Supplement version.

Computational Spherical Astronomy (Taff) 1.1, 2.7

Vector/Matrix approach.
$$ \\ \begin{align*} \mathbf{l}(h,\delta) &= \begin{bmatrix} \cos \delta\cos h \\ \cos \delta\sin h \\ \sin \delta \\ \end{bmatrix}\\ \\~\\ \mathbf{l}(A_z,a) &= R_2(90^{\circ}-\phi )\ \mathbf{l}(h,\delta) \end{align*} $$ \(\mathbf{l}(A_z,a)\) will be in cartesian coordinates (\(x, y, z\)), to convert to Alt/Az:

$$ \begin{align*} r &= \sqrt{ x^2+y^2+z^2} \\ \cos A_z &= z / r \\ \tan a &= y / x \\ \end{align*} $$

Spherical Astronomy (Robin Green) 2.36, 2.37

$$ \begin{align*} \sin a &= \sin \delta \sin \phi + \cos \delta \cos \phi \cos h \\~\\ \cos A_z &= \frac{\sin \delta}{\cos a \cos \phi} - \tan a \tan \phi \end{align*} \\ $$ if H < 12 then Az = 360° - Az

To compute the Hour Angle, this example uses Greenwich Mean Sidereal Time to compute the approximate hour angle, if you need the coordiates also corrected for nutation, you will need to modify this to use Greenwhich Apparent Sidereal Time. It is assumed that all corrections the user has interest in have already been applied to the RA/Dec coordinates.

Julian Date:
Right Ascension:Decimal hours, eg. 10.382938
Declination:Decimal degrees, eg. 249.382988