Question

I have a table of data in an ascii file with header and I would like to convert my ascii table to a fits file with header

#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 

any idea how I could manage to do it with python? cheers.

Was it helpful?

Solution

I have found a way to solve my own problem: read the file as an array

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

Devote each column to a header name

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])

Write the header and columns as a fits file:

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

It worked!!

OTHER TIPS

Even though the question is about how to do it via Python, a much easier way to go about is using Topcat (Tool for OPerations on Catalogues And Tables).

The website for this software can be found in THIS LINK HERE

Topcat allows you to load almost all formats (ASCII, FITS, FITS+, CSV etc). Once your table has been loaded, in this case an ASCII table, you can save it as a FITS file, the same way one saves a .pptx document to a .pdf document! Once saved as a FITS file, the FITS file can be opened and the headers to the columns can be entered manually!

This is really fast and useful and immediately on can also plot the columns without having to write any code.

Converting an ASCII file to a FITS file can be done in a much easier and less time consuming way. One can make use of this:

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

The FITS file will be created in the current working directory if absolute path is not provided.

Required Installations: (Astropy Library)

pip install --no-deps astropy

You can view the other basic requirements in the link below. eg., numpy. They download alongside the above statement, if not present. So no worries.

Link to Astropy website: http://docs.astropy.org/en/stable/install.html

After you have created the FITS file, it can be opened as follows:

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

In order to manipulate and work with the FITS file, you can try using Astropy. Written originally for the main purpose of Astronomy related datasets, where FITS files are very commonly used, it's a very useful library when it comes to other applications as well.

Documentation: http://docs.astropy.org/en/stable/index.html

From Input to Output:

Input ASCII file:

col1 col2 col3
1234 2345 3456

Output FITS file:

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

Hope this helps!

necro post but since a friend of mine has recently asked me the same question and couldn't get your solution to work I thought I'd add a simple function that wrote since I do this very regularly.

It has the flexibility of allowing for a custom header to be used, and retains the same column names provided in the ascii table (without having to enter them explicitly).

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])

usage would be

from astropy.io import ascii
myasciitable = asii.read("/path/to/input.file")
myfitstable = table2fits(myasciitable)
myfitstable.writeto("/path/to/output.fits", clobber=True)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top