<p>I would suggest that this:</p>

<blockquote>
  <p>LST = GMST + longitude;</p>
</blockquote>

<p>line is wrong. 360 degrees of longitude correspond to 24 hours, so it should be something like:</p>

<blockquote>
  <p>LST = GMST + longitude/15.0 ;</p>
</blockquote>

<p>Assuming these are all in floating point and you are subsequently handling the potential overflow of time into the next day correctly...</p>

CC BY-SA 3.0
2015-11-27T11:29:13.410Z
I've tried that, I know it is the way it should be. I even added 180 degrees to azimuth so it is measured from south, but I still don't get correct result. I've tried the opposite way, converting from ra/dec to az/alt and it works, but this doesn't.
yode
CC BY-SA 3.0
2015-11-27T10:14:04.567Z
2015-11-27T10:14:04.567Z
Conrad Turner
<p>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:</p>

<pre><code>        az = az * DEG_TO_RAD;
        alt = alt * DEG_TO_RAD;

        lati = latitude * 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;
        dec = dec * RAD_TO_DEGREE;

        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
</code></pre>

<p>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.</p>

CC BY-SA 3.0
2015-11-30T10:50:30.680Z
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).
yode
CC BY-SA 3.0
2015-11-30T14:45:52.743Z
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.
yode
CC BY-SA 3.0
2015-12-01T14:07:43.827Z
I've just added data for Mintaka (delta Ori) and it still gives wrong RA and good Dec. Switching from atan2(x,y) to atan2(y,x) doesn't work.
yode
CC BY-SA 3.0
2015-12-01T14:31:58.293Z
I think I found an answer! I've just noticed that difference between given and required RA is 12, so I just subtracted 12 from HA and voila, the RA is correct! I'm just not sure why is that. Maybe it has something to do with Julian day beginning at noon, if I'm not wrong?
yode
<fundamental-astronomy><coordinate>
Calculation of hour angle
yode
# Calculation of hour angle

(Asked by: username_0 on 2015-11-26)

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;
        alt = alt * DEG_TO_RAD;

        lati = latitude * 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;
        dec = dec * RAD_TO_DEGREE;

        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.

**Tags:** fundamental-astronomy, coordinate
> **Comments:**
> **username_0**: 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).  
> **username_0**: 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.  
> **username_0**: I've just added data for Mintaka (delta Ori) and it still gives wrong RA and good Dec. Switching from atan2(x,y) to atan2(y,x) doesn't work.  
> **username_0**: I think I found an answer! I've just noticed that difference between given and required RA is 12, so I just subtracted 12 from HA and voila, the RA is correct! I'm just not sure why is that. Maybe it has something to do with Julian day beginning at noon, if I'm not wrong?  

---

## 1 answers

### Answer by username_1 on 2015-11-27. Score: 2

I would suggest that this:

> LST = GMST + longitude;

line is wrong. 360 degrees of longitude correspond to 24 hours, so it should be something like:

> LST = GMST + longitude/15.0 ;

Assuming these are all in floating point and you are subsequently handling the potential overflow of time into the next day correctly...

> **Comments:**
> **username_0**: I've tried that, I know it is the way it should be. I even added 180 degrees to azimuth so it is measured from south, but I still don't get correct result. I've tried the opposite way, converting from ra/dec to az/alt and it works, but this doesn't.  

---

