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]>
80 lines
2.9 KiB
Python
80 lines
2.9 KiB
Python
"""ISS orbit from CelesTrak and cloud cover from Open-Meteo for Zlín: fetching and parsing (no drawing)."""
|
|
from dataclasses import dataclass
|
|
from datetime import datetime, timedelta
|
|
|
|
import ephem
|
|
import requests
|
|
|
|
from weather_data import LAT, LON, TIMEZONE, URL as OPEN_METEO_URL
|
|
|
|
CELESTRAK_URL = "https://celestrak.org/NORAD/elements/gp.php"
|
|
ISS_CATALOG_NUMBER = 25544
|
|
STORY_HOURS = 12
|
|
SOME_CLOUD, OVERCAST = 25, 60 # percent
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class Tle:
|
|
"""Two-line orbital elements: what ephem needs to place a satellite at any moment."""
|
|
name: str
|
|
line1: str
|
|
line2: str
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class CloudHour:
|
|
time: datetime # naive local time, on the hour
|
|
pct: int | None
|
|
|
|
|
|
def parse_tle(text: str) -> Tle:
|
|
lines = [line.strip() for line in text.strip().splitlines()]
|
|
if len(lines) < 3 or not lines[1].startswith("1 ") or not lines[2].startswith("2 "):
|
|
raise ValueError(f"not a TLE: {text[:60]!r}")
|
|
try:
|
|
ephem.readtle(*lines[:3])
|
|
except ValueError as exc:
|
|
raise ValueError(f"unreadable TLE: {exc}") from None
|
|
return Tle(*lines[:3])
|
|
|
|
|
|
def fetch_tle(get=requests.get) -> Tle:
|
|
response = get(CELESTRAK_URL, params={"CATNR": ISS_CATALOG_NUMBER, "FORMAT": "TLE"}, timeout=10)
|
|
response.raise_for_status()
|
|
return parse_tle(response.text)
|
|
|
|
|
|
def parse_clouds(payload: dict) -> tuple[CloudHour, ...]:
|
|
hourly = payload.get("hourly") or {}
|
|
pcts = hourly.get("cloud_cover") or []
|
|
return tuple(CloudHour(datetime.fromisoformat(t), pcts[i] if i < len(pcts) else None)
|
|
for i, t in enumerate(hourly.get("time") or []))
|
|
|
|
|
|
def fetch_clouds(get=requests.get) -> tuple[CloudHour, ...]:
|
|
params = {"latitude": LAT, "longitude": LON, "hourly": "cloud_cover", "timezone": TIMEZONE, "forecast_days": 2}
|
|
response = get(OPEN_METEO_URL, params=params, timeout=10)
|
|
response.raise_for_status()
|
|
return parse_clouds(response.json())
|
|
|
|
|
|
def cloud_at(clouds: tuple[CloudHour, ...], when: datetime) -> int | None:
|
|
"""Cloud cover for the hour containing `when`, None if the forecast does not reach it."""
|
|
hour = when.replace(minute=0, second=0, microsecond=0)
|
|
return next((c.pct for c in clouds if c.time == hour), None)
|
|
|
|
|
|
def cloud_story(clouds: tuple[CloudHour, ...], now: datetime) -> str | None:
|
|
"""The next 12 hours in a few words: `Jasno`, `Polojasno`, `Zataženo` or `Jasno do 20 h, od 22 h zataženo`."""
|
|
start = now.replace(minute=0, second=0, microsecond=0)
|
|
hours = [c for c in clouds if start <= c.time <= start + timedelta(hours=STORY_HOURS)]
|
|
if not hours:
|
|
return None
|
|
some = next((c for c in hours if (c.pct or 0) >= SOME_CLOUD), None)
|
|
full = next((c for c in hours if (c.pct or 0) >= OVERCAST), None)
|
|
if some is None:
|
|
return "Jasno"
|
|
if some is hours[0]:
|
|
return "Zataženo" if full is some else "Polojasno"
|
|
return f"Jasno do {some.time.hour} h" + (f", od {full.time.hour} h zataženo" if full else ", pak polojasno")
|