Ejemplo n.º 1
0
def main(options=None):
    if options == None:
        f = open(SaveName, 'rb')
        options = pickle.load(f)

    ListDirSpec = [
        l.strip() for l in open(options.DirsDynSpecMSProds).readlines()
        if not ".tgz" in l
    ]

    D = {}
    sdb = surveys_db.SurveysDB()
    for iFile, f in enumerate(ListDirSpec):
        OBSID = str(f.split("_L")[1])
        Di = sdb.get_observation(OBSID)
        Field = Di["field"]
        D[iFile] = {}
        D[iFile]["OBSID"] = str(OBSID)
        D[iFile]["Field"] = str(Field)
    sdb.close()

    MyPickle.Save(D, "LoTSS_OBSID_vs_Field.Dico")

    for iFile in list(D.keys()):
        OBSID = D[iFile]["OBSID"]
        Field = D[iFile]["Field"]
        ss = "ms2dynspec.py --imageI /databf/lofar/SURVEYS_KSP/LOTSS/DynSpecMS/IMAGES/%s/image_full_ampphase_di_m.NS_shift.int.facetRestored.fits --imageV /databf/lofar/SURVEYS_KSP/LOTSS/DynSpecMS/IMAGES/%s/image_full_low_stokesV.dirty.fits --BaseDirSpecs /databf/lofar/SURVEYS_KSP/LOTSS/DynSpecMS/DynSpecs_L%s --srclist Transient_LOTTS.csv" % (
            Field, Field, OBSID)
        print(ss)
        os.system(ss)
Ejemplo n.º 2
0
def read_options():

    default_values = Parset.value_dict
    attrs = Parset.attr_dict

    desc = """Questions and suggestions: [email protected]"""

    OP = MyOptParse.MyOptParse(usage='Usage: %prog [parset file] <options>', version='%prog version '+report_version(),
                               description=desc, defaults=default_values, attributes=attrs)

    # create options based on contents of parset
    for section in Parset.sections:
        values = default_values[section]
        # "_Help" value in each section is its documentation string
        OP.OptionGroup(values.get("_Help", section), section)
        for name, value in getattr(default_values[section], "iteritems", default_values[section].items)():
            if not attrs[section][name].get("no_cmdline"):
                OP.add_option(name, value)


    OP.Finalise()
    OP.ReadInput()

    # #optcomplete.autocomplete(opt)

    # options, arguments = opt.parse_args()
    MyPickle.Save(OP, SaveFile)
    return OP
Ejemplo n.º 3
0
    def reinitPop2(self, pop):
        print >> log, "Initialise population"
        x0, x1 = self.x.min(), self.x.max()
        y0, y1 = self.y.min(), self.y.max()
        N = len(pop)
        # p=np.load("pop.npy")
        # for i,ii in enumerate(p): pop[i][:]=ii[:]
        # return

        pop0 = pop[0:N / 2]
        pop1 = pop[N / 2:]
        for iIndiv, Indiv in enumerate(pop0):
            #print iIndiv,len(pop0)
            x, y = Indiv.reshape((2, self.nNode))
            indSel = DDFacet.ToolsDir.GeneDist.GiveNonRedundantSample(
                self.S, self.nNode)
            x[:] = self.x[indSel]
            y[:] = self.y[indSel]
        for iIndiv, Indiv in enumerate(pop1):
            #print iIndiv,len(pop1)
            x, y = Indiv.reshape((2, self.nNode))
            x[:] = np.random.uniform(x0, x1, self.nNode)
            y[:] = np.random.uniform(y0, y1, self.nNode)

        MyPickle.Save(pop, "pop.myPickle")
