views:

1319

answers:

6

I like to keep track of sunrise and sunset times. For the past couple of years I've been doing this with a small program written with a popular library for my favorite programming language. The last two months I've been keeping track of these times more regularly than usual, and I happened to notice that on the day of the equinox the sunrise time jumped eight minutes as compared to the day before! I knew this was impossible and compared with NOAA, finding out that my rise and set times had been off for several days and in fact seemed to be off by about a minute for most of the year.

At this point, I'd like to just implement the calculations myself. What algorithms or formulas are available to do this computation?

A: 

I did see some interesting things on this NOAA page under the Technical Definitions and Computational Details heading, but I'm sure you've read that already.

The answer to the SO question "Position of the sun given time of day, and lat/long" and the above may actually be all you need.

As a side note (it doesn't answer your question directly), is there a reason you can't pull the NOAA data and use it as a lookup table instead of calculating it? Storage tends to be relatively cheap these days.

lc
I like the lookup table approach, but I want to know how to implement it in code, and at this point I want to know what algorithm is behind the data I'm using, since what I was using turned out to be so wonky.
skiphoppy
Besides, I'm also in the process of trying to replace a lookup table for equinoxes/solstices with an algorithm. :)
skiphoppy
+4  A: 

You may consider reading Wikipedia's article on sunrise equations. The lead paragraph gives the equation:

where:

  • ωo is the hour angle in degrees at either sunrise (when negative value is taken) or sunset (when positive value is taken) in degree (°)
  • φ is the latitude of the observer on the Earth in degrees
  • δ is the sun declination in degrees
Bryson
Thank you! I missed that article!
skiphoppy
A: 

You might want to look at an earlier entry on calculating the position of the sun. Specifically, the Solpos program that I pointed to has support for sunrise/sunset.

Toybuilder
+1  A: 

Have a look at this book:
"Practical Astronomy with your Calculator (Paperback)" by Peter Duffett-Smith.
It is quite old but still in print... link

Volker Voecking
A: 

For accuracy in the 5min range you have to consider 'which' sunset.
Do you want the time the bottom of the sun touches the horizon or the time the top of the sun passes below the horizon?

It takes 2mins for the sun to cross the horizon.
Below the 1min level you also need to take into account atmospheric refraction.

Martin Beckett
A: 

Definitions of what constitutes sunrise/sunset can vary. For example, in ephem "rising and setting are defined as the moments when the upper limb of the body touches the horizon (that is, when the body’s alt plus radius equals zero)" [PyEphem Quick Reference].

#!/usr/bin/env python
import datetime
import ephem # to install, run `pip install pyephem`

o = ephem.Observer()
o.lat, o.long, o.date = '34:3', '-118:15', datetime.datetime.utcnow()
sun = ephem.Sun(o)
print "Los Angeles"
print "sunrise:", o.next_rising(sun), "UTC"
print "sunset:",o.next_setting(sun), "UTC"

Output

Los Angeles, CA
sunrise: 2010/3/30 13:42:43 UTC
sunset: 2010/3/30 02:11:50 UTC

If it is open-source library then you could fix it instead of creating a new one with new bugs.

J.F. Sebastian