-1

I think I screwed something up when selecting data from a fits file...Basically what I did was:

File='/SomePath/xxx.fits'
hdulist=fits.open(File)
tbdata=hdulist[1].data

and applied a selection rule:

for i in range (0, len(tbdata)):
     if tbdata[i]['z']<0.2:
         A.append(tbdata[i])

Is there any way to recombine the data in A into a new fits file? I tried manipulations using Pandas but can't find the right method...

Gabriel
  • 40,504
  • 73
  • 230
  • 404
Leo
  • 53
  • 1
  • 5
  • You may like to have a look at my short Numpy tutorial: https://github.com/embray/notebooks/blob/master/numpy.ipynb In most cases, especially simple filtering like this, you're doing something wrong if you use `for ... in range` with a Numpy array. Once you've filtered your array saving it to a new FITS file is a separate issue. `astropy.io.fits` returns Numpy arrays which are a standard data structure in Python (with Numpy). FITS just comes in when reading files from or writing them to FITS files. – Iguananaut Dec 08 '15 at 14:43

1 Answers1

4

Probably something like this:

from astropy.table import Table
tbdata = Table.read('file.fits')
ok = tbdata['z'] < 0.2  # boolean selection mask
new_tbdata = tbdata[ok]
new_tbdata.write('new_file.fits')
Tom Aldcroft
  • 2,339
  • 1
  • 14
  • 16
  • Yes this method works, but later on the task involves some complicated if statements which I don't think a selection mask works...but I will work on it! Thanks a lot! – Leo Dec 07 '15 at 22:59