First familiarize yourself with the available supported formats:
Answer:
The 'cds'
, 'daophot'
, and 'ipac'
formats have support for units and/or metadata.
Read the following files in the data/ascii/
sub-directory and show the contents for each one. Try to use the minimum effort and number of non-default options possible.
'basic.dat'
'ipac.dat'
-- from NASA/IPAC Infrared Science Archive'daophot.dat.gz'
-- Hint: unzipping is automatic'cds/lhs2065.dat'
-- Hint: read the docs! Note that Vizier catalog results are in CDS format.'fixed_width.dat'
-- Hint: check out the Fixed width gallery.from astropy.table import Table
Table.read('data/ascii/basic.dat', format='ascii')
Table.read('data/ascii/ipac.dat', format='ascii.ipac')
Table.read('data/ascii/daophot.dat.gz', format='ascii.daophot')
Table.read('data/ascii/cds/lhs2065.dat', readme='data/ascii/cds/ReadMe', format='ascii.cds')
Table.read('data/ascii/fixed_width.dat', header_start=1, position_line=2, data_end=-1,
format='ascii.fixed_width_two_line')
For the table from 'ipac.dat'
, there is table metadata that is in an ordered dictionary in the table meta
attribute. If you read the table into a variable named dat
then you would be looking at dat.meta
.
dat.meta
. What are they?keywords
and their values from dat.meta
, one per line in the format keyword: value
.colnames
.dat = Table.read('data/ascii/ipac.dat', format='ascii.ipac')
dat.meta.keys()
for name, keyword in dat.meta['keywords'].items():
print('{0}: {1}'.format(name, keyword['value']))
for colname in dat.colnames:
print('{0}: {1}'.format(colname, dat[colname].unit))
Read two slightly challenging tables:
hard1.dat
-- This is part of a real data file produced by a Sybase SQL query.hard2.dat
-- Your neighbor asked you to help computerize his garden and handed you this data file to read. You'll need to study up on fill_values
.t = Table.read('data/ascii/hard1.dat', format='ascii', delimiter='|', data_start=2)
t.remove_column('col0')
t
Table.read('data/ascii/hard2.dat', format='ascii.no_header', delimiter=':', data_start=5,
names=['year', 'peas', 'beans', 'carrots'], fill_values=[('not sure', 0)])
Start from the table from 'ipac.dat'
and make a new table that has just first 2 lines of the first 3 columns. This is done with:
>>> dat3 = dat['designation', 'ra', 'dec'][:2]
dat3 = dat['designation', 'ra', 'dec'][:2]
Find the output format output that will create each of the following three outputs from the table t3
. You can use the pattern:
dat3.write(sys.stdout, format='...', ...) # assuming import sys beforehand
designation,ra,dec,sigra,sigdec
J095542.55+690421.2,148.9273263,69.0725621,0.0401,0.039
J095540.45+690439.1,148.9185759,69.0775361,0.0519,0.0494
import sys
dat3.write(sys.stdout, format='ascii', delimiter=',')
designation ra dec
------------------- ----------- ----------
J095542.55+690421.2 148.9273263 69.0725621
J095540.45+690439.1 148.9185759 69.0775361
dat3.write(sys.stdout, format='ascii.fixed_width_two_line')
\begin{table}
\begin{tabular}{ccc}
designation & ra & dec \\
J095542.55+690421.2 & 148.9273263 & 69.0725621 \\
J095540.45+690439.1 & 148.9185759 & 69.0775361 \\
\end{tabular}
\end{table}
dat3.write(sys.stdout, format='ascii.latex')
t3
table back out in IPAC format. Does it look like the output has the same units and metadata as the original?dat3.write(sys.stdout, format='ipac')