Copying header in astropy from one FITS file to a newly created FITS file

2k views Asked by At

I have a program that processes an existing FITS file (xbulge-w1.fits), and saves the processed image to a new FITS (w1_resampled.fits). I want to copy the header from the original to the new, so that they're in the same coordinates (i.e. galactic).
I'm trying to do it with the following code:

#   Open the FITS files as input image and mask
#   Process the images
#   Rescale image to galactic coordinates and display
#   Plot and save median filtered images as png, rescaled to galactic coordinates
#   Save as FITS files and close
#   Edit FITS headers to recenter images at galactic center
header = fits.getdata('xbulge-w1.fits', header=True)

header['COMMENT'] = 'Resampled with median filtered pixels'
header['IMAGEW'] = 877
header['IMAGEH'] = 901
header['WCSAXES'] = (2, 'Number of coordinate axes')
header['CRPIX1'] = 438.5
header['CRPIX2'] = 450.5                                                  
header['PC1_1'] = (-0.0333333333333, 'Coordinate transformation matrix element')
header['PC2_2'] = (0.0333333333333, 'Coordinate transformation matrix element')
header['CDELT1'] = (1., '[deg] Coordinate increment at reference point')
header['CDELT2'] = (1., '[deg] Coordinate increment at reference point')
header['CUNIT1'] = ('deg     ', 'Units of coordinate increment and value')
header['CUNIT2'] = ('deg     ', 'Units of coordinate increment and value')
header['CTYPE1'] = 'GLON-AIT'                
header['CTYPE2'] = 'GLAT-AIT'                                                            
header['CRVAL1'] = (0., '[deg] Coordinate value at reference point')
header['CRVAL2'] = (0., '[deg] Coordinate value at reference point')
header['LONPOLE'] = (0., '[deg] Native longitude of celestial pole')
header['LATPOLE'] = (90., '[deg] Native latitude of celestial pole')
header['RADESYS'] = ('ICRS    ', 'Equatorial coordinate system')

fits.writeto('w1_resampled.fits', header, overwrite=True)
fits.writeto('w2_resampled.fits', header, overwrite=True)

hdulist.close()
hdulist2.close()
hdulist3.close()

The first 5 comments list the working functions in the program, it's just the header that I'm having difficulty with. When I open the w1_resampled.fits in DS9, the header contains

SIMPLE  =                    T / conforms to FITS standard                      
BITPIX  =                  -32 / array data type                                
NAXIS   =                    2 / number of array dimensions                     
NAXIS1  =                  877                                                  
NAXIS2  =                  901                                                  
EXTEND  =                    T                                                  
END  

instead of the data copied from xbulge-w1.fits header.
How can I copy the data from one header to the other?

1

There are 1 answers

0
Jim421616 On BEST ANSWER

Oh, I see. I was overcomplicating it. Here's all I needed to do:

#   Edit FITS headers to recenter images at galactic center
w1_resampled_header = w1header
w2_resampled_header = w2header

w1_resampled_header['CRPIX1'] = w1header['CRPIX1']
w1_resampled_header['CRPIX2'] = w1header['CRPIX2']
w2_resampled_header['CRPIX1'] = w2header['CRPIX1']
w2_resampled_header['CRPIX2'] = w2header['CRPIX2']                                                

#   Save as FITS files and close
fits.writeto('w1_resampled.fits',
             w1_resampled,
             w1_resampled_header,
             overwrite = True)
fits.writeto('w2_resampled.fits',
             w2_resampled,
             w2_resampled_header,
             overwrite = True)