首页 > 解决方案 > 如果在日落后调用 Pyephem 在错误的日子给出 next_sunset

问题描述

当我尝试使用 pyephem 计算下一个日落时间时,它给了我今天的日落时间,即使我在今天日落之后打电话。我期望的行为是明天返回日落 - 即下一个日落。也许我在做一些简单而错误的事情,但我无法解决。

在发布这个问题时,问题可能出在使用localtime.

这是代码:

#import modules    
import datetime
import ephem

now = datetime.datetime(2020, 3, 4, 21, 00, 00) #set a relevant time after sunset today

wa = ephem.Observer() #create observer object

wa.lat = '47' #set observer parameters
wa.lon = '-122'
wa.date = now

s = ephem.Sun() #identify observed object (the sun)
s.compute(wa) #compute parameters

next_sunrise = ephem.localtime(wa.next_rising(s)) #return sunrise and sunset
next_sunset = ephem.localtime(wa.next_setting(s))

print('Its currently {}, next sunrise will be {}.'.format(now, next_sunrise))
print('Its currently {}, next sunset will be {}.'.format(now, next_sunset))

给出:

Its currently 2020-03-04 21:00:00, next sunrise will be 2020-03-05 06:38:46.186438.
Its currently 2020-03-04 21:00:00, next sunset will be 2020-03-04 17:59:11.109622.
[Finished in 0.065s]

这显然是错误的:下一个日落是在 2020-03-05,如果它在 2020-03-04 日落之后。

值得注意的是,如果我不使用localtime(并使用格林威治 - 使检查结果变得简单),它似乎可以工作:

#import modules
import datetime
import ephem

now = datetime.datetime(2020, 3, 4, 21, 00, 00) #set a relevant time after sunset today

greenw = ephem.Observer() #create observer object

greenw.lat = '50' #set observer parameters
greenw.lon = '0'
greenw.date = now

s = ephem.Sun() #identify observed object (the sun)
s.compute(greenw) #compute parameters

next_sunrise = greenw.next_rising(s) #return sunrise and sunset
next_sunset = greenw.next_setting(s)

print('Its currently {}, next sunrise will be {}.'.format(now, next_sunrise))
print('Its currently {}, next sunset will be {}.'.format(now, next_sunset))

Its currently 2020-03-04 21:00:00, next sunrise will be 2020/3/5 06:33:54.
Its currently 2020-03-04 21:00:00, next sunset will be 2020/3/5 17:49:44.
[Finished in 0.07s]

我不确定这里有什么问题。我查看了文档,但无法解决。也许有问题localtime,或者我错过了一些简单的东西 - 抱歉,我对 python 和这个模块都是新手。

标签: pythonpyephem

解决方案


我更多地研究了这个问题,并最终找到了解决方法。

似乎 pyephem 已经/正在被放弃以支持 Skyfield ( https://rhodesmill.org/pyephem/ )。我查看了用于确定上升、过境和下沉的文档 ( https://rhodesmill.org/skyfield/almanac.html ),但它似乎是一把大锤敲钉子。进一步看到我只是要运行一个 API,我只是决定我可以使用 sunrise-sunset api 更轻松地解决我的小问题:https ://sunrise-sunset.org/api

我想写的代码是看太阳是否升起。这是python3中的解决方案(对任何草率的代码表示歉意):

#import the relevant modules
import requests
from datetime import datetime, timedelta

#set lat and lng (use example of NYC):
lng = '-74'
lat = '40.7'

#call the api for today and tomorrow (times are UTC)
today = requests.get('https://api.sunrise-sunset.org/json?lat=' + lat + '&lng=' + lng + '&date=' + str(datetime.now().date()) + '&formatted=0')
tomorrow = requests.get('https://api.sunrise-sunset.org/json?lat=' + lat + '&lng=' + lng + '&date=' + str(datetime.now().date() + timedelta(days = 1)) + '&formatted=0')

#pull the results
unformatted_times = [i.json().get('results').get('sunset') for i in [today, tomorrow]]

#clean the results
cleaner_times = [i.replace('T', ' ') for i in unformatted_times]
cleaned_times = [i.replace('+00:00', '') for i in cleaner_times]

#create datetime objects out of results
date_time_class_times = [datetime.strptime(i, '%Y-%m-%d %H:%M:%S') for i in cleaned_times]

#create sunrise and sunset variables
sunset_today = date_time_class_times[0]
sunrise_tomorrow = date_time_class_times[1]

#write function to check if its dark, which takes a datetime object (times are in UTC)
def is_dark(now):
    if now > sunset_today and now < sunrise_tomorrow:
        return(True)
    else:
        return(False)

#call the function using utc time now
print(is_dark(datetime.utcnow()))

给出:

False
[Finished in 1.026s]

推荐阅读