Ejemplo n.º 1
1
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
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
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()
Ejemplo n.º 4
0
    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
Ejemplo n.º 5
0
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
Ejemplo n.º 6
0
    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
Ejemplo n.º 7
0
def get_background(frames, centroids, bckg_method):
    print 'Calculating background levels'
    frames = np.asarray(frames)
    backgrounds = []

    pbar = progressbar.ProgressBar()
    for i in pbar(range(len(frames))):
        if bckg_method == 'annulus':
            annulus_apertures = CircularAnnulus(centroids[i],
                                                r_in=10.,
                                                r_out=15.)
            bkgflux = aperture_photometry(frames[i], annulus_apertures)
            bkg_mean = bkgflux['aperture_sum'] / annulus_apertures.area()
            backgrounds.append(bkg_mean[0])

        if bckg_method == 'corners':
            #calculate background by summing flux in corners of image, fit gaussian, get mean
            background = []
            N = 10
            background += list(np.reshape(frames[i, :N, :N], (N**2)))
            background += list(np.reshape(frames[i, -N:, :N], (N**2)))
            background += list(np.reshape(frames[i, :N, -N:], (N**2)))
            background += list(np.reshape(frames[i, -N:, -N:], (N**2)))

            mu, sigma = norm.fit(background)
            backgrounds.append(mu)

    print 'Backgrounds done calculating'
    print ' '
    return backgrounds
Ejemplo n.º 8
0
def find_backgrounds(image,sources,inner_radius=10.,outer_radius=15.):
    '''
    Return the backgrounds of multiple sources
    determine by centering an annulus on each source
    and taking a mean.
    
    Parameters:
        image : nd array
            2D stellar image
        sources : nd array
            Array of (x,y) coordinates
        inner_radius : float
            Radial distance (pixels) of inner annulus
        outer_radius : 
            Radial distance (pixels) of outer annulus
            
    Returns:
        backgrounds : nd array
            Measured background for each star in sources.
    '''
    annulus_apertures = CircularAnnulus(sources, r_in=inner_radius, r_out=outer_radius)
    annulus_phot = aperture_photometry(image, annulus_apertures)
    
    bkg_mean = annulus_phot['aperture_sum'] / annulus_apertures.area()
    backgrounds = bkg_mean.data
    
    return backgrounds
Ejemplo n.º 9
0
 def annulus_background(self, positions, radius=3.):
     #Obtain the sky local background
     annulus_apertures = CircularAnnulus(positions,
                                         r_in=radius + 2.,
                                         r_out=radius + 4.)
     bkg_table = aperture_photometry(self.data, annulus_apertures)
     bkg_counts = float(bkg_table['aperture_sum'])
     bkg_mean = bkg_counts / annulus_apertures.area()
     bkg = np.random.poisson(bkg_mean, self.data.shape)
     return bkg, bkg_counts, bkg_mean
Ejemplo n.º 10
0
 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
Ejemplo n.º 11
0
    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
Ejemplo n.º 12
0
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)
Ejemplo n.º 13
0
def remove_concentric(data, x_mean=512, y_mean=512, x_stddev=3, y_stddev=3, Nsig=10, dp=0.1):
    """
    This function first fits a gaussian PSF to the data to find the centroid.
    Then, it subtracts concentric apertures from the data rather than just 
    subtracting the gaussian model.
    """
    data = data.copy() # Don't overwrite the data
    nx, ny = data.shape
    
    gauss, psf, centroid = fit_gaussian_psf(data, x_mean=x_mean, y_mean=y_mean, 
                                          x_stddev=x_stddev, y_stddev=y_stddev)
    std = (gauss.x_stddev_0.value + gauss.y_stddev_0.value)/2.0
    x0, y0 = centroid
    
    radii = np.arange(dp, Nsig*std, dp)[::-1]
    
    # Get extents using the photutils function
    pos = np.atleast_2d(centroid)
    extents = np.zeros((len(pos), 4), dtype=int)

    extents[:, 0] = pos[:, 0] - max(radii) + 0.5
    extents[:, 1] = pos[:, 0] + max(radii) + 1.5
    extents[:, 2] = pos[:, 1] - max(radii) + 0.5
    extents[:, 3] = pos[:, 1] + max(radii) + 1.5

    _, extent, phot_extent = get_phot_extents(data, pos, extents)
    x_min, x_max, y_min, y_max = extent
    x_pmin, x_pmax, y_pmin, y_pmax = phot_extent
    args = [x_pmin[0], x_pmax[0], y_pmin[0], y_pmax[0], x_max[0]-x_min[0], y_max[0]-y_min[0]]

    # Fit apertures
    psf = np.zeros(data.shape)
    for radius in radii:
        Rin = radius - dp
        Rout = radius
        if Rin > dp/10.0:
            ap = CircularAnnulus(centroid, r_in=Rin, r_out=Rout)
        else:
            ap = CircularAperture(centroid, r=Rout)
        phot = aperture_photometry(data, ap)
        avg_flux = phot['aperture_sum'].item()/ap.area()
        
        outer = circular_overlap_grid(*args, r=Rout, use_exact=1, subpixels=5)
        if Rout-dp > dp/10.0:
            inner = circular_overlap_grid(*args, r=Rin, use_exact=1, subpixels=5)
        else:
            inner = 0.0
        annulus_overlap = outer - inner
        psf[y_min[0]:y_max[0], x_min[0]:x_max[0]] += annulus_overlap * avg_flux
    return data - psf, psf, np.array(centroid)
Ejemplo n.º 14
0
    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
