コード例 #1
0
    def MaskSelectedDS9(self):
        ds9Mask=self.ds9Mask
        print>>log,"Reading ds9 region file: %s"%ds9Mask
        R=ModRegFile.RegToNp(ds9Mask)
        R.Read()
        
        IncludeCat=R.CatSel
        
        ExcludeCat=R.CatExclude
        
        print>>log,"  Excluding pixels"
        for iRegExclude in range(R.CatExclude.shape[0]):
            
            rac,decc,Radius=R.CatExclude.ra[iRegExclude],R.CatExclude.dec[iRegExclude],R.CatExclude.Radius[iRegExclude]
            RadiusPix=(1.1*Radius/self.incr_rad)
            freq,pol,_,_=self.CasaIm.toworld((0,0,0,0))

            _,_,yc,xc=self.CasaIm.topixel((freq,pol,decc,rac))

            xGrid,yGrid=np.mgrid[int(xc-RadiusPix):int(xc+RadiusPix)+1,int(yc-RadiusPix):int(yc+RadiusPix)+1]
            xGrid=xGrid.ravel()
            yGrid=yGrid.ravel()

            for iPix in range(xGrid.size):
                # if iPix%10000==0:
                #     print iPix,"/",xGrid.size
                ipix,jpix=xGrid[iPix],yGrid[iPix]
                _,_,dec,ra=self.CasaIm.toworld((0,0,jpix,ipix))
                #d=np.sqrt((ra-rac)**2+(dec-decc)**2)
                d=self.GiveAngDist(ra,dec,rac,decc)
                if d<Radius:
                    self.ImMask[jpix,ipix]=0
        

        #self.ImMask.fill(0)
        print>>log,"  Including pixels"
        for iRegInclude in range(IncludeCat.shape[0]):
            rac,decc,Radius=IncludeCat.ra[iRegInclude],IncludeCat.dec[iRegInclude],IncludeCat.Radius[iRegInclude]
            RadiusPix=(1.1*Radius/self.incr_rad)
            freq,pol,_,_=self.CasaIm.toworld((0,0,0,0))

            _,_,yc,xc=self.CasaIm.topixel((freq,pol,decc,rac))
            
            xGrid,yGrid=np.mgrid[int(xc-RadiusPix):int(xc+RadiusPix)+1,int(yc-RadiusPix):int(yc+RadiusPix)+1]
            xGrid=xGrid.flatten().tolist()
            yGrid=yGrid.flatten().tolist()

            for ipix,jpix in zip(xGrid,yGrid):
                _,_,dec,ra=self.CasaIm.toworld((0,0,jpix,ipix))
                #d=np.sqrt((ra-rac)**2+(dec-decc)**2)
                d=self.GiveAngDist(ra,dec,rac,decc)
                #print ipix,jpix
                if d<Radius: 
                    #print "ones",ipix,jpix
                    self.ImMask[jpix,ipix]=1
