Page 1 of 1
How to calculate sun position?
Posted: Tue Oct 25, 2011 8:57 pm
by Moohasha
I would like to be able to take the time of day and the calendar date and be able to get the exact position of the sun relative to the earth (that is, the latitude/longitude of its zenith). I know that the Earth's axial tilt is ~23.5 degrees and can look up the date and time of the summer solstice, so that would give me a baseline, but is there a better way to do this? I can find all sorts of calculators online that will do this, but I need to do it in code.
Re: How to calculate sun position?
Posted: Wed Oct 26, 2011 2:41 am
by Kojack
No idea what the license is, but here's some c++ code:
http://www.psa.es/sdg/sunpos.htm
(From the Plataforma Solar de Almeria solar research center)
It calculates sun pos based on time, date and lattitude/longitude.
Re: How to calculate sun position?
Posted: Wed Oct 26, 2011 4:38 pm
by jacmoe
I believe that there's code for that in Caelum - I could be wrong, but do check.
Re: How to calculate sun position?
Posted: Wed Oct 26, 2011 5:03 pm
by lingfors
Re: How to calculate sun position?
Posted: Wed Oct 26, 2011 5:55 pm
by Moohasha
jacmoe wrote:I believe that there's code for that in Caelum - I could be wrong, but do check.
I checked there, and it calculates sun position based on time of day. I also need it based on time of year since days are shorter in the winter and longer in the summer. Those other two links were way too complicated, so I made a compromise.
The Earth's axial tilt is ~23.5 degrees, and the summer solstice (for the northern hemi) is usually June 21. So I just assumed that at 12:00 UTC, June 21 of any given year, the sun's zenith would be 23.5 latitude, 0 longitude. From there I interpolate longitude based on time of day (linear interpolation), and latitude based on days from June 21 using a cosine function. Thus, the sun should fluctuate between 23.5 latitude (summer solstice) and -23.5 latitude (winter solstice). Doing this, I'm accurate to within 1 degree latitude and 3 degrees longitude according to timeanddate.com, which is an acceptable level of error to me.
Thanks for the links/tips, they got me going in the right direction.