Files
second-screen/iss_view.py
T
rickzkmandClaude Opus 5.5 2132262896 Sky and ISS views
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]>
2026-10-29 00:03:38 +01:00

144 lines
5.9 KiB
Python

"""Where the ISS is right now: a dotted world map with day and night, its ground track and the next pass."""
import math
from datetime import datetime
from functools import lru_cache
from pathlib import Path
from PIL import Image, ImageDraw
import astro
import skykit
from astro import direction
from poller import Poller
from sky_data import CloudHour, Tle, cloud_at
from skykit import H, W, hm
from view import Patch
from weather_data import LAT, LON
BG, ACCENT, TEXT, DIM = (3, 8, 7), (120, 236, 206), (232, 240, 236), (110, 140, 130)
MARKER, HOME = (255, 255, 255), (255, 110, 100)
LAND_DAY, LAND_NIGHT, SEA_DAY, SEA_NIGHT = (70, 168, 120), (26, 66, 50), (16, 32, 28), (8, 15, 13)
PAST, AHEAD = (50, 110, 96), ACCENT
STALE_BG = (240, 200, 90)
MAP_TOP, MAP_H, DOT = 40, 240, 4
LAND_PATH = Path(__file__).parent / "assets" / "world_land.png"
EARTH_KM, GM = 6371.0, 398600.4418 # radius, and the gravitational parameter in km³/s²
def map_xy(lat: float, lon: float) -> tuple[float, float]:
return (lon + 180) / 360 * W, MAP_TOP + (90 - lat) / 180 * MAP_H
def is_day(lat: float, lon: float, subsolar: tuple[float, float]) -> bool:
"""Whether the sun is above the horizon at lat/lon, given where it stands overhead."""
sun_lat, sun_lon = (math.radians(v) for v in subsolar)
lat, lon = math.radians(lat), math.radians(lon)
return math.sin(lat) * math.sin(sun_lat) + math.cos(lat) * math.cos(sun_lat) * math.cos(lon - sun_lon) > 0
@lru_cache(maxsize=1)
def _land() -> Image.Image:
return Image.open(LAND_PATH).convert("L")
def _fmt(value: float) -> str:
return f"{abs(value):.1f}".replace(".", ",")
class IssView:
name = "iss"
TLE_STALE_AFTER = 3 * 86400.0
FRAME_EVERY_S = 10 # the ISS moves about one pixel of this map in 10 s
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()
def _stale(self) -> bool:
age = self._tle.age()
return age is not None and age > self.TLE_STALE_AFTER
def frame_key(self, now: datetime):
tick = now.replace(second=now.second - now.second % self.FRAME_EVERY_S, microsecond=0)
return (tick, self._tle.version, self._clouds.version, self._stale())
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)
self._map(d, now)
zx, zy = map_xy(LAT, LON)
d.ellipse((zx - 3, zy - 3, zx + 3, zy + 3), outline=HOME, width=2)
d.text((zx + 6, zy - 4), "Zlín", font=skykit.font(10, 700), fill=HOME, anchor="ls")
d.text((14, 28), "ISS", font=skykit.font(22, 800), fill=ACCENT, anchor="ls")
tle: Tle | None = self._tle.value
if tle is None:
d.text((240, MAP_TOP + MAP_H / 2), "Čekám na dráhu ISS…", font=skykit.font(16, 600), fill=TEXT,
anchor="mm")
return img
for minutes, lat, lon in astro.iss_track(tle, now):
x, y = map_xy(lat, lon)
if minutes < 0:
d.point((x, y), fill=PAST)
else:
d.ellipse((x - 1, y - 1, x + 1, y + 1), fill=AHEAD)
fix = astro.iss_fix(tle, now)
x, y = map_xy(fix.lat, fix.lon)
d.ellipse((x - 7, y - 7, x + 7, y + 7), outline=MARKER, width=2)
d.ellipse((x - 2, y - 2, x + 2, y + 2), fill=MARKER)
speed = math.sqrt(GM / (EARTH_KM + fix.km)) * 3600
header = f"{fix.km:.0f} km · {speed:,.0f} km/h".replace(",", " ")
end = d.textlength(header, font=skykit.font(13, 500)) + 62
d.text((62, 27), header, font=skykit.font(13, 500), fill=DIM, anchor="ls")
if self._stale():
skykit.tag(d, (end + 8, 27), "STARÉ", skykit.font(10, 700), STALE_BG, BG)
coords = (f"{_fmt(fix.lat)}° {'s' if fix.lat >= 0 else 'j'}. š. "
f"{_fmt(fix.lon)}° {'v' if fix.lon >= 0 else 'z'}. d.")
d.text((466, 27), coords, font=skykit.font(12, 500), fill=DIM, anchor="rs")
self._next_pass(d, tle, now)
return img
@staticmethod
def _map(d: ImageDraw.ImageDraw, now: datetime) -> None:
land = _land().load()
sun = astro.subsolar(now)
for gy in range(DOT // 2, MAP_H, DOT):
lat = 90 - (gy + 1) / MAP_H * 180
for gx in range(DOT // 2, W, DOT):
day = is_day(lat, (gx + 1) / W * 360 - 180, sun)
if land[gx, gy]:
colour = LAND_DAY if day else LAND_NIGHT
else:
colour = SEA_DAY if day else SEA_NIGHT
d.rectangle((gx, MAP_TOP + gy, gx + 1, MAP_TOP + gy + 1), fill=colour)
def _next_pass(self, d: ImageDraw.ImageDraw, tle: Tle, now: datetime) -> None:
skykit.tracked(d, (14, 298), "DALŠÍ VIDITELNÝ PŘELET", skykit.font(10, 650), ACCENT)
p = astro.next_visible_pass(tle, now)
if p is None:
d.text((14, 316), "v příštích 3 dnech žádný", font=skykit.font(15, 700), fill=TEXT, anchor="ls")
return
d.text((14, 316), f"{skykit.WEEKDAYS[p.start.weekday()]} {p.start.day}. {p.start.month}. v {hm(p.start)} · "
f"{p.max_alt:.0f}° · z {direction(p.az_from)} na {direction(p.az_to)}", font=skykit.font(15, 700),
fill=TEXT, anchor="ls")
pct = cloud_at(self._clouds.value or (), p.start)
if pct is not None:
label = f"mraky {pct} %"
colour = (255, 120, 100) if pct >= 60 else (240, 200, 90) if pct >= 25 else (120, 236, 160)
width = d.textlength(label, font=skykit.font(11, 700))
d.rounded_rectangle((466 - width - 16, 290, 466, 310), 10, fill=colour)
d.text((458, 304), label, font=skykit.font(11, 700), fill=BG, anchor="rs")