## Sunrise and Sunset calculation in Python

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

### Sunrise and Sunset calculation in Python

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

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

### Re: Sunrise and Sunset calculation in Python

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).
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

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

### Re: Sunrise and Sunset calculation in Python

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

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

### Re: Sunrise and Sunset calculation in Python

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

http://rhodesmill.org/pyephem/quick.html#local-time
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

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

### Re: Sunrise and Sunset calculation in Python

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