user3125347 user3125347 - 21 days ago 5
Python Question

Specifying a large commented header using astropy.tables module

N.B. I'm using a combination of astropy's Table and ascii modules.

I'm looking to include the following commented header above my table:

#1 pxlname 1
#2 x 0
#3 y 0
#4 z 8
#5 Dist 9
#6 FUV 6 FUV
#7 UVW2 6 UVW2
#8 UVM2 6 UVM2
#9 NUV 6 NUV
#10 UVW1 6 UVW1
#11 sdssu 6 u_sdss
#12 sdssg 6 g_sdss
#13 sdssr 6 r_sdss
#14 sdssi 6 i_sdss
#15 sdssz 6 z_sdss
#16 J 6 J_2MASS
#17 H 6 H_2MASS
#18 K 6 Ks_2MASS
#19 FUV_err 7
#20 UVW2_err 7
#21 UVM2_err 7
#22 NUV_err 7
#23 UVW1_err 7
#24 sdssu_err 7
#25 sdssg_err 7
#26 sdssr_err 7
#27 sdssi_err 7
#28 sdssz_err 7
#29 J_err 7
#30 H_err 7
#31 K_err 7


At which point my data will now proceed.

Currently, I write my data with the following:

table_ssp_M33_SWIFT_cat = Table([pxlname, x, y, SDSS_u, SDSS_g, SDSS_r, SDSS_i, SDSS_z], names=('##pxlname', 'x', 'y', 'SDSS_u', 'SDSS_g', 'SDSS_r', 'SDSS_i', 'SDSS_z'))

ascii.write(table_ssp_M33_SWIFT_cat, 'table_ssp_M33_SWIFT_cat.dat')


Essentially I would like this header above the
##pxlname x y
etc, etc columns.

Is it possible to achieve this?

Answer

Something like this:

In [29]: t = Table([[1],[1]], names=('##pxlname', 'x'))
In [30]: t.meta['comments'] = ['1 pxlname 1', '2 x      0']  # you define these.
In [31]: t.write('out.dat', format='ascii')
In [32]: cat out.dat
# 1 pxlname 1
# 2 x      0
##pxlname x
1 1

In [33]: t2 = t.read('out.dat', format='ascii', comment='# ')
In [34]: t2
Out[34]: 
<Table length=1>
##pxlname   x  
  int64   int64
--------- -----
        1     1

In [35]: print(t2.meta['comments'])
['1 pxlname 1', '2 x      0']