user3685310
user3685310

Reputation:

How to make a sort from a fits table python

I own a table in fits format. I am using the astropy.io.fits library in Python. I'm reading the file and I would like to print my array with conditions.

For exemple, I want to print all rows where the column "CHI" > value

#!/usr/bin/python
# coding: utf-8

from astropy.io import fits
import numpy as np

        ##################################
        # Fichier contenant le champ 169 #
        ##################################

file = '/astromaster/home/xxxx/Bureau/Stage/Champs/Field_169/Field169_combined_final_roughcal.fits'

field169 = fits.open(file)          # Ouverture du fichier à l'aide d'astropy

tbdata = field169[1].data           # Lecture des données fits

#print tbdata['CHI']
#print tbdata[tbdata['CHI'] < 1.0]

for row in tbdata :
    if tbdata['CHI'] < 1.0 :
        print row

And, how is it possible to add one or two more conditions in the new printed array ?

Thanks !

Upvotes: 3

Views: 1233

Answers (2)

MSeifert
MSeifert

Reputation: 152765

Well first of all and that's unrelated to your question: If you open any file you should either close it again afterwards or use it as context manager. I usually prefer the context manager:

filename = '...'
with fits.open(filename) as field169:
    tbdata = field169[1].data
... continue without indentation after you have finished with the file.

Afterwards you have a record numpy array, you can use it but since structured numpy arrays are more tables a true table program like pandas or astropy.table might be very convenient.

But if you really want to confine yourself to the table you get from fits.open (you shouldn't though because the recarray and structured array behave a bit different than normal table operations) you can use what is given in the documentation:

mask = tbdata['CHI'] < 1.0
tbdata[mask]

or if you want to have multiple conditions just add them with the & (if both should apply) or | (if at least one of the conditions should apply):

mask = tbdata['CHI'] < 1.0 & tbdata['CHI'] > 0.5
tbdata[mask]

mask = tbdata['CHI'] < 1.0 | tbdata['OTHER'] > 0.5
tbdata[mask]

I hope this helps you. But probably you are better off using @DathosPachy answer with tables.

Upvotes: 2

DathosPachy
DathosPachy

Reputation: 742

Astropy's fits interface functionality is best for working with image data, rather than tabular.

The Astropy tables library will be helpful to you. Using t = astropy.table.Table.read(fname) (aside: file is reserved in Python, so you should avoid using it as a variable name) will allow you to read in a FITS table as an Astropy table, which works a lot like a numpy record array.

You can then filter based on column values, e.g.: print t[t['CHI'] < 1.] (If you aren't familiar with numpy, you might do some more reading on masking and element selection, since this is essentially identical to numpy's functionality--if you aren't familiar, I think of reading the above statement "t such that t['CHI'] less than 1.")

You can combine conditions, as well: print t[(t['A'] < 1) & (t['B'] > 2)]. And when you're done, you can write out using t.write(fname2)

Upvotes: 2

Related Questions