4

我在带有标题的 ascii 文件中有一个数据表,我想将我的 ascii 表转换为带有标题的适合文件

#ID ra  dec x   y   Umag    Bmag    Vmag    Rmag    Imag    
1.0 53.146  -27.8123    3422.98 3823.58 24.4528 24.7995 23.6266 22.64   20.8437 
2.0 53.1064 -27.801         3953.49 3994.62 23.3284 22.6716 22.1762 21.6189 21.2141 
3.0 53.1322 -27.7829    3608.34 4269.29 21.2676 20.1937 19.6743 19.0707 18.6983 
4.0 53.1017 -27.8022    4017.09 3975.24 23.6987 22.84   21.9946 21.0781 19.8616 
5.0 53.118  -27.8021    3798.98 3978.42 23.3087 22.1932 21.2205 20.1842 18.6448     
6.0 53.1479 -27.8239    3397.92 3648.27 25.0347 24.598  23.7259 22.9945 21.9228     
7.0 53.1334 -27.7758    3592.51 4375.76 21.5159 20.4777 19.6065 18.6609 17.188      
8.0 53.1048 -27.8259    3974.47 3617.5  22.3266 22.3517 22.0677 21.7664 21.6781     
9.0 53.1249 -27.8109    3706.47 3843.89 24.0539 23.3009 22.4001 21.4732 20.1244     
10.0 53.1207 -27.7822   3763.3  4278.76 24.417  23.7635 22.9405 22.1379 21.5564     
11.0 53.0886 -27.7611   4193.25 4596.77 22.012  22.3081 22.125  21.9488 21.9071     
12.0 53.1272 -27.7498   3676.7  4768.82 19.3631 19.7458 19.5979 19.4438 19.4002 

知道我怎么能用python做到这一点吗?干杯。

4

4 回答 4

7

我找到了解决自己问题的方法:将文件作为数组读取

import pyfits
from scipy.io import *
M=read_array(filename)

将每一列用于标题名称

c1=pyfits.Column(name='ID', format='E', array=M[:,0])
c2=pyfits.Column(name='RA', format='E', array=M[:,1])
c3=pyfits.Column(name='DEC', format='E', array=M[:,2])
c4=pyfits.Column(name='X', format='E', array=M[:,3])
c5=pyfits.Column(name='Y', format='E', array=M[:,4])
c6=pyfits.Column(name='Umag', format='E', array=M[:,5])   
c7=pyfits.Column(name='Bmag', format='E', array=M[:,6])      
c8=pyfits.Column(name='Vmag', format='E', array=M[:,7])      
c9=pyfits.Column(name='Rmag', format='E', array=M[:,8])      
c10=pyfits.Column(name='Imag', format='E', array=M[:,9])      
cols = pyfits.ColDefs([c1, c2, c3, c4, c5, c6, c7, c8, c9, c10])

将标题和列写入适合文件:

tbhdu = pyfits.new_table(cols)
hdu = pyfits.PrimaryHDU(data=M)
thdulist = pyfits.HDUList([hdu,tbhdu])
thdulist.writeto(outfilename)
thdulist.close()

有效!!

于 2013-10-30T23:11:57.670 回答
1

尽管问题是关于如何通过 Python 来完成,但更简单的方法是使用 Topcat(目录和表上的操作工具)。

该软件的网站可以在此链接中找到

Topcat 允许您加载几乎所有格式(ASCII、FITS、FITS+、CSV 等)。加载表格后,在本例中为 ASCII 表格,您可以将其保存为 FITS 文件,就像将 .pptx 文档保存为 .pdf 文档一样!保存为 FITS 文件后,可以打开 FITS 文件并手动输入列标题!

这真的非常快速和有用,并且可以立即绘制列,而无需编写任何代码。

于 2014-09-08T11:21:02.613 回答
1

将 ASCII 文件转换为 FITS 文件可以更简单、更省时的方式完成。可以利用这一点:

from astropy.io import ascii
test_input = '../Data/test.txt'
text_file = ascii.read(test_input)
text_file.write('text_to_fits.fits')

如果未提供绝对路径,将在当前工作目录中创建 FITS 文件。

所需安装:(Astropy 库)

pip install --no-deps astropy

您可以在下面的链接中查看其他基本要求。例如,numpy。如果不存在,它们会与上述声明一起下载。所以不用担心。

Astropy 网站链接:http ://docs.astropy.org/en/stable/install.html

创建 FITS 文件后,可以按如下方式打开它:

import astropy.table as at
fits_file = '../Data/text_to_fits.fits'
table = at.Table.read(fits_file)

为了操作和使用 FITS 文件,您可以尝试使用 Astropy。最初是为天文学相关数据集的主要目的而编写的,其中 FITS 文件非常常用,当涉及到其他应用程序时,它也是一个非常有用的库。

文档:http ://docs.astropy.org/en/stable/index.html

从输入到输出:

输入 ASCII 文件:

col1 col2 col3
1234 2345 3456

输出 FITS 文件:

Out[10]: 
<Table length=1>
 col1  col2  col3
int64 int64 int64
----- ----- -----
    1     2     3

希望这可以帮助!

于 2017-08-24T15:07:50.677 回答
0

necro post 但由于我的一个朋友最近问了我同样的问题并且无法让您的解决方案起作用,我想我会添加一个简单的函数,因为我经常这样做。

它具有允许使用自定义标题的灵活性,并保留 ascii 表中提供的相同列名(无需显式输入)。

def table2fits(tab, header=None):
    ''' 
        takes the data from an ascii.table.Table and outputs 
        a pyfits.hdu.table.BinTableHDU, creating a blank
        header if no header is provided
    '''
    from astropy.io import fits
    if header is None:
        prihdr = fits.Header()
        prihdu = fits.PrimaryHDU(header=prihdr)
    else:
        prihdu = fits.PrimaryHDU(header=header)

    table_hdu = fits.BinTableHDU.from_columns(np.array(tab.filled()))

return fits.HDUList([prihdu, table_hdu])

用法将是

from astropy.io import ascii
myasciitable = asii.read("/path/to/input.file")
myfitstable = table2fits(myasciitable)
myfitstable.writeto("/path/to/output.fits", clobber=True)
于 2017-04-03T05:07:12.210 回答