pythonnumpyscipypyfits

How is it possible in python to convert an ascii table to a fits table?


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.


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!!