pythonastropypyfits

Save 2 fits files with 3 headers astropy or pyfits


I am trying to make a fits file with two images inside of it, and i want to give them both a header. There should also be a basic header for the both of them. What I have found until now is this, but I don't know how to add the headers properly. Variables are headermain, header1, header2, data1, data2. I want it to have this structure.

Filename: test.fits
No.    Name         Type      Cards   Dimensions   Format
0    PRIMARY     PrimaryHDU     828   ()              
1    name1  ImageHDU        52   (1024, 900)   float32   
2    name2  ImageHDU        52   (1024, 900)   float32 

I currently have this, but I can't find how to add headers.

new_hdul = pyfits.HDUList()
new_hdul.append(pyfits.ImageHDU(data1)
new_hdul.append(pyfits.ImageHDU(data2)

new_hdul.writeto('test.fits', clobber=True)

Solution

  • From the astropy documentation on ImageHDU you can pass the header as keyword to the ImageHDU:

    from astropy.io import fits
    
    new_hdul = fits.HDUList()
    new_hdul.append(fits.PrimaryHDU(header=headermain))
    new_hdul.append(fits.ImageHDU(data1, header=header1, name='name1'))
    new_hdul.append(fits.ImageHDU(data2, header=header2, name='name2'))
    
    new_hdul.writeto('test.fits', clobber=True)