0

I'm very new to Python and I have no idea how to approach this! I have a directory of FITS files, I want to be able to read a specific header from each of them and print them as a table.

I know how to construct the algorithm in English, and I also know how to read headers from individual FITS files, I just need help with doing it from a whole bunch in a directory.

  1. First run ls and view all the files
  2. Somehow construct a for loop telling python to go through each file of my directory and make it into a hdulist
  3. Give the command of hdulist[0].header['name of the header I want'] (looking at primary only)
  4. Print all of them, possibly in an ASCII table or just a regular table/text file is fine.
3
  • The parts you don't understand are how to loop over the contents of a directory and how to pretty print the headers in a table?
    – jerry
    Feb 5, 2014 at 17:31
  • The part I don't understand are how to do a for loop for getting header information for say 2 or 3 files at once....This is what I did to view everything: rootpath = '/Users/Arya/cbcd/' pattern = '*.fits' for root, dirs, files in os.walk(rootpath): for filename in fnmatch.filter(files, pattern): print(os.path.join(root, filename))
    – Arya
    Feb 5, 2014 at 20:08
  • What do you hope the output will look like? Please edit your question to include the expected output.
    – Robᵩ
    Feb 5, 2014 at 20:19

2 Answers 2

1
# yes, glob is your friend.
import glob
import os

# astropy is really your astro-friend.
# http://docs.astropy.org/en/stable/index.html
from astropy.io import fits as pyfits
from astropy.table import Table, Column

# where is your data?
dir = "./"

# pick the header keys you want to dump to a table.
keys = ['NAXIS', 'RA', 'DEC', 'FILTER']
# pick the HDU you want to pull them from. It might be that your data are spectra, or FITS tables, or multi-extension "mosaics". 
hdu = 0

# get header keyword values
# http://docs.astropy.org/en/stable/io/fits/index.html#working-with-a-fits-header
values = []
fitsNames = []
for fitsName in glob.glob(dir+'*.fits'):
    # opening the file is unnecessary. just pull the (right) header
    header = pyfits.getheader(fitsName, hdu)
    values.append([header.get(key) for key in keys])
    fitsNames.append(fitsName)
    # if you want the fits file name only without the full path then
    # fitsNames.append(os.path.split(fitsName)[1])

# Create a table container. 
# http://docs.astropy.org/en/stable/table/construct_table.html
# One trick is to use the data types in the first "values" to let astropy guess datatypes.
# to use this trick, you need to specify the column names in the table
row0 = [dict(zip(keys, values[0]))]
t = Table(row0, names=keys)

# now add all the other rows. again, because dict didn't preserve column order, you have to repeat
# the dict here.
for i in range(1, len(values)):
    t.add_row(values[i])

# add the filenames column
#t.add_column
new_column = Column(name='fitsName', data=fitsNames)
t.add_column(new_column, 0)

# save the file
# http://docs.astropy.org/en/stable/table/io.html
t.write('table.dat', format='ascii.ipac')

inline refs:

2
  • Thanks so much for helping me out! There is a problem however, I'm not able to create a table with the dict command. I get a value error stating the following: ValueError: Arguments "names" and "dtypes" must match number of columns [astropy.table.table] and I'm not sure how to go about fixing it! Thank you again!
    – Arya
    Feb 10, 2014 at 22:04
  • so the "row0" command works, but the t=Table(row0, names=keys) command throws that error? i think i know what is wrong, but could you list which keys you are pulling from the header, @user3276219?
    – Gus
    Feb 11, 2014 at 2:17
0

Perhaps this does what you want:

# UNTESTED
import glob
import pyfits

for fitsName in glob.glob('*.fits'):
    hdulist = pyfits.open(fitsName)
    print hdulist[0].header['name of the header I want']
    hdulist.close()

To print it as a table:

# UNTESTED
import glob
import pyfits

print "# FileName, Header"  
for fitsName in glob.glob('*.fits'):
    hdulist = pyfits.open(fitsName)
    print fitsName, hdulist[0].header['name of the header I want']
    hdulist.close()

References:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.