def sharpen_iracpsf(psfname, mag, interpolant='sinc'): """ Make PSF sharper by shrinking pixels! mag: pixel ratio (mag > 1 means shrinking pixels and make PSF sharper) """ newpsf = os.path.splitext(psfname)[0] + '_mag%.2f.fits' % mag if os.path.exists(newpsf): os.remove(newpsf) oldpsf_array = pyfits.getdata(psfname) ncols, nrows = oldpsf_array.shape iraf.imlintran(input=psfname, output=newpsf, xrotation=0., yrotation=0., xmag=mag, ymag=mag, ncols=ncols, nlines=nrows, interpolant=interpolant)
def oversample_all(self, factor=10., interpolant='sinc'): """ For each star, perform centroid finding and over-sample the images by a factor supplied in the method call. """ self.factor = factor self.interpolant = interpolant f = open('psflist', 'wb') for i in range(len(self.c)): imgname = 'obj%d_%s.fits' % (self.c.number[i], self.band) starimg = pyfits.getdata(imgname) pm = image_moments.moments(starimg) pm.firstorder() mag = 1. / factor # for use in iraf.imlintran # Now over-sample the image shape0 = starimg.shape shape1 = (np.array(shape0) * factor).astype('int') imgname_oversample = os.path.splitext( imgname)[0] + '_mag%.2f.fits' % (mag) # xin = self.x_image_list[i] - self.xmin_list[i] # yin = self.y_image_list[i] - self.ymin_list[i] xin = pm.y1 yin = pm.x1 iraf.imlintran( input=imgname, output=imgname_oversample, xrotation=0., yrotation=0., xmag=mag, ymag=mag, xin=xin, yin=yin, # xin=pm.x1+1, yin=pm.y1+1, # xout=shape1[0]/2., yout=shape1[1]/2., ncols=shape1[1], nlines=shape1[0], interpolant=interpolant) f.write('%s\n' % imgname_oversample) f.close()
def plotWithHST(rotateFITS=True): # Load up the OSIRIS image cubeFile = datadir + cuberoot + '_img.fits' cube, cubehdr = pyfits.getdata(cubeFile, header=True) paCube = cubehdr['PA_SPEC'] scaleCube = 0.05 # Load up the NIRC2 image kFile = '/u/jlu/data/m31/05jul/combo/m31_05jul_kp.fits' # kFile = '/u/jlu/data/m31/09sep/combo/mag09sep_m31_kp.fits' k, khdr = pyfits.getdata(kFile, header=True) m31K = np.array([751.986, 716.989]) paK = 0.0 scaleK = 0.00995 f330Root = '/u/jlu/data/m31/hst/m31_acshrc_f330w_j9am01030' f435Root = '/u/jlu/data/m31/hst/m31_acshrc_f435w_j9am01010' f555Root = '/u/jlu/data/m31/hst/m31_wfpc2pc1_f555w_u2lg020at' f814Root = '/u/jlu/data/m31/hst/m31_wfpc2pc1_f814w_u2lg020bt' ########## # Rotate all the HST images to PA=0, scale=NIRC2 ########## if rotateFITS == True: # Load up the HST ACS image (330 nm) f330File = f330Root + '.fits' f330FileRot = f330Root + '_rot.fits' f330, f330hdr = pyfits.getdata(f330File, 1, header=True) m31F330 = np.array([547.0, 623.2]) paF330 = f330hdr['ORIENTAT'] scaleF330 = 0.025 # Load up the HST ACS image (435 nm) f435File = f435Root + '.fits' f435FileRot = f435Root + '_rot.fits' f435, f435hdr = pyfits.getdata(f435File, 1, header=True) m31F435 = np.array([546.9, 623.5]) paF435 = f435hdr['ORIENTAT'] scaleF435 = 0.025 # Load up the HST ACS image (555 nm) f555File = f555Root + '.fits' f555FileRot = f555Root + '_rot.fits' f555, f555hdr = pyfits.getdata(f555File, 1, header=True) m31F555 = np.array([973.0, 961.0]) paF555 = f555hdr['ORIENTAT'] scaleF555 = 0.1 # Load up the HST ACS image (814 nm) f814File = f814Root + '.fits' f814FileRot = f814Root + '_rot.fits' f814, f814hdr = pyfits.getdata(f814File, 1, header=True) m31F814 = np.array([975.0, 962.0]) paF814 = f814hdr['ORIENTAT'] scaleF814 = 0.1 print('scaleK = ', scaleK) print('scaleF330 = ', scaleF330) print('scaleF435 = ', scaleF435) print('scaleF555 = ', scaleF555) print('scaleF814 = ', scaleF814) print('paK = ', paK) print('paF330 = ', paF330) print('paF435 = ', paF435) print('paF555 = ', paF555) print('paF814 = ', paF814) gcutil.rmall([f330FileRot, f435FileRot, f555FileRot, f814FileRot]) from pyraf import iraf as ir ir.unlearn('imlintran') ir.imlintran.boundary = 'constant' ir.imlintran.constant = 0 ir.imlintran.interpolant = 'spline3' ir.imlintran.fluxconserve = 'yes' # 330 ir.imlintran.xin = m31F330[0] ir.imlintran.yin = m31F330[1] ir.imlintran.xout = m31K[0] ir.imlintran.yout = m31K[1] ir.imlintran.ncols = k.shape[1] ir.imlintran.nlines = k.shape[0] ir.imlintran(f330File + '[1]', f330FileRot, paF330, paF330, scaleK / scaleF330, scaleK / scaleF330) # 435 ir.imlintran.xin = m31F435[0] ir.imlintran.yin = m31F435[1] ir.imlintran.xout = m31K[0] ir.imlintran.yout = m31K[1] ir.imlintran.ncols = k.shape[1] ir.imlintran.nlines = k.shape[0] ir.imlintran(f435File + '[1]', f435FileRot, paF435, paF435, scaleK / scaleF435, scaleK / scaleF435) # 555 ir.imlintran.xin = m31F555[0] ir.imlintran.yin = m31F555[1] ir.imlintran.xout = m31K[0] ir.imlintran.yout = m31K[1] ir.imlintran.ncols = k.shape[1] ir.imlintran.nlines = k.shape[0] ir.imlintran(f555File + '[1]', f555FileRot, paF555, paF555, scaleK / scaleF555, scaleK / scaleF555) # 814 ir.imlintran.xin = m31F814[0] ir.imlintran.yin = m31F814[1] ir.imlintran.xout = m31K[0] ir.imlintran.yout = m31K[1] ir.imlintran.ncols = k.shape[1] ir.imlintran.nlines = k.shape[0] ir.imlintran(f814File + '[1]', f814FileRot, paF814, paF814, scaleK / scaleF814, scaleK / scaleF814) f330 = pyfits.getdata(f330Root + '_rot.fits') f435 = pyfits.getdata(f435Root + '_rot.fits') img = np.zeros((k.shape[0], k.shape[1], 3), dtype=float) img[:, :, 0] = img_scale.linear(k, scale_min=300, scale_max=2400) img[:, :, 1] = img_scale.linear(f435, scale_min=0.45, scale_max=1.8) img[:, :, 2] = img_scale.linear(f330, scale_min=0, scale_max=0.16) # Axes xaxis = (np.arange(img.shape[0], dtype=float) - m31K[0]) * 0.00995 yaxis = (np.arange(img.shape[1], dtype=float) - m31K[1]) * 0.00995 py.clf() py.imshow(img, aspect='equal', extent=[xaxis[0], xaxis[-1], yaxis[0], yaxis[-1]]) #py.plot([m31K[0]], [m31K[1]], 'ko') #py.axis([625, 825, 650, 850]) py.plot([0], [0], 'k+') py.axis([-1.5, 1.5, -1.5, 1.5]) py.xlabel('X Offset from M31* (arcsec)') py.ylabel('Y Offset from M31* (arcsec)') py.title('Blue = F330W, Green = F435W, Red = Kband') py.savefig(workdir + 'plots/hst_nirc2_rgb.png') py.show()
def make_iracpsf(psfname, oversample=10, interp='sinc', iracpix=0.6, radius=3.0): """ Make an oversample IRAC PSF given the PSF image in the IRAC pixel scale, for use in TFIT. psfname: the input IRAC PSF oversample: the oversampling factor (default is 10) interp: the algorithm of interpolation (default is linear) other options are nearest, poly3, poly5, spline3, sinc, lsinc, and drizzle. iracpix: the pixel scale (in arcsec) of the input IRAC PSF (default is 0.6) radius: the radius of the circularized PSF in arcsec (beyond which is set to 0) """ newscale = int(round(iracpix / oversample * 1000)) # the new pixel scale in milli-arcsec interp = interp.replace('[', '_') interp = interp.replace(']', '_') psfroot = os.path.splitext(psfname)[0] psfroot = psfroot + '_%dmas' % newscale psfroot = psfroot + '_%s' % interp size0 = pyfits.getdata(psfname).shape[0] size1 = size0 * oversample # First, run iraf.imlintran factor = 1. / oversample print "factor", factor if os.path.exists('temp1.fits'): os.remove('temp1.fits') iraf.imlintran(psfname, 'temp1.fits', 0., 0., factor, factor, ncols=size1, nlines=size1, interpolant=interp, fluxconserve='yes') # Second, circularize the PSF, assume that the input PSF has equal numbers # of rows and columns imgsize = pyfits.getdata('temp1.fits').shape[0] imgcenter = (imgsize + 1) / 2 print "imgcenter", imgcenter pixrad = radius / iracpix * oversample pixrad = np.round(pixrad) mathstr = 'if ((x-%d)**2 + (y-%d)**2) < %f**2 then im1 else 0' % ( imgcenter, imgcenter, pixrad) print "mathstr", mathstr if os.path.exists('temp2.fits'): os.remove('temp2.fits') iraf.imcalc('temp1.fits', 'temp2.fits', mathstr) # Third, trim the borders; shed border with units of 100 pixels nborder = ((imgsize - 2 * pixrad) / 2) / 100 print "nborder:", nborder if nborder > 0: begin = nborder * 100 + 1 end = imgsize - nborder * 100 - 1 iraf.imcopy('temp2.fits[%d:%d,%d:%d]' % (begin, end, begin, end), psfroot + '.fits') else: os.system('mv temp2.fits %s' % (psfroot + '.fits')) os.system('rm temp*.fits')
def plotWithHST(rotateFITS=True): # Load up the OSIRIS image cubeFile = datadir + cuberoot + "_img.fits" cube, cubehdr = pyfits.getdata(cubeFile, header=True) paCube = cubehdr["PA_SPEC"] scaleCube = 0.05 # Load up the NIRC2 image kFile = "/u/jlu/data/m31/05jul/combo/m31_05jul_kp.fits" # kFile = '/u/jlu/data/m31/09sep/combo/mag09sep_m31_kp.fits' k, khdr = pyfits.getdata(kFile, header=True) m31K = np.array([751.986, 716.989]) paK = 0.0 scaleK = 0.00995 f330Root = "/u/jlu/data/m31/hst/m31_acshrc_f330w_j9am01030" f435Root = "/u/jlu/data/m31/hst/m31_acshrc_f435w_j9am01010" f555Root = "/u/jlu/data/m31/hst/m31_wfpc2pc1_f555w_u2lg020at" f814Root = "/u/jlu/data/m31/hst/m31_wfpc2pc1_f814w_u2lg020bt" ########## # Rotate all the HST images to PA=0, scale=NIRC2 ########## if rotateFITS == True: # Load up the HST ACS image (330 nm) f330File = f330Root + ".fits" f330FileRot = f330Root + "_rot.fits" f330, f330hdr = pyfits.getdata(f330File, 1, header=True) m31F330 = np.array([547.0, 623.2]) paF330 = f330hdr["ORIENTAT"] scaleF330 = 0.025 # Load up the HST ACS image (435 nm) f435File = f435Root + ".fits" f435FileRot = f435Root + "_rot.fits" f435, f435hdr = pyfits.getdata(f435File, 1, header=True) m31F435 = np.array([546.9, 623.5]) paF435 = f435hdr["ORIENTAT"] scaleF435 = 0.025 # Load up the HST ACS image (555 nm) f555File = f555Root + ".fits" f555FileRot = f555Root + "_rot.fits" f555, f555hdr = pyfits.getdata(f555File, 1, header=True) m31F555 = np.array([973.0, 961.0]) paF555 = f555hdr["ORIENTAT"] scaleF555 = 0.1 # Load up the HST ACS image (814 nm) f814File = f814Root + ".fits" f814FileRot = f814Root + "_rot.fits" f814, f814hdr = pyfits.getdata(f814File, 1, header=True) m31F814 = np.array([975.0, 962.0]) paF814 = f814hdr["ORIENTAT"] scaleF814 = 0.1 print "scaleK = ", scaleK print "scaleF330 = ", scaleF330 print "scaleF435 = ", scaleF435 print "scaleF555 = ", scaleF555 print "scaleF814 = ", scaleF814 print "paK = ", paK print "paF330 = ", paF330 print "paF435 = ", paF435 print "paF555 = ", paF555 print "paF814 = ", paF814 gcutil.rmall([f330FileRot, f435FileRot, f555FileRot, f814FileRot]) from pyraf import iraf as ir ir.unlearn("imlintran") ir.imlintran.boundary = "constant" ir.imlintran.constant = 0 ir.imlintran.interpolant = "spline3" ir.imlintran.fluxconserve = "yes" # 330 ir.imlintran.xin = m31F330[0] ir.imlintran.yin = m31F330[1] ir.imlintran.xout = m31K[0] ir.imlintran.yout = m31K[1] ir.imlintran.ncols = k.shape[1] ir.imlintran.nlines = k.shape[0] ir.imlintran(f330File + "[1]", f330FileRot, paF330, paF330, scaleK / scaleF330, scaleK / scaleF330) # 435 ir.imlintran.xin = m31F435[0] ir.imlintran.yin = m31F435[1] ir.imlintran.xout = m31K[0] ir.imlintran.yout = m31K[1] ir.imlintran.ncols = k.shape[1] ir.imlintran.nlines = k.shape[0] ir.imlintran(f435File + "[1]", f435FileRot, paF435, paF435, scaleK / scaleF435, scaleK / scaleF435) # 555 ir.imlintran.xin = m31F555[0] ir.imlintran.yin = m31F555[1] ir.imlintran.xout = m31K[0] ir.imlintran.yout = m31K[1] ir.imlintran.ncols = k.shape[1] ir.imlintran.nlines = k.shape[0] ir.imlintran(f555File + "[1]", f555FileRot, paF555, paF555, scaleK / scaleF555, scaleK / scaleF555) # 814 ir.imlintran.xin = m31F814[0] ir.imlintran.yin = m31F814[1] ir.imlintran.xout = m31K[0] ir.imlintran.yout = m31K[1] ir.imlintran.ncols = k.shape[1] ir.imlintran.nlines = k.shape[0] ir.imlintran(f814File + "[1]", f814FileRot, paF814, paF814, scaleK / scaleF814, scaleK / scaleF814) f330 = pyfits.getdata(f330Root + "_rot.fits") f435 = pyfits.getdata(f435Root + "_rot.fits") img = np.zeros((k.shape[0], k.shape[1], 3), dtype=float) img[:, :, 0] = img_scale.linear(k, scale_min=300, scale_max=2400) img[:, :, 1] = img_scale.linear(f435, scale_min=0.45, scale_max=1.8) img[:, :, 2] = img_scale.linear(f330, scale_min=0, scale_max=0.16) # Axes xaxis = (np.arange(img.shape[0], dtype=float) - m31K[0]) * 0.00995 yaxis = (np.arange(img.shape[1], dtype=float) - m31K[1]) * 0.00995 py.clf() py.imshow(img, aspect="equal", extent=[xaxis[0], xaxis[-1], yaxis[0], yaxis[-1]]) # py.plot([m31K[0]], [m31K[1]], 'ko') # py.axis([625, 825, 650, 850]) py.plot([0], [0], "k+") py.axis([-1.5, 1.5, -1.5, 1.5]) py.xlabel("X Offset from M31* (arcsec)") py.ylabel("Y Offset from M31* (arcsec)") py.title("Blue = F330W, Green = F435W, Red = Kband") py.savefig(workdir + "plots/hst_nirc2_rgb.png") py.show()