From a4769f3a76ccc2b2d2e35720ba34cf7018414a5b 2012-08-29 22:55:16 From: Miguel Valdez Date: 2012-08-29 22:55:16 Subject: [PATCH] Cambio en la forma de generacion de graficos espectrales --- diff --git a/schainpy/Graphics/BaseGraph.py b/schainpy/Graphics/BaseGraph.py index df45b8b..cfb3998 100644 --- a/schainpy/Graphics/BaseGraph.py +++ b/schainpy/Graphics/BaseGraph.py @@ -199,7 +199,6 @@ def savePlplot(filename,width,height): plplot.plsfnam(filename) plplot.plcpstrm(curr_strm,0) plplot.plreplot() - plplot.plclear() plplot.plend1() plplot.plsstrm(curr_strm) @@ -215,6 +214,13 @@ def initPlplot(indexPlot,ncol,nrow,winTitle,width,height): plplot.plspause(False) plplot.plssub(ncol,nrow) +def setNewPage(): + plplot.plbop() + plplot.pladv(0) + +def closePage(): + plplot.pleop() + def clearData(objGraph): objGraph.plotBox(objGraph.xrange[0], objGraph.xrange[1], objGraph.yrange[0], objGraph.yrange[1], "bc", "bc") @@ -238,7 +244,7 @@ def setStrm(indexPlot): def plFlush(): plplot.plflush() -def setPlTitle(pltitle,color): +def setPlTitle(pltitle,color, szchar=0.7): setSubpages(1, 0) plplot.pladv(0) plplot.plvpor(0., 1., 0., 1.) @@ -248,12 +254,14 @@ def setPlTitle(pltitle,color): if color == "white": plplot.plcol0(15) + plplot.plschr(0.0,szchar) plplot.plmtex("t",-1., 0.5, 0.5, pltitle) def setSubpages(ncol,nrow): plplot.plssub(ncol,nrow) class BaseGraph: + __name = None __xpos = None __ypos = None @@ -267,7 +275,9 @@ class BaseGraph: deltax = None xmin = None xmax = None + def __init__(self,name,subplot,xpos,ypos,xlabel,ylabel,title,szchar,xrange,yrange,zrange=None,deltax=1.0): + self.setName(name) self.setScreenPos(xpos, ypos) self.setLabels(xlabel,ylabel,title) @@ -553,7 +563,7 @@ class LinearPlot: -class SpectraPlot: +class PcolorPlot: pcolorObjDic = {} colorbarObjDic = {} pwprofileObjDic = {} @@ -577,11 +587,9 @@ class SpectraPlot: self.showPowerProfile = showPowerProfile self.XAxisAsTime = XAxisAsTime - nrow = 2 - if (nsubplot%2)==0: - ncol = nsubplot/nrow - else: - ncol = int(nsubplot)/nrow + 1 + + ncol = int(numpy.sqrt(nsubplot)+0.9) + nrow = int(nsubplot*1./ncol + 0.9) initPlplot(indexPlot,ncol,nrow,winTitle,self.width,self.height) setColormap(colormap) @@ -630,8 +638,13 @@ class SpectraPlot: return xpos,ypos - def setup(self,subplot,xmin,xmax,ymin,ymax,zmin,zmax,title,xlabel,ylabel): + def createObjects(self,subplot,xmin,xmax,ymin,ymax,zmin,zmax,title,xlabel,ylabel): + """ + Crea los objetos necesarios para un subplot + """ + # Config Spectra plot + szchar = 0.7 name = "spc" key = name + "%d"%subplot @@ -641,7 +654,6 @@ class SpectraPlot: xpos,ypos = self.setSpectraPos() pcolorObj = BaseGraph(name,subplot,xpos,ypos,xlabel,ylabel,title,szchar,xrange,yrange,zrange) - pcolorObj.plotBox(pcolorObj.xrange[0], pcolorObj.xrange[1], pcolorObj.yrange[0], pcolorObj.yrange[1], "bcnst", "bcnstv") self.pcolorObjDic[key] = pcolorObj # Config Colorbar @@ -654,9 +666,6 @@ class SpectraPlot: xrange = [0.,1.] yrange = [zmin,zmax] cmapObj = BaseGraph(name,subplot,xpos,ypos,"","","dB",szchar,xrange,yrange) - cmapObj.plotBox(cmapObj.xrange[0], cmapObj.xrange[1], cmapObj.yrange[0], cmapObj.yrange[1], "bc", "bcm") - cmapObj.colorbarPlot(cmapObj.xrange[0], cmapObj.xrange[1], cmapObj.yrange[0], cmapObj.yrange[1]) - cmapObj.plotBox(cmapObj.xrange[0], cmapObj.xrange[1], cmapObj.yrange[0], cmapObj.yrange[1], "bc", "bcmtsv") self.colorbarObjDic[key] = cmapObj # Config Power profile @@ -669,21 +678,59 @@ class SpectraPlot: xrange = [zmin,zmax] yrange = [ymin,ymax] powObj = BaseGraph(name,subplot,xpos,ypos,"dB","","Power Profile",szchar,xrange,yrange) + self.pwprofileObjDic[key] = powObj + + def setNewPage(self, pltitle='No title'): + szchar = 0.7 + setNewPage() + setPlTitle(pltitle,"black", szchar=szchar) + setSubpages(self.ncol, self.nrow) + + def closePage(self): + closePage() + + def iniPlot(self,subplot): + """ + Inicializa los subplots con su frame, titulo, etc + """ + + # Config Spectra plot + name = "spc" + key = name + "%d"%subplot + + pcolorObj = self.pcolorObjDic[key] + pcolorObj.plotBox(pcolorObj.xrange[0], pcolorObj.xrange[1], pcolorObj.yrange[0], pcolorObj.yrange[1], "bcnst", "bcnstv") + + # Config Colorbar + if self.showColorbar: + name = "colorbar" + key = name + "%d"%subplot + + cmapObj = self.colorbarObjDic[key] + cmapObj.plotBox(cmapObj.xrange[0], cmapObj.xrange[1], cmapObj.yrange[0], cmapObj.yrange[1], "bc", "bcmtsv") + cmapObj.colorbarPlot(cmapObj.xrange[0], cmapObj.xrange[1], cmapObj.yrange[0], cmapObj.yrange[1]) +# cmapObj.plotBox(cmapObj.xrange[0], cmapObj.xrange[1], cmapObj.yrange[0], cmapObj.yrange[1], "bc", "bcmtsv") + + # Config Power profile + if self.showPowerProfile: + name = "pwprofile" + key = name + "%d"%subplot + + powObj = self.pwprofileObjDic[key] powObj.setLineStyle(2) powObj.plotBox(powObj.xrange[0], powObj.xrange[1], powObj.yrange[0], powObj.yrange[1], "bcntg", "bc") powObj.setLineStyle(1) powObj.plotBox(powObj.xrange[0], powObj.xrange[1], powObj.yrange[0], powObj.yrange[1], "bc", "bc") - self.pwprofileObjDic[key] = powObj def printTitle(self,pltitle): - if self.__lastTitle != None: - setPlTitle(self.__lastTitle,"white") - - self.__lastTitle = pltitle +# if self.__lastTitle != None: +# setPlTitle(self.__lastTitle,"white") +# +# self.__lastTitle = pltitle setPlTitle(pltitle,"black") - setSubpages(self.ncol,self.nrow) +# setSubpages(self.ncol,self.nrow) def plot(self,subplot,x,y,z,subtitle): # Spectra plot @@ -693,8 +740,10 @@ class SpectraPlot: # newx = [x[0],x[-1]] pcolorObj = self.pcolorObjDic[key] + pcolorObj.plotBox(pcolorObj.xrange[0], pcolorObj.xrange[1], pcolorObj.yrange[0], pcolorObj.yrange[1], "bcst", "bcst") - pcolorObj.delLabels() + + #pcolorObj.delLabels() pcolorObj.setLabels(title=subtitle) deltax = None; deltay = None @@ -712,9 +761,10 @@ class SpectraPlot: deltay=deltay, getGrid=pcolorObj.getGrid) + #Solo se calcula la primera vez que se ingresa a la funcion pcolorObj.getGrid = False - pcolorObj.plotBox(pcolorObj.xrange[0], pcolorObj.xrange[1], pcolorObj.yrange[0], pcolorObj.yrange[1], "bcst", "bcst") + pcolorObj.plotBox(pcolorObj.xrange[0], pcolorObj.xrange[1], pcolorObj.yrange[0], pcolorObj.yrange[1], "bcst", "bcst", nolabels=True) # Power Profile if self.showPowerProfile: @@ -724,7 +774,7 @@ class SpectraPlot: powObj = self.pwprofileObjDic[key] if powObj.setXYData() != None: - clearData(powObj) + #clearData(powObj) powObj.setLineStyle(2) powObj.plotBox(powObj.xrange[0], powObj.xrange[1], powObj.yrange[0], powObj.yrange[1], "bcntg", "bc") powObj.setLineStyle(1) @@ -735,16 +785,11 @@ class SpectraPlot: powObj.basicXYPlot(power,y) powObj.setXYData(power,y) - def savePlot(self,indexPlot,path): + def savePlot(self,indexPlot,filename): - now = datetime.datetime.now().timetuple() - file = "spc_img%02d_%03d_%02d%02d%02d"%(indexPlot,now[7],now[3],now[4],now[5]) - filename = os.path.join(path,file+".png") width = self.width*self.ncol hei = self.height*self.nrow savePlplot(filename,width,hei) - - def refresh(self): plFlush() diff --git a/schainpy/Graphics/SpectraPlot.py b/schainpy/Graphics/SpectraPlot.py index fe9f0d9..334eb99 100644 --- a/schainpy/Graphics/SpectraPlot.py +++ b/schainpy/Graphics/SpectraPlot.py @@ -25,22 +25,45 @@ class Spectrum: self.__isPlotIni = False self.__xrange = None self.__yrange = None - self.nGraphs = 0 + self.nsubplots = 0 self.indexPlot = index self.spectraObj = Spectra - def setup(self,indexPlot,nsubplot,winTitle='',colormap="br_green",showColorbar=False,showPowerProfile=False,XAxisAsTime=False): - self.colorplotObj = SpectraPlot(indexPlot,nsubplot,winTitle,colormap,showColorbar,showPowerProfile,XAxisAsTime) - - def initPlot(self,xmin,xmax,ymin,ymax,zmin,zmax,titleList,xlabelList,ylabelList): - nsubplot = self.spectraObj.nChannels + def setup(self,indexPlot, nsubplots, winTitle='', colormap="br_green", showColorbar=False, showPowerProfile=False, XAxisAsTime=False): + """ + Crea un objeto colorPlot con las opciones seleccinoadas + """ + + self.nsubplots = nsubplots + self.colorplotObj = PcolorPlot(indexPlot, + nsubplots, + winTitle, + colormap, + showColorbar, + showPowerProfile, + XAxisAsTime) + + def createObjects(self,xmin,xmax,ymin,ymax,zmin,zmax,titleList,xlabelList,ylabelList): + """ + Configura cada subplot con los valores maximos y minimos incluyendo los subtitulos + """ - for index in range(nsubplot): + for index in range(self.nsubplots): title = titleList[index] xlabel = xlabelList[index] ylabel = ylabelList[index] subplot = index - self.colorplotObj.setup(subplot+1,xmin,xmax,ymin,ymax,zmin,zmax,title,xlabel,ylabel) + self.colorplotObj.createObjects(subplot+1,xmin,xmax,ymin,ymax,zmin,zmax,title,xlabel,ylabel) + + def initPlot(self): + """ + Configura cada subplot con los valores maximos y minimos incluyendo los subtitulos + """ + + + for index in range(self.nsubplots): + subplot = index + self.colorplotObj.iniPlot(subplot+1) def plotData(self, @@ -58,13 +81,24 @@ class Spectrum: showColorbar = True, showPowerProfile = True, XAxisAsTime = False, - save = False): + save = False, + channelList=[]): + + if channelList == []: + channelList = numpy.arange(self.spectraObj.nChannels) + - databuffer = 10.*numpy.log10(self.spectraObj.data_spc) - noise = 10.*numpy.log10(self.spectraObj.noise) + nsubplots = len(channelList) + nX = self.spectraObj.nFFTPoints + nY = self.spectraObj.nHeights - nsubplot = self.spectraObj.nChannels - nsubplot, nX, nY = numpy.shape(databuffer) + if self.spectraObj.noise == None: + noise = numpy.ones(nsubplots) + else: + noise = 10.*numpy.log10(self.spectraObj.noise[channelList]) + + datadB = 10.*numpy.log10(self.spectraObj.data_spc[channelList,:,:]) + noisedB = 10.*numpy.log10(noise) x = numpy.arange(nX) y = self.spectraObj.heightList @@ -72,23 +106,31 @@ class Spectrum: indexPlot = self.indexPlot if not(self.__isPlotConfig): - self.setup(indexPlot,nsubplot,winTitle,colormap,showColorbar,showPowerProfile,XAxisAsTime) + self.setup(indexPlot, + nsubplots, + winTitle, + colormap, + showColorbar, + showPowerProfile, + XAxisAsTime) + + self.__isPlotConfig = True - + if not(self.__isPlotIni): if titleList == None: titleList = [] - for i in range(nsubplot): + for i in range(nsubplots): titleList.append("Channel: %d - Noise: %.2f" %(i, noise[i])) if xlabelList == None: xlabelList = [] - for i in range(nsubplot): + for i in range(nsubplots): xlabelList.append("") if ylabelList == None: ylabelList = [] - for i in range(nsubplot): + for i in range(nsubplots): ylabelList.append("Range (Km)") if xmin == None: xmin = x[0] @@ -98,29 +140,31 @@ class Spectrum: if zmin == None: zmin = 0 if zmax == None: zmax = 120 - self.initPlot(xmin,xmax,ymin,ymax,zmin,zmax,titleList,xlabelList,ylabelList) + self.createObjects(xmin,xmax,ymin,ymax,zmin,zmax,titleList,xlabelList,ylabelList) self.__isPlotIni = True - self.colorplotObj.setFigure(indexPlot) - thisDatetime = datetime.datetime.fromtimestamp(self.spectraObj.m_BasicHeader.utc) pltitle = "Self Spectra - Date: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S")) - self.colorplotObj.printTitle(pltitle) #setPlTitle(pltitle) - - for index in range(nsubplot): - data = databuffer[index,:,:] - subtitle = "Channel: %d - Noise: %.2f" %(index, noise[index]) - self.colorplotObj.plot(index+1,x,y,data,subtitle) - + self.colorplotObj.setFigure(indexPlot) + self.colorplotObj.setNewPage(pltitle) + self.initPlot() + for channel in range(nsubplots): + data = datadB[channel,:,:] + subtitle = "Channel: %d - Noise: %.2f" %(channel, noise[channel]) + self.colorplotObj.plot(channel+1, x, y, data, subtitle) self.colorplotObj.refresh() if save: self.colorplotObj.setFigure(indexPlot) - path4plot = "/Users/jro/Pictures" - self.colorplotObj.savePlot(indexPlot,path4plot) - + path = "/home/roj-idl71/tmp/" + now = datetime.datetime.now().timetuple() + file = "spc_img%02d_%03d_%02d%02d%02d.png"%(indexPlot,now[7],now[3],now[4],now[5]) + filename = os.path.join(path,file) + self.colorplotObj.savePlot(indexPlot, filename) + + self.colorplotObj.closePage() diff --git a/schainpy/Graphics/VoltagePlot.py b/schainpy/Graphics/VoltagePlot.py index d2ac9f1..3d6cf04 100644 --- a/schainpy/Graphics/VoltagePlot.py +++ b/schainpy/Graphics/VoltagePlot.py @@ -94,13 +94,6 @@ class Osciloscope: self.linearplotObj.refresh() - - - - - - - class RTI: colorplotObj = None diff --git a/schainpy/Processing/JRONoise.py b/schainpy/Processing/JRONoise.py index 3a7e0ef..e332aeb 100644 --- a/schainpy/Processing/JRONoise.py +++ b/schainpy/Processing/JRONoise.py @@ -1,7 +1,6 @@ import numpy -from Model.Spectra import Spectra -def hildebrand_sekhon(Data, navg): +def hildebrand_sekhon(data, navg): """ This method is for the objective determination of de noise level in Doppler spectra. This implementation technique is based on the fact that the standard deviation of the spectral @@ -16,51 +15,39 @@ def hildebrand_sekhon(Data, navg): anoise : noise's level """ - divisor = 8 - ratio = 7 / divisor - data = Data.reshape(-1) - npts = data.size #numbers of points of the data + dataflat = data.reshape(-1) + dataflat.sort() + npts = dataflat.size #numbers of points of the data if npts < 32: print "error in noise - requires at least 32 points" return -1.0 + dataflat2 = numpy.power(dataflat,2) + + cs = numpy.cumsum(dataflat) + cs2 = numpy.cumsum(dataflat2) + # data sorted in ascending order - nmin = int(npts/divisor + ratio); - s = 0.0 - s2 = 0.0 - data2 = data[:npts] - data2.sort() - - for i in range(nmin): - s += data2[i] - s2 += data2[i]**2; - - icount = nmin - iflag = 0 + nmin = int((npts + 7.)/8) for i in range(nmin, npts): - s += data2[i]; - s2 += data2[i]**2 - icount=icount+1; - p = s / float(icount); + s = cs[i] + s2 = cs2[i] + p = s / float(i); p2 = p**2; - q = s2 / float(icount) - p2; + q = s2 / float(i) - p2; leftc = p2; rightc = q * float(navg); - - if leftc > rightc: - iflag = 1; #No weather signal + R2 = leftc/rightc + # Signal detect: R2 < 1 (R2 = leftc/rightc) - if(leftc < rightc): - if iflag: - break - - anoise = 0.0; - for j in range(i): - anoise += data2[j]; - - anoise = anoise / float(i); + if R2 < 1: + npts_noise = i + break + + + anoise = numpy.average(dataflat[0:npts_noise]) return anoise; diff --git a/schainpy/Processing/SpectraProcessor.py b/schainpy/Processing/SpectraProcessor.py index 6f5d91f..76bb241 100644 --- a/schainpy/Processing/SpectraProcessor.py +++ b/schainpy/Processing/SpectraProcessor.py @@ -6,6 +6,7 @@ Created on Feb 7, 2012 ''' import os, sys import numpy +import time path = os.path.split(os.getcwd())[0] sys.path.append(path) @@ -163,7 +164,7 @@ class SpectraProcessor: self.buffer[:,self.profIndex,:] = self.dataInObj.data self.profIndex += 1 - if self.profIndex == self.nFFTPoints: + if self.profIndex == self.nFFTPoints: self.__getFft() self.dataOutObj.flagNoData = False @@ -210,6 +211,7 @@ class SpectraProcessor: self.dataOutObj.m_ProcessingHeader.spectraComb self.dataOutObj.m_ProcessingHeader.shif_fft """ + if self.dataInObj.flagNoData: return 0 @@ -218,7 +220,8 @@ class SpectraProcessor: #calculo de self-spectra fft_volt = numpy.fft.fftshift(fft_volt,axes=(1,)) - spc = numpy.abs(fft_volt * numpy.conjugate(fft_volt)) + spc = fft_volt * numpy.conjugate(fft_volt) + spc = spc.real blocksize = 0 blocksize += dc.size @@ -240,7 +243,7 @@ class SpectraProcessor: self.dataOutObj.m_ProcessingHeader.blockSize = blocksize self.dataOutObj.m_BasicHeader.utc = self.dataInObj.m_BasicHeader.utc - self.getNoise() +# self.getNoise() def addWriter(self,wrpath): objWriter = SpectraWriter(self.dataOutObj) @@ -286,7 +289,8 @@ class SpectraProcessor: showPowerProfile=False, XAxisAsTime=False, save=False, - index=None): + index=None, + channelList=[]): if self.dataOutObj.flagNoData: return 0 @@ -308,7 +312,8 @@ class SpectraProcessor: showColorbar, showPowerProfile, XAxisAsTime, - save) + save, + channelList) self.plotterObjIndex += 1 @@ -330,16 +335,14 @@ class SpectraProcessor: #print "myIncohIntObj.navg: ",myIncohIntObj.navg self.dataOutObj.flagNoData = False - self.getNoise(type="hildebrand") -# self.getNoise(type="sort", parm=16) - + """Calcular el ruido""" + self.getNoise() else: self.dataOutObj.flagNoData = True self.integratorObjIndex += 1 - """Calcular el ruido""" -# self.getNoise(type="hildebrand", parm=1) + def removeDC(self, type):