Skip to content

Instantly share code, notes, and snippets.

@stucka
Created May 25, 2018 19:09
Show Gist options
  • Save stucka/8749adf3cbdab13931e4b3b0c7e213c6 to your computer and use it in GitHub Desktop.
Save stucka/8749adf3cbdab13931e4b3b0c7e213c6 to your computer and use it in GitHub Desktop.
Find sunrise, sunset and duration of sunlight at a given point. Haven't verified this works.
import ephem
import datetime
basedate = datetime.datetime.strptime("2018-01-01", "%Y-%m-%d")
target = ephem.Observer()
target.lat = "38.6247"
target.long = "-90.185"
target.elevation = 142 #meters
sun = ephem.Sun()
print("date\trise\tset\tseconds")
for deltadays in range (0, 800):
thisdate = basedate + datetime.timedelta(days=deltadays)
datetext = thisdate.strftime("%Y-%m-%d")
ephemtext = thisdate.strftime("%Y/%m/%d")
rising = ephem.localtime(target.next_rising(sun, start=ephemtext))
setting = ephem.localtime(target.next_setting(sun, start=ephemtext))
sunlight = (setting-rising).seconds
print(datetext + "\t" + str(rising) + "\t" + str(setting) + "\t" + str(sunlight))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment