Exemplo n.º 1
0
    def __init__(self, **kwargs):
        for key, value in list(kwargs.items()):
            setattr(self, key, value)
        print("Reading %s" % self.SourceCat, file=log)
        f = fits.open(self.SourceCat)
        # fix up comments
        keywords = [('CRVAL1', float), ('CRVAL2', float), ('CDELT1', float),
                    ('NAXIS1', int)]
        c = f[1].header['COMMENT']
        for l in c:
            for k, ty in keywords:
                if k in l:
                    bits = l.split()
                    print("Warning: getting keyword %s from comments" % k,
                          file=log)
                    f[1].header['I_' + k] = ty(bits[2])

        decc, rac = f[1].header["I_CRVAL1"], f[1].header["I_CRVAL2"]
        rac, decc = f[1].header["I_CRVAL1"], f[1].header["I_CRVAL2"]
        self.dPix = abs(f[1].header["I_CDELT1"])
        self.NPix = abs(f[1].header["I_NAXIS1"])
        rac *= np.pi / 180
        decc *= np.pi / 180
        sRA = rad2hmsdms(rac, Type="ra").replace(" ", ":")
        sDEC = rad2hmsdms(decc, Type="dec").replace(" ", ":")
        print("Image center: %s %s" % (sRA, sDEC), file=log)
        self.rarad = rac
        self.decrad = decc
        self.CoordMachine = ModCoord.ClassCoordConv(self.rarad, self.decrad)

        lmax = self.NPix // 2 * self.dPix * np.pi / 180
        self.PolyCut = np.array([[-lmax, -lmax], [-lmax, lmax], [lmax, lmax],
                                 [lmax, -lmax]])

        self.setCatName(self.SourceCat)
Exemplo n.º 2
0
    def __init__(self,infile,infile_cluster="",killdirs=[],invert=False,DoPrintCat=False,\
                     ReName=False,DoREG=False,SaveNp=False,NCluster=0,DoPlot=True,Tigger=False,\
                     FromExt=None,ClusterMethod=1,SelSource=False):
        self.ClusterMethod = ClusterMethod
        self.infile_cluster = infile_cluster
        self.TargetList = infile
        self.Type = "Catalog"

        self.DoPrint = True
        if type(infile).__name__ == "instance":
            Cat = infile.SourceCat
            Cat = Cat.view(np.recarray)
            self.DoPrint = 0
        elif ".npy" in infile:
            Cat = np.load(infile)
            Cat = Cat.view(np.recarray)
        elif Tigger:
            Cat = ModTigger.ReadTiggerModel(infile)
        elif FromExt != None:

            Cat = ModSMFromNp.ReadFromNp(FromExt)
        else:
            Cat = ReadBBSModel(infile, infile_cluster=infile_cluster)
        self.SourceCat = Cat
        self.killdirs = killdirs
        self.invert = invert
        self.infile = infile
        self.REGFile = None
        self.Dirs = sorted(list(set(self.SourceCat.Cluster.tolist())))
        self.NDir = np.max(self.SourceCat.Cluster) + 1
        self.NSources = Cat.shape[0]

        try:
            SourceCat = self.SourceCat
            indIMax = np.argmax(SourceCat.I)
            self.rarad = SourceCat.ra[
                indIMax]  #np.sum(SourceCat.I*SourceCat.ra)/np.sum(SourceCat.I)
            self.decrad = np.sum(SourceCat.I * SourceCat.dec) / np.sum(
                SourceCat.I)
            self.CoordMachine = ModCoord.ClassCoordConv(
                self.rarad, self.decrad)
        except:
            pass

        self.BuildClusterCat()
        self.NDir = np.max(self.SourceCat.Cluster) + 1
        self.NSources = Cat.shape[0]
        self.SetSelection()
        if self.DoPrint: self.PrintBasics()
