Skip to content

Commit

Permalink
Merge pull request #24 from python4astronomers/v2
Browse files Browse the repository at this point in the history
Update for astropy 1.0, Python 3, and generally refresh
  • Loading branch information
taldcroft committed Mar 19, 2015
2 parents 26a27db + 57ceda6 commit c2fc89a
Show file tree
Hide file tree
Showing 51 changed files with 2,420 additions and 696 deletions.
60 changes: 0 additions & 60 deletions source/_templates/layout.html

This file was deleted.

42 changes: 24 additions & 18 deletions source/astropy-UVES/UVES.rst
@@ -1,4 +1,4 @@
Analyzing UVES Spectroscopy with Astropy
Astropy II: Analyzing UVES Spectroscopy
========================================

This tutorial follows my real live data analysis of MN Lup and the code developed
Expand All @@ -25,15 +25,21 @@ Please download this
the content, either by clicking on the link or by executing this
python code::

import urllib2, tarfile
from astropy.extern.six.moves.urllib import request
import tarfile
url = 'http://python4astronomers.github.io/_downloads/astropy_UVES.tar.gz'
tarfile.open(fileobj=urllib2.urlopen(url), mode='r|*').extractall()
tarfile.open(fileobj=request.urlopen(url), mode='r|*').extractall()
cd UVES
ls

Then start up IPython with the ``--pylab`` option to enable easy plotting::
Then start up IPython with the ``--matplotlib`` option and do the usual imports
to enable interactive plotting::

$ ipython --matplotlib

import numpy as np
import matplotlib.pyplot as plt

ipython --pylab

Acknowledgments
---------------
Expand Down Expand Up @@ -253,7 +259,7 @@ than two lines::
# Let's just print the setup on the screen
# We'll see if it's all the same.
for f in filelist:
print read_setup(f)
print(read_setup(f))

.. raw:: html

Expand All @@ -280,12 +286,12 @@ Units and constants in astropy
Often, one has to keep track of the units for certain values. Was the wavelength
given in Angstrom or in nm? In X-ray observations, a common unit of wavelength is
keV. How many nm is 0.65 keV?
`astropy.units <http://docs.astropy.org/en/v0.2.1/units/index.html>`_
`astropy.units <http://docs.astropy.org/en/stable/units/index.html>`_
offers a framework that can take
care of this book-keeping and propagate the units through many (but not all)
mathematical operations (e.g. addition, division, multiplication).
Furthermore,
`astropy.constants <http://docs.astropy.org/en/v0.2.1/constants/index.html>`_ supplies the values of
`astropy.constants <http://docs.astropy.org/en/stable/constants/index.html>`_ supplies the values of
many physical and astronomical constants.
The easiest way to attach a unit to a number is by multiplication::

Expand Down Expand Up @@ -326,13 +332,13 @@ energy conservation:
So, let us calculate the free-fall velocity for MN Lup::

>>> v_accr = (2.* G * M_MN_Lup/R_MN_Lup)**0.5
>>> print v_accr
>>> print(v_accr)
504469.027564 m / (s)
>>> # Maybe astronomers prefer it in the traditional cgs system?
>>> print v_accr.cgs
>>> print(v_accr.cgs)
50446902.7564 cm / (s)
>>> # Or in some really obscure unit?
>>> print v_accr.to(u.mile / u.hour)
>>> print(v_accr.to(u.mile / u.hour))
1128465.07598 mi / (h)

How does the accretion velocity relate to the rotational velocity?
Expand Down Expand Up @@ -374,7 +380,7 @@ to tell astropy that this number is dimensionless and does not carry any scaling
wavelength = wavelength * (1. * u.dimensionless_unscaled+ heliocentric/c)

I want to mention one more feature here (check out
`astropy.units <http://docs.astropy.org/en/v0.2.1/units/index.html>`_ for
`astropy.units <http://docs.astropy.org/en/stable/units/index.html>`_ for
more): The ability to convert the spectral axis to frequencies or energies.
Normally, a unit of length is not equivalent to a unit of energy or to a
frequency, but this conversion makes sense for the wavelength of a spectrum.
Expand Down Expand Up @@ -403,7 +409,7 @@ This is how it can be done::
The values from evolutionary tracks are indeed consistent with the
spectroscopically estimated surface gravity::
>>> print np.log10((G*M_MN_Lup/R_MN_Lup**2).cgs)
>>> print(np.log10((G*M_MN_Lup/R_MN_Lup**2).cgs))
4.3080943799180433

.. raw:: html
Expand All @@ -427,7 +433,7 @@ spectroscopically estimated surface gravity::
of the following wavelengths relative to :math:`H_\alpha`::

waveclosetoHa = np.array([6562.,6563,6565.]) * u.AA
print wave2doppler(waveclosetoHa, 656.489 * u.nm)
print(wave2doppler(waveclosetoHa, 656.489 * u.nm))

