I'd like to learn how to grab information from a FITS file header and transfer that information to an ascii table. For example, this is how I would obtain the info.:
import pyfits
a = pyfits.open('data.fits')
header = a[0].header # Which should return something like this (It is BinHDUlist)
SIMPLE = T / conforms to FITS standards
/ institution responsible for creating this file
TELESCOP= 'Kepler ' / telescope
INSTRUME= 'Kepler Photometer' / detector type
OBJECT = 'KIC 8631743' / string version of KEPLERID
RA_OBJ = 294.466516 / [deg] right ascension
DEC_OBJ = 44.751131 / [deg] declination
How could I create an ASCII table that contains RA_OBJ and DEC_OBJ ?
EDIT: I'd like to create a .dat file that contains two columns (RA and DEC) from the headers. Here is an example of what I am trying:
import asciitable
import asciidata
import pyfits
import numpy as np
# Here I have taken all the fits files in my current directory and did the following:
# ls > z.txt so that all the fits files are in one place.
a = asciidata.open('z.txt')
i = 0 #There are 371 fits files in z.txt
while i<=370:
b = pyfits.open(a[0][i])
h = b[0].header
RA = np.array([h['RA_OBJ']])
DEC = np.array(h['DEC_OBJ']])
asciitable.write({'RA': RA, 'DEC': DEC}, 'coordinates.dat', names=['RA', 'DEC'])
i = i+1
I'd like for this to write a .dat file containing something like this:
RA DEC
### ###
... ...
... ...
... ...
Instead, my code just writes over the keys of the previous files. Any ideas?
I think you might benefit from reading the pyfits documentation more carefully. The header attribute is a pyfits.header.Header
object, which is a dictionary-like object. So you can do something like:
import pyfits
keys = ['SIMPLE', 'TELESCOP', 'INSTRUME', 'OBJECTS', 'RA_OBJ', 'DEV_OBJ']
hdulist = pyfits.open("data.fits")
header = hdulist[0].header
for k in keys:
print k, "=", header[k]
You can add more fancy outputting, put the resulting string in variable, checking for missing keys, etc.
EDIT:
Here's how this could be coupled with asciitable
and numpy
:
import asciitable
import numpy as np
keys = ['RA', 'DEC']
data = {}
# Initialize "data" with empty lists for each key
for k in keys:
data[k] = []
# Collect all data in the "data" dictionary
for i in range(0, 50):
data['RA'].append(np.array(i))
data['DEC'].append(np.array(i+1))
asciitable.write(data, "coords.dat", names=keys)