Exemplo n.º 3
0
    def __init__(self,**kwargs):
        for key, value in kwargs.items(): setattr(self, key, value)
        print>>log,"Reading %s"%self.SourceCat
        f=pyfits.open(self.SourceCat)
        decc,rac=f[1].header["I_CRVAL1"],f[1].header["I_CRVAL2"]
        rac,decc=f[1].header["I_CRVAL1"],f[1].header["I_CRVAL2"]
        self.dPix=abs(f[1].header["I_CDELT1"])
        self.NPix=abs(f[1].header["I_NAXIS1"])
        rac*=np.pi/180
        decc*=np.pi/180
        sRA =rad2hmsdms(rac,Type="ra").replace(" ",":")
        sDEC=rad2hmsdms(decc,Type="dec").replace(" ",":")
        print>>log,"Image center: %s %s"%(sRA,sDEC)
        self.rarad=rac
        self.decrad=decc
        self.CoordMachine = ModCoord.ClassCoordConv(self.rarad, self.decrad)

        lmax=self.NPix/2*self.dPix*np.pi/180
        self.PolyCut=np.array([[-lmax,-lmax],[-lmax,lmax],[lmax,lmax],[lmax,-lmax]])

        self.setCatName(self.SourceCat)
Exemplo n.º 4
0
    def __init__(self, VS):
        logger.setSilent(["ClassJones", "ClassLOFARBeam"])
        self.VS = VS

        self.GD = copy.deepcopy(self.VS.GD)
        self.DoCentralNorm = self.GD["Beam"]["CenterNorm"]
        self.SmoothBeam = None
        self.CheckCache()
        if self.CacheValid:
            logger.setLoud(["ClassJones", "ClassLOFARBeam"])
            return

        #self.GD["Beam"]["CenterNorm"]=0

        self.ListMS = self.VS.ListMS
        self.MS = self.ListMS[0]
        rac, decc = self.MS.radec
        self.CoordMachine = ModCoord.ClassCoordConv(rac, decc)
        self.CalcGrid()
        #self.Padding=Padding

        #self.SumJJsq=np.zeros((self.npix,self.npix,self.MS.Nchan),np.float64)
        #self.SumWsq=np.zeros((1,self.MS.Nchan),np.float64)

        self.StackedBeamDict = shared_dict.create("StackedBeamDict")
        for iDir in range(self.NDir):
            sd = self.StackedBeamDict.addSubdict(iDir)
            sd.addSharedArray("SumJJsq", (self.VS.NFreqBands, ), np.float64)
            sd.addSharedArray("SumWsq", (self.VS.NFreqBands, ), np.float64)

        self.DicoJonesMachine = {}
        for iMS, MS in enumerate(self.ListMS):
            JonesMachine = ClassJones.ClassJones(self.GD, MS,
                                                 self.VS.FacetMachine)
            JonesMachine.InitBeamMachine()
            self.DicoJonesMachine[iMS] = JonesMachine
        logger.setLoud(["ClassJones", "ClassLOFARBeam"])
Exemplo n.º 5
0
    def __init__(self,FitsFile=None,
                 Th=5.,
                 Box=(50,10),
                 UseIslands=False,
                 OutName="mask",
                 ds9Mask="",
                 OutNameNoiseMap="",
                 options=None,
                 RevertInput=False):

        self.ds9Mask=ds9Mask
        self.FitsFile=FitsFile
        self.Th=Th
        self.Box,self.IncrPix=Box
        self.Boost=self.IncrPix
        self.box=self.Box,self.Box
        self.CasaIm=image(self.FitsFile)
        self.Restored=self.CasaIm.getdata()
        if options.RevertInput:
            print>>log, "Reverting the image..."
            self.Restored*=-1
        self.UseIslands=UseIslands
        self.OutName=OutName
        self.OutNameNoiseMap=OutNameNoiseMap
        self.options=options

        im=self.CasaIm
        c=im.coordinates()
        incr=np.abs(c.dict()["direction0"]["cdelt"][0])
        self.incr_rad=incr

        ra,dec=c.dict()["direction0"]["crval"]
        self.rarad, self.decrad = ra*np.pi/180, dec*np.pi/180
        self.CoordMachine = ModCoord.ClassCoordConv(self.rarad, self.decrad)

        if self.UseIslands:
            PMaj=(im.imageinfo()["restoringbeam"]["major"]["value"])
            PMin=(im.imageinfo()["restoringbeam"]["minor"]["value"])
            PPA=(im.imageinfo()["restoringbeam"]["positionangle"]["value"])

            
            ToSig=(1./3600.)*(np.pi/180.)/(2.*np.sqrt(2.*np.log(2)))
            SigMaj_rad=PMaj*ToSig
            SigMin_rad=PMin*ToSig
            SixMaj_pix=SigMaj_rad/incr
            SixMin_pix=SigMin_rad/incr
            PPA_rad=PPA*np.pi/180

            x,y=np.mgrid[-10:11:1,-10:11:1]
            self.RefGauss=Gaussian.GaussianXY(x,y,1.,sig=(SixMin_pix,SixMaj_pix),pa=PPA_rad)
            self.RefGauss_xy=x,y
            
            self.BeamMin_pix=SixMin_pix*(2.*np.sqrt(2.*np.log(2)))
            self.BeamMaj_pix=SixMaj_pix*(2.*np.sqrt(2.*np.log(2)))
            self.RBeam_pix=SixMaj_pix
            print>>log, "Restoring Beam size of (%3.3f, %3.3f) pixels"%(self.BeamMin_pix, self.BeamMaj_pix)
        
        
        

        # #################"
        # _,_,nx,ny=self.Restored.shape
        # xc,yc=nx/2,nx/2
        # sup=200
        # x,y=np.mgrid[-sup:sup:1,-sup:sup:1]
        # G=Gaussian.GaussianXY(x,y,1.,sig=(7,18),pa=0.)
        # self.Restored[0,0,xc:xc+2*sup,yc:yc+2*sup]+=G[:,:]

        # xc,yc=nx/2+10,nx/2+10

        # G=Gaussian.GaussianXY(x,y,1.,sig=(3,3),pa=0.)
        # self.Restored[0,0,xc:xc+2*sup,yc:yc+2*sup]+=G[:,:]


        # #################"
        
        
        #self.Restored=np.load("testim.npy")
        self.A=self.Restored[0,0]
