def hrot2(img, hdr, angle, xc=None, yc=None, pivot=False): """ Rotate an image and updates the FITS header with updated astrometry. param img: image as a ndarray param hdr: PyFITS header instance or None param xc: X Center of rotation (None for center of image) param yc: Y Center of rotation (None for center of image) param angle: rotation angle, in degrees :return: rotated_image, rotated_header """ ysize, xsize = img.shape xc_new = (xsize - 1) / 2. yc_new = (ysize - 1) / 2. shape = img.shape if xc is None: xc = shape[1] / 2.0 if yc is None: yc = shape[0] / 2.0 #do the actual rotation A = transform.makeCenteredRotation(angle, (xc, yc)) imgrot = transform.Image(img, A) #update astrometry theta = np.deg2rad(angle) rot_mat = np.matrix([[np.cos(theta), np.sin(theta)], [-np.sin(theta), np.cos(theta)]]) if hdr is None: return imgrot, None else: #WCS info wcs = pywcs.WCS(hdr) crpix = wcs.wcs.crpix cd = wcs.wcs.cd ncrpix = (rot_mat.T * (crpix - 1 - np.matrix([xc, yc])).T + 1).T if pivot: ncrpix = np.array([xc, yc]) + ncrpix else: ncrpix = np.array([xc_new, yc_new]) + ncrpix hdr['CRPIX1'] = ncrpix[0, 0] hdr['CRPIX2'] = ncrpix[0, 1] newcd = np.asmatrix(cd) * rot_mat hdr['CD1_1'] = newcd[0, 0] hdr['CD1_2'] = newcd[0, 1] hdr['CD2_1'] = newcd[1, 0] hdr['CD2_2'] = newcd[1, 1] #crota = np.rad2deg(np.arctan(newcd[1,0], newcd[1,1])) #hdr['CROTA1'] = crota #hdr['CROTA2'] = crota return imgrot, hdr
def hrot(imagefile, angle, xc=None, yc=None, ext=0, output='rotated.fits', pivot=False): """ Rotate an image and create new FITS header with updated astrometry. param imagefile: name of the FITS file to be rotated param angle: rotation angle, in degrees param xc: X Center of rotation (None for center of image) param yc: Y Center of rotation (None for center of image) :return: rotated_image, rotated_header """ #read in the file fh = pf.open(imagefile) hdr = fh[ext].header img = fh[ext].data fh.close() wcs = pywcs.WCS(hdr) ysize, xsize = img.shape xc_new = (xsize - 1) / 2. yc_new = (ysize - 1) / 2. shape = img.shape if xc is None: xc = shape[1] / 2.0 if yc is None: yc = shape[0] / 2.0 #do the actual rotation A = transform.makeCenteredRotation(angle, (xc, yc)) imgrot = transform.Image(img, A) #update astrometry theta = np.deg2rad(angle) rot_mat = np.matrix([[np.cos(theta), np.sin(theta)], [-np.sin(theta), np.cos(theta)]]) #WCS info crpix = wcs.wcs.crpix cd = wcs.wcs.cd ncrpix = (rot_mat.T * (crpix - 1 - np.matrix([xc, yc])).T + 1).T if pivot: ncrpix = np.array([xc, yc]) + ncrpix else: ncrpix = np.array([xc_new, yc_new]) + ncrpix hdr['CRPIX1'] = ncrpix[0, 0] hdr['CRPIX2'] = ncrpix[0, 1] newcd = np.asmatrix(cd) * rot_mat hdr['CD1_1'] = newcd[0, 0] hdr['CD1_2'] = newcd[0, 1] hdr['CD2_1'] = newcd[1, 0] hdr['CD2_2'] = newcd[1, 1] #crota = np.rad2deg(np.arctan(newcd[1,0], newcd[1,1])) #hdr['CROTA1'] = crota #hdr['CROTA2'] = crota #write out a new FITS file hdu = pf.PrimaryHDU(imgrot) hdu.header = hdr hdu.header.add_history( 'Updated: %s' % datetime.datetime.isoformat(datetime.datetime.now())) if os.path.isfile(output): os.remove(output) hdu.writeto(output) return imgrot, hdr
def hrot(imagefile, angle, xc=None, yc=None, ext=0, output='rotated.fits', pivot=False): """ Rotate an image and create new FITS header with updated astrometry. param imagefile: name of the FITS file to be rotated param angle: rotation angle, in degrees param xc: X Center of rotation (None for center of image) param yc: Y Center of rotation (None for center of image) :return: rotated_image, rotated_header """ #read in the file fh = pf.open(imagefile) hdr = fh[ext].header img = fh[ext].data fh.close() wcs = pywcs.WCS(hdr) ysize, xsize = img.shape xc_new = (xsize - 1) / 2. yc_new = (ysize - 1) / 2. shape = img.shape if xc is None: xc = shape[1] / 2.0 if yc is None: yc = shape[0] / 2.0 #do the actual rotation A = transform.makeCenteredRotation(angle, (xc, yc)) imgrot = transform.Image(img, A) #update astrometry theta = np.deg2rad(angle) rot_mat = np.matrix([[np.cos(theta), np.sin(theta)], [-np.sin(theta), np.cos(theta)]]) #WCS info crpix = wcs.wcs.crpix cd = wcs.wcs.cd ncrpix = (rot_mat.T * (crpix - 1 - np.matrix([xc, yc])).T + 1).T if pivot: ncrpix = np.array([xc, yc]) + ncrpix else: ncrpix = np.array([xc_new, yc_new]) + ncrpix hdr['CRPIX1'] = ncrpix[0, 0] hdr['CRPIX2'] = ncrpix[0, 1] newcd = np.asmatrix(cd) * rot_mat hdr['CD1_1'] = newcd[0, 0] hdr['CD1_2'] = newcd[0, 1] hdr['CD2_1'] = newcd[1, 0] hdr['CD2_2'] = newcd[1, 1] #crota = np.rad2deg(np.arctan(newcd[1,0], newcd[1,1])) #hdr['CROTA1'] = crota #hdr['CROTA2'] = crota #write out a new FITS file hdu = pf.PrimaryHDU(imgrot) hdu.header = hdr hdu.header.add_history('Updated: %s' % datetime.datetime.isoformat(datetime.datetime.now())) if os.path.isfile(output): os.remove(output) hdu.writeto(output) return imgrot, hdr