Пример #1
0
def get_elliptical_aperture(data,isophote,mask=None,skyp=2):
    lenofell=len(isophote)
    sky_mean, sky_median, sky_std = sigma_clipped_stats(data, sigma=3.0, iters=5)
    datap=data-sky_median
    sky=sky_median
    DeltaSKY=0
    ellip=isophote['col6']
    ex_0=isophote['col10']
    ey_0=isophote['col12']
    ePA=isophote['col8']
    eSMA=isophote['col1']
    fluxList=[]
    aperarea=[]
    for loop in range(lenofell):
        a=eSMA[loop]
        b=a*( 1 - (ellip[loop]) )
        posi = [ex_0[loop], ey_0[loop]]
        apertures = EllipticalAperture(positions=posi,a=a,b=b,theta=(ePA[loop]+90))
        rawflux_table = aperture_photometry(datap, apertures, mask=mask)
        aperturesMaskedArea = aperture_photometry(mask.astype(int), apertures)['aperture_sum']
        aperarea.append((apertures.area()-aperturesMaskedArea[0]))
        fluxList.append(rawflux_table['aperture_sum'][0])
        print ("{0} percent done".format(100*loop/lenofell) )
    for loop in range(skyp) :
        antieerFunc=interp1d(fluxList,eSMA)
        aper1=1.4*antieerFunc(0.9*fluxList[lenofell-1])
        deltaSKY=0
        mass=0
        if(aper1 < 0.5*eSMA[lenofell-1]):
            aper1=0.5*eSMA[lenofell-1]
        if(aper1 > 0.75*eSMA[lenofell-1]):
            aper1=0.5*eSMA[lenofell-1]
        bignumber=0
        for loop2 in range(lenofell) :
            if eSMA[loop2] > aper1 :
                bignumber=loop2
                break
        loop3=bignumber+1
        while (loop3 < lenofell) :
            deltaSKy=(fluxList[loop3]-fluxList[bignumber])/(aperarea[loop3]-aperarea[bignumber])
            deltaSKY += deltaSKy*(pow(loop3,3))
            loop3+=1
            mass += pow(loop3,3)
        Dsky=deltaSKY/mass
        DeltaSKY += 0.3*Dsky
        for loop3 in range(lenofell) :
            fluxList[loop3] += (-0.3)*Dsky*(aperarea[loop3])
        print ("{0} percent done".format(100*loop/lenofell) )
        fig = plt.figure(figsize=(8, 6))
        plt.plot(eSMA, fluxList, label='Curve of Growth')
        plt.axvline(x=1.4*antieerFunc(0.95*fluxList[lenofell-1]), linestyle='--', color='b',   label='standard aperture')
        plt.xlabel('Semi-major axis length [pixel]', fontsize=16)
        plt.legend(loc='lower right')
        plt.show()
    sky += DeltaSKY
    antieerFunc=interp1d(fluxList,eSMA)
    for loop in range(lenofell) :
        if eSMA[loop] > (1.4*antieerFunc(0.95*fluxList[lenofell-1])) :
            std2=loop
            break
    a2=eSMA[std2]
    b2=a2*( 1 - (ellip[std2]) )
    posi2 = [ex_0[std2], ey_0[std2]]
    aperturestd = EllipticalAperture(positions=posi2,a=a2,b=b2,theta=(ePA[std2]+90))
    stdflux=fluxList[std2]
    plt.figure(figsize=(10, 10))
    norm = ImageNormalize(stretch=LogStretch())
    plt.imshow(data, cmap='Greys', origin='lower', norm=norm)
    ax = plt.gca()
    ellipseAp = patches.Ellipse(xy=posi2,width=2*a2,height=2*b2,angle=(ePA[std2]+90),color='r',fill=False)
    ax.add_patch(ellipseAp)
    plt.show()
    result = {
        'aperture': aperturestd ,
        'flux' : stdflux ,
        'sky': sky,
        'fluxList': fluxList,
        'stdn':std2
    }
    return result
