Using astropy cosmology


To use the astropy cosmology package, first make sure astropy is installed on your python package. If you use anaconda, it is installed by default. Otherwise, you can go to the astropy website for download and install information. Information on the cosmology package is here.

In python, load the package by saying

from astropy import cosmology

Then you define one or more cosmologies to work with by saying:

cosmo_flat_nolam=cosmology.LambdaCDM(H0=70, Om0=1.0, Ode0=0.0)
cosmo_flat_lam=
cosmology.LambdaCDM(H0=70, Om0=0.3, Ode0=0.7)
etc....

(you can also define lots of other parameters to set the cosmology, but the three listed above are all we will be needing for class.)

Then you can use lots of tools to get ages, distances, lookback times, etc for a given cosmology. For example, if you want to know the age of the flat_nolam universe at z=3, you just say

print cosmo_flat_nolam.age(z=3.0)

or if you want to make a plot as a function of z for both universes, say

z=np.arange(0.0,5.0,0.01)
age=cosmo_flat_nolam.age(z)
plt.plot(z,age)
age=cosmo_flat_lam.age(z)
plt.plot(z,age)

To see a list of all functions you can get from the cosmology package, you can say

dir(cosmo_flat_nolam)

and then help on a specific function by saying

help(cosmo_flat_nolam.function_name)

These functions return not only values but units, which can sometimes confuse non-astropy functions, for example

print np.log10(cosmo_flat_nolam.age(3.0))

throws an error complaining about taking the log of a value with units, so you have to instead say

print np.log10(cosmo_flat_nolam.age(3.0).value)

to get only the value without the units attached (but you should know what the units are!).