简体   繁体   中英

Picking out a single row in a FITS table with Astropy

I "simply" want to read in an (Astronomy) FITS table, and pick out all the information on one object via its name::

from astropy.io import fits
dr7q = fits.open('Shen_dr7_bh_May_2010.fits')
tbdata = dr7q[1].data
w = tbdata[tbdata['SDSS_NAME'] == 'J000006.53+003055.2']

print(tbdata[w])

gives an

IndexError: arrays used as indices must be of integer (or boolean) type

Trying:

mask = (tbdata['SDSS_NAME'] == 'J000006.53+003055.2')
print(mask)

then gives an array the size of the original FITS table file. This is straight-forward in IDL. Why's it so hard here?!?!

The mask should be an array of booleans, right?

>>> tbdata['sdssj'] == '000006.53+003055.2'
array([True, False, False, ..., False, False, False], dtype=bool)

If you want an array of the integers where the condition is true, use np.nonzero or np.flatnonzero:

>>> np.flatnonzero(tbdata['sdssj'] == '000006.53+003055.2')
array([0])

which you should then be able to use to index the array:

>>> T[T['sdssj'] == '000006.53+003055.2']
FITS_rec([ ('000006.53+003055.2', 0.027227999999999999, 0.51534100000000005, 1.8246, 20.384, 0.065000000000000002, 20.460999999999999, 0.034000000000000002, 20.324000000000002, 0.037999999999999999, 20.093, 0.041000000000000002, 20.042000000000002, 0.121, 0.13, 20.507999999999999, 0.0, 0.0, 0.0, -9.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0, -25.109000000000002, 0.090999999999999998, 0, 0, 1, 0, 34603008, 0, 0, 0, 0, 1, 0, 0, 3325, 52522, 52203, 685, 467, 41, 5, 108, 118, 34603008, 0, 0, 0, 0, 1, 0, 0, 20.384, 0.065000000000000002, 20.460999999999999, 0.034000000000000002, 20.324000000000002, 0.037999999999999999, 20.093, 0.041000000000000002, 20.042000000000002, 0.121, '588015509806252150', '193034571183292416', ' SDSS J000006.53+003055.2')],
      dtype=(numpy.record, [('SDSSJ', 'S31'), ('RA', '>f8'), ('DEC', '>f8'), ('z', '>f8'), ('UMAG', '>f8'), ('UMAGERR', '>f8'), ('GMAG', '>f8'), ('GMAGERR', '>f8'), ('RMAG', '>f8'), ('RMAGERR', '>f8'), ('IMAG', '>f8'), ('IMAGERR', '>f8'), ('ZMAG', '>f8'), ('ZMAGERR', '>f8'), ('AU', '>f8'), ('logNH', '>f8'), ('FIRSTMAG', '>f8'), ('FIRSTSN', '>f8'), ('FIRSTDEL', '>f8'), ('RASSCNT', '>f8'), ('RASSSN', '>f8'), ('RASSDEL', '>f8'), ('JMAG', '>f8'), ('JMAGERR', '>f8'), ('HMAG', '>f8'), ('HMAGERR', '>f8'), ('KMAG', '>f8'), ('KMAGERR', '>f8'), ('MASSDEL', '>f8'), ('MASSFLG', '>i4'), ('MIMAG', '>f8'), ('DGMI', '>f8'), ('MFLAG', '>i4'), ('SPFLAG', '>i4'), ('MODEFLAG', '>i4'), ('USELFLAG', '>i4'), ('BESTFLAG', '>i4'), ('LZFLAG', '>i4'), ('HZFLAG', '>i4'), ('FTFLAG', '>i4'), ('RTFLAG', '>i4'), ('SRFLAG', '>i4'), ('STFLAG', '>i4'), ('GXFLAG', '>i4'), ('RUN', '>i4'), ('RMJD', '>i4'), ('SMJD', '>i4'), ('PLATE', '>i4'), ('FIBER', '>i4'), ('RERUN', '>i4'), ('CAMCOL', '>i4'), ('FIELD', '>i4'), ('OBJECT', '>i4'), ('TFLAG', '>i4'), ('LZTFLAG', '>i4'), ('HZTFLAG', '>i4'), ('FTTFLAG', '>i4'), ('RTTFLAG', '>i4'), ('SRTFLAG', '>i4'), ('STTFLAG', '>i4'), ('GXTFLAG', '>i4'), ('UTMAG', '>f8'), ('UTMAGERR', '>f8'), ('GTMAG', '>f8'), ('GTMAGERR', '>f8'), ('RTMAG', '>f8'), ('RTMAGERR', '>f8'), ('ITMAG', '>f8'), ('ITMAGERR', '>f8'), ('ZTMAG', '>f8'), ('ZTMAGERR', '>f8'), ('BESTID', 'S31'), ('SPECOID', 'S31'), ('ONAME', 'S31')]))

I don't have the same quasar table as you, but is there any chance the name doesn't start with a J? That seems to be the case for the dr7qso table I have.

Additionally, this

 w = tbdata[tbdata['SDSS_NAME'] == 'J000006.53+003055.2']

had it been the correct comparison, at least, would have given you an array of all the matching rows, assigned to w . So when you ran

print(tbdata[w])

You indexed tbdata using the array assigned to w (either an empty array if there were no matches, or probably some strings if there were matches). Hence the IndexError . All you needed was print(w) to see what w was.

This is straight-forward in IDL. Why's it so hard here?!?!

It really isn't. You're just learning. I feel the same way in the rare cases that I have to do anything in IDL (with which I have null experience) :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM