# Calculation of hour angle

I need to determine Right Ascension and Declination from Azimuth and Altitude, working in C#. The problem is that the formula for calculating hour angle, for some reason, doesn't work. Here's the code:

        az = az * DEG_TO_RAD;

// Julian day
JD = CalculateJDN(year, month, day, h, m, s);

// Greenwich mean sidereal time
GMST = CalculateGMST(JD);

LST = GMST + longitude / 15;

dec = Math.Asin((Math.Sin(lati) * Math.Sin(alt)) + (Math.Cos(lati) * Math.Cos(alt) * Math.Cos(az)));

ha = Math.Atan2(Math.Sin(az), (Math.Cos(az) * Math.Sin(lati) + Math.Tan(alt) * Math.Cos(lati)));

ha = ha * RAD_TO_DEGREE / 15;

ra = LST - ha;

// Input data for Mintaka (delta Ori):
// az = 47.5, alt = -35.3 on 13:57 UTC, 1 Dec 2015
// latitude = 43.897, longitude = 20.344
// Required output: dec = -0.19, ra = 5.5
// Given output:
// dec = -0.19, ra = 17.5, ha = 2.5


Az and alt are given in degrees, so they are first converted into radians. Functions for calculating Julian day number and GMST are correct, since I've already tested them. Formula for declination is good, but for some reasons formula for hour angle (ha) doesn't work. I don't know where's the error.

• Standard advice: could you show us a specific example where it goes wrong and print each value as you compute it? – user21 Nov 27 '15 at 13:56
• I've edited the code with the changes I've made, but still no better. The values I use are: az = 0.5 alt = 43.25 latitude = 43.897 longitude = 20.344 This are data to track Polaris from my location (Serbia). The values I get are, using the time as in now (10:55 UTC, 30 Nov 2015): JD: 2457356.95347222 GMST: 15:28:59 LST: 16:50:22 HA: 1:57:40 (obviously wrong), and finally, DEC: 89d15m31s (good) RA: 14:52:42 (wrong). – dejan Nov 30 '15 at 10:50
• Try something a little further away from the North Pole (ie, something other than Polaris). At the declination you give, all lines of right ascension are very close to each other. Polaris moves very little for most observers: do you need telescope-level precision? – user21 Nov 30 '15 at 14:30
• I've tried several other stars and every one gives error in RA. Yes, I need telescope-level precision, since this is a system that will be attached to telescope and via sensors it gives alt and az, and then I need to transform it into RA and Dec so that it can be used for positioning of telescope. I think that maybe there is something wrong with the formula, but every book I've looked gives the same formula for RA. – dejan Nov 30 '15 at 14:45
• Could you show us an example of failure with a star closer to the celestial equator? Go ahead and put the program inputs and outputs directly into your question to make things easier (putting values in comments can get ugly). One thought is that you might have atan2() backwards: different languages define it differently. atan2(y,x) usually means atan(y/x) corrected for quadrant, but can mean atan(x/y) corrected for quadrant. – user21 Nov 30 '15 at 15:29