Python Tutorials I

In this section we will review some of the concepts seen in the introduction, such as units, coordinate systems, etc. and work some tutorials in Python

This is not intended to be a complete course on programing in python .The idea is mor to put python in the context of astro-article physics, and address some of the questions seen in the following tutorials.

Working with units

In this first case or tutorial we are going to show a n example of how to work with units using the module units that exist for example in astropyarrow-up-right

import astropy.units as u
from astropy import constants as const

M_Earth = 5.97E24 * u.kg
M_Sun = 1.99E30 * u.kg
M_MW  = 1E12 * M_Sun
#By adding the quantity u.kg you can print directly the mass in Kg
print ("Mass Earth is: %s" %M_Earth)
circle-info

There are many other modules in python that have the units utility

Note that the variables defined above already have their units attached to them, so when you make a print statement it will provide as well the units as a string. That's the reason of the %s format. More examples:

R_Earth = 6.371E6 * u.m # meters
R_Sun = 6.955E8 * u.m # meters
AU = 1.496E11 * u.m # meters

With the radius we can calculate the mean density of Earth and the Sun. We will show that units are preserved along calculations:

from numpy import pi
vol_sphere = lambda r: 4*pi/3*r**3
rho_Sun = M_Sun / vol_sphere(R_Sun)
rho_Earth = M_Earth / vol_sphere(R_Earth)

#A unit can be changed calling the .to(u.unit) method
print ("Density of Earth: %s" %(rho_Earth.to(u.g/u.cm**3)))
print ("Density of Sun: %s" %rho_Sun)

We can use this module to make different transformations of units, for example from light years to meters:

Assuming that the Galaxy is roughly a disk 50 kpc in diameter and 500 pc thick we can now calculate its density:

circle-exclamation

Coordinates

The coordinates package arrow-up-rightof astropy provides classes for representing a variety of celestial/spatial coordinates and their velocity components, as well as tools for converting between common coordinate systems in a uniform way. The simplest way to use coordinates is to use the SkyCoord class. SkyCoord objects are instantiated by passing in positions (and optional velocities) with specified units and a coordinate frame.

With SkyCoord you can handle more than one set of coordinates:

You can use from_name method of SkyCoord to retrieve the coordinates of known sources. It uses Sesame (http://cds.u-strasbg.fr/cgi-bin/Sesamearrow-up-right) to retrieve coordinates for a particular named object:

circle-exclamation

Working with Tables

Astropy.table (http://docs.astropy.org/en/stable/table/arrow-up-right) provides functionality for storing and manipulating heterogeneous tables of data.

Let's create a simple table with three columns of data named a, b, and c. These columns have integer, float, and string values respectively:

To check which columns are available you can use Table.colnames:

And access individual columns just by their name:

And also a subset of columns:

Rows can be accessed using numpy indexing:

Or by using a boolean numpy array for indexing:

Astropy tables can be serialized into many formats. Some examples below:

To sort the table by column 'a':

circle-info

🖋️ Exercise: The (Lon, Lat) positions of Crab, Sagittarius A* and Cassiopeia A in the Galactic Coordinate system are:

  • (184.55754381,-5.78427369) Crab

  • (359.94422947,-0.04615714) Sag A*

  • (111.74169477,-2.13544151) Cas A

Put in a Table the positions (with the first two columns for Lon, Lat) of the three objects (rows), and add two columns with the RA and DEC coordinates of the objects

The Astroquery package

Another package that allows one to retrieve information from astronomy databases for further processing is astroquery. It can query in different databases (SIMBADarrow-up-right , SDSSarrow-up-right). You can import this package to explore the SDSS database by inserting the following line at the top of your script:

circle-info

🖋️ Exercise: Using the astroquery.sdss imported above, call the function query_region to get the following output [’ra’, ’dec’, ’u’, ’g’, ’r’, ’i’, ’z’, ’type’] in 1 arc minutes around ’pos’. Call this output ’xid’. Hint: To specify the output, use the option photoobj_fields If you check the type of this output, you see that it is of type Table.

Working with coordinates

Now let's do a little tutorial. We are going to work. Let's do some "representation" of the galactic plane. We generate some random points using numpy following a 2-dimensional gaussian in the \ell: π,+π-\pi, +\pi and bb:π/2,+π/2-\pi/2, +\pi/2 space. Now we are going to use matplotlib to make plots. If you are using jupyter notebook, don't forget to use the magic command %matplotlib inline if you want to make the plots appear inline inside the notebook:

Now let's plot it in equatorial coordinates (right ascension, declination). Because matplotlib needs the coordinates in radians and between −π\pi and π\pi, not between 0 and 2π2\pi, we have to convert them.

circle-exclamation

Calculating the Age of the Universe

We are going to use python to numerically solve the look-back time for any given set of cosmological parameters. For that we are going to rewrite the loopback formula in terms of the scale factor $a$ since we have the redshift scale relation:

(1+z)=1a(1+z) = \frac{1}{a}

we can prove thatL

dz1+z=daa\frac{{\rm d}z}{1 +z} = -\frac{{\rm d}a}{a}
dt=daH(a)a{\rm d}t = \frac{{\rm d}a}{H(a)a}

with

H2(a)=H02[ΩM0a3+Ωr0a4+Ωk0a2+ΩΛ0]H^2(a) = H^2_0 [\Omega^0_M a^{-3} + \Omega^0_r a^{-4} + \Omega^0_k a^{-2} +\Omega^0_\Lambda]

First we are going to use the astropy.cosmology module to retrieve the values of the cosmological constants using Planck data from 2013.

Last updated