# Calculating azimuth from equatorial coordinates

I try to get the azimuth of an object from its equatorial coordinates using this formula:

$a = \arctan2(\sin(θ - α), \sin φ * \cos(θ - α) - \cos φ * \tan δ)$

Where
φ = geographic latitude of the observer (here: 0°)
θ = sidereal time (here: 0°)
δ = declination
α = right ascension

I made two JavaScript functions to implement this calculation:

  obliq = deg2rad(23.44);  // obliquity of ecliptic
lat2  = deg2rad(0);  // observer's latitude
lmst  = deg2rad(0);  // siderial time

function equatorial(lat, lon) {
// returns equatorial from ecliptic coordinates
dec = Math.asin( Math.cos(obliq) * Math.sin(lat) + Math.sin(obliq) *
Math.cos(lat) * Math.sin(lon));
ra  = Math.atan2(Math.cos(obliq) * Math.sin(lon) - Math.sin(obliq) * Math.tan(lat),
Math.cos(lon));
ra += 2 * Math.PI * (ra < 0);
return [dec, ra];
}

function horizontal(lat, lon) {
// returns horizontal from ecliptic coordinates
coords = equatorial(lat, lon);
dec = coords[0];  // δ
ra  = coords[1];  // α
alt = Math.asin(Math.sin(lat2) * Math.sin(dec) + Math.cos(lat2) *
Math.cos(dec) * Math.cos(lmst - ra));
azm = Math.atan2(Math.sin(lmst - ra), Math.sin(lat2) * Math.cos(lmst - ra) -
Math.cos(lat2) * Math.tan(dec));
azm += 2 * Math.PI * (azm < 0);
return [alt, azm];
}


I cannot see any error, but I get strange results for azimuth (a) as can be seen in this table (the other values seem correct):

   λ       δ         α         h         a
0    0.0000    0.0000   90.0000    0.0000
15    5.9094   13.8115   75.0000  246.5600
30   11.4723   27.9104   60.0000  246.5600
45   16.3366   42.5357   45.0000  246.5600
60   20.1510   57.8186   30.0000  246.5600
75   22.5962   73.7196   15.0000  246.5600
90   23.4400   90.0000    0.0000  246.5600
105   22.5962  106.2804  -15.0000  246.5600
120   20.1510  122.1814  -30.0000  246.5600
135   16.3366  137.4643  -45.0000  246.5600
150   11.4723  152.0896  -60.0000  246.5600
165    5.9094  166.1885  -75.0000  246.5600
180    0.0000  180.0000  -90.0000  248.3079
195   -5.9094  193.8115  -75.0000   66.5600
210  -11.4723  207.9104  -60.0000   66.5600
225  -16.3366  222.5357  -45.0000   66.5600
240  -20.1510  237.8186  -30.0000   66.5600
255  -22.5962  253.7196  -15.0000   66.5600
270  -23.4400  270.0000   -0.0000   66.5600
285  -22.5962  286.2804   15.0000   66.5600
300  -20.1510  302.1814   30.0000   66.5600
315  -16.3366  317.4643   45.0000   66.5600
330  -11.4723  332.0896   60.0000   66.5600
345   -5.9094  346.1885   75.0000   66.5600
360   -0.0000  360.0000   90.0000   68.3079


Does anyone see the error? Thank you.

• This is debugging more than astronomy, perhaps better suited for stackoverflow? I'm not getting your values in a quick test in the Chrome console; I suggest you print out the intermediate values to see what's happening. May 27 '16 at 15:51
• Depends on where the error is. I guess it is in the original formula and not in the code. But I don't know if the formula is correct or not, that's why I ask. If anyone can confirm the formula should yield the correct values then I would look for the error in my implementation. The code example is only to document my approach. May 27 '16 at 17:09

lat2 = 0 and lmst = 0 place the vernal equinox at the zenith, so most points on the ecliptic should have azimuth either obliq degrees north of east or obliq degrees south of west. Try other values for lat2 or lmst and see if the resulting azimuths make sense.
• When I try actual data like lmst = 184.18° (12h16m); obliq = 23.44° to get the azimuth for Venus on May 27th 2016 19 h UT for Berlin (lat2 = 52.52°; lon2 = 13.41667°) the function returns 127.38° but the Astronomical Almanac says it should be 307.366°. δ, α and h are according to the Almanac. May 27 '16 at 19:34
• Aha, the azimuths in your table are also off by 180$^\circ$. Negate both arguments of atan2. May 27 '16 at 21:24