Astropy Fits: How to write out a table with rows sliced out?

人走茶凉 提交于 2019-12-02 02:14:33

If you want to stick to using FITS_rec, you can try the following, which seems to be a workaround:

new_hdu = fits.BinTableHDU.from_columns(hdu_sliced._get_raw_data())

Can you use astropy.table.Table instead of astropy.io.fits.BinTable?

It's a much more friendly table object.

One way to make a row selection is to index into the table object with a list (or array) of rows you want:

>>> from astropy.table import Table
>>> table = Table()
>>> table['col_a'] = [1, 2, 3]
>>> table['col_b'] = ['spam', 'ham', 'jam']
>>> print(table)
col_a col_b
----- -----
    1  spam
    2   ham
    3   jam
>>> table[[0, 2]] # Table with rows 0 and 2 only, row 1 removed (a copy)
<Table length=2>
col_a col_b
int64  str4
----- -----
    1  spam
    3   jam

You can read and write to FITS directly with Table:

table = Table.read('file.fits', hdu='mydata')
table2 = table[[2, 7, 10]]
table2.write('file2.fits')

There are potential issues, e.g. the FITS BINTABLE header isn't preserved when using Table, only the key, value info is storead in table.meta. You can consult the Astropy docs on table and FITS BINTABLE for details about the two table objects, how they represent data or how you can convert between the two, or just ask follow-up questions here or on the astropy-dev mailing list.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!