Ejemplo n.º 15
0
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)
Ejemplo n.º 16
0
    def Photometrie(self):
        q = []
        for elem in self.data:
            phot = []
            for x, y in zip(self.Tx[self.index], self.Ty[self.index]):
                radii = range(3, 25)
                positions = [(x, y)]
                apertures = [CircularAperture(positions, r=r) for r in radii]
                annulus_apertures = CircularAnnulus(positions,
                                                    r_in=70.,
                                                    r_out=80.)
                phot_table = aperture_photometry(self.data[self.index],
                                                 apertures)
                phot_background = aperture_photometry(self.data[self.index],
                                                      annulus_apertures)
                bkg_mean = (phot_background['aperture_sum'] /
                            annulus_apertures.area())
                Int = []
                Area = []
                Int_bkg1 = []

                for num in range(3, 25):
                    Int.append(phot_table[0][num])
                    Area.append(apertures[num - 3].area())
                    Int_bkg1.append(phot_table[0][num] -
                                    apertures[num - 3].area() * bkg_mean)

                phot.append(Int_bkg1)

            filename = self.files[self.index]
            print('Processing file: ' + str(filename))
            phot = numpy.array(phot)
            qq = (phot[0] - phot[1]) / (phot[0] + phot[1])
            with open(filename + '.txt', 'w') as f:
                for elem3 in qq:
                    for elem2 in elem3:
                        f.write(
                            str(self.JD[self.index]) + '\t' +
                            str(self.retarder[self.index]) + '\t' +
                            str(elem2) + '\n')
            q.append(qq.flatten())
            self.nextframe(1)

        q = numpy.array(q)
        with open('q', 'w') as f:
            for elem in q:
                for elem2 in elem:
                    f.write(str(elem2) + '\n')
Ejemplo n.º 17
0
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
Ejemplo n.º 18
0
def get_bkg(image, centroid, r_in, r_out):
    '''
    To calculate the mean local background within the circular annulus aperture
    with thickness dr, divide its sum by its area calculated using the area() method
    '''
    annulus = CircularAnnulus(centroid, r_in, r_out)
    result = aperture_photometry(image, annulus)
    bkg_mean = float(result['aperture_sum'] / annulus.area())
    '''
    The background sum within the circular aperture is then the mean local background 
    times the circular aperture area
    
    bkg_sum = bkg_mean * apertures.area()
    residual=aperture_sum - bkg_sum
    '''
    return bkg_mean  #, residual
Ejemplo n.º 19
0
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'
Ejemplo n.º 20
0
    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)
Ejemplo n.º 21
0
def skybg_phot(data,xc,yc,r=25,dr=5):    
    # create a crude annulus to mask out bright background pixels 
    xv,yv = mesh_box([xc,yc], np.round(r+dr) )
    rv = ((xv-xc)**2 + (yv-yc)**2)**0.5
    mask = (rv>r) & (rv<(r+dr))
    cutoff = np.percentile(data[yv,xv][mask], 50)
    dat = np.copy(data)
    dat[dat>cutoff] = cutoff # ignore bright pixels like stars 

    try:
        # under estimate background 
        positions = [(xc, yc)]
        apertures = CircularAnnulus(positions, r_in=r, r_out=r+dr)
        phot_table = aperture_photometry(dat, apertures, method='exact')
        aper_area = apertures.area()
        return float(phot_table['aperture_sum'])/aper_area
    except:
        return min( np.mean(data[yv,xv][mask]), np.median(data[yv,xv][mask]) )
Ejemplo n.º 22
0
    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()
Ejemplo n.º 23
0
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
Ejemplo n.º 24
0
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
Ejemplo n.º 25
0
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
Ejemplo n.º 26
0
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
Ejemplo n.º 27
0
Archivo: ifu.py Proyecto: mcara/jwst
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)
				#print xll, yll, center, xcen, ycen
				ppgplot.pgcirc(plotx, ploty, apertureRadius)
				ppgplot.pgcirc(plotx, ploty, innerSkyRadius)
				ppgplot.pgcirc(plotx, ploty, outerSkyRadius)
				ppgplot.pgptxt(plotx-10, ploty-10, 0, 0, str(index)) 
				
			
			data = window.data
			
			innerFluxes = aperture_photometry(data, apertures)
			outerFluxes = aperture_photometry(data, annulus_apertures)
			
			innerFlux = innerFluxes[0]['aperture_sum']
			outerFlux = outerFluxes[0]['aperture_sum']
			
			bkg_mean = outerFlux / annulus_apertures.area()
			bkg_sum = bkg_mean * apertures.area()
			final_sum = innerFlux - bkg_sum
			#print "Sky subtracted flux:", final_sum
			s.addFluxMeasurement(trueFrameNumber, final_sum)
			
			xValues.append(trueFrameNumber)
			yValues.append(final_sum)
			yMin = 0
			yMax = numpy.max(yValues)
			shortXArray = [trueFrameNumber]
			shortYArray = [final_sum]
			if arg.preview: ppgplot.pgslct(lightcurveView)
			if yMax > yAxisMax:
				yAxisMax = yMax * 1.1
				ppgplot.pgenv(startFrame, startFrame + frameRange, yMin, yAxisMax, 0, 0)
Ejemplo n.º 29
0
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
Ejemplo n.º 30
0

tp.lightcurve(info)

position = (xnew,ynew)



aperture = CircularAperture(position, r=radius)

bkg_aperture = CircularAnnulus(position, r_in=15., r_out=20.)

# 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_mean
bkg_sum = bkg_mean * aperture.area()

# plot the apertures
plt.imshow(scale_image(image, scale='sqrt', percent=98.), 
           origin='lower',cmap='gray')
aperture.plot(color='blue')
bkg_aperture.plot(color='cyan', hatch='//', alpha=0.8)
plt.xlim(xnew-100,xnew+100)
plt.ylim(ynew-100,ynew+100)

