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

后端 未结 2 616
囚心锁ツ
囚心锁ツ 2021-01-20 01:42

I\'m currently working with some fits tables and I\'m having trouble with outputting in Astropy.io.fits. Essentially, I am slicing out a bunch of rows that have data for obj

相关标签:
2条回答
  • 2021-01-20 02:13

    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())
    
    0 讨论(0)
  • 2021-01-20 02:32

    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.

    0 讨论(0)
提交回复
热议问题