{ "metadata": { "name": "An-Introduction-First-Example" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "My first experiment with modern Python scientific plotting tools and astronomy,\n", "that inspired all of the rest of this activity,\n", "was showing the Atlanta Python Meetup\n", "how to illustrate what it means\n", "that Mars has a very *elliptical* orbit." ] }, { "cell_type": "code", "collapsed": false, "input": [ "%pylab inline" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "import de405\n", "import jplephem\n", "from sgp4.ext import jday" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "j0 = jday(2013, 1, 1, 0, 0, 0)\n", "\n", "print 'The Julian Day of Jan 1 2013:', j0" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "twoyears = np.arange(j0, j0 + 365.251 * 2.0, 0.25)\n", "\n", "print 'Two years range from', twoyears[0], 'to', twoyears[-1]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "eph = jplephem.Ephemeris(de405)\n", "x, y, z = eph.position('mars', twoyears)\n", "\n", "print 'The first x value is:', x[0], 'km'" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Polar plotting\n", "\n", "A polar plot helps our eyes discern\n", "when an ellipse is close to circular,\n", "versus when its shape is strongly exaggerated." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# This function looks innocently scalar.\n", "\n", "def length(*args):\n", " return sqrt(sum(a*a for a in args))\n", "\n", "a = 3\n", "b = 4\n", "\n", "print 'The hypotenuse of a 3-4 triangle is', length(a, b)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# But it also accepts arrays!\n", "\n", "a = array([6, 11, 12, 13])\n", "b = array([8, 60, 35, 84])\n", "\n", "print 'Four more hypotenuses:', length(a, b)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print 'A 3D vector length:', length(3, 0, -4)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "theta = np.arctan2(x, y)\n", "r = length(x, y, z)\n", "\n", "print theta.shape\n", "print r.shape" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# We are ready to see how the distance\n", "# from Mars to the Sun varies over time!\n", "\n", "polar(theta, r)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## All four inner planets\n", "\n", "Python is a general-purpose programming language\n", "that lets us package up our logic inside of first-class functions." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def plot_planet(t):\n", " x, y, z = eph.position(name, twoyears)\n", " theta = np.arctan2(x, y)\n", " r = length(x, y, z)\n", " polar(theta, r) " ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "for name in 'mercury', 'venus', 'earthmoon', 'mars':\n", " plot_planet(name)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }