def do_phot(image,position,radius = 5, r_in=15., r_out=20.): aperture = CircularAperture(position,r=radius) bkg_aperture = CircularAnnulus(position,r_in=r_in,r_out=r_out) # perform the photometry; the default method is 'exact' phot = aperture_photometry(image, aperture) bkg = aperture_photometry(image, bkg_aperture) # calculate the mean background level (per pixel) in the annuli bkg_mean = bkg['aperture_sum'] / bkg_aperture.area() bkg_sum = bkg_mean * aperture.area() #look at ipython notebook; his may need editing; 'phot' in second line below may need brackets with 'flux_sum' inside #phot['bkg_sum'] = bkg_sum #phot['flux_sum'] = phot['flux'] - bkg_sum #these two lines from ipython notebook flux_bkgsub = phot['aperture_sum'] - bkg_sum phot['aperture_sum_bkgsub'] = flux_bkgsub return phot
def GetAP(cut, theta, reso=0.5): """ Performs aperture photometry. Calculates the average value of the cutout within a circle of radius theta minus the average in the outer ring [theta, sqrt(2)*theta] Params ------ - cut: the cutout (numpy array) - theta: radius (float, in arcmin) - reso: pixel resolution (float, in arcmin/pix) """ theta_R_pix = theta / reso positions = [(cut.shape[1] / 2., cut.shape[0] / 2.)] apertures = CircularAperture(positions, r=theta_R_pix) annulus_apertures = CircularAnnulus(positions, r_in=theta_R_pix, r_out=theta_R_pix * np.sqrt(2)) apers = [apertures, annulus_apertures] phot_table = aperture_photometry(cut, apers) bkg_mean = phot_table['aperture_sum_1'] / annulus_apertures.area() bkg_sum = bkg_mean * apertures.area() final_sum = phot_table['aperture_sum_0'] - bkg_sum phot_table['residual_aperture_sum'] = final_sum return phot_table['residual_aperture_sum'][0] / apertures.area()
def snr_astropy(image,sx,sy,r,r_in,r_out): """ Returns signal to noise ratio, signal value, noise value using Astropy's Photutils module Args: image (2d float array): Image array extracted from fits file sx,sy (float): x and y pixel locations for the center of the source r (float): radius for aperture r_in,r_out (float): inner and outer radii for annulus Return: snr (float): signal-to-noise ratio signal (float): sum of all pixel values within the specified aperture minus sky background noise (float): noise in the signal calculated as std deviation of pixels in the sky annulus times sqrt of the area of the signal aperture poisson_noise (float): snr determined from the poisson noise in the source signal. Poisson noise = sqrt(counts) [because variance of a poisson distribution is the parameter itself]. Poisson snr = Signal/sqrt(signal) = sqrt(signal) Written by: Logan Pearce, 2018 """ import warnings warnings.filterwarnings('ignore') from photutils import CircularAperture, CircularAnnulus positions = (cx-1,cy-1) ap = CircularAperture(positions,r=r) skyan = CircularAnnulus(positions,r_in=11,r_out=14) apsum = ap.do_photometry(image)[0] skysum = skyan.do_photometry(image)[0] averagesky = skysum/skyan.area() signal = (apsum - ap.area()*averagesky)[0] n = ap.area() ap_an = aperture_annulus(sx,sy,r,r_in,r_out) skyan = ap_an[1] poisson_noise = np.sqrt(signal) noise = noise = np.std(image[skyan[1],skyan[0]]) noise = noise*np.sqrt(n) snr = signal/noise return snr,signal,noise,poisson_noise
def do_phot(fname, datadir, phot_dir_name, apsize, annsize): hdul = fits.open(fname) if '.fz' in fname: data = hdul[1].data header = hdul[1].header hdul.close() else: data = hdul[0].data header = hdul[0].header hdul.close() gain = header.get('GAIN') if gain == None: a = 1 else: data = data.clip(min=0) rdnoise = header['RDNOISE'] error = np.sqrt(data * gain) + rdnoise mean, median, std = sigma_clipped_stats(data, sigma=3.0, iters=5) daofind = DAOStarFinder(fwhm=5.0, threshold=5. * std) sources = daofind(data - median) positions = (sources['xcentroid'], sources['ycentroid']) apertures_pix = CircularAperture(positions, r=apsize) annulus_pix = CircularAnnulus(positions, r_in=annsize[0], r_out=annsize[1]) apertures_sky = apertures_pix.to_sky(wcs.WCS(header)) annulus_sky = annulus_pix.to_sky(wcs.WCS(header)) apers = [apertures_sky, annulus_sky] # plt.imshow(data)#, cmap='Greys', origin='lower', norm=norm) # apertures_pix.plot(color='white', lw=1.5, alpha=0.5) # annulus_pix.plot(color='white', lw=1.5, alpha=0.5) # plt.show() # pdb.set_trace() phot_table = aperture_photometry(data, apers, wcs=wcs.WCS(header), error=error) bkg_mean = phot_table['aperture_sum_1'] / annulus_pix.area() bkg_sum = bkg_mean * apertures_pix.area() apertures_pix.area() final_sum = phot_table['aperture_sum_0'] - bkg_sum final_error = np.sqrt(phot_table['aperture_sum_err_0']**2 + phot_table['aperture_sum_err_1']**2) phot_table['residual_aperture_sum'] = final_sum phot_table['residual_err'] = final_error fname = datadir + phot_dir_name + "phot_table_{0:.7f}_{1}s.txt".format( header['MJD-OBS'], header['EXPTIME']) phot_table.write(fname, format='ascii') replace_comma(fname)
def measure_source_fwhm(detection, data, rmax=10): """ TO USE CAREFULLY Function used to estimate the FWHM of a source. It performs aperture photometry with inscreasing radius from the source position, and then, tries to find where half of the maximum of flux is reached. Parameters ---------- detection : :class:`~pandas:pandas.Series` Row of a DataFrame containing detected sources. Should have columns ``xcentroid`` and ``ycentroid``, in classic convention (not astropy) data : 2D :class:`~numpy:numpy.ndarray` Flux map used for the photometry """ x, y = np.array(detection[['xcentroid', 'ycentroid']]) photo_flux = np.zeros(rmax) for r in range(rmax): if r == 0: aper = CircularAperture((x, y), 1.) else: aper = CircularAnnulus((x, y), r, r + 1) photo_flux[r] = aper.do_photometry(data)[0] / aper.area() def get_fwhm(flux): #We assume max is on 0. If not, source is probably contaminated flux = flux - flux.min() spline = UnivariateSpline(np.arange(rmax), flux - flux[0] / 2., s=0) if spline.roots().shape != (1, ): return np.nan return spline.roots()[0] return (get_fwhm(photo_flux))
def ap_phot(sources, data, source_r=6., sky_in=15, sky_out=20, plot=False): global fig centroids = (sources['xcentroid'], sources['ycentroid']) source_aperture = CircularAperture(centroids, r=source_r) source_area = source_aperture.area() source_table = aperture_photometry(data, source_aperture) sky_aperture = CircularAnnulus(centroids, r_in=sky_in, r_out=sky_out) sky_area = sky_aperture.area() sky_table = aperture_photometry(data, sky_aperture) sky_subtracted_source = deepcopy(source_table) for i in range(np.shape(centroids)[1]): sky_value = sky_table[i][3] sky_per_pix = sky_value / sky_area sky_behind_source = sky_per_pix * source_area sky_subtracted_source[i][3] -= sky_behind_source if plot: fig = plt.figure(figsize=(17, 17)) plt.imshow(data, cmap='gray', origin='lower', vmin=0, vmax=1500) for i in range(np.shape(centroids)[1]): plt.annotate(str(source_table[i][0]), xy=(np.float64(source_table[i][1]) + 15., np.float64(source_table[i][2]) + 15.), color="white") source_aperture.plot(color='blue', lw=1.5, alpha=0.5) sky_aperture.plot(color="orange", lw=0.5, alpha=0.5) plt.tight_layout() plt.show() return sky_subtracted_source
def autocorr(self, ell_mask_scale=2, aperture_radius=5, annulus_width=4): # Compute 2D autocorrelation function try: masked_image = self.masked_image.copy() if ell_mask_scale > 0: masked_image.mask |= ~self.elliptical_mask(ell_mask_scale) masked_image = masked_image.filled(0.0) fft_imgae = np.fft.fft2(masked_image) acorr_image = np.fft.ifft2(fft_imgae * np.conjugate(fft_imgae)).real acorr_image = np.fft.ifftshift(acorr_image) ny, nx = masked_image.shape yy, xx = np.mgrid[:ny, :nx] circ = CircularAperture([nx // 2, ny // 2], r=aperture_radius) ann = CircularAnnulus([nx // 2, ny // 2], r_in=aperture_radius, r_out=aperture_radius + annulus_width) ann_mean = aperture_photometry( acorr_image, ann)['aperture_sum'][0] / ann.area() circ_mean = aperture_photometry( acorr_image, circ)['aperture_sum'][0] / circ.area() except: acorr_image = np.nan circ_mean = np.nan ann_mean = np.nan return acorr_image, circ_mean, ann_mean
def get_flux(frame, background, centroids, r, n, x, y): data = np.nan_to_num(frame) pixel_fluxes = [] for i in range(y - n / 2, y + n / 2 + 1): for j in range(x - n / 2, x + n / 2 + 1): pixel_fluxes.append(data[i][j] - background) pixel_fluxes_sum = np.sum(pixel_fluxes) pixel_fluxes = [i / pixel_fluxes_sum for i in pixel_fluxes] #calculate flux as sum of discrete pixels if r == 'pld': flux = pixel_fluxes_sum return [flux] + pixel_fluxes #calculate flux using a variable radius if r == 'var': #calculate beta, noise pixel parameter from #http://iopscience.iop.org/article/10.1088/0004-637X/766/2/95/pdf b = np.sum(frame)**2 / (np.sum(frame**2)) star_aperture = CircularAperture(centroids, r=b**0.5) else: star_aperture = CircularAperture(centroids, r=r) rawflux = aperture_photometry(data, star_aperture) #how much to remove from star aperture bkg_sum = background * star_aperture.area() flux = rawflux['aperture_sum'] - bkg_sum return [flux[0]] + pixel_fluxes
def aper_phot(data, header, positions, aper, data_out, gain=1, rdnoise=0): exptime = header['EXPTIME'] apertures = CircularAperture(positions, r=aper[0]) annulus_apertures = CircularAnnulus(positions, r_in=aper[1], r_out=aper[2]) annulus_masks = annulus_apertures.to_mask(method='center') bkg_median = [] for mask in annulus_masks: annulus_data = mask.multiply(data) annulus_data_1d = annulus_data[mask.data > 0] _, median_sigclip, _ = sigma_clipped_stats(annulus_data_1d) bkg_median.append(median_sigclip) bkg_intensity = np.array(bkg_median) * apertures.area() * gain phot = aperture_photometry(data, apertures) phot['signal_intensity'] = phot['aperture_sum'] * gain - bkg_intensity phot['SNR'] = phot['signal_intensity'] / (phot['signal_intensity'] + bkg_intensity + rdnoise**2)**0.5 phot['magnitude'] = -2.5 * np.log10( phot['signal_intensity'] / exptime) + 24 phot['delta_magnitude'] = 2.5 * np.log10(1 + 1 / phot['SNR']) for col in phot.colnames: phot[col].info.format = '%.4f' # for consistent table output imageshow(data, positions, aper=aper) return phot
def autocorr(self, ell_mask_scale=2, aperture_radius=5, annulus_width=4): # Compute 2D autocorrelation function try: masked_image = self.masked_image.copy() if ell_mask_scale > 0: masked_image.mask |= ~self.elliptical_mask(ell_mask_scale) masked_image = masked_image.filled(0.0) fft_imgae = np.fft.fft2(masked_image) acorr_image = np.fft.ifft2(fft_imgae * np.conjugate(fft_imgae)).real acorr_image = np.fft.ifftshift(acorr_image) ny, nx = masked_image.shape yy, xx = np.mgrid[:ny, :nx] circ = CircularAperture([nx // 2, ny // 2], r=aperture_radius) ann = CircularAnnulus([nx // 2, ny // 2], r_in=aperture_radius, r_out=aperture_radius + annulus_width) ann_mean = aperture_photometry( acorr_image, ann)['aperture_sum'][0] / ann.area() circ_mean = aperture_photometry( acorr_image, circ)['aperture_sum'][0] / circ.area() except: acorr_image = np.nan circ_mean = np.nan ann_mean = np.nan return acorr_image, circ_mean, ann_mean
def compute_snr(x, y): positions = (x - 1, y - 1) ap = CircularAperture(positions, r=radius) skyan = CircularAnnulus(positions, r_in=11, r_out=14) ap = CircularAperture(positions, r=radius) skyan = CircularAnnulus(positions, r_in=11, r_out=14) apsum = ap.do_photometry(image)[0] skysum = skyan.do_photometry(image)[0] averagesky = skysum / skyan.area() signal = (apsum - ap.area() * averagesky)[0] n = ap.area() box = image[y + 12:y + 12 + 15, x + 12:x + 12 + 15] noise = np.std(box) noise = noise * np.sqrt(n) snr = signal / noise return snr
def do_Circ_phot(pos, FWHM, ap_min=3., ap_factor=1.5, rin=None, rout=None, \ sky_nsigma=3., sky_iter=10): ''' In rigorous manner, we should use error = sqrt (flux / epadu + area * stdev**2 + area**2 * stdev**2 / nsky) as in http://stsdas.stsci.edu/cgi-bin/gethelp.cgi?phot . Here flux == aperture_sum - sky, i.e., flux from image_reduc. stdev should include sky error AND ronoise. ''' if rin == None: rin = 4 * FWHM if rout == None: rout = 6 * FWHM N = len(pos) if pos.ndim == 1: N = 1 an = CircAn(pos, r_in=rin, r_out=rout) ap_size = np.max([ap_min, ap_factor*FWHM]) aperture = CircAp(pos, r=ap_size) flux = aperture.do_photometry(image_reduc, method='exact')[0] # do phot and get sum from aperture. [0] is sum and [1] is error. #For test: #N=len(pos_star_fit) #an = CircAn(pos_star_fit, r_in=4*FWHM_moffat, r_out=6*FWHM_moffat) #ap_size = 1.5*FWHM_moffat #aperture = CircAp(pos_star_fit, r=ap_size) #flux = aperture.do_photometry(image_reduc, method='exact')[0] flux_ss = np.zeros(N) error = np.zeros(N) for i in range(0, N): mask_an = (an.to_mask(method='center'))[i] # cf: test = mask_an.cutout(image_reduc) <-- will make cutout image. sky_an = mask_an.apply(image_reduc) all_sky = sky_an[np.nonzero(sky_an)] # only annulus region will be saved as np.ndarray msky, stdev, nsky, nrej = sky_fit(all_sky, method='Mode', mode_option='sex') area = aperture.area() flux_ss[i] = flux[i] - msky*area # sky subtracted flux error[i] = np.sqrt( flux_ss[i]/gain \ + area * stdev**2 \ + area**2 * stdev**2 / nsky ) # To know rejected number, uncomment the following. # plt.imshow(sky_an, cmap='gray_r', vmin=-20) # plt.colorbar() # plt.show() # mask_ap = (aperture.to_mask(method='exact'))[i] # star_ap = mask_ap.apply(image_reduc) # plt.imshow(star_ap) # plt.colorbar() # plt.show() # plt.cla() if pos.ndim > 1: print('\t[{x:7.2f}, {y:7.2f}], {nsky:3d} {nrej:3d} {msky:7.2f} {stdev:7.2f} {flux:7.1f} {ferr:3.1f}'.format(\ x=pos[i][0], y=pos[i][1], \ nsky=nsky, nrej=nrej, msky=msky, stdev=stdev,\ flux=flux_ss[i], ferr=error[i])) return flux_ss, error
def GetApertureTau(self, M, z, theta_R, x_c=0.5, gamma=-0.2, reso=0.2, theta_max=10., chi=1., fwhm_arcmin=0., profile='Battaglia', lowpass=False): # if theta_R > theta_max: # theta_max = theta_R * 2. tau_profile = self.GetTauProfile(M, z, x_c=x_c, gamma=gamma, reso=reso, theta_max=theta_max, chi=chi, fwhm_arcmin=fwhm_arcmin, profile=profile, lowpass=lowpass) theta_R_pix = theta_R / reso apertures = CircularAperture( [(tau_profile.shape[1] / 2., tau_profile.shape[0] / 2.)], r=theta_R_pix) annulus_apertures = CircularAnnulus( [(tau_profile.shape[1] / 2., tau_profile.shape[0] / 2.)], r_in=theta_R_pix, r_out=theta_R_pix * np.sqrt(2)) apers = [apertures, annulus_apertures] phot_table = aperture_photometry(tau_profile, apers) bkg_mean = phot_table['aperture_sum_1'] / annulus_apertures.area() bkg_sum = bkg_mean * apertures.area() final_sum = phot_table['aperture_sum_0'] - bkg_sum phot_table['residual_aperture_sum'] = final_sum return phot_table['residual_aperture_sum'][0] / apertures.area()
def phot(self, image, objpos, aper): """ Aperture photometry using Astropy's photutils. Parameters ---------- image : numpy array 2D image array objpos : list of tuple Object poistions as list of tuples aper : float Aperture radius in pixels Returns ------- phot_table : astropy table Output table with stellar photometry """ try: from astropy.table import hstack from photutils import aperture_photometry, CircularAnnulus, CircularAperture except ImportError: pass apertures = CircularAperture(objpos, r = aper) annulus_apertures = CircularAnnulus(objpos, r_in = self.inner_radius, r_out = self.outer_radius) rawflux_table = aperture_photometry(image, apertures = apertures, method = self.method) bkgflux_table = aperture_photometry(image, apertures = annulus_apertures, method = self.method) phot_table = hstack([rawflux_table, bkgflux_table], table_names = ["raw", "bkg"]) bkg = phot_table["aperture_sum_bkg"] / annulus_apertures.area() phot_table["msky"] = bkg phot_table["area"] = apertures.area() phot_table["nsky"] = annulus_apertures.area() bkg_sum = bkg * apertures.area() final_sum = phot_table["aperture_sum_raw"] - bkg_sum phot_table["flux"] = final_sum return phot_table
def phot(self, image, objpos, aper): """ Aperture photometry using Astropy's photutils. Parameters ---------- image : numpy array 2D image array objpos : list of tuple Object poistions as list of tuples aper : float Aperture radius in pixels Returns ------- phot_table : astropy table Output table with stellar photometry """ try: from astropy.table import hstack from photutils import aperture_photometry, CircularAnnulus, CircularAperture except ImportError: pass apertures = CircularAperture(objpos, r = aper) annulus_apertures = CircularAnnulus(objpos, r_in = self.inner_radius, r_out = self.outer_radius) rawflux_table = aperture_photometry(image, apertures = apertures, method = self.method) bkgflux_table = aperture_photometry(image, apertures = annulus_apertures, method = self.method) phot_table = hstack([rawflux_table, bkgflux_table], table_names = ["raw", "bkg"]) bkg = phot_table["aperture_sum_bkg"] / annulus_apertures.area() phot_table["msky"] = bkg phot_table["area"] = apertures.area() phot_table["nsky"] = annulus_apertures.area() bkg_sum = bkg * apertures.area() final_sum = phot_table["aperture_sum_raw"] - bkg_sum phot_table["flux"] = final_sum return phot_table
def ap_phot(sources, data, source_r=3.): global fig centroids = (sources['xcentroid'], sources['ycentroid']) source_aperture = CircularAperture(centroids, r=source_r) source_area = source_aperture.area() source_table = aperture_photometry(data, source_aperture) sky_aperture = CircularAperture((290, 130), r=3.) sky_area = sky_aperture.area() sky_table = aperture_photometry(data, sky_aperture) sky_subtracted_source = deepcopy(source_table) for i in range(np.shape(centroids)[1]): sky_value = sky_table[0][3] sky_per_pix = sky_value / sky_area sky_behind_source = sky_per_pix * source_area sky_subtracted_source[i][3] -= sky_behind_source return sky_subtracted_source
def ap_phot(im, cen, r1, r2, r3): """ Simple aperture photometry, using a circular source aperture and sky annulus. """ apertures = CircularAperture(cen, r1) annulus_apertures = CircularAnnulus(cen, r2, r3) apers = [apertures, annulus_apertures] phot_table = aperture_photometry(im, apers) bkg_mean = phot_table['aperture_sum_1'] / annulus_apertures.area() bkg_sum = bkg_mean * apertures.area() final_sum = phot_table['aperture_sum_0'] - bkg_sum return float(final_sum)
def aperture_photometry(self, image, positions, r, r_int, r_ext): # Define apertures and anulluses at all found sources: object_aperture = CircularAperture(positions, r) background_annulus = CircularAnnulus(positions, r_int, r_ext) # Calculate the flux in each aperture (star, and background, and subtract) backgound_solution = aperture_photometry(image, background_annulus) object_solution = aperture_photometry(image, object_aperture) background_per_pixel = backgound_solution[ "aperture_sum"] / background_annulus.area() object_solution[ "aperture_sum"] -= background_per_pixel * object_aperture.area() return np.array(object_solution["aperture_sum"]), background_per_pixel
def GetAvg(cut, theta, reso=0.5): """ Calculates the average value of the cutout within a circle of radius theta. Params ------ - cut: the cutout (numpy array) - theta: radius (float, in arcmin) - reso: pixel resolution (float, in arcmin/pix) """ theta_R_pix = theta / reso apertures = CircularAperture([(cut.shape[1] / 2., cut.shape[0] / 2.)], r=theta_R_pix) phot_table = aperture_photometry(cut, apertures) bkg_mean = phot_table['aperture_sum'] return bkg_mean / apertures.area()
def photometry(positionX, positionY, radius, radiusInner, radiusOuter, imageForPhotometry, sessionId): try: positionX = int(positionX) positionY = int(positionY) radius = int(radius) radiusInner = int(radiusInner) radiusOuter = int(radiusOuter) positions = [(positionX, positionY)] apertures = CircularAperture(positions, r=radius) annulus_apertures = CircularAnnulus(positions, r_in=radiusInner, r_out=radiusOuter) #Open data dataList = [] dataList = openFile(backendInputFits + sessionId + "_Processed_" + imageForPhotometry) #object aperture try: rawflux_table = aperture_photometry(dataList, apertures) except (RuntimeError, TypeError, NameError): print 'I cannot' #backround apertures bkgflux_table = aperture_photometry(dataList, annulus_apertures) try: phot_table = astropy.table.hstack([rawflux_table, bkgflux_table], table_names=['raw', 'bkg']) except (RuntimeError, TypeError, NameError): print 'error' bkg_mean = phot_table['aperture_sum_bkg'] / annulus_apertures.area() bkg_sum = bkg_mean * apertures.area() final_sum = phot_table['aperture_sum_raw'] - bkg_sum phot_table['residual_aperture_sum'] = final_sum #instrumental magnitude instrumentalMag = 2.5 * math.log10( (rawflux_table[0][3] - phot_table['residual_aperture_sum'][0])) return instrumentalMag except (RuntimeError, TypeError, NameError): print 'error in photometry function'
def getPhotometry( self, x, y, data, rphot, rin, rout, plot_on_ds9, image_name): #function to retrieve photometry of a target #rphot=5. #rin=10. #rout=20. #rin=rphot+2 #give dead zone outside of the rphot value #rout=rin+4 #provide plenty of room for background measurment #pos=[(30.,30.),(40.,40.)] #positions of photometry pos = [(x, y)] apertures = CircularAperture(pos, r=rphot) annulus_apertures = CircularAnnulus(pos, r_in=rin, r_out=rout) rawflux_table = aperture_photometry( data, apertures, method='exact' ) #subpixel divides pixels up to determine aperature in or out bkgflux_table = aperture_photometry(data, annulus_apertures, method='exact') phot_table = hstack([rawflux_table, bkgflux_table], table_names=['raw', 'bkg']) bkg_mean = phot_table['aperture_sum_bkg'] / annulus_apertures.area() bkg_sum = bkg_mean * apertures.area() final_sum = phot_table['aperture_sum_raw'] - bkg_sum phot_table['residual_aperture_sum'] = final_sum #print bkg_mean #print bkg_sum #print phot_table a = phot_table['residual_aperture_sum'][0] #print a #stop if plot_on_ds9 == True: ds = DS9() ds.set('file %s' % (image_name)) reg1 = 'regions command "annulus %s %s %s %s #color=blue"' % ( x, y, rin, rout) reg2 = 'regions command "circle %s %s %s #color=yellow"' % (x, y, rphot) ds.set('%s' % (reg1)) ds.set('%s' % (reg2)) return round(a, 3)
def simpleapphot(fileName, pos, r, rI, rO, frame='image'): '''Performs simple circular aperture photometry with local bg subtraction ''' # Check Type of pos first if not isinstance(pos, np.ndarray): pos = np.array(pos) if pos.ndim == 1: pos = pos.reshape((-1, 2)) # Create Aperture frame = frame.lower() if frame == 'image': # Create the Aperatures cirAps = CircularAperture(pos, r) annAps = CircularAnnulus(pos, rI, rO) elif frame == 'fk5': # Create Sky Apertures pos = SkyCoord(frame=frame, ra=pos[:, 0] * u.deg, dec=pos[:, 1] * u.deg) cirAps = SkyCircularAperture(pos, r * u.arcsec) annAps = SkyCircularAnnulus(pos, rI * u.arcsec, rO * u.arcsec) else: raise ValueError('Unsupported coordinate system.') # Load in the files and do photometry hdu = fits.open(fileName) cirPhotTab = aperture_photometry(hdu, cirAps) annPhotTab = aperture_photometry(hdu, annAps) if frame == 'fk5': cirAps = cirAps.to_pixel(WCS(header=hdu['SCI'].header)) annAps = annAps.to_pixel(WCS(header=hdu['SCI'].header)) hdu.close() # Get Photometry as ndarray phot = cirPhotTab['aperture_sum'].data - \ (cirAps.area()/annAps.area())*annPhotTab['aperture_sum'].data return phot
def aper_phot(img_data,positions, r = 10., r_in = 14., r_out = 18,bkg_sub=False,plot=False): """ :params: r: Aperture Radius :params: r_in: annulus aperture inside radius :params: r_out: annulus aperture outside radius :params: bkg_sub: True if background subtraction is needed :params: plot: True to plot :out: phot_table: Table with the values of the aperture photometry """ #Background subtraction if bkg_sub == True: sigma_clip = SigmaClip(sigma=3., iters=10) bkg_estimator = MedianBackground() bkg = Background2D(img_data, (50, 50), filter_size=(3, 3),sigma_clip=sigma_clip, bkg_estimator=bkg_estimator) data_sub = img_data - bkg.background else: data_sub = img_data #Aperture Photometry using a circular aperture and a circular annulus apertures = CircularAperture(positions, r=r) annulus_apertures = CircularAnnulus(positions,r_in = r_in,r_out = r_out) apers = [apertures,annulus_apertures] phot_table = aperture_photometry(data_sub, apers) bkg_mean = phot_table['aperture_sum_1'] / annulus_apertures.area() bkg_sum = bkg_mean * apertures.area() final_sum = phot_table['aperture_sum_0'] - bkg_sum phot_table['residual_aperture_sum'] = final_sum positions = np.array(positions) if plot == True: #Ploting norm = ImageNormalize(data_sub, interval=ZScaleInterval(),stretch=LinearStretch()) plt.imshow(data_sub, cmap='Greys', origin='lower',norm=norm) apertures.plot(color='blue', lw=1.5, alpha=0.5) annulus_apertures.plot(color='green',lw=1.5,alpha=0.5) plt.plot(positions[:,0],positions[:,1], ls='none', color = 'red', marker='.', ms=10, lw=1.5) plt.xlim(0, data_sub.shape[1]-1) plt.ylim(0, data_sub.shape[0]-1) plt.show() return phot_table
def ap_phot(image, star_tbl, read_noise, exposure, r=1.5, r_in=1.5, r_out=3.): ''' Given an image, go do some aperture photometry ''' from astropy.stats import sigma_clipped_stats from photutils import aperture_photometry, CircularAperture, CircularAnnulus from photutils.utils import calc_total_error # Build apertures from star_tbl positions = np.transpose([star_tbl['x'], star_tbl['y']]) apertures = CircularAperture(positions, r=r) annulus_apertures = CircularAnnulus(positions, r_in=r_in, r_out=r_out) annulus_masks = annulus_apertures.to_mask(method='center') # Get backgrounds in annuli bkg_median = [] for mask in annulus_masks: annulus_data = mask.multiply(image) annulus_data_1d = annulus_data[mask.data > 0] _, median_sigclip, _ = sigma_clipped_stats(annulus_data_1d) bkg_median.append(median_sigclip.value) bkg_median = np.array(bkg_median) * image.unit # Set error error = calc_total_error(image.value, read_noise / exposure.value, exposure.value) error *= image.unit # Perform aperture photometry result = aperture_photometry(image, apertures, error=error) result['annulus_median'] = bkg_median result['aper_bkg'] = bkg_median * apertures.area() result['aper_sum_bkgsub'] = result['aperture_sum'] - result['aper_bkg'] # To-do: fold an error on background level into the aperture photometry error for col in result.colnames: result[col].info.format = '%.8g' # for consistent table output # print("Aperture photometry complete") return result, apertures, annulus_apertures
def flux_aperture(image, Xc, Yc, sigx): from photutils import CircularAperture from photutils import aperture_photometry from photutils import CircularAnnulus #local where we will apply th photometry apertures = CircularAperture((Xc, Yc), r=sigx) annulus_apertures = CircularAnnulus((Xc, Yc), r_in=sigx + 2, r_out=sigx + 4) #perform photometry apers = [apertures, annulus_apertures] phot_table = aperture_photometry(image, apers) # phot_table = aperture_photometry(image- bkg, apertures) #subtracted the sky background bkg_mean = phot_table['aperture_sum_1'] / annulus_apertures.area() bkg_sum = bkg_mean * apertures.area() final_sum = phot_table['aperture_sum_0'] - bkg_sum return final_sum
def calc_bkg_rms(ap, image, src_ap_area, rpsrc, mask=None, min_ap=6): if isinstance(ap, CircularAnnulus): aback = bback = ap.r_in + rpsrc ap_theta = 0 elif isinstance(ap, EllipticalAnnulus): aback = ap.a_in + rpsrc bback = ap.b_in + rpsrc ap_theta = ap.theta ecirc = ellip_circumference(aback, bback) diam = 2*rpsrc # Estimate the number of background apertures that can fit around the source # aperture. naps = np.int(np.round(ecirc/diam)) # Use a minimum of 6 apertures naps = np.max([naps, min_ap]) #naps = 6 theta_back = np.linspace(0, 2*np.pi, naps, endpoint=False) # Get the x, y positions of the background apertures x, y = ellip_point(ap.positions[0], aback, bback, ap_theta, theta_back) # Create the background apertures and calculate flux within each bkg_aps = CircularAperture(np.vstack([x,y]).T, rpsrc) flux_bkg = aperture_photometry(image, bkg_aps, mask=mask) flux_bkg = flux_bkg['aperture_sum'] flux_bkg_adj = flux_bkg/bkg_aps.area() * src_ap_area # Use sigma-clipping to determine the RMS of the background # Scale to the area of the source aperture me, md, sd = sigma_clipped_stats(flux_bkg_adj, sigma=3) bkg_rms = sd return bkg_rms, bkg_aps
def circleApp(x, y, r, img_data): numAps = 20 maxR = 1.5 myPhot = list() myRs = list() myCounts = list() myCountsNoBack = list() positions = [(x, y)] # print positions print r print(maxR * r) myBackAp = CircularAnnulus(positions, r_in=maxR * r, r_out=(maxR + 0.5) * r) # / numAps) myBackPhot = aperture_photometry(img_data, myBackAp) print myBackPhot['aperture_sum'][0] myBackRate = myBackPhot['aperture_sum'][0] / myBackAp.area( ) # (math.pi * (((maxR + 0.5) * r) ** 2 - (maxR * r) ** 2)) print myBackRate for j in range(numAps): curR = maxR * r * (j + 1) / numAps aperture = CircularAperture(positions, r=curR) myRs.append(curR) # maxR*(j+1.0)/numAps myPhot.append(aperture_photometry(img_data, aperture, method="exact")) tmp = myPhot[j] myCounts.append( tmp['aperture_sum'][0] - (myBackRate * aperture.area())) # math.pi * (curR) ** 2)) myCountsNoBack.append(tmp['aperture_sum'][0]) yerr = np.sqrt(myCountsNoBack) / myCountsNoBack[ numAps - 1] # don't use subtract background return myRs, myCountsNoBack, yerr
def extract_ifu(input_model, source_type, extract_params): """This function does the extraction. Parameters ---------- input_model : IFUCubeModel The input model. source_type : string "point" or "extended" extract_params : dict The extraction parameters for aperture photometry. Returns ------- ra, dec : float ra and dec are the right ascension and declination respectively at the nominal center of the image. wavelength : ndarray, 1-D The wavelength in micrometers at each pixel. net : ndarray, 1-D The count rate (or flux) minus the background at each pixel. background : ndarray, 1-D The background count rate that was subtracted from the total source count rate to get `net`. npixels : ndarray, 1-D, float64 For each slice, this is the number of pixels that were added together to get `net`. dq : ndarray, 1-D, uint32 The data quality array. """ data = input_model.data shape = data.shape if len(shape) != 3: log.error("Expected a 3-D IFU cube; dimension is %d.", len(shape)) raise RuntimeError("The IFU cube should be 3-D.") # We need to allocate net, background, npixels, and dq arrays # no matter what. We may need to divide by npixels, so the default # is 1 rather than 0. net = np.zeros(shape[0], dtype=np.float64) background = np.zeros(shape[0], dtype=np.float64) npixels = np.ones(shape[0], dtype=np.float64) dq = np.zeros(shape[0], dtype=np.uint32) x_center = extract_params['x_center'] y_center = extract_params['y_center'] if x_center is None: x_center = float(shape[2]) / 2. else: x_center = float(x_center) if y_center is None: y_center = float(shape[1]) / 2. else: y_center = float(y_center) method = extract_params['method'] # subpixels is only needed if method = 'subpixel'. subpixels = extract_params['subpixels'] subtract_background = extract_params['subtract_background'] smaller_axis = float(min(shape[1], shape[2])) # for defaults radius = None inner_bkg = None outer_bkg = None if source_type == 'point': radius = extract_params['radius'] if radius is None: radius = smaller_axis / 4. if subtract_background: inner_bkg = extract_params['inner_bkg'] if inner_bkg is None: inner_bkg = radius outer_bkg = extract_params['outer_bkg'] if outer_bkg is None: outer_bkg = min(inner_bkg * math.sqrt(2.), smaller_axis / 2. - 1.) if inner_bkg <= 0. or outer_bkg <= 0. or inner_bkg >= outer_bkg: log.debug("Turning background subtraction off, due to " "the values of inner_bkg and outer_bkg.") subtract_background = False width = None height = None theta = None else: width = extract_params['width'] if width is None: width = smaller_axis / 2. height = extract_params['height'] if height is None: height = smaller_axis / 2. theta = extract_params['theta'] * math.pi / 180. subtract_background = False log.debug("IFU 1-D extraction parameters:") log.debug(" x_center = %s", str(x_center)) log.debug(" y_center = %s", str(y_center)) if source_type == 'point': log.debug(" radius = %s", str(radius)) log.debug(" subtract_background = %s", str(subtract_background)) log.debug(" inner_bkg = %s", str(inner_bkg)) log.debug(" outer_bkg = %s", str(outer_bkg)) log.debug(" method = %s", method) if method == "subpixel": log.debug(" subpixels = %s", str(subpixels)) else: log.debug(" width = %s", str(width)) log.debug(" height = %s", str(height)) log.debug(" theta = %s degrees", str(extract_params['theta'])) log.debug(" subtract_background = %s", str(subtract_background)) log.debug(" method = %s", method) if method == "subpixel": log.debug(" subpixels = %s", str(subpixels)) # Check for out of bounds. # The problem with having the background aperture extend beyond the # image is that the normalization would not account for the resulting # decrease in the area of the annulus, so the background subtraction # would be systematically low. outside = False f_nx = float(shape[2]) f_ny = float(shape[1]) if x_center < 0. or x_center >= f_nx - 1. or \ y_center < 0. or y_center >= f_ny - 1.: outside = True log.error("Target location is outside the image.") if subtract_background and \ (x_center - outer_bkg < -0.5 or x_center + outer_bkg > f_nx - 0.5 or y_center - outer_bkg < -0.5 or y_center + outer_bkg > f_ny - 0.5): outside = True log.error("Background region extends outside the image.") if outside: (ra, dec) = (0., 0.) wavelength = np.zeros(shape[0], dtype=np.float64) dq[:] = dqflags.pixel['DO_NOT_USE'] return (ra, dec, wavelength, net, background, npixels, dq) # all bad x0 = float(shape[2]) / 2. y0 = float(shape[1]) / 2. (ra, dec, wavelength) = get_coordinates(input_model, x0, y0) position = (x_center, y_center) if source_type == 'point': aperture = CircularAperture(position, r=radius) if subtract_background: annulus = CircularAnnulus(position, r_in=inner_bkg, r_out=outer_bkg) normalization = aperture.area() / annulus.area() else: aperture = RectangularAperture(position, width, height, theta) # No background is computed for an extended source. npixels[:] = aperture.area() for k in range(shape[0]): phot_table = aperture_photometry(data[k, :, :], aperture, method=method, subpixels=subpixels) net[k] = float(phot_table['aperture_sum'][0]) if subtract_background: bkg_table = aperture_photometry(data[k, :, :], annulus, method=method, subpixels=subpixels) background[k] = float(bkg_table['aperture_sum'][0]) net[k] = net[k] - background[k] * normalization # Check for NaNs in the wavelength array, flag them in the dq array, # and truncate the arrays if NaNs are found at endpoints (unless the # entire array is NaN). (wavelength, net, background, npixels, dq) = \ nans_in_wavelength(wavelength, net, background, npixels, dq) return (ra, dec, wavelength, net, background, npixels, dq)
def extract_ifu(input_model, source_type, extract_params): """This function does the extraction. Parameters ---------- input_model : IFUCubeModel The input model. source_type : string "point" or "extended" extract_params : dict The extraction parameters for aperture photometry. Returns ------- ra, dec : float ra and dec are the right ascension and declination respectively at the nominal center of the image. wavelength : ndarray, 1-D The wavelength in micrometers at each pixel. net : ndarray, 1-D The count rate (counts / s) minus the background at each pixel. background : ndarray, 1-D The background count rate that was subtracted from the total source count rate to get `net`. dq : ndarray, 1-D, int32 The data quality array. """ data = input_model.data shape = data.shape if len(shape) != 3: log.error("Expected a 3-D IFU cube; dimension is %d.", len(shape)) raise RuntimeError("The IFU cube should be 3-D.") # We need to allocate net, background, and dq arrays no matter what. net = np.zeros(shape[0], dtype=np.float64) background = np.zeros(shape[0], dtype=np.float64) dq = np.zeros(shape[0], dtype=np.int32) x_center = extract_params['x_center'] y_center = extract_params['y_center'] if x_center is None: x_center = float(shape[2]) / 2. else: x_center = float(x_center) if y_center is None: y_center = float(shape[1]) / 2. else: y_center = float(y_center) method = extract_params['method'] # subpixels is only needed if method = 'subpixel'. subpixels = extract_params['subpixels'] subtract_background = extract_params['subtract_background'] smaller_axis = float(min(shape[1], shape[2])) # for defaults if source_type == 'point': radius = extract_params['radius'] if radius is None: radius = smaller_axis / 4. if subtract_background: inner_bkg = extract_params['inner_bkg'] if inner_bkg is None: inner_bkg = radius outer_bkg = extract_params['outer_bkg'] if outer_bkg is None: outer_bkg = min(inner_bkg * math.sqrt(2.), smaller_axis / 2. - 1.) if inner_bkg <= 0. or outer_bkg <= 0. or inner_bkg >= outer_bkg: log.debug("Turning background subtraction off, due to " "the values of inner_bkg and outer_bkg.") subtract_background = False width = None height = None theta = None else: width = extract_params['width'] if width is None: width = smaller_axis / 2. height = extract_params['height'] if height is None: height = smaller_axis / 2. theta = extract_params['theta'] * math.pi / 180. radius = None subtract_background = False inner_bkg = None outer_bkg = None log.debug("IFU 1-D extraction parameters:") log.debug(" x_center = %s", str(x_center)) log.debug(" y_center = %s", str(y_center)) if source_type == 'point': log.debug(" radius = %s", str(radius)) log.debug(" subtract_background = %s", str(subtract_background)) log.debug(" inner_bkg = %s", str(inner_bkg)) log.debug(" outer_bkg = %s", str(outer_bkg)) log.debug(" method = %s", method) if method == "subpixel": log.debug(" subpixels = %s", str(subpixels)) else: log.debug(" width = %s", str(width)) log.debug(" height = %s", str(height)) log.debug(" theta = %s degrees", str(extract_params['theta'])) log.debug(" subtract_background = %s", str(subtract_background)) log.debug(" method = %s", method) if method == "subpixel": log.debug(" subpixels = %s", str(subpixels)) # Check for out of bounds. # The problem with having the background aperture extend beyond the # image is that the normalization would not account for the resulting # decrease in the area of the annulus, so the background subtraction # would be systematically low. outside = False f_nx = float(shape[2]) f_ny = float(shape[1]) if x_center < 0. or x_center >= f_nx - 1. or \ y_center < 0. or y_center >= f_ny - 1.: outside = True log.error("Target location is outside the image.") if subtract_background and \ (x_center - outer_bkg < -0.5 or x_center + outer_bkg > f_nx - 0.5 or y_center - outer_bkg < -0.5 or y_center + outer_bkg > f_ny - 0.5): outside = True log.error("Background region extends outside the image.") if outside: (ra, dec) = (0., 0.) wavelength = np.zeros(shape[0], dtype=np.float64) dq[:] = dqflags.pixel['DO_NOT_USE'] return (ra, dec, wavelength, net, background, dq) # all bad if hasattr(input_model.meta, 'wcs'): wcs = input_model.meta.wcs else: log.warning("WCS function not found in input.") wcs = None if wcs is not None: x_array = np.empty(shape[0], dtype=np.float64) x_array.fill(float(shape[2]) / 2.) y_array = np.empty(shape[0], dtype=np.float64) y_array.fill(float(shape[1]) / 2.) z_array = np.arange(shape[0], dtype=np.float64) # for wavelengths ra, dec, wavelength = wcs(x_array, y_array, z_array) nelem = len(wavelength) ra = ra[nelem // 2] dec = dec[nelem // 2] else: (ra, dec) = (0., 0.) wavelength = np.arange(1, shape[0] + 1, dtype=np.float64) position = (x_center, y_center) if source_type == 'point': aperture = CircularAperture(position, r=radius) if subtract_background: annulus = CircularAnnulus(position, r_in=inner_bkg, r_out=outer_bkg) normalization = aperture.area() / annulus.area() else: aperture = RectangularAperture(position, width, height, theta) # No background is computed for an extended source. for k in range(shape[0]): phot_table = aperture_photometry(data[k, :, :], aperture, method=method, subpixels=subpixels) net[k] = float(phot_table['aperture_sum'][0]) if subtract_background: bkg_table = aperture_photometry(data[k, :, :], annulus, method=method, subpixels=subpixels) background[k] = float(bkg_table['aperture_sum'][0]) net[k] = net[k] - background[k] * normalization # Check for NaNs in the wavelength array, flag them in the dq array, # and truncate the arrays if NaNs are found at endpoints (unless the # entire array is NaN). nan_mask = np.isnan(wavelength) n_nan = nan_mask.sum(dtype=np.intp) if n_nan > 0: log.warning("%d NaNs in wavelength array.", n_nan) dq[nan_mask] = np.bitwise_or(dq[nan_mask], dqflags.pixel['DO_NOT_USE']) not_nan = np.logical_not(nan_mask) flag = np.where(not_nan) if len(flag[0]) > 0: n_trimmed = flag[0][0] + nelem - (flag[0][-1] + 1) if n_trimmed > 0: log.info("Output arrays have been trimmed by %d elements", n_trimmed) slc = slice(flag[0][0], flag[0][-1] + 1) wavelength = wavelength[slc] net = net[slc] background = background[slc] dq = dq[slc] else: dq |= dqflags.pixel['DO_NOT_USE'] return (ra, dec, wavelength, net, background, dq)
def do_detection(self): """Flag outlier pixels in DQ of input images.""" self.build_suffix(**self.outlierpars) self._convert_inputs() pars = self.outlierpars save_intermediate_results = pars['save_intermediate_results'] # Start by performing initial TSO Photometry on stack of DataModels # TODO: need information about the actual source position in # TSO imaging mode (for all subarrays). # Meanwhile, this is a placeholder representing the geometric # center of the image. nints, ny, nx = self.inputs.data.shape xcenter = (ny - 1) / 2. ycenter = (ny - 1) / 2. # all radii are in pixel units if self.inputs.meta.instrument.pupil == 'WLP8': radius = 50 radius_inner = 60 radius_outer = 70 else: radius = 3 radius_inner = 4 radius_outer = 5 apertures = CircularAperture((xcenter, ycenter), r=radius) aperture_mask = apertures.to_mask(method='center')[0] # This mask has 1 for mask region, 0 for outside of mask median_mask = aperture_mask.to_image((ny, nx)) inv_median_mask = np.abs(median_mask - 1) # Perform photometry catalog = tso_aperture_photometry(self.inputs, xcenter, ycenter, radius, radius_inner, radius_outer) # Extract net photometry for the source # This will be the value used for scaling the median image within # the aperture region phot_values = catalog['net_aperture_sum'] # Convert CubeModel into ModelContainer of 2-D DataModels for image in self.input_models: image.wht = resample_utils.build_driz_weight( image, weight_type='exptime', good_bits=pars['good_bits'] ) # Initialize intermediate products used in the outlier detection input_shape = self.input_models[0].data.shape median_model = datamodels.ImageModel(init=input_shape) median_model.meta = deepcopy(self.input_models[0].meta) base_filename = self.inputs.meta.filename median_model.meta.filename = self.make_output_path( basepath=base_filename, suffix='median' ) # Perform median combination on set of drizzled mosaics median_model.data = self.create_median(self.input_models) aper2 = CircularAnnulus((xcenter, ycenter), r_in=radius_inner, r_out=radius_outer) tbl1 = aperture_photometry(median_model.data, apertures, error=median_model.data * 0.0 + 1.0) tbl2 = aperture_photometry(median_model.data, aper2, error=median_model.data * 0.0 + 1.0) aperture_sum = u.Quantity(tbl1['aperture_sum'][0]) annulus_sum = u.Quantity(tbl2['aperture_sum'][0]) annulus_mean = annulus_sum / aper2.area() aperture_bkg = annulus_mean * apertures.area() median_phot_value = aperture_sum - aperture_bkg if save_intermediate_results: log.info("Writing out MEDIAN image to: {}".format( median_model.meta.filename)) median_model.save(median_model.meta.filename) # Scale the median image by the initial photometry (only in aperture) # to create equivalent of 'blot' images # Area outside of aperture in median will remain unchanged blot_models = datamodels.ModelContainer() for i in range(nints): scale_factor = float(phot_values[i] / median_phot_value) scaled_image = datamodels.ImageModel(init=median_model.data.shape) scaled_image.meta = deepcopy(median_model.meta) scaled_data = (median_model.data * (scale_factor * median_mask) + ( median_model.data * inv_median_mask)) scaled_image.data = scaled_data blot_models.append(scaled_image) if save_intermediate_results: log.info("Writing out Scaled Median images...") def make_output_path(ignored, idx=None): output_path = self.make_output_path( basepath=base_filename, suffix='blot', idx=idx, component_format='_{asn_id}_{idx}' ) return output_path blot_models.save(make_output_path) # Perform outlier detection using statistical comparisons between # each original input image and its blotted version of the median image self.detect_outliers(blot_models) # clean-up (just to be explicit about being finished # with these results) del median_model, blot_models
annulus_apertures = CircularAnnulus( (result['source_x'], result['source_y']), r_in=apr + 1.0, r_out=2.0 * apr + 1.0) # Can make it nice like http://docs.astropy.org/en/stable/visualization/wcsaxes/, # save the files for future use. # Do aperture_photometry. Local background subtraction version, # http://photutils.readthedocs.io/en/stable/aperture.html#local-background-subtraction # For an estimate of the error, assume all errors are Poisson and that # the sky/source signal dominates the error. apers = [apertures, annulus_apertures] phot_table = aperture_photometry(data, apers, error=np.sqrt(data.copy())) bkg_mean = phot_table['aperture_sum_1'] / annulus_apertures.area() bkg_sum = bkg_mean * apertures.area() final_sum = phot_table['aperture_sum_0'] - bkg_sum phot_table['residual_aperture_sum'] = final_sum # Based on error propagation calculation by Nicole. phot_table['residual_aperture_sum_err'] = np.sqrt( phot_table['aperture_sum_err_0']**2 + (apertures.area() / annulus_apertures.area() * phot_table['aperture_sum_err_1'])**2) # Put this in instrumental magnitude. phot_table['instrmag'] = 2.5 * np.log10( phot_table['residual_aperture_sum'] / hdu[0].header['EXPTIME']) # Based on error propagation calculation by Nicole. phot_table['instrmag_err'] = 1.085 * phot_table[ 'residual_aperture_sum_err'] / phot_table['residual_aperture_sum'] # Add this to the results table. Add errors as well.
def extract_ifu(input_model, source_type, extract_params): """This function does the extraction. Parameters ---------- input_model: IFUCubeModel The input model. source_type: string "point" or "extended" extract_params: dict The extraction parameters for aperture photometry. Returns ------- (ra, dec, wavelength, net, background, dq) """ data = input_model.data shape = data.shape if len(shape) != 3: log.error("Expected a 3-D IFU cube; dimension is %d.", len(shape)) raise RuntimeError("The IFU cube should be 3-D.") # We need to allocate net, background, and dq arrays no matter what. net = np.zeros(shape[0], dtype=np.float64) background = np.zeros(shape[0], dtype=np.float64) dq = np.zeros(shape[0], dtype=np.int32) x_center = extract_params['x_center'] y_center = extract_params['y_center'] if x_center is None: x_center = float(shape[2]) / 2. else: x_center = float(x_center) if y_center is None: y_center = float(shape[1]) / 2. else: y_center = float(y_center) method = extract_params['method'] # subpixels is only needed if method = 'subpixel'. subpixels = extract_params['subpixels'] subtract_background = extract_params['subtract_background'] smaller_axis = float(min(shape[1], shape[2])) # for defaults if source_type == 'point': radius = extract_params['radius'] if radius is None: radius = smaller_axis / 4. if subtract_background: inner_bkg = extract_params['inner_bkg'] if inner_bkg is None: inner_bkg = radius outer_bkg = extract_params['outer_bkg'] if outer_bkg is None: outer_bkg = min(inner_bkg * math.sqrt(2.), smaller_axis / 2. - 1.) if inner_bkg <= 0. or outer_bkg <= 0. or inner_bkg >= outer_bkg: log.debug("Turning background subtraction off, due to " "the values of inner_bkg and outer_bkg.") subtract_background = False width = None height = None theta = None else: width = extract_params['width'] if width is None: width = smaller_axis / 2. height = extract_params['height'] if height is None: height = smaller_axis / 2. theta = extract_params['theta'] * math.pi / 180. radius = None subtract_background = False inner_bkg = None outer_bkg = None log.debug("IFU 1-D extraction parameters:") log.debug(" x_center = %s", str(x_center)) log.debug(" y_center = %s", str(y_center)) if source_type == 'point': log.debug(" radius = %s", str(radius)) log.debug(" subtract_background = %s", str(subtract_background)) log.debug(" inner_bkg = %s", str(inner_bkg)) log.debug(" outer_bkg = %s", str(outer_bkg)) log.debug(" method = %s", method) if method == "subpixel": log.debug(" subpixels = %s", str(subpixels)) else: log.debug(" width = %s", str(width)) log.debug(" height = %s", str(height)) log.debug(" theta = %s degrees", str(extract_params['theta'])) log.debug(" subtract_background = %s", str(subtract_background)) log.debug(" method = %s", method) if method == "subpixel": log.debug(" subpixels = %s", str(subpixels)) # Check for out of bounds. # The problem with having the background aperture extend beyond the # image is that the normalization would not account for the resulting # decrease in the area of the annulus, so the background subtraction # would be systematically low. outside = False f_nx = float(shape[2]) f_ny = float(shape[1]) if x_center < 0. or x_center >= f_nx - 1. or \ y_center < 0. or y_center >= f_ny - 1.: outside = True log.error("Target location is outside the image.") if subtract_background and \ (x_center - outer_bkg < -0.5 or x_center + outer_bkg > f_nx - 0.5 or y_center - outer_bkg < -0.5 or y_center + outer_bkg > f_ny - 0.5): outside = True log.error("Background region extends outside the image.") if outside: (ra, dec) = (0., 0.) wavelength = np.zeros(shape[0], dtype=np.float64) dq[:] = dqflags.pixel['DO_NOT_USE'] return (ra, dec, wavelength, net, background, dq) # all bad if hasattr(input_model.meta, 'wcs'): wcs = input_model.meta.wcs else: log.warning("WCS function not found in input.") wcs = None if wcs is not None: x_array = np.empty(shape[0], dtype=np.float64) x_array.fill(float(shape[2]) / 2.) y_array = np.empty(shape[0], dtype=np.float64) y_array.fill(float(shape[1]) / 2.) z_array = np.arange(shape[0], dtype=np.float64) # for wavelengths ra, dec, wavelength = wcs(x_array, y_array, z_array) nelem = len(wavelength) ra = ra[nelem // 2] dec = dec[nelem // 2] else: (ra, dec) = (0., 0.) wavelength = np.arange(1, shape[0] + 1, dtype=np.float64) position = (x_center, y_center) if source_type == 'point': aperture = CircularAperture(position, r=radius) if subtract_background: annulus = CircularAnnulus(position, r_in=inner_bkg, r_out=outer_bkg) normalization = aperture.area() / annulus.area() else: aperture = RectangularAperture(position, width, height, theta) # No background is computed for an extended source. for k in range(shape[0]): phot_table = aperture_photometry(data[k, :, :], aperture, method=method, subpixels=subpixels) net[k] = float(phot_table['aperture_sum'][0]) if subtract_background: bkg_table = aperture_photometry(data[k, :, :], annulus, method=method, subpixels=subpixels) background[k] = float(bkg_table['aperture_sum'][0]) net[k] = net[k] - background[k] * normalization # Check for NaNs in the wavelength array, flag them in the dq array, # and truncate the arrays if NaNs are found at endpoints (unless the # entire array is NaN). nan_mask = np.isnan(wavelength) n_nan = nan_mask.sum(dtype=np.intp) if n_nan > 0: log.warning("%d NaNs in wavelength array.", n_nan) dq[nan_mask] = np.bitwise_or(dq[nan_mask], dqflags.pixel['DO_NOT_USE']) not_nan = np.logical_not(nan_mask) flag = np.where(not_nan) if len(flag[0]) > 0: n_trimmed = flag[0][0] + nelem - (flag[0][-1] + 1) if n_trimmed > 0: log.info("Output arrays have been trimmed by %d elements", n_trimmed) slc = slice(flag[0][0], flag[0][-1] + 1) wavelength = wavelength[slc] net = net[slc] background = background[slc] dq = dq[slc] else: dq |= dqflags.pixel['DO_NOT_USE'] return (ra, dec, wavelength, net, background, dq)
def do_detection(self): """Flag outlier pixels in DQ of input images.""" self.build_suffix(**self.outlierpars) self._convert_inputs() pars = self.outlierpars save_intermediate_results = pars['save_intermediate_results'] # Start by performing initial TSO Photometry on stack of DataModels # TODO: need information about the actual source position in # TSO imaging mode (for all subarrays). # Meanwhile, this is a placeholder representing the geometric # center of the image. nints, ny, nx = self.inputs.data.shape xcenter = (ny - 1) / 2. ycenter = (ny - 1) / 2. # all radii are in pixel units if self.inputs.meta.instrument.pupil == 'WLP8': radius = 50 radius_inner = 60 radius_outer = 70 else: radius = 3 radius_inner = 4 radius_outer = 5 apertures = CircularAperture((xcenter, ycenter), r=radius) aperture_mask = apertures.to_mask(method='center')[0] # This mask has 1 for mask region, 0 for outside of mask median_mask = aperture_mask.to_image((ny, nx)) inv_median_mask = np.abs(median_mask - 1) # Perform photometry catalog = tso_aperture_photometry(self.inputs, xcenter, ycenter, radius, radius_inner, radius_outer) # Extract net photometry for the source # This will be the value used for scaling the median image within # the aperture region phot_values = catalog['net_aperture_sum'] # Convert CubeModel into ModelContainer of 2-D DataModels for image in self.input_models: image.wht = resample_utils.build_driz_weight( image, weight_type='exptime', good_bits=pars['good_bits']) # Initialize intermediate products used in the outlier detection input_shape = self.input_models[0].data.shape median_model = datamodels.ImageModel(init=input_shape) median_model.meta = deepcopy(self.input_models[0].meta) base_filename = self.inputs.meta.filename median_model.meta.filename = self.make_output_path( basepath=base_filename, suffix='median') # Perform median combination on set of drizzled mosaics median_model.data = self.create_median(self.input_models) aper2 = CircularAnnulus((xcenter, ycenter), r_in=radius_inner, r_out=radius_outer) tbl1 = aperture_photometry(median_model.data, apertures, error=median_model.data * 0.0 + 1.0) tbl2 = aperture_photometry(median_model.data, aper2, error=median_model.data * 0.0 + 1.0) aperture_sum = u.Quantity(tbl1['aperture_sum'][0]) annulus_sum = u.Quantity(tbl2['aperture_sum'][0]) annulus_mean = annulus_sum / aper2.area() aperture_bkg = annulus_mean * apertures.area() median_phot_value = aperture_sum - aperture_bkg if save_intermediate_results: log.info("Writing out MEDIAN image to: {}".format( median_model.meta.filename)) median_model.save(median_model.meta.filename) # Scale the median image by the initial photometry (only in aperture) # to create equivalent of 'blot' images # Area outside of aperture in median will remain unchanged blot_models = datamodels.ModelContainer() for i in range(nints): scale_factor = float(phot_values[i] / median_phot_value) scaled_image = datamodels.ImageModel(init=median_model.data.shape) scaled_image.meta = deepcopy(median_model.meta) scaled_data = (median_model.data * (scale_factor * median_mask) + (median_model.data * inv_median_mask)) scaled_image.data = scaled_data blot_models.append(scaled_image) if save_intermediate_results: log.info("Writing out Scaled Median images...") def make_output_path(ignored, idx=None): output_path = self.make_output_path( basepath=base_filename, suffix='blot', idx=idx, component_format='_{asn_id}_{idx}') return output_path blot_models.save(make_output_path) # Perform outlier detection using statistical comparisons between # each original input image and its blotted version of the median image self.detect_outliers(blot_models) # clean-up (just to be explicit about being finished # with these results) del median_model, blot_models
phot_table = aperture_photometry(data, [aperture, annulus]) #new background estimation bkg_mask = np.ma.masked_outside(radial[0].reshape(new_image.shape), inner_sky_radius, outer_sky_radius) bkg_map = Background2D(new_image, tuple(np.array(new_image.shape) / 4), mask=bkg_mask.mask, exclude_mesh_method='all') bkg_map_med = MMMBackground().calc_background( bkg_map.data) #bkg_map_med=np.median(bkg_map.background) #print 'Map sky mean '+str(bkg_map_med) #bkg_mean=phot_table['aperture_sum_1']/annulus.area() #print 'Aperture sky mean '+str(bkg_mean) #phot_table['residual_aperture_sum']=phot_table['aperture_sum_0']-bkg_mean*aperture.area() phot_table['residual_aperture_sum'] = phot_table[ 'aperture_sum_0'] - bkg_map_med * aperture.area() #print 'Map sky result: '+str(phot_table['aperture_sum_0']-bkg_map_med*aperture.area()) #print "Aperture Photometry Result: "+str(phot_table['residual_aperture_sum']) fig2 = Figure(figsize=(int(6 * scaling), int(6 * scaling))) fig2.set_facecolor('0.85') ax2 = fig2.add_axes([0.1, 0.1, .9, .9]) ax2.grid(True, color='white', linestyle='-', linewidth=1) ax2.set_axisbelow(True) ax2.scatter(radial[0], radial[1]) ax2.plot(np.linspace(0, new_image_halfwidth, num=50), gauss1d(np.linspace(0, new_image_halfwidth, num=50), popt[0], 0, np.mean([popt[3], popt[4]]), popt[6]), c='k', lw=2) #guess=(np.amax(new_image)-new_image[0,0],x,y,3,3,0,new_image[0,0])
def addzb(fitsname, redo=False, fau_dir=None): telescopes = ['1','2','3','4'] # night = (fitsname.split('/'))[3] night = (fitsname.split('/'))[-2] print 'beginning ' + fitsname # 2D spectrum h = pyfits.open(fitsname,mode='update') if 'BARYSRC4' in h[0].header.keys() and not redo: print fitsname + " already done" h.flush() h.close() return specstart = datetime.datetime.strptime(h[0].header['DATE-OBS'],"%Y-%m-%dT%H:%M:%S.%f") specmid = specstart + datetime.timedelta(seconds = h[0].header['EXPTIME']/2.0) specend = specstart + datetime.timedelta(seconds = h[0].header['EXPTIME']) t0 = datetime.datetime(2000,1,1) t0jd = 2451544.5 aperture_radius = 3.398 # fiber radius in pixels annulus_inner = 2.0*aperture_radius annulus_outer = 3.0*aperture_radius for telescope in telescopes: print 'beginning telescope ' + telescope + ' on ' + fitsname # get the barycentric redshift for each time ra = h[0].header['TARGRA' + telescope] dec = h[0].header['TARGDEC' + telescope] try: pmra = h[0].header['PMRA' + telescope] except: pmra = 0.0 try: pmdec = h[0].header['PMDEC' + telescope] except: pmdec = 0.0 try: parallax = h[0].header['PARLAX' + telescope] except: parallax = 0.0 try: rv = h[0].header['RV' + telescope] except: rv = 0.0 objname = h[0].header['OBJECT' + telescope] if fau_dir is None: faupath = '/Data/t' + telescope + '/' + night + '/' + night + '.T' + telescope + '.FAU.' + objname + '.????.fits' else: faupath = fau_dir + '/t' + telescope + '/' + night + '/' + night + '.T' + telescope + '.FAU.' + objname + '.????.fits' guideimages = glob.glob(faupath) # if telescope == '2' and "HD62613" in fitsname: ipdb.set_trace() times = [] fluxes = np.array([]) for guideimage in guideimages: try: fauimage = pyfits.open(guideimage) except: print "corrupt file for " + guideimage continue # midtime of the guide image (UTC) midtime = datetime.datetime.strptime(fauimage[0].header['DATE-OBS'],"%Y-%m-%dT%H:%M:%S") +\ datetime.timedelta(seconds=fauimage[0].header['EXPTIME']/2.0) # convert to Julian date midjd = t0jd + (midtime-t0).total_seconds()/86400.0 # only look at images during the spectrum if midtime < specstart or midtime > specend: continue # find the fiber position try: fiber_x = fauimage[0].header['XFIBER' + telescope] fiber_y = fauimage[0].header['YFIBER' + telescope] except: print "keywords missing for " + guideimage continue # do aperture photometry positions = [(fiber_x,fiber_y)] apertures = CircularAperture(positions,r=aperture_radius) annulus_apertures = CircularAnnulus(positions, r_in=annulus_inner, r_out=annulus_outer) # calculate the background-subtracted flux at the fiber position rawflux_table = aperture_photometry(fauimage[0].data, apertures) bkgflux_table = aperture_photometry(fauimage[0].data, annulus_apertures) bkg_mean = bkgflux_table['aperture_sum'].sum() / annulus_apertures.area() bkg_sum = bkg_mean * apertures.area() flux = rawflux_table['aperture_sum'].sum() - bkg_sum # append to the time and flux arrays times.append(midjd) fluxes = np.append(fluxes,flux) if len(times) == 0: print "No guider images for " + fitsname + " on Telescope " + telescope + "; assuming mid time" if 'daytimeSky' in fitsname: h[0].header['BARYCOR' + telescope] = ('UNKNOWN','Barycentric redshift') h[0].header['BARYSRC' + telescope] = ('UNKNOWN','Source for the barycentric redshift') h[0].header['FLUXMID' + telescope] = (midjd,'Flux-weighted mid exposure time (JD_UTC)') continue # convert specmid to Julian date midjd = t0jd + (specmid-t0).total_seconds()/86400.0 print midjd zb = barycorr(midjd, ra, dec, pmra=pmra, pmdec=pmdec, parallax=parallax, rv=rv)/2.99792458e8 h[0].header['BARYCOR' + telescope] = (zb,'Barycentric redshift') h[0].header['BARYSRC' + telescope] = ('MIDTIME','Source for the barycentric redshift') h[0].header['FLUXMID' + telescope] = (midjd,'Flux-weighted mid exposure time (JD_UTC)') continue zb = np.asarray(barycorr(times, ra, dec, pmra=pmra, pmdec=pmdec, parallax=parallax, rv=rv))/2.99792458e8 # weight the barycentric correction by the flux #*******************!*!*!*!*!*!* #this assumes guider images were taken ~uniformly throughout the spectroscopic exposure! #****************!*!*!*!*!**!*!*!*!**!*!*!*!* wzb = np.sum(zb*fluxes)/np.sum(fluxes) wmidjd = np.sum(times*fluxes)/np.sum(fluxes) # update the header to include aperture photometry and barycentric redshift h[0].header['BARYCOR' + telescope] = (wzb,'Barycentric redshift') h[0].header['BARYSRC' + telescope] = ('FAU Flux Weighted','Source for the barycentric redshift') h[0].header['FLUXMID' + telescope] = (wmidjd,'Flux-weighted mid exposure time (JD_UTC)') hdu = pyfits.PrimaryHDU(zip(times,fluxes)) hdu.header['TELESCOP'] = ('T' + telescope,'Telescope') h.append(hdu) # write updates to the disk h.flush() h.close()
def tso_aperture_photometry(datamodel, xcenter, ycenter, radius, radius_inner, radius_outer): """ Create a photometric catalog for NIRCam TSO imaging observations. Parameters ---------- datamodel : `CubeModel` The input `CubeModel` of a NIRCam TSO imaging observation. xcenter, ycenter : float The ``x`` and ``y`` center of the aperture. radius : float The radius (in pixels) of the circular aperture. radius_inner, radius_outer : float The inner and outer radii (in pixels) of the circular-annulus aperture, used for local background estimation. Returns ------- catalog : `~astropy.table.QTable` An astropy QTable (Quantity Table) containing the source photometry. """ if not isinstance(datamodel, CubeModel): raise ValueError('The input data model must be a CubeModel.') aper1 = CircularAperture((xcenter, ycenter), r=radius) aper2 = CircularAnnulus((xcenter, ycenter), r_in=radius_inner, r_out=radius_outer) nimg = datamodel.data.shape[0] aperture_sum = [] aperture_sum_err = [] annulus_sum = [] annulus_sum_err = [] for i in np.arange(nimg): tbl1 = aperture_photometry(datamodel.data[i, :, :], aper1, error=datamodel.err[i, :, :]) tbl2 = aperture_photometry(datamodel.data[i, :, :], aper2, error=datamodel.err[i, :, :]) aperture_sum.append(tbl1['aperture_sum'][0]) aperture_sum_err.append(tbl1['aperture_sum_err'][0]) annulus_sum.append(tbl2['aperture_sum'][0]) annulus_sum_err.append(tbl2['aperture_sum_err'][0]) # convert array of Quantities to Quantity arrays aperture_sum = u.Quantity(aperture_sum) aperture_sum_err = u.Quantity(aperture_sum_err) annulus_sum = u.Quantity(annulus_sum) annulus_sum_err = u.Quantity(annulus_sum_err) # construct metadata for output table meta = OrderedDict() meta['instrument'] = datamodel.meta.instrument.name meta['detector'] = datamodel.meta.instrument.detector meta['channel'] = datamodel.meta.instrument.channel meta['subarray'] = datamodel.meta.subarray.name meta['filter'] = datamodel.meta.instrument.filter meta['pupil'] = datamodel.meta.instrument.pupil meta['target_name'] = datamodel.meta.target.catalog_name meta['xcenter'] = xcenter meta['ycenter'] = ycenter ra_icrs, dec_icrs = datamodel.meta.wcs(xcenter, ycenter) meta['ra_icrs'] = ra_icrs meta['dec_icrs'] = dec_icrs info = ('Photometry measured in a circular aperture of r={0} pixels. ' 'Background calculated as the mean in a circular annulus with ' 'r_inner={1} pixels and r_outer={2} pixels.' .format(radius, radius_inner, radius_outer)) meta['apertures'] = info tbl = QTable(meta=meta) dt = (datamodel.meta.exposure.group_time * (datamodel.meta.exposure.ngroups + 1)) dt_arr = (np.arange(1, 1 + datamodel.meta.exposure.nints) * dt - (dt / 2.)) int_dt = TimeDelta(dt_arr, format='sec') int_times = (Time(datamodel.meta.exposure.start_time, format='mjd') + int_dt) tbl['MJD'] = int_times.mjd tbl['aperture_sum'] = aperture_sum tbl['aperture_sum_err'] = aperture_sum_err tbl['annulus_sum'] = annulus_sum tbl['annulus_sum_err'] = annulus_sum_err annulus_mean = annulus_sum / aper2.area() annulus_mean_err = annulus_sum_err / aper2.area() tbl['annulus_mean'] = annulus_mean tbl['annulus_mean_err'] = annulus_mean_err aperture_bkg = annulus_mean * aper1.area() aperture_bkg_err = annulus_mean_err * aper1.area() tbl['aperture_bkg'] = aperture_bkg tbl['aperture_bkg_err'] = aperture_bkg_err net_aperture_sum = aperture_sum - aperture_bkg net_aperture_sum_err = np.sqrt(aperture_sum_err ** 2 + aperture_bkg_err ** 2) tbl['net_aperture_sum'] = net_aperture_sum tbl['net_aperture_sum_err'] = net_aperture_sum_err return tbl
def fors2_pol_phot(folder_path,apermul): """ Script runs photometry for FORS2 polarimetry images """ # Read in four wave plate angle files and set up arrays for later file_ang0 = os.path.join(folder_path,'0ang.fits') file_ang225 = os.path.join(folder_path,'225ang.fits') file_ang45 = os.path.join(folder_path,'45ang.fits') file_ang675 = os.path.join(folder_path,'675ang.fits') files = [file_ang0,file_ang225,file_ang45,file_ang675] angle = ['0','225','45','675'] ang_dec = ['0','22.5','45','67.5'] label = ['$0^{\circ}$ image','$22.5^{\circ}$ image', '$45^{\circ}$ image','$67.5^{\circ}$ image'] # Set up array to store FWHM and number of sources per half-wave plate fwhm = [] numsource = [] # Loop over files for the four wave plate files for k in range(0,len(angle),1): # Open fits file, extract pixel flux data and remove saturated pixels try: hdulist = fits.open(files[k]) image_data = hdulist[0].data except FileNotFoundError as e: print("Cannot find the fits file(s) you are looking for!") print("Please check the input!") sys.exit() # Remove bad pixels and mask edges image_data[image_data > 60000] = 0 image_data[image_data < 0] = 0 rows = len(image_data[:,0]) cols = len(image_data[0,:]) hdulist.close() # Calculate estimate of background using sigma-clipping and detect # the sources using DAOStarFinder xord, yord = 843, 164 xexord, yexord = 843, 72 go_bmean, go_bmedian, go_bstd = sigma_clipped_stats(image_data [yord-40:yord+40,xord-40:xord+40],sigma=3.0,iters=5) ge_bmean, ge_bmedian, ge_bstd = sigma_clipped_stats(image_data [yexord-40:yexord+40,xord-40:xord+40],sigma=3.0,iters=5) daofind_o = DAOStarFinder(fwhm=5,threshold=5*go_bstd, exclude_border=True) daofind_e = DAOStarFinder(fwhm=5,threshold=5*ge_bstd, exclude_border=True) sources_o = daofind_o(image_data[yord-20:yord+20,xord-20:xord+20]) sources_e = daofind_e(image_data[yexord-20:yexord+20,xexord-20: xexord+20]) if (len(sources_o) < 1 or len(sources_e) < 1): print("No source detected in",ang_dec[k],"degree image") sys.exit() if len(sources_o) != len(sources_e): print("Unequal number of sources detected in o and e images!") sys.exit() glob_bgm = [go_bmean,ge_bmean] glob_bgerr = [go_bstd,ge_bstd] # Convert the source centroids back into detector pixels sources_o['xcentroid'] = sources_o['xcentroid'] + xord - 20 sources_o['ycentroid'] = sources_o['ycentroid'] + yord - 20 sources_e['xcentroid'] = sources_e['xcentroid'] + xexord - 20 sources_e['ycentroid'] = sources_e['ycentroid'] + yexord - 20 # Estimate the FWHM of the source by simulating a 2D Gaussian # (only done on 0 angle image ensuring aperture sizes are equal) if not fwhm: xpeaks_o = [] xpeaks_e = [] ypeaks_o = [] ypeaks_e = [] for i in range(0,len(sources_o),1): data_o = image_data[yord-20:yord+20,xord-20:xord+20] xpeaks_o.append(int(sources_o[i]['xcentroid']) - (xord - 20)) ypeaks_o.append(int(sources_o[i]['ycentroid']) - (yord - 20)) data_e = image_data[yexord-20:yexord+20,xexord-20:xexord+20] xpeaks_e.append(int(sources_e[i]['xcentroid']) - (xexord - 20)) ypeaks_e.append(int(sources_e[i]['ycentroid']) - (yexord - 20)) min_count_o = np.min(data_o) min_count_e = np.min(data_e) max_count_o = data_o[ypeaks_o[i],xpeaks_e[i]] max_count_e = data_e[ypeaks_o[i],xpeaks_e[i]] half_max_o = (max_count_o + min_count_o)/2 half_max_e = (max_count_e + min_count_e)/2 # Crude calculation for each source nearest_above_x_o = ((np.abs(data_o[ypeaks_o[i], xpeaks_o[i]:-1] - half_max_o)).argmin()) nearest_below_x_o = ((np.abs(data_o[ypeaks_o[i],0: xpeaks_o[i]] - half_max_o)).argmin()) nearest_above_x_e = ((np.abs(data_e[ypeaks_e[i], xpeaks_e[i]:-1] - half_max_e)).argmin()) nearest_below_x_e = ((np.abs(data_e[ypeaks_e[i],0: xpeaks_e[i]] - half_max_e)).argmin()) nearest_above_y_o = ((np.abs(data_o[ypeaks_o[i]:-1, xpeaks_o[i]] - half_max_o)).argmin()) nearest_below_y_o = ((np.abs(data_o[0:ypeaks_o[i], xpeaks_o[i]] - half_max_o)).argmin()) nearest_above_y_e = ((np.abs(data_e[ypeaks_e[i]:-1, xpeaks_e[i]] - half_max_e)).argmin()) nearest_below_y_e = ((np.abs(data_e[0:ypeaks_e[i], xpeaks_e[i]] - half_max_e)).argmin()) fwhm.append((nearest_above_x_o + (xpeaks_o[i] - nearest_below_x_o))) fwhm.append((nearest_above_y_o + (ypeaks_o[i] - nearest_below_y_o))) fwhm.append((nearest_above_x_e + (xpeaks_e[i] - nearest_below_x_e))) fwhm.append((nearest_above_y_e + (ypeaks_e[i] - nearest_below_y_e))) fwhm = np.mean(fwhm) # Stack both ord and exord sources together tot_sources = vstack([sources_o,sources_e]) # Store the ordinary and extraordinary beam source images and # create apertures for aperture photometry positions = np.swapaxes(np.array((tot_sources['xcentroid'], tot_sources['ycentroid']),dtype='float'),0,1) aperture = CircularAperture(positions, r=0.5*apermul*fwhm) phot_table = aperture_photometry(image_data,aperture) # Set up arrays of ord and exord source parameters s_id = np.zeros([len(np.array(phot_table['id']))]) xp = np.zeros([len(s_id)]) yp = np.zeros([len(s_id)]) fluxbgs = np.zeros([len(s_id)]) mean_bg = np.zeros([len(s_id)]) bg_err = np.zeros([len(s_id)]) s_area = [] ann_area = [] for i in range(0,len(np.array(phot_table['id'])),1): s_id[i] = np.array(phot_table['id'][i]) xpos = np.array(phot_table['xcenter'][i]) ypos = np.array(phot_table['ycenter'][i]) xp[i] = xpos yp[i] = ypos s_area.append(np.pi*(0.5*apermul*fwhm)**2) j = i%2 fluxbgs[i] = (phot_table['aperture_sum'][i] - aperture.area()*glob_bgm[j]) mean_bg[i] = glob_bgm[j] bg_err[i] = glob_bgerr[j] ann_area.append(80*80) # Create and save the image in z scale and overplot the ordinary and # extraordinary apertures and local background annuli if applicable fig = plt.figure() zscale = ZScaleInterval(image_data) norm = ImageNormalize(stretch=SqrtStretch(),interval=zscale) image = plt.imshow(image_data,cmap='gray',origin='lower',norm=norm) bg_annulus_o = RectangularAnnulus((843,159),w_in=0,w_out=80,h_out=80, theta=0) bg_annulus_e = RectangularAnnulus((843,69),w_in=0,w_out=80,h_out=80, theta=0) bg_annulus_o.plot(color='skyblue',lw=1.5,alpha=0.5) bg_annulus_e.plot(color='lightgreen',lw=1.5,alpha=0.5) for i in range(0,len(np.array(phot_table['id'])),1): aperture = CircularAperture((xp[i],yp[i]),r=0.5*apermul*fwhm) if i < int(len(np.array(phot_table['id']))/2): aperture.plot(color='blue',lw=1.5,alpha=0.5) else: aperture.plot(color='green',lw=1.5,alpha=0.5) plt.xlim(760,920) plt.ylim(20,210) plt.title(label[k]) image_fn = folder_path + angle[k] + '_image.png' fig.savefig(image_fn) # Write ordinary and extraordinary beams to file following the # convention angleXXX_ord.txt and angleXXX_exord.txt orig_stdout = sys.stdout ord_result_file= folder_path + 'angle' + angle[k] + '_ord.txt' ordresultf = open(ord_result_file, 'w') sys.stdout = ordresultf print("# id, xpix, ypix, fluxbgs, sourcearea, meanbg, bgerr, bgarea") for i in range(0,int(len(np.array(phot_table['id']))/2),1): print(i+1,xp[i],yp[i],fluxbgs[i],s_area[i],mean_bg[i],bg_err[i], ann_area[i]) sys.stdout = orig_stdout ordresultf.close() orig_stdout = sys.stdout exord_result_file = folder_path + 'angle' + angle[k] + '_exord.txt' exordresultf = open(exord_result_file, 'w') sys.stdout = exordresultf print("# id, xpix, ypix, fluxbgs, sourcearea, meanbg, bgerr, bgarea") for i in range(int(len(np.array(phot_table['id']))/2),len(np.array (phot_table['id'])),1): print(i+1-int(len(np.array(phot_table['id']))/2),xp[i],yp[i], fluxbgs[i],s_area[i],mean_bg[i],bg_err[i],ann_area[i]) sys.stdout = orig_stdout exordresultf.close() # Save the number of sources in each beam to a list numsource.append(int(len(np.array(phot_table['id']))/2)) # Print number of sources per half-wave plate image for i in range(0,len(numsource),1): print("No of sources detected at",ang_dec[i],"degrees:",numsource[i]) return 0
iraf.ptools.txdump(textfiles='mag_test_g.dat', fields="id,mag,merr,sum,msky,stdev,rapert,xcen,ycen,ifilter,xairmass,image", expr='yes', headers='no', Stdout=txdump_out) txdump_out.close() g_iraf, ge_iraf, gf_iraf, gsky_iraf = np.loadtxt('phot_test_g.txdump', usecols=(1,2,3,4), unpack=True) i_iraf, ie_iraf, if_iraf, isky_iraf = np.loadtxt('phot_test_i.txdump', usecols=(1,2,3,4), unpack=True) # now try python x, y = np.loadtxt(coords_file, usecols=(0,1), unpack=True) positions = np.array(zip(x,y)) hdu_g = fits.open(fits_g) hdu_i = fits.open(fits_i) apertures = CircularAperture(positions, r=8.) annulus_apertures = CircularAnnulus(positions, r_in=10., r_out=14.) print apertures.area() ap_mask = apertures.to_mask(method='subpixel', subpixels=7) dummy = np.ones_like(hdu_g[0].data) ann_mask = annulus_apertures.to_mask(method='center') ap_g = [m.apply(hdu_g[0].data) for i,m in enumerate(ap_mask)] ap_i = [m.apply(hdu_i[0].data) for i,m in enumerate(ap_mask)] area_g = [np.sum(m.apply(dummy)) for i,m in enumerate(ap_mask)] area_i = [np.sum(m.apply(dummy)) for i,m in enumerate(ap_mask)] print area_g, area_i # plt.imshow(ap_g[0], interpolation='nearest') # plt.show() ann_g = [m.apply(hdu_g[0].data, fill_value=-999.) for i,m in enumerate(ann_mask)] ann_i = [m.apply(hdu_i[0].data, fill_value=-999.) for i,m in enumerate(ann_mask)] flux_g = np.array([np.sum(a) for j,a in enumerate(ap_g)])
def extract_ifu(input_model, source_type, extract_params): """This function does the extraction. Parameters ---------- input_model: IFUCubeModel The input model. source_type: string "point" or "extended" extract_params: dict The extraction parameters for aperture photometry. Returns ------- (wavelength, net, background, dq) """ data = input_model.data shape = data.shape if len(shape) != 3: log.error("Expected a 3-D IFU cube; dimension is %d.", len(shape)) raise RuntimeError("The IFU cube should be 3-D.") # We need to allocate net, background, and dq arrays no matter what. net = np.zeros(shape[0], dtype=np.float64) background = np.zeros(shape[0], dtype=np.float64) dq = np.zeros(shape[0], dtype=np.int32) x_center = extract_params['x_center'] y_center = extract_params['y_center'] if x_center is None: x_center = float(shape[2]) / 2. else: x_center = float(x_center) if y_center is None: y_center = float(shape[1]) / 2. else: y_center = float(y_center) method = extract_params['method'] # subpixels is only needed if method = 'subpixel'. subpixels = extract_params['subpixels'] subtract_background = extract_params['subtract_background'] smaller_axis = float(min(shape[1], shape[2])) # for defaults if source_type == 'point': radius = extract_params['radius'] if radius is None: radius = smaller_axis / 4. if subtract_background: inner_bkg = extract_params['inner_bkg'] if inner_bkg is None: inner_bkg = radius outer_bkg = extract_params['outer_bkg'] if outer_bkg is None: outer_bkg = min(inner_bkg * math.sqrt(2.), smaller_axis / 2. - 1.) width = None height = None theta = None else: width = extract_params['width'] if width is None: width = smaller_axis / 2. height = extract_params['height'] if height is None: height = smaller_axis / 2. theta = extract_params['theta'] * math.pi / 180. radius = None inner_bkg = None outer_bkg = None if inner_bkg <= 0. or outer_bkg <= 0. or inner_bkg >= outer_bkg: subtract_background = False log.debug("IFU 1-D extraction parameters:") log.debug(" x_center = %s", str(x_center)) log.debug(" y_center = %s", str(y_center)) if source_type == 'point': log.debug(" radius = %s", str(radius)) log.debug(" subtract_background = %s", str(subtract_background)) log.debug(" inner_bkg = %s", str(inner_bkg)) log.debug(" outer_bkg = %s", str(outer_bkg)) log.debug(" method = %s", method) if method == "subpixel": log.debug(" subpixels = %s", str(subpixels)) else: log.debug(" width = %s", str(width)) log.debug(" height = %s", str(height)) log.debug(" theta = %s degrees", str(extract_params['theta'])) log.debug(" subtract_background = %s", str(subtract_background)) log.debug(" method = %s", method) if method == "subpixel": log.debug(" subpixels = %s", str(subpixels)) # Check for out of bounds. # The problem with having the background aperture extend beyond the # image is that the normalization would not account for the resulting # decrease in the area of the annulus, so the background subtraction # would be systematically low. outside = False f_nx = float(shape[2]) f_ny = float(shape[1]) if x_center < 0. or x_center >= f_nx - 1. or \ y_center < 0. or y_center >= f_ny - 1.: outside = True log.error("Target location is outside the image.") if subtract_background and \ (x_center - outer_bkg < -0.5 or x_center + outer_bkg > f_nx - 0.5 or y_center - outer_bkg < -0.5 or y_center + outer_bkg > f_ny - 0.5): outside = True log.error("Background region extends outside the image.") if outside: wavelength = np.zeros(shape[0], dtype=np.float64) dq[:] = dqflags.pixel['DO_NOT_USE'] return (wavelength, net, background, dq) # all bad wcs = input_model.meta.wcs x_array = np.empty(shape[0], dtype=np.float64) x_array.fill(float(shape[2]) / 2.) y_array = np.empty(shape[0], dtype=np.float64) y_array.fill(float(shape[1]) / 2.) z_array = np.arange(shape[0], dtype=np.float64) # for wavelengths _, _, wavelength = wcs(x_array, y_array, z_array) position = (x_center, y_center) if source_type == 'point': aperture = CircularAperture(position, r=radius) if subtract_background: annulus = CircularAnnulus(position, r_in=inner_bkg, r_out=outer_bkg) normalization = aperture.area() / annulus.area() else: aperture = RectangularAperture(position, width, height, theta) # No background is computed for an extended source. for k in range(shape[0]): phot_table = aperture_photometry(data[k, :, :], aperture, method=method, subpixels=subpixels) net[k] = float(phot_table['aperture_sum'][0]) if subtract_background: bkg_table = aperture_photometry(data[k, :, :], annulus, method=method, subpixels=subpixels) background[k] = float(bkg_table['aperture_sum'][0]) net[k] = net[k] - background[k] * normalization return (wavelength, net, background, dq)
#Lines 35-37 create aperture objects (a circle around each source with a concentric annulus around each circle) and group them in a 2x1 array called apers. apertures = CircularAperture(positions, r=5.) annuli = CircularAnnulus(positions, r_in=6., r_out=8.) apers = [apertures, annuli] #Lines 40-41 replace all negatives in 'data' with , estimate the uncertainty in the number of counts at each pixel, and define that uncertainty as 'error' bkg_err = np.zeros(shape=(500, 500)) gain_eff = 7058 #seconds error = calc_total_error(data, bkg_err, gain_eff) #Line 44 calculates the total number of counts/s in both the center circles and annuli around each source and propogates the uncertainty from each pixel over the entire source (note: each pixel in a drizzled image like this one has information about counts/s, not counts. You can use the observation time as an "effective gain" and multiply each data point by this gain if you desire total counts) phot_table = aperture_photometry(data, apers, error=error) #Lines 47-50 do the math necessary for aperture photometry. Using the annulus, and average background flux rate level is determined (counts/s in annulus divided by area of annulus), which is then multiplied by the area inside the center circle to find the total counts/s due to the background that are inside the circle. That value is subtracted from the measured flux rate inside the circle and you have an accurate estimate of the flux rate of the star itself. Line 48 adds a new column to phot_table with this information. bkg_mean = phot_table['aperture_sum_1'] / annuli.area() bkg_sum = bkg_mean * apertures.area() final_sum = phot_table['aperture_sum_0'] - bkg_sum phot_table['residual_aperture_sum'] = final_sum #Lines 53-56 calculate the total error in the same way as the flux rate in counts/s is calculated above err_mean = phot_table['aperture_sum_err_1'] / annuli.area() err_sum = err_mean * apertures.area() final_err = phot_table['aperture_sum_err_0'] - err_sum phot_table['residual_err_sum'] = final_err #Lines 59-62 save an untruncated text file called 'phot_table.txt' that has columns "id, xcenter pix, ycenter pix, aperture_sum_0 (counts/s in the inner circle), aperture_sum_1 (counts/s in the annulus), aperture_sum_err_0 (total uncertainty from annulus), aperture_sum_err_1 (total uncertainty in center circle), residual_aperture_sum (counts/s due to sources inside the circle), and residual_sum_err (total uncertainty in source flux rate)" flux = phot_table['residual_aperture_sum'] flux[flux < 0] = 0 phot_table = np.array(phot_table) np.set_printoptions(threshold=np.nan) log = open("/Users/computationalphysics/Desktop/phot_table.txt", "w")
def photom_av(ima, pos, radius, r_in=False, r_out=False, mode='median'): ''' Aperture photometry in an aperture located at pixel coordinates pos = ( (x0, y0), (x1, y1), ... ) with a radius=radius. When r_in and r_out are given, background is estimated in CircularAnnulus and subtracted. mode refers to how the background is estimated within the circlar annulus. Can be 'median' or 'mean' Photometry is calculating by median averaging the pixels within the aperture and multiplying by the number of pixels in the aperture (including fractions of pixels). ''' # Setting up the mask if hasattr(ima, 'mask'): if ima.mask.size == 1: mask = np.zeros(ima.shape, dtype=np.bool) | ima.mask else: mask = ima.mask.copy() else: mask = np.zeros(ima.shape, dtype=np.bool) ### Performing the actual photometry - identical for each method # Median averaging of flux in aperture # Setting up the aperture apertures = CircularAperture(pos, r = radius) ap_mask = apertures.to_mask(method='center') # Setting up arrays to store data nflx = len(ap_mask) flx = np.zeros(nflx, dtype=np.float) flux_max = np.zeros(nflx, dtype=np.float) flux_min = np.zeros(nflx, dtype=np.float) # Median averaging of flux for i, am in enumerate(ap_mask): fluxmask = ~mask & am.to_image(shape=mask.shape).astype(np.bool) flx[i] = np.median(ima[fluxmask]) flux_max[i] = np.max(ima[fluxmask]) flux_min[i] = np.min(ima[fluxmask]) # Aperture photometry on mask to see how many masked pixels are in the # aperture apm = aperture_photometry(mask.astype(int), apertures) # Number of unmasked pixels in aperture ap_area = Column(name = 'area_aper', data=apertures.area() - apm['aperture_sum'].data) # Flux in aperture using median av flux and fractional no. pixels in aperture flux_init = flx*ap_area ### Two different modes for analysing the background if ( r_in and r_out and mode in ('mean', 'median') ): ### This stuff is the same regardless of method # Setting up the annulus anulus_apertures = CircularAnnulus(pos, r_in=r_in, r_out=r_out) # Performing annulus photometry on the mask bkgm = aperture_photometry(mask.astype(int), anulus_apertures) # Number of masked pixels in bkg mbkg_area = Column(name = 'bpix_bkg', data=bkgm['aperture_sum']) # Number of non-masked pixels in aperture and bkg bkg_area = Column(name = 'area_bkg', data=anulus_apertures.area() - bkgm['aperture_sum']) ### This stuff is specific to the mean if mode == 'mean': # Perform the annulus photometry on the image bkg = aperture_photometry(ima, anulus_apertures, mask=mask) # Average bkg where this divides by only number of NONMASKED pixels # as the aperture photometry ignores the masked pixels bkga = Column(name='background', data=bkg['aperture_sum']/bkg_area) # Bkg subtracted flux flux = flux_init - bkga*ap_area # Adding that data ap.add_column(bkga) elif mode == 'median': # Number of pixels in the annulus, a different method aperture_mask = anulus_apertures.to_mask(method='center') nbkg = len(aperture_mask) # Background mask bkgm = np.zeros(nbkg, dtype=np.float) # Median averaging for i, am in enumerate(aperture_mask): bmask = ~mask & am.to_image(shape=mask.shape).astype(np.bool) bkgm[i] = np.median(ima[bmask]) flux = flux_init - bkgm*ap_area bkgm = Column(name = 'background', data = bkgm) return flux, apm, flx, ap_area, flux_max, flux_min #flux, no.masked pixels in ap, median av flux
def qphot(data, coord, rad, skyradin, skyradout, wcs=None, calfctr=None, skycoord=None, unit='Jy', error=None, filter=None): if is_pix_coord(coord): coord = [np.float(coord[0]), np.float(coord[1])] aperture = CircularAperture(coord, r=rad) else: coord = SkyCoord(' '.join(coord), unit=(u.hourangle, u.deg), frame='icrs') aperture = SkyCircularAperture(coord, r=rad * u.arcsec) if skycoord: if is_pix_coord(skycoord): scoord = [np.float(skycoord[0]), np.float(skycoord[1])] annulus = CircularAnnulus(scoord, skyradin, skyradout) else: scoord = SkyCoord(' '.join(skycoord), unit=(u.hourangle, u.deg), frame='icrs') annulus = SkyCircularAnnulus(scoord, skyradin * u.arcsec, skyradout * u.arcsec) else: if isinstance(coord, SkyCoord): annulus = SkyCircularAnnulus(coord, skyradin * u.arcsec, skyradout * u.arcsec) else: annulus = CircularAnnulus(coord, skyradin, skyradout) #mask out nans or negative mask = np.logical_or(np.isnan(data), data < 0.) apflux = aperture_photometry(data, aperture, wcs=wcs, error=error, mask=mask) skyflux = aperture_photometry(data, annulus, wcs=wcs, error=error, mask=mask) phot_table = hstack([apflux, skyflux], table_names=['src', 'sky']) #calculate mean local background in annulus if isinstance(annulus, SkyCircularAnnulus): sky_area = annulus.to_pixel(wcs).area() else: sky_area = annulus.area() if isinstance(aperture, SkyCircularAperture): src_area = aperture.to_pixel(wcs).area() else: src_area = aperture.area() sky_mean = phot_table['aperture_sum_sky'] / sky_area sky_sum = sky_mean * src_area final_sum = phot_table['aperture_sum_src'] - sky_sum phot_table['residual_aperture_sum'] = final_sum phot_table['residual_aperture_sum'].unit = unit phot_table['aperture_area_sky'] = sky_area phot_table['aperture_area_src'] = src_area phot_table['aperture_mean_sky'] = sky_mean phot_table['aperture_mean_sky'].unit = unit phot_table['aperture_rad_src'] = rad phot_table['aperture_irad_sky'] = skyradin phot_table['aperture_orad_sky'] = skyradout phot_table['aperture_area_sky'].unit = u.pix**2 phot_table['aperture_area_src'].unit = u.pix**2 if error is not None: src_err = phot_table['aperture_sum_err_src'] sky_err = phot_table['aperture_sum_err_sky'] src_var = src_err / phot_table['residual_aperture_sum'] sky_var = sky_err / sky_sum color_err = 0. # 10 percent photoerr color_var = color_err * phot_table['residual_aperture_sum'] phot_table['residual_aperture_err'] = np.sqrt(src_var**2 + sky_var**2 + color_var**2) else: color_err = 0.07 # 7 percent photoerr color_var = color_err * sky_sum phot_table.add_column( Column([color_var for x in phot_table], name='residual_aperture_err')) phot_table.remove_columns( ['xcenter_src', 'ycenter_src', 'xcenter_sky', 'ycenter_sky']) if 'center_input' in phot_table.colnames: phot_table.remove_columns('center_input') if 'center_input_src' in phot_table.colnames: phot_table.remove_columns('center_input_src') if 'center_input_sky' in phot_table.colnames: phot_table.remove_columns('center_input_sky') if isinstance(coord, SkyCoord): phot_table['xcen'], phot_table['ycen'] = wcs2pix(coord, wcs) phot_table['ra'], phot_table['dec'] = coord.to_string('hmsdms', sep=':').split() else: phot_table['xcen'] = coord[0] phot_table['ycen'] = coord[1] if skycoord: if isinstance(scoord, SkyCoord): phot_table['xcensky'], phot_table['ycensky'] = wcs2pix(scoord, wcs) phot_table['rasky'], phot_table['decsky'] = scoord.to_string( 'hmsdms', sep=':').split() else: phot_table['xcensky'] = scoord[0] phot_table['ycensky'] = scoord[1] if wcs: if 'ra' not in phot_table.colnames: ra, dec = pix2wcs(coord, wcs) phot_table['ra'] = ra phot_table['dec'] = dec if skycoord: if 'rasky' not in phot_table.colnames: skyra, skydec = pix2wcs(scoord, wcs) phot_table['rasky'] = skyra phot_table['decsky'] = skydec if calfctr: flux = phot_table['residual_aperture_sum'] / calfctr phot_table.add_column(Column(flux, name='ap_flux', unit=unit)) #if error is not None: fluxerr = phot_table['residual_aperture_err'] / calfctr phot_table.add_column(Column(fluxerr, name='ap_err', unit=unit)) if filter: phot_table['filter'] = str(filter) return phot_table
error in photometry is assumed as Poissonian -- sqrt(N) but smooth background error not included for now """ for eachFile in imFiles: print('Prcoessing %s ...' %(eachFile)) try: hdu = pyfits.open(eachFile) im = hdu[0].data except: print('File could not be opened: %s' %(eachFile)) # create aperture and annulus objects apertures = CircularAperture(coords, r=aper_size) annulus_apertures = CircularAnnulus(coords, r_in=annulus, r_out=dannulus) npix_src, npix_bkg = apertures.area(), annulus_apertures.area() # calculate the object and annulus flux data_error = np.sqrt(im) rawflux_table = aperture_photometry(im, apertures, error=data_error) bkgflux_table = aperture_photometry(im, annulus_apertures, error=data_error) # stack the two tables into one phot_table = hstack([rawflux_table, bkgflux_table], table_names=['raw', 'bkg']) # calculate bkg mean & normalize by area bkg_mean = phot_table['aperture_sum_bkg'] / npix_bkg # final photometric counts -- in ADUs Nsrc = phot_table['aperture_sum_raw'] - bkg_mean * npix_src # change counts to flux flux = gain * Nsrc / hdu[0].header['EXPTIME']
def onpress(event): if event.key == 'p': filename = d.get('file') #hdu=fits.open(filename) hdu = d.get_pyfits() data = hdu[0].data x = d.get('crosshair image') x, y = x.split() x, y = int(float(x)), int(float(y)) new_image_halfwidth = int(entry1.get()) aperture_radius = int(entry2.get()) inner_sky_radius = int(entry3.get()) outer_sky_radius = int(entry4.get()) new_image = data[y - new_image_halfwidth:y + new_image_halfwidth, x - new_image_halfwidth:x + new_image_halfwidth] x_grid = np.arange(x - new_image_halfwidth, x + new_image_halfwidth, 1) y_grid = np.arange(y - new_image_halfwidth, y + new_image_halfwidth, 1) x_grid, y_grid = np.meshgrid(x_grid, y_grid) guess = (np.amax(new_image) - new_image[0, 0], x, y, 3, 3, 0, new_image[0, 0]) popt, pcov = curve_fit(gauss2d, (x_grid, y_grid), new_image.ravel(), p0=guess) #print popt x = int(popt[1]) y = int(popt[2]) labeltext.set('X: ' + str(x)) label2text.set('Y: ' + str(y)) new_image = data[y - new_image_halfwidth:y + new_image_halfwidth, x - new_image_halfwidth:x + new_image_halfwidth] # for artist in ax1.get_children(): # if hasattr(artist,'get_label') and artist.get_label()=='centroid': # artist.remove() ax1.clear() ax1.matshow(np.flip(new_image, axis=0), cmap='gray', origin='upper', clim=zscale(new_image), zorder=0) ax1.scatter( [new_image_halfwidth + 1], [new_image_halfwidth - 1], marker='+', s=120, c='k', zorder=1 ) #ax1.scatter([popt[1]-x+new_image_halfwidth],[popt[2]-y+new_image_halfwidth],marker='+',s=120,c='k',zorder=1) aperture_circle = plt.Circle((popt[1] - x + new_image_halfwidth, popt[2] - y + new_image_halfwidth), radius=aperture_radius, linewidth=3, color='hotpink', fill=False, lw=3, zorder=2) ax1.add_patch(aperture_circle) inner_sky = plt.Circle((popt[1] - x + new_image_halfwidth, popt[2] - y + new_image_halfwidth), radius=inner_sky_radius, linewidth=3, color='lime', fill=False, zorder=2) ax1.add_patch(inner_sky) outer_sky = plt.Circle((popt[1] - x + new_image_halfwidth, popt[2] - y + new_image_halfwidth), radius=outer_sky_radius, linewidth=3, color='red', fill=False, zorder=2) ax1.add_patch(outer_sky) canvas.draw() #update the radial plot ax2.clear() radial = radial_profile(new_image, [new_image_halfwidth, new_image_halfwidth]) #perform the aperture photometry #currently 2% different from IDL atv aperture = CircularAperture((x, y), r=aperture_radius) annulus = CircularAnnulus((x, y), r_in=inner_sky_radius, r_out=outer_sky_radius) phot_table = aperture_photometry(data, [aperture, annulus]) #new background estimation bkg_mask = np.ma.masked_outside(radial[0].reshape(new_image.shape), inner_sky_radius, outer_sky_radius) bkg_map = Background2D(new_image, tuple(np.array(new_image.shape) / 4), mask=bkg_mask.mask, exclude_mesh_method='all') bkg_map_med = MMMBackground().calc_background( bkg_map.data) #bkg_map_med=np.median(bkg_map.background) #print 'Map sky mean '+str(bkg_map_med) #bkg_mean=phot_table['aperture_sum_1']/annulus.area() #print 'Aperture sky mean '+str(bkg_mean) #phot_table['residual_aperture_sum']=phot_table['aperture_sum_0']-bkg_mean*aperture.area() phot_table['residual_aperture_sum'] = phot_table[ 'aperture_sum_0'] - bkg_map_med * aperture.area() #print 'Map sky result: '+str(phot_table['aperture_sum_0']-bkg_map_med*aperture.area()) #print "Aperture Photometry Result: "+str(phot_table['residual_aperture_sum']) label8text.set('Sky Value: ' + str(int(bkg_map_med))) label9text.set('Aperture Counts: ' + str(int(phot_table['residual_aperture_sum'][0]))) label10text.set( 'Mag: ' + str(-2.5 * np.log10(int(phot_table['residual_aperture_sum'][0])) + 25.)[:5]) ax2.scatter(radial[0], radial[1]) if var10.get() == 1: ax2.plot(np.linspace(0, new_image_halfwidth, num=50), gauss1d(np.linspace(0, new_image_halfwidth, num=50), popt[0], 0, np.mean([popt[3], popt[4]]), popt[6]), c='k', lw=2) ax2.text(0.5, 0.93, 'Gaussian FWHM: ' + str(2.35482 * np.mean([popt[3], popt[4]]))[:5], transform=ax2.transAxes, fontsize=int(15 * scaling)) if var11.get() == 1: moffat1d_guess = (np.amax(new_image) - bkg_map_med, 0, 3, 1, bkg_map_med) popt2, pcov2 = curve_fit(moffat1d, radial[0], radial[1], p0=moffat1d_guess) ax2.plot(np.linspace(0, new_image_halfwidth, num=50), moffat1d(np.linspace(0, new_image_halfwidth, num=50), popt2[0], popt2[1], popt2[2], popt2[3], popt2[4]), c='r', lw=2) ax2.text( 0.5, 0.85, 'Moffat FWHM: ' + str(2.0 * popt2[2] * np.sqrt(2.0**(1. / popt2[3]) - 1.))[:5], transform=ax2.transAxes, fontsize=int(15 * scaling)) ax2.grid(True, color='white', linestyle='-', linewidth=1) ax2.set_axisbelow(True) ax2.autoscale(False) ax2.set_xlim([0, new_image_halfwidth]) ax2.set_xlabel('Radius (Pixels)') ax2.set_ylim([np.amin(radial[1]), np.amax(radial[1])]) ax2.set_axis_bgcolor('0.85') ax2.axvline(aperture_radius, linewidth=2, color='hotpink') ax2.axvline(inner_sky_radius, linewidth=2, color='lime') ax2.axvline(outer_sky_radius, linewidth=2, color='red') ax2.axhline(bkg_map_med, linewidth=2, color='yellow') canvas2.draw()
def tso_aperture_photometry(datamodel, xcenter, ycenter, radius, radius_inner, radius_outer): """ Create a photometric catalog for NIRCam TSO imaging observations. Parameters ---------- datamodel : `CubeModel` The input `CubeModel` of a NIRCam TSO imaging observation. xcenter, ycenter : float The ``x`` and ``y`` center of the aperture. radius : float The radius (in pixels) of the circular aperture. radius_inner, radius_outer : float The inner and outer radii (in pixels) of the circular-annulus aperture, used for local background estimation. Returns ------- catalog : `~astropy.table.QTable` An astropy QTable (Quantity Table) containing the source photometry. """ if not isinstance(datamodel, CubeModel): raise ValueError('The input data model must be a CubeModel.') # For the SUB64P subarray with the WLP8 pupil, the circular aperture # extends beyond the image and the circular annulus does not have any # overlap with the image. In that case, we simply sum all values # in the array and skip the background subtraction. sub64p_wlp8 = False if (datamodel.meta.instrument.pupil == 'WLP8' and datamodel.meta.subarray.name == 'SUB64P'): sub64p_wlp8 = True if not sub64p_wlp8: phot_aper = CircularAperture((xcenter, ycenter), r=radius) bkg_aper = CircularAnnulus((xcenter, ycenter), r_in=radius_inner, r_out=radius_outer) aperture_sum = [] aperture_sum_err = [] annulus_sum = [] annulus_sum_err = [] nimg = datamodel.data.shape[0] if sub64p_wlp8: info = ('Photometry measured as the sum of all values in the ' 'subarray. No background subtraction was performed.') for i in np.arange(nimg): aperture_sum.append(np.sum(datamodel.data[i, :, :])) aperture_sum_err.append( np.sqrt(np.sum(datamodel.err[i, :, :]**2))) else: info = ('Photometry measured in a circular aperture of r={0} ' 'pixels. Background calculated as the mean in a ' 'circular annulus with r_inner={1} pixels and ' 'r_outer={2} pixels.'.format(radius, radius_inner, radius_outer)) for i in np.arange(nimg): aper_sum, aper_sum_err = phot_aper.do_photometry( datamodel.data[i, :, :], error=datamodel.err[i, :, :]) ann_sum, ann_sum_err = bkg_aper.do_photometry( datamodel.data[i, :, :], error=datamodel.err[i, :, :]) aperture_sum.append(aper_sum[0]) aperture_sum_err.append(aper_sum_err[0]) annulus_sum.append(ann_sum[0]) annulus_sum_err.append(ann_sum_err[0]) aperture_sum = np.array(aperture_sum) aperture_sum_err = np.array(aperture_sum_err) annulus_sum = np.array(annulus_sum) annulus_sum_err = np.array(annulus_sum_err) # construct metadata for output table meta = OrderedDict() meta['instrument'] = datamodel.meta.instrument.name meta['detector'] = datamodel.meta.instrument.detector meta['channel'] = datamodel.meta.instrument.channel meta['subarray'] = datamodel.meta.subarray.name meta['filter'] = datamodel.meta.instrument.filter meta['pupil'] = datamodel.meta.instrument.pupil meta['target_name'] = datamodel.meta.target.catalog_name meta['xcenter'] = xcenter meta['ycenter'] = ycenter ra_icrs, dec_icrs = datamodel.meta.wcs(xcenter, ycenter) meta['ra_icrs'] = ra_icrs meta['dec_icrs'] = dec_icrs meta['apertures'] = info # initialize the output table tbl = QTable(meta=meta) if hasattr(datamodel, 'int_times') and datamodel.int_times is not None: nrows = len(datamodel.int_times) else: nrows = 0 if nrows == 0: log.warning("There is no INT_TIMES table in the input file.") if nrows > 0: shape = datamodel.data.shape if len(shape) == 2: num_integ = 1 else: # len(shape) == 3 num_integ = shape[0] int_start = datamodel.meta.exposure.integration_start if int_start is None: int_start = 1 log.warning("INTSTART not found; assuming a value of %d", int_start) # Columns of integration numbers & times of integration from the # INT_TIMES table. int_num = datamodel.int_times['integration_number'] mid_utc = datamodel.int_times['int_mid_MJD_UTC'] offset = int_start - int_num[0] # both are one-indexed if offset < 0: log.warning("Range of integration numbers in science data extends " "outside the range in INT_TIMES table.") log.warning("Can't use INT_TIMES table.") del int_num, mid_utc nrows = 0 # flag as bad else: log.debug("Times are from the INT_TIMES table.") time_arr = mid_utc[offset: offset + num_integ] int_times = Time(time_arr, format='mjd', scale='utc') else: log.debug("Times were computed from EXPSTART and TGROUP.") dt = (datamodel.meta.exposure.group_time * (datamodel.meta.exposure.ngroups + 1)) dt_arr = (np.arange(1, 1 + datamodel.meta.exposure.nints) * dt - (dt / 2.)) int_dt = TimeDelta(dt_arr, format='sec') int_times = (Time(datamodel.meta.exposure.start_time, format='mjd') + int_dt) tbl['MJD'] = int_times.mjd tbl['aperture_sum'] = aperture_sum tbl['aperture_sum_err'] = aperture_sum_err if not sub64p_wlp8: tbl['annulus_sum'] = annulus_sum tbl['annulus_sum_err'] = annulus_sum_err annulus_mean = annulus_sum / bkg_aper.area() annulus_mean_err = annulus_sum_err / bkg_aper.area() tbl['annulus_mean'] = annulus_mean tbl['annulus_mean_err'] = annulus_mean_err aperture_bkg = annulus_mean * phot_aper.area() aperture_bkg_err = annulus_mean_err * phot_aper.area() tbl['aperture_bkg'] = aperture_bkg tbl['aperture_bkg_err'] = aperture_bkg_err net_aperture_sum = aperture_sum - aperture_bkg net_aperture_sum_err = np.sqrt(aperture_sum_err ** 2 + aperture_bkg_err ** 2) tbl['net_aperture_sum'] = net_aperture_sum tbl['net_aperture_sum_err'] = net_aperture_sum_err else: colnames = ['annulus_sum', 'annulus_sum_err', 'annulus_mean', 'annulus_mean_err', 'aperture_bkg', 'aperture_bkg_err'] for col in colnames: tbl[col] = np.full(nimg, np.nan) tbl['net_aperture_sum'] = aperture_sum tbl['net_aperture_sum_err'] = aperture_sum_err return tbl
def photom(ima, pos, radius, r_in=None, r_out=None, method='median'): ''' Aperture photometry in an aperture located at pixel coordinates pos = ( (x0, y0), (x1, y1), ... ) with a radius=radius. When r_in and r_out are given, background is estimated in CircularAnnulus and subtracted. method refers to how the background is estimated within the circlar annulus. Can be 'median' or 'mean' or 'mode' ''' ima_local = np.ma.asanyarray(ima.copy()) ima_local.fill_value = np.nan mask_ = ima_local.mask ima_ = ima_local.filled() ### Do photometry - identical for each method apertures = CircularAperture(pos, r = radius) ap = aperture_photometry(ima_, apertures, mask=mask_, method='exact') # Aperture photometry on mask to estimate # of masked pixels in aperture apm = aperture_photometry(mask_.astype(int), apertures, method='exact') ap.add_columns( [apertures.area()-apm['aperture_sum'], apm['aperture_sum']], names=['aperture_area', 'aperture_badpix']) ap.add_column(ap['aperture_sum'], index=3, name='Flux') if ( r_in == None or r_out == None or not method in ('mean', 'median', 'mode') ): # Quit here if background correction is not requested return ap annulus_apertures = CircularAnnulus(pos, r_in=r_in, r_out=r_out) annulus_masks = annulus_apertures.to_mask(method='center') bg_values = [] for annulus_mask in annulus_masks: bg_ima = annulus_mask.cutout(ima_) bg_mask = annulus_mask.cutout(mask_.astype(np.int)) bg_ima = np.ma.array(bg_ima, mask= bg_mask.astype(np.bool) | ~annulus_mask.data.astype(np.bool)) if method == 'mean': bg_val = bg_ima.mean() elif method == 'median': bg_val = np.ma.median(bg_ima) elif method == 'mode': kernel = gaussian_kde(bg_ima.data[~bg_ima.mask], bw_method='silverman') mode = bg_ima.mean() std = bg_ima.std() mode = minimize_scalar(lambda x: -kernel(x), bounds=(mode-3*std, mode+3*std), method='bounded') bg_val=mode.x[0] if False: median = np.ma.median(bg_ima) h, b = np.histogram(bg_ima.data[~bg_ima.mask], bins=15, normed=True) bc = 0.5*(b[1:]+ b[:-1]) plt.figure(33); plt.clf(); plt.ioff() fig, (ax0,ax1) = plt.subplots(ncols=2, nrows=1, num=33) ax0.plot(bc, h, 'x') x = np.linspace(bc.min(), bc.max(), 100) ax0.plot(x, kernel(x)) ax0.vlines(mode.x, ax0.get_ylim()[0], ax0.get_ylim()[1]) ax0.vlines(median, ax0.get_ylim()[0], ax0.get_ylim()[1]) ax1.imshow(bg_ima) plt.show() bg_values.append(bg_val) ap.add_column(Column(data=bg_values, name = 'background')) ap['Flux'] = ap['Flux'] - ap['aperture_area']*ap['background'] return ap, bg_ima
def align_norm(fnlist, tolerance=5, thresh=3.5): """Aligns a set of images to each other, as well as normalizing the images to the same average brightness. Both the alignment and normalization are accomplished through stellar photometry using the IRAF routine 'daophot'. The centroids of a handful of stars are found and used to run the IRAF routine 'imalign'. The instrumental magnitudes of the stars are used to determine by how much each image must be scaled for the photometry to match across images. The images are simply updated with their rescaled, shifted selves. This overwrites the previous images and adds the header keyword 'fpphot' to the images. A handful of temporary files are created during this process, which should all be deleted by the routine at the end. But if it is interrupted, they might not be. If the uncertainty images exist, this routine also shifts them by the same amounts as the intensity images, as well as updating the uncertainty values for both the new normalization and the uncertainties in normalizing the images. Inputs: fnlist -> List of strings, each the path to a fits image. tolerance -> How close two objects can be and still be considered the same object. Default is 3 pixels. thresh -> Optional. Level above sky background variation to look for objs. Default is 3.5 (times SkySigma). Decrease if center positions aren't being found accurately. Increase for crowded fields to decrease computation time. """ # Get image FWHMs fwhm = np.empty(len(fnlist)) firstimage = FPImage(fnlist[0]) toggle = firstimage.fwhm axcen = firstimage.axcen aycen = firstimage.aycen arad = firstimage.arad firstimage.close() if axcen is None: print "Error! Images have not yet been aperture-masked! Do this first!" crash() if toggle is None: print "Warning! FWHMs have not been measured!" print "Assuming 5 pixel FWHM for all images." for i in range(len(fnlist)): fwhm[i] = 5 else: for i in range(len(fnlist)): image = FPImage(fnlist[i]) fwhm[i] = image.fwhm image.close() # Get sky background levels skyavg = np.empty(len(fnlist)) skysig = np.empty(len(fnlist)) for i in range(len(fnlist)): image = FPImage(fnlist[i]) skyavg[i], skysig[i], _skyvar = image.skybackground() image.close() # Identify the stars in each image xlists = [] ylists = [] print "Identifying stars in each image..." for i in range(len(fnlist)): xlists.append([]) ylists.append([]) image = FPImage(fnlist[i]) axcen = image.axcen aycen = image.aycen arad = image.arad sources = daofind(image.inty-skyavg[i], fwhm=fwhm[i], threshold=thresh*skysig[i]).as_array() for j in range(len(sources)): # If the source is not near the center or edge centermask = ((sources[j][1]-axcen)**2 + (sources[j][2]-aycen)**2 > (0.05*arad)**2) edgemask = ((sources[j][1]-axcen)**2 + (sources[j][2]-aycen)**2 < (0.95*arad)**2) if np.logical_and(centermask, edgemask): xlists[i].append(sources[j][1]) ylists[i].append(sources[j][2]) image.close() # Match objects between fields print "Matching objects between images..." xcoo = [] ycoo = [] for i in range(len(xlists[0])): # For each object in the first image accept = True for j in range(1, len(fnlist)): # For each other image dist2 = ((np.array(xlists[j])-xlists[0][i])**2 + (np.array(ylists[j])-ylists[0][i])**2) if (min(dist2) > tolerance**2): accept = False break if accept: # We found an object at that position in every image xcoo.append(xlists[0][i]) ycoo.append(ylists[0][i]) # Create coordinate arrays for the photometry and shifting x = np.zeros((len(fnlist), len(xcoo))) y = np.zeros_like(x) for i in range(len(xcoo)): # For every object found in the first image for j in range(len(fnlist)): # Find that object in every image dist2 = ((np.array(xlists[j])-xcoo[i])**2 + (np.array(ylists[j])-ycoo[i])**2) index = np.argmin(dist2) x[j, i] = xlists[j][index] y[j, i] = ylists[j][index] # Do aperture photometry on the matched objects print "Performing photometry on matched stars..." counts = np.zeros_like(x) dcounts = np.zeros_like(x) for i in range(len(fnlist)): image = FPImage(fnlist[i]) apertures = CircularAperture((x[i], y[i]), r=2*fwhm[i]) annuli = CircularAnnulus((x[i], y[i]), r_in=3*fwhm[i], r_out=4*fwhm[i]) phot_table = aperture_photometry(image.inty, apertures, error=np.sqrt(image.vari)) sky_phot_table = aperture_photometry(image.inty, annuli, error=np.sqrt(image.vari)) counts[i] = phot_table["aperture_sum"] / apertures.area() counts[i] -= sky_phot_table["aperture_sum"] / annuli.area() counts[i] *= apertures.area() dcounts[i] = phot_table["aperture_sum_err"] / apertures.area() image.close() # Calculate the shifts and normalizations norm, dnorm = calc_norm(counts, dcounts) for i in range(x.shape[1]): x[:, i] = -(x[:, i] - x[0, i]) y[:, i] = -(y[:, i] - y[0, i]) xshifts = np.average(x, axis=1) yshifts = np.average(y, axis=1) # Normalize the images and put shifts in the image headers for i in range(len(fnlist)): image = FPImage(fnlist[i], update=True) image.phottog = "True" image.dnorm = dnorm[i] image.inty /= norm[i] image.vari = image.vari/norm[i]**2 image.xshift = xshifts[i] image.yshift = yshifts[i] image.close() return
# overplotted apertures, smaller should be better. circ_apertures = CircularAperture(positions, r=arb.radius[band]) annulus_apertures = CircularAnnulus( positions, r_in=arb.annuli_r[band][0], r_out=arb.annuli_r[band][1]) apers = [circ_apertures, annulus_apertures] phot_table = aperture_photometry(image, apers) ################################# # Local background subtraction. # ################################# ## Method 1: Follow ## http://photutils.readthedocs.io/en/stable/photutils (...) ## /aperture.html#local-background-subtraction bkg_mean = phot_table['aperture_sum_1'] / annulus_apertures.area() bkg_sum = bkg_mean * circ_apertures.area() final_sum = phot_table['aperture_sum_0'] - bkg_sum phot_table['residual_aperture_sum'] = final_sum ## Method 2: Follow ## https://github.com/astropy/photutils/pull/453, sigclipping away stars ## in the annulus. #ann_masks = annulus_apertures.to_mask(method='center') #ann_masked_data = [am.apply(image) for am in ann_masks] ## Sigma clip stars in annular aperture. #pre_sc_median = [np.nanmedian(amd[amd != 0]) # for amd in ann_masked_data] #pre_sc_std = [ # (np.nanmedian(np.abs(amd[amd != 0] - pre_sc_median[ix])))*1.483
np.logical_or(xStars < 40, xStars > (nx - 40)), np.logical_or(yStars < 40, yStars > (ny - 40))) # Now let's do aperture photometry on the remaining sources in the image # 1. Setup the apertures sourcePos = [xStars, yStars] apertures = CircularAperture(sourcePos, r = 6.0) annulus_apertures = CircularAnnulus(sourcePos, r_in=12., r_out=14.) # 2. Perform the basic photometry rawflux_table = aperture_photometry(stokesI.arr, apertures) bkgflux_table = aperture_photometry(stokesI.arr, annulus_apertures) phot_table = hstack([rawflux_table, bkgflux_table], table_names=['raw', 'bkg']) # 3. Compute background contribution and subtract from raw photometry bkg_mean = phot_table['aperture_sum_bkg'] / annulus_apertures.area() bkg_sum = bkg_mean * apertures.area() final_sum = phot_table['aperture_sum_raw'] - bkg_sum phot_table['residual_aperture_sum'] = final_sum # Compute the signal-to-noise ratio and find the stars with SNR < 3.0 SNR = final_sum/bkg_sum bkgDominated = SNR < 1.0 # ########################################################################### # # PRINT OUT THE PHOTOMETRY TO CHECK FOR CONSISTENCY # ########################################################################### # xFmtStr = '{x[0]:>6}.{x[1]:<3}' # yFmtStr = '{y[0]:>6}.{y[1]:<3}' # starFmtStr = '{star[0]:>9}.{star[1]:<3}' # bkgFmtStr = '{bkg[0]:>9}.{bkg[1]:<3}' # snrFmtStr = '{snr[0]:>9}.{snr[1]:<3}'
def extract_ifu(input_model, source_type, extract_params): """This function does the extraction. Parameters ---------- input_model : IFUCubeModel The input model. source_type : string "point" or "extended" extract_params : dict The extraction parameters for aperture photometry. Returns ------- ra, dec : float ra and dec are the right ascension and declination respectively at the nominal center of the image. wavelength : ndarray, 1-D The wavelength in micrometers at each pixel. net : ndarray, 1-D The count rate (or flux) minus the background at each pixel. background : ndarray, 1-D The background count rate that was subtracted from the total source count rate to get `net`. npixels : ndarray, 1-D, float64 For each slice, this is the number of pixels that were added together to get `net`. dq : ndarray, 1-D, uint32 The data quality array. """ data = input_model.data shape = data.shape if len(shape) != 3: log.error("Expected a 3-D IFU cube; dimension is %d.", len(shape)) raise RuntimeError("The IFU cube should be 3-D.") # We need to allocate net, background, npixels, and dq arrays # no matter what. We may need to divide by npixels, so the default # is 1 rather than 0. net = np.zeros(shape[0], dtype=np.float64) background = np.zeros(shape[0], dtype=np.float64) npixels = np.ones(shape[0], dtype=np.float64) dq = np.zeros(shape[0], dtype=np.uint32) # For an extended target, the entire aperture will be extracted, so # it makes no sense to shift the extraction location. if source_type.lower() != "extended": ra_targ = input_model.meta.target.ra dec_targ = input_model.meta.target.dec locn = locn_from_wcs(input_model, ra_targ, dec_targ) if locn is None or np.isnan(locn[0]): log.warning("Couldn't determine pixel location from WCS, so " "nod/dither correction will not be applied.") x_center = extract_params['x_center'] y_center = extract_params['y_center'] if x_center is None: x_center = float(shape[-1]) / 2. else: x_center = float(x_center) if y_center is None: y_center = float(shape[-2]) / 2. else: y_center = float(y_center) else: (x_center, y_center) = locn log.info("Using x_center = %g, y_center = %g, based on " "TARG_RA and TARG_DEC.", x_center, y_center) method = extract_params['method'] # subpixels is only needed if method = 'subpixel'. subpixels = extract_params['subpixels'] subtract_background = extract_params['subtract_background'] smaller_axis = float(min(shape[-2], shape[-1])) # for defaults radius = None inner_bkg = None outer_bkg = None if source_type == 'extended': # Ignore any input parameters, and extract the whole image. width = float(shape[-1]) height = float(shape[-2]) x_center = width / 2. - 0.5 y_center = height / 2. - 0.5 theta = 0. subtract_background = False else: radius = extract_params['radius'] if radius is None: radius = smaller_axis / 4. if subtract_background: inner_bkg = extract_params['inner_bkg'] if inner_bkg is None: inner_bkg = radius outer_bkg = extract_params['outer_bkg'] if outer_bkg is None: outer_bkg = min(inner_bkg * math.sqrt(2.), smaller_axis / 2. - 1.) if inner_bkg <= 0. or outer_bkg <= 0. or inner_bkg >= outer_bkg: log.debug("Turning background subtraction off, due to " "the values of inner_bkg and outer_bkg.") subtract_background = False width = None height = None theta = None log.debug("IFU 1-D extraction parameters:") log.debug(" x_center = %s", str(x_center)) log.debug(" y_center = %s", str(y_center)) if source_type == 'point': log.debug(" radius = %s", str(radius)) log.debug(" subtract_background = %s", str(subtract_background)) log.debug(" inner_bkg = %s", str(inner_bkg)) log.debug(" outer_bkg = %s", str(outer_bkg)) log.debug(" method = %s", method) if method == "subpixel": log.debug(" subpixels = %s", str(subpixels)) else: log.debug(" width = %s", str(width)) log.debug(" height = %s", str(height)) log.debug(" theta = %s degrees", str(theta)) log.debug(" subtract_background = %s", str(subtract_background)) log.debug(" method = %s", method) if method == "subpixel": log.debug(" subpixels = %s", str(subpixels)) x0 = float(shape[2]) / 2. y0 = float(shape[1]) / 2. (ra, dec, wavelength) = get_coordinates(input_model, x0, y0) position = (x_center, y_center) if source_type == 'point': aperture = CircularAperture(position, r=radius) else: aperture = RectangularAperture(position, width, height, theta) if subtract_background and inner_bkg is not None and outer_bkg is not None: annulus = CircularAnnulus(position, r_in=inner_bkg, r_out=outer_bkg) else: annulus = None # Compute the area of the aperture and possibly also of the annulus. normalization = 1. temp = np.ones(shape[-2:], dtype=np.float64) phot_table = aperture_photometry(temp, aperture, method=method, subpixels=subpixels) aperture_area = float(phot_table['aperture_sum'][0]) log.debug("aperture.area() = %g; aperture_area = %g", aperture.area(), aperture_area) if subtract_background and annulus is not None: # Compute the area of the annulus. phot_table = aperture_photometry(temp, annulus, method=method, subpixels=subpixels) annulus_area = float(phot_table['aperture_sum'][0]) log.debug("annulus.area() = %g; annulus_area = %g", annulus.area(), annulus_area) if annulus_area > 0.: normalization = aperture_area / annulus_area else: log.warning("Background annulus has no area, so background " "subtraction will be turned off.") subtract_background = False del temp npixels[:] = aperture_area for k in range(shape[0]): phot_table = aperture_photometry(data[k, :, :], aperture, method=method, subpixels=subpixels) net[k] = float(phot_table['aperture_sum'][0]) if subtract_background: bkg_table = aperture_photometry(data[k, :, :], annulus, method=method, subpixels=subpixels) background[k] = float(bkg_table['aperture_sum'][0]) net[k] = net[k] - background[k] * normalization # Check for NaNs in the wavelength array, flag them in the dq array, # and truncate the arrays if NaNs are found at endpoints (unless the # entire array is NaN). (wavelength, net, background, npixels, dq) = \ nans_in_wavelength(wavelength, net, background, npixels, dq) return (ra, dec, wavelength, net, background, npixels, dq)
def photometry(star_positions, aperture_radii, centroid_stamp_half_width, psf_stddev_init, aperture_annulus_radius, output_path): """ Parameters ---------- master_dark_path : str Path to master dark frame master_flat_path :str Path to master flat field target_centroid : `~numpy.ndarray` position of centroid, with shape (2, 1) comparison_flux_threshold : float Minimum fraction of the target star flux required to accept for a comparison star to be included aperture_radii : `~numpy.ndarray` Range of aperture radii to use centroid_stamp_half_width : int Centroiding is done within image stamps centered on the stars. This parameter sets the half-width of the image stamps. psf_stddev_init : float Initial guess for the width of the PSF stddev parameter, used for fitting 2D Gaussian kernels to the target star's PSF. aperture_annulus_radius : int For each aperture in ``aperture_radii``, measure the background in an annulus ``aperture_annulus_radius`` pixels bigger than the aperture radius output_path : str Path to where outputs will be saved. """ master_dark = fits.getdata(master_dark_path) master_flat = fits.getdata(master_flat_path) master_flat[master_flat < 0.1] = 1.0 # tmp #star_positions = init_centroids(image_paths[0:3], master_flat, master_dark, # target_centroid, plots=True, # min_flux=comparison_flux_threshold).T # Initialize some empty arrays to fill with data: times = np.zeros(len(image_paths)) fluxes = np.zeros((len(image_paths), len(star_positions), len(aperture_radii))) errors = np.zeros((len(image_paths), len(star_positions), len(aperture_radii))) xcentroids = np.zeros((len(image_paths), len(star_positions))) ycentroids = np.zeros((len(image_paths), len(star_positions))) airmass = np.zeros(len(image_paths)) airpress = np.zeros(len(image_paths)) humidity = np.zeros(len(image_paths)) telfocus = np.zeros(len(image_paths)) psf_stddev = np.zeros(len(image_paths)) medians = np.zeros(len(image_paths)) with ProgressBar(len(image_paths)) as bar: for i in range(len(image_paths)): bar.update() # Subtract image by the dark frame, normalize by flat field imagedata = (fits.getdata(image_paths[i]) - master_dark) / master_flat # Collect information from the header imageheader = fits.getheader(image_paths[i]) exposure_duration = imageheader['EXPTIME'] times[i] = Time(imageheader['DATE-OBS'], format='isot', scale=imageheader['TIMESYS'].lower()).jd medians[i] = np.median(imagedata) airmass[i] = imageheader['AIRMASS'] airpress[i] = imageheader['AIRPRESS'] humidity[i] = imageheader['HUMIDITY'] telfocus[i] = imageheader['TELFOCUS'] # Initial guess for each stellar centroid informed by previous centroid for j in range(len(star_positions)): if i == 0: init_x = star_positions[j][0] init_y = star_positions[j][1] else: init_x = ycentroids[i-1][j] init_y = xcentroids[i-1][j] # Cut out a stamp of the full image centered on the star image_stamp = imagedata[int(init_y) - centroid_stamp_half_width: int(init_y) + centroid_stamp_half_width, int(init_x) - centroid_stamp_half_width: int(init_x) + centroid_stamp_half_width] # Measure stellar centroid with 2D gaussian fit x_stamp_centroid, y_stamp_centroid = centroid_com(image_stamp) y_centroid = x_stamp_centroid + init_x - centroid_stamp_half_width x_centroid = y_stamp_centroid + init_y - centroid_stamp_half_width xcentroids[i, j] = x_centroid ycentroids[i, j] = y_centroid # For the target star, measure PSF: if j == 0: psf_model_init = models.Gaussian2D(amplitude=np.max(image_stamp), x_mean=centroid_stamp_half_width, y_mean=centroid_stamp_half_width, x_stddev=psf_stddev_init, y_stddev=psf_stddev_init) fit_p = fitting.LevMarLSQFitter() y, x = np.mgrid[:image_stamp.shape[0], :image_stamp.shape[1]] best_psf_model = fit_p(psf_model_init, x, y, image_stamp - np.median(image_stamp)) psf_stddev[i] = 0.5*(best_psf_model.x_stddev.value + best_psf_model.y_stddev.value) positions = np.vstack([ycentroids[i, :], xcentroids[i, :]]) for k, aperture_radius in enumerate(aperture_radii): target_apertures = CircularAperture(positions, aperture_radius) background_annuli = CircularAnnulus(positions, r_in=aperture_radius + aperture_annulus_radius, r_out=aperture_radius + 2 * aperture_annulus_radius) flux_in_annuli = aperture_photometry(imagedata, background_annuli)['aperture_sum'].data background = flux_in_annuli/background_annuli.area() flux = aperture_photometry(imagedata, target_apertures)['aperture_sum'].data background_subtracted_flux = (flux - background * target_apertures.area()) fluxes[i, :, k] = background_subtracted_flux/exposure_duration errors[i, :, k] = np.sqrt(flux) ## Save some values results = PhotometryResults(times, fluxes, errors, xcentroids, ycentroids, airmass, airpress, humidity, medians, psf_stddev, aperture_radii) results.save(output_path) return results