Ejemplo n.º 1
0
    def aper_phot(self,x,y):
        """Perform aperture photometry, uses photutils functions
        
            Rapert,  sum,  area  and  flux  are  the  radius  of the aperture in
            pixels, the total number of counts including sky  in  the  aperture,
            the  area  of the aperture in square pixels, and the total number of
            counts in  the  aperture  excluding  sky.   Mag  and  merr  are  the
            magnitude and error in the magnitude in the aperture (see below).

                    flux = sum - area * msky
                     mag = zmag - 2.5 * log10 (flux) + 2.5 * log10 (itime)
                    merr = 1.0857 * error / flux
                   error = sqrt (flux / epadu + area * stdev**2 +
                           area**2 * stdev**2 / nsky)      
          """
        sigma=0. #no centering
        amp=0. #no centering
        if self.aperphot_pars["center"][0]:
            center=True
            delta=10
            popt=self.gauss_center(x,y,delta)
            if 5 > popt.count(0) > 1: #an error occurred in centering
                warnings.warn("Problem fitting center, using original coordinates")                            
            else:
                amp,x,y,sigma,offset=popt
        
        radius=int(self.aperphot_pars["radius"][0])
        width=int(self.aperphot_pars["width"][0])
        inner=int(self.aperphot_pars["skyrad"][0])
        subsky=bool(self.aperphot_pars["subsky"][0])  
        
        aper_flux=photutils.aperture_photometry(self._data,x,y,apertures=photutils.CircularAperture(radius),subpixels=1,method="center")
        aperture_area = np.pi * (radius)**2
        if subsky:
            outer=inner + width
            annulus_sky=photutils.annulus_circular(self._data,x,y,inner,outer)
            annulus_area = np.pi * (outer**2 - inner**2)

            total_flux=aper_flux - annulus_sky  * (aperture_area / annulus_area)
        else:   
            total_flux=aper_flux

        #compute the magnitude of the sky corrected flux    
        magzero=float(self.aperphot_pars["zmag"][0])
        mag=magzero-2.5*(np.log10(total_flux))
        
        pheader=("x\ty\tradius\tflux\tmag(zpt={0:0.2f})\tsky".format(magzero))
        if center:
            pheader+=("\tfwhm")
            pstr="\n{0:.2f}\t{1:0.2f}\t{2:d}\t{3:0.2f}\t{4:0.2f}\t{5:0.2f}\t{6:0.2f}".format(x,y,radius,total_flux,mag,annulus_sky/annulus_area,math_helper.gfwhm(sigma))
        else:
            pstr="\n{0:0.2f}\t{1:0.2f}\t{2:d}\t{3:0.2f}\t{4:0.2f}\t{5:0.2f}".format(x,y,radius,total_flux,mag,annulus_sky/annulus_area,)

        print(pheader+pstr)
        logging.info(pheader + pstr)
Ejemplo n.º 2
0
    def _aperture_phot(self,
                       x,
                       y,
                       radsize=1,
                       sky_inner=5,
                       skywidth=5,
                       method="subpixel",
                       subpixels=4):
        """Perform sky subtracted aperture photometry, uses photutils functions, photutil must be installed

        Parameters
        ----------
        radsize: int
            Size of the radius

        sky_inner: int
            Inner radius of the sky annulus

        skywidth: int
            Width of the sky annulus

        method: string
            Pixel sampling method to use

        subpixels: int
            How many subpixels to use

        Notes
        -----
           background is taken from sky annulus pixels, check into masking bad pixels

        """
        if not photutils_installed:
            print("Install photutils to enable")
        else:

            aper_flux = photutils.aperture_circular(self._data,
                                                    x,
                                                    y,
                                                    radsize,
                                                    subpixels=subpixels,
                                                    method=method)
            aperture_area = np.pi * (radsize)**2

            annulus_sky = photutils.annulus_circular(self._data, x, y,
                                                     sky_inner,
                                                     sky_inner + skywidth)
            outer = sky_inner + skywidth
            inner = sky_inner
            annulus_area = np.pi * (outer**2 - inner**2)

            skysub_flux = aper_flux - annulus_sky * (aperture_area /
                                                     annulus_area)

            return (aper_flux, annulus_sky, skysub_flux)
