{ "metadata": { "name": "", "signature": "sha256:4ae85c6955a91ceb2f2b1a4978dc23c53cb3126a70825ee2781c8f1f10bd7132" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ " Working with Astronomical Coordinate Systems" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Author: [Eduardo Mart\u00edn Calleja](http://balbuceosastropy.blogspot.com.es/)\n", "\n", "The purpose of this post is to show how you can work with the various astronomical coordinate systems using appropriate Python libraries.\n", "\n", "Converting between various types of astronomical coordinates will be useful in future posts.This entry, as most of the following, will be fully written with the IPython Notebook" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Imports and references" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%matplotlib inline\n", "from __future__ import division\n", "\n", "# Import of the PyEphem package\n", "import ephem\n", "\n", "from astropy.coordinates import SkyCoord\n", "\n", "#This removes some nasty deprecation warnings that do not interfere with the execution\n", "import warnings\n", "warnings.filterwarnings('ignore')\n", "\n", "# This IPython magic generates a table with version information\n", "#https://github.com/jrjohansson/version_information\n", "%load_ext version_information\n", "%version_information ephem, astropy" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
SoftwareVersion
Python2.7.9 64bit [GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]
IPython2.3.1
OSLinux 3.13.0 45 generic x86_64 with debian jessie sid
ephem3.7.5.3
astropy0.4.3
Fri Feb 20 12:51:19 2015 CET
" ], "json": [ "{\"Software versions\": [{\"version\": \"2.7.9 64bit [GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]\", \"module\": \"Python\"}, {\"version\": \"2.3.1\", \"module\": \"IPython\"}, {\"version\": \"Linux 3.13.0 45 generic x86_64 with debian jessie sid\", \"module\": \"OS\"}, {\"version\": \"3.7.5.3\", \"module\": \"ephem\"}, {\"version\": \"0.4.3\", \"module\": \"astropy\"}]}" ], "latex": [ "\\begin{tabular}{|l|l|}\\hline\n", "{\\bf Software} & {\\bf Version} \\\\ \\hline\\hline\n", "Python & 2.7.9 64bit [GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] \\\\ \\hline\n", "IPython & 2.3.1 \\\\ \\hline\n", "OS & Linux 3.13.0 45 generic x86\\_64 with debian jessie sid \\\\ \\hline\n", "ephem & 3.7.5.3 \\\\ \\hline\n", "astropy & 0.4.3 \\\\ \\hline\n", "\\hline \\multicolumn{2}{|l|}{Fri Feb 20 12:51:19 2015 CET} \\\\ \\hline\n", "\\end{tabular}\n" ], "metadata": {}, "output_type": "pyout", "prompt_number": 1, "text": [ "Software versions\n", "Python 2.7.9 64bit [GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]\n", "IPython 2.3.1\n", "OS Linux 3.13.0 45 generic x86_64 with debian jessie sid\n", "ephem 3.7.5.3\n", "astropy 0.4.3\n", "Fri Feb 20 12:51:19 2015 CET" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Some useful references:\n", "\n", "[PyEphem home page](http://rhodesmill.org/pyephem/)\n", "\n", "[Astrometry in Python with PyEphem](http://oneau.wordpress.com/2010/07/04/astrometry-in-python-with-pyephem/)\n", "\n", "[PyEphem Tutorial](http://zeus.asu.cas.cz/extra/pyephem-manual.html)" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "1. Equatorial spherical coordinates" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "They are defined by Right Ascension (ra), Declination (dec) and an epoch.\n", "\n", "- The ra is normally represented as a string of the form \"hh: mm: ss\"\n", "\n", "- The dec is usually represented as a string of the form \"/ - \u00b0: ':''\"\n", "\n", "- The default is epoch J2000.0\n", "\n", "Its elements are:\n", "\n", "- The origin is the center of the Earth (ie, they are geocentric coordinates)\n", "- The fundamental plane is the projection of the terrestrial Ecuador on the celestial sphere\n", "- The primary direction is the spring equinox on a date (the epoch)\n", "\n", "By convention the ra is measured positively to the east and north.\n", "\n", "The equatorial coordinates of an object are represented in ephem as instances of the Equatorial class. ra, dec and epoch are object attributes" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Let's define the equatorial coordinates of an object\n", "eq=ephem.Equatorial('13:24:42.5','-60:15:59.4')\n", "\n", "# The values are internally stored in radians\n", "eq.ra, eq.dec, eq.epoch" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 2, "text": [ "(3.511202483725676, -1.0518488536531627, 36524.5)" ] } ], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "# However they are printed as strings\n", "print eq.ra, '\\t', eq.dec, '\\t', eq.epoch" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "13:24:42.50 \t-60:15:59.4 \t2000/1/1 00:00:00\n" ] } ], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "# We can get a tuple (ra,dec) by using the get() method\n", "print eq.get()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "(3.511202483725676, -1.0518488536531627)\n" ] } ], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "# To find the constellation corresponding to a coordinate tuple:\n", "ephem.constellation((eq.ra,eq.dec))" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 5, "text": [ "('Cen', 'Centaurus')" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "We may also use other epoch different of J2000:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "eq50=ephem.Equatorial('13:24:42.5','-60:15:59.4',epoch=ephem.B1950)\n", "print eq50.ra, '\\t', eq50.dec, '\\t', eq50.epoch" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "13:24:42.50 \t-60:15:59.4 \t1949/12/31 22:09:50\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "2. Galactic coordinates" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is another spherical coordinate system. Its elements are:\n", "\n", "- The center is located in the Sun (they are heliocentric coordinates)\n", "- The fundamental plane is approximately the plane of our galaxy (the Milky Way)\n", "- The primary direction points about to the center of our own galaxy\n", "- By comnvention the directions are positive northward and eastward at the fundamental plane.\n", "\n", "The coordinates are longitude (lon / l) and latitude (lat / b), both expressed in degrees, not hours" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Definition of the galactic coordinates of an object\n", "ga=ephem.Galactic('1:59:55.9','+89:59:59.9') \n", "print ga.lon, ga.lat\n", "\n", "# Longitude and latitude are returned by the get() method. They are shown in radians\n", "print ga.get()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1:59:55.9 89:59:59.9\n", "(0.0348867076789611, 1.5707958419812156)\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "3. Conversion between coordinate systems" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The coordinates can be converted from one system to another giving an object of the first type as an input to a class of the second type:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Conversion of equatorial coordinates between epochs\n", "eq1 = eq=ephem.Equatorial('13:24:42.5','-60:15:59.4')\n", "eq2 = ephem.Equatorial(eq1,epoch=ephem.B1950)\n", "print eq1.get()\n", "print eq2.get()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "(3.511202483725676, -1.0518488536531627)\n", "(3.49702212460356, -1.04730555904456)\n" ] } ], "prompt_number": 8 }, { "cell_type": "code", "collapsed": false, "input": [ "# Conversi\u00f3o from galactic to equatorial coordinates\n", "# (we can in fact omit to declare the epoch, J2000 by default)\n", "ga1 = ephem.Galactic('0','90',epoch=ephem.J2000)\n", "eq1 = ephem.Equatorial(ga1)\n", "print eq1.ra, eq1.dec" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "12:51:26.28 27:07:41.7\n" ] } ], "prompt_number": 9 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "4. Get the coordinates of an object by name" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For this we'll use the SkyCoord module from the astropy library, which gets the coordinates from the [SESAME](http://cds.u-strasbg.fr/cgi-bin/Sesame) server" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The \"International Celestial Reference System\" (ICRS) is the celestial reference system adopted as standard today by the International Astronomical Union (IAU). These coordinates coincide very nearly with the J2000.0 equatorial coordinates. ICRS coordinates can be obtained by giving the name of the object in any of the catalogs used by the service SESAME" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Whirlpool Galaxy\n", "SkyCoord.from_name(\"M51\") # \"icrs\" es la opci\u00f3n por defecto" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 10, "text": [ "" ] } ], "prompt_number": 10 }, { "cell_type": "code", "collapsed": false, "input": [ "# We can as well use other names for the same galaxy\n", "SkyCoord.from_name('NGC 5194')" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 11, "text": [ "" ] } ], "prompt_number": 11 }, { "cell_type": "code", "collapsed": false, "input": [ "# The same object in galactic coordinates\n", "SkyCoord.from_name(\"M51\", frame='galactic')" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 12, "text": [ "" ] } ], "prompt_number": 12 }, { "cell_type": "markdown", "metadata": {}, "source": [ "To get the values of the angles (longitude and latitude in this case) there are several options:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "c = SkyCoord.from_name(\"M51\", frame='galactic')\n", "# longitude and latitude can be accessed internally with c.l y c.b\n", "# The internal representation of the angles is in degrees \n", "# However, these values can not be operated directly\n", "c.l, c.b" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 13, "text": [ "(, )" ] } ], "prompt_number": 13 }, { "cell_type": "markdown", "metadata": {}, "source": [ "To obtain numerical values of the angles at which to operate, proceed as follows:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# angles in radians\n", "print c.l.radian, c.b.radian\n", "\n", "# Angles in degrees\n", "print c.l.degree, c.b.degree\n", "\n", "# Arithmetic operation example\n", "c.l.degree + 90" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1.83000537933 1.19660998408\n", "104.851584722 68.5607018112\n" ] }, { "metadata": {}, "output_type": "pyout", "prompt_number": 14, "text": [ "194.85158472198123" ] } ], "prompt_number": 14 }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the case of ICRS coordinates, the coordinate angles are ra and dec" ] }, { "cell_type": "code", "collapsed": false, "input": [ "c = SkyCoord.from_name(\"M51\", frame='icrs')\n", "\n", "# Prints a string representation unfit to operate with\n", "print c\n", "\n", "# provides a tuple \"hours\", \"minutes\", \"seconds\"\n", "print c.ra.hms\n", "\n", "# Values in degrees\n", "print c.dec.degree\n", "\n", "# Of course they can be expressed in other types of units\n", "print c.ra.radian, c.dec.radian" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "hms_tuple(h=13.0, m=29.0, s=52.69800000000572)\n", "47.1952583\n", "3.53376071886 0.823712648664\n" ] } ], "prompt_number": 15 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Another coordinate system provided by the astropy package is FK5. It is an equatorial coordinate system based on the Epoch J2000, which practically coincides with the coordinates ICRS" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# ICRS coordinates\n", "c = SkyCoord.from_name(\"M51\", frame='icrs')\n", "\n", "# FK5 coordinates\n", "c5 = SkyCoord.from_name(\"M51\", frame='fk5')\n", "\n", "# In this example you can see the similarity between the two\n", "print 'ICRS coordinates: ',c.ra.hms, c.dec.degree\n", "print 'FK5 coordinates: ', c5.ra.hms, c5.dec.degree\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "ICRS coordinates: hms_tuple(h=13.0, m=29.0, s=52.69800000000572) 47.1952583\n", "FK5 coordinates: hms_tuple(h=13.0, m=29.0, s=52.697952632941849) 47.1952580768\n" ] } ], "prompt_number": 16 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "5. Convert between coordinate systems with astropy" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Let's obtain the galactic coordinates of a star\n", "# We chose a southern hemisphere star\n", "\n", "c = SkyCoord.from_name('Achernar', frame='galactic')\n", "c" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 17, "text": [ "" ] } ], "prompt_number": 17 }, { "cell_type": "code", "collapsed": false, "input": [ "# Let's turn to FK5 equatorial coordinates:\n", "c.fk5" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 18, "text": [ "" ] } ], "prompt_number": 18 }, { "cell_type": "code", "collapsed": false, "input": [ "# Conversely, from the ICRS obtain the galactic coordinates:\n", "c = SkyCoord.from_name('Achernar', frame='icrs')\n", "c.galactic" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 19, "text": [ "" ] } ], "prompt_number": 19 } ], "metadata": {} } ] }