phot['bkg_sum'] = bkg_sum
Ejemplo n.º 31
0
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
Ejemplo n.º 32
0
def azimuthal_avg_radial_intensity(wave, rtout, plotname, dstar,
                                   annulus_width=10, rrange=[10,200], group=8, obs=None,
                                   other_obs=None):

    """
    The 'obs' option only works for Herschel PACS/SPIRE image.
    The 'obs' option now accept
    """

    import numpy as np
    import matplotlib as mpl
    # to avoid X server error
    mpl.use('Agg')
    from astropy.io import ascii, fits
    import matplotlib.pyplot as plt
    from photutils import aperture_photometry as ap
    from photutils import CircularAperture, CircularAnnulus
    from astropy import units as u
    from astropy.coordinates import SkyCoord
    from astropy import wcs
    from hyperion.model import ModelOutput
    import astropy.constants as const
    import os

    pc = const.pc.cgs.value
    AU = const.au.cgs.value

    # radial grid in arcsec
    # make the annulus center on
    r = np.arange(rrange[0], rrange[1], annulus_width, dtype=float) - annulus_width*0.5
    # r = np.arange(rrange[0], rrange[1], annulus_width, dtype=float) - annulus_width*0.55

    # source_center = '12 01 36.3 -65 08 53.0'

    def ExtractIntensityObs(rrange, annulus_width, obs):
        import numpy as np
        from astropy.io import fits
        from astropy.coordinates import SkyCoord
        from astropy import wcs
        from photutils import aperture_photometry as ap
        from photutils import CircularAperture, CircularAnnulus

        r = np.arange(rrange[0], rrange[1], annulus_width, dtype=float) - annulus_width*0.5
        # r = np.arange(rrange[0], rrange[1], annulus_width, dtype=float) - annulus_width*0.55

        imgpath = obs['imgpath']
        source_center = obs['source_center']
        # Read in data and set up coversions
        im_hdu = fits.open(imgpath)
        im = im_hdu[1].data
        wave = im_hdu[0].header['WAVELNTH']
        # error
        if (wave < 200.0) & (wave > 70.0):
            im_err = im_hdu[5].data
        elif (wave > 200.0) & (wave < 670.0):
            im_err = im_hdu[2].data
        else:
            im_err_exten = raw_input('The extension that includes the image error: ')
            im_err = im_hdu[int(im_err_exten)].data

        w = wcs.WCS(im_hdu[1].header)

        coord = SkyCoord(source_center, unit=(u.hourangle, u.deg))
        pixcoord = w.wcs_world2pix(coord.ra.degree, coord.dec.degree, 1)
        pix2arcsec = abs(im_hdu[1].header['CDELT1'])*3600.

        # determine whether need to convert the unit
        factor = 1
        print 'Image unit is ', im_hdu[1].header['BUNIT']
        if im_hdu[1].header['BUNIT'] != 'Jy/pixel':
            print 'Image unit is ', im_hdu[1].header['BUNIT']

            if im_hdu[1].header['BUNIT'] == 'MJy/sr':
                # convert intensity unit from MJy/sr to Jy/pixel
                factor = 1e6/4.25e10*abs(im_hdu[1].header['CDELT1']*im_hdu[1].header['CDELT2'])*3600**2
            else:
                factor = raw_input('What is the conversion factor to Jy/pixel?')

        I = np.empty_like(r[:-1])
        I_low = np.empty_like(r[:-1])
        I_hi = np.empty_like(r[:-1])
        I_err = np.empty_like(r[:-1])

        # for calculating the uncertainty from the variation within each annulus
        # construct the x- and y-matrix
        grid_x, grid_y = np.meshgrid(np.linspace(0,len(im[0,:])-1,len(im[0,:])),
                                     np.linspace(0,len(im[:,0])-1,len(im[:,0])))

        grid_dist = ((grid_x-pixcoord[0])**2+(grid_y-pixcoord[1])**2)**0.5

        # iteration
        for ir in range(len(r)-1):
            aperture = CircularAnnulus((pixcoord[0],pixcoord[1]), r_in=r[ir]/pix2arcsec, r_out=r[ir+1]/pix2arcsec)
            phot = ap(im, aperture, error=im_err)
            I[ir] = phot['aperture_sum'].data * factor / aperture.area()

            # uncertainty
            im_dum = np.where((grid_dist < r[ir+1]/pix2arcsec) & (grid_dist >= r[ir]/pix2arcsec), im, np.nan)

            # estimate the uncertainty by offsetting the annulus by +/- 1 pixel
            offset = -1
            if r[ir]/pix2arcsec + offset < 0:
                offset = -r[ir]/pix2arcsec
            aperture = CircularAnnulus((pixcoord[0],pixcoord[1]),
                                r_in=r[ir]/pix2arcsec + offset, r_out=r[ir+1]/pix2arcsec + offset)
            phot = ap(im, aperture, error=im_err)
            I_low[ir] = phot['aperture_sum'].data * factor / aperture.area()

            offset = 1
            aperture = CircularAnnulus((pixcoord[0],pixcoord[1]),
                                r_in=r[ir]/pix2arcsec + offset, r_out=r[ir+1]/pix2arcsec + offset)
            phot = ap(im, aperture, error=im_err)
            I_hi[ir] = phot['aperture_sum'].data * factor / aperture.area()

        I_err = (abs(I_low - I) + abs(I_hi - I))/2.

        return r, I, I_err

    if obs != None:
        I_obs = []
        for o in obs:
            if 'label' not in o.keys():
                label_dum = r'$\rm{observation}$'
                color_dum = 'g'
                linestyle_dum = '-'
                rrange_dum = rrange
                annulus_width_dum = annulus_width
            else:
                label_dum = o['label']
                color_dum = o['plot_color']
                linestyle_dum = o['plot_linestyle']
                rrange_dum = o['rrange']
                annulus_width_dum = o['annulus_width']

            r_dum, I_dum, I_err_dum = ExtractIntensityObs(rrange_dum, annulus_width_dum, o)
            # determine the label
            I_obs.append({'imgpath':o['imgpath'], 'r':r_dum, 'I':I_dum, 'I_err':I_err_dum, 'label': label_dum,
                          'plot_color':color_dum, 'plot_linestyle':linestyle_dum})

        # The first image should be the one to be compared primarily, and written out
        I = I_obs[0]['I']
        I_err = I_obs[0]['I_err']
        imgpath = I_obs[0]['imgpath']
        #

    # read in from RTout
    rtout = ModelOutput(rtout)

    im = rtout.get_image(group=group, inclination=0, distance=dstar*pc, units='Jy', uncertainties=True)
    factor = 1

    # Find the closest wavelength
    iwav = np.argmin(np.abs(wave - im.wav))
    # avoid zero when log, and flip the image
    val = im.val[::-1, :, iwav]
    unc = im.unc[::-1, :, iwav]

    w = np.degrees(max(rtout.get_quantities().r_wall) / im.distance) * 3600
    npix = len(val[:,0])
    pix2arcsec = 2*w/npix

    I_sim = np.empty_like(r[:-1])
    I_sim_hi = np.empty_like(r[:-1])
    I_sim_low = np.empty_like(r[:-1])
    I_sim_err = np.empty_like(r[:-1])

    # for calculating the uncertainty from the variation within each annulus
    # construct the x- and y-matrix
    grid_x, grid_y = np.meshgrid(np.linspace(0,npix-1,npix),
                                 np.linspace(0,npix-1,npix))

    dist_x = abs(grid_x - ((npix-1)/2.))
    dist_y = abs(grid_y - ((npix-1)/2.))

    grid_dist = (dist_x**2+dist_y**2)**0.5

    # iteration
    for ir in range(len(r)-1):
        aperture = CircularAnnulus((npix/2.+0.5, npix/2.+0.5),
                            r_in=r[ir]/pix2arcsec, r_out=r[ir+1]/pix2arcsec)
        phot = ap(val, aperture, error=unc)
        I_sim[ir] = phot['aperture_sum'].data / aperture.area()

        # uncertainty
        im_dum = np.where((grid_dist < r[ir+1]/pix2arcsec) & (grid_dist >= r[ir]/pix2arcsec), val, np.nan)
        # I_sim_err[ir] = phot['aperture_sum_err'].data / aperture.area()
        # I_sim_err[ir] = (np.nanstd(im_dum)**2+phot['aperture_sum_err'].data**2)**0.5 * factor / aperture.area()

        offset = -1
        aperture = CircularAnnulus((npix/2.+0.5, npix/2.+0.5),
                            r_in=r[ir]/pix2arcsec + offset, r_out=r[ir+1]/pix2arcsec + offset)
        phot = ap(val, aperture, error=unc)
        I_sim_low[ir] = phot['aperture_sum'].data * factor / aperture.area()

        offset = 1
        aperture = CircularAnnulus((npix/2.+0.5, npix/2.+0.5),
                            r_in=r[ir]/pix2arcsec + offset, r_out=r[ir+1]/pix2arcsec + offset)
        phot = ap(val, aperture, error=unc)
        I_sim_hi[ir] = phot['aperture_sum'].data * factor / aperture.area()

    I_sim_err = (abs(I_sim_low - I_sim)+ abs(I_sim_hi - I_sim))/2.


    if obs != None:
        # write the numbers into file
        foo = open(plotname+'_radial_profile_'+str(wave)+'um.txt', 'w')
        # print some header info
        foo.write('# wavelength '+str(wave)+' um \n')
        foo.write('# image file '+os.path.basename(imgpath)+' \n')
        foo.write('# annulus width '+str(annulus_width)+' arcsec \n')
        # write profiles
        foo.write('r_in \t I \t I_err \t I_sim \t I_sim_err \n')
        foo.write('# [arcsec] \t [Jy/pixel] \t [Jy/pixel] \t [Jy/pixel] \t [Jy/pixel] \n')
        for i in range(len(I)):
            foo.write('%f \t %e \t %e \t %e \t %e \n' % (r[i]+annulus_width/2., I[i], I_err[i], I_sim[i], I_sim_err[i]))
        foo.close()
    else:
        # write the numbers into file
        foo = open(plotname+'_radial_profile_'+str(wave)+'um.txt', 'w')
        # print some header info
        foo.write('# wavelength '+str(wave)+' um \n')
        foo.write('# annulus width '+str(annulus_width)+' arcsec \n')
        # write profiles
        foo.write('r_in \t I_sim \t I_sim_err \n')
        foo.write('# [arcsec] \t [Jy/pixel] \t [Jy/pixel] \n')
        for i in range(len(I_sim)):
            foo.write('%f \t %e \t %e \n' % (r[i]+annulus_width/2., I_sim[i], I_sim_err[i]))
        foo.close()

    # plot
    fig = plt.figure(figsize=(8,6))
    ax = fig.add_subplot(111)

    I_sim_hi = np.log10((I_sim+I_sim_err)/I_sim.max())-np.log10(I_sim/I_sim.max())
    I_sim_low = np.log10(I_sim/I_sim.max())-np.log10((I_sim-I_sim_err)/I_sim.max())
    i_sim = ax.errorbar(np.log10(r[:-1]*dstar), np.log10(I_sim/I_sim.max()), color='b',
                    yerr=(I_sim_low, I_sim_hi), marker='o', linestyle='-', mec='None', markersize=5,
                    ecolor='b', elinewidth=1.5, capthick=1.5, barsabove=True)

    if obs != None:
        plot_profile = []
        plot_label = []
        for o in I_obs:
            I_hi = np.log10((o['I']+o['I_err'])/o['I'].max())-np.log10(o['I']/o['I'].max())
            I_low = np.log10(o['I']/o['I'].max())-np.log10((o['I']-o['I_err'])/o['I'].max())
            i = ax.errorbar(np.log10(o['r'][:-1]*dstar), np.log10(o['I']/o['I'].max()), color=o['plot_color'],
                            yerr=(I_low, I_hi), marker='o', linestyle=o['plot_linestyle'], mec='None', markersize=5,
                            ecolor=o['plot_color'], elinewidth=1.5, capthick=1.5, barsabove=True)
            plot_profile.append(i)
            plot_label.append(o['label'])

        plot_profile.append(i_sim)
        plot_label.append(r'$\rm{simulation}$')
        ax.legend(plot_profile, plot_label,
                  fontsize=16, numpoints=1, loc='best')
    else:
        ax.legend([i_sim], [r'$\rm{simulation}$'], fontsize=16, numpoints=1, loc='best')

    # limit radius
    ax.axvline([np.log10(100*dstar)], color='k', linestyle='--', linewidth=1)
    #
    [ax.spines[axis].set_linewidth(1.5) for axis in ['top','bottom','left','right']]
    ax.minorticks_on()
    ax.tick_params('both',labelsize=18,width=1.5,which='major',pad=10,length=5)
    ax.tick_params('both',labelsize=18,width=1.5,which='minor',pad=10,length=2.5)
    ax.set_xlabel(r'$\rm{log(\it{b})\,[\rm{AU}]}$', fontsize=18)
    ax.set_ylabel(r'$\rm{log(I\,/\,I_{max})}$', fontsize=18)

    # fix the tick label font
    ticks_font = mpl.font_manager.FontProperties(family='STIXGeneral',size=18)
    for label in ax.get_xticklabels():
        label.set_fontproperties(ticks_font)
    for label in ax.get_yticklabels():
        label.set_fontproperties(ticks_font)

    fig.savefig(plotname+'_radial_profile_'+str(wave)+'um.pdf', format='pdf', dpi=300, bbox_inches='tight')
    fig.clf()