I get -132, -86 and +5 km/s.

Expand All @@ -441,7 +447,7 @@ spectroscopically estimated surface gravity::
doppler = ((w-w0)/w0 * c)
return doppler

print wave2doppler(waveclosetoHa, 656.489 * u.nm).decompose().to(u.km/u.s)
print(wave2doppler(waveclosetoHa, 656.489 * u.nm).decompose().to(u.km/u.s))

.. raw:: html

Expand Down Expand Up @@ -478,7 +484,7 @@ spectroscopically estimated surface gravity::

Converting times
----------------
`astropy.time <http://docs.astropy.org/en/v0.2.1/time/index.html>`_
`astropy.time <http://docs.astropy.org/en/stable/time/index.html>`_
provides methods to convert times and dates between different
systems and formats. Since the ESO fits headers already contain the time of the
observation in different systems, we could just read the keyword in the time
Expand Down Expand Up @@ -579,7 +585,7 @@ for the first spectrum::

ew = fcaII[0,:] - 1.
ew = ew[:-1] * np.diff(wcaII.to(u.AA).value)
print ew.sum()
print(ew.sum())

Using ``numpy`` array notation we can actually process all spectra at once::

Expand All @@ -589,7 +595,7 @@ Using ``numpy`` array notation we can actually process all spectra at once::
Now, we want to generate a LaTeX table of the observation times, period
and equivalent width that we can directly paste into our manuscript. To do so,
we first collect all the columns and make an ``astropy.table.Table`` object. (Please
check `astropy.table <http://docs.astropy.org/en/v0.2.1/table/index.html>`_
check `astropy.table <http://docs.astropy.org/en/stable/table/index.html>`_
or :ref:`tabular-data` for more
details on ``Table``). So, here is the code::

Expand Down
17 changes: 11 additions & 6 deletions source/astropy/astropy.rst
@@ -1,5 +1,5 @@
Astropy
=======
Astropy I: core functions
=========================

