Script for å beregne soloppgang og solnedgang, til bruk med raspistill
authorEinar Jørgen Haraldseid <einar@haraldseid.net>
Wed, 13 Dec 2023 17:49:06 +0000 (18:49 +0100)
committerEinar Jørgen Haraldseid <einar@haraldseid.net>
Wed, 13 Dec 2023 17:49:06 +0000 (18:49 +0100)
sun.py [new file with mode: 0644]

diff --git a/sun.py b/sun.py
new file mode 100644 (file)
index 0000000..c46cc67
--- /dev/null
+++ b/sun.py
@@ -0,0 +1,52 @@
+#!/usr/bin/python3
+from suncalc import get_position, get_times
+from datetime import datetime, timedelta, date
+
+debug = False
+#debug = True
+
+now = datetime.now()
+#now = datetime(2023, 12, 13, 0, 0)
+date = now.replace(hour=12, minute=0, second=0, microsecond=0)
+
+lon = 10.71
+lat = 60.772
+height = 100
+
+sun = get_times(date, lon, lat, height)
+
+if type(sun["nautical_dawn"]) is datetime:
+  sunup = sun["nautical_dawn"] + timedelta(hours=1)
+else:
+  sunup = now
+if type(sun["nautical_dusk"]) is datetime:
+  sundown = sun["nautical_dusk"] + timedelta(hours=1)
+else:
+  sundown = now + timedelta(hours=23, minutes=59, seconds=50)
+
+# If sunup is in the future
+if now < sunup:
+  countdown = (sunup - now).total_seconds()
+  countdown_read = sunup - now
+  duration = (sundown - sunup).total_seconds() * 1000
+  duration_read = sundown - sunup
+else:
+  countdown = 0
+  countdown_read = 0
+  duration = (sundown - now).total_seconds() * 1000
+  duration_read = sundown - now
+
+if int(duration) < 0:
+  print("Sun already set, fool")
+  exit(1)
+
+if debug:
+  print("      Now is ", now)
+  print("     Date is ", date)
+  print("   Sun up is ", sunup)
+  print(" Sun down is ", sundown)
+  print("Countdown is ", countdown_read)
+  print(" Duration is ", duration_read)
+  print("")
+
+print(int(countdown), int(duration), sep='\t')