from astropy import wcs
import numpy as np
from astropy.nddata import Cutout2D
f = fits.open(in_file) #read in original FITS image with the input image path in in_file
scidata = f[0].data
header = f[0].header
w = wcs.WCS(f[0].header)
position = (header['CRPIX1'], header['CRPIX2']) #define the center pixel of the stamp
size = (2133,2133) #define the dimensions of the stamp (which is ~15% of the image dimensions)
cutout = Cutout2D(scidata, position, size, wcs=w)
header_new = cutout.wcs.to_header()
hdu = fits.PrimaryHDU(cutout.data,header=header_new) #create new hdu
hdulist = fits.HDUList([hdu]) #create new hdulist
hdulist.writeto(out_file)
Below is a screen grab of the ds9 display with the original image on the right with its wcs matched to stamp image's wcs (on the left).
Below are the header files for the two images as displayed by ds9 (the original image's header is first):
Can anyone help me identify what's wrong here?