pythondatetimeastronomypyephem

Calculate "Solar Noon" using ephem, translating to local time


I have looked at the examples here on using ephem to calculate sunrise and sunset, and have that working great.

I get in trouble when I try to calculate the midpoint between those two times. Here's what I have:

import datetime
import ephem

o = ephem.Observer()
o.lat, o.long, o.date = '37.0625', '-95.677068', datetime.datetime.utcnow()
sun = ephem.Sun(o)
print "sunrise:", o.previous_rising(sun), "UTC"
print "sunset:",o.next_setting(sun), "UTC"
print "noon:",datetime.timedelta((o.next_setting(sun)-o.previous_rising(sun))/2)

I get:

sunrise: 2010/11/2 12:47:40 UTC
sunset: 2010/11/2 23:24:25 UTC
noon: 5:18:22.679044

That's where I'm stuck. I'm a python beginner and frankly not much of a programmer in general.

Any suggestions would be most welcome!


Solution

  • Solar noon is not the mean of sunrise and sunset (see equation of time for the explanation). The ephem package has methods for getting transit times which you should use instead:

    >>> import ephem
    >>> o = ephem.Observer()
    >>> o.lat, o.long = '37.0625', '-95.677068'
    >>> sun = ephem.Sun()
    >>> sunrise = o.previous_rising(sun, start=ephem.now())
    >>> noon = o.next_transit(sun, start=sunrise)
    >>> sunset = o.next_setting(sun, start=noon)
    >>> noon
    2010/11/6 18:06:21
    >>> ephem.date((sunrise + sunset) / 2)
    2010/11/6 18:06:08
    

    Note that noon today is 13 seconds later (at your location) than the mean of sunrise and sunset.

    (The line of code ephem.date((sunrise + sunset) / 2) shows how you could easily manipulate dates in the ephem package, if it were the right thing to do.)