Skip to content

Add an altitude-crossing calculator and base twilight on it - #311

Merged
rhannequin merged 3 commits into
mainfrom
altitude-crossing-calculator
Oct 26, 2026
Merged

rhannequin merged 3 commits into
mainfrom
altitude-crossing-calculator

Conversation

@rhannequin

@rhannequin rhannequin commented Oct 26, 2026 •

Copy link
Copy Markdown
Owner

Closes #310

Adds AltitudeCrossingCalculator, which finds when a body reaches any altitude, with unrounded instants, and rebuilds TwilightCalculator on top of it.

calculator = Astronoby::AltitudeCrossingCalculator.new(
  body: Astronoby::Sun,
  observer: observer,
  ephem: ephem
)

calculator.crossings_between(
  Time.utc(2026, 10, 23),
  Time.utc(2026, 10, 24),
  altitude: Astronoby::Angle.from_degrees(-17.5)
)

It also has crossing_on, extremes_between and multiple_crossings_between, plus the refraction: and reference: (:center, :upper_limb, :lower_limb) options.

The window is split at the culminations. Where the target is clearly crossed, the instant is found on the real altitude with a safeguarded secant. Where the target is within 0.05° of a culmination, it falls back to sampling and bisection and the crossing is flagged grazing?.

Twilight

TwilightCalculator keeps its signatures and still returns times rounded to the second. What changes:

  • Times change slightly: up to about 30 seconds at the standard angles, and up to about 100 seconds with time_for_zenith_angle and custom angles. The new values match IMCCE, Horizons and Skyfield.
  • Twilight that exists is returned when the Sun doesn't rise or set, instead of nil.
  • event_on now covers midnight to the next midnight, so a crossing in the last second of a day isn't dropped.

Validation

Compared 22 Sun crossings (-6°, -12°, -18° at Paris, Makkah, 60° N and 75° N) with other tools. The largest difference with Astronoby is 0.6 s for IMCCE, 0.10 s for Horizons and 0.19 s for Skyfield on the same ephemeris. USNO agrees to the minute for civil twilight. The Skyfield gap is the difference between its ΔT prediction and IERS.

Performance

Twilight is slower than before, because each crossing needs a few real position evaluations:

before after
event_on x 30 0.10 s 0.41 s
events_between over a year 1.22 s 3.4 s

@rhannequin rhannequin self-assigned this Oct 26, 2026
@rhannequin
rhannequin force-pushed the altitude-crossing-calculator branch from 802f576 to 3985d75 Compare October 26, 2026 22:49
@rhannequin
rhannequin merged commit 2651f24 into main Oct 26, 2026
44 checks passed
@rhannequin
rhannequin deleted the altitude-crossing-calculator branch October 26, 2026 23:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add an altitude-crossing calculator and base twilight times on it

1 participant