#!/usr/bin/env python # coding: utf-8 # # Going to Mars with Python using poliastro # # # # This is an example on how to use [poliastro](https://github.com/poliastro/poliastro), a little library I've been working on to use in my Astrodynamics lessons. It features conversion between **classical orbital elements** and position vectors, propagation of **Keplerian orbits**, initial orbit determination using the solution of the **Lambert's problem** and **orbit plotting**. # # In this example we're going to draw the trajectory of the mission [Mars Science Laboratory (MSL)](http://mars.jpl.nasa.gov/msl/), which carried the rover Curiosity to the surface of Mars in a period of something less than 9 months. # # **Note**: This is a very simplistic analysis which doesn't take into account many important factors of the mission, but can serve as an starting point for more serious computations (and as a side effect produces a beautiful plot at the end). # First of all, we import the necessary modules. Apart from poliastro we will make use of astropy to deal with physical units and time definitions and jplephem to compute the positions and velocities of the planets. # In[1]: import numpy as np import astropy.units as u from astropy import time from poliastro import iod from poliastro.bodies import Sun from poliastro.twobody import Orbit from poliastro.twobody.propagation import propagate from poliastro.util import time_range # We need a binary file from NASA called *SPICE kernel* to compute the position and velocities of the planets. Astropy downloads it for us: # In[2]: from astropy.coordinates import solar_system_ephemeris, get_body_barycentric_posvel solar_system_ephemeris.set("jpl") # The initial data was gathered from Wikipedia: the date of the launch was on **November 26, 2011 at 15:02 UTC** and landing was on **August 6, 2012 at 05:17 UTC**. We compute then the time of flight, which is exactly what it sounds. It is a crucial parameter of the mission. # In[3]: # Initial data N = 50 date_launch = time.Time("2011-11-26 15:02", scale="utc") date_arrival = time.Time("2012-08-06 05:17", scale="utc") tof = date_arrival - date_launch tof.to(u.h) # Once we have the vector of times we can use `get_body_barycentric_posvel` to compute the array of positions and velocities of the Earth and Mars. # In[4]: times_vector = time_range(date_launch, end=date_arrival, periods=N) times_vector[:5] # In[5]: rr_earth, vv_earth = get_body_barycentric_posvel("earth", times_vector) # In[6]: rr_earth[:3] # In[7]: vv_earth[:3] # In[8]: rr_mars, vv_mars = get_body_barycentric_posvel("mars", times_vector) # In[9]: rr_mars[:3] # In[10]: vv_mars[:3] #
The positions and velocities are in the International Celestial Reference Frame (ICRS), which has the Earth equator as the fundamental plane
# To compute the transfer orbit, we have the useful function `lambert`: according to a theorem with the same name, *the transfer orbit between two points in space only depends on those two points and the time it takes to go from one to the other*. We have the starting and final position and we have the time of flight: there we go! # In[11]: # Compute the transfer orbit! r0 = rr_earth[0].xyz rf = rr_mars[-1].xyz (va, vb), = iod.lambert(Sun.k, r0, rf, tof) ss0_trans = Orbit.from_vectors(Sun, r0, va, date_launch) ssf_trans = Orbit.from_vectors(Sun, rf, vb, date_arrival) # In[12]: from poliastro.plotting import OrbitPlotter3D from poliastro.bodies import Earth, Mars from plotly.graph_objs import FigureWidget # In[13]: # I like color color_earth0 = "#3d4cd5" color_earthf = "#525fd5" color_mars0 = "#ec3941" color_marsf = "#ec1f28" color_sun = "#ffcc00" color_orbit = "#888888" color_trans = "#444444" fig = FigureWidget() frame = OrbitPlotter3D(figure=fig) frame.set_attractor(Sun) frame.plot_trajectory(rr_earth, label=Earth, color=color_earth0) frame.plot_trajectory(rr_mars, label=Mars, color=color_marsf) frame.plot_trajectory( propagate(ss0_trans, time.TimeDelta(times_vector - ss0_trans.epoch)), label="MSL trajectory", color=color_trans, ) frame.set_view(30 * u.deg, 260 * u.deg, distance=3 * u.km) fig.layout.title = "MSL Mission: from Earth to Mars" # ## Plotting figure using Plotly new renderers # In[14]: import plotly.io as pio # In[15]: pio.show(fig, renderer='plotly_mimetype') # In[16]: pio.show(fig, renderer='notebook') # In[17]: pio.show(fig, renderer='notebook_connected') # In[18]: pio.show(fig, renderer='iframe') # In[19]: pio.show(fig, renderer='browser') # In[20]: pio.show(fig, renderer='png') # In[21]: pio.show(fig, renderer='svg') # In[22]: pio.show(fig, renderer='jpg') # In[23]: pio.show(fig, renderer='pdf') # In[24]: pio.show(fig, renderer='json') # This line opens a new browser tab and saves the resulting image: # In[25]: #frame.savefig("msl3d.png", title="MSL Mission: from Earth to Mars") # Not bad! Let's celebrate with some music! # In[26]: from IPython.display import YouTubeVideo YouTubeVideo('zSgiXGELjbc')