Пример #1
0
    def CreateMask(self):
        self.ComputeNoiseMap()
        self.MakeMask()

        if self.ds9Mask!="":
            self.MaskSelectedDS9()

        ExternalAndMask=self.options.ExternalMask
        if ExternalAndMask is not None and ExternalAndMask is not "":
            from DDFacet.Imager import ClassCasaImage
            CleanMaskImageName=ExternalAndMask
            print>>log,"Use mask image %s"%CleanMaskImageName
            CleanMaskImage = np.bool8(ClassCasaImage.FileToArray(CleanMaskImageName,False))[0,0]
            self.ImMask=(self.ImMask & CleanMaskImage)

        if self.UseIslands:
            # Make island list
            self.BuildIslandList()
            self.FilterIslands()
            self.IslandsToMask()

        # self.plot()
        nx,ny=self.ImMask.shape
        ImWrite=self.ImMask.reshape((1,1,nx,ny))
        
        PutDataInNewImage(self.FitsFile,self.FitsFile+"."+self.OutName,np.float32(ImWrite))
Пример #2
0
    def Restore(self):
        print("Create restored image", file=log)





        
        FWHMFact=2.*np.sqrt(2.*np.log(2.))
        FWHMFact=2.*np.sqrt(2.*np.log(2.))

        BeamPix=self.BeamPix/FWHMFact
        sigma_x, sigma_y=BeamPix,BeamPix
        theta=0.
        bmaj=np.max([sigma_x, sigma_y])*self.CellArcSec*FWHMFact
        bmin=np.min([sigma_x, sigma_y])*self.CellArcSec*FWHMFact

        #bmaj=bmin=0.001666666666666667*3600
        #sigma_x=
        self.FWHMBeam=(bmaj/3600./np.sqrt(2.),bmin/3600./np.sqrt(2.),theta)
        self.PSFGaussPars = (sigma_x*self.CellSizeRad, sigma_y*self.CellSizeRad, theta)

        #print "!!!!!!!!!!!!!!!!!!!!"
        #self.PSFGaussPars = (BeamPix,BeamPix,0)



        RefFreq=self.ModelMachine.RefFreq
        df=RefFreq*0.5

        # ################################"


        if self.options.PSFCache!="":

            import os
            IdSharedMem=str(int(os.getpid()))+"."
            MeanModelImage=self.ModelMachine.GiveModelImage(RefFreq)

            # #imNorm=image("6SBc.KAFCA.restoredNew.fits.6SBc.KAFCA.restoredNew.fits.MaskLarge.fits").getdata()
            # imNorm=image("6SB.KAFCA.GA.BIC_00.AP.dirty.fits.mask.fits").getdata()
            # MASK=np.zeros_like(imNorm)
            # nchan,npol,_,_=MASK.shape
            # for ch in range(nchan):
            #     for pol in range(npol):
            #         MASK[ch,pol,:,:]=imNorm[ch,pol,:,:].T[::-1,:]
            # MeanModelImage[MASK==0]=0

            # MeanModelImage.fill(0)
            # MeanModelImage[0,0,100,100]=1


            from DDFacet.Imager.GA import ClassSmearSM
            from DDFacet.Imager import ClassPSFServer
            self.DicoVariablePSF = MyPickle.FileToDicoNP(self.options.PSFCache)
            
            self.PSFServer=ClassPSFServer.ClassPSFServer()
            
            self.PSFServer.setDicoVariablePSF(self.DicoVariablePSF,NormalisePSF=True)
            #return self.Residual,MeanModelImage,self.PSFServer


            # CasaImage=ClassCasaImage.ClassCasaimage("Model.fits",MeanModelImage.shape,self.Cell,self.radec)#Lambda=(Lambda0,dLambda,self.NBands))
            # CasaImage.setdata(MeanModelImage,CorrT=True)
            # CasaImage.ToFits()
            # #CasaImage.setBeam((SmoothFWHM,SmoothFWHM,0))
            # CasaImage.close()


            SmearMachine=ClassSmearSM.ClassSmearSM(self.Residual,
                                                   MeanModelImage*self.SqrtNormImage,
                                                   self.PSFServer,
                                                   DeltaChi2=4.,
                                                   IdSharedMem=IdSharedMem,
                                                   NCPU=self.options.NCPU)
            SmearedModel=SmearMachine.Smear()
            SmoothFWHM=self.CellArcSec*SmearMachine.RestoreFWHM/3600.
            ModelSmearImage="%s.RestoredSmear"%self.BaseImageName
            CasaImage=ClassCasaImage.ClassCasaimage(ModelSmearImage,SmearedModel.shape,self.Cell,self.radec)#Lambda=(Lambda0,dLambda,self.NBands))
            CasaImage.setdata(SmearedModel+self.Residual,CorrT=True)
            #CasaImage.setdata(SmearedModel,CorrT=True)
            CasaImage.setBeam((SmoothFWHM,SmoothFWHM,0))
            CasaImage.ToFits()
            CasaImage.close()
            SmearMachine.CleanUpSHM()
            stop

        # ################################"
        #self.ModelMachine.ListScales[0]["Alpha"]=-0.8

        # model image
        #ModelMachine.GiveModelImage(RefFreq)

        FEdge=np.linspace(RefFreq-df,RefFreq+df,self.NBands+1)
        FCenter=(FEdge[0:-1]+FEdge[1::])/2.
        C=299792458.
        Lambda0=C/FCenter[-1]
        dLambda=1
        if self.NBands>1:
            dLambda=np.abs(C/FCenter[0]-C/FCenter[1])

        ListRestoredIm=[]
        Lambda=[Lambda0+i*dLambda for i in range(self.NBands)]
        ListRestoredImCorr=[]
        ListModelIm=[]
        #print C/np.array(Lambda)
        # restored image

        for l in Lambda:
            freq=C/l
            
            if self.options.RandomCat:
                print("Create random catalog... ", file=log)
                ModelImage=self.GiveRandomModelIm()
            else:
                print("Get ModelImage... ", file=log)
                ModelImage=self.ModelMachine.GiveModelImage(freq)
            
            if self.options.ZeroNegComp:
                print("Zeroing negative componants... ", file=log)
                ModelImage[ModelImage<0]=0
            ListModelIm.append(ModelImage)


            if self.options.Mode=="App":
                print("  ModelImage to apparent flux... ", file=log)
                ModelImage=ModelImage*self.SqrtNormImage
            print("Convolve... ", file=log)
            print("   MinMax = [%f , %f] @ freq = %f MHz"%(ModelImage.min(),ModelImage.max(),freq/1e6), file=log)
            #RestoredImage=ModFFTW.ConvolveGaussianScipy(ModelImage,CellSizeRad=self.CellSizeRad,GaussPars=[self.PSFGaussPars])

            
            if self.options.AddNoise>0.:
                print("Adding Noise... ", file=log)
                ModelImage+=np.random.randn(*ModelImage.shape)*self.options.AddNoise

            RestoredImage,_=ModFFTW.ConvolveGaussianWrapper(ModelImage,Sig=BeamPix)
