pythonmatlabfitspyfits

Converting ASCII Table to FITS image


I am a beginner in this domain. I have a text file having three columns: X, Y, Intensity at (X, Y). They are basically arrays (1X10000) each written out in a text files via python. To plot the dataset in python, I can simply use trisurf to achieve this. But for further processing, I need to create a fits image from it. How do I make FITS image (and NOT a simple FITS table) out of the this text file (through python or matlab will be preferable).


Solution

  • You should be able to do this mostly with Astropy. The details are a little vague, but you should be able to read the text file into an Astropy table like:

    >>> from astropy.table import Table
    >>> table = Table.read('/path/to/text/file.txt', format='ascii')
    

    where the options you end up passing to Table might depend heavily on exactly how the table is formatted.

    Then you need to convert the columns to a Numpy array. Your problem statement is a bit vague, but if the coordinates in your table are just pixel coordinates you should be able to do something like:

    >>> import numpy as np
    >>> img = np.zeros((len(table), len(table))
    >>> for x, y, intensity in table:
    ...     img[x, y] = intensity
    

    (I have to wonder if Numpy has a slicker way of doing this but not that I know of.)

    Then to save the image to a FITS file:

    >>> from astropy.io import fits
    >>> fits.writeto('filename.fits', img)
    

    That's the very high level process. The details depend a lot on your data.