New time period calculation system, bind it also to the UI
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
import os, json
|
||||
from enums.PreferenceEnums import PrefenceEnums
|
||||
|
||||
# Location of the Cinnamon preference file since Cinnamon 5.4
|
||||
pref_location = os.path.expanduser("~") + \
|
||||
"/.config/cinnamon/spices/cinnamon-dynamic-wallpaper@TobiZog/cinnamon-dynamic-wallpaper@TobiZog.json"
|
||||
|
||||
|
||||
def write_to_preferences(parameter: PrefenceEnums, value: str):
|
||||
""" Write a preference value to the JSON file
|
||||
|
||||
Args:
|
||||
parameter (PrefenceEnums): Name of the parameter
|
||||
value (str): Value to write
|
||||
"""
|
||||
with open(pref_location, "r") as pref_file:
|
||||
pref_data = json.load(pref_file)
|
||||
|
||||
if parameter in pref_data:
|
||||
pref_data[parameter]["value"] = value
|
||||
else:
|
||||
pref_data[parameter] = {
|
||||
"type": "entry",
|
||||
"default": "",
|
||||
"description": "",
|
||||
"value": value
|
||||
}
|
||||
|
||||
with open(pref_location, "w") as pref_file:
|
||||
json.dump(pref_data, pref_file, separators=(',', ':'), indent=4)
|
||||
|
||||
|
||||
def read_str_from_preferences(parameter: PrefenceEnums) -> str:
|
||||
""" Read a value from the JSON file
|
||||
|
||||
Args:
|
||||
parameter (PrefenceEnums): Name of the parameter to get
|
||||
|
||||
Returns:
|
||||
str: Value of the parameter
|
||||
"""
|
||||
with open(pref_location, "r") as pref_file:
|
||||
pref_data = json.load(pref_file)
|
||||
|
||||
if parameter in pref_data:
|
||||
return pref_data[parameter]["value"]
|
||||
else:
|
||||
return ""
|
||||
|
||||
def read_int_from_preferences(parameter: PrefenceEnums) -> int:
|
||||
value = read_str_from_preferences(parameter)
|
||||
|
||||
if value == "":
|
||||
return 0
|
||||
else:
|
||||
return int(value)
|
||||
@@ -0,0 +1,182 @@
|
||||
# # import datetime, math
|
||||
|
||||
# # day_ms = 1000 * 60 * 60 * 24
|
||||
# # year_1970 = 2440588
|
||||
# # year_2000 = 2451545
|
||||
|
||||
# # def from_julian(j) -> datetime.date:
|
||||
# # return datetime.date(ms_date = (j + 0.5 - year_1970))
|
||||
|
||||
# # def sun_events_of_day(latitude, longitude, date):
|
||||
# # rad = math.pi / 180
|
||||
# # lw = rad * (-longitude)
|
||||
|
||||
# # d = (date / day_ms) - 0.5 + year_1970 - year_2000
|
||||
# # n = math.floor(d - 0.0009 - lw / (2 * math.pi))
|
||||
# # ds = 0.0009 + lw / (2 * math.pi) + n
|
||||
|
||||
# # M = rad * (357.5291 + 0.98560028 * ds)
|
||||
# # C = rad * (1.9148 * math.sin(M) + 0.02 * math.sin(2 * M) + 0.0003 * math.sin(3 * M))
|
||||
# # P = rad * 102.9372
|
||||
# # L = M + C + P + math.pi
|
||||
|
||||
# # dec = math.asin(math.sin(rad * 23.4397) * math.sin(L))
|
||||
|
||||
# # angles = [-0.833, -6]
|
||||
|
||||
# # for angle in angles:
|
||||
# # angle *= rad
|
||||
# # angle = math.acos((math.sin(angle) - math.sin(rad * latitude) * math.sin(dec)) / (math.cos(rad * latitude) * math.cos(dec)))
|
||||
# # angle = 0.0009 + (angle + lw) / (2 * math.pi) + n
|
||||
|
||||
# # j_noon = year_2000 + ds + 0.0053 * math.sin(M) - 0.0069 * math.sin(2 * L)
|
||||
|
||||
# # print(from_julian(j_noon - (year_2000 + angles[1] + 0.0053 * math.sin(M) - 0.0069 * math.sin(2 * L) - j_noon)))
|
||||
|
||||
|
||||
# # sun_events_of_day(48.1663, 11.5683, datetime.datetime.now())
|
||||
|
||||
|
||||
# import datetime, math
|
||||
# from math import cos, sin, acos, asin, tan
|
||||
# from math import degrees as deg, radians as rad
|
||||
# from datetime import date, datetime, time
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# DAY_MS = 1000 * 60 * 60 * 24
|
||||
# YEAR_1970 = 2440588
|
||||
# YEAR_2000 = 2451545
|
||||
|
||||
# def date_to_julian(year, month, day):
|
||||
# if month <= 2:
|
||||
# year += 1
|
||||
# month += 12
|
||||
|
||||
# A = math.trunc(year / 100.)
|
||||
# B = 2 - A + math.trunc(A / 4.)
|
||||
|
||||
# if year < 0:
|
||||
# C = math.trunc((365.25 * year) - 0.75)
|
||||
# else:
|
||||
# C = math.trunc(365.25 * year)
|
||||
|
||||
# D = math.trunc(30.6001 * (month + 1))
|
||||
|
||||
# return B + C + D + day + 1720994.5
|
||||
|
||||
|
||||
# latitude_rad = rad(latitude)
|
||||
|
||||
|
||||
# n = date_to_julian(datetime.now().year, datetime.now().month, datetime.now().day) - YEAR_2000 + 0.0008
|
||||
# jstar = n - deg(longitude) / 360
|
||||
|
||||
# M_deg = (357.5291 + 0.98560028 * jstar) % 360
|
||||
# M = M_deg * math.pi / 180
|
||||
|
||||
# C = 1.9148 * sin(M) + 0.0200 * sin(2*M) + 0.003 * sin(3*M)
|
||||
|
||||
# lamda = math.fmod(M_deg + C + 180 + 102.9372, 360) * math.pi / 180
|
||||
|
||||
# Jtransit = 2451545.5 + jstar + 0.0053 * sin(M) - 0.0069 * sin(2 * lamda)
|
||||
|
||||
# earth_tilt_rad = rad(23.44)
|
||||
# angle_delta = asin(sin(lamda) * sin(earth_tilt_rad))
|
||||
# sun_disc_rad = rad(-0.83)
|
||||
|
||||
# os_omega =
|
||||
|
||||
|
||||
# print(date_to_julian(2023, 12, 12))
|
||||
|
||||
# #s = sun(lat=48.1663, long=11.5683)
|
||||
|
||||
from math import pi, sin, asin, acos, cos
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
DAY_MS = 1000 * 60 * 60 * 24
|
||||
YEAR_1970 = 2440588
|
||||
YEAR_2000 = 2451545
|
||||
|
||||
class Suntimes:
|
||||
def __init__(self, latitude, longitude) -> None:
|
||||
self.latitude = latitude
|
||||
self.longitude = longitude
|
||||
self.date = (datetime.utcnow() - datetime(1970, 1, 1)).total_seconds() * 1000
|
||||
self.sun_events_of_day()
|
||||
|
||||
def from_julian(self, j_date) -> datetime:
|
||||
j_date = (j_date + 0.5 - YEAR_1970) * DAY_MS
|
||||
return datetime.fromtimestamp(j_date / 1000)
|
||||
|
||||
def sun_events_of_day(self):
|
||||
rad = pi / 180
|
||||
lw = rad * (-self.longitude)
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
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)
|
||||
|
||||
# -8 = Start of Civil dawn/dusk
|
||||
# -2 = Start of Sunrise/Sunset
|
||||
# 0 = Start/End of daylight phases
|
||||
self.angles = [-10, -4, 0]
|
||||
|
||||
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
|
||||
|
||||
def angle_correction(self, angle: float) -> datetime:
|
||||
return (YEAR_2000 + angle + 0.0053 * sin(self.M) - 0.0069 * sin(2 * self.L))
|
||||
|
||||
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
|
||||
"""
|
||||
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)]
|
||||
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)]
|
||||
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))]
|
||||
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)]
|
||||
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
|
||||
@@ -0,0 +1,128 @@
|
||||
import math
|
||||
|
||||
image_code = []
|
||||
|
||||
colors = [
|
||||
"00193dff",
|
||||
"05597fff",
|
||||
"54babfff",
|
||||
"bfe3c2ff",
|
||||
"ffbf6bff",
|
||||
"fdb55cff",
|
||||
"f37f73ff",
|
||||
"7f3d85ff",
|
||||
"4a217aff",
|
||||
"00193dff"
|
||||
]
|
||||
|
||||
bar_pos_x = []
|
||||
|
||||
def create_bar_chart(image_width, image_height, times):
|
||||
create_bar(image_width, image_height, times)
|
||||
create_polylines(image_width, image_height)
|
||||
create_time_markers(image_width, image_height)
|
||||
|
||||
# Write to file
|
||||
image_code.insert(0, '<svg xmlns="http://www.w3.org/2000/svg" width="%s" height="%s">' % (image_width, image_height))
|
||||
image_code.append('</svg>')
|
||||
|
||||
file = open("time_bar.svg", "w")
|
||||
for i in image_code:
|
||||
file.write(i + '\n')
|
||||
|
||||
|
||||
def create_bar(image_width: int, image_height: int, times: list):
|
||||
""" Generates the code for the horizontal multi-color bar chart
|
||||
|
||||
Args:
|
||||
image_width (int): Total width of the image
|
||||
image_height (int): Total height of the image
|
||||
times (list): List of start times of the periods, in minutes
|
||||
"""
|
||||
x = 0
|
||||
y = 40
|
||||
width = 0
|
||||
height = image_height - 80
|
||||
times.append(1440)
|
||||
|
||||
# Adding the bar parts
|
||||
for i in range(1, len(times)):
|
||||
width = math.ceil((((100 / 1440) * (times[i] - times[i - 1]) / 100) * image_width))
|
||||
|
||||
image_code.append(
|
||||
'<rect fill="#%s" x="%s" y="%s" width="%s" height="%s"/>' % (colors[i - 1], x, y, width, height)
|
||||
)
|
||||
|
||||
bar_pos_x.append(x)
|
||||
x += width
|
||||
|
||||
|
||||
def create_time_markers(image_width: int, image_height: int):
|
||||
""" Generates the code for the vertical hour markers
|
||||
|
||||
Args:
|
||||
image_width (int): Total width of the image
|
||||
image_height (int): Total height of the image
|
||||
"""
|
||||
for i in range(0, 8):
|
||||
image_code.append(
|
||||
'<line x1="%s" y1="40" x2="%s" y2="%s" stroke="gray" stroke-width="2" />' %
|
||||
(i * (image_width // 8), i * (image_width // 8), image_height - 40)
|
||||
)
|
||||
|
||||
image_code.append(
|
||||
'<text x="%s" y="%s" fill="gray" font-size="20" font-family="Liberation Sans">%s</text>' %
|
||||
(i * (image_width // 8) + 5, image_height - 45, i * 3)
|
||||
)
|
||||
|
||||
|
||||
def create_polylines(image_width: int, image_height: int):
|
||||
""" Generates the code for the polylines which connect the images with the bar sections
|
||||
|
||||
Args:
|
||||
image_width (int): Total width of the image
|
||||
image_height (int): Total height of the image
|
||||
"""
|
||||
bar_x_start = 0
|
||||
|
||||
bar_pos_x.append(image_width)
|
||||
|
||||
for i in range(0, len(bar_pos_x) - 1):
|
||||
# X-Middle of a bar
|
||||
bar_mid = bar_x_start + (bar_pos_x[i + 1] - bar_x_start) / 2
|
||||
|
||||
# Position of the image in the window
|
||||
image_x = (image_width - 32) / 10 + ((i // 2) % 5) * image_width / 5
|
||||
|
||||
# i == 0, 2, 4, ... => Upper Polylines
|
||||
if (i % 2 == 0):
|
||||
polyline_y = 0
|
||||
else:
|
||||
polyline_y = image_height
|
||||
|
||||
if i == 0 or i == 8:
|
||||
polyline_x = 30
|
||||
elif i == 2 or i == 6:
|
||||
polyline_x = 20
|
||||
elif i == 1 or i == 9:
|
||||
polyline_x = image_height - 30
|
||||
elif i == 3 or i == 7:
|
||||
polyline_x = image_height - 20
|
||||
elif i == 5:
|
||||
polyline_x = image_height - 10
|
||||
else:
|
||||
polyline_x = 10
|
||||
|
||||
image_code.append(
|
||||
'<polyline points="%s,%s %s,%s %s,%s %s,%s" stroke="#%s" fill="none" stroke-width="5" />' %
|
||||
(image_x, polyline_y, image_x, polyline_x, bar_mid, polyline_x, bar_mid, image_height / 2, colors[i])
|
||||
)
|
||||
|
||||
# Store the end point of the bar as start point of the next
|
||||
bar_x_start = bar_pos_x[i + 1]
|
||||
|
||||
# Hannover
|
||||
#create_bar_chart(1036, 180, [0, 455, 494, 523, 673, 792, 882, 941, 973, 1013])
|
||||
|
||||
# Other Test bar
|
||||
#create_bar_chart(1036, 180, [0, 180, 190, 523, 673, 792, 882, 941, 973, 1300])
|
||||
Reference in New Issue
Block a user