在天空中寻找黄昏时刻

2024-09-26 18:06:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个函数,返回给定位置的日出、日落、太阳正午和黄昏时间。此函数使用pyephem,由于它已被弃用,我想我应该重新编写该函数以使用skyfield。但是,skyfield没有函数.previous_rising.next_setting或{}(至少,我找不到它们),这是我在pyephem中使用的。在

skyfield确实有一个函数^{},它将在给定的时间间隔内进行搜索,以确定函数何时发生变化,因此我编写了以下代码进行测试:

from datetime import datetime, timedelta
from skyfield import api, almanac
import pytz

ts = api.load.timescale()
planets = api.load('de421.bsp')
sun, earth = planets['sun'], 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)
earth_loc = earth + loc

def civil_twil(time):
    "returns true/false if sun is above -6 degrees"
    alt, az, dis = earth_loc.at(time).observe(sun).apparent().altaz()
    return alt > -6

t0 = ts.utc(datetime.now(tz))
t1 = ts.utc(tz.normalize(datetime.now(tz) + timedelta(1)))
civil_dawn = almanac.find_discrete(t0, t1, civil_twil)

但这只是给了我一个错误,即函数缺少“rough_period”属性,而且文档中没有提到可能是什么。我会猜测它可能只对almanac类中定义的函数有效;但同样,它没有提到。在

《暮光之城》,我怎么能找到?在


Tags: 函数importapidatetime时间loctzsun
1条回答
网友
1楼 · 发布于 2024-09-26 18:06:56

文档中有一个example,用find_离散函数来查找日出和日落的值。有关此函数,请参阅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)

这将打印以下结果:

Sunrise Sunset center of sun is even with horizon:
['2019-02-03T14:20:33Z', '2019-02-04T00:05:20Z'] [ True False]

Sunrise Sunset top of sun is apparently even with horizon:
['2019-02-03T14:15:28Z', '2019-02-04T00:10:25Z'] [ True False]

Civil twilight:
['2019-02-03T13:44:36Z', '2019-02-04T00:41:18Z'] [ True False]

第一个列表显示发现变化的时间,第二个列表显示True如果太阳升起(或黄昏开始),而{}当太阳落山时。在

在您的例子中缺少的rough_period应该是一个浮点数,它表示每天发生的次数。太阳每天升起和落下一次,所以这个函数中的事件一天发生两次。这意味着rough_period0.5。例如,当你想计算月相时,rough_period可以设置为7.0(满月轨道为27.3天,每相位为6.825天)。请参阅almanac.py源代码中有关季节或月亮阶段计算的其他示例。在

相关问题 更多 >

    热门问题