0

私はPythonが初めてで、これにアプローチする方法がわかりません! FITS ファイルのディレクトリがあり、それぞれから特定のヘッダーを読み取り、テーブルとして出力できるようにしたいと考えています。

私は英語でアルゴリズムを構築する方法を知っており、個々の FITS ファイルからヘッダーを読み取る方法も知っています。ディレクトリ内の束全体からそれを行うには助けが必要です。

  1. 最初lsにすべてのファイルを実行して表示します
  2. どういうわけかfor、私のディレクトリの各ファイルを調べてそれをhdulist
  3. のコマンドを与えるhdulist[0].header['name of the header I want'](プライマリのみを見て)
  4. おそらくASCIIテーブルまたは通常のテーブル/テキストファイルで、それらすべてを印刷してください。
4

2 に答える 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')

インライン参照:

于 2014-02-06T16:07:56.377 に答える
0

おそらく、これはあなたが望むことをします:

# 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()

表として印刷するには:

# 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()

参考文献:

于 2014-02-05T20:16:58.233 に答える