def center(xx): global imdata # The aperture size for finding the location of the stars is arbitrarily set here app = 7. flux = -photutils.aperture_circular( imdata, xx[0], xx[1], app, method='exact', subpixels=10) return flux
def _aperture_phot(self, x, y, radsize=1, sky_inner=5, skywidth=5, method="subpixel", subpixels=4): """Perform sky subtracted aperture photometry, uses photutils functions, photutil must be installed Parameters ---------- radsize: int Size of the radius sky_inner: int Inner radius of the sky annulus skywidth: int Width of the sky annulus method: string Pixel sampling method to use subpixels: int How many subpixels to use Notes ----- background is taken from sky annulus pixels, check into masking bad pixels """ if not photutils_installed: print("Install photutils to enable") else: aper_flux = photutils.aperture_circular(self._data, x, y, radsize, subpixels=subpixels, method=method) aperture_area = np.pi * (radsize)**2 annulus_sky = photutils.annulus_circular(self._data, x, y, sky_inner, sky_inner + skywidth) outer = sky_inner + skywidth inner = sky_inner annulus_area = np.pi * (outer**2 - inner**2) skysub_flux = aper_flux - annulus_sky * (aperture_area / annulus_area) return (aper_flux, annulus_sky, skysub_flux)
def _aperture_phot(self,x,y,radsize=1,sky_inner=5,skywidth=5,method="subpixel",subpixels=4): """Perform sky subtracted aperture photometry, uses photutils functions background is taken from sky annulus pixels, check into masking """ aper_flux=photutils.aperture_circular(self._data,x,y,radsize,subpixels=subpixels,method=method) aperture_area = np.pi * (radsize)**2 annulus_sky=photutils.annulus_circular(self._data,x,y,sky_inner,sky_inner+skywidth) outer=sky_inner+skywidth inner=sky_inner annulus_area = np.pi * (outer**2 - inner**2) skysub_flux=aper_flux - annulus_sky * (aperture_area / annulus_area) return (aper_flux,annulus_sky,skysub_flux)
def _aperture_phot(self, x, y, radsize=1, sky_inner=5, skywidth=5, method="subpixel", subpixels=4): """Perform sky subtracted aperture photometry, uses photutils functions, photutil must be installed Parameters ---------- radsize: int Size of the radius sky_inner: int Inner radius of the sky annulus skywidth: int Width of the sky annulus method: string Pixel sampling method to use subpixels: int How many subpixels to use Notes ----- background is taken from sky annulus pixels, check into masking bad pixels """ if not photutils_installed: print("Install photutils to enable") else: aper_flux = photutils.aperture_circular( self._data, x, y, radsize, subpixels=subpixels, method=method) aperture_area = np.pi * (radsize) ** 2 annulus_sky = photutils.annulus_circular( self._data, x, y, sky_inner, sky_inner + skywidth) outer = sky_inner + skywidth inner = sky_inner annulus_area = np.pi * (outer ** 2 - inner ** 2) skysub_flux = aper_flux - annulus_sky * (aperture_area / annulus_area) return (aper_flux, annulus_sky, skysub_flux)
def get_fluxes(regfile, outfile, inneraprad=35, outeraprad=60, hdu=None, PPBEAM=1.0, debug=False, print_nulls=False, photut=True): """ Extract fluxes from a region-defined aperture with inner and outer circular apertures specififed MUST BE IN GALACTIC COORDINATES """ if hdu is None: raise ValueError("hdu keyword is required") data = hdu.data header = hdu.header wcs = pywcs.WCS(header) glonmax = wcs.wcs_pix2sky(0,0,0)[0] glonmin = wcs.wcs_pix2sky(data.shape[1],0,0)[1] reglist = pyregion.open(regfile) outf = open(outfile,'w') print "".join("%16s" % s for s in ['Source_Name','SumJy','ApSumJy','MeanApSumJy','SumJyBm','ApSumJyBm','BgMed','BgMean','BgStd','FracErrBg']) print >>outf,"".join("%16s" % s for s in ['Source_Name','SumJy','ApSumJy','MeanApSumJy','SumJyBm','ApSumJyBm','BgMed','BgMean','BgStd','FracErrBg']) if photut and not photut_ok: photut = False print "photutils is not loaded" if photut: dsqueeze = data.squeeze() for reg in reglist: glon,glat = position_region(reg).galactic() if not((glon > glonmin) and (glon < glonmax)): # these are the limits of the survey if print_nulls: print >>outf,"%16s" % sourcename,"".join("%16s" % f for f in ['-','-','-','-','-','-','-','-','-']) continue xc,yc = wcs.wcs_sky2pix(glon,glat,0) if xc < 0 or xc > data.shape[1] or yc < 0 or yc > data.shape[0]: if print_nulls: print >>outf,"%16s" % sourcename,"".join("%16s" % f for f in ['-','-','-','-','-','-','-','-','-']) continue regL = pyregion.ShapeList() reg.name = 'circle' while len(reg.coord_list) < 3: reg.coord_list.append(0) reg.coord_list[2] = inneraprad/3600.0 # set inner aperture (foreground) to R=25" regL.append(reg) if not photut: innerap = regL.get_mask(hdu=hdu) if innerap.sum() == 0: print "Skipped a source that was in the boundaries: ",reg continue regL[0].coord_list[2] = outeraprad/3600.0 # set outer aperture (background) to R=100" #regL.append(reg) # I think this turns two circles into a panda? if not photut: outerap = regL.get_mask(hdu=hdu) backreg = outerap-innerap if photut: innerappix = inneraprad / 3600. / np.abs(wcs.wcs.get_cdelt()[0]) / 2. outerappix = outeraprad / 3600. / np.abs(wcs.wcs.get_cdelt()[0]) / 2. total = photutils.aperture_circular(dsqueeze, xc, yc, innerappix) outer = photutils.aperture_circular(dsqueeze, xc, yc, outerappix) background = (outer-total) / ((outerappix**2-innerappix**2)*np.pi) backmean = background backstd = np.nan else: total = data[innerap].sum() background = np.median(data[backreg]) backmean = data[backreg].mean() backstd = data[backreg].std() sourcename = pos_to_name(reg) if backstd > total or total < 0: print "%s set to zero" % reg.attr[1]['text'] total = np.float(0) total_backsub = np.float(0) total_mbacksub = np.float(0) else: if photut: total_backsub = total_mbacksub = outer-total else: total_backsub = total - innerap.sum() * background total_mbacksub = total - innerap.sum() * backmean if total_backsub == 0: total_backsub = -np.inf print "%16s" % sourcename,"".join("%16.5g" % f for f in [total/PPBEAM,total_backsub/PPBEAM,total_mbacksub/PPBEAM,total,total_backsub,background,backmean,backstd,backstd/total_backsub]) print >>outf,"%16s" % sourcename,"".join("%16.5g" % f for f in [total/PPBEAM,total_backsub/PPBEAM,total_mbacksub/PPBEAM,total,total_backsub,background,backmean,backstd,backstd/total_backsub]) print "Done with %s" % outfile outf.close()
def aperture(image, hdr): global iap, pguess_old, nstars, svec dnorm = 500. rann1 = 18. dann = 2. rann2 = rann1 + dann app_min = 1. app_max = 19. dapp = 1. app_sizes = np.arange(app_min, app_max, dapp) # If first time through, read in "guesses" for locations of stars if iap == 0: var = np.loadtxt('phot_coords') xvec = var[:, 0] yvec = var[:, 1] nstars = len(xvec) #print app_sizes,'\n' else: xvec = svec[:, 0] yvec = svec[:, 1] # Find locations of stars dxx0 = 10. for i in range(nstars): xx0 = [xvec[i], yvec[i]] xbounds = (xx0[0] - dxx0, xx0[0] + dxx0) ybounds = (xx0[1] - dxx0, xx0[1] + dxx0) #res = sco.minimize(center, xx0, method='BFGS', jac=der_center) #res = sco.fmin_tnc(center, xx0, bounds=(xbounds,ybounds)) #res = sco.minimize(center, xx0, method='tnc', bounds=(xbounds,ybounds)) res = sco.minimize(center, xx0, method='L-BFGS-B', bounds=(xbounds, ybounds), jac=der_center) xx0 = res.x xvec[i] = xx0[0] yvec[i] = xx0[1] # Calculate sky around stars sky = photutils.annulus_circular(image, xvec, yvec, rann1, rann2, method='exact', subpixels=10) # Do psf fits to stars. Results are stored in arrays fwhm, pflux, psky, psf_x, and psf_y fwhm = np.zeros(nstars) # Make stacked array of star positions from aperture photometry svec = np.dstack((xvec, yvec))[0] #print svec # Make stacked array of star positions from PSF fitting # pvec = np.dstack((psf_x,psf_y))[0] pvec = svec iap = iap + 1 starr = [] apvec = [] app = -1.0 # Get time of observation from the header #date = hdr['DATE-OBS'] # for Argos files #utc = hdr['UTC'] # for Argos files date = hdr['UTC-DATE'] # for Pro-EM files utc = hdr['UTC-BEG'] # for Pro-EM files times = date + " " + utc t = Time(times, format='iso', scale='utc') # Calculate Julian Date of observation jd = t.jd for app in app_sizes: flux = photutils.aperture_circular(image, xvec, yvec, app, method='exact', subpixels=10) skyc = sky * app**2 / (rann2**2 - rann1**2) fluxc = flux - skyc starr.append([fluxc, skyc, fwhm]) apvec.append(app) starr = np.array(starr) apvec = np.array(apvec) #print starr return jd, svec, pvec, apvec, starr
def get_fluxes(regfile, outfile, inneraprad=35, outeraprad=60, hdu=None, PPBEAM=1.0, debug=False, print_nulls=False, photut=True): """ Extract fluxes from a region-defined aperture with inner and outer circular apertures specififed MUST BE IN GALACTIC COORDINATES """ if hdu is None: raise ValueError("hdu keyword is required") data = hdu.data header = hdu.header wcs = pywcs.WCS(header) glonmax = wcs.wcs_pix2sky(0, 0, 0)[0] glonmin = wcs.wcs_pix2sky(data.shape[1], 0, 0)[1] reglist = pyregion.open(regfile) outf = open(outfile, 'w') print "".join("%16s" % s for s in [ 'Source_Name', 'SumJy', 'ApSumJy', 'MeanApSumJy', 'SumJyBm', 'ApSumJyBm', 'BgMed', 'BgMean', 'BgStd', 'FracErrBg' ]) print >> outf, "".join("%16s" % s for s in [ 'Source_Name', 'SumJy', 'ApSumJy', 'MeanApSumJy', 'SumJyBm', 'ApSumJyBm', 'BgMed', 'BgMean', 'BgStd', 'FracErrBg' ]) if photut and not photut_ok: photut = False print "photutils is not loaded" if photut: dsqueeze = data.squeeze() for reg in reglist: glon, glat = position_region(reg).galactic() if not ((glon > glonmin) and (glon < glonmax)): # these are the limits of the survey if print_nulls: print >> outf, "%16s" % sourcename, "".join( "%16s" % f for f in ['-', '-', '-', '-', '-', '-', '-', '-', '-']) continue xc, yc = wcs.wcs_sky2pix(glon, glat, 0) if xc < 0 or xc > data.shape[1] or yc < 0 or yc > data.shape[0]: if print_nulls: print >> outf, "%16s" % sourcename, "".join( "%16s" % f for f in ['-', '-', '-', '-', '-', '-', '-', '-', '-']) continue regL = pyregion.ShapeList() reg.name = 'circle' while len(reg.coord_list) < 3: reg.coord_list.append(0) reg.coord_list[ 2] = inneraprad / 3600.0 # set inner aperture (foreground) to R=25" regL.append(reg) if not photut: innerap = regL.get_mask(hdu=hdu) if innerap.sum() == 0: print "Skipped a source that was in the boundaries: ", reg continue regL[0].coord_list[ 2] = outeraprad / 3600.0 # set outer aperture (background) to R=100" #regL.append(reg) # I think this turns two circles into a panda? if not photut: outerap = regL.get_mask(hdu=hdu) backreg = outerap - innerap if photut: innerappix = inneraprad / 3600. / np.abs( wcs.wcs.get_cdelt()[0]) / 2. outerappix = outeraprad / 3600. / np.abs( wcs.wcs.get_cdelt()[0]) / 2. total = photutils.aperture_circular(dsqueeze, xc, yc, innerappix) outer = photutils.aperture_circular(dsqueeze, xc, yc, outerappix) background = (outer - total) / ( (outerappix**2 - innerappix**2) * np.pi) backmean = background backstd = np.nan else: total = data[innerap].sum() background = np.median(data[backreg]) backmean = data[backreg].mean() backstd = data[backreg].std() sourcename = pos_to_name(reg) if backstd > total or total < 0: print "%s set to zero" % reg.attr[1]['text'] total = np.float(0) total_backsub = np.float(0) total_mbacksub = np.float(0) else: if photut: total_backsub = total_mbacksub = outer - total else: total_backsub = total - innerap.sum() * background total_mbacksub = total - innerap.sum() * backmean if total_backsub == 0: total_backsub = -np.inf print "%16s" % sourcename, "".join("%16.5g" % f for f in [ total / PPBEAM, total_backsub / PPBEAM, total_mbacksub / PPBEAM, total, total_backsub, background, backmean, backstd, backstd / total_backsub ]) print >> outf, "%16s" % sourcename, "".join("%16.5g" % f for f in [ total / PPBEAM, total_backsub / PPBEAM, total_mbacksub / PPBEAM, total, total_backsub, background, backmean, backstd, backstd / total_backsub ]) print "Done with %s" % outfile outf.close()