commit 165c69e4c3518c4adbf1da9c59780637afcdfd43 Author: Lukas Winkler Date: Fri May 18 21:59:06 2018 +0200 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2d7acfd --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +config.py +sonnenuntergang.txt +.idea/ +__pycache__/ +average.txt diff --git a/guess.py b/guess.py new file mode 100644 index 0000000..9b9840d --- /dev/null +++ b/guess.py @@ -0,0 +1,35 @@ +from datetime import datetime, timedelta, time + +midday = time(12) + +import astropy.coordinates as coord +from astropy.time import Time + +import config + + +def time2altitude(time: datetime) -> float: + astro_time = Time(time.astimezone()) + altaz = coord.AltAz(location=config.loc, obstime=astro_time) + sun = coord.get_sun(astro_time) + return sun.transform_to(altaz).alt.degree + + +def get_time(altitude, time=False): + lower = datetime.combine(datetime.now().date(), midday) + upper = lower + timedelta(hours=12) + + while upper - lower > timedelta(seconds=30): + middle = lower + (upper - lower) / 2 + if time2altitude(middle) > altitude: + lower = middle + else: + upper = middle + # print(upper, lower) + + return lower + (upper - lower) / 2 + + +if __name__ == "__main__": + target = 14.480046611643763 + print(get_time(target)) diff --git a/import.py b/import.py new file mode 100644 index 0000000..ccdbcad --- /dev/null +++ b/import.py @@ -0,0 +1,40 @@ +from datetime import datetime +from statistics import mean, stdev + +import astropy.coordinates as coord +from astropy.time import Time + +import config + +loc = coord.EarthLocation(lon=config.lon, + lat=config.lat, height=config.height) + +with open("sonnenuntergang.txt") as f: + content = f.readlines() + lines = [line.strip() for line in content] + +angles = [] + +for line in lines: + if "#" in line: + print("skipped") + continue + parsetime = datetime.strptime("2018 " + line, "%Y %d.%m %H:%M").astimezone() + print(parsetime.isoformat()) + time = Time(parsetime) + print(time) + altaz = coord.AltAz(location=loc, obstime=time) + sun = coord.get_sun(time) + + altitude = sun.transform_to(altaz).alt.degree + print(altitude) + angles.append(altitude) + +average = mean(angles) +stdev = stdev(angles, average) +print(stdev) + +print(average) + +with open("average.txt", "w") as f: + f.write(str(average) + "\n" + str(stdev)) diff --git a/main.py b/main.py new file mode 100644 index 0000000..5ca2393 --- /dev/null +++ b/main.py @@ -0,0 +1,38 @@ +import time + +import schedule + +import guess + + +def send_notification(on_time=False): + print("DAS IST EIN TEST") + return schedule.CancelJob + + +def create_schedule(): + with open("average.txt") as file: + lines = file.readlines() + altitude = float(lines[0].strip()) + standard_derivation = float(lines[1].strip()) + print(altitude, standard_derivation) + + s = schedule.every().day + + s.at_time = guess.get_time(altitude).time() + s.at_time = guess.get_time(altitude).time() + s.do(send_notification, on_time=True) + + s = schedule.every().day + print(guess.get_time(altitude + standard_derivation * 3).time()) + s.at_time = guess.get_time(altitude + standard_derivation * 3).time() + s.do(send_notification) + + +create_schedule() + +schedule.every().day.at("12:00").do(create_schedule) + +while True: + schedule.run_pending() + time.sleep(1) diff --git a/sendmail.py b/sendmail.py new file mode 100644 index 0000000..e4fd451 --- /dev/null +++ b/sendmail.py @@ -0,0 +1,15 @@ +import smtplib +from email.mime.text import MIMEText + +from config import by, to + + +def sendmail(): + msg = MIMEText("test") + + msg['Subject'] = "subject" + msg['From'] = by + msg['To'] = to + + s = smtplib.SMTP('localhost') + s.sendmail(by, [to], msg.as_string())