文档中有一个示例,可以使用 find_discrete 函数查找日出和日落的值。有关此功能,请参阅almanac.py中的源代码。这里唯一的问题是它仅在太阳顶部明显与地平线齐平时计算。在以下示例中,我已将函数更改sunrise_sunset
为daylength
函数。有了这个,您可以为函数提供所需的角度daylength
。
from skyfield import api, almanac
from datetime import datetime, timedelta
import pytz
from skyfield.nutationlib import iau2000b
DAYLENGTH_CENTER_HORIZON = 0.0
DAYLENGTH_TOP_HORIZON = 0.26667
DAYLENGTH_TOP_HORIZON_APPARENTLY = 0.8333
DAYLENGTH_CIVIL_TWILIGHT = 6.0
DAYLENGTH_NAUTICAL_TWILIGHT = 12.0
DAYLENGTH_ASTRONOMICAL_TWILIGHT = 18.0
def daylength(ephemeris, topos, degrees):
"""Build a function of time that returns the daylength.
The function that this returns will expect a single argument that is a
:class:`~skyfield.timelib.Time` and will return ``True`` if the sun is up
or twilight has started, else ``False``.
"""
sun = ephemeris['sun']
topos_at = (ephemeris['earth'] + topos).at
def is_sun_up_at(t):
"""Return `True` if the sun has risen by time `t`."""
t._nutation_angles = iau2000b(t.tt)
return topos_at(t).observe(sun).apparent().altaz()[0].degrees > -degrees
is_sun_up_at.rough_period = 0.5 # twice a day
return is_sun_up_at
ts = api.load.timescale()
planets = api.load('de421.bsp')
sun = planets['sun']
earth = planets['earth']
lat, lon = '44.59028 N', '104.71528 W' # UFO Mooring Site
tzn, elv = 'US/Mountain', 1559
tz = pytz.timezone(tzn)
loc = api.Topos(lat, lon, elevation_m=elv)
t0 = ts.utc(datetime.now(tz))
t1 = ts.utc(tz.normalize(datetime.now(tz) + timedelta(1)))
center_time, center_up = almanac.find_discrete(t0, t1, daylength(planets, loc,
DAYLENGTH_CENTER_HORIZON))
print('Sunrise Sunset center of sun is even with horizon:')
print(center_time.utc_iso(), center_up)
apparent_top_time, apparent_top_up = almanac.find_discrete(t0, t1,
daylength(planets, loc, DAYLENGTH_TOP_HORIZON_APPARENTLY))
print('Sunrise Sunset top of sun is apparently even with horizon:')
print(apparent_top_time.utc_iso(), apparent_top_up)
civil_twil_time, civil_twil_up = almanac.find_discrete(t0, t1,
daylength(planets, loc, DAYLENGTH_CIVIL_TWILIGHT))
print('Civil twilight:')
print(civil_twil_time.utc_iso(), civil_twil_up)
这将打印以下结果:
日出日落太阳中心与地平线齐平:
['2019-02-03T14:20:33Z', '2019-02-04T00:05:20Z'] [真假]
日出日落太阳的顶部显然与地平线齐平:
['2019-02-03T14:15:28Z', '2019-02-04T00:10:25Z'] [真假]
民间暮光之城:
['2019-02-03T13:44:36Z', '2019-02-04T00:41:18Z'] [真假]
第一个列表显示发现更改的时间,第二个列表显示True
太阳是否升起(或黄昏开始)以及False
太阳落山的时间。
在您的rough_period
情况下缺少的应该是一个浮点值,其中包含一天发生的次数。太阳每天升起和落下一次,因此此函数中的事件每天发生两次。这意味着rough_period
是0.5
。例如,当您要计算月相时,rough_period
可以将其设置为7.0
(满月轨道为 27.3 天,因此每个阶段为 6.825 天)。请参阅almanac.py源代码中有关季节或月相计算的其他示例。