Ejemplo n.º 3
0
    def aper_phot(self, x, y):
        """Perform aperture photometry, uses photutils functions, photutils must be available

        """
        if not photutils_installed:
            print("Install photutil to enable")
        else:

            sigma = 0.  # no centering
            amp = 0.  # no centering
            if self.aperphot_pars["center"][0]:
                center = True
                delta = 10
                popt = self.gauss_center(x, y, delta)
                if 5 > popt.count(0) > 1:  # an error occurred in centering
                    warnings.warn("Problem fitting center, using original coordinates")
                else:
                    amp, x, y, sigma, offset = popt

            radius = int(self.aperphot_pars["radius"][0])
            width = int(self.aperphot_pars["width"][0])
            inner = int(self.aperphot_pars["skyrad"][0])
            subsky = bool(self.aperphot_pars["subsky"][0])

            aper_flux = photutils.aperture_photometry(
                self._data, x, y, apertures=photutils.CircularAperture(radius), subpixels=1, method="center")
            aperture_area = np.pi * (radius) ** 2
            if subsky:
                outer = inner + width
                annulus_sky = photutils.annulus_circular(self._data, x, y, inner, outer)
                annulus_area = np.pi * (outer ** 2 - inner ** 2)

                total_flux = aper_flux - annulus_sky * (aperture_area / annulus_area)
            else:
                total_flux = aper_flux

            # compute the magnitude of the sky corrected flux
            magzero = float(self.aperphot_pars["zmag"][0])
            mag = magzero - 2.5 * (np.log10(total_flux))

            pheader = (
                "x\ty\tradius\tflux\tmag(zpt={0:0.2f})\tsky\t".format(magzero)).expandtabs(15)
            if center:
                pheader += ("fwhm")
                pstr = "\n{0:.2f}\t{1:0.2f}\t{2:d}\t{3:0.2f}\t{4:0.2f}\t{5:0.2f}\t{6:0.2f}".format(
                    x + 1, y + 1, radius, total_flux, mag, annulus_sky / annulus_area, math_helper.gfwhm(sigma)).expandtabs(15)
            else:
                pstr = "\n{0:0.2f}\t{1:0.2f}\t{2:d}\t{3:0.2f}\t{4:0.2f}\t{5:0.2f}".format(
                    x + 1, y + 1, radius, total_flux, mag, annulus_sky / annulus_area,).expandtabs(15)

            print(pheader + pstr)
            logging.info(pheader + pstr)
Ejemplo n.º 4
0
    def _aperture_phot(self,x,y,radsize=1,sky_inner=5,skywidth=5,method="subpixel",subpixels=4):
        """Perform sky subtracted aperture photometry, uses photutils functions
        
           background is taken from sky annulus pixels, check into masking
        """
        
        aper_flux=photutils.aperture_circular(self._data,x,y,radsize,subpixels=subpixels,method=method)
        aperture_area = np.pi * (radsize)**2

        annulus_sky=photutils.annulus_circular(self._data,x,y,sky_inner,sky_inner+skywidth)
        outer=sky_inner+skywidth
        inner=sky_inner
        annulus_area = np.pi * (outer**2 - inner**2)

        skysub_flux=aper_flux - annulus_sky  * (aperture_area / annulus_area)

        return (aper_flux,annulus_sky,skysub_flux)
Ejemplo n.º 5
0
    def _aperture_phot(self, x, y, radsize=1, sky_inner=5, skywidth=5, method="subpixel", subpixels=4):
        """Perform sky subtracted aperture photometry, uses photutils functions, photutil must be installed

        Parameters
        ----------
        radsize: int
            Size of the radius

        sky_inner: int
            Inner radius of the sky annulus

        skywidth: int
            Width of the sky annulus

        method: string
            Pixel sampling method to use

        subpixels: int
            How many subpixels to use

        Notes
        -----
           background is taken from sky annulus pixels, check into masking bad pixels

        """
        if not photutils_installed:
            print("Install photutils to enable")
        else:

            aper_flux = photutils.aperture_circular(
                self._data, x, y, radsize, subpixels=subpixels, method=method)
            aperture_area = np.pi * (radsize) ** 2

            annulus_sky = photutils.annulus_circular(
                self._data, x, y, sky_inner, sky_inner + skywidth)
            outer = sky_inner + skywidth
            inner = sky_inner
            annulus_area = np.pi * (outer ** 2 - inner ** 2)

            skysub_flux = aper_flux - annulus_sky * (aperture_area / annulus_area)

            return (aper_flux, annulus_sky, skysub_flux)
Ejemplo n.º 6
0
    def aper_phot(self, x, y):
        """Perform aperture photometry, uses photutils functions, photutils must be available

        """
        if not photutils_installed:
            print("Install photutil to enable")
        else:

            sigma = 0.  # no centering
            amp = 0.  # no centering
            if self.aperphot_pars["center"][0]:
                center = True
                delta = 10
                popt = self.gauss_center(x, y, delta)
                if 5 > popt.count(0) > 1:  # an error occurred in centering
                    warnings.warn(
                        "Problem fitting center, using original coordinates")
                else:
                    amp, x, y, sigma, offset = popt

            radius = int(self.aperphot_pars["radius"][0])
            width = int(self.aperphot_pars["width"][0])
            inner = int(self.aperphot_pars["skyrad"][0])
            subsky = bool(self.aperphot_pars["subsky"][0])

            aper_flux = photutils.aperture_photometry(
                self._data,
                x,
                y,
                apertures=photutils.CircularAperture(radius),
                subpixels=1,
                method="center")
            aperture_area = np.pi * (radius)**2
            if subsky:
                outer = inner + width
                annulus_sky = photutils.annulus_circular(
                    self._data, x, y, inner, outer)
                annulus_area = np.pi * (outer**2 - inner**2)

                total_flux = aper_flux - annulus_sky * (aperture_area /
                                                        annulus_area)
            else:
                total_flux = aper_flux

            # compute the magnitude of the sky corrected flux
            magzero = float(self.aperphot_pars["zmag"][0])
            mag = magzero - 2.5 * (np.log10(total_flux))

            pheader = ("x\ty\tradius\tflux\tmag(zpt={0:0.2f})\tsky\t".format(
                magzero)).expandtabs(15)
            if center:
                pheader += ("fwhm")
                pstr = "\n{0:.2f}\t{1:0.2f}\t{2:d}\t{3:0.2f}\t{4:0.2f}\t{5:0.2f}\t{6:0.2f}".format(
                    x + 1, y + 1, radius,
                    total_flux, mag, annulus_sky / annulus_area,
                    math_helper.gfwhm(sigma)).expandtabs(15)
            else:
                pstr = "\n{0:0.2f}\t{1:0.2f}\t{2:d}\t{3:0.2f}\t{4:0.2f}\t{5:0.2f}".format(
                    x + 1,
                    y + 1,
                    radius,
                    total_flux,
                    mag,
                    annulus_sky / annulus_area,
                ).expandtabs(15)

            print(pheader + pstr)
            logging.info(pheader + pstr)
Ejemplo n.º 7
0
def aperture(image, hdr):
    global iap, pguess_old, nstars, svec
    dnorm = 500.
    rann1 = 18.
    dann = 2.
    rann2 = rann1 + dann
    app_min = 1.
    app_max = 19.
    dapp = 1.
    app_sizes = np.arange(app_min, app_max, dapp)

    # If first time through, read in "guesses" for locations of stars
    if iap == 0:
        var = np.loadtxt('phot_coords')
        xvec = var[:, 0]
        yvec = var[:, 1]
        nstars = len(xvec)
        #print app_sizes,'\n'
    else:
        xvec = svec[:, 0]
        yvec = svec[:, 1]

    # Find locations of stars
    dxx0 = 10.
    for i in range(nstars):
        xx0 = [xvec[i], yvec[i]]
        xbounds = (xx0[0] - dxx0, xx0[0] + dxx0)
        ybounds = (xx0[1] - dxx0, xx0[1] + dxx0)
        #res = sco.minimize(center, xx0, method='BFGS', jac=der_center)
        #res = sco.fmin_tnc(center, xx0, bounds=(xbounds,ybounds))
        #res = sco.minimize(center, xx0, method='tnc', bounds=(xbounds,ybounds))
        res = sco.minimize(center,
                           xx0,
                           method='L-BFGS-B',
                           bounds=(xbounds, ybounds),
                           jac=der_center)
        xx0 = res.x
        xvec[i] = xx0[0]
        yvec[i] = xx0[1]

    # Calculate sky around stars
    sky = photutils.annulus_circular(image,
                                     xvec,
                                     yvec,
                                     rann1,
                                     rann2,
                                     method='exact',
                                     subpixels=10)

    # Do psf fits to stars. Results are stored in arrays fwhm, pflux, psky, psf_x, and psf_y
    fwhm = np.zeros(nstars)

    # Make stacked array of star positions from aperture photometry
    svec = np.dstack((xvec, yvec))[0]
    #print svec

    # Make stacked array of star positions from PSF fitting
    # pvec = np.dstack((psf_x,psf_y))[0]
    pvec = svec

    iap = iap + 1

    starr = []
    apvec = []
    app = -1.0

    # Get time of observation from the header
    #date = hdr['DATE-OBS']   # for Argos files
    #utc  = hdr['UTC']         # for Argos files
    date = hdr['UTC-DATE']  # for Pro-EM files
    utc = hdr['UTC-BEG']  # for Pro-EM files
    times = date + "  " + utc
    t = Time(times, format='iso', scale='utc')
    # Calculate Julian Date of observation
    jd = t.jd
    for app in app_sizes:
        flux = photutils.aperture_circular(image,
                                           xvec,
                                           yvec,
                                           app,
                                           method='exact',
                                           subpixels=10)
        skyc = sky * app**2 / (rann2**2 - rann1**2)
        fluxc = flux - skyc
        starr.append([fluxc, skyc, fwhm])
        apvec.append(app)
    starr = np.array(starr)
    apvec = np.array(apvec)
    #print starr
    return jd, svec, pvec, apvec, starr