Rewrite suntimes.py
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -6,3 +6,4 @@ custom_images/
|
||||
selected/
|
||||
__pycache__
|
||||
time_bar.svg
|
||||
time_bar_polylines.svg
|
||||
@@ -37,8 +37,7 @@ class Preferences:
|
||||
self.c_prefs = Cinnamon_Pref_Handler()
|
||||
|
||||
# Suntimes object
|
||||
self.suntimes = Suntimes(float(self.c_prefs.prefs[PrefenceEnums.LATITUDE_AUTO]),
|
||||
float(self.c_prefs.prefs[PrefenceEnums.LONGITUDE_AUTO]))
|
||||
self.suntimes = Suntimes()
|
||||
|
||||
########## UI objects ##########
|
||||
|
||||
@@ -160,14 +159,27 @@ class Preferences:
|
||||
|
||||
time_periods_min.append(int(time_str[0:2]) * 60 + int(time_str[3:5]))
|
||||
else:
|
||||
if self.c_prefs.prefs[PrefenceEnums.PERIOD_SOURCE] == PeriodSourceEnum.NETWORKLOCATION:
|
||||
self.suntimes.calc_suntimes(float(self.c_prefs.prefs[PrefenceEnums.LATITUDE_AUTO]),
|
||||
float(self.c_prefs.prefs[PrefenceEnums.LONGITUDE_AUTO]))
|
||||
else:
|
||||
self.suntimes.calc_suntimes(float(self.etr_latitude.get_text()), float(self.etr_longitude.get_text()))
|
||||
|
||||
|
||||
# Get all time periods. Store the minutes to the list and print the values to the text views
|
||||
for i in range(0, 10):
|
||||
time_range = self.suntimes.get_time_period(i)
|
||||
self.etr_periods[i].set_text(
|
||||
str(time_range[0].hour).rjust(2, '0') + ":" + str(time_range[0].minute).rjust(2, '0') +\
|
||||
" - " + str(time_range[1].hour).rjust(2, '0') + ":" + str(time_range[1].minute).rjust(2, '0'))
|
||||
time_range_now = self.suntimes.day_periods[i]
|
||||
|
||||
time_periods_min.append(time_range[0].hour * 60 + time_range[0].minute)
|
||||
if i != 9:
|
||||
time_range_next = self.suntimes.day_periods[i + 1]
|
||||
else:
|
||||
time_range_next = time(hour=23, minute=59)
|
||||
|
||||
self.etr_periods[i].set_text(
|
||||
str(time_range_now.hour).rjust(2, '0') + ":" + str(time_range_now.minute).rjust(2, '0') +\
|
||||
" - " + str(time_range_next.hour).rjust(2, '0') + ":" + str(time_range_next.minute).rjust(2, '0'))
|
||||
|
||||
time_periods_min.append(time_range_now.hour * 60 + time_range_now.minute)
|
||||
|
||||
# Create time bar
|
||||
self.time_bar_chart.create_bar_chart_with_polylines(PREFERENCES_URI, 1200, 150, time_periods_min)
|
||||
@@ -291,23 +303,43 @@ class Preferences:
|
||||
|
||||
|
||||
def on_spb_period_value_changed(self, spin_button):
|
||||
spin_button_name = Gtk.Buildable.get_name(spin_button)
|
||||
index = int(spin_button_name[11:12]) - 1
|
||||
hours = self.spb_periods_hour[index].get_value()
|
||||
minutes = self.spb_periods_minute[index].get_value()
|
||||
""" Callback if one of the time spinners (minute or hour) will be clicked
|
||||
|
||||
time_value = datetime(2024,1,1, int(hours), int(minutes))
|
||||
time_str = str(time_value.hour).rjust(2, '0') + ":" + str(time_value.minute).rjust(2, '0')
|
||||
(1) (2) (3)
|
||||
Previous period Current period Next period
|
||||
12:34 - 14:40 14:41 - 16:20 16:21 - 17:30
|
||||
^
|
||||
Variable to change
|
||||
|
||||
self.c_prefs.prefs["period_%s_custom_start_time" % (index + 1)] = time_str
|
||||
Args:
|
||||
spin_button (_type_): _description_
|
||||
"""
|
||||
spin_button_name = Gtk.Buildable.get_name(spin_button)
|
||||
index = int(spin_button_name[11:12]) - 1
|
||||
|
||||
next_time = time_value - timedelta(minutes=1)
|
||||
time_str = str(next_time.hour).rjust(2, '0') + ":" + str(next_time.minute).rjust(2, '0')
|
||||
# Determe time string and store to prefs
|
||||
time_current_start = datetime(2024,1,1, int(self.spb_periods_hour[index].get_value()), int(self.spb_periods_minute[index].get_value()))
|
||||
time_current_start_str = str(time_current_start.hour).rjust(2, '0') + ":" + str(time_current_start.minute).rjust(2, '0')
|
||||
|
||||
# Update the start time of the previous period
|
||||
self.lb_period_end[index].set_text(time_str)
|
||||
self.c_prefs.prefs["period_%s_custom_start_time" % (index + 1)] = time_current_start_str
|
||||
|
||||
self.refresh_chart()
|
||||
|
||||
# (1) Update the start time of the previous period
|
||||
time_previous_end = time_current_start - timedelta(minutes=1)
|
||||
self.lb_period_end[index].set_text(str(time_previous_end.hour).rjust(2, '0') + ":" + str(time_previous_end.minute).rjust(2, '0'))
|
||||
|
||||
# todo:
|
||||
# hours_next = self.spb_periods_hour[index + 1].get_value()
|
||||
# minutes_next = self.spb_periods_minute[index + 1].get_value()
|
||||
# time_next_start = datetime(2024, 1, 1, int(hours_next), int(minutes_next))
|
||||
|
||||
# if time_next_start < time_current_start:
|
||||
# # (2) Update the end time of the current period
|
||||
# current_time_end = time_current_start + timedelta(minutes=1)
|
||||
# time_current_start_str = str(current_time_end.hour).rjust(2, '0') + ":" + str(current_time_end.minute).rjust(2, '0')
|
||||
|
||||
|
||||
self.refresh_chart()
|
||||
|
||||
|
||||
def on_spb_network_location_refresh_time_changed(self, spin_button):
|
||||
@@ -317,6 +349,7 @@ class Preferences:
|
||||
def on_etr_longitude_changed(self, entry):
|
||||
try:
|
||||
self.c_prefs.prefs[PrefenceEnums.LONGITUDE_CUSTOM] = float(entry.get_text())
|
||||
self.refresh_chart()
|
||||
except:
|
||||
pass
|
||||
|
||||
@@ -324,6 +357,7 @@ class Preferences:
|
||||
def on_etr_latitude_changed(self, entry):
|
||||
try:
|
||||
self.c_prefs.prefs[PrefenceEnums.LATITUDE_CUSTOM] = float(entry.get_text())
|
||||
self.refresh_chart()
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
@@ -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
|
||||
# 10
|
||||
hr = self.to_range(int(UT), 24)
|
||||
min = round((UT - int(UT))*60, 0)
|
||||
if min == 60:
|
||||
hr += 1
|
||||
min = 0
|
||||
|
||||
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)]
|
||||
|
||||
# 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