1

I'm having some difficulty using pyEphem to get accurate sunrise and sunset times for my location. I have read a few questions and answers, and looked at the docs, but it's still not totally clear what I'm doing wrong. Here is the code:

import datetime as dt
import ephem as ep

date = dt.datetime.now().strftime("%Y/%m/%d 00:00:00")
lat, lon = [<redacted>, -1.4147]

# Use lat and lon to create ephem observer instance and update with given
# values
my_location = ep.Observer()
my_location.lat = lat
my_location.lon = lon
my_location.date = date

# Get sunrise of the current day
sunrise = my_location.next_rising(ep.Sun())
sunset = my_location.next_setting(ep.Sun())

print "Given date: {0}".format(date)
print "Detected coordinates: {0}, {1}".format(lat, lon)
print "Sunrise at {0}".format(sunrise)
print " Sunset at {0}".format(sunset)

Which produces this output:

Given date: 2015/01/31 00:00:00
Detected coordinates: <redacted>, -1.4147
Sunrise at 2015/1/31 12:28:02
 Sunset at 2015/1/31 22:47:39

What I was expecting was to get the times of the first sunrise after midnight (this morning) and the first sunset after that. Now I happen to know the sun came up before 12:30 today (I was walking down the road at 9:30am in broad daylight) and I don't think there's a UTC offset since I know my timezone.

What am I missing?

Magic_Matt_Man
  • 1,870
  • 3
  • 14
  • 15
  • It almost appears that there is a UTC time offset appearing in your output –  Jan 31 '15 at 14:47
  • Thanks, it does seem that way although I definitely live in UTC+00. – Magic_Matt_Man Jan 31 '15 at 15:10
  • This may be of help - http://rhodesmill.org/pyephem/quick#transit-rising-setting - particularly the 'observer.horizon' example –  Jan 31 '15 at 15:32
  • Also, related - http://stackoverflow.com/questions/2637293/calculating-dawn-and-sunset-times-using-pyephem –  Jan 31 '15 at 15:38

1 Answers1

3

If you provide longitude and latitude as raw floating point numbers, then PyEphem assumes that you have already done the work of converting them to radians. The value 1.4147 radians is nearly an eighth of the way around the globe. If you want PyEphem to convert degrees for you, try using a string to represent them instead that PyEphem will be forced to convert:

lat, lon = [<redacted>, '-1.4147']

You can check whether a value got set properly by printing it back out:

print my_location.lat
print my_location.lon

Asking an Observer object to print back out its values is the best way to find out if it had gotten set up correctly.

You will note that the new Skyfield library, learning from PyEphem’s mistake here, does no auto-conversion at all but requires values to be specified as explicitly radians or degrees!

Brandon Rhodes
  • 69,820
  • 15
  • 101
  • 136
  • also the date should be in UTC (or UT1) (`datetime.utcnow()` or `ephem.now()`), not a local timezone (`.now()` is incorrect unless the local timezone is UTC). – jfs Feb 01 '15 at 03:02
  • Ah ok, so I need to pass the angles into pyephem in radians instead of degrees? I didn't realise since pyEphem usually prints angles in degrees. Thanks I'll test this later and see if it solves my problem. Also, @J.F.Sebastian thank you but I've been through that with other commenters: I know what time zone I am in. – Magic_Matt_Man Feb 04 '15 at 12:55