`Astropy <http://www.astropy.org>`_ is a community-developed core Python
package for Astronomy (with the term used in the broad sense, from Solar
Expand Down Expand Up @@ -32,15 +32,20 @@ Please download this
the content, either by clicking on the link or by executing this
python code::

import urllib2, tarfile
from astropy.extern.six.moves.urllib import request
import tarfile
url = 'http://python4astronomers.github.io/_downloads/astropy_examples.tar'
tarfile.open(fileobj=urllib2.urlopen(url), mode='r|').extractall()
tarfile.open(fileobj=request.urlopen(url), mode='r|').extractall()
cd data
ls

Then start up IPython with the ``--pylab`` option to enable easy plotting::
Then start up IPython with the ``--matplotlib`` option to enable interactive
plotting and then import numpy and matplotlib::

ipython --pylab
$ ipython --matplotlib

import numpy as np
import matplotlib.pyplot as plt

Acknowledgments
---------------
Expand Down
4 changes: 2 additions & 2 deletions source/astropy/coordinates.rst
Expand Up @@ -92,10 +92,10 @@ Practical Exercises

>>> from astropy import coordinates as coord
>>> crab = coord.ICRSCoordinates.from_name('M1')
>>> print crab
>>> print(crab)
<ICRSCoordinates RA=83.63308 deg, Dec=22.01450 deg>
>>> crab_gal = crab.transform_to(coord.GalacticCoordinates)
>>> print crab_gal
>>> print(crab_gal)
<GalacticCoordinates l=-175.44248 deg, b=-5.78434 deg>

.. raw:: html
Expand Down
15 changes: 9 additions & 6 deletions source/astropy/fits.rst
@@ -1,9 +1,11 @@
.. include:: ../references.rst

.. _handling-fits-files:

Handling FITS files
===================

.. note:: If you are already familiar with PyFITS, `astropy.io.fits` is in
.. note:: If you are already familiar with PyFITS, `astropy.io.fits`_ is in
fact the same code as the latest version of PyFITS, and you can
adapt old scripts that use PyFITS to use Astropy by simply doing::

Expand All @@ -17,7 +19,7 @@ Documentation
-------------

For more information about the features presented below, you can read the
`astropy.io.fits <http://docs.astropy.org/en/v0.2/io/fits/index.html>`_ docs.
`astropy.io.fits`_ docs.



Expand Down Expand Up @@ -108,7 +110,8 @@ header keywords using standard item notation::
>>> hdu.header['INSTRUME']
'LAT'

Provided that we started up ``ipython`` with the ``--pylab`` flag, we can plot
Provided that we started up ``ipython`` with the ``--matplotlib`` flag and did
``import matplotlib.pyplot as plt``, we can plot
one of the slices in photon energy::

>>> plt.imshow(hdu.data[0,:,:], origin='lower')
Expand Down Expand Up @@ -215,14 +218,14 @@ Accessing Tabular Data

In Astropy 0.2, FITS tables cannot be read/written directly from the ``Table``
class. To create a ``Table`` object from a FITS table, you can use
``astropy.io.fits``::
`astropy.io.fits`_::

>>> from astropy.io import fits
>>> from astropy.table import Table
>>> data = fits.getdata('catalog.fits', 1)
>>> t = Table(data)
and to write out, you can use ``astropy.io.fits``, converting the table to a
and to write out, you can use `astropy.io.fits`_, converting the table to a
Numpy array::

>>> fits.writeto('new_catalog.fits', np.array(t))
Expand Down Expand Up @@ -285,7 +288,7 @@ Practical Exercises
Using Matplotlib, make an all-sky plot of the LAT Background Model in the
Plate Carée projection showing the LAT Point Source Catalog overlaid with
markers, and with the correct coordinates on the axes. You should do this
using only ``astropy.io.fits``, Numpy, and Matplotlib (no WCS or
using only `astropy.io.fits`_, Numpy, and Matplotlib (no WCS or
coordinate conversion library). Hint: the -CAR projection is such that the
x pixel position is proportional to longitude, and the y pixel position to
latitude. Bonus points for a pretty colormap.
Expand Down
19 changes: 10 additions & 9 deletions source/astropy/tables.rst
Expand Up @@ -17,7 +17,7 @@ Documentation
-------------

For more information about the features presented below, you can read the
`astropy.table <http://docs.astropy.org/en/v0.2.1/table/index.html>`_ docs.
`astropy.table <http://docs.astropy.org/en/stable/table/index.html>`_ docs.


Constructing and Manipulating tables
Expand All @@ -43,7 +43,7 @@ about the table values and column definitions as follows::
If you print the table (either from the noteboook or in a text console
session) then a formatted version appears::

>>> print t
>>> print(t)
a b c
--- --- ---
1 2.0 x
Expand Down Expand Up @@ -79,7 +79,7 @@ arrays::
One can retrieve a subset of a table by rows (using a slice) or columns (using
column names), where the subset is returned as a new table::

>>> print t[0:2] # Table object with rows 0 and 1
>>> print(t[0:2]) # Table object with rows 0 and 1
a b c
--- --- ---
1 2.0 x
Expand All @@ -99,7 +99,7 @@ Modifying table values in place is flexible and works as one would expect::
>>> t[1] = (8, 9.0, "W") # Set all row values
>>> t[1]['b'] = -9 # Set column 'b' of row 1
>>> t[0:2]['b'] = 100.0 # Set column 'c' of rows 0 and 1
>>> print t
>>> print(t)
a b c
--- ----- ---
-1 100.0 x
Expand Down Expand Up @@ -132,7 +132,7 @@ Lastly, one can create a table with support for missing values, for example by s
fill_value = (999999, 1e+20, 'N'),
dtype = [('a', '<i8'), ('b', '<f8'), ('c', '|S1')])

>>> print t
>>> print(t)
a b c
--- --- ---
-- 2.0 x
Expand All @@ -159,7 +159,7 @@ You can read this in as a ``Table`` object by simply doing::
(just ignore the warnings, which are due to Vizier not complying with the VO
standard). We can see a quick overview of the table with::

>>> print t
>>> print(t)
_1RXS RAJ2000 DEJ2000 PosErr NewFlag Count e_Count HR1 e_HR1 HR2 e_HR2 Extent
---------------- --------- --------- ------ ------- --------- --------- ----- ----- ----- ----- ------
J000000.0-392902 0.00000 -39.48403 19 __.. 0.13 0.035 0.69 0.25 0.28 0.24 0
Expand All @@ -178,7 +178,8 @@ standard). We can see a quick overview of the table with::
J235944.7+220014 359.93625 22.00389 17 __.. 0.052 0.015 -0.01 0.27 0.37 0.35 0
J235959.1+083355 359.99625 8.56528 10 __.. 0.12 0.018 0.54 0.13 0.10 0.17 9

Since we are using IPython with the ``--pylab`` option, we can easily make a
Since we are using IPython with the ``--matplotlib`` option along with
``import matplotlib.pyplot as plt``, we can easily make a
histogram of the count rates::

>>> plt.hist(t['Count'], range=[0., 2], bins=100)
Expand Down Expand Up @@ -218,7 +219,7 @@ Practical Exercises
::

>>> t.keep_columns(['RAJ2000', 'DEJ2000', 'Count'])
>>> print t
>>> print(t)
RAJ2000 DEJ2000 Count
--------- --------- ---------
0.00000 -39.48403 0.13
Expand All @@ -239,7 +240,7 @@ Practical Exercises
Note that you can also do this with::
>>> t_new = t['RAJ2000', 'DEJ2000', 'Count']
>>> print t_new
>>> print(t_new)
RAJ2000 DEJ2000 Count
--------- --------- ---------
0.00000 -39.48403 0.13
Expand Down

0 comments on commit c2fc89a

Please sign in to comment.