<p>For the Sun at JD 2458599.125, RA = 32.8&deg; and dec = 13.2&deg; are correct regardless of geographic location.
In the <a href="https://aa.usno.navy.mil/faq/docs/Alt_Az.php" rel="nofollow noreferrer">alt/az calculation</a>,</p>

<ul>
<li><code>LHA</code> should be 323.6&deg; and converted to radians, and</li>
<li>the expression for <code>alt</code> needs a call to <code>Math.Asin</code>.</li>
</ul>

<p>Trigonometric expressions are easier to verify if they are free of inline unit conversions.
Store angles in radians where possible; otherwise <a href="https://www.joelonsoftware.com/2005/05/11/making-wrong-code-look-wrong/" rel="nofollow noreferrer">make it obvious</a> with a name suffix such as <code>_deg</code>.</p>

<p>When computing the local hour angle, you can approximate <a href="https://aa.usno.navy.mil/faq/docs/GAST.php" rel="nofollow noreferrer">GAST</a> with GMST.
The difference eqeq is smaller than the approximation error of the <a href="https://aa.usno.navy.mil/faq/docs/SunApprox.php" rel="nofollow noreferrer">Sun position algorithm</a> you're using, and the alternative GMST formula is valid for D within a few centuries of J2000.0.</p>

CC BY-SA 4.0
2019-06-15T07:57:36.583Z
2019-06-15T07:57:36.583Z
Mike G
<p>For anyone looking for the same information, I would point you to an <a href="https://doi.org/10.1016/j.renene.2021.03.047" rel="nofollow noreferrer">excellent paper</a> by Zhang, Stackhouse, Macpherson, and Mikovitz where they do precisely what you are looking for, providing equations along with a code implementation, albeit in Fortran. I <a href="https://astronomy.stackexchange.com/a/54663/47994">implemented it</a> myself in TypeScript. Do keep in mind that the term  <span class="math-container">$$T_{GMT}$$</span> is fractional days since UTC 00:00 on the day in question.</p>

CC BY-SA 4.0
2023-09-12T23:18:11.670Z
2023-09-12T23:18:11.670Z
Michael Bonnet
<p>I recommend you to use astropy, it's a python package with plenty of astronomical libraries.<br>
Check this tutorial on <a href="http://docs.astropy.org/en/stable/generated/examples/coordinates/plot_obs-planning.html" rel="nofollow noreferrer">Determining and plotting the altitude/azimuth of a celestial object</a>.<br>
You have the specific function</p>



<pre><code>from astropy.coordinates import get_sun
</code></pre>

<p>Hope it helps.</p>

