pythonpyephem

PyEphem: Can't get correct answers (noob)


I copied the script from this post and modified it for a location near me. But when I run it, I'm getting nonsense times for sunrise, sunset, and astronomical twilight even after adjusting for the local UTC offset (-7h). For example, it's reporting sunrise at 03:34:51 UTC, which would be 20:34:51 the previous day. In reality, sunrise at this lat/long should be 06:37 PDT (UTC - 7h).

I've included my modified code below. What am I doing wrong?

from datetime import datetime
import ephem

# Make an observer
my = ephem.Observer()

# PyEphem takes and returns only UTC times.
now = datetime.now()
nowutc = datetime.utcnow()
my.date = nowutc.strftime("%Y-%m-%d %H:%M:%S")

# Location and elevation of TDS
my.lon = str(32.614)  # Note that lon should be in string format
my.lat = str(-116.333)  # Note that lat should be in string format
my.elev = 1140  # in meters

# To get U.S. Naval Astronomical Almanac values, use these settings
my.pressure = 0
my.horizon = '-0:34'

sunrise = my.previous_rising(ephem.Sun())  # Sunrise
noon = my.next_transit(ephem.Sun(), start=sunrise)  # Solar noon
sunset = my.next_setting(ephem.Sun())  # Sunset

# We relocate the horizon to get twilight times
my.horizon = '-18'  # -6=civil twilight, -12=nautical, -18=astronomical
beg_twilight = my.previous_rising(ephem.Sun(), use_center=True)  # Begin civil twilight
end_twilight = my.next_setting(ephem.Sun(), use_center=True)  # End civil twilight

Solution

  • Check whether you have reversed your latitude and longitude. By swapping those numbers, you have asked about a different location on the Earth’s surface than you intended.