# =======
#             #RestoredImage,_=ModFFTW.ConvolveGaussianWrapper(ModelImage,Sig=BeamPix)

#             def GiveGauss(Sig0,Sig1):
#                 npix=20*int(np.sqrt(Sig0**2+Sig1**2))
#                 if not npix%2: npix+=1
#                 dx=npix/2
#                 x,y=np.mgrid[-dx:dx:npix*1j,-dx:dx:npix*1j]
#                 dsq=x**2+y**2
#                 return Sig0**2/(Sig0**2+Sig1**2)*np.exp(-dsq/(2.*(Sig0**2+Sig1**2)))
#             R2=np.zeros_like(ModelImage)

#             Sig0=BeamPix/np.sqrt(2.)
#             if self.options.RandomCat:
#                 Sig1=(self.options.RandomCat_SigFactor-1.)*Sig0
#             else:
#                 Sig1=0.
#             nch,npol,_,_=ModelImage.shape
#             for ch in range(nch):
#                 in1=ModelImage[ch,0]
#                 R2[ch,0,:,:]=scipy.signal.fftconvolve(in1,GiveGauss(Sig0,Sig1), mode='same').real
#             RestoredImage=R2

#             self.header_dict["GSIGMA"]=Sig0


#             # print np.max(np.abs(R2-RestoredImage))
#             # import pylab
#             # ax=pylab.subplot(1,3,1)
#             # pylab.imshow(RestoredImage[0,0],interpolation="nearest")
#             # pylab.colorbar()
#             # pylab.subplot(1,3,2,sharex=ax,sharey=ax)
#             # pylab.imshow(R2[0,0],interpolation="nearest")
#             # pylab.colorbar()
#             # pylab.subplot(1,3,3,sharex=ax,sharey=ax)
#             # pylab.imshow((RestoredImage-R2)[0,0],interpolation="nearest")
#             # pylab.colorbar()
#             # pylab.show()
#             # stop

