pythonastronomyfitspyfits

Collapsing / Flattening a FITS data cube in python


I've looked all over the place and am not finding a solution to this issue. I feel like it should be fairly straightforward, but we'll see.

I have a .FITS format data cube and I need to collapse it into a 2D FITS image. The data cube has two spacial dimensions and one spectral/velocity dimension.

Just looking for a simple python routine to load in the cube and flatten all these layers (i.e. integrate them along the spectral/velocity axis). Thanks for any help.


Solution

  • OK, this seems to work:

    import pyfits
    import numpy as np
    
    hdulist = pyfits.open(filename)  
    header = hdulist[0].header  
    data = hdulist[0].data   
    data = np.nan_to_num(data)   
    new_data = data[0]
    
    for i in range(1,84):                #this depends on number of layers or pages
        new_data += data[i]
    
    hdu = pyfits.PrimaryHDU(new_data)
    hdu.writeto(new_filename)
    

    One problem with this routine is that WCS coordinates (which are attached to the original data cube) are lost during this conversion.