【问题标题】:Pyephem giving next_sunset on the wrong day if called after sunset如果在日落后调用 Pyephem 在错误的日子给出 next_sunset
【发布时间】:2020-03-05 06:06:51
【问题描述】:

当我尝试使用 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 和这个模块都是新手。

【问题讨论】:

    标签: python pyephem


    【解决方案1】:

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

    似乎 pyephem 已经/正在被放弃以支持 Skyfield (https://rhodesmill.org/pyephem/)。我查看了用于确定上升、过境和下沉的文档 (https://rhodesmill.org/skyfield/almanac.html),但它似乎是一把大锤敲钉子。进一步看到我只是要运行一个 API,我只是决定我可以使用日出日落 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]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-07
      • 2019-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-16
      • 2018-01-26
      相关资源
      最近更新 更多