"""The sky over Zlín, calculated locally (ephem): sun, moon, planets, bright stars and the ISS. No drawing. Every time in and out is naive local wall-clock time (BOARD_TZ); ephem itself works in UTC. """ import math from dataclasses import dataclass from datetime import datetime, timedelta import ephem import ephem.stars from sky_data import Tle from timeutil import from_utc, to_utc from weather_data import LAT, LON ELEVATION_M = 230 DIRECTIONS = ["S", "SV", "V", "JV", "J", "JZ", "Z", "SZ"] # Czech: S = sever (north), J = jih (south) BODIES = (("Měsíc", ephem.Moon), ("Merkur", ephem.Mercury), ("Venuše", ephem.Venus), ("Mars", ephem.Mars), ("Jupiter", ephem.Jupiter), ("Saturn", ephem.Saturn)) DARK_SUN_ALT = "-18" # astronomical darkness VISIBLE_SUN_ALT = -6 # a satellite pass is visible once the sky is at least this dark TRACK_BACK, TRACK_AHEAD = 40, 95 # minutes of ISS ground track around now @dataclass(frozen=True) class Body: name: str alt: float # degrees above the horizon, negative below az: float # degrees clockwise from north rise: datetime | None # next rising, None when it does not rise or set @dataclass(frozen=True) class Star: name: str mag: float alt: float az: float @dataclass(frozen=True) class Pass: start: datetime max_alt: float az_from: float az_to: float visible: bool # the ISS is sunlit while the sky here is dark @dataclass(frozen=True) class Fix: lat: float lon: float km: float # height above the ground def direction(az: float) -> str: return DIRECTIONS[round(az / 45) % 8] def _observer(when: datetime, horizon: str = "0") -> ephem.Observer: o = ephem.Observer() o.lat, o.lon, o.elevation, o.horizon = str(LAT), str(LON), ELEVATION_M, horizon o.date = to_utc(when) return o def _local(date: ephem.Date) -> datetime: return from_utc(ephem.Date(date).datetime()) def bodies(now: datetime) -> tuple[Body, ...]: """Moon first, then the planets in order from the sun.""" out = [] for name, cls in BODIES: body = cls() o = _observer(now) try: rise = _local(o.next_rising(body)) except (ephem.AlwaysUpError, ephem.NeverUpError): rise = None body.compute(_observer(now)) out.append(Body(name, math.degrees(body.alt), math.degrees(body.az), rise)) return tuple(out) def sun(now: datetime) -> Body: s = ephem.Sun(_observer(now)) return Body("Slunce", math.degrees(s.alt), math.degrees(s.az), None) def moon_phase(now: datetime) -> tuple[float, bool]: """Illuminated percentage and whether it is waxing.""" moon = ephem.Moon(_observer(now)) utc = to_utc(now) return moon.phase, ephem.next_full_moon(utc) < ephem.next_new_moon(utc) def darkness(now: datetime) -> tuple[datetime, datetime] | None: """Astronomical darkness of tonight (before noon: of the night now ending); None if the sun never sinks enough.""" evening = now.date() - timedelta(days=1) if now.hour < 12 else now.date() o = _observer(datetime.combine(evening, datetime.min.time()).replace(hour=12), DARK_SUN_ALT) sun = ephem.Sun() try: start = o.next_setting(sun, use_center=True) o.date = start end = o.next_rising(sun, use_center=True) except (ephem.AlwaysUpError, ephem.NeverUpError): return None return _local(start), _local(end) def stars(now: datetime, brighter_than: float = 3.4) -> tuple[Star, ...]: """Named stars above the horizon, brightest first.""" o = _observer(now) out = [] for name in ephem.stars.stars: star = ephem.star(name) star.compute(o) if star.alt > 0 and star.mag < brighter_than: out.append(Star(name, star.mag, math.degrees(star.alt), math.degrees(star.az))) return tuple(sorted(out, key=lambda s: s.mag)) def _iss(tle: Tle) -> ephem.EarthSatellite: return ephem.readtle(tle.name, tle.line1, tle.line2) def iss_fix(tle: Tle, when: datetime) -> Fix: iss = _iss(tle) iss.compute(to_utc(when)) return Fix(math.degrees(iss.sublat), math.degrees(iss.sublong), iss.elevation / 1000) def iss_track(tle: Tle, now: datetime) -> tuple[tuple[int, float, float], ...]: """(minutes from now, lat, lon) for every minute from TRACK_BACK before to TRACK_AHEAD after now.""" iss = _iss(tle) track = [] for m in range(-TRACK_BACK, TRACK_AHEAD + 1): iss.compute(to_utc(now + timedelta(minutes=m))) track.append((m, math.degrees(iss.sublat), math.degrees(iss.sublong))) return tuple(track) def next_visible_pass(tle: Tle, now: datetime, within_hours: float = 72) -> Pass | None: iss, sun = _iss(tle), ephem.Sun() o = _observer(now) limit = ephem.Date(to_utc(now + timedelta(hours=within_hours))) while o.date < limit: rise, az_rise, top, top_alt, set_, az_set = o.next_pass(iss) if rise is None or rise > limit: return None o.date = top iss.compute(o) sun.compute(o) if not iss.eclipsed and math.degrees(sun.alt) < VISIBLE_SUN_ALT: return Pass(_local(rise), math.degrees(top_alt), math.degrees(az_rise), math.degrees(az_set), True) o.date = set_ + ephem.minute return None def subsolar(now: datetime) -> tuple[float, float]: """Latitude and longitude where the sun is straight overhead.""" o = ephem.Observer() o.lat, o.lon, o.date = "0", "0", to_utc(now) sun = ephem.Sun(o) lon = math.degrees(sun.ra - o.sidereal_time()) return math.degrees(sun.dec), (lon + 540) % 360 - 180