1

When I try and work out the next sunset time using pyephem it gives me sunset time today, even if I am calling after sunset today. The behavior i would expect is to return sunset tomorrow - i.e. the next sunset. Perhaps I am doing something simple and wrong, but i can't work it out.

In posting this question it appears that the problem might be with using localtime.

Here is the code:

#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))

Gives:

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]

This is clearly wrong: next sunset is on 2020-03-05, if its after sunset on 2020-03-04.

Notably it appears to work if I don't use localtime (and use Greenwich - to make checking the results simple):

#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))

Gives

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]

I am not sure what the problem is here. I have looked through the documentation but cannot work it out. Perhaps there is a problem with localtime, or perhaps I am missing something simple - apologies I am new to both python and this module.

MorrisseyJ
  • 1,191
  • 12
  • 19

1 Answers1

0

I looked more at this problem, and ended up with a workaround.

Seems that pyephem has been/is being dropped in favor of Skyfield (https://rhodesmill.org/pyephem/). I looked at that documentation for determining rising, transit and setting (https://rhodesmill.org/skyfield/almanac.html), but it seemed like a sledge hammer for a nail. Further upon seeing that I was simply going to be running an API, I just decided I could solve my small problem more easily using a sunrise-sunset api: https://sunrise-sunset.org/api

The code I wanted to write was looking at whether or not the sun was up. Here is the solution in python3 (apologies for any sloppy code):

#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()))

Gives:

False
[Finished in 1.026s]
MorrisseyJ
  • 1,191
  • 12
  • 19