def init2(): from pyrap.images import image im = image("/home/tasse/Desktop/FITS/image_049_073.img.restored.fits") PMaj = (im.imageinfo()["restoringbeam"]["major"]["value"]) PMin = (im.imageinfo()["restoringbeam"]["minor"]["value"]) PPA = (im.imageinfo()["restoringbeam"]["positionangle"]["value"]) print( ModColor.Str(" - Using psf (maj,min,pa)=(%6.2f, %6.2f, %6.2f)" % (PMaj, PMin, PPA), col='green', Bold=False)) ToSig = (1. / 3600.) * (np.pi / 180.) / (2. * np.sqrt(2. * np.log(2))) PMaj *= ToSig PMin *= ToSig PPA *= np.pi / 180 b = im.getdata()[0, 0, :, :] b = b[3000:4000, 3000:4000] #[100:250,200:350] c = im.coordinates() incr = np.abs(c.dict()["direction0"]["cdelt"][0]) print( ModColor.Str(" - Psf Size Sigma_(Maj,Min) = (%5.1f,%5.1f) pixels" % (PMaj / incr, PMin / incr), col="green", Bold=False)) Islands = ClassIslands.ClassIslands(b, 10., Boost=1, DoPlot=1) #Islands.Noise=30e-3 Islands.FindAllIslands() sourceList = [] ImOut = np.zeros_like(b) pylab.ion() for i in range(len(Islands.ListX)): #comment='Isl %i/%i' % (i+1,len(Islands.ListX)) #pBAR.render(int(100* float(i+1) / len(Islands.ListX)), comment) xin, yin, zin = np.array(Islands.ListX[i]), np.array( Islands.ListY[i]), np.array(Islands.ListS[i]) xm = int(np.sum(xin * zin) / np.sum(zin)) ym = int(np.sum(yin * zin) / np.sum(zin)) #Fit=ClassPointFit(xin,yin,zin,psf=(PMaj/incr,PMin/incr,PPA),noise=Islands.Noise) Fit = ClassPointFit(xin, yin, zin, psf=(PMaj / incr, PMin / incr, PPA), noise=Islands.Noise[xm, ym]) sourceList += Fit.DoAllFit() Fit.PutFittedArray(ImOut) xlist = [] ylist = [] slist = [] for ijs in sourceList: i, j, s = ijs xlist.append(i) ylist.append(j) slist.append(s) Islands.FittedComps = (xlist, ylist, slist) Islands.FitIm = ImOut Islands.plot()
def fillNoise(self): return A=self.A print ModColor.Str("Fill blanks with noise...") Avec=A.reshape((A.shape[1]*A.shape[0],)) ind=np.where(np.abs(Avec)>1e-8)[0] Avec=Avec[ind] rms=findrms.findrms(Avec) imnoise=np.random.randn(A.shape[0],A.shape[1])*rms ind=np.where(np.abs(A)<1e-8) A[ind]=imnoise[ind] print ModColor.Str(" rms in the image: %6.2f mJy"%(rms*1000.),col="green",Bold=False) print ModColor.Str(" done ...")
def Save(self): infile=self.infile print ModColor.Str(" SkyModel PROPERTIES: ") print " - SkyModel File Name: %s"%ModColor.Str(infile,col="green") if self.REGFile!=None: print " - ds9 region file: %s"%ModColor.Str(self.REGFile,col="green") npext="" if not(".npy" in infile): npext=".npy" self.NpFile="%s%s"%(infile,npext) np.save(infile,self.SourceCat) FileClusterCat="%s.ClusterCat.npy"%(self.infile) print " - ClusterCat File Name: %s"%ModColor.Str(FileClusterCat,col="green") np.save(FileClusterCat,self.ClusterCat) self.PrintBasics()
def VorToReg(self,regFile,vor,radius=0.1,Col="red"): print>>log,"Writing voronoi in: %s"%ModColor.Str(regFile,col="blue") f=open(regFile,"w") f.write("# Region file format: DS9 version 4.1\n") ss0='global color=green dashlist=8 3 width=1 font="helvetica 10 normal roman" select=1 highlite=1 dash=0' ss1=' fixed=0 edit=1 move=1 delete=1 include=1 source=1\n' f.write(ss0+ss1) f.write("fk5\n") CoordMachine=self.CoordMachine regions, vertices = vor.regions,vor.vertices for region in regions: if len(region)==0: continue polygon0 = vertices[region] P=polygon0.tolist() polygon=np.array(P+[P[0]]) for iline in range(polygon.shape[0]-1): x0,y0=CoordMachine.lm2radec(np.array([polygon[iline][0]]),np.array([polygon[iline][1]])) x1,y1=CoordMachine.lm2radec(np.array([polygon[iline+1][0]]),np.array([polygon[iline+1][1]])) x0*=180./np.pi y0*=180./np.pi x1*=180./np.pi y1*=180./np.pi f.write("line(%f,%f,%f,%f) # line=0 0 color=%s dash=1\n"%(x0,y0,x1,y1,Col)) #f.write("line(%f,%f,%f,%f) # line=0 0 color=red dash=1\n"%(x1,y0,x0,y1)) f.close()
def ComputeNoiseMap(self): print ModColor.Str("Compute noise map...") Boost=self.Boost Acopy=self.A[0::Boost,0::Boost].copy() SBox=(self.box[0]/Boost,self.box[1]/Boost) Noise=np.sqrt(scipy.ndimage.filters.median_filter(np.abs(Acopy)**2,SBox)) self.Noise=np.zeros_like(self.A) 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] print ModColor.Str(" ... done") ind=np.where(self.Noise==0.) self.Noise[ind]=1e-10
def PolygonToReg(self,regFile,LPolygon,radius=0.1,Col="red",labels=None): print("Writing voronoi in: %s"%ModColor.Str(regFile,col="blue"), file=log) f=open(regFile,"w") f.write("# Region file format: DS9 version 4.1\n") ss0='global color=green dashlist=8 3 width=1 font="helvetica 10 normal roman" select=1 highlite=1 dash=0' ss1=' fixed=0 edit=1 move=1 delete=1 include=1 source=1\n' f.write(ss0+ss1) f.write("fk5\n") CoordMachine=self.CoordMachine for iFacet,polygon0 in zip(list(range(len(LPolygon))),LPolygon): #polygon0 = vertices[region] P=polygon0.tolist() if len(polygon0)==0: continue polygon=np.array(P+[P[0]]) ThisText="" if labels!=None: lmean0=np.mean(polygon[:,0]) mmean0=np.mean(polygon[:,1]) lmean,mmean,ThisText=labels[iFacet] # print "!!!!======" # print lmean0,mmean0 # print lmean,mmean xm,ym=CoordMachine.lm2radec(np.array([lmean]),np.array([mmean])) xm*=180./np.pi ym*=180./np.pi f.write("point(%f,%f) # text={%s} point=circle 5 color=red width=2\n"%(xm,ym,ThisText)) for iline in range(polygon.shape[0]-1): L0,M0=np.array([polygon[iline][0]]),np.array([polygon[iline][1]]) x0,y0=CoordMachine.lm2radec(L0,M0) L1,M1=np.array([polygon[iline+1][0]]),np.array([polygon[iline+1][1]]) x1,y1=CoordMachine.lm2radec(L1,M1) x0*=180./np.pi y0*=180./np.pi x1*=180./np.pi y1*=180./np.pi # print "====================" # print "[%3.3i] %f %f %f %f"%(iline,x0,y0,x1,y1) # print " %s"%str(L0) # print " %s"%str(L1) # print " %s"%str(M0) # print " %s"%str(M1) f.write("line(%f,%f,%f,%f) # line=0 0 color=%s dash=1 \n"%(x0,y0,x1,y1,Col)) #f.write("line(%f,%f,%f,%f) # line=0 0 color=red dash=1\n"%(x1,y0,x0,y1)) f.close()
def printStatus(self,DictNode): print() print("State:") for ind in list(DictNode.keys()): print(ModColor.Str("Cluster %i: "%ind,col="green")) print(" - ListCluster : %s"%str(DictNode[ind]["ListCluster"])) print(" - SourceNum : %s"%str(DictNode[ind]["SourceNum"])) print(" - Flux : %s"%str(DictNode[ind]["TotalFlux"]))
def PrintBasics(self): infile=self.infile npext="" if not(".npy" in infile): npext=".npy" print " - Numpy catalog file: %s"%ModColor.Str("%s%s"%(infile,npext),col="green") #print "Oufile: %s"%self.infile_cluster #if infile_cluster!="": #print " - Cluster File Name: %s"%self.infile_cluster print " - Number of Sources = ",self.SourceCat.shape[0] print " - Number of Directions = ",self.NDir print
def FindIsland(self,A,Lpix,x,y,dirfrom=-1,threshold=1): T=threshold digo=set(range(4))-set([dirfrom]) #print Lpix #time.sleep(1.) #if (x,y) in Lpix: return Lpix pos=(x,y) S=ModColor.Str("@(%i,%i) "%(x,y),col="blue") try: aa=A[x,y] except: return if A[x,y]==False: #print ModColor.Str("(%i,%i)"%(x,y)) return if A[x,y]==True: #print S,ModColor.Str("(%i,%i)"%(x,y),col="green") Lpix.append((x,y)) if 0 in digo: this=(x+1,y) if not(this in Lpix): #print S,"-> from %i and going to %i"%(dirfrom,0) self.FindIsland(A,Lpix,x+1,y,dirfrom=2) if 2 in digo: this=(x-1,y) if not(this in Lpix): #print S,"-> from %i and going to %i"%(dirfrom,2) self.FindIsland(A,Lpix,x-1,y,dirfrom=0) if 1 in digo: this=(x,y+1) if not(this in Lpix): #print S,"-> from %i and going to %i"%(dirfrom,1) self.FindIsland(A,Lpix,x,y+1,dirfrom=3) if 3 in digo: this=(x,y-1) if not(this in Lpix): #print "-> from %i and going to %i"%(dirfrom,3) self.FindIsland(A,Lpix,x,y-1,dirfrom=1)
def SaveNP(self): infile=self.NpFile np.save(self.NpFile,self.SourceCat) print " - Numpy catalog file: %s"%ModColor.Str(self.NpFile,col="green")