コード例 #2
0
    def ComputeNoiseMap(self):
        print>>log, "Compute noise map..."
        Boost=self.Boost
        Acopy=self.Restored[0,0,0::Boost,0::Boost].copy()
        SBox=(self.box[0]/Boost,self.box[1]/Boost)

        # MeanAbs=scipy.ndimage.filters.mean_filter(np.abs(Acopy),SBox)
        # Acopy[Acopy>0]=MeanAbs[Acopy>0]
        # Noise=np.sqrt(scipy.ndimage.filters.median_filter(np.abs(Acopy)**2,SBox))

        x=np.linspace(-10,10,1000)
        f=0.5*(1.+scipy.special.erf(x/np.sqrt(2.)))
        n=SBox[0]*SBox[1]
        F=1.-(1.-f)**n
        ratio=np.abs(np.interp(0.5,F,x))

        Noise=-scipy.ndimage.filters.minimum_filter(Acopy,SBox)/ratio




        Noise[Noise<0]=1e-10

        # indxy=(Acopy>5.*Noise)
        # Acopy[indxy]=5*Noise[indxy]
        # Noise=np.sqrt(scipy.ndimage.filters.median_filter(np.abs(Acopy)**2,SBox))

        # indxy=(Acopy>5.*Noise)
        # Acopy[indxy]=5*Noise[indxy]
        # Noise=np.sqrt(scipy.ndimage.filters.median_filter(np.abs(Acopy)**2,SBox))

        #NoiseMed=np.median(Noise)
        #Noise[Noise<NoiseMed]=NoiseMed

        self.Noise=np.zeros_like(self.Restored[0,0])
        for i in range(Boost):
            for j in range(Boost):
                s00,s01=Noise.shape
                s10,s11=self.Noise[i::Boost,j::Boost].shape
                s0,s1=min(s00,s10),min(s10,s11)
                self.Noise[i::Boost,j::Boost][0:s0,0:s1]=Noise[:,:][0:s0,0:s1]
        ind=np.where(self.Noise==0.)
        self.Noise[ind]=1e-10

        if self.options.OutMaskExtended:
            GD=None
            MaskExtended=(self.Noise<0.1*np.median(self.Noise))
            OutMaskExtended=self.options.OutMaskExtended
            nx=MaskExtended.shape[-1]
            CurrentNegMask=np.logical_not(MaskExtended).reshape((1,1,nx,nx))
            PSFServer=None
            IdSharedMem=None
            DicoDirty=None

            IslandDistanceMachine=DDFacet.Imager.SSD.ClassIslandDistanceMachine.ClassIslandDistanceMachine(GD,
                                                                                                           CurrentNegMask,
                                                                                                           PSFServer,
                                                                                                           DicoDirty,
                                                                                                           IdSharedMem=IdSharedMem)
            ListIslands=IslandDistanceMachine.SearchIslands(None,Image=self.Restored)
            ListIslands=IslandDistanceMachine.ConvexifyIsland(ListIslands)#,PolygonFile="%s.pickle"%OutMaskExtended)
            ListPolygons=IslandDistanceMachine.ListPolygons

            MaskOut=np.zeros_like(CurrentNegMask)
            N=0
            for Island in ListIslands:
                x,y=np.array(Island).T
                #if x.size<=10: continue
                MaskOut[0,0,x,y]=1
            
            #ff,pol,_,_dec,ra=self.CasaIm.toworld((0,0,0,0))
            ListPolygonsRADEC=[]
            for Polygon in ListPolygons:
                xx,yy=Polygon.T
                ThisPolygon=[]
                for iP in range(xx.shape[0]):
                    xcc,ycc =xx[iP],yy[iP]
                    ff,pol,dec,ra=self.CasaIm.toworld((0,0,xcc,ycc))
                    ThisPolygon.append((ra,dec))
                ListPolygonsRADEC.append(np.array(ThisPolygon))

            FName="%s.pickle"%OutMaskExtended
            print>>log,"Saving %s"%FName
            MyPickle.Save(ListPolygonsRADEC,FName)

            REGName="%s.reg"%OutMaskExtended
            RM=ModRegFile.PolygonNpToReg(ListPolygonsRADEC,REGName)
            RM.makeRegPolyREG()
            
            # TestArray=np.zeros_like(CurrentNegMask)
            # nx=TestArray.shape[-1]
            # xx,yy=20,100
            # TestArray[0,0,xx,yy]=1
            # PutDataInNewImage(self.FitsFile,"TestCoord.fits",np.float32(TestArray))
            # ff,pol,dec,ra=self.CasaIm.toworld((0,0,xx,yy))
            # pp=[[xx,yy],[yy,xx],
            #     [nx/2-xx,yy],[nx/2+xx,yy],
            #     [nx/2-yy,xx],[nx/2+yy,xx]]
            # for isol in range(len(pp)):
            #     xx,yy=pp[isol]
            #     ff,pol,dec,ra=self.CasaIm.toworld((0,0,xx,yy))
            #     sRA =rad2hmsdms(ra,Type="ra").replace(" ",":")
            #     sDEC =rad2hmsdms(dec,Type="dec").replace(" ",":")
            #     stop
            
            
            MaskOut=np.zeros_like(CurrentNegMask)
            N=0
            for Island in ListIslands:
                x,y=np.array(Island).T
                if x.size<=10: continue
                MaskOut[0,0,x,y]=1
                N+=1
            print>>log,"Number of large enough islands %i"%N
            MaskExtended=MaskOut
            os.system("rm -rf %s"%OutMaskExtended)
            os.system("rm -rf %s.fits"%OutMaskExtended)
            PutDataInNewImage(self.FitsFile,OutMaskExtended,np.float32(MaskExtended))

        NoiseMed=np.median(self.Noise)
        self.Noise[self.Noise<NoiseMed]=NoiseMed
            
        nx=self.Noise.shape[-1]

        if self.options.ConvNoise:
            print>>log,"Convolve..."
            NoiseMap,G=ModFFTW.ConvolveGaussianWrapper(self.Noise.reshape((1,1,nx,nx)),Sig=4*SBox[0])
            NoiseMap/=np.sum(G)
            self.Noise=NoiseMap[0,0]
        
        if self.OutNameNoiseMap!="":
            #print>>log, "Save noise map as %s"%self.OutNameNoiseMap
            #self.CasaIm.saveas(self.OutNameNoiseMap)
            #CasaNoise=image(self.OutNameNoiseMap)
            #CasaNoise.putdata(self.Noise)
            #CasaNoise.tofits(self.OutNameNoiseMap+".fits")
            #del(CasaNoise)
            os.system("rm -rf %s"%self.OutNameNoiseMap)
            #os.system("rm -rf %s"%self.OutNameNoiseMap+".fits")
            PutDataInNewImage(self.FitsFile,self.OutNameNoiseMap,np.float32(self.Noise))

            os.system("rm -rf %s.mean"%self.OutNameNoiseMap)
            PutDataInNewImage(self.FitsFile,self.OutNameNoiseMap+".mean",np.float32(np.zeros_like(self.Noise)))