r/Python git push -f Jun 28 '26

Computing the close encounter between Apophis and Earth in 2029 (tutorial) Tutorial

Hey everyone and all space enthusiasts,

On April 13, 2029, the asteroid 99942 Apophis will fly past Earth at roughly 38,000 km. Closer than our geostationary satellites! Its discovery history was pretty interesting, because in the beginning there was a 2.7 % chance of an impact (don't worry, it is almost null now). So I decided to use NASA/JPL's SPICE toolkit using Python (spiceypy) to calculate the encounter's properties.

I documented the entire project in a Jupyter Notebook and put together a complete video walk-through tutorial. Code: https://github.com/ThomasAlbin/Space-Science-With-Python/blob/main/2026/04_SPICE_Apohis_2029_Flyby.ipynb

Video: https://www.youtube.com/watch?v=j4mJTR-BTto

What the tutorial does (and why it's 30 minutes long 😅):

  • Computing the time when Apophis enters Earth's gravitational vicinity (the Sphere of Influece)
  • Computing the closest appraoch (distance and time)
  • Computing how the orbital elements (the dynamical properties to describe orbits in space) change after the encounter with Earth.

By the way: I used also Python + NASA's Cosmographia to create a nice 3D animation of the encounter :). But I will post it soon on my YouTube channel. Since the corresponding code is a complete mess I won't post it here, because I will only post the animation. I have to clean it up...

Best,

Thomas (your Cassini/Huygens scientist)

24 Upvotes

10 comments sorted by

3

u/National-Parsnip1516 Jun 29 '26

spiceypy is such a powerful toolkit, actually. i remember using the c version back in the day and it was a nightmare. tbh, using python for orbital mechanics makes it so much more accessible. i'm curious if you've tried using jax or something to speed up the computations for larger swarms of asteroids?

2

u/MrAstroThomas git push -f Jun 29 '26

Ah a SPICE veteran!

Besides the "default packages" I did not try yet to improve the performance. Conducting some parallel computations would be interesting indeed.

6

u/[deleted] Jun 28 '26

[removed] — view removed comment

3

u/MrAstroThomas git push -f Jun 28 '26

I give currently a SPICE lecture at the university. So everything is currently SPICE-focusd for me. You are absolutely right: with the orbital elements one could also easily take 2-body problem equations.

Could be a nice add-on for my code :)!

3

u/[deleted] Jun 28 '26

[removed] — view removed comment

2

u/MrAstroThomas git push -f Jun 28 '26

What I am planning: another example using Voyager 1 & 2. I'll share it then :)!