def fix_dico_shape(fulldico, outdico, NPixOut):
    # dico_model = 'image_full_ampphase_di_m.NS.DicoModel'
    # save_dico = dico_model.replace('.DicoModel', '.restricted.DicoModel')
    # NPixOut = 10000

    dico = MyPickle.Load(fulldico)

    NPix = dico['ModelShape'][-1]
    NPix0, _ = EstimateNpix(float(NPix), Padding=1)
    if NPix != NPix0:
        raise ValueError("NPix != NPix0")
    logger.info("Changing image size: %i -> %i pixels" % (NPix, NPixOut))
    xc0 = NPix // 2
    xc1 = NPixOut // 2
    dx = xc0 - xc1
    DCompOut = {}
    for k, v in dico.items():
        if k == 'Comp':
            DCompOut['Comp'] = {}
            continue
        DCompOut[k] = v
    DCompOut["Type"] = "SSD"

    N, M, _, _ = dico['ModelShape']
    DCompOut['ModelShape'] = [N, M, NPixOut, NPixOut]
    for (x0, y0) in dico['Comp'].keys():
        x1 = x0 - dx
        y1 = y0 - dx
        c0 = (x1 >= 0) & (x1 < NPixOut)
        c1 = (y1 >= 0) & (y1 < NPixOut)
        if c0 & c1:
            logger.info("Mapping (%i,%i)->(%i,%i)" % (x0, y0, x1, y1))
            DCompOut['Comp'][(x1, y1)] = dico['Comp'][(x0, y0)]
    logger.info("Saving in {}".format(outdico))
    MyPickle.Save(DCompOut, outdico)
Ejemplo n.º 5
0
    def ToFile(self, FileName, DicoIn=None):
        print("Saving dico model to %s" % FileName, file=log)
        if DicoIn is None:
            D = self.DicoSMStacked
        else:
            D = DicoIn

        D["ListScales"] = self.ListScales
        D["ModelShape"] = self.ModelShape
        MyPickle.Save(D, FileName)
Ejemplo n.º 6
0
    def ToFile(self, FileName, DicoIn=None):
        print >> log, "Saving dico model to %s" % FileName
        if DicoIn is None:
            D = self.DicoSMStacked
        else:
            D = DicoIn

        D["GD"] = self.GD
        D["Type"] = "HMP"
        D["ListScales"] = self.ListScales
        D["ModelShape"] = self.ModelShape
        MyPickle.Save(D, FileName)
Ejemplo n.º 7
0
    def ToFile(self, FileName, DicoIn=None):
        print >> log, "Saving dico model to %s" % FileName
        if DicoIn is None:
            D = self.DicoModel
        else:
            D = DicoIn

        D["GD"] = self.GD
        D["ModelShape"] = self.ModelShape
        D["Type"] = "MORESANE"

        MyPickle.Save(D, FileName)
Ejemplo n.º 8
0
    def ToFile(self, FileName, DicoIn=None):
        print("Saving dico model to %s" % FileName, file=log)
        if DicoIn is None:
            D = self.DicoModel
        else:
            D = DicoIn

        D["GD"] = self.GD
        D["ModelShape"] = self.ModelShape
        D["FreqsCube"] = self.GridFreqs
        D["Type"] = "MUFFIN"

        MyPickle.Save(D, FileName)
Ejemplo n.º 9
0
    def ToFile(self, FileName, DicoIn=None):
        print >> log, "Saving dico model to %s" % FileName
        if DicoIn is None:
            D = self.DicoSMStacked
        else:
            D = DicoIn

        #D["PM"]=self.PM
        D["GD"] = self.GD
        D["ModelShape"] = self.ModelShape
        D["Type"] = "SSD"
        D["SolveParam"] = self.SolveParam

        MyPickle.Save(D, FileName)
Ejemplo n.º 10
0
    def GiveSpectralIndexMap(self, threshold=0.1, save_dict=True):
        # Get the model image
        IM = self.GiveModelImage(self.FreqMachine.Freqsp)
        nchan, npol, Nx, Ny = IM.shape

        # Fit the alpha map
        self.FreqMachine.FitAlphaMap(IM[:, 0, :, :], threshold=threshold) # should set threshold based on SNR of final residual

        if save_dict:
            FileName = self.GD['Output']['Name'] + ".Dicoalpha"
            print>>log, "Saving componentwise SPI map to %s"%FileName

            MyPickle.Save(self.FreqMachine.alpha_dict, FileName)

        return self.FreqMachine.weighted_alpha_map.reshape((1, 1, Nx, Ny))
Ejemplo n.º 11
0
    def ToFile(self, FileName, DicoIn=None):
        print >> log, "Saving dico model to %s" % FileName
        if DicoIn is None:
            D = self.DicoSMStacked
        else:
            D = DicoIn

        D["GD"] = self.GD
        D["Type"] = "WSCMS"
        # try:
        #     D["ListScales"] = list(self.ScaleMachine.sigmas)  # list containing std of Gaussian components
        # except:
        #     D["ListScales"] = self.ListScales
        D["ModelShape"] = self.ModelShape
        MyPickle.Save(D, FileName)
