Category Archives: Python

Python intro

First of all python is free! Then, I think of python as a super cool programming language that is also a super calculator. It allows you to do higher level math calculations but it can also help find cheaper airplane tickets in the Internet (ask me if you’re interested). Here is a short tutorial to get you started with python after you have installed canopy.

Quick start:

  1. Install Enthought Canopy Express from https://www.enthought.com/downloads/.
  2. Open the Canopy editor and create a new file.
  3. Read the following mini tutorial and try some commands in the prompt.
  4. Type in the given equations from 1 to 6 and check the results with print statements. Don’t forget to hit run (Ctr + R or Cmd + R).

mini tutorial continued …

variables in python

you can basically use any typical variable name in python such as ‘perigee’ or ‘radius_earth’. Try to give descriptive variable names so your code is easily readable by others.

assign numerical values to variables

it’s as easy as you’d expect

perigee = 418.0

important note: in python 2 an integer number (such as ’18’) is treated differently than a floating number (such as ‘18.0’). To do math with floating numbers in python 2 you can’t have integers in the mix. So that’s why I usually initialize my integer variables with a ‘.0’ at the end so I won’t get strange results. You should try to see what I mean.

math calculations in python

To do math in python is also very simple and as expected

Examples:

Rp = perigee + radius_earth
R = a*(1-e**2)/(1+e*cos(nu))

important note: in python the exponent operator is ‘**’ instead of ‘^’ as in other programming languages. Here is a short list of the operators you  can use in python:

http://www.tutorialspoint.com/python/python_basic_operators.htm

print your results

The way you print in python 2 is different from python 3. This example is for python 2:

print 'Period = ',P,' min'

last but not the least: numpy

numpy is a python library that helps you do math. You can read more about it here: http://wiki.scipy.org/Tentative_NumPy_Tutorial

For now you just need to import this python library and use it blindly. On the first line of your script type:

from numpy import *

this will import all the functionality of numpy into your python script. For this tutorial it’s specially useful to be able to use the constant ‘pi’ and also for the function ‘cos’. Otherwise you would have to make your own ‘pi’ and ‘cos’.

Here is the source code to make the magic happen:

#iss orbital speed and period
from numpy import *

perigee = 418.0 #km
apogee  = 426.0 #km
radius_earth = 6371.0 #km
mu_earth = 3.986e5 # km^3/s^2

Rp = perigee + radius_earth
Ra = apogee  + radius_earth

print 'Radius Perigee: ', Rp, 'km'
print 'Radius Apogee : ', Ra, 'km'

# 1) eccentricity
e = (Ra - Rp)/(Ra + Rp)
print 'eccentricity: ', e

# 2) semimajor axis
a = (Ra+Rp)/2.0
print 'a =', a, ' km'

# 3) mechanical energy
epsilon = -mu_earth/(2*a)

# 4) altitude at nu=90deg
nu = 90*pi/180
R = a*(1-e**2)/(1+e*cos(nu))

print 'R =', R, 'km'

# 5) orbital speed
V = sqrt(2*(mu_earth/R + epsilon))
print("V = {:2f} km/s".format(V))

# 6) orbital period
P  = 2*pi*sqrt(a**3/mu_earth)
print("P = {:.2f} sec = {:.2f} min = {:.2f} hr".format(P, P/60, P/60/60))

Astronautics 101

This tutorial will help you get started in simple Astronautics calculations using the Systems Tool Kit from AGI or Python. We will use the International Space Station as our orbiting body around the Earth.

The ISS is the largest man-made satellite ever made. It’s been orbiting the earth since 1998 and estimates say it has cost about $100 billion so far. It can be seen with the naked eye from ground. Since the year 2000 it has been continuously occupied by different astronauts from different nations. To read more about it go to: http://en.wikipedia.org/wiki/International_Space_Station

This tutorial will help you calculate some interesting orbital properties of the ISS like it’s orbital speed and period. I’ll give you some of the relevant variables and equations so you can easily compute everything. The main purpose of this tutorial is to get you started with powerful software tools that can help you solve real problems.

With a simple calculator go trough the given equations and get the relevant numbers. Just because I give you the numbers you should’t trust them blindly. Then you will verify your hand made numbers  using STK or Python.

Don’t worry if you don’t understand all the terms yet. Later you will learn more about these terms like the true anomaly and orbital energy (read this and this to get started right away) . These steps were made so you can easily compute the orbital speed and period of the ISS.

Here is the data and the relevant equations (make sure you use the right units):

Earth Radius $R_e =  6371 km$
Earth gravitational parameter $\mu = 3.986×10^5  km^3/s^2$
Perigee = 418 km
Apogee = 426 km

$R_p = 418 + Re$
$R_a = 426 + Re$

1) Find the eccentricity of the orbit (e)

$e = \frac{R_a – R_p}{R_a + R_p} = 0.000588841454438$

it’s very small!!! as expected, meaning the orbit is almost circular

2) Find the semi-major axis (a)

$2a = Ra + Rp => a = 6793.0 km$

3) Find the specific mechanical energy for the ISS orbit (espilon)

$\epsilon = – \frac{\mu}{2a} = -29.339025467392904 km^2/s^2$

4) Find the altitude (R) of the ISS when the true anomaly is 90deg (usually represented by the greek letter “nu”)

$R = \frac{a(1-e^2)}{1+e \times cos(\nu)} = 6792.9976446341825 km$

don’t forget to convert deg to radians when you use this equation.

5) Find the orbital speed (at nu = 90deg)

$V = \sqrt{2(\frac{\mu}{R} + \epsilon)} = 7.660163 km/s$

6) Find the orbiting period of the ISS

$P = 2\pi\sqrt{\frac{a^3}{\mu}} = 5571.90 sec = 92.87 min = 1.55 hr$

because the results are given in seconds I convert to minutes and hours to get a better feeling of it how long it really takes for the ISS to orbit the Earth once.

BTW, here’s a really cool video of the Earth taken from the ISS at 7.6 km/s

Now follow these links to check if the above equations are correct:

Systems Tool Kit (STK) intro

go to http://www.spacemig.com/stk-intro/

Python intro

go to http://www.spacemig.com/python-intro/ 

Starting with Python

python logo
Python logo

For about a year I have been programming in Python as a Matlab alternative. I love Matlab but I can’t afford being tied to a licence. Python is free, open source, and so far I have been able to do everything I did with Matlab. I am fairly impressed!

Python is becoming, or it may be already, a main stream programming language, specially for open source software. Since it’s a scripting language a lot of programs use it for scripting in that app. A few examples are Blender for 3D modeling and animation, FreeCad for 3D mechanical modeling, Fenics for solving differential equations by finite element methods, etc. Many universities have adopted Python in their class work, MIT, Georgia Tech, UC Davis, etc.

If you want to start with Python let me recommend the following:

  1. Use Enthought! Yes, I know … there is also a licence attached if you really want full support and all the packages. But it’s free if you have a .edu email and even if you don’t have one there is a totally free version of Enhtought: Canopy Express! By the way, the Enthought distro works on Mac, Linux and Windows. It’s perfect!
  2. If you really… really don’t want to be attached to a licence (I made my peace with Enthought since they have a free version) you can use Spyderlib. It’s also an Integrated Development Environment (IDE) like Enthought but has a few less python libraries… but you can install them separately with a bit of pain.

If you want to lear more about Python and how to get started check these references: