From dc8fe29c201e80e40217d56291af1860845ed41f 2012-12-04 05:39:58 From: Miguel Valdez Date: 2012-12-04 05:39:58 Subject: [PATCH] -Se agrego la funcionalidad de replotear el grafico de RTI, ademas de los parametros timerange, xmin, xmax -Se modifico como se imprimen los labels del RTI en el driver de matplotlib --- diff --git a/schainpy/model/graphics/figure.py b/schainpy/model/graphics/figure.py index 9a4488b..0583027 100644 --- a/schainpy/model/graphics/figure.py +++ b/schainpy/model/graphics/figure.py @@ -34,20 +34,20 @@ class Figure: raise ValueError, "Abstract method: This method should be defined" - def getScreenDim(self): + def getScreenDim(self, widthplot, heightplot): nrow, ncol = self.getSubplots() - width = self.WIDTH*ncol - height = self.HEIGHT*nrow + widthscreen = widthplot*ncol + heightscreen = heightplot*nrow - return width, height + return widthscreen, heightscreen def init(self, idfigure, nplots, wintitle): raise ValueError, "This method has been replaced with createFigure" - def createFigure(self, idfigure, wintitle): + def createFigure(self, idfigure, wintitle, widthplot=None, heightplot=None): """ Crea la figura de acuerdo al driver y parametros seleccionados seleccionados. @@ -60,16 +60,22 @@ class Figure: """ + if widthplot == None: + widthplot = self.WIDTH + + if heightplot == None: + heightplot = self.HEIGHT + self.idfigure = idfigure self.wintitle = wintitle - self.width, self.height = self.getScreenDim() + self.widthscreen, self.heightscreen = self.getScreenDim(widthplot, heightplot) self.fig = self.__driver.createFigure(self.idfigure, self.wintitle, - self.width, - self.height) + self.widthscreen, + self.heightscreen) self.axesObjList = [] @@ -125,12 +131,16 @@ class Axes: ax = None plot = None - firsttime = None + __firsttime = None __showprofile = False - __zmin = None - __zmax = None + xmin = None + xmax = None + ymin = None + ymax = None + zmin = None + zmax = None def __init__(self, *args): @@ -146,7 +156,7 @@ class Axes: self.ax = ax self.plot = None - self.firsttime = True + self.__firsttime = True def setText(self, text): @@ -180,7 +190,7 @@ class Axes: ytick_visible """ - if self.firsttime: + if self.__firsttime: if xmin == None: xmin = numpy.nanmin(x) if xmax == None: xmax = numpy.nanmax(x) @@ -194,7 +204,7 @@ class Axes: ylabel=ylabel, title=title, **kwargs) - self.firsttime = False + self.__firsttime = False return self.__driver.pline(self.plot, x, y, xlabel=xlabel, @@ -230,7 +240,7 @@ class Axes: rti = True or False """ - if self.firsttime: + if self.__firsttime: if xmin == None: xmin = numpy.nanmin(x) if xmax == None: xmax = numpy.nanmax(x) @@ -248,15 +258,27 @@ class Axes: ylabel=ylabel, title=title, **kwargs) - self.firsttime = False - if self.__zmin == None: self.__zmin = zmin - if self.__zmax == None: self.__zmax = zmax + + if self.xmin == None: self.xmin = xmin + if self.xmax == None: self.xmax = xmax + if self.ymin == None: self.ymin = ymin + if self.ymax == None: self.ymax = ymax + if self.zmin == None: self.zmin = zmin + if self.zmax == None: self.zmax = zmax + + self.__firsttime = False return if rti: - self.__driver.addpcolor(self.ax, x, y, z, self.__zmin, self.__zmax) + self.__driver.addpcolor(self.ax, x, y, z, self.zmin, self.zmax, + xlabel=xlabel, + ylabel=ylabel, + title=title) return - self.__driver.pcolor(self.plot, z, xlabel=xlabel, ylabel=ylabel, title=title) + self.__driver.pcolor(self.plot, z, + xlabel=xlabel, + ylabel=ylabel, + title=title) \ No newline at end of file diff --git a/schainpy/model/graphics/mpldriver.py b/schainpy/model/graphics/mpldriver.py index c7c896e..ac35a3d 100644 --- a/schainpy/model/graphics/mpldriver.py +++ b/schainpy/model/graphics/mpldriver.py @@ -7,7 +7,9 @@ import matplotlib.dates #import scitools.numpyutils from mpl_toolkits.axes_grid1 import make_axes_locatable -from matplotlib.dates import DayLocator, HourLocator, MinuteLocator, SecondLocator, DateFormatter +from matplotlib.dates import DayLocator, HourLocator, MinuteLocator, SecondLocator, DateFormatter +from matplotlib.ticker import FuncFormatter +from matplotlib.ticker import * def init(idfigure, wintitle, width, height, facecolor="w"): @@ -200,7 +202,9 @@ def pline(iplot, x, y, xlabel='', ylabel='', title=''): iplot.set_data(x, y) -def createPcolor(ax, x, y, z, xmin, xmax, ymin, ymax, zmin, zmax, xlabel='', ylabel='', title='', ticksize = 9, cblabel='',XAxisAsTime=False): +def createPcolor(ax, x, y, z, xmin, xmax, ymin, ymax, zmin, zmax, + xlabel='', ylabel='', title='', ticksize = 9, + cblabel='',XAxisAsTime=False): divider = make_axes_locatable(ax) ax_cb = divider.new_horizontal(size="4%", pad=0.05) @@ -208,19 +212,6 @@ def createPcolor(ax, x, y, z, xmin, xmax, ymin, ymax, zmin, zmax, xlabel='', yla fig.add_axes(ax_cb) ax.set_xlim([xmin,xmax]) - - if XAxisAsTime: - seconds = numpy.array([xmin, xmax]) - datesList = map(datetime.datetime.fromtimestamp, seconds) - ax.set_xlim([datesList[0],datesList[-1]]) - ax.xaxis.set_major_locator(MinuteLocator(numpy.arange(0,61,10))) - ax.xaxis.set_minor_locator(SecondLocator(numpy.arange(0,61,60))) - ax.xaxis.set_major_formatter(DateFormatter("%H:%M:%S")) - xdateList = map(datetime.datetime.fromtimestamp, x) - xdate = matplotlib.dates.date2num(xdateList) - x = xdate - - ax.set_ylim([ymin,ymax]) printLabels(ax, xlabel, ylabel, title) @@ -231,8 +222,8 @@ def createPcolor(ax, x, y, z, xmin, xmax, ymin, ymax, zmin, zmax, xlabel='', yla ax_cb.yaxis.tick_right() - for tl in ax_cb.get_yticklabels(): - tl.set_visible(True) +# for tl in ax_cb.get_yticklabels(): +# tl.set_visible(True) for tick in ax.yaxis.get_major_ticks(): tick.label.set_fontsize(ticksize) @@ -246,6 +237,29 @@ def createPcolor(ax, x, y, z, xmin, xmax, ymin, ymax, zmin, zmax, xlabel='', yla ax_cb.yaxis.tick_right() matplotlib.pyplot.tight_layout() + if XAxisAsTime: + + func = lambda x, pos: ('%s') %(datetime.datetime.fromtimestamp(x).strftime("%H:%M:%S")) + ax.xaxis.set_major_formatter(FuncFormatter(func)) + ax.xaxis.set_major_locator(LinearLocator(7)) + +# seconds = numpy.array([xmin, xmax]) +# datesList = map(datetime.datetime.fromtimestamp, seconds) +# ax.set_xlim([datesList[0],datesList[-1]]) +# ax.xaxis.set_major_locator(MinuteLocator(numpy.arange(0,61,10))) +# ax.xaxis.set_minor_locator(SecondLocator(numpy.arange(0,61,60))) +# ax.xaxis.set_major_formatter(DateFormatter("%H:%M:%S")) +# xdateList = map(datetime.datetime.fromtimestamp, x) +# xdate = matplotlib.dates.date2num(xdateList) +# x = xdate + +# labels = [] +# for item in ax.xaxis.get_ticklabels(): +# stri = item.get_text() +# text = datetime.datetime.fromtimestamp(float(stri)) +# labels.append(text) +# +# ax.xaxis.set_ticklabels(labels) return imesh def pcolor(imesh, z, xlabel='', ylabel='', title=''): @@ -258,11 +272,14 @@ def pcolor(imesh, z, xlabel='', ylabel='', title=''): imesh.set_array(z.ravel()) -def addpcolor(ax, x, y, z, zmin, zmax): - xdateList = map(datetime.datetime.fromtimestamp, x) - xdate = matplotlib.dates.date2num(xdateList) +def addpcolor(ax, x, y, z, zmin, zmax, xlabel='', ylabel='', title=''): + +# xdateList = map(datetime.datetime.fromtimestamp, x) +# xdate = matplotlib.dates.date2num(xdateList) - imesh = ax.pcolormesh(xdate,y,z.T,vmin=zmin,vmax=zmax) + printLabels(ax, xlabel, ylabel, title) + + imesh = ax.pcolormesh(x,y,z.T,vmin=zmin,vmax=zmax) def draw(fig): diff --git a/schainpy/model/jroplot.py b/schainpy/model/jroplot.py index 17ee6d9..01501f3 100644 --- a/schainpy/model/jroplot.py +++ b/schainpy/model/jroplot.py @@ -1,5 +1,5 @@ import numpy -import datetime +import time, datetime from graphics.figure import * class RTIPlot(Figure): @@ -12,12 +12,12 @@ class RTIPlot(Figure): def __init__(self): - self.__timerange = 30*60 + self.__timerange = 24*60*60 self.__isConfig = False self.__nsubplots = 1 self.WIDTH = 800 - self.HEIGHT = 400 + self.HEIGHT = 300 self.WIDTHPROF = 120 self.HEIGHTPROF = 0 @@ -39,10 +39,11 @@ class RTIPlot(Figure): ncolspan = 7 colspan = 6 self.__nsubplots = 2 - self.WIDTH += self.WIDTHPROF - self.HEIGHT += self.HEIGHTPROF - self.createFigure(idfigure, wintitle) + self.createFigure(idfigure = idfigure, + wintitle = wintitle, + widthplot = self.WIDTH + self.WIDTHPROF, + heightplot = self.HEIGHT + self.HEIGHTPROF) nrow, ncol = self.getSubplots() @@ -60,8 +61,38 @@ class RTIPlot(Figure): counter += 1 + def __getTimeLim(self, x, xmin, xmax): + + thisdatetime = datetime.datetime.fromtimestamp(numpy.min(x)) + thisdate = datetime.datetime.combine(thisdatetime.date(), datetime.time(0,0,0)) + + #################################################### + #If the x is out of xrange + if xmax < (thisdatetime - thisdate).seconds/(60*60.): + xmin = None + xmax = None + + if xmin == None: + td = thisdatetime - thisdate + xmin = td.seconds/(60*60.) + + if xmax == None: + xmax = xmin + self.__timerange/(60*60.) + + mindt = thisdate + datetime.timedelta(0,0,0,0,0, xmin) + tmin = time.mktime(mindt.timetuple()) + + maxdt = thisdate + datetime.timedelta(0,0,0,0,0, xmax) + tmax = time.mktime(maxdt.timetuple()) + + self.__timerange = tmax - tmin + + return tmin, tmax + def run(self, dataOut, idfigure, wintitle="", channelList=None, showprofile='True', - xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None, save=False, filename=None): + xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None, + timerange=None, + save=False, filename=None): """ @@ -88,13 +119,18 @@ class RTIPlot(Figure): raise ValueError, "Channel %d is not in dataOut.channelList" channelIndexList.append(channel) + if timerange != None: + self.__timerange = timerange + + tmin = None + tmax = None x = dataOut.getDatatime() y = dataOut.getHeiRange() z = 10.*numpy.log10(dataOut.data_spc[channelIndexList,:,:]) avg = numpy.average(z, axis=1) noise = dataOut.getNoise() - + if not self.__isConfig: nplots = len(channelIndexList) @@ -104,8 +140,7 @@ class RTIPlot(Figure): wintitle=wintitle, showprofile=showprofile) - if xmin == None: xmin = numpy.min(x) - if xmax == None: xmax = xmin + self.__timerange + tmin, tmax = self.__getTimeLim(x, xmin, xmax) if ymin == None: ymin = numpy.nanmin(y) if ymax == None: ymax = numpy.nanmax(y) if zmin == None: zmin = numpy.nanmin(avg)*0.9 @@ -114,18 +149,18 @@ class RTIPlot(Figure): self.__isConfig = True thisDatetime = datetime.datetime.fromtimestamp(dataOut.utctime) - title = "Spectra: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S")) + title = "RTI: %s" %(thisDatetime.strftime("%d-%b-%Y")) xlabel = "Velocity (m/s)" ylabel = "Range (Km)" self.setWinTitle(title) for i in range(self.nplots): - title = "Channel %d: %4.2fdB" %(dataOut.channelList[i], noise[i]) + title = "Channel %d: %s" %(dataOut.channelList[i], thisDatetime.strftime("%d-%b-%Y %H:%M:%S")) axes = self.axesList[i*self.__nsubplots] z = avg[i].reshape((1,-1)) axes.pcolor(x, y, z, - xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax, + xmin=tmin, xmax=tmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax, xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True, ticksize=9, cblabel='') @@ -141,6 +176,9 @@ class RTIPlot(Figure): if save: self.saveFigure(filename) + + if x[1] + (x[1]-x[0]) >= self.axesList[0].xmax: + self.__isConfig = False class SpectraPlot(Figure): @@ -178,10 +216,11 @@ class SpectraPlot(Figure): ncolspan = 3 colspan = 2 self.__nsubplots = 2 - self.WIDTH += self.WIDTHPROF - self.HEIGHT += self.HEIGHTPROF - - self.createFigure(idfigure, wintitle) + + self.createFigure(idfigure = idfigure, + wintitle = wintitle, + widthplot = self.WIDTH + self.WIDTHPROF, + heightplot = self.HEIGHT + self.HEIGHTPROF) nrow, ncol = self.getSubplots()