从FITS文件头创建Ascii表

时间:2012-07-16 01:35:06

标签: python ascii astronomy fits

我想学习如何从FITS文件头中获取信息并将该信息传输到ascii表。例如,这就是我获取信息的方式。:

    python
    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                              

如何创建包含RA_OBJ和DEC_OBJ的ASCII表?

编辑:我想创建一个包含标题中两列(RA和DEC)的.dat文件。这是我正在尝试的一个例子:

import asciitable
import asciidata
imort 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

我想为此编写一个包含以下内容的.dat文件:

RA    DEC
###   ###
...   ...
...   ...
...   ...

相反,我的代码只是写了以前文件的键。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我认为你可能会更仔细地阅读the pyfits documentation。 header属性是一个pyfits.header.Header对象,它是一个类似字典的对象。所以你可以这样做:

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]

您可以添加更多花哨的输出,将结果字符串放入变量,检查缺失的键等等。

修改

以下是asciitablenumpy

的结合方式
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)