Ejemplo n.º 12
0
    def ToFile(self, FileName, DicoIn=None):
        print("Saving dico model to %s" % FileName, file=log)
        if DicoIn is None:
            D = self.DicoSMStacked
        else:
            D = DicoIn

        if self.GD is None:
            print(
                "Warning - you are haven't initialised GD before writing to the DicoModel"
            )
        D["GD"] = self.GD
        D["Type"] = "WSCMS"
        D["ModelShape"] = self.ModelShape
        MyPickle.Save(D, FileName)
Ejemplo n.º 13
0
    def ToFile(self, FileName, DicoIn=None):
        print("Saving dico model to %s" % FileName, file=log)
        if DicoIn is None:
            D = self.DicoSMStacked
        else:
            D = DicoIn

        if self.GD is None:
            print("Warning - You have not initialised self.GD in ModelMachine so " \
                        "we can't write it to the DicoModel", file=log)

        D["GD"] = self.GD
        D["Type"] = "Hogbom"
        D["ListScales"] = "Delta"
        D["ModelShape"] = self.ModelShape
        MyPickle.Save(D, FileName)
Ejemplo n.º 14
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)))
Ejemplo n.º 15
0
    def setFacetsLocs(self):
        NFacets = self.NFacets
        Npix = self.GD["Image"]["NPix"]
        Padding = self.GD["Facets"]["Padding"]
        self.Padding = Padding
        Npix, _ = EstimateNpix(float(Npix), Padding=1)
        self.Npix = Npix
        self.OutImShape = (self.nch, self.npol, self.Npix, self.Npix)

        RadiusTot = self.CellSizeRad * self.Npix / 2
        self.RadiusTot = RadiusTot

        lMainCenter, mMainCenter = 0., 0.
        self.lmMainCenter = lMainCenter, mMainCenter
        self.CornersImageTot = np.array(
            [[lMainCenter - RadiusTot, mMainCenter - RadiusTot],
             [lMainCenter + RadiusTot, mMainCenter - RadiusTot],
             [lMainCenter + RadiusTot, mMainCenter + RadiusTot],
             [lMainCenter - RadiusTot, mMainCenter + RadiusTot]])

        # MSName = self.GD["Data"]["MS"]
        # if ".txt" in MSName:
        #     f = open(MSName)
        #     Ls = f.readlines()
        #     f.close()
        #     MSName = []
        #     for l in Ls:
        #         ll = l.replace("\n", "")
        #         MSName.append(ll)
        #     MSName = MSName[0]

        MSName = self.VS.ListMS[0].MSName

        SolsFile = self.GD["DDESolutions"]["DDSols"]
        if isinstance(SolsFile, list):
            SolsFile = self.GD["DDESolutions"]["DDSols"][0]

        if SolsFile and (not (".npz" in SolsFile)) and (not (".h5"
                                                             in SolsFile)):
            Method = SolsFile
            ThisMSName = reformat.reformat(os.path.abspath(MSName),
                                           LastSlash=False)
            SolsFile = "%s/killMS.%s.sols.npz" % (ThisMSName, Method)