CC BY-SA 4.0
2019-05-15T21:39:18.137Z
Thanks but this will end up being used in a mobile app so C# is the language of choice. Do you think I would have any luck using Python just to get the formulas and implement them myself in C#.  All I need are the formulas and an explanation of when and where to convert them to and from radians etc. I have never used Python but this would be pretty basic.
noCodeMonkeys
CC BY-SA 4.0
2019-05-16T06:52:08.243Z
@noCodeMonkeys for Python use [Skyfield](https://rhodesmill.org/skyfield/) or the (now deprecated) [PyEphem](https://rhodesmill.org/pyephem/). Skyfield uses an ephemeris and it needs to download information regularly (a bit table to interpolate) but PyEphem uses something a little different. That will be fun on a computer, but not for a mobile app.
uhoh
CC BY-SA 4.0
2019-05-15T21:06:10.467Z
2019-05-15T21:06:10.467Z
cosimoNigro
<p>As the title says I am trying to calculate solar coordinates of the sun for a given location and time. I have spent many 10's of hours on this without success so I will sincerely appreciate help on this.  My RA value seems to be right on but the Dec, Alt and Az are off. </p>

<p>Alt value is within 6 degrees but I cannot figure out why it is that close because the latitude is in radians instead of degrees like the other values. If I change latitude to degrees the values don't even make sense.</p>

<p>I have included all the values of the intermediate variables to help see at a glance what might be off. I hard coded the JD and the Local Hour Angle just to take those out of the equation for debugging.  Links are shown as to where I got those values. </p>

<p>The C# code is below although the programming language doesn't matter. The formulas are the same in any language.</p>



<pre><code>  //for formulas
            double D = 2458599.125000 - 2451545.0; //the  first  term is the jd of interest; 2nd term is jd of 1-1-2000 at 12:00:00.0
            double Latitude = 41.369341;
            double Longitude = 81.885493;

            double gTmp = 357.529 + 0.98560028 * D;  //mean anomaly of the sun  = 7310.0765751550007
            double gTmp1 = (gTmp % 360);     //subtract multiples of 360 (returns remainder in degrees)  =  110.07657515500068
            double g = gTmp1 * Math.PI / 180;   //Convert to radians = 1.9211986657737494
            double qTmp = 280.459 + 0.98564736 * D; //mean longitude of the sun = 7233.33868336
            double qTmp1 = (qTmp % 360); //subtract multiples of 360 (returns remainder in degrees) =  33.338683359999777
            double q = qTmp1;   //don't convert to radians here; convert after terms in degrees are added in the computation of L; =33.338683359999777
            double L_tmp = q + 1.915 * Math.Sin(g) + 0.020 * Math.Sin(2 * g);//geocentric apparent ecliptic longitude of the sun(adjusted for aberration) = 35.124421105295383
            var L = (L_tmp % 360) * Math.PI / 180;   //Convert to radians; //subtract multiples of 360 = 0.61303679614439033
            double eTmp = 23.4393 - 0.00000036 * D;  // = 23.436760515
            double e = eTmp * Math.PI / 180;   //Convert to radians;   = 0.40904863698815186

            //http://www.neoprogrammics.com/de405_usno_ae98/DE405_Sun.php
            // formulas per https://aa.usno.navy.mil/faq/docs/SunApprox.php
            double RA = (Math.Atan2(Math.Cos(e) * Math.Sin(L), Math.Cos(L))) * 180 / Math.PI; //right ascension converted from radians to degrees = 32.838762087228496
            double dec = (Math.Asin(Math.Sin(e) * Math.Sin(L))) * 180 / Math.PI;  //declination converted from radians to degrees =  3.228747909189217         
            string[] sunCoordinates = new string[2];   
            sunCoordinates[0] = ToHMSStr(RA);    // = "2h 11m 21.3s"
            sunCoordinates[1] = ToDMSstr(dec);  //  = "13d 13m 43.49s"

            //compute alt and az per https://aa.usno.navy.mil/faq/docs/Alt_Az.php and https://aa.usno.navy.mil/faq/docs/GAST.php
            //LHA for testing is from https://ssd.jpl.nasa.gov/horizons.cgi#results and https://www.vercalendario.info/en/how/convert-ra-degrees-hours.html
            //from http://www.neoprogrammics.com/de405_usno_ae98/DE405_Sun.php  
            double LHA = 307.39;   //Local Hour Angle        
            double lat = Latitude * Math.PI / 180; //Convert to radians; //Latitude is defined above; We get sign (below) after converted to Rad????  = 0.72203120983028346
            double alt = (Math.Cos(LHA) * Math.Cos(dec) * Math.Cos(lat) + Math.Sin(dec) * Math.Sin(lat)) * 180 / Math.PI; //convert from radians  = 53.266598643452909
            double az =Math.Atan2(-Math.Sin(LHA) * Math.PI / 180, (Math.Tan(dec)*Math.Cos(lat) * Math.PI / 180 - Math.Sin(lat) * Math.Cos(LHA)) * Math.PI / 180); //convert each term to radians   = 2.4585704411338072
            az = az * 180 / Math.PI;   //convert from radians   = 140.86570991258415

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

CC BY-SA 4.0
2019-05-15T19:07:46.700Z
What program language are you using for this calculation?
Fil
CC BY-SA 4.0
2019-05-16T06:54:03.817Z
If some resources in [this answer](https://astronomy.stackexchange.com/a/27961/7982) or especially in [this answer](https://astronomy.stackexchange.com/a/18029/7982) are helpful,  feel free to go ahead and write a short answer to your own question.
uhoh
CC BY-SA 4.0
2019-05-18T14:24:45.040Z
Is valuable. Actually I already was aware of the C sharp implementation of the sofa Library. It is meticulously crafted but even the guy who wrote it said he has not had time to actually use it. So the implementation documentation is a bit sketchy.
noCodeMonkeys
CC BY-SA 4.0
2019-05-18T14:33:01.333Z
The spreadsheet for which included a link, is a work of art. All the formulas are easily visible and can the implemented in your programming language of choice. Do you know if there's such a spreadsheet for lunar ephemeris.?
noCodeMonkeys
CC BY-SA 4.0
2019-05-18T18:31:51.810Z
No it's definitely negative. I'm going to have to double-check my code cuz I do see where I do not show it as negative. Thanks for bringing that up. I'm on stackexchange mobile right now
noCodeMonkeys
<the-sun><declination><azimuth><right-ascension>
How to calculate solar coordinates. (Right Ascension, Declination, and ultimately the Altitude and Azimuth of the Sun)
noCodeMonkeys
# How to calculate solar coordinates. (Right Ascension, Declination, and ultimately the Altitude and Azimuth of the Sun)

(Asked by: username_0 on 2019-05-15)

As the title says I am trying to calculate solar coordinates of the sun for a given location and time. I have spent many 10's of hours on this without success so I will sincerely appreciate help on this.  My RA value seems to be right on but the Dec, Alt and Az are off. 
Alt value is within 6 degrees but I cannot figure out why it is that close because the latitude is in radians instead of degrees like the other values. If I change latitude to degrees the values don't even make sense.
I have included all the values of the intermediate variables to help see at a glance what might be off. I hard coded the JD and the Local Hour Angle just to take those out of the equation for debugging.  Links are shown as to where I got those values. 
The C# code is below although the programming language doesn't matter. The formulas are the same in any language.
  //for formulas
            double D = 2458599.125000 - 2451545.0; //the  first  term is the jd of interest; 2nd term is jd of 1-1-2000 at 12:00:00.0
            double Latitude = 41.369341;
            double Longitude = 81.885493;

            double gTmp = 357.529 + 0.98560028 * D;  //mean anomaly of the sun  = 7310.0765751550007
            double gTmp1 = (gTmp % 360);     //subtract multiples of 360 (returns remainder in degrees)  =  110.07657515500068
            double g = gTmp1 * Math.PI / 180;   //Convert to radians = 1.9211986657737494
            double qTmp = 280.459 + 0.98564736 * D; //mean longitude of the sun = 7233.33868336
            double qTmp1 = (qTmp % 360); //subtract multiples of 360 (returns remainder in degrees) =  33.338683359999777
            double q = qTmp1;   //don't convert to radians here; convert after terms in degrees are added in the computation of L; =33.338683359999777
            double L_tmp = q + 1.915 * Math.Sin(g) + 0.020 * Math.Sin(2 * g);//geocentric apparent ecliptic longitude of the sun(adjusted for aberration) = 35.124421105295383
            var L = (L_tmp % 360) * Math.PI / 180;   //Convert to radians; //subtract multiples of 360 = 0.61303679614439033
            double eTmp = 23.4393 - 0.00000036 * D;  // = 23.436760515
            double e = eTmp * Math.PI / 180;   //Convert to radians;   = 0.40904863698815186

            //http://www.neoprogrammics.com/de405_usno_ae98/DE405_Sun.php
            // formulas per https://aa.usno.navy.mil/faq/docs/SunApprox.php
            double RA = (Math.Atan2(Math.Cos(e) * Math.Sin(L), Math.Cos(L))) * 180 / Math.PI; //right ascension converted from radians to degrees = 32.838762087228496
            double dec = (Math.Asin(Math.Sin(e) * Math.Sin(L))) * 180 / Math.PI;  //declination converted from radians to degrees =  3.228747909189217         
            string[] sunCoordinates = new string[2];   
            sunCoordinates[0] = ToHMSStr(RA);    // = "2h 11m 21.3s"
            sunCoordinates[1] = ToDMSstr(dec);  //  = "13d 13m 43.49s"

            //compute alt and az per https://aa.usno.navy.mil/faq/docs/Alt_Az.php and https://aa.usno.navy.mil/faq/docs/GAST.php
            //LHA for testing is from https://ssd.jpl.nasa.gov/horizons.cgi#results and https://www.vercalendario.info/en/how/convert-ra-degrees-hours.html
            //from http://www.neoprogrammics.com/de405_usno_ae98/DE405_Sun.php  
            double LHA = 307.39;   //Local Hour Angle        
            double lat = Latitude * Math.PI / 180; //Convert to radians; //Latitude is defined above; We get sign (below) after converted to Rad????  = 0.72203120983028346
            double alt = (Math.Cos(LHA) * Math.Cos(dec) * Math.Cos(lat) + Math.Sin(dec) * Math.Sin(lat)) * 180 / Math.PI; //convert from radians  = 53.266598643452909
            double az =Math.Atan2(-Math.Sin(LHA) * Math.PI / 180, (Math.Tan(dec)*Math.Cos(lat) * Math.PI / 180 - Math.Sin(lat) * Math.Cos(LHA)) * Math.PI / 180); //convert each term to radians   = 2.4585704411338072
            az = az * 180 / Math.PI;   //convert from radians   = 140.86570991258415

```

**Tags:** the-sun, declination, azimuth, right-ascension

> **Comments:**
> **username_1**: What program language are you using for this calculation?  
> **username_2**: If some resources in [this answer](https://astronomy.stackexchange.com/a/27961/7982) or especially in [this answer](https://astronomy.stackexchange.com/a/18029/7982) are helpful,  feel free to go ahead and write a short answer to your own question.  
> **username_0**: Is valuable. Actually I already was aware of the C sharp implementation of the sofa Library. It is meticulously crafted but even the guy who wrote it said he has not had time to actually use it. So the implementation documentation is a bit sketchy.  
> **username_0**: The spreadsheet for which included a link, is a work of art. All the formulas are easily visible and can the implemented in your programming language of choice. Do you know if there's such a spreadsheet for lunar ephemeris.?  
> **username_0**: No it's definitely negative. I'm going to have to double-check my code cuz I do see where I do not show it as negative. Thanks for bringing that up. I'm on stackexchange mobile right now  

---

## 3 answers

### Answer by username_3 on 2019-06-15. Score: 1

For the Sun at JD 2458599.125, RA = 32.8° and dec = 13.2° are correct regardless of geographic location.
In the [alt/az calculation](https://aa.usno.navy.mil/faq/docs/Alt_Az.php),

* `LHA` should be 323.6° and converted to radians, and
* the expression for `alt` needs a call to `Math.Asin`.

Trigonometric expressions are easier to verify if they are free of inline unit conversions.
Store angles in radians where possible; otherwise [make it obvious](https://www.joelonsoftware.com/2005/05/11/making-wrong-code-look-wrong/) with a name suffix such as `_deg`.

When computing the local hour angle, you can approximate [GAST](https://aa.usno.navy.mil/faq/docs/GAST.php) with GMST.
The difference eqeq is smaller than the approximation error of the [Sun position algorithm](https://aa.usno.navy.mil/faq/docs/SunApprox.php) you're using, and the alternative GMST formula is valid for D within a few centuries of J2000.0.


---

### Answer by username_4 on 2023-09-12. Score: 0

For anyone looking for the same information, I would point you to an [excellent paper](https://doi.org/10.1016/j.renene.2021.03.047) by Zhang, Stackhouse, Macpherson, and Mikovitz where they do precisely what you are looking for, providing equations along with a code implementation, albeit in Fortran. I [implemented it](https://astronomy.stackexchange.com/a/54663/47994) myself in TypeScript. Do keep in mind that the term $$T\_{GMT}$$ is fractional days since UTC 00:00 on the day in question.


---

### Answer by username_5 on 2019-05-15. Score: 0

I recommend you to use astropy, it's a python package with plenty of astronomical libraries.
Check this tutorial on Determining and plotting the altitude/azimuth of a celestial object.
You have the specific function
from astropy.coordinates import get_sun

Hope it helps.

> **Comments:**
> **username_0**: Thanks but this will end up being used in a mobile app so C# is the language of choice. Do you think I would have any luck using Python just to get the formulas and implement them myself in C#.  All I need are the formulas and an explanation of when and where to convert them to and from radians etc. I have never used Python but this would be pretty basic.  
> **username_2**: @username_0 for Python use [Skyfield](https://rhodesmill.org/skyfield/) or the (now deprecated) [PyEphem](https://rhodesmill.org/pyephem/). Skyfield uses an ephemeris and it needs to download information regularly (a bit table to interpolate) but PyEphem uses something a little different. That will be fun on a computer, but not for a mobile app.  

---