Ejemplo n.º 33
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()
Ejemplo n.º 34
0
def master_photometry(hdul, dtype=np.float32, method=None, sigma=3, fwhm=4,
                      kappa=5, r=3, r_in=5, r_out=9, save_format="fits",
                      saveto=None, overwrite=False):
    r"""
    Generate statistics, positions and more fore a given hdul object.
    This uses photutils package, see it for more information,
    also astropy tables could be relevant to look at.

    Parameters
    ----------
    hdul : astropy.io.fits.HDUList
        Image to process.
    dtype : data-type, optional
        Type to use for computing, defaults to np.float32.
    method : str, optional
        Method to use for normalization if value is not None.
        Valid values/metods are 'mean' or 'median'.
        Pay attention if you already used this with master_science,
        it's the same thing!
    sigma : float, optional
        Sigma level for mean, median and standard deviation, defaults to 3.
    fwhm : float, optional
        Expected FWHM in pixels, defaults to 4.
    kappa : float, optional
        Sigma level for source detection above background, defaults to 5.
    r : float, optional
        Aperture radius, defaults to 3.
    r_in : float, optional
        Inner sky annulus radius, defaults to 5.
    r_out : float, optional
        Outer sky annulus radius, defaults to 9.
    save_format : str, optional
        Format to save the table with, defaults to 'fits',
        has no effect if saveto is None.
    saveto : str, optional
        If this is not set (None, default) files won't be saved.
        If this is set to a string, save the file with the string as name.
    overwrite : bool, optional
        While saving, overwrite existing files? Defaults to False.

    Returns
    -------
    table : photutils.aperture_photometry
    """
    data = normalize(hdul[0].data.astype(dtype), method)
    mean, median, std = sigma_clipped_stats(data, sigma=sigma)
    DAOfind = DAOStarFinder(fwhm=fwhm, threshold=kappa*std)
    positions = DAOfind(data-median)
    centroid = (positions["xcentroid"], positions["ycentroid"])
    aperture = CircularAperture(centroid, r)
    annulus = CircularAnnulus(centroid, r_in, r_out)
    apertures = [aperture, annulus]
    table = aperture_photometry(data, apertures)
    bg_mean = table["aperture_sum_1"] / annulus.area()
    bg_sum = bg_mean * aperture.area()
    table["residual_aperture_sum"] = table["aperture_sum_0"] - bg_sum
    table["mag"] = - 2.5 * np.log10(table["residual_aperture_sum"])
    if saveto is not None:
        # TODO: dtype?
        table.write(saveto, overwrite=overwrite, format=save_format)
    return table
    edgeStars = np.logical_or(
                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}'
