-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsuntime.py
More file actions
56 lines (46 loc) · 2.12 KB
/
suntime.py
File metadata and controls
56 lines (46 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
'''
Algorithmically calculate times for different sun positions based on location data using the
[ephem] module
'''
from datetime import datetime, timedelta, time
from time import localtime
from collections import namedtuple
import ephem
def suntime(day_offset: int, lat: str, lon: str, elevation: int = 0):
'''suntime function'''
utc_offset = localtime().tm_gmtoff
sun = ephem.Sun()
home = ephem.Observer()
home.lat = lat
home.lon = lon
home.elevation = elevation
home.compute_pressure()
home.date = datetime.combine(datetime.now().date(), time(
00, 00, 00)) - timedelta(seconds=utc_offset) - timedelta(days=day_offset)
print(
f"Evaluating sunrise/sunset times for: {(home.date.datetime() + timedelta(seconds=utc_offset)).date()} UTC+{utc_offset/3600}")
# Sunrise starting time
sunrise_start = home.next_rising(
sun).datetime() + timedelta(seconds=utc_offset)
# Calculate sunrise ending time
home.date = home.next_rising(sun).datetime()
sunrise_alt = (ephem.Sun(home).alt - ephem.Sun(home).radius)
while abs(sunrise_alt) > 1e-9:
multiplier = -sunrise_alt * 150
home.date = home.date.datetime() + timedelta(minutes=(1 * multiplier))
sunrise_alt = (ephem.Sun(home).alt - ephem.Sun(home).radius)
sunrise_end = home.date.datetime() + timedelta(seconds=utc_offset)
# Sunset ending time
sunset_end = home.next_setting(
sun).datetime() + timedelta(seconds=utc_offset)
# Calculate sunset starting time
home.date = home.next_setting(sun).datetime()
sunset_alt = (ephem.Sun(home).alt - ephem.Sun(home).radius)
while abs(sunset_alt) > 1e-9:
multiplier = -sunset_alt * 150
home.date = home.date.datetime() - timedelta(minutes=(1 * multiplier))
sunset_alt = (ephem.Sun(home).alt - ephem.Sun(home).radius)
sunset_start = home.date.datetime() + timedelta(seconds=utc_offset)
suntime_params = namedtuple("suntime_returns", [
"sunrise_start", "sunrise_end", "sunset_start", "sunset_end"])
return suntime_params(sunrise_start, sunrise_end, sunset_start, sunset_end)