How can I compute sunrise/sunset times? [closed]
Asked Answered
B

8

25

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?

Blondell answered 1/4, 2009 at 4:40 Comment(5)
Frankly, Jason, I'm not sure if I belong here any more or not. I am so sick of finding that questions I asked a year ago are being hacked over and pulled onto other sites. It's a programming question, isn't it?Blondell
if they are "hacked over" or pulled onto other sites, it's for a reason. would you go into a bank and ask the teller what gas prices are? i was just not convinced that this was a programming question, and i'm still not. there is no mention of anything to do with programming, just algorithms and computation. there's a site for that. You also have tagged your question as "astronomy". There's a site for that as well. It doesn't mean your question is bad or invalid, just that it's in the wrong venue.Valvulitis
To get NASA-level accuracy, use the CSPICE libraries: naif.jpl.nasa.gov/naif/tutorials.html If you re-ask this question on astronomy.stackexchange.com, I can give you a more complete answer (I don't want to answer a closed question).Paternalism
Hey, I know I'm late, but I have written an MATLAB function to compute the sunrise and sunset times on the NOAA website. Follow this linkBrunt
You may want to consider the skyfield python library. Usage example: astronomy.stackexchange.com/a/30141Einsteinium
O
11

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

cos(ωo) = -tan(φ) * tan(δ)

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
Oller answered 1/4, 2009 at 4:52 Comment(1)
If the OP is concerned about 1min differences you have to do a lot better than that spherical earth approximationDraco
H
8

If you want to match NOAA, you'll have to consult Jean Meeus' Astronomical Algorithms (mostly Chapter 15). And it's complicated! Martin Beckett is correct, you have to define the sunset. Typically this is the apparent rise or set of upper limb of the sun, which makes your "standard" altitide -5/6 degrees (not zero). And you can't calculate the sunrise or sunset directly with NOAA's accuracy. You'll have to create a governing set of equations for apparent right ascension and declination for the day in question and then interpolate the apparent right ascension and declination over time to find the exact rising and setting times at the standard altitude.

Hope this helps. I spent about a month digesting AA and re-writing all our solar code when I came across the same thing, and it still took over a year to sort out some of the corner cases where my code broke. So it will take some time to figure out. I'm not aware of any public code examples of this algorithm and don't have any to share at this moment, but I'm happy to help you through some headaches if I can.

Homeric answered 8/3, 2011 at 22:0 Comment(8)
What about using NOAA's own algorithm (srrb.noaa.gov/highlights/sunrise/program.txt). Details described at srrb.noaa.gov/highlights/sunrise/calcdetails.htmlUpkeep
@Upkeep NOAA's algorithms published here calculate solar position at a given time. This question is about calculating sunrise and sunset times, which is solving a time for a given position. The problems are related but not the same thing!Homeric
@Homeric I am not sure what all is in the javascript, but I know that it powers their sunrise/set calculator (srrb.noaa.gov/highlights/sunrise/sunrise.html), which tells the time for sunrise/set based on location. As far as I understand, that's exactly what the OP wants to do.Upkeep
@Upkeep Yes, their tool does calculate sunrise and sunset, but that part of the code is not made public by NOAA to my knowledge. I would love to see it if it is! BTW I have a local rubygem that calculates sunrise & sunset. I'm working on publishing it (trying to grok rubyforge release process). If you are really interested in seeing some code, I can send it over or point you to the published link once I get it up there.Homeric
@matexx The code is there, on srrb.noaa.gov/highlights/sunrise/sunrise.html. It's just javascript, so view source will show you everything.Upkeep
@Upkeep Thanks for that pointer! I've always been curious what they do differently. It looks like they do a two-step approximation, which I'm sure is fine and much faster than my interpolation.Homeric
Just a side note. I went throught the whole javascript inside @Upkeep 's answer for a while, without noticing the updated version here: esrl.noaa.gov/gmd/grad/solcalc . Please, don't rush into the old calculator like I did.Vollmer
I have now realized the NOAA-algorithm (which is based on Meeus-AA) in Java, see the API of my lib Time4JSuperpose
D
4

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.

Draco answered 1/6, 2009 at 18:48 Comment(2)
It doesn't take 2 minutes everywhere on earth for the sun to cross the horizon.Southeast
It can take the sun over 43 hours to rise or set: astronomy.stackexchange.com/questions/12824/…Paternalism
R
4

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.

Rotter answered 29/3, 2010 at 21:34 Comment(1)
"If it is open-source library then you could fix it instead of creating a new one with new bugs." <3Audacious
L
3

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

Linkoski answered 1/6, 2009 at 18:36 Comment(0)
V
2

In Ruby I wrote this for equation of time.

include Math

# degrees to radians = PI/180
to_r = PI/180.0

#radians to degrees = 180/PI
to_d = 180.0/PI

puts "Day, Declination, EofT"

# test a celestial year worth of values.
for jday in 1..366

  et = -7.633 * sin(jday * (2 * PI)/365.24) + 9.65 * sin((jday - 78) * 180/92 * to_r)
  a_sin = sin(23.433 * to_r) * sin((2 * PI/366) * (jday - 81))
  declination = asin(a_sin) * to_d
  puts "#{jday}, #{declination}, #{et}"

end

Then for the above equation:

# center disk and refraction factor have been considered.
cos_omega = sin(-0.83 * to_r) - tan(latitude * to_r) * tan(declination * to_r)
semi_diurnal_arc = acos(cos_omega)

There is a whole website devoted to this at http://www.analemma.com/

  • The key to these calculations are good libraries like the one for python above.
  • Also usage of Date and Time classes. I would look on rubyforge for something like ephem.
Vassily answered 23/2, 2011 at 19:25 Comment(1)
Try astro-algo.Homeric
G
0

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.

Graig answered 1/4, 2009 at 4:50 Comment(3)
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.Blondell
Besides, I'm also in the process of trying to replace a lookup table for equinoxes/solstices with an algorithm. :)Blondell
I don't think a lookup table will work if the latitude and longitude are arbitrary. Re equinoxes, perhaps take a look at: astronomy.stackexchange.com/questions/13008/…Paternalism
E
0

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.

Equalizer answered 1/4, 2009 at 4:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.