Ejemplo n.º 36
0
def time_series(xcenter, ycenter, filenames, r = None, r_in = None, r_out = None, rs_in = None, rs_out = None, flat_name = False, w = None, h = None, w_in = None, w_out = None, h_out = None, ws_in = None, ws_out = None, hs_out = None, red = False, red2 = False, bg_xcen = None, bg_ycen = None, mode = "astropy", src_shape = "Circ", bkg_shape = "Circ", average = "med"):

    flux_table = Table(names = ('raw_flux', 'bkg_flux', 'res_flux', 'time'))
    
    for i, hdu in enumerate(filenames):
        test_im = test_image(filename = hdu, r = red, r2 = red2, f_name = flat_name)
        image2d, time, header, mask = test_im[0], test_im[1], test_im[2], test_im[3]
        ap_phot = photometry(image2d, xcenter, ycenter, mask, index = i, shape = src_shape, rad = r, r_in = rs_in, r_out = rs_out, ht = h, wid = w, w_in = ws_in, w_out = ws_out, h_out = hs_out)
        raw_flux = ap_phot[0]
        source_ap = ap_phot[1]
       
        
        if mode == "astropy":
            if bkg_shape == "Circ":
                bkg_ap = CircularAnnulus((xcenter[i], ycenter[i]), r_in = r_in, r_out = r_out)
                bkg = aperture_photometry(image2d, bkg_ap, mask = mask)
                bkg_mean = bkg['aperture_sum']/bkg_ap.area()
            
            elif bkg_shape == "Rect":
                bkg_ap = photometry(image2d, bg_xcen, bg_ycen, mask, index = i, shape = bkg_shape, ht = h, wid = w)[1]
                bkg = aperture_photometry(image2d, bkg_ap, mask = mask)
                bkg_mean = bkg['aperture_sum']/bkg_ap.area()
            
            elif bkg_shape == "RectAnn":
                bkg_ap = RectangularAnnulus((xcenter[i], ycenter[i]), w_in = w_in, w_out = w_out, h_out = h_out, 
                                                   theta = 0.0)
                bkg = aperture_photometry(image2d, bkg_ap, mask = mask)
                bkg_mean = bkg['aperture_sum']/bkg_ap.area()
                
            else:
                warnings.warn("Not a recognized astropy shape")
            
            bkg_flux = bkg_mean*source_ap.area()
            res_flux = raw_flux - bkg_flux
                

        elif mode == "shapes":
            y, x = np.mgrid[:image2d.shape[0], :image2d.shape[1]]
            if bkg_shape == "Circ":
                bkg_pts = ((((x - xcenter[i])**2 + (y - ycenter[i])**2) > (r_in)**2) & 
                               (((x - xcenter[i])**2 + (y -ycenter[i])**2) < (r_out)**2))
            elif bkg_shape == "CIS":
                bkg_pts = ((((x - xcenter[i])**2 + (y - ycenter[i])**2) > (r_in)**2) & 
                               ((np.abs(x - xcenter[i]) < r_out) & (np.abs(y -ycenter[i]) < r_out)))
            else:
                warnings.warn("Not a recognized shape")

            if average == "med":
                bkg_med = np.nanmedian(image2d[bkg_pts])
            elif average == "avg":
                bkg_med = np.nanmean(image2d[bkg_pts])
            elif average =="mad":
                ad = np.abs(image2d[bkg_pts]-np.nanmedian(image2d[bkg_pts]))
                mad = np.nanmedian(ad)
                keep_pts = (np.abs(image2d-np.nanmedian(image2d[bkg_pts]))<(5*mad)) & bkg_pts
                bkg_med = np.nanmean(image2d[keep_pts])
            else:
                warnings.warn("Not a recognized average")

            bkg_flux = bkg_med*(np.pi*(r**2))
            res_flux = raw_flux - bkg_flux
            
        elif mode == "col_col":
            new_im = col_col(image2d, mask, xcenter[i], ycenter[i], r, box = 150)
            bkg_flux = 0
            res_flux = photometry(new_im, xcenter, ycenter, mask, index = i, shape = 'Circ', rad = r)[0]
            
        elif mode == "row_row":
            new_im = row_row(image2d, mask, xcenter[i], ycenter[i], r, box = 150)
            bkg_flux = 0
            res_flux = photometry(new_im, xcenter, ycenter, mask, index = i, shape = 'Circ', rad = r)[0]
            
        else:
            raise Warning("Not a recognized mode")
        
        flux_table.add_row([raw_flux, bkg_flux, res_flux, time])

    return flux_table