Exemplo n.º 6
0
    def cluster(self,nk=10,DoPlot=False,PreClusterCat=None,FromClusterCat=""):

        # pylab.clf()
    
        #s.fill(0.)
        #s[0]=1

        cos=np.cos
        sin=np.sin


        self.SourceCat.Cluster=-1
        indSubSel=np.arange(self.SourceCat.shape[0])
        NPreCluster=0

        # #######################################
        # if (PreClusterCat!=None)&(FromClusterCat==""):
        #     N=PreClusterCat.shape[0]
        #     Ns=self.SourceCat.ra.shape[0]
        #     for iReg in range(N):
        #         #d=np.sqrt((self.SourceCat.ra-PreClusterCat.ra[iReg])**2+(self.SourceCat.dec-PreClusterCat.dec[iReg])**2)
        #         ra1=self.SourceCat.ra
        #         ra2=PreClusterCat.ra[iReg]
        #         d1=self.SourceCat.dec
        #         d2=PreClusterCat.dec[iReg]
        #         cosD = sin(d1)*sin(d2) + cos(d1)*cos(d2)*cos(ra1-ra2)
        #         d=np.arccos(cosD)
        #         self.SourceCat.Cluster[d<PreClusterCat.Radius[iReg]]=PreClusterCat.Cluster[iReg]
        #         self.SourceCat.Exclude[d<PreClusterCat.Radius[iReg]]=False
        #     print self.SourceCat.Cluster
        #     indPreCluster=np.where(self.SourceCat.Cluster!=-1)[0]
        #     NPreCluster=np.max(PreClusterCat.Cluster)+1
        #     SourceCatPreCluster=self.SourceCat[indPreCluster]
        #     indSubSel=np.where(self.SourceCat.Cluster==-1)[0]
        #     print "number of preselected clusters: %i"%NPreCluster
        
        # if nk==-1:
        #     print "Removing non-clustered sources"%NPreCluster
        #     self.SourceCat=self.SourceCat[self.SourceCat.Cluster!=-1]
        #     print self.SourceCat.Cluster
        #     return
        # #######################################

        SourceCat=self.SourceCat[indSubSel]

        indIMax=np.argmax(SourceCat.I)

        self.rarad=SourceCat.ra[indIMax]#np.sum(SourceCat.I*SourceCat.ra)/np.sum(SourceCat.I)
        self.decrad=np.sum(SourceCat.I*SourceCat.dec)/np.sum(SourceCat.I)

        self.CoordMachine = ModCoord.ClassCoordConv(self.rarad, self.decrad)


        x,y,s=SourceCat.ra,SourceCat.dec,SourceCat.I
        x,y=self.radec2lm_scalar(x,y)
        
        SourceCat.Cluster=0

        if self.ClusterMethod==1:
            CM=ClassClusterClean(x,y,s,nk,DoPlot=DoPlot)
        elif self.ClusterMethod==2:
            CM=ClassClusterTessel(x,y,s,nk,DoPlot=DoPlot)
        elif self.ClusterMethod==3:
            CM=ClassClusterRadial(x,y,s,nk,DoPlot=DoPlot)
        elif self.ClusterMethod==4:
            if PreClusterCat!=None:
                l0,m0=self.radec2lm_scalar(PreClusterCat.ra,PreClusterCat.dec)
                CM=ClassClusterKMean(x,y,s,nk,DoPlot=DoPlot,PreCluster=(l0,m0))
            else:
                CM=ClassClusterClean(x,y,s,nk,DoPlot=0)#DoPlot)
                DictNode=CM.Cluster()
                ra,dec=[],[]
                ListL,ListM=[],[]
                for idDir in DictNode.keys():
                    LDir=DictNode[idDir]["ListCluster"]
                    #ra0,dec0=np.mean(self.SourceCat.ra[LDir]),np.mean(self.SourceCat.dec[LDir])
                    # print idDir,ra0,dec0,self.SourceCat.ra[LDir].min(),self.SourceCat.ra[LDir].max()
                    # if not np.isnan(ra0):
                    #     ra.append(ra0)
                    #     dec.append(dec0)
                    This_l,This_m=self.radec2lm_scalar(np.array(self.SourceCat.ra[LDir]),np.array(self.SourceCat.dec[LDir]))
                    ListL.append(np.mean(This_l))
                    ListM.append(np.mean(This_m))

                # l0,m0=self.radec2lm_scalar(np.array(ra),np.array(dec))
                l0,m0=np.array(ListL),np.array(ListM)
                nk=l0.size

                CM=ClassClusterKMean(x,y,s,nk,DoPlot=DoPlot,InitLM=(l0,m0))


        REGFile="%s.tessel.reg"%self.TargetList

        #print FromClusterCat
        if FromClusterCat=="":
            DictNode=CM.Cluster()
        else:

            DictNode={}
            if not("reg" in FromClusterCat):
                SourceCatRef=np.load(FromClusterCat)
                SourceCatRef=SourceCatRef.view(np.recarray)
            else:
                R=ModRegFile.RegToNp(FromClusterCat)
                R.Read()
                SourceCatRef=R.CatSel

            ClusterList=sorted(list(set(SourceCatRef.Cluster.tolist())))
            xc,yc=self.radec2lm_scalar(SourceCatRef.ra,SourceCatRef.dec)
            lc=np.zeros((len(ClusterList),),dtype=np.float32)
            mc=np.zeros((len(ClusterList),),dtype=np.float32)
            for iCluster in ClusterList:
                indC=np.where(SourceCatRef.Cluster==iCluster)[0]
                lc[iCluster]=np.sum(SourceCatRef.I[indC]*xc[indC])/np.sum(SourceCatRef.I[indC])
                mc[iCluster]=np.sum(SourceCatRef.I[indC]*yc[indC])/np.sum(SourceCatRef.I[indC])
            Ns=x.size
            Nc=lc.size
            D=np.sqrt((x.reshape((Ns,1))-lc.reshape((1,Nc)))**2+(y.reshape((Ns,1))-mc.reshape((1,Nc)))**2)
            Cid=np.argmin(D,axis=1)

            #pylab.clf()
            for iCluster in ClusterList:
                ind=np.where(Cid==iCluster)[0]
                DictNode["%3.3i"%iCluster]={}
                DictNode["%3.3i"%iCluster]["ListCluster"]=ind.tolist()
                # pylab.scatter(x[ind],y[ind],c=np.ones((ind.size,))*iCluster,vmin=0,vmax=Nc,lw=0)
                # pylab.draw()
                # pylab.show(False)
            


        try:
            CM.ToReg(REGFile,self.rarad,self.decrad)
        except:
            pass

        iK=NPreCluster
        self.NDir=len(DictNode.keys())
        
        # print self.SourceCat.Cluster.min(),self.SourceCat.Cluster.max()
        
        for key in DictNode.keys():
            ind=np.array(DictNode[key]["ListCluster"])
            if ind.size==0: 
                print "Direction %i is empty"%int(key)
                continue
            self.SourceCat["Cluster"][indSubSel[ind]]=iK
            iK+=1