User avatar
wymand
Posts: 48
Joined: Thu Nov 28, 2013 2:06 am
Location: relocating to hiddenridge

Sunrise and Sunset calculation in Python

Thu Jul 02, 2015 6:59 pm

Here is a link to my blog http://tigermountainsprings.net/wp/?p=343 on adding
Sunrise and Sunset calculation to a Raspberry Pi running Python.
this is part of my Chicken Coop Control http://tigermountainsprings.net/wp/ system
that I use to operate and protect the chicken coop and flock.
Never stop learning
Never stop evolving

User avatar
DougieLawson
Posts: 42760
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK

Re: Sunrise and Sunset calculation in Python

Thu Jul 02, 2015 7:51 pm

Code: Select all

#!/usr/bin/python
import ephem
import datetime

somewhere = ephem.Observer()
somewhere.lat = '51.2xxxxxx0' # <== change me
somewhere.lon = '-1.1xxxxxx0' # <== and change me
somewhere.elevation = 112
print somewhere.date

sun = ephem.Sun()
r1 = somewhere.next_rising(sun)
s1 = somewhere.next_setting(sun)

somewhere.horizon = '-0:34'
r2 = somewhere.next_rising(sun)
s2 = somewhere.next_setting(sun)
print ("Visual sunrise %s" % r1)
print ("Visual sunset %s" % s1)
print ("Naval obs sunrise %s" % r2)
print ("Naval obs sunset %s" % s2)
I've obfuscated my lat/long (+ve lat == N of equator, +ve long == E of Greenwich).
Languages using left-hand whitespace for syntax are ridiculous

DMs sent on https://twitter.com/DougieLawson or LinkedIn will be answered next month.
Fake doctors - are all on my foes list.

The use of crystal balls and mind reading is prohibited.

rfeyer
Posts: 149
Joined: Sun Nov 23, 2014 9:25 pm

Re: Sunrise and Sunset calculation in Python

Mon Oct 31, 2016 7:32 pm

Question:
The results above are given in UTC time.
How would you extract 4 hours from the given results as I live in EDT?

User avatar
DougieLawson
Posts: 42760
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK

Re: Sunrise and Sunset calculation in Python

Wed Nov 02, 2016 8:10 pm

Of course they're in GMT/UTC that's my local TZ.

http://rhodesmill.org/pyephem/quick.html#local-time
Languages using left-hand whitespace for syntax are ridiculous

DMs sent on https://twitter.com/DougieLawson or LinkedIn will be answered next month.
Fake doctors - are all on my foes list.

The use of crystal balls and mind reading is prohibited.

richrarobi
Posts: 273
Joined: Sun Feb 08, 2015 1:13 pm

Re: Sunrise and Sunset calculation in Python

Thu Nov 03, 2016 11:41 pm

Had the same problem, found this treasure: http://pythonhosted.org/astral/
Works for me ...
Any use?

Return to “Python”