[AstroPy] Find record(s) with specific key in fits table
embray at stsci.edu
Mon Aug 31 10:28:31 EDT 2015
On 08/31/2015 09:36 AM, Steffen Brinkmann wrote:
> Currently, after a tedious hour of trying different things, I do the
> following to extract records with a specific key from a fits table. In
> this case the key name is 'OBJECT' and, say, I am looking for '18_Sco':
> $ from astropy.io import fits
> $ fits_data = fits.getdata("file.fits")
> $ fits_data[list(fits_data['OBJECT']).index('18_Sco')]
> The disadvantage is that only the first record with that 'OBJECT' key is
> $ fits_data[list(fits_data['OBJECT']) == '18_Sco']
> does not work. The output is a different record with another 'OBJECT'
> name. No idea why...
Because you're comparing a list to a string. This is the same as if you tried:
>>> ['a', 'b', 'c'] == 'd'
Python lists compare to other objects to by comparing element-wise. They'll
only compare true to something if that something is another list containing the
same values in the same order.
In Python True and False are equivalent to the integers 1 and 0, so the line of
code you quoted is always equivalent to:
Since the comparison will always return False.
Phil's response is the right approach.
FITS data returned by astropy.io.fits is in a Numpy array (for the most part,
with some caveats, though none that apply here). If you'll be working with
Python extensively you'll save yourself a lot of trouble in the future by
getting comfortable with Numpy (much of which is inspired by matlab, if that helps).
The Numpy documentation is found here:
I have an introductory tutorial to Numpy that I teach, which can be read here:
You should be able to find other people at MPIA who are familiar with Numpy as well.
More information about the AstroPy