Ejemplo n.º 37
0
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
Ejemplo n.º 38
0
    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
Ejemplo n.º 39
0
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)
Ejemplo n.º 40
0
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
Ejemplo n.º 41
0
class PhotutilsAperturePhotometry(Block):
    r"""
    Aperture photometry using the :code:`CircularAperture` and :code:`CircularAnnulus` of photutils_ with a wide range of apertures. By default annulus goes from 5 fwhm to 8 fwhm and apertures from 0.1 to 10 times the fwhm with 0.25 steps (leading to 40 apertures).

    The error (e.g. in ADU) is then computed following:

    .. math::
    
        \sigma = \sqrt{S + (A_p + \frac{A_p}{A_n})(b + r^2 + \frac{gain^2}{2}) + scint }


    .. image:: images/aperture_phot.png
        :align: center
        :width: 110px

    with :math:`S` the flux (ADU) within an aperture of area :math:`A_p`, :math:`b` the background flux (ADU) within an annulus of area :math:`A_n`, :math:`r` the read-noise (ADU) and :math:`scint` is a scintillation term expressed as:


    .. math::

        scint = \frac{S_fd^{2/3} airmass^{7/4} h}{16T}

    with :math:`S_f` a scintillation factor, :math:`d` the aperture diameter (m), :math:`h` the altitude (m) and :math:`T` the exposure time.

    The positions of individual stars are taken from :code:`Image.stars_coords` so one of the detection block should be used, placed before this one.

    For more details check https://photutils.readthedocs.io/en/stable/aperture.html

    |write| 
    
    - ``Image.stars_coords``
    - ``Image.apertures_area``
    - ``Image.sky``
    - ``Image.fluxes``
    - ``Image.annulus_area``
    - ``Image.annulus_rin``
    - ``Image.annulus_rout``
    - ``Image.apertures_radii``
    - ``Image.fluxes``

    |modify| 

    Parameters
    ----------
    apertures : ndarray or list, optional
        apertures in fraction of fwhm, by default None, i.e. np.arange(0.1, 8, 0.25)
    r_in : int, optional
        radius of the inner annulus in fraction of fwhm, by default 5
    r_out : int, optional
        radius of the outer annulus in fraction of fwhm, by default 8
    scale: bool or float:
        Multiplication factor applied to `apertures`.
        - if True: `apertures` multiplied by image.fwhm, varying for each image
        - if False: `apertures` not multiplied
        - if float: `apertures` multiplied `scale` and held fixed for all images
    """
    def __init__(self,
                 apertures=None,
                 r_in=5,
                 r_out=8,
                 scale=True,
                 sigclip=2.,
                 **kwargs):

        super().__init__(**kwargs)
        if apertures is None:
            self.apertures = np.arange(0.1, 8, 0.25)
        else:
            self.apertures = apertures

        self.annulus_inner_radius = r_in
        self.annulus_outer_radius = r_out
        self.annulus_final_rin = None
        self.annulus_final_rout = None
        self.aperture_final_r = None

        self.n_apertures = len(self.apertures)
        self.n_stars = None
        self.circular_apertures = None
        self.annulus_apertures = None
        self.annulus_masks = None

        self.circular_apertures_area = None
        self.annulus_area = None
        self.scale = scale
        self.sigclip = sigclip

        self._has_fix_scale = not isinstance(self.scale, bool)

    def set_apertures(self, stars_coords, fwhm=1):

        self.annulus_final_rin = self.annulus_inner_radius * fwhm
        self.annulus_final_rout = self.annulus_outer_radius * fwhm
        self.aperture_final_r = fwhm * self.apertures

        self.annulus_apertures = CircularAnnulus(
            stars_coords,
            r_in=self.annulus_final_rin,
            r_out=self.annulus_final_rout,
        )
        if callable(self.annulus_apertures.area):
            self.annulus_area = self.annulus_apertures.area()
        else:
            self.annulus_area = self.annulus_apertures.area

        self.circular_apertures = [
            CircularAperture(stars_coords, r=r) for r in self.aperture_final_r
        ]

        # Unresolved buf; sometimes circular_apertures.area is a method, sometimes a float
        if callable(self.circular_apertures[0].area):
            self.circular_apertures_area = [
                ca.area() for ca in self.circular_apertures
            ]
        else:
            self.circular_apertures_area = [
                ca.area for ca in self.circular_apertures
            ]

        self.annulus_masks = self.annulus_apertures.to_mask(method="center")
        self.n_stars = len(stars_coords)

    def run(self, image):
        try:
            if self._has_fix_scale:
                self.set_apertures(image.stars_coords, self.scale)
            elif self.scale:
                self.set_apertures(image.stars_coords, image.fwhm)
            else:
                self.set_apertures(image.stars_coords)
        except ZeroDivisionError:  # temporary
            image.discard = True
            return None

        bkg_median = []
        for mask in self.annulus_masks:
            annulus_data = mask.multiply(image.data)
            if annulus_data is not None:
                annulus_data_1d = annulus_data[mask.data > 0]
                _, median_sigma_clip, _ = sigma_clipped_stats(
                    annulus_data_1d, sigma=self.sigclip)
                bkg_median.append(median_sigma_clip)
            else:
                bkg_median.append(0.)

        bkg_median = np.array(bkg_median)

        image.apertures_area = self.circular_apertures_area
        image.annulus_sky = bkg_median
        image.sky = bkg_median.mean()
        image.fluxes = np.zeros((self.n_apertures, self.n_stars))
        image.annulus_area = self.annulus_area
        image.annulus_rin = self.annulus_final_rin
        image.annulus_rout = self.annulus_final_rout
        image.apertures_radii = self.aperture_final_r

        data = image.data.copy()
        data[data < 0] = 0

        photometry = aperture_photometry(data, self.circular_apertures)
        fluxes = np.array([
            photometry[f"aperture_sum_{a}"] -
            (bkg_median * self.circular_apertures_area[a])
            for a in range(len(self.apertures))
        ])

        # dummy values if negative or nan
        fluxes[np.isnan(fluxes)] = 1
        fluxes[fluxes < 0] = 1

        image.fluxes = fluxes

        self.compute_error(image)
        image.header["sky"] = np.mean(image.sky)

    def compute_error(self, image):

        image.errors = np.zeros((self.n_apertures, self.n_stars))

        for i, aperture_area in enumerate(self.circular_apertures_area):
            area = aperture_area * (1 + aperture_area / self.annulus_area)
            image.errors[i, :] = image.telescope.error(
                image.fluxes[i],
                area,
                image.sky,
                image.exposure,
                airmass=image.get("keyword_airmass"),
            )

    def citations(self):
        return "astropy", "photutils"
