Two new views, picked from mockups. `sky`: a star chart of the sky over Zlín as seen lying on your back, with the planets, the moon in its phase, tonight's darkness, cloud for the next 12 hours and the next visible ISS pass. `iss`: a dotted world map with day and night, the ISS and its ground track, and the next pass over Zlín with the cloud cover for that hour. Positions are calculated locally with ephem; only the ISS orbit (CelesTrak, every 6 h, retried gently) and cloud cover (Open-Meteo) are fetched. The poller gained a per-source retry delay for that. Co-Authored-By: Claude Opus 5.5 <[email protected]>
172 lines
8.3 KiB
Python
172 lines
8.3 KiB
Python
"""The sky over Zlín right now: a star chart as seen lying on your back, and what is up tonight."""
|
||
import math
|
||
from datetime import datetime, timedelta
|
||
|
||
from PIL import Image, ImageDraw
|
||
|
||
import astro
|
||
import skykit
|
||
from astro import Body, direction
|
||
from poller import Poller
|
||
from sky_data import CloudHour, Tle, cloud_at, cloud_story
|
||
from skykit import H, W, hm, mix, tracked
|
||
from view import Patch
|
||
|
||
BG, RING, LINE, LABEL = (7, 10, 24), (30, 42, 72), (46, 62, 100), (120, 140, 182)
|
||
TEXT, SUB, STAR, ISS = (236, 238, 246), (150, 165, 200), (214, 222, 242), (130, 220, 230)
|
||
DOME_NIGHT, DOME_TWILIGHT, DOME_DAY = (12, 19, 42), (17, 28, 60), (26, 46, 90)
|
||
CLEAR, CLOUDY = (40, 120, 90), (120, 124, 140)
|
||
STALE_BG, STALE_INK = (240, 200, 90), BG
|
||
COLOURS = {"Měsíc": (235, 235, 220), "Merkur": (180, 170, 160), "Venuše": (255, 248, 220), "Mars": (255, 120, 80),
|
||
"Jupiter": (245, 222, 180), "Saturn": (240, 200, 110), "Slunce": (255, 214, 90)}
|
||
LINES = (("Vega", "Deneb"), ("Deneb", "Altair"), ("Altair", "Vega"),
|
||
("Dubhe", "Merak"), ("Merak", "Phecda"), ("Phecda", "Megrez"), ("Megrez", "Dubhe"),
|
||
("Megrez", "Alioth"), ("Alioth", "Mizar"), ("Mizar", "Alkaid"),
|
||
("Markab", "Scheat"), ("Scheat", "Alpheratz"), ("Alpheratz", "Algenib"), ("Algenib", "Markab"),
|
||
("Alpheratz", "Mirach"), ("Mirach", "Almach"), ("Deneb", "Sadr"), ("Sadr", "Albireo"), ("Mirfak", "Algol"),
|
||
("Betelgeuse", "Bellatrix"), ("Bellatrix", "Mintaka"), ("Mintaka", "Alnilam"), ("Alnilam", "Alnitak"),
|
||
("Alnitak", "Saiph"), ("Saiph", "Rigel"), ("Rigel", "Mintaka"), ("Alnitak", "Betelgeuse"))
|
||
CX, CY, R = 162, 162, 150 # the dome
|
||
PANEL_X = 326
|
||
RISING_WITHIN = timedelta(hours=10)
|
||
MAX_ROWS = 4
|
||
|
||
|
||
def dome_xy(alt: float, az: float) -> tuple[float, float]:
|
||
"""Zenith in the centre, horizon on the rim, north up and east on the left (a chart held overhead)."""
|
||
r = R * (90 - alt) / 90
|
||
return CX - r * math.sin(math.radians(az)), CY - r * math.cos(math.radians(az))
|
||
|
||
|
||
def panel_rows(bodies: tuple[Body, ...], moon_pct: float, now: datetime) -> list[tuple[str, str]]:
|
||
"""What is up (highest first), then what rises in the next hours (soonest first); at most MAX_ROWS."""
|
||
up = sorted((b for b in bodies if b.alt > 0), key=lambda b: -b.alt)
|
||
rising = sorted((b for b in bodies if b.alt <= 0 and b.rise is not None and b.rise - now <= RISING_WITHIN),
|
||
key=lambda b: b.rise)
|
||
rows = [(b.name, f"{moon_pct:.0f} % · {direction(b.az)}" if b.name == "Měsíc" else f"{b.alt:.0f}° {direction(b.az)}")
|
||
for b in up]
|
||
rows += [(b.name, f"↑ {hm(b.rise)}") for b in rising]
|
||
return rows[:MAX_ROWS]
|
||
|
||
|
||
class SkyView:
|
||
name = "sky"
|
||
TLE_STALE_AFTER = 3 * 86400.0 # ISS predictions drift once the orbit is days old
|
||
CLOUDS_STALE_AFTER = 3600.0
|
||
|
||
def __init__(self, tle: Poller[Tle], clouds: Poller[tuple[CloudHour, ...]]) -> None:
|
||
self._tle = tle
|
||
self._clouds = clouds
|
||
|
||
def start(self) -> None:
|
||
self._tle.start()
|
||
self._clouds.start()
|
||
|
||
def stop(self) -> None:
|
||
self._tle.stop()
|
||
self._clouds.stop()
|
||
|
||
@staticmethod
|
||
def _old(poller: Poller, limit: float) -> bool:
|
||
age = poller.age()
|
||
return age is not None and age > limit
|
||
|
||
def frame_key(self, now: datetime):
|
||
return (now.strftime("%Y%m%d%H%M"), self._tle.version, self._clouds.version,
|
||
self._old(self._tle, self.TLE_STALE_AFTER), self._old(self._clouds, self.CLOUDS_STALE_AFTER))
|
||
|
||
def patch(self, now: datetime) -> Patch | None:
|
||
return None
|
||
|
||
def render(self, now: datetime) -> Image.Image:
|
||
img = Image.new("RGB", (W, H), BG)
|
||
d = ImageDraw.Draw(img)
|
||
bodies = astro.bodies(now)
|
||
moon_pct, waxing = astro.moon_phase(now)
|
||
self._dome(img, d, now, bodies, moon_pct, waxing)
|
||
self._panel(d, now, bodies, moon_pct)
|
||
return img
|
||
|
||
@staticmethod
|
||
def _dome(img, d, now, bodies, moon_pct, waxing) -> None:
|
||
sun = astro.sun(now)
|
||
fill = DOME_DAY if sun.alt > 0 else DOME_TWILIGHT if sun.alt > -12 else DOME_NIGHT
|
||
d.ellipse((CX - R, CY - R, CX + R, CY + R), fill=fill, outline=RING, width=2)
|
||
for alt in (30, 60):
|
||
r = R * (90 - alt) / 90
|
||
for k in range(0, 360, 4):
|
||
d.point((CX + r * math.cos(math.radians(k)), CY + r * math.sin(math.radians(k))), fill=RING)
|
||
|
||
stars = {s.name: s for s in astro.stars(now)}
|
||
for a, b in LINES:
|
||
if a in stars and b in stars:
|
||
d.line((*dome_xy(stars[a].alt, stars[a].az), *dome_xy(stars[b].alt, stars[b].az)), fill=LINE)
|
||
for s in stars.values():
|
||
x, y = dome_xy(s.alt, s.az)
|
||
r = max(0.8, 2.8 - 0.8 * s.mag)
|
||
d.ellipse((x - r, y - r, x + r, y + r), fill=STAR)
|
||
|
||
visible = [b for b in (sun, *bodies) if b.alt > 0] # the sun and moon claim their label space first
|
||
for b in visible:
|
||
x, y = dome_xy(b.alt, b.az)
|
||
if b.name == "Měsíc":
|
||
skykit.moon_disc(img, x, y, 9, moon_pct, waxing, lit=COLOURS[b.name], dark=(40, 44, 60))
|
||
else:
|
||
r = 7 if b.name == "Slunce" else 4
|
||
d.ellipse((x - r, y - r, x + r, y + r), fill=COLOURS[b.name])
|
||
labels: list[tuple[float, float, float, float]] = []
|
||
for b in visible:
|
||
x, y = dome_xy(b.alt, b.az)
|
||
colour = COLOURS[b.name]
|
||
f = skykit.font(11, 600)
|
||
right = x + 12 + d.textlength(b.name, font=f) <= CX + R # else put the label on the left
|
||
at, anchor = ((x + 12, y + 4), "ls") if right else ((x - 12, y + 4), "rs")
|
||
box = d.textbbox(at, b.name, font=f, anchor=anchor)
|
||
if not any(box[0] < o[2] and o[0] < box[2] and box[1] < o[3] and o[1] < box[3] for o in labels):
|
||
d.text(at, b.name, font=f, fill=colour, anchor=anchor)
|
||
labels.append(box)
|
||
for text, (x, y) in (("S", (CX, 24)), ("J", (CX, 300)), ("V", (24, CY)), ("Z", (300, CY))):
|
||
d.text((x, y), text, font=skykit.font(12, 700), fill=LABEL, anchor="mm")
|
||
|
||
def _panel(self, d, now, bodies, moon_pct) -> None:
|
||
x0, x1 = PANEL_X, 468
|
||
tracked(d, (x0, 24), "OBLOHA · ZLÍN", skykit.font(10, 650), LABEL)
|
||
d.text((x0, 60), hm(now), font=skykit.font(30, 700), fill=TEXT, anchor="ls")
|
||
dark = astro.darkness(now)
|
||
d.text((x0, 78), f"tma {hm(dark[0])}–{hm(dark[1])}" if dark else "bez úplné tmy", font=skykit.font(12, 500),
|
||
fill=SUB, anchor="ls")
|
||
d.line((x0, 90, x1, 90), fill=RING)
|
||
|
||
rows = panel_rows(bodies, moon_pct, now)
|
||
for i, (name, info) in enumerate(rows):
|
||
y = 112 + i * 24
|
||
d.ellipse((x0, y - 8, x0 + 7, y - 1), fill=COLOURS[name])
|
||
d.text((x0 + 14, y), name, font=skykit.font(13, 600), fill=TEXT, anchor="ls")
|
||
d.text((x1, y), info, font=skykit.font(12, 500), fill=SUB, anchor="rs")
|
||
y = 112 + len(rows) * 24 - 8
|
||
d.line((x0, y, x1, y), fill=RING)
|
||
|
||
clouds: tuple[CloudHour, ...] = self._clouds.value or ()
|
||
end = tracked(d, (x0, y + 20), "MRAKY", skykit.font(10, 650), LABEL)
|
||
if self._old(self._clouds, self.CLOUDS_STALE_AFTER):
|
||
skykit.tag(d, (end + 8, y + 20), "STARÉ", skykit.font(10, 700), STALE_BG, STALE_INK)
|
||
for i in range(12):
|
||
pct = cloud_at(clouds, now + timedelta(hours=i))
|
||
colour = RING if pct is None else mix(CLEAR, CLOUDY, pct / 100)
|
||
d.rectangle((x0 + i * 12, y + 28, x0 + i * 12 + 9, y + 44), fill=colour)
|
||
story = cloud_story(clouds, now) or "--"
|
||
for k, line in enumerate(story.split(", ")):
|
||
d.text((x0, y + 60 + k * 15), line, font=skykit.font(11, 600), fill=TEXT, anchor="ls")
|
||
|
||
tle: Tle | None = self._tle.value
|
||
if tle is None:
|
||
text = "ISS --"
|
||
else:
|
||
p = astro.next_visible_pass(tle, now)
|
||
text = f"ISS {skykit.WEEKDAYS[p.start.weekday()]} {hm(p.start)} · {p.max_alt:.0f}°" if p else \
|
||
"ISS: nic do 3 dnů"
|
||
end = d.textlength(text, font=skykit.font(12, 600)) + x0
|
||
d.text((x0, 304), text, font=skykit.font(12, 600), fill=ISS, anchor="ls")
|
||
if self._old(self._tle, self.TLE_STALE_AFTER):
|
||
skykit.tag(d, (end + 8, 304), "STARÉ", skykit.font(10, 700), STALE_BG, STALE_INK)
|