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

后端 未结 2 720
囚心锁ツ
囚心锁ツ 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: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)
    
    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.

    提交回复
    热议问题