Astropy tables

A good way of keeping track of heterogeneous data is to use an astropy table. This allows you to track a variety of different types of data in a single, manipulable format. There are a bunch of other approaches to this from python dictionaries or fancy things like PANDAS data frames. Astropy tables are good because they interface neatly with FITS binary tables, a common format in our field. There is also some minimal LaTeX support should you need it for journal articles.

You may, at some point want to create your own table-like data. To build a table from scratch requires defining your own table structure. I find it most logical to build an empty table and then add a row each time through a processing loop. For example, you can define a table as follows:

from astropy.table import Table, Column

column_names = ['RA','Dec','Mass','Name']
# This specifies data types.  f8 means an 4-byte floating point 
# number and S20 means a 20 character long string.
column_types = ['f4','f4','f4','S20']

t = Table(names=column_names, dtype = column_types)

Alternatively, you can build a table up in a loop, which can be useful if you read in column names from another file.

t2 = Table()
for colname,coltype in zip(column_names,column_types):
	c = Column(name=colname,dtype=coltype)		
	t2.add_column(c)

Then, in each time through a processing loop, you can use the add_row() method to append a row to the table. What is clever is that you can then access the last (just added) row of the table using the table row index -1:

for thisstep in longloop:
	ra_value, dec_value = somecalculation()
	t.add_row()
	t[-1]['RA'] = ra_value
	t[-1]['DEC'] = dec_value
	t[-1]['Mass'] = mass_value
	t[-1]['Name'] = this_cloud_name

Once finished, you have a nice astropy table which you can then manipulate or potentially export:

t.write('mytable.fits',overwrite=True)

Your data are saved for next time or viewable in Topcat and the like.

Written on March 2, 2016