#!/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]
#