如果在日落后调用,Pypephem将在错误的日期发出下一次日落

2024-05-23 19:41:42 发布

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

当我尝试用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(并使用Greenwich-使检查结果变得简单),它似乎可以工作:

#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和这个模块都是新手


Tags: importdatetimebewillnownextitswa
1条回答
网友
1楼 · 发布于 2024-05-23 19:41:42

我仔细研究了这个问题,最后找到了一个解决办法

似乎以弗所已经/正在被抛弃,取而代之的是天域(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]

相关问题 更多 >