#        if "CatNodes" in self.GD.keys():
        regular_grid = False
        if self.GD["Facets"]["CatNodes"] is not None:
            print >> log, "Taking facet directions from Nodes catalog: %s" % self.GD[
                "Facets"]["CatNodes"]
            ClusterNodes = np.load(self.GD["Facets"]["CatNodes"])
            ClusterNodes = ClusterNodes.view(np.recarray)
            raNode = ClusterNodes.ra
            decNode = ClusterNodes.dec
            lFacet, mFacet = self.CoordMachine.radec2lm(raNode, decNode)
        elif ".npz" in SolsFile:
            print >> log, "Taking facet directions from solutions file: %s" % SolsFile
            ClusterNodes = np.load(SolsFile)["ClusterCat"]
            ClusterNodes = ClusterNodes.view(np.recarray)
            raNode = ClusterNodes.ra
            decNode = ClusterNodes.dec
            lFacet, mFacet = self.CoordMachine.radec2lm(raNode, decNode)
        elif ".h5" in SolsFile:
            print >> log, "Taking facet directions from HDF5 solutions file: %s" % SolsFile
            H = tables.open_file(SolsFile)
            raNode, decNode = H.root.sol000.source[:]["dir"].T
            lFacet, mFacet = self.CoordMachine.radec2lm(raNode, decNode)
            H.close()
            del (H)
        else:
            print >> log, "Taking facet directions from regular grid"
            regular_grid = True
            CellSizeRad = (self.GD["Image"]["Cell"] / 3600.) * np.pi / 180
            lrad = Npix * CellSizeRad * 0.5

            NpixFacet = Npix / NFacets
            lfacet = NpixFacet * CellSizeRad * 0.5
            lcenter_max = lrad - lfacet

            lFacet, mFacet, = np.mgrid[-lcenter_max:lcenter_max:(NFacets) * 1j,
                                       -lcenter_max:lcenter_max:(NFacets) * 1j]
            lFacet = lFacet.flatten()
            mFacet = mFacet.flatten()
        print >> log, "  There are %i Jones-directions" % lFacet.size
        self.lmSols = lFacet.copy(), mFacet.copy()

        raSols, decSols = self.CoordMachine.lm2radec(lFacet.copy(),
                                                     mFacet.copy())
        self.radecSols = raSols, decSols

        NodesCat = np.zeros((raSols.size, ),
                            dtype=[('ra', np.float), ('dec', np.float),
                                   ('l', np.float), ('m', np.float)])
        NodesCat = NodesCat.view(np.recarray)
        NodesCat.ra = raSols
        NodesCat.dec = decSols
        # print>>log,"Facet RA %s"%raSols
        # print>>log,"Facet Dec %s"%decSols
        NodesCat.l = lFacet
        NodesCat.m = mFacet
        ## saving below
        # NodeFile = "%s.NodesCat.%snpy" % (self.GD["Output"]["Name"], "psf." if self.DoPSF else "")
        # print>> log, "Saving Nodes catalog in %s" % NodeFile
        # np.save(NodeFile, NodesCat)

        self.DicoImager = {}

        xy = np.zeros((lFacet.size, 2), np.float32)
        xy[:, 0] = lFacet
        xy[:, 1] = mFacet

        regFile = "%s.tessel0.reg" % self.ImageName
        NFacets = self.NFacets = lFacet.size
        rac, decc = self.MainRaDec
        VM = ModVoronoiToReg.VoronoiToReg(rac, decc)

        if NFacets > 2:

            vor = Voronoi(xy, furthest_site=False)
            regions, vertices = ModVoronoi.voronoi_finite_polygons_2d(
                vor, radius=1.)

            PP = Polygon.Polygon(self.CornersImageTot)

            LPolygon = []
            ListNode = []
            for region, iNode in zip(regions, range(NodesCat.shape[0])):
                ThisP = np.array(PP
                                 & Polygon.Polygon(np.array(vertices[region])))
                if ThisP.size > 0:
                    LPolygon.append(ThisP[0])
                    ListNode.append(iNode)
            NodesCat = NodesCat[np.array(ListNode)].copy()
