使用ephem计算"Solar Noon",转换为当地时间

Lor*_*ers 8 python datetime astronomy

我在这里看了一些关于使用ephem来计算日出和日落的例子,并且让它工作得很好.

当我试图计算这两次之间的中点时,我遇到了麻烦.这就是我所拥有的:

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)
Run Code Online (Sandbox Code Playgroud)

我明白了:

日出:2010/11/2 12:47:40 UTC
日落:2010/11/2 23:24:25 UTC
中午:5:18:22.679044

那就是我被困住的地方.我是一个蟒蛇初学者,坦白说,一般来说,程序员并不多.

任何建议都是最受欢迎的!

Gar*_*ees 8

太阳正午不是日出和日落的平均值(见解释的时间等式).该ephem软件包具有获取传输时间的方法,您应该使用这些方法:

>>> 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
Run Code Online (Sandbox Code Playgroud)

请注意,今天中午是13秒(在您的位置),而不是日出和日落的平均值.

(代码行ephem.date((sunrise + sunset) / 2)显示了如何正确操作ephem包中的日期,如果这是正确的事情.)