Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sunrise and Sunset time in Python

I'm currently working on a project that notifies the users when certain activities related to light are triggered. I've done the part related to light. Hence, I need to find an effective way to retrieve sunrise and sunset time in python, since the whole script is written in python. I know there are several libraries out there for other languages, I wonder what the most convenient way is to do this in python.

It will seem pretty much like this, I suppose:

if(sunrise<T<sunset) and (light<threshold):
    notifyUser()

I'd appreciate any help here, have a good one.

like image 282
bkaankuguoglu Avatar asked Aug 17 '16 00:08

bkaankuguoglu


People also ask

What data does the sunrise and Sunset API return?

This Sunrise and Sunset API will return interesting data about the day like: sunset, sunrise, day length, nautical twilight, civil twilight and astronomical twilight. One thing to be careful about is times will be GMT so depending on the location you’re looking up you might want to adjust the times returned.

Is it possible to get sunrise and sunset time from Google data?

Sunset/Sunrise time is almost the same every year on a particular day. So you can first try to build your clock with static data then later when it's working you can switch to real time google data.

What is the percentage difference between sunrise and Sunset?

This means sunrise would be 0.00% and sunset would be 100%. I wanted to create a easier version of this, having a program Google the sunset and sunrise times for the day and work from there.

How to calculate the value of minutes = 60 in Python?

I'm using Sun.py. Today I got the value of minutes = 60, UT = 12.9979740551 #10. Return min = round ( (UT - int (UT))*60,0) #add this after min calculate if min == 60: hr += 1 min = 0


2 Answers

Check out astral. Here's a slightly modified example from their docs:

>>> from astral import Astral
>>> city_name = 'London'
>>> a = Astral()
>>> a.solar_depression = 'civil'
>>> city = a[city_name]
>>> sun = city.sun(date=datetime.date(2009, 4, 22), local=True)

>>> if (sun['sunrise'] < T < sun['sunset']) and (light < threshold):
>>>    notifyUser()

If you use something like this example, please remember to change the city_name and date provided to city.sun.

like image 52
Kevin London Avatar answered Sep 27 '22 17:09

Kevin London


You can also try with suntime. An example from their doc below:

import datetime
from suntime import Sun, SunTimeException

latitude = 51.21
longitude = 21.01

sun = Sun(latitude, longitude)

# Get today's sunrise and sunset in UTC
today_sr = sun.get_sunrise_time()
today_ss = sun.get_sunset_time()
print('Today at Warsaw the sun raised at {} and get down at {} UTC'.
      format(today_sr.strftime('%H:%M'), today_ss.strftime('%H:%M')))

# On a special date in your machine's local time zone
abd = datetime.date(2014, 10, 3)
abd_sr = sun.get_local_sunrise_time(abd)
abd_ss = sun.get_local_sunset_time(abd)
print('On {} the sun at Warsaw raised at {} and get down at {}.'.
      format(abd, abd_sr.strftime('%H:%M'), abd_ss.strftime('%H:%M')))
like image 38
K. Stopa Avatar answered Sep 27 '22 18:09

K. Stopa