计算“太阳正午”使用 ephem,转换为当地时间

发布于 2024-09-30 12:22:40 字数 599 浏览 0 评论 0原文

我已经查看了这里使用 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)

我得到:

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

这就是我被困的地方。我是一个Python初学者,坦率地说,我并不是一个程序员。

任何建议将非常受欢迎!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

阳光①夏 2024-10-07 12:22:40

太阳正午不是日出和日落的平均值(请参阅时间方程了解解释) 。 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

请注意,今天中午(在您所在的位置)比日出和日落的平均时间晚 13 秒。

(代码行 ephem.date((sunrise + Sunset) / 2) 展示了如何轻松操作 ephem 包中的日期(如果它是正确的)做。)

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.)

七颜 2024-10-07 12:22:40

如果使用 ephem 不是硬性要求,我最近编写了一个名为 daylight 的库,它有一个本机函数太阳正午直接。

>>> import daylight, pytz
>>> from datetime import datetime
>>> sun = daylight.Sunclock(37.0625, -95.677068)
>>> t = sun.solar_noon(datetime.utcnow().timestamp())
>>> datetime.utcfromtimestamp(t)
datetime.datetime(2020, 6, 4, 18, 20, 54)

并不是说上面的时间是UTC时间。对于更合适的时区,例如 EST,您可以执行以下操作:

>>> tz = pytz.timezone('EST')
>>> tz_offset = tz.utcoffset(datetime.utcnow()).total_seconds()/3600
>>> sun = daylight.Sunclock(37.0625, -95.677068, tz_offset)
>>> t = sun.solar_noon(datetime.utcnow().timestamp())
>>> datetime.utcfromtimestamp(t).astimezone(tz)
datetime.datetime(2020, 6, 3, 7, 50, 54, tzinfo=<StaticTzInfo 'EST'>)

或针对特定日期执行此操作,例如 2020 年 5 月 21 日

>>> t = sun.solar_noon(datetime(2020, 5, 21).timestamp())
>>> datetime.utcfromtimestamp(t)
datetime.datetime(2020, 5, 20, 18, 19, 14)

If using ephem is not a hard requirement, I recently wrote a library called daylight which has a native function for solar noon directly.

>>> import daylight, pytz
>>> from datetime import datetime
>>> sun = daylight.Sunclock(37.0625, -95.677068)
>>> t = sun.solar_noon(datetime.utcnow().timestamp())
>>> datetime.utcfromtimestamp(t)
datetime.datetime(2020, 6, 4, 18, 20, 54)

Not that above time is in UTC. For a more appropriate timezone, say EST, you can do:

>>> tz = pytz.timezone('EST')
>>> tz_offset = tz.utcoffset(datetime.utcnow()).total_seconds()/3600
>>> sun = daylight.Sunclock(37.0625, -95.677068, tz_offset)
>>> t = sun.solar_noon(datetime.utcnow().timestamp())
>>> datetime.utcfromtimestamp(t).astimezone(tz)
datetime.datetime(2020, 6, 3, 7, 50, 54, tzinfo=<StaticTzInfo 'EST'>)

or do this for a particular date, say May 21st 2020

>>> t = sun.solar_noon(datetime(2020, 5, 21).timestamp())
>>> datetime.utcfromtimestamp(t)
datetime.datetime(2020, 5, 20, 18, 19, 14)
╰つ倒转 2024-10-07 12:22:40

两个数字的平均值是数字之和除以二,而不是日光长度除以二。如果将其添加到 o.previous_rising(sun) 中,这也可以工作,但这应该与直接平均相同(我们平均日期时间对象应该不重要)

Average of two numbers is sum of the numbers divided by two, not by dividing length of day light by two. That could also work if you add that to the o.previous_rising(sun), but that should be same as taking direct average (should not matter that we are averageing datetime objects)

夏の忆 2024-10-07 12:22:40

我意识到这个问题很久以前就得到了回答。但是对于那些可能使用任何代码或不太了解Python的人来说,从答案中可以看到时间方程的目的的简单解释。你的平均太阳凌日(中午)发生在你的经轴位置。这意味着,只要知道您的经度,您只需将经度除以 15.0,然后用 12 减去该值即可找到平均太阳凌日,如果您愿意,这将是 GMT 或 UTC 的结果,以给出小时的十进制时间。真正的太阳凌日是平均值减去时间方程的结果。通常,您无论如何都需要计算此值以获得日出和日落时间,如维基百科公式所示。但是感谢您提供 python 软件包信息,因为我在使用 sunpy 软件包时遇到了一些麻烦。 bitbucket 上有一个很棒的程序在这里
https://bitbucket.org/cmcqueen1975/sundials/wiki/Home

I realize that this was answered long ago. But from the answer for those who may use any code or don't quite understand Python, there is the simple explanation for the purpose of the Equation of Time. Your mean solar transit (noon) occurs at your longitudinal location. This means that knowing your longitude you can find your mean solar transit just by dividing your longitude by 15.0 and subtract that value from 12, which will be a result in GMT or UTC if you prefer, to give hours decimal time. Your true solar transit is a result of subtracting the equation of time from the mean. Normally you would need to compute this anyway to get sunrise and sunset times as is shown in the wikipedia formula. But thanks for the python package info as I'm having a bit of trouble with sunpy package. There is a great program on bitbucket here
https://bitbucket.org/cmcqueen1975/sundials/wiki/Home

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文