diff --git a/astro.py b/astro.py index de9cf61..7a0ee27 100644 --- a/astro.py +++ b/astro.py @@ -21,7 +21,8 @@ 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 ISS_STD_MAG = -1.8 # brightness at 1000 km and a 90° phase angle; observers quote -1.3 to -1.8 -PASS_STEP_S = 10 # how finely a pass is sampled to find its brightest moment +PASS_STEP_S = 10 # how finely a pass is sampled to find when it is visible and how bright +PASS_MIN_ALT = 10 # degrees; a pass counts from here up, as on Heavens-Above and Clear Outside @dataclass(frozen=True) @@ -150,19 +151,37 @@ def next_visible_pass(tle: Tle, now: datetime, within_hours: float = 72) -> Pass 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: + rise, _, _, _, set_, _ = o.next_pass(iss) + if rise is None or set_ 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, - _brightest(iss, sun, o, rise, set_)) + seen = _visible_part(iss, sun, o, rise, set_) + if seen: + start, end = seen[0], seen[-1] + return Pass(_local(start[0]), max(alt for _, alt, _, _ in seen), start[2], end[2], True, + min(mag for *_, mag in seen)) o.date = set_ + ephem.minute return None +def _visible_part(iss: ephem.EarthSatellite, sun: ephem.Sun, o: ephem.Observer, rise, + set_) -> list[tuple[ephem.Date, float, float, float]]: + """(date, alt, az, magnitude) every PASS_STEP_S while the ISS is high enough, sunlit and the sky here dark. + + A pass often starts in daylight or ends in the earth's shadow well before it sets, so only this part is seen. + """ + seen = [] + t = rise + while t <= set_: + o.date = t + iss.compute(o) + sun.compute(o) + if math.degrees(iss.alt) >= PASS_MIN_ALT and not iss.eclipsed and math.degrees(sun.alt) < VISIBLE_SUN_ALT: + seen.append((ephem.Date(t), math.degrees(iss.alt), math.degrees(iss.az), + magnitude(iss.range / 1000, _phase(iss, sun)))) + t += PASS_STEP_S * ephem.second + return seen + + def magnitude(range_km: float, phase: float, std_mag: float = ISS_STD_MAG) -> float: """Estimated magnitude of a satellite seen as a diffusely reflecting sphere (Robert Matson's formula). @@ -183,20 +202,6 @@ def _phase(sat: ephem.EarthSatellite, sun: ephem.Sun) -> float: return math.acos(max(-1.0, min(1.0, cos))) -def _brightest(iss: ephem.EarthSatellite, sun: ephem.Sun, o: ephem.Observer, rise, set_) -> float: - """The lowest magnitude while the ISS is above the horizon and sunlit.""" - best = math.inf - t = rise - while t <= set_: - o.date = t - iss.compute(o) - sun.compute(o) - if iss.alt > 0 and not iss.eclipsed: - best = min(best, magnitude(iss.range / 1000, _phase(iss, sun))) - t += PASS_STEP_S * ephem.second - return best - - def subsolar(now: datetime) -> tuple[float, float]: """Latitude and longitude where the sun is straight overhead.""" o = ephem.Observer() diff --git a/tests/test_astro.py b/tests/test_astro.py index ab60230..f7d4841 100644 --- a/tests/test_astro.py +++ b/tests/test_astro.py @@ -80,14 +80,21 @@ def test_iss_track_runs_east_minute_by_minute(): def test_next_visible_iss_pass(): - p = astro.next_visible_pass(TLE, EVENING) - assert near(p.start, datetime(2026, 10, 29, 5, 38), 2) + p = astro.next_visible_pass(TLE, datetime(2026, 10, 29, 4, 30)) + assert near(p.start, datetime(2026, 10, 29, 5, 41), 1) # when it climbs past 10°, not when it rises assert 35 < p.max_alt < 45 assert (direction(p.az_from), direction(p.az_to)) == ("Z", "JV") +def test_pass_that_comes_out_of_the_earths_shadow_counts_from_there(): + p = astro.next_visible_pass(TLE, EVENING) # in the shadow at its highest, so easy to miss + assert near(p.start, datetime(2026, 10, 29, 4, 8), 1) + assert 35 < p.max_alt < 45 + assert (direction(p.az_from), direction(p.az_to)) == ("V", "V") + + def test_pass_brightness_is_that_of_a_typical_iss_pass(): - p = astro.next_visible_pass(TLE, EVENING) + p = astro.next_visible_pass(TLE, datetime(2026, 10, 29, 4, 30)) assert -4.2 < p.mag < -2.5 # a 40° pass: brighter than any star, short of the best overhead passes diff --git a/tests/test_iss_view.py b/tests/test_iss_view.py index 29a3532..210b712 100644 --- a/tests/test_iss_view.py +++ b/tests/test_iss_view.py @@ -103,7 +103,7 @@ def test_next_pass_grid_shows_height_direction_brightness_and_clouds(): p = astro.next_visible_pass(TLE, NOW) cells = {label: value for label, value, _ in pass_cells(p, CLOUDS)} assert cells["VÝŠKA"] == "40°" - assert cells["SMĚR"] == "Z → JV" + assert cells["SMĚR"] == "V → V" # out of the earth's shadow high in the east, sinking there assert cells["JASNOST"].startswith("−") and cells["JASNOST"].endswith(" mag") and "," in cells["JASNOST"] assert cells["MRAKY"] == "100 %" assert {label: value for label, value, _ in pass_cells(p, ())}["MRAKY"] == "--"