# >>>>>>> 0457182a873da89a2758f4be8a18f55cefd88e44

            RestoredImageRes=RestoredImage+self.Residual
            ListRestoredIm.append(RestoredImageRes)
            RestoredImageResCorr=RestoredImageRes/self.SqrtNormImage
            ListRestoredImCorr.append(RestoredImageResCorr)

        #print FEdge,FCenter

        print("Save... ", file=log)
        _,_,nx,_=RestoredImageRes.shape
        RestoredImageRes=np.array(ListRestoredIm).reshape((self.NBands,1,nx,nx))
        RestoredImageResCorr=np.array(ListRestoredImCorr).reshape((self.NBands,1,nx,nx))

        ModelImage=np.array(ListModelIm).reshape((self.NBands,1,nx,nx))
        


        if self.OutName=="":
            ImageName="%s.restoredNew"%self.BaseImageName
            ImageNameCorr="%s.restoredNew.corr"%self.BaseImageName
            ImageNameModel="%s.model"%self.BaseImageName
            ImageNameModelConv="%s.modelConv"%self.BaseImageName
        else:
            ImageName=self.OutName
            ImageNameCorr=self.OutName+".corr"
            ImageNameModel="%s.model"%self.OutName
            ImageNameModelConv="%s.modelConv"%self.OutName

        CasaImage=ClassCasaImage.ClassCasaimage(ImageNameModel,RestoredImageRes.shape,self.Cell,self.radec,header_dict=self.header_dict)#Lambda=(Lambda0,dLambda,self.NBands))
        CasaImage.setdata(ModelImage,CorrT=True)
        CasaImage.setBeam(self.FWHMBeam)
        CasaImage.ToFits()
        CasaImage.close()

        CasaImage=ClassCasaImage.ClassCasaimage(ImageName,RestoredImageRes.shape,self.Cell,self.radec,Freqs=C/np.array(Lambda).ravel(),header_dict=self.header_dict)#,Lambda=(Lambda0,dLambda,self.NBands))
        CasaImage.setdata(RestoredImageRes,CorrT=True)
        CasaImage.setBeam(self.FWHMBeam)
        CasaImage.ToFits()
        CasaImage.close()
        
        CasaImage=ClassCasaImage.ClassCasaimage(ImageNameModelConv,RestoredImage.shape,self.Cell,self.radec,Freqs=C/np.array(Lambda).ravel(),header_dict=self.header_dict)#,Lambda=(Lambda0,dLambda,self.NBands))
        CasaImage.setdata(RestoredImage,CorrT=True)
        CasaImage.setBeam(self.FWHMBeam)
        CasaImage.ToFits()
        CasaImage.close()

        if self.MakeCorrected:
            CasaImage=ClassCasaImage.ClassCasaimage(ImageNameCorr,RestoredImageResCorr.shape,self.Cell,self.radec,Freqs=C/np.array(Lambda).ravel(),header_dict=self.header_dict)#,Lambda=(Lambda0,dLambda,self.NBands))
            CasaImage.setdata(RestoredImageResCorr,CorrT=True)
            CasaImage.setBeam(self.FWHMBeam)
            CasaImage.ToFits()
            CasaImage.close()
        

        # ImageName="%s.modelConv"%self.BaseImageName
        # CasaImage=ClassCasaImage.ClassCasaimage(ImageName,ModelImage.shape,self.Cell,self.radec)
        # CasaImage.setdata(self.RestoredImage,CorrT=True)
        # CasaImage.ToFits()
        # CasaImage.setBeam(self.FWHMBeam)
        # CasaImage.close()


        # Alpha image
        if self.DoAlpha:
            print("Get Index Map... ", file=log)
            IndexMap=self.ModelMachine.GiveSpectralIndexMap(CellSizeRad=self.CellSizeRad,GaussPars=[self.PSFGaussPars])
            ImageName="%s.alphaNew"%self.BaseImageName
            print("  Save... ", file=log)
            CasaImage=ClassCasaImage.ClassCasaimage(ImageName,ModelImage.shape,self.Cell,self.radec)
            CasaImage.setdata(IndexMap,CorrT=True)
            CasaImage.ToFits()
            CasaImage.close()
            print("  Done. ", file=log)