# =======
#             LPolygon = [
#                 np.array(PP & Polygon.Polygon(np.array(vertices[region])))[0]
#                 for region in regions]
# >>>>>>> issue-255

        elif NFacets == 1:
            l0, m0 = lFacet[0], mFacet[0]
            LPolygon = [self.CornersImageTot]
        # VM.ToReg(regFile,lFacet,mFacet,radius=.1)

        NodeFile = "%s.NodesCat.npy" % self.GD["Output"]["Name"]
        print >> log, "Saving Nodes catalog in %s" % NodeFile
        np.save(NodeFile, NodesCat)

        for iFacet, polygon0 in zip(range(len(LPolygon)), LPolygon):
            # polygon0 = vertices[region]
            P = polygon0.tolist()

        # VM.PolygonToReg(regFile,LPolygon,radius=0.1,Col="red")

        # stop

        ###########################################
        # SubDivide
        def GiveDiam(polygon):
            lPoly, mPoly = polygon.T
            l0 = np.max([lMainCenter - RadiusTot, lPoly.min()])
            l1 = np.min([lMainCenter + RadiusTot, lPoly.max()])
            m0 = np.max([mMainCenter - RadiusTot, mPoly.min()])
            m1 = np.min([mMainCenter + RadiusTot, mPoly.max()])
            dl = l1 - l0
            dm = m1 - m0
            diam = np.max([dl, dm])
            return diam, (l0, l1, m0, m1)

        DiamMax = self.GD["Facets"]["DiamMax"] * np.pi / 180
        # DiamMax=4.5*np.pi/180
        DiamMin = self.GD["Facets"]["DiamMin"] * np.pi / 180

        def ClosePolygon(polygon):
            P = polygon.tolist()
            polygon = np.array(P + [P[0]])
            return polygon

        def GiveSubDivideRegions(polygonFacet, DMax):

            polygonFOV = self.CornersImageTot
            # polygonFOV=ClosePolygon(polygonFOV)
            PFOV = Polygon.Polygon(polygonFOV)

            # polygonFacet=ClosePolygon(polygonFacet)
            P0 = Polygon.Polygon(polygonFacet)
            P0Cut = Polygon.Polygon(P0 & PFOV)

            if P0Cut.nPoints() == 0:
                return []

            polygonFacetCut = np.array(P0Cut[0])
            # polygonFacetCut=ClosePolygon(polygonFacetCut)

            diam, (l0, l1, m0, m1) = GiveDiam(polygonFacetCut)
            if diam < DMax:
                return [polygonFacetCut]

            Nl = int((l1 - l0) / DMax) + 1
            Nm = int((m1 - m0) / DMax) + 1
            dl = (l1 - l0) / Nl
            dm = (m1 - m0) / Nm
            lEdge = np.linspace(l0, l1, Nl + 1)
            mEdge = np.linspace(m0, m1, Nm + 1)
            lc = (lEdge[0:-1] + lEdge[1::]) / 2
            mc = (mEdge[0:-1] + mEdge[1::]) / 2
            LPoly = []
            Lc, Mc = np.meshgrid(lc, mc)
            Lc = Lc.ravel().tolist()
            Mc = Mc.ravel().tolist()

            DpolySquare = np.array([[-dl, -dm], [dl, -dm], [dl, dm], [-dl, dm]
                                    ]) * 0.5
            for lc, mc in zip(Lc, Mc):
                polySquare = DpolySquare.copy(
                )  # ClosePolygon(DpolySquare.copy())
                polySquare[:, 0] += lc
                polySquare[:, 1] += mc
                # polySquare=ClosePolygon(polySquare)
                P1 = Polygon.Polygon(polySquare)

                POut = (P0Cut & P1)
                if POut.nPoints() == 0:
                    continue

                polyOut = np.array(POut[0])
                # polyOut=ClosePolygon(polyOut)
                LPoly.append(polyOut)

                # pylab.clf()
                # x,y=polygonFacetCut.T
                # pylab.plot(x,y,color="blue")
                # x,y=polygonFacet.T
                # pylab.plot(x,y,color="blue",ls=":",lw=3)
                # x,y=np.array(PFOV[0]).T
                # pylab.plot(x,y,color="black")
                # x,y=polySquare.T
                # pylab.plot(x,y,color="green",ls=":",lw=3)
                # x,y=polyOut.T
                # pylab.plot(x,y,color="red",ls="--",lw=3)
                # pylab.xlim(-0.03,0.03)
                # pylab.ylim(-0.03,0.03)
                # pylab.draw()
                # pylab.show(False)
                # pylab.pause(0.5)

            return LPoly

        def PlotPolygon(P, *args, **kwargs):
            for poly in P:
                x, y = ClosePolygon(np.array(poly)).T
                pylab.plot(x, y, *args, **kwargs)

        LPolygonNew = []

        for iFacet in xrange(len(LPolygon)):
            polygon = LPolygon[iFacet]
            ThisDiamMax = DiamMax
            SubReg = GiveSubDivideRegions(polygon, ThisDiamMax)

            LPolygonNew += SubReg

        regFile = "%s.FacetMachine.tessel.ReCut.reg" % self.ImageName
        # VM.PolygonToReg(regFile,LPolygonNew,radius=0.1,Col="green",labels=[str(i) for i in range(len(LPolygonNew))])

        DicoPolygon = {}
        for iFacet in xrange(len(LPolygonNew)):
            DicoPolygon[iFacet] = {}
            poly = LPolygonNew[iFacet]
            DicoPolygon[iFacet]["poly"] = poly
            diam, (l0, l1, m0, m1) = GiveDiam(poly)
            DicoPolygon[iFacet]["diam"] = diam
            DicoPolygon[iFacet]["diamMin"] = np.min([(l1 - l0), (m1 - m0)])
            xc, yc = np.mean(poly[:, 0]), np.mean(poly[:, 1])
            DicoPolygon[iFacet]["xyc"] = xc, yc
            dSol = np.sqrt((xc - lFacet)**2 + (yc - mFacet)**2)
            DicoPolygon[iFacet]["iSol"] = np.where(dSol == np.min(dSol))[0]

        for iFacet in sorted(DicoPolygon.keys()):
            diam = DicoPolygon[iFacet]["diamMin"]
            # print iFacet,diam,DiamMin
            if diam < DiamMin:
                dmin = 1e6
                xc0, yc0 = DicoPolygon[iFacet]["xyc"]
                HasClosest = False
                for iFacetOther in sorted(DicoPolygon.keys()):
                    if iFacetOther == iFacet:
                        continue
                    iSolOther = DicoPolygon[iFacetOther]["iSol"]
                    # print "  ",iSolOther,DicoPolygon[iFacet]["iSol"]
                    if iSolOther != DicoPolygon[iFacet]["iSol"]:
                        continue
                    xc, yc = DicoPolygon[iFacetOther]["xyc"]
                    d = np.sqrt((xc - xc0)**2 + (yc - yc0)**2)
                    if d < dmin:
                        dmin = d
                        iFacetClosest = iFacetOther
                        HasClosest = True
                if (HasClosest):
                    print >> log, "Merging facet #%i to #%i" % (iFacet,
                                                                iFacetClosest)
                    P0 = Polygon.Polygon(DicoPolygon[iFacet]["poly"])
                    P1 = Polygon.Polygon(DicoPolygon[iFacetClosest]["poly"])
                    P2 = (P0 | P1)
                    POut = []
                    for iP in xrange(len(P2)):
                        POut += P2[iP]

                    poly = np.array(POut)
                    hull = ConvexHull(poly)
                    Contour = np.array([
                        hull.points[hull.vertices, 0],
                        hull.points[hull.vertices, 1]
                    ])
                    poly2 = Contour.T

                    del (DicoPolygon[iFacet])
                    DicoPolygon[iFacetClosest]["poly"] = poly2
                    DicoPolygon[iFacetClosest]["diam"] = GiveDiam(poly2)[0]
                    DicoPolygon[iFacetClosest]["xyc"] = np.mean(
                        poly2[:, 0]), np.mean(poly2[:, 1])

        # stop
        LPolygonNew = []
        for iFacet in sorted(DicoPolygon.keys()):
            # if DicoPolygon[iFacet]["diam"]<DiamMin:
            #     print>>log, ModColor.Str("  Facet #%i associated to direction #%i is too small, removing it"%(iFacet,DicoPolygon[iFacet]["iSol"]))
            #     continue
            LPolygonNew.append(DicoPolygon[iFacet]["poly"])

        # for iFacet in range(len(regions)):
        #     polygon=LPolygon[iFacet]
        #     ThisDiamMax=DiamMax
        #     while True:
        #         SubReg=GiveSubDivideRegions(polygon,ThisDiamMax)
        #         if SubReg==[]:
        #             break
        #         Diams=[GiveDiam(poly)[0] for poly in SubReg]

        #         if np.min(Diams)>DiamMin: break
        #         ThisDiamMax*=1.1
        #     LPolygonNew+=SubReg
        #     print

        regFile = "%s.tessel.%sreg" % (self.GD["Output"]["Name"],
                                       "psf." if self.DoPSF else "")
        # labels=["[F%i.C%i]"%(i,DicoPolygon[i]["iSol"]) for i in range(len(LPolygonNew))]
        # VM.PolygonToReg(regFile,LPolygonNew,radius=0.1,Col="green",labels=labels)

        # VM.PolygonToReg(regFile,LPolygonNew,radius=0.1,Col="green")

        # pylab.clf()
        # x,y=LPolygonNew[11].T
        # pylab.plot(x,y)
        # pylab.draw()
        # pylab.show()
        # stop
        ###########################################

        NFacets = len(LPolygonNew)

        NJonesDir = NodesCat.shape[0]
        self.JonesDirCat = np.zeros(
            (NodesCat.shape[0], ),
            dtype=[('Name', '|S200'), ('ra', np.float), ('dec', np.float),
                   ('SumI', np.float), ("Cluster", int), ("l", np.float),
                   ("m", np.float), ("I", np.float)])
        self.JonesDirCat = self.JonesDirCat.view(np.recarray)
        self.JonesDirCat.I = 1
        self.JonesDirCat.SumI = 1

        self.JonesDirCat.ra = NodesCat.ra
        self.JonesDirCat.dec = NodesCat.dec
        self.JonesDirCat.l = NodesCat.l
        self.JonesDirCat.m = NodesCat.m
        self.JonesDirCat.Cluster = range(NJonesDir)

        print >> log, "Sizes (%i facets):" % (self.JonesDirCat.shape[0])
        print >> log, "   - Main field :   [%i x %i] pix" % (self.Npix,
                                                             self.Npix)

        l_m_Diam = np.zeros((NFacets, 4), np.float32)
        l_m_Diam[:, 3] = np.arange(NFacets)

        Np = 10000
        D = {}
        for iFacet in xrange(NFacets):
            D[iFacet] = {}
            polygon = LPolygonNew[iFacet]
            D[iFacet]["Polygon"] = polygon
            lPoly, mPoly = polygon.T

            ThisDiam, (l0, l1, m0, m1) = GiveDiam(polygon)

            # ###############################
            # # Find barycenter of polygon
            # X=(np.random.rand(Np))*ThisDiam+l0
            # Y=(np.random.rand(Np))*ThisDiam+m0
            # XY = np.dstack((X, Y))
            # XY_flat = XY.reshape((-1, 2))
            # mpath = Path( polygon )
            # XY = np.dstack((X, Y))
            # XY_flat = XY.reshape((-1, 2))
            # mask_flat = mpath.contains_points(XY_flat)
            # mask=mask_flat.reshape(X.shape)
            # ###############################
            ThisPolygon = Polygon.Polygon(polygon)
            lc, mc = ThisPolygon.center()
            dl = np.max(np.abs([l0 - lc, l1 - lc]))
            dm = np.max(np.abs([m0 - mc, m1 - mc]))
            ###############################
            # lc=np.sum(X*mask)/np.sum(mask)
            # mc=np.sum(Y*mask)/np.sum(mask)
            # dl=np.max(np.abs(X[mask==1]-lc))
            # dm=np.max(np.abs(Y[mask==1]-mc))
            diam = 2 * np.max([dl, dm])

            ######################
            # lc=(l0+l1)/2.
            # mc=(m0+m1)/2.
            # dl=l1-l0
            # dm=m1-m0
            # diam=np.max([dl,dm])

            l_m_Diam[iFacet, 0] = lc
            l_m_Diam[iFacet, 1] = mc
            l_m_Diam[iFacet, 2] = diam

        self.SpacialWeigth = {}
        self.DicoImager = {}

        # sort facets by size, unless we're in regular grid mode
        if not regular_grid:
            indDiam = np.argsort(l_m_Diam[:, 2])[::-1]
            l_m_Diam = l_m_Diam[indDiam]

        for iFacet in xrange(l_m_Diam.shape[0]):
            self.DicoImager[iFacet] = {}
            self.DicoImager[iFacet]["Polygon"] = D[l_m_Diam[iFacet,
                                                            3]]["Polygon"]
            x0 = round(l_m_Diam[iFacet, 0] / self.CellSizeRad)
            y0 = round(l_m_Diam[iFacet, 1] / self.CellSizeRad)
            if x0 % 2 == 0:
                x0 += 1
            if y0 % 2 == 0:
                y0 += 1
            l0 = x0 * self.CellSizeRad
            m0 = y0 * self.CellSizeRad
            diam = round(
                l_m_Diam[iFacet, 2] / self.CellSizeRad) * self.CellSizeRad
            # self.AppendFacet(iFacet,l0,m0,diam)
            self.AppendFacet(iFacet, l0, m0, diam)

        # self.MakeMasksTessel()

        NpixMax = np.max([
            self.DicoImager[iFacet]["NpixFacet"]
            for iFacet in sorted(self.DicoImager.keys())
        ])
        NpixMaxPadded = np.max([
            self.DicoImager[iFacet]["NpixFacetPadded"]
            for iFacet in sorted(self.DicoImager.keys())
        ])
        self.PaddedGridShape = (1, 1, NpixMaxPadded, NpixMaxPadded)
        self.FacetShape = (1, 1, NpixMax, NpixMax)

        dmin = 1
        for iFacet in xrange(len(self.DicoImager)):
            l, m = self.DicoImager[iFacet]["l0m0"]
            d = np.sqrt(l**2 + m**2)
            if d < dmin:
                dmin = d
                iCentralFacet = iFacet
        self.iCentralFacet = iCentralFacet
        self.NFacets = len(self.DicoImager)
        # regFile="%s.tessel.reg"%self.GD["Output"]["Name"]
        labels = [(self.DicoImager[i]["lmShift"][0],
                   self.DicoImager[i]["lmShift"][1],
                   "[F%i_S%i]" % (i, self.DicoImager[i]["iSol"]))
                  for i in xrange(len(LPolygonNew))]
        VM.PolygonToReg(regFile,
                        LPolygonNew,
                        radius=0.1,
                        Col="green",
                        labels=labels)

        self.WriteCoordFacetFile()

        self.FacetDirections = set([
            self.DicoImager[iFacet]["RaDec"]
            for iFacet in range(len(self.DicoImager))
        ])
        #DicoName = "%s.DicoFacet" % self.GD["Images"]["ImageName"]
        DicoName = "%s.%sDicoFacet" % (self.GD["Output"]["Name"],
                                       "psf." if self.DoPSF else "")

        # Find the minimum l,m in the facet (for decorrelation calculation)
        for iFacet in self.DicoImager.keys():
            #Create smoothned facet tessel mask:
            Npix = self.DicoImager[iFacet]["NpixFacetPadded"]
            l0, l1, m0, m1 = self.DicoImager[iFacet]["lmExtentPadded"]
            X, Y = np.mgrid[l0:l1:Npix / 10 * 1j, m0:m1:Npix / 10 * 1j]
            XY = np.dstack((X, Y))
            XY_flat = XY.reshape((-1, 2))
            vertices = self.DicoImager[iFacet]["Polygon"]
            mpath = Path(vertices)  # the vertices of the polygon
            mask_flat = mpath.contains_points(XY_flat)
            mask = mask_flat.reshape(X.shape)
            mpath = Path(self.CornersImageTot)
            mask_flat2 = mpath.contains_points(XY_flat)
            mask2 = mask_flat2.reshape(X.shape)
            mask[mask2 == 0] = 0
            R = np.sqrt(X**2 + Y**2)
            R[mask == 0] = 1e6
            indx, indy = np.where(R == np.min(R))
            lmin, mmin = X[indx[0], indy[0]], Y[indx[0], indy[0]]
            self.DicoImager[iFacet]["lm_min"] = lmin, mmin

        print >> log, "Saving DicoImager in %s" % DicoName
        MyPickle.Save(self.DicoImager, DicoName)
Ejemplo n.º 16
0
                        default=0,
                        help="Boring?",
                        required=False)
    parser.add_argument("--image",
                        type=str,
                        default=None,
                        help="Survey image to plot",
                        required=False)
    parser.add_argument("--uv",
                        type=list,
                        default=[1., 1000.],
                        help="UV range in km [UVmin, UVmax]",
                        required=False)
    parser.add_argument("--SolsDir",
                        type=str,
                        default="",
                        help="Base directory for the DDE solutions",
                        required=False)
    parser.add_argument("--NCPU",
                        type=int,
                        default=0,
                        help="NCPU",
                        required=False)
    args = parser.parse_args()

    MyPickle.Save(args, SaveFile)

    ModColor.silent = progressbar.ProgressBar.silent = args.LogBoring

    main(args)