For more information about the features presented below, you can read the astropy.table docs.
import numpy as np
from astropy.table import Table
t1 = Table()
t1['name'] = ['source 1', 'source 2', 'source 3']
t1['flux'] = [1.2, 2.2, 3.1]
t1
print(t1)
t1['size'] = [1,5,4]
t1
t1['size']
np.array(t1['size'])
t1['size'][0]
It is possible to iterate over rows or over columns. To iterate over rows, simply iterate over the table itself:
for row in t1:
print(row)
Rows can act like dictionaries, so you can access specific columns from a row:
for row in t1:
print(row['name'])
You can also access rows by accessing a numerical item in the table:
row = t1[0]
Iterating over columns is also easy:
for colname in t1.columns:
column = t1[colname]
print(column)
Accessing specific rows from a column object can also be done with the item notation:
for colname in t1.columns:
column = t1[colname]
print(column[0])
from astropy.table import join
t2 = Table()
t2['name'] = ['source 1', 'source 3']
t2['flux2'] = [1,9]
t3 = join(t1, t2, join_type='outer')
t3
np.mean(t3['flux2'])
from astropy.table import Table
obs = Table.read("""name obs_date mag_b mag_v
M31 2012-01-02 17.0 17.5
M31 2012-01-02 17.1 17.4
M101 2012-01-02 15.1 13.5
M82 2012-02-14 16.2 14.5
M31 2012-02-14 16.9 17.3
M82 2012-02-14 15.2 15.5
M101 2012-02-14 15.0 13.6
M82 2012-03-26 15.7 16.5
M101 2012-03-26 15.1 13.5
M101 2012-03-26 14.8 14.3
""", format='ascii')
obs_by_name = obs.group_by('name')
obs_by_name
for group in obs_by_name.groups:
print(group)
print("")
obs_by_name.groups.aggregate(np.mean)
t4 = Table(masked=True)
t4['name'] = ['source 1', 'source 2', 'source 3']
t4['flux'] = [1.2, 2.2, 3.1]
t4['flux'].mask = [1,0,1]
t4
t3.write('test.fits', overwrite=True)
t3.write('test.vot', format='votable', overwrite=True)
You can download the data used below here.
t4 = Table.read('data/2mass.tbl', format='ascii.ipac')
t4
These exercises use the all-sky ROSAT catalog which you can download from here.
Try and find a way to make a table of the ROSAT point source catalog that
contains only the RA, Dec, and count rate. Hint: you can see what methods
are available on an object by typing e.g. t.
and then pressing
<TAB>
. You can also find help on a method by typing e.g.
t.remove_column?
.
Make an all-sky equatorial plot of the ROSAT sources, with all sources shown in black, and only the sources with a count rate larger than 2. shown in red (bonus points if you use an Aitoff projection!)
Try and write out the ROSAT catalog into a format that you can read into another software package. For example, try and write out the catalog into CSV format, then read it into a spreadsheet software package (e.g. Excel, Google Docs, Numbers, OpenOffice).