2

I'm writing a Python program that needs to determine whether it's post-sunrise or not, given only the current UTC time and the target latitude and longitude. I see apps do this sort of thing all the time, but I have no idea how it's done. Any ideas?

Tom Zych
  • 13,329
  • 9
  • 36
  • 53
Daniel Quinn
  • 6,010
  • 6
  • 38
  • 61

4 Answers4

4

Here's someone's implementation of sunrise/sunset calculation in Python: http://michelanders.blogspot.com/2010/12/calulating-sunrise-and-sunset-in-python.html

Acorn
  • 49,061
  • 27
  • 133
  • 172
1

Just doing a quick google search I found this. The calculations in the NOAA Sunrise/Sunset and Solar Position Calculators are based on equations from Astronomical Algorithms, by Jean Meeus. The sunrise and sunset results have been verified to be accurate to within a minute for locations between +/- 72° latitude, and within 10 minutes outside of those latitudes.

A detailed explanation of the calculation details can be found from that page and also here.

smulholland2
  • 1,143
  • 2
  • 14
  • 28
  • Single-link answers can **easily** bit-rot. You should *at least* provide a short summary of the link in your answer. – Joachim Sauer Sep 12 '11 at 08:57
  • Posters are also supposed to have at least run their questions through a search engine. I was trying to relay that requirement in as helpful a manner as I could. – smulholland2 Sep 12 '11 at 09:01
  • If you think that the question is bad, then a bad answer is *not* the appropriate response. – Joachim Sauer Sep 12 '11 at 09:01
  • Actually, I'd run into that link in my own search, and you're right it's exactly what I needed, just in Javascript. I was hoping that there was a Python module out there for something like this rather than having to rewrite it based on that. – Daniel Quinn Sep 12 '11 at 10:03
1

A complete layout of the algorithm is here

Not a lot to add really. The 10 points in the document quoted break it down into simple to reproduce steps that can be implemented in any language really.

nimbusgb
  • 393
  • 1
  • 3
  • 12
0

You need something that just works™? Use the superior language. And you know it'll always work. How can you trust 122 lines of advanced astrophysics? You can't. But you can always trust PHP.

>>> def is_day(lat, lon): # optinally adjust zenith (currently 96) at the end of the line
...   return subprocess.check_output(["php","-r","""date_default_timezone_set("GMT");\n$a=date_sunrise(time(),SUNFUNCS_RET_DOUBLE,{0},{1},{2},0);\n$b=date_sunset(time(),SUNFUNCS_RET_DOUBLE,{0},{1},{2},0);\n$t=date("H")+date("i")/60+date("s")/3600;\necho($a<$b?($t>$a&&$t<$b):($t>$a||$t<$b))?"day":"night";""".format(lat,lon,96)])=="day"
...   #                                                                                                                                                                                                                                                                                                                             ^^ optionally adjust zenith here
... 
>>> is_day(-33,151) # Sydney
True
>>> is_day(0,0) # Some hundred km off the coast of Africa
False
>>> __import__("datetime").datetime.utcnow()
datetime.datetime(2012, 2, 10, 22, 29, 30, 365019)
>>> 
Janus Troelsen
  • 20,267
  • 14
  • 135
  • 196