# Going to Mars with Python using poliastro¶

This is an example on how to use 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), 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.

```
[1]:
```

```
import numpy as np
import astropy.units as u
from astropy import time
from poliastro import iod
from poliastro.bodies import Earth, Mars, Sun
from poliastro.twobody import Orbit
from poliastro.maneuver import Maneuver
```

```
WARNING: AstropyDeprecationWarning: astropy.extern.six will be removed in 4.0, use the six module directly if it is still needed [astropy.extern.six]
```

We need a binary file from NASA called *SPICE kernel* to compute the position and velocities of the planets. Astropy downloads it for us:

```
[2]:
```

```
from astropy.coordinates import solar_system_ephemeris
solar_system_ephemeris.set("jpl")
```

```
[2]:
```

```
<ScienceState solar_system_ephemeris: '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.

```
[3]:
```

```
# Initial data
date_launch = time.Time("2011-11-26 15:02", scale="utc")
date_arrival = time.Time("2012-08-06 05:17", scale="utc")
```

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 could make use of the raw algorithms available in `poliastro.iod`

for solving this but working with the `poliastro.maneuvers`

is even easier!

We just need to create the orbits for each one of the planets at the specific departure and arrival dates.

```
[4]:
```

```
# Solve for departure and target orbits
ss_earth = Orbit.from_body_ephem(Earth, date_launch)
ss_mars = Orbit.from_body_ephem(Mars, date_arrival)
```

```
/home/lobo/Github/poliastro/src/poliastro/twobody/orbit.py:393: TimeScaleWarning: Input time was converted to scale='tdb' with value 2011-11-26 15:03:06.183. Use Time(..., scale='tdb') instead.
TimeScaleWarning,
/home/lobo/Github/poliastro/src/poliastro/twobody/orbit.py:393: TimeScaleWarning: Input time was converted to scale='tdb' with value 2012-08-06 05:18:07.183. Use Time(..., scale='tdb') instead.
TimeScaleWarning,
```

We can now solve for the maneuver that will take us from Earth to Mars. After solving it, we just need to apply it to the departure orbit to solve for the transfer one.

```
[5]:
```

```
# Solve for the transfer maneuver
man_lambert = Maneuver.lambert(ss_earth, ss_mars)
# Get the transfer and final orbits
ss_trans, ss_target = ss_earth.apply_maneuver(man_lambert, intermediate=True)
```

Let’s plot this transfer orbit in 3D!

```
[6]:
```

```
from poliastro.plotting import OrbitPlotter3D
from plotly.graph_objs import FigureWidget
```

```
[7]:
```

```
# We will make use of OrbitPlotter3D class
fig = FigureWidget()
plotter = OrbitPlotter3D(fig)
plotter.plot(ss_earth, label="Earth at launch position", color="navy")
plotter.plot(ss_mars, label="Mars at arrival position", color="red")
plotter.plot_trajectory(ss_trans.sample(max_anomaly=180*u.deg).cartesian, color="black", label="Transfer orbit")
```

```
/home/lobo/Github/poliastro/src/poliastro/twobody/orbit.py:1095: UserWarning:
Frame <class 'astropy.coordinates.builtin_frames.icrs.ICRS'> does not support 'obstime', time values were not returned
```

This line opens a new browser tab and saves the resulting image:

```
[8]:
```

```
#frame.savefig("msl3d.png", title="MSL Mission: from Earth to Mars")
```

Not bad! Let’s celebrate with some music!

```
[9]:
```

```
from IPython.display import YouTubeVideo
YouTubeVideo('zSgiXGELjbc')
```

```
[9]:
```