如何直接使用FITS文件中的数据(不将其转换为文本文件)

时间:2017-07-24 12:49:11

标签: python fits pyfits

我有一堆写在FITS文件中的值。每列中有320,000个点...我需要的列是3列,称为detx,dety和energy。 我需要将这些数据用于我的程序。 我知道如何将此FITS文件转换为txt文件(如下所示),然后使用数据,但我如何直接使用数据?例如,我如何从这个FITS文件中获取数据,然后在python中打印出来?

以下是如何将装配文件转换为文本文件的示例:

from astropy.io import fits
#This is what the file is called
hdulist = fits.open('acisf02149N003_evt2.fits.gz')
hdulist.info()
tbdata = hdulist[1].data

#This prints it out into a text file
f=open('ChandraXraysources.txt','w+')

#This chooses only the detx, dety, and energy columns I was talking about
detxvect=tbdata.field('detx')
detyvect=tbdata.field('dety')
detzvect=tbdata.field('energy')

#This lists at the top of the text file the names of each column 
f.write('Chandra X-ray source, RA(x),dec(y),energy  \r\n')

for i in range(len(detxvect)):
    f.write('%e  %e  %e\r\n' %(detxvect[i],detyvect[i],detzvect[i]))

print(detxvect[0]) 

2 个答案:

答案 0 :(得分:0)

要直接使用数据,您可以尝试

import os, glob
from astropy.io import fits, ascii

# Open all files and store them into a list
files = glob.glob(dir + '*.fits')
hdu_lst = [fits.open(dir + fi) for fi in files]

#You choose the columns you'll be using
detx = [hdi[0].header['DETX'] for hdi in hdu_lst]
dety = [hdi[0].header['DETY'] for hdi in hdu_lst]
energy = [hdi[0].header['ENERGY'] for hdi in hdu_lst]

# You can also write a file with all the filenames
ascii.write([files, detx,dety,energy],'table.dat',names=(['Filename','DETX','DETY','ENERGY']),format = 'fixed_width',delimiter = '|',bookend =   True,overwrite = True)

#Close all files at end
[hdi.close for hdi in hdu_lst]

答案 1 :(得分:0)

  

我知道如何将此FITS文件转换为txt文件(如下所示),然后使用数据,但我如何直接使用数据?例如,我如何从这个FITS文件中获取数据,然后在python中打印出来?

关于你究竟在问什么,你的问题非常混乱。具体来说,在您的示例中,您已经说明了如何直接使用表数据:

f.write('%e  %e  %e\r\n' %(detxvect[i],detyvect[i],detzvect[i]))
循环中的

语句说明了这一点。

也就是说,只需使用detxvectdetyvectdetzvect,就像使用任何numpy 1D数组一样。

相关问题