Пример #2
0
def get_aperture(data,isophote,gain,sky_std=None,sky_median=None,mask=None,sigma=3,CGSmodel=False,imagepath=None,QuietMode=True,hold=None,holdsky=None,withmask=False,instrument='2MASS',usemedian=False):
    #imagepath=(name(targname_band),savepath)
    #hold=(posi,sma,PA,ellipticity)
    eta = 1
    N_depth = 1
    kappa = 1
    sky_mean, sky_median1, sky_std1 = sigma_clipped_stats(data, sigma=3.0, iters=5)
    if(sky_std == None):
        sky_std = sky_std1
    if(sky_median == None):
        sky_median = sky_median1
    if hold is None:
        inten=isophote['col2']
        for loop in range(len(isophote)):
            if inten[loop] < (sigma*sky_std+sky_median) :
                std2=loop
                break
        if CGSmodel :
            loop2=std2-1
            while loop2 < len(isophote):
                if (inten[loop2] - inten[loop2+1]) < -0.005:
                    std2=loop2
                    break
                loop2 +=1
        ellip=isophote['col6']
        ex_0=isophote['col10']
        ey_0=isophote['col12']
        ePA=isophote['col8']
        eSMA=isophote['col1']
        posi=[ex_0[std2],ey_0[std2]]
        a=eSMA[std2]
        theta=ePA[std2]+90.
        ellipticity=ellip[std2]
    if hold is not None :
        posi=hold['posi']
        theta=hold['PA']+90.
        ellipticity=hold['ellipticity']
        a=hold['sma']
        std2=0
    b=a*(1-ellipticity)
    if mask is None :
        mask = np.zeros_like(data, dtype=bool)
    annulus = np.zeros_like(data, dtype=bool)
    thetarad=(theta)*np.pi/180.
    radiint=int(1.801*a)
    intx=int(posi[0])
    inty=int(posi[1])
    radiint=int(2*a)
    nny, nnx = data.shape
    boundary=[0,nnx-1,0,nny-1]
    minx=np.max([intx-radiint,boundary[0]])
    miny=np.max([inty-radiint,boundary[2]])
    maxx=np.min([intx+radiint,boundary[1]])
    maxy=np.min([inty+radiint,boundary[3]])
    a1=a*1.250
    b1=b*1.250
    a2=a*1.601
    b2=b*1.601
    A=(a**2)*(np.sin(thetarad))**2+(b**2)*(np.cos(thetarad))**2
    B=2*(b**2-a**2)*np.sin(thetarad)*np.cos(thetarad)
    C=(a**2)*(np.cos(thetarad))**2+(b**2)*(np.sin(thetarad))**2
    A1=(a1**2)*(np.sin(thetarad))**2+(b1**2)*(np.cos(thetarad))**2
    B1=2*(b1**2-a1**2)*np.sin(thetarad)*np.cos(thetarad)
    C1=(a1**2)*(np.cos(thetarad))**2+(b1**2)*(np.sin(thetarad))**2
    A2=(a2**2)*(np.sin(thetarad))**2+(b2**2)*(np.cos(thetarad))**2
    B2=2*(b2**2-a2**2)*np.sin(thetarad)*np.cos(thetarad)
    C2=(a2**2)*(np.cos(thetarad))**2+(b2**2)*(np.sin(thetarad))**2
    loopx=minx
    masknan=np.zeros_like(data, dtype=bool)
    masknan[np.isnan(data)] = True
    masknan[np.isinf(data)] = True
    while loopx < (maxx+1) :
        loopy=miny
        while loopy < (maxy+1) :
            dx1=loopx+0.5-posi[0]
            dy1=loopy+0.5-posi[1]
            distan1=A1*dx1**2+B1*dx1*dy1+C1*dy1**2-(a1**2)*(b1**2)
            distan2=A2*dx1**2+B2*dx1*dy1+C2*dy1**2-(a2**2)*(b2**2)
            inner=distan1*distan2
            if (inner < 0.)&(not mask[loopy][loopx]) :
                annulus[loopy][loopx] = True
            loopy += 1
        loopx += 1
    new_image = data[annulus]
    temp1 = mask.astype(int)
    temp2 = temp1[annulus]
    new_mask = temp2.astype(bool)
    annulus_int = annulus.astype(int)
    sky_area = np.sum(annulus_int) - np.sum(temp2)
    sky_final=new_image[~new_mask]
    sky_meanF=np.nanmean(sky_final)
    sky_medianF=np.nanmedian(sky_final)
    sky_stdF=np.nanstd(sky_final)
    sky =  sky_meanF
    if holdsky is not None :
        sky_meanF=holdsky
    if usemedian:
        sky_meanF=sky_medianF
    datap=data-sky_meanF
    fluxList=[]
    aperturestd = EllipticalAperture(positions=posi,a=a,b=b,theta=thetarad)
    if  withmask :
        rawflux_table0 = aperture_photometry(datap, aperturestd, mask=mask)
    else :
        rawflux_table0 = aperture_photometry(datap, aperturestd, mask=masknan)
    area=aperturestd.area()
    stdflux=rawflux_table0['aperture_sum'][0]
    flux_err = (stdflux / (eta * gain * N_depth) + kappa * (area * sky_stdF)**2
             / sky_area + 1.7**2 * 4 * area * sky_stdF**2)**0.5
    if hold is None :
        for loop in range(len(isophote)):
            a111=eSMA[loop]
            b222=a111*( 1 - (ellip[loop]) )
            posi111 = [ex_0[loop], ey_0[loop]]
            aperture111 = EllipticalAperture(positions=posi111,a=a111,b=b222,theta=((ePA[loop]+90)*np.pi/180.))
            rawflux_table111 = aperture_photometry(datap, aperture111, mask=mask)
            fluxList.append(rawflux_table111['aperture_sum'][0])
        antieerFunc=interp1d(fluxList,eSMA)
        if (np.min(fluxList) < 0.3*stdflux)&(np.max(fluxList) > 0.9*stdflux) :
            HLR=antieerFunc(0.5*stdflux)
            concentration=5*np.log(antieerFunc(0.7*stdflux)/antieerFunc(0.3*stdflux))
        else:
            HLR=np.nan
            concentration=np.nan
    else:
        HLR=np.nan
        concentration=np.nan
    if (not QuietMode)&(hold is None) :
        lenofe=len(eSMA)
        amax=eSMA[lenofe-1]
        bmax=amax*(1-ellip[lenofe-1])
        fig = plt.figure(figsize=(8, 6))
        plt.plot(eSMA, fluxList, label='Curve of Growth')
        plt.axvline(x=a, linestyle='--', color='b',   label='standard aperture')
        plt.axvline(x=1.602*a, linestyle='--', color='r',   label='outer ellipse')
        plt.axvline(x=amax, linestyle='--', color='k',   label='biggest ellipse')
        plt.xlabel('Semi-major axis length [pixel]', fontsize=16)
        plt.title('{0} Curve of growth'.format(imagepath['name']), fontsize=16)
        plt.ylabel('Total Flux [DN]', fontsize=16)
        plt.legend(loc='lower right')
        if imagepath is None :
            plt.show()
        else:
            plt.savefig(imagepath['savepath'] + "{0}cog.png".format(imagepath['name']), dpi=200)
        plt.close()
        if instrument is '2MASS':
            plt.figure(figsize=(10, 20))
        else:
            plt.figure(figsize=(20, 20))
        norm = ImageNormalize(stretch=LogStretch())
        plt.imshow(data, cmap='Greys', origin='lower', norm=norm)
        ax = plt.gca()
        ellipseAp2 = patches.Ellipse(xy=posi,width=2.5*a,height=2.5*b,angle=theta,color='c',fill=False)
        ellipseAp3 = patches.Ellipse(xy=posi,width=2*1.601*a,height=2*1.601*b,angle=theta,color='r',fill=False)
        ellipseAp1 = patches.Ellipse(xy=posi,width=2*a,height=2*b,angle=theta,color='b',fill=False)
        ellipseAp4 = patches.Ellipse(xy=posi,width=2*amax,height=2*bmax,angle=ePA[lenofe-1]+90.,color='k',fill=False)
        ax.add_patch(ellipseAp4)
        ax.add_patch(ellipseAp1)
        ax.add_patch(ellipseAp2)
        ax.add_patch(ellipseAp3)
        if imagepath is None :
            plt.show()
        else:
            plt.savefig(imagepath['savepath'] + "{0}sky.png".format(imagepath['name']), dpi=200)
        plt.close()
    result = {
        'sma': a ,
        'b':b,
        'PA': theta ,
        'paY' :theta-90.,
        'ellipticity': ellipticity ,
        'posi':posi,
        'flux' : stdflux ,
        'flux_err' :flux_err,
        'fluxList':fluxList,
        'sky': sky_meanF,
        'sky_std':sky_stdF,
        'stdn':std2,
        'concentration':concentration,
        'HLR':HLR,
    }
    return result