Rewrite suntimes.py
This commit is contained in:
@@ -1,127 +1,140 @@
|
||||
from math import pi, sin, asin, acos, cos
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
# Constants
|
||||
DAY_MS = 1000 * 60 * 60 * 24
|
||||
YEAR_1970 = 2440588
|
||||
|
||||
# Julian date of 01.01.2000 11:59 UTC
|
||||
YEAR_2000 = 2451545
|
||||
from math import pi, sin, asin, acos, cos, floor, atan, tan
|
||||
from datetime import datetime, timezone, time
|
||||
|
||||
|
||||
class Suntimes:
|
||||
def __init__(self, latitude: float, longitude: float) -> None:
|
||||
""" Calculates all time periods based on latitude and longitude
|
||||
Inspired by https://github.com/SatAgro/suntime
|
||||
|
||||
author: TobiZog
|
||||
"""
|
||||
def __init__(self) -> None:
|
||||
""" Initialization
|
||||
|
||||
Args:
|
||||
latitude (float): Latitude of the position
|
||||
longitude (float): Longitude of the position
|
||||
"""
|
||||
self.today = datetime.now()
|
||||
|
||||
|
||||
def calc_suntimes(self, latitude: float, longitude: float) -> None:
|
||||
self.latitude = latitude
|
||||
self.longitude = longitude
|
||||
self.date = (datetime.utcnow() - datetime(1970, 1, 1)).total_seconds() * 1000
|
||||
self.sun_events_of_day()
|
||||
self.calc_sun_events()
|
||||
|
||||
|
||||
def from_julian(self, j_date: float) -> datetime:
|
||||
""" Convert Julian date to a datetime
|
||||
|
||||
def to_range(self, value: float, range_max: float) -> float:
|
||||
""" Converting a variable to a given range
|
||||
|
||||
Args:
|
||||
j_date (float): Julian date
|
||||
value (float): The given value
|
||||
range_max (float): Upper boundary
|
||||
|
||||
Returns:
|
||||
datetime: Converted datetime object
|
||||
float: Corrected value inside range 0 to range_max
|
||||
"""
|
||||
j_date = (j_date + 0.5 - YEAR_1970) * DAY_MS
|
||||
return datetime.fromtimestamp(j_date / 1000)
|
||||
if value < 0:
|
||||
return value + range_max
|
||||
elif value >= range_max:
|
||||
return value - range_max
|
||||
else:
|
||||
return value
|
||||
|
||||
|
||||
def sun_events_of_day(self):
|
||||
def calc_sun_events(self):
|
||||
civial_dawn_start = self.calc_sunrise_sunset_time(True, 96)
|
||||
sunrise_start = self.calc_sunrise_sunset_time(True)
|
||||
morning_start = self.calc_sunrise_sunset_time(True, 89.167)
|
||||
|
||||
sunset_start = self.calc_sunrise_sunset_time(False, 89.167)
|
||||
civial_dusk_start = self.calc_sunrise_sunset_time(False)
|
||||
night_start = self.calc_sunrise_sunset_time(False, 96)
|
||||
|
||||
light_period_duration = (sunset_start - morning_start) / 8
|
||||
|
||||
noon_start = morning_start + 3 * light_period_duration
|
||||
afternoon_start = morning_start + 5 * light_period_duration
|
||||
evening_start = morning_start + 7 * light_period_duration
|
||||
|
||||
self.day_periods = [
|
||||
time(hour=0, minute=0),
|
||||
time(civial_dawn_start.hour, civial_dawn_start.minute),
|
||||
time(sunrise_start.hour, sunrise_start.minute),
|
||||
time(morning_start.hour, morning_start.minute),
|
||||
time(noon_start.hour, noon_start.minute),
|
||||
time(afternoon_start.hour, afternoon_start.minute),
|
||||
time(evening_start.hour, evening_start.minute),
|
||||
time(sunset_start.hour, sunset_start.minute),
|
||||
time(civial_dusk_start.hour, civial_dusk_start.minute),
|
||||
time(night_start.hour, night_start.minute),
|
||||
]
|
||||
|
||||
|
||||
|
||||
|
||||
def calc_sunrise_sunset_time(self, is_sunrise: bool, zenith=90.833) -> datetime:
|
||||
""" Calculate all values to estimate the day periods
|
||||
"""
|
||||
rad = pi / 180
|
||||
lw = rad * (-self.longitude)
|
||||
RAD = pi / 180
|
||||
|
||||
d = (self.date / DAY_MS) - 0.5 + YEAR_1970 - YEAR_2000
|
||||
n = round(d - 0.0009 - lw / (2 * pi))
|
||||
ds = 0.0009 + lw / (2 * pi) + n
|
||||
# Day of the year
|
||||
day_of_year = self.today.timetuple().tm_yday
|
||||
|
||||
self.M = rad * (357.5291 + 0.98560028 * ds)
|
||||
C = rad * (1.9148 * sin(self.M) + 0.02 * sin(2 * self.M) + 0.0003 * sin(3 * self.M))
|
||||
P = rad * 102.9372
|
||||
self.L = self.M + C + P + pi
|
||||
# 2
|
||||
lng_hour = self.longitude / 15
|
||||
|
||||
dec = asin(sin(rad * 23.4397) * sin(self.L))
|
||||
self.j_noon = YEAR_2000 + ds + 0.0053 * sin(self.M) - 0.0069 * sin(2 * self.L)
|
||||
if is_sunrise:
|
||||
t = day_of_year + ((6 - lng_hour) / 24)
|
||||
else:
|
||||
t = day_of_year + ((18 - lng_hour) / 24)
|
||||
|
||||
# -8 = Start of Civil dawn/dusk
|
||||
# -2 = Start of Sunrise/Sunset
|
||||
# 0 = Start/End of daylight phases
|
||||
self.angles = [-10, -4, 0]
|
||||
# 3
|
||||
M = (0.9856 * t) - 3.289
|
||||
|
||||
for i in range(0, len(self.angles)):
|
||||
self.angles[i] = rad * self.angles[i]
|
||||
self.angles[i] = acos((sin(self.angles[i]) - sin(rad * self.latitude) * sin(dec)) /
|
||||
(cos(rad * self.latitude) * cos(dec)))
|
||||
self.angles[i] = 0.0009 + (self.angles[i] + lw) / (2 * pi) + n
|
||||
# 4
|
||||
L = self.to_range(M + (1.916 * sin(RAD * M)) + (0.020 * sin(RAD * 2 * M)) + 282.634, 360)
|
||||
|
||||
# 5
|
||||
RA = self.to_range((1 / RAD) * atan(0.91764 * tan(RAD * L)), 360)
|
||||
RA += ((floor(L / 90)) * 90 - (floor(RA / 90)) * 90)
|
||||
RA /= 15
|
||||
|
||||
# 6
|
||||
sin_dec = 0.39782 * sin(RAD * L)
|
||||
cos_dec = cos(asin(sin_dec))
|
||||
|
||||
|
||||
def angle_correction(self, angle: float) -> float:
|
||||
""" Last correction for the sun angle
|
||||
# 7a
|
||||
cos_h = (cos(RAD * zenith) - (sin_dec * sin(RAD * self.latitude))) / (cos_dec * cos(RAD * self.latitude))
|
||||
|
||||
Args:
|
||||
angle (float): Angle before the correction
|
||||
# The sun rises or sets never
|
||||
if cos_h > 1 or cos_h < -1:
|
||||
return None
|
||||
|
||||
Returns:
|
||||
float: Angle after the correction
|
||||
"""
|
||||
return YEAR_2000 + angle + 0.0053 * sin(self.M) - 0.0069 * sin(2 * self.L)
|
||||
# 7b
|
||||
if is_sunrise:
|
||||
H = 360 - (1 / RAD) * acos(cos_h)
|
||||
else: #setting
|
||||
H = (1 / RAD) * acos(cos_h)
|
||||
|
||||
H = H / 15
|
||||
|
||||
# 8
|
||||
T = H + RA - (0.06571 * t) - 6.622
|
||||
|
||||
# 9
|
||||
UT = T - lng_hour
|
||||
UT = self.to_range(UT, 24) # UTC time in decimal format (e.g. 23.23)
|
||||
|
||||
|
||||
def get_time_period(self, period_nr: int) -> list:
|
||||
""" Get start and end time of a time period
|
||||
|
||||
Args:
|
||||
period_nr (int): Number between 0 and 9
|
||||
0 = Early Night
|
||||
1 = Civial dawn
|
||||
2 = Sunrise
|
||||
3 = Morning
|
||||
4 = Noon
|
||||
5 = Afternoon
|
||||
6 = Evening
|
||||
7 = Sunset
|
||||
8 = Civial Dusk
|
||||
9 = Late Night
|
||||
|
||||
Returns:
|
||||
list: Two datetime objects
|
||||
"""
|
||||
# Early night
|
||||
if period_nr == 0:
|
||||
res = [datetime.now().replace(hour=0, minute=0, second=0, microsecond=0),
|
||||
self.from_julian(2 * self.j_noon - self.angle_correction(self.angles[0])) - timedelta(minutes=1)]
|
||||
# 10
|
||||
hr = self.to_range(int(UT), 24)
|
||||
min = round((UT - int(UT))*60, 0)
|
||||
if min == 60:
|
||||
hr += 1
|
||||
min = 0
|
||||
|
||||
# Civilian dawn, Sunrise
|
||||
elif period_nr <= 2:
|
||||
res = [self.from_julian(2 * self.j_noon - self.angle_correction(self.angles[period_nr - 1])),
|
||||
self.from_julian(2 * self.j_noon - self.angle_correction(self.angles[period_nr])) - timedelta(minutes=1)]
|
||||
|
||||
# Morning, Noon, Afternoon, Evening
|
||||
elif period_nr <= 6:
|
||||
daylength = self.get_time_period(8)[0] - self.get_time_period(2)[1]
|
||||
|
||||
res = [self.get_time_period(2)[1] + ((daylength / 4) * (period_nr - 3)),
|
||||
self.get_time_period(2)[1] + ((daylength / 4) * (period_nr - 2))]
|
||||
|
||||
# Sunset, Civial dusk
|
||||
elif period_nr <= 8:
|
||||
res = [self.from_julian(self.angle_correction(self.angles[9 - period_nr])),
|
||||
self.from_julian(self.angle_correction(self.angles[8 - period_nr])) - timedelta(minutes=1)]
|
||||
|
||||
# Late Night
|
||||
elif period_nr == 9:
|
||||
res = [self.from_julian(YEAR_2000 + self.angles[0] + 0.0053 * sin(self.M) - 0.0069 * sin(2 * self.L)),
|
||||
datetime.now().replace(hour=23, minute=59, second=59, microsecond=0)]
|
||||
|
||||
return res
|
||||
res = datetime(self.today.year, self.today.month, self.today.day, hr, int(min))
|
||||
return res.replace(tzinfo=timezone.utc).astimezone(tz=None)
|
||||
|
||||
Reference in New Issue
Block a user