Ejemplo n.º 42
0
#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)
Ejemplo n.º 43
0
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)
Ejemplo n.º 44
0
    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
Ejemplo n.º 45
0
    def aperture_photometry(self, xStars, yStars, starApr, skyAprIn, skyAprOut):
        """
        Computes the aperture photometry for the specified locations

        Paramaters
        ----------
        xStars : array_like (length - numStars)
            An array of star locations (in pixels) along the x-axis

        yStars : array_like (length - numStars)
            An array of star locations (in pixels) along the y-axis

        starApr : scalar or array_like (length - numApr)
            The size of the circular aperture (in pixels) within which to sum
            up the star counts.

        skyAprIn : int or float
            The inner radius (in pixels) of the circular annulus within which to
            sum up the sky counts.

        skyAprOut : int or float
            The outer radius (in pixels) of the cirucal annulus within which to
            sum up the sky counts.

        Returns
        -------
        instrumentalMagnitudes : numpy.ndarray (shape - (numStars, numApr))
            Instrumental magnitudes computed using the supplied parameters
        """
        multipleApr = hasattr(starApr, '__iter__')
        if multipleApr:
            # Construct the stellar apertures
            starApertures = [CircularAperture((xStars, yStars), r=r) for r in starApr]
        else:
            # Treat the starApr variable as a scalar
            try:
                starApr = float(starApr)
                starApertures = CircularAperture((xStars, yStars), r=starApr)
            except:
                raise

        # Compute the raw stellar photometry
        starRawPhotTable = aperture_photometry(
            self.image.data,
            starApertures,
            error=self.image.uncertainty,
            pixelwise_error=True
        )

        # Construct the sky apertures
        skyApertures = CircularAnnulus((xStars, yStars),
            r_in=skyAprIn, r_out=skyAprOut)

        # Compute the raw sky photometry
        skyRawPhotTable = aperture_photometry(
            self.image.data,
            skyApertures,
            error=self.image.uncertainty,
            pixelwise_error=True
        )

        # Compute the mean packgroud value at each star
        bkg_mean = skyRawPhotTable['aperture_sum'] / skyApertures.area()

        # Subtract the average sky background and store the resultself
        if multipleApr:
            bkg_sum = [bkg_mean * sa.area() for sa in starApertures]
            subtractedStarPhot = np.array([
                starRawPhotTable['aperture_sum_{}'.format(i)] - bkg_sum[i]
                for i in range(len(starApr))])

            # Compute the uncertainty in the background subtracted photometry.
            subtractedPhotUncert = np.array([
                np.sqrt(
                    starRawPhotTable['aperture_sum_err_{}'.format(i)]**2 +
                    skyRawPhotTable['aperture_sum_err']**2
                ) for i in range(len(starApr))
            ])
        else:
            bkg_sum = bkg_mean * starApertures.area()
            subtractedStarPhot = starRawPhotTable['aperture_sum'] - bkg_sum
            subtractedPhotUncert = np.sqrt(
                starRawPhotTable['aperture_sum_err']**2 +
                skyRawPhotTable['aperture_sum_err']**2
            )

        return subtractedStarPhot, subtractedPhotUncert
