Skip to content

Calculate apoapsis and periapsis events#213

Merged
rhannequin merged 3 commits intomainfrom
apoapsis-periapsis
Sep 15, 2025
Merged

Calculate apoapsis and periapsis events#213
rhannequin merged 3 commits intomainfrom
apoapsis-periapsis

Conversation

@rhannequin
Copy link
Owner

@rhannequin rhannequin commented Sep 10, 2025

As Solar System bodies have elliptical orbits (and not circular), there are moments when they are closer or further from their primary body (Sun for planets, Earth for the Moon).

The further point on the orbits is called the apoapsis, while the closer is the periapsis. Each body reach them once during each period. Technically, when the Sun is the primary body, they are called aphelion and perihelion, while when the primary is the Earth they are called apogee and perigee. Astronoby uses the generic term for all bodies.

This change introduces the ability to compute when these points are reached and how far from the primary body the body is. It searches between two provided dates.
Note that is is possible that no event happen during the provided time range. For example, the Moon's orbital period is ~27 days, which means looking for an event within an hour is very unlikely to result in finding one.

The result is an array of MaximumEvent which exposes #instant (Astronoby::Instant) and #value (Astronoby::Distance).

# All aphelions happening for Saturn between 2015 and 2050

ephem = Astronoby::Ephem.load("inpop19a.bsp")

calculator = Astronoby::ExtremumCalculator.new(
  ephem: ephem,
  body: Astronoby::Saturn,
  primary_body: Astronoby::Sun
)

events = calculator.apoapsis_events_between(Time.utc(2015, 1, 1), Time.utc(2051, 1, 1))

events.each do |event|
  puts "#{event.instant.to_time.round} (#{event.value.km.round} km)"
end
# 2018-04-17 11:27:15 UTC (1505798987 km)
# 2047-07-15 06:57:26 UTC (1502883334 km)

@rhannequin rhannequin self-assigned this Sep 10, 2025
@rhannequin rhannequin changed the title Create ExtremumCalculator for apoapsis/periapsis events Calculate apoapsis and periapsis events Sep 10, 2025
@rhannequin rhannequin marked this pull request as ready for review September 10, 2025 17:50
@rhannequin rhannequin requested a review from Copilot September 10, 2025 17:50
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces functionality to calculate orbital extrema (apoapsis and periapsis) for celestial bodies. It implements a sophisticated algorithm using adaptive sampling and golden section search to accurately determine when bodies reach their maximum and minimum distances from their primary body (e.g., Earth from Sun, Moon from Earth).

  • Adds ExtremumCalculator class with apoapsis/periapsis event calculation methods
  • Implements ExtremumEvent to represent timing and distance of orbital extrema
  • Provides comprehensive test coverage across all solar system bodies and various orbital periods

Reviewed Changes

Copilot reviewed 5 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
spec/support/test_ephem_helper.rb Adds helper method for loading full ephemeris data needed for long-period calculations
spec/astronoby/events/extremum_calculator_spec.rb Comprehensive test suite covering all celestial bodies and edge cases
lib/astronoby/events/extremum_event.rb Simple data class representing an extremum event with instant and value
lib/astronoby/events/extremum_calculator.rb Core calculator implementing adaptive sampling and golden section search
lib/astronoby.rb Updates require statements to include new extremum calculation classes

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@rhannequin rhannequin merged commit 9c0750e into main Sep 15, 2025
43 checks passed
@rhannequin rhannequin mentioned this pull request Oct 25, 2025
rhannequin added a commit that referenced this pull request Oct 31, 2025
## 0.9.0 - 2025-10-31

_If you are upgrading: please see [UPGRADING.md]._

### Features

* Add `#approaching_primary?` and `#receding_from_primary?` to solar system bodies ([#211])
* Calculate apoapsis and periapsis events ([#213])
* Improve precision of ΔT ([#219])
* Deep Sky Object: Compute astrometric position ([#217])
* Deep Sky Object: Compute apparent position ([#220])
* Deep Sky Object: Handle velocities properly ([#222])
* Deep Sky Object: Compute topocentric position ([#226])
* Deep Sky Object: difference between the body and the position ([#227])
* Deep Sky Object: Add support for RiseTransitSetCalculator ([#228])

### Improvements

* Drop `Astronoby::Apparent#angular_diameter` ([#221])
* Bump rubyzip from 3.0.2 to 3.2.1 by @dependabot ([#210], [#215], [#223], [#233])
* Bump standard from 1.50.0 to 1.51.1 by @dependabot ([#212], [#214])
* Be proud about the precision achieved ([#218])
* Use local apparent instead of local mean sidereal time for hour angle ([#225])
* Bump rspec from 3.13.1 to 3.13.2 by @dependabot ([#229])
* Bump benchmark from 0.4.1 to 0.5.0 by @dependabot ([#230])
* Add documentation for deep-sky objects ([#232])
* Bump rake from 13.3.0 to 13.3.1 by @dependabot ([#235])

### Backward-incompatible changes

* Drop `Astronoby::Apparent#angular_diameter` ([#221])
* Use local apparent instead of local mean sidereal time for hour angle ([#225])

**Full Changelog**: v0.8.0...v0.9.0

[#210]: #210
[#211]: #211
[#212]: #212
[#213]: #213
[#214]: #214
[#215]: #215
[#217]: #217
[#218]: #218
[#219]: #219
[#220]: #220
[#221]: #221
[#222]: #222
[#223]: #223
[#225]: #225
[#226]: #226
[#227]: #227
[#228]: #228
[#229]: #229
[#230]: #230
[#232]: #232
[#233]: #233
[#235]: #235
[UPGRADING.md]: https://github.com/rhannequin/astronoby/blob/main/UPGRADING.md
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.

2 participants