sun.py computes today's sunrise/sunset offline (NOAA equations) from the location install.sh now saves in screen.conf. screen.sh 'on' picks the evening level when it's dark out, and a new 'tick' state (cron, every 5 min) follows sunset/sunrise while leaving the night-off hours alone so it can't cut short a tap-to-wake. install.sh gains --evening-brightness.
34 lines
1.8 KiB
Python
Executable File
34 lines
1.8 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Prints today's local sunrise and sunset as "HHMM HHMM". Usage: sun.py LAT LON [SUNRISE_MIN SUNSET_MIN]
|
|
|
|
The optional minutes are added to sunrise/sunset (e.g. "0 30" = sunrise, and 30 min after sunset).
|
|
NOAA's approximate solar equations, good to a minute or two; no internet needed.
|
|
Prints nothing if the sun doesn't rise and set today (polar summer/winter).
|
|
"""
|
|
import datetime as dt, math, sys
|
|
|
|
def sun_times(lat, lon, day):
|
|
"""-> (sunrise, sunset) as UTC datetimes, or None."""
|
|
g = 2 * math.pi / 365 * (day.timetuple().tm_yday - 1)
|
|
eqtime = 229.18 * (0.000075 + 0.001868 * math.cos(g) - 0.032077 * math.sin(g)
|
|
- 0.014615 * math.cos(2 * g) - 0.040849 * math.sin(2 * g))
|
|
decl = (0.006918 - 0.399912 * math.cos(g) + 0.070257 * math.sin(g) - 0.006758 * math.cos(2 * g)
|
|
+ 0.000907 * math.sin(2 * g) - 0.002697 * math.cos(3 * g) + 0.00148 * math.sin(3 * g))
|
|
phi = math.radians(lat)
|
|
cos_ha = math.cos(math.radians(90.833)) / (math.cos(phi) * math.cos(decl)) - math.tan(phi) * math.tan(decl)
|
|
if not -1 <= cos_ha <= 1:
|
|
return None
|
|
ha = math.degrees(math.acos(cos_ha))
|
|
midnight = dt.datetime.combine(day, dt.time(), dt.timezone.utc)
|
|
at = lambda minutes: midnight + dt.timedelta(minutes=minutes) # UTC minutes after midnight
|
|
return at(720 - 4 * (lon + ha) - eqtime), at(720 - 4 * (lon - ha) - eqtime)
|
|
|
|
if __name__ == "__main__":
|
|
lat, lon = float(sys.argv[1]), float(sys.argv[2])
|
|
rise_extra, set_extra = (float(x) for x in (sys.argv[3:5] if len(sys.argv) > 4 else (0, 0)))
|
|
times = sun_times(lat, lon, dt.date.today())
|
|
if times:
|
|
rise, sset = times
|
|
hhmm = lambda t, extra: (t + dt.timedelta(minutes=extra)).astimezone().strftime("%H%M")
|
|
print(hhmm(rise, rise_extra), hhmm(sset, set_extra))
|