python - 从 FITS 文件头创建 Ascii 表

标签 python ascii astronomy fits

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

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 表?

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

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

我希望编写一个包含以下内容的 .dat 文件:

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

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

最佳答案

我认为阅读 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)

关于python - 从 FITS 文件头创建 Ascii 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11497158/

相关文章:

python - 未绑定(bind)本地错误: local variable 'b' referenced before assignment

python - NumPy 数组中不需要的额外维度

c# - 在 C# 中将字符串存储为 UTF8

python - 如何使用 PyEphem 计算正确的行星经度和星座

python - 转换日期 (YYYY MM DD HH :MM:SS) to Decimal Day (YYYY MM DD. ddddd)

python - 如何使用ansible正确升级pip?

linux - 如何在 Linux 中从文本文件中区分二进制文件

c - toascii 有什么作用?

python - 根据条件替换所有列中的所有值