Ejemplo n.º 46
0
    def ExtractIntensityObs(rrange, annulus_width, obs):
        import numpy as np
        from astropy.io import fits
        from astropy.coordinates import SkyCoord
        from astropy import wcs
        from photutils import aperture_photometry as ap
        from photutils import CircularAperture, CircularAnnulus

        r = np.arange(rrange[0], rrange[1], annulus_width, dtype=float) - annulus_width*0.5
        # r = np.arange(rrange[0], rrange[1], annulus_width, dtype=float) - annulus_width*0.55

        imgpath = obs['imgpath']
        source_center = obs['source_center']
        # Read in data and set up coversions
        im_hdu = fits.open(imgpath)
        im = im_hdu[1].data
        wave = im_hdu[0].header['WAVELNTH']
        # error
        if (wave < 200.0) & (wave > 70.0):
            im_err = im_hdu[5].data
        elif (wave > 200.0) & (wave < 670.0):
            im_err = im_hdu[2].data
        else:
            im_err_exten = raw_input('The extension that includes the image error: ')
            im_err = im_hdu[int(im_err_exten)].data

        w = wcs.WCS(im_hdu[1].header)

        coord = SkyCoord(source_center, unit=(u.hourangle, u.deg))
        pixcoord = w.wcs_world2pix(coord.ra.degree, coord.dec.degree, 1)
        pix2arcsec = abs(im_hdu[1].header['CDELT1'])*3600.

        # determine whether need to convert the unit
        factor = 1
        print 'Image unit is ', im_hdu[1].header['BUNIT']
        if im_hdu[1].header['BUNIT'] != 'Jy/pixel':
            print 'Image unit is ', im_hdu[1].header['BUNIT']

            if im_hdu[1].header['BUNIT'] == 'MJy/sr':
                # convert intensity unit from MJy/sr to Jy/pixel
                factor = 1e6/4.25e10*abs(im_hdu[1].header['CDELT1']*im_hdu[1].header['CDELT2'])*3600**2
            else:
                factor = raw_input('What is the conversion factor to Jy/pixel?')

        I = np.empty_like(r[:-1])
        I_low = np.empty_like(r[:-1])
        I_hi = np.empty_like(r[:-1])
        I_err = np.empty_like(r[:-1])

        # for calculating the uncertainty from the variation within each annulus
        # construct the x- and y-matrix
        grid_x, grid_y = np.meshgrid(np.linspace(0,len(im[0,:])-1,len(im[0,:])),
                                     np.linspace(0,len(im[:,0])-1,len(im[:,0])))

        grid_dist = ((grid_x-pixcoord[0])**2+(grid_y-pixcoord[1])**2)**0.5

        # iteration
        for ir in range(len(r)-1):
            aperture = CircularAnnulus((pixcoord[0],pixcoord[1]), r_in=r[ir]/pix2arcsec, r_out=r[ir+1]/pix2arcsec)
            phot = ap(im, aperture, error=im_err)
            I[ir] = phot['aperture_sum'].data * factor / aperture.area()

            # uncertainty
            im_dum = np.where((grid_dist < r[ir+1]/pix2arcsec) & (grid_dist >= r[ir]/pix2arcsec), im, np.nan)

            # estimate the uncertainty by offsetting the annulus by +/- 1 pixel
            offset = -1
            if r[ir]/pix2arcsec + offset < 0:
                offset = -r[ir]/pix2arcsec
            aperture = CircularAnnulus((pixcoord[0],pixcoord[1]),
                                r_in=r[ir]/pix2arcsec + offset, r_out=r[ir+1]/pix2arcsec + offset)
            phot = ap(im, aperture, error=im_err)
            I_low[ir] = phot['aperture_sum'].data * factor / aperture.area()

            offset = 1
            aperture = CircularAnnulus((pixcoord[0],pixcoord[1]),
                                r_in=r[ir]/pix2arcsec + offset, r_out=r[ir+1]/pix2arcsec + offset)
            phot = ap(im, aperture, error=im_err)
            I_hi[ir] = phot['aperture_sum'].data * factor / aperture.area()

        I_err = (abs(I_low - I) + abs(I_hi - I))/2.

        return r, I, I_err
Ejemplo n.º 47
0
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
Ejemplo n.º 48
0
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
Ejemplo n.º 49
0
Archivo: ifu.py Proyecto: sosey/jwst
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)
Ejemplo n.º 50
0
    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']
Ejemplo n.º 51
0
                                 r=apr)  # pixels
    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']
Ejemplo n.º 52
0
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)