@@ -1,284 +1,294 | |||
|
1 | import os | |
|
1 | 2 | import numpy |
|
2 | 3 | import mpldriver |
|
3 | 4 | |
|
4 | 5 | |
|
5 | 6 | class Figure: |
|
6 | 7 | |
|
7 | 8 | __driver = mpldriver |
|
8 | 9 | fig = None |
|
9 | 10 | |
|
10 | 11 | idfigure = None |
|
11 | 12 | wintitle = None |
|
12 | 13 | width = None |
|
13 | 14 | height = None |
|
14 | 15 | nplots = None |
|
15 | 16 | |
|
16 | 17 | axesObjList = [] |
|
17 | 18 | |
|
18 | 19 | WIDTH = None |
|
19 | 20 | HEIGHT = None |
|
21 | PREFIX = 'fig' | |
|
20 | 22 | |
|
21 | 23 | def __init__(self): |
|
22 | 24 | |
|
23 | 25 | raise ValueError, "This method is not implemented" |
|
24 | 26 | |
|
25 | 27 | def __del__(self): |
|
26 | 28 | |
|
27 | 29 | self.__driver.closeFigure() |
|
30 | ||
|
31 | def getFilename(self, name, ext='.png'): | |
|
32 | ||
|
33 | filename = '%s_%s%s' %(self.PREFIX, name, ext) | |
|
34 | ||
|
35 | return filename | |
|
28 | 36 | |
|
29 | 37 | def getAxesObjList(self): |
|
30 | 38 | |
|
31 | 39 | return self.axesObjList |
|
32 | 40 | |
|
33 | 41 | def getSubplots(self): |
|
34 | 42 | |
|
35 | 43 | raise ValueError, "Abstract method: This method should be defined" |
|
36 | 44 | |
|
37 | 45 | def getScreenDim(self, widthplot, heightplot): |
|
38 | 46 | |
|
39 | 47 | nrow, ncol = self.getSubplots() |
|
40 | 48 | |
|
41 | 49 | widthscreen = widthplot*ncol |
|
42 | 50 | heightscreen = heightplot*nrow |
|
43 | 51 | |
|
44 | 52 | return widthscreen, heightscreen |
|
45 | 53 | |
|
46 | 54 | def init(self, idfigure, nplots, wintitle): |
|
47 | 55 | |
|
48 | 56 | raise ValueError, "This method has been replaced with createFigure" |
|
49 | 57 | |
|
50 | 58 | def createFigure(self, idfigure, wintitle, widthplot=None, heightplot=None): |
|
51 | 59 | |
|
52 | 60 | """ |
|
53 | 61 | Crea la figura de acuerdo al driver y parametros seleccionados seleccionados. |
|
54 | 62 | Las dimensiones de la pantalla es calculada a partir de los atributos self.WIDTH |
|
55 | 63 | y self.HEIGHT y el numero de subplots (nrow, ncol) |
|
56 | 64 | |
|
57 | 65 | Input: |
|
58 | 66 | idfigure : Los parametros necesarios son |
|
59 | 67 | wintitle : |
|
60 | 68 | |
|
61 | 69 | """ |
|
62 | 70 | |
|
63 | 71 | if widthplot == None: |
|
64 | 72 | widthplot = self.WIDTH |
|
65 | 73 | |
|
66 | 74 | if heightplot == None: |
|
67 | 75 | heightplot = self.HEIGHT |
|
68 | 76 | |
|
69 | 77 | self.idfigure = idfigure |
|
70 | 78 | |
|
71 | 79 | self.wintitle = wintitle |
|
72 | 80 | |
|
73 | 81 | self.widthscreen, self.heightscreen = self.getScreenDim(widthplot, heightplot) |
|
74 | 82 | |
|
75 | 83 | self.fig = self.__driver.createFigure(self.idfigure, |
|
76 | 84 | self.wintitle, |
|
77 | 85 | self.widthscreen, |
|
78 | 86 | self.heightscreen) |
|
79 | 87 | |
|
80 | 88 | self.axesObjList = [] |
|
81 | 89 | |
|
82 | 90 | def setDriver(self, driver=mpldriver): |
|
83 | 91 | |
|
84 | 92 | self.__driver = driver |
|
85 | 93 | |
|
86 | 94 | def setTitle(self, title): |
|
87 | 95 | |
|
88 | 96 | self.__driver.setTitle(self.fig, title) |
|
89 | 97 | |
|
90 | 98 | def setWinTitle(self, title): |
|
91 | 99 | |
|
92 | 100 | self.__driver.setWinTitle(self.fig, title=title) |
|
93 | 101 | |
|
94 | 102 | def setTextFromAxes(self, text): |
|
95 | 103 | |
|
96 | 104 | raise ValueError, "Este metodo ha sido reemplazaado con el metodo setText de la clase Axes" |
|
97 | 105 | |
|
98 | 106 | def makeAxes(self, nrow, ncol, xpos, ypos, colspan, rowspan): |
|
99 | 107 | |
|
100 | 108 | raise ValueError, "Este metodo ha sido reemplazaado con el metodo addAxes" |
|
101 | 109 | |
|
102 | 110 | def addAxes(self, *args): |
|
103 | 111 | """ |
|
104 | 112 | |
|
105 | 113 | Input: |
|
106 | 114 | *args : Los parametros necesarios son |
|
107 | 115 | nrow, ncol, xpos, ypos, colspan, rowspan |
|
108 | 116 | """ |
|
109 | 117 | |
|
110 | 118 | axesObj = Axes(self.fig, *args) |
|
111 | 119 | self.axesObjList.append(axesObj) |
|
112 | 120 | |
|
113 | def saveFigure(self, *args): | |
|
114 | self.__driver.saveFigure(self.fig, *args) | |
|
121 | def saveFigure(self, figpath, figfile, *args): | |
|
122 | ||
|
123 | filename = os.path.join(figpath, figfile) | |
|
124 | self.__driver.saveFigure(self.fig, filename, *args) | |
|
115 | 125 | |
|
116 | 126 | def draw(self): |
|
117 | 127 | |
|
118 | 128 | self.__driver.draw(self.fig) |
|
119 | 129 | |
|
120 | 130 | def run(self): |
|
121 | 131 | |
|
122 | 132 | raise ValueError, "This method is not implemented" |
|
123 | 133 | |
|
124 | 134 | axesList = property(getAxesObjList) |
|
125 | 135 | |
|
126 | 136 | |
|
127 | 137 | class Axes: |
|
128 | 138 | |
|
129 | 139 | __driver = mpldriver |
|
130 | 140 | fig = None |
|
131 | 141 | ax = None |
|
132 | 142 | plot = None |
|
133 | 143 | |
|
134 | 144 | __firsttime = None |
|
135 | 145 | |
|
136 | 146 | __showprofile = False |
|
137 | 147 | |
|
138 | 148 | xmin = None |
|
139 | 149 | xmax = None |
|
140 | 150 | ymin = None |
|
141 | 151 | ymax = None |
|
142 | 152 | zmin = None |
|
143 | 153 | zmax = None |
|
144 | 154 | |
|
145 | 155 | def __init__(self, *args): |
|
146 | 156 | |
|
147 | 157 | """ |
|
148 | 158 | |
|
149 | 159 | Input: |
|
150 | 160 | *args : Los parametros necesarios son |
|
151 | 161 | fig, nrow, ncol, xpos, ypos, colspan, rowspan |
|
152 | 162 | """ |
|
153 | 163 | |
|
154 | 164 | ax = self.__driver.createAxes(*args) |
|
155 | 165 | self.fig = args[0] |
|
156 | 166 | self.ax = ax |
|
157 | 167 | self.plot = None |
|
158 | 168 | |
|
159 | 169 | self.__firsttime = True |
|
160 | 170 | |
|
161 | 171 | def setText(self, text): |
|
162 | 172 | |
|
163 | 173 | self.__driver.setAxesText(self.ax, text) |
|
164 | 174 | |
|
165 | 175 | def setXAxisAsTime(self): |
|
166 | 176 | pass |
|
167 | 177 | |
|
168 | 178 | def pline(self, x, y, |
|
169 | 179 | xmin=None, xmax=None, |
|
170 | 180 | ymin=None, ymax=None, |
|
171 | 181 | xlabel='', ylabel='', |
|
172 | 182 | title='', |
|
173 | 183 | **kwargs): |
|
174 | 184 | |
|
175 | 185 | """ |
|
176 | 186 | |
|
177 | 187 | Input: |
|
178 | 188 | x : |
|
179 | 189 | y : |
|
180 | 190 | xmin : |
|
181 | 191 | xmax : |
|
182 | 192 | ymin : |
|
183 | 193 | ymax : |
|
184 | 194 | xlabel : |
|
185 | 195 | ylabel : |
|
186 | 196 | title : |
|
187 | 197 | **kwargs : Los parametros aceptados son |
|
188 | 198 | |
|
189 | 199 | ticksize |
|
190 | 200 | ytick_visible |
|
191 | 201 | """ |
|
192 | 202 | |
|
193 | 203 | if self.__firsttime: |
|
194 | 204 | |
|
195 | 205 | if xmin == None: xmin = numpy.nanmin(x) |
|
196 | 206 | if xmax == None: xmax = numpy.nanmax(x) |
|
197 | 207 | if ymin == None: ymin = numpy.nanmin(y) |
|
198 | 208 | if ymax == None: ymax = numpy.nanmax(y) |
|
199 | 209 | |
|
200 | 210 | self.plot = self.__driver.createPline(self.ax, x, y, |
|
201 | 211 | xmin, xmax, |
|
202 | 212 | ymin, ymax, |
|
203 | 213 | xlabel=xlabel, |
|
204 | 214 | ylabel=ylabel, |
|
205 | 215 | title=title, |
|
206 | 216 | **kwargs) |
|
207 | 217 | self.__firsttime = False |
|
208 | 218 | return |
|
209 | 219 | |
|
210 | 220 | self.__driver.pline(self.plot, x, y, xlabel=xlabel, |
|
211 | 221 | ylabel=ylabel, |
|
212 | 222 | title=title) |
|
213 | 223 | |
|
214 | 224 | |
|
215 | 225 | def pcolor(self, x, y, z, |
|
216 | 226 | xmin=None, xmax=None, |
|
217 | 227 | ymin=None, ymax=None, |
|
218 | 228 | zmin=None, zmax=None, |
|
219 | 229 | xlabel='', ylabel='', |
|
220 | 230 | title='', rti = False, |
|
221 | 231 | **kwargs): |
|
222 | 232 | |
|
223 | 233 | """ |
|
224 | 234 | Input: |
|
225 | 235 | x : |
|
226 | 236 | y : |
|
227 | 237 | x : |
|
228 | 238 | xmin : |
|
229 | 239 | xmax : |
|
230 | 240 | ymin : |
|
231 | 241 | ymax : |
|
232 | 242 | zmin : |
|
233 | 243 | zmax : |
|
234 | 244 | xlabel : |
|
235 | 245 | ylabel : |
|
236 | 246 | title : |
|
237 | 247 | **kwargs : Los parametros aceptados son |
|
238 | 248 | ticksize=9, |
|
239 | 249 | cblabel='' |
|
240 | 250 | rti = True or False |
|
241 | 251 | """ |
|
242 | 252 | |
|
243 | 253 | if self.__firsttime: |
|
244 | 254 | |
|
245 | 255 | if xmin == None: xmin = numpy.nanmin(x) |
|
246 | 256 | if xmax == None: xmax = numpy.nanmax(x) |
|
247 | 257 | if ymin == None: ymin = numpy.nanmin(y) |
|
248 | 258 | if ymax == None: ymax = numpy.nanmax(y) |
|
249 | 259 | if zmin == None: zmin = numpy.nanmin(z) |
|
250 | 260 | if zmax == None: zmax = numpy.nanmax(z) |
|
251 | 261 | |
|
252 | 262 | |
|
253 | 263 | self.plot = self.__driver.createPcolor(self.ax, x, y, z, |
|
254 | 264 | xmin, xmax, |
|
255 | 265 | ymin, ymax, |
|
256 | 266 | zmin, zmax, |
|
257 | 267 | xlabel=xlabel, |
|
258 | 268 | ylabel=ylabel, |
|
259 | 269 | title=title, |
|
260 | 270 | **kwargs) |
|
261 | 271 | |
|
262 | 272 | if self.xmin == None: self.xmin = xmin |
|
263 | 273 | if self.xmax == None: self.xmax = xmax |
|
264 | 274 | if self.ymin == None: self.ymin = ymin |
|
265 | 275 | if self.ymax == None: self.ymax = ymax |
|
266 | 276 | if self.zmin == None: self.zmin = zmin |
|
267 | 277 | if self.zmax == None: self.zmax = zmax |
|
268 | 278 | |
|
269 | 279 | self.__firsttime = False |
|
270 | 280 | return |
|
271 | 281 | |
|
272 | 282 | if rti: |
|
273 | 283 | self.__driver.addpcolor(self.ax, x, y, z, self.zmin, self.zmax, |
|
274 | 284 | xlabel=xlabel, |
|
275 | 285 | ylabel=ylabel, |
|
276 | 286 | title=title) |
|
277 | 287 | return |
|
278 | 288 | |
|
279 | 289 | self.__driver.pcolor(self.plot, z, |
|
280 | 290 | xlabel=xlabel, |
|
281 | 291 | ylabel=ylabel, |
|
282 | 292 | title=title) |
|
283 | 293 | |
|
284 | 294 | No newline at end of file |
@@ -1,288 +1,305 | |||
|
1 | 1 | import numpy |
|
2 | 2 | import datetime |
|
3 | 3 | import matplotlib |
|
4 | 4 | matplotlib.use("TKAgg") |
|
5 | 5 | import matplotlib.pyplot |
|
6 | 6 | import matplotlib.dates |
|
7 | 7 | #import scitools.numpyutils |
|
8 | 8 | from mpl_toolkits.axes_grid1 import make_axes_locatable |
|
9 | 9 | |
|
10 | 10 | from matplotlib.dates import DayLocator, HourLocator, MinuteLocator, SecondLocator, DateFormatter |
|
11 | 11 | from matplotlib.ticker import FuncFormatter |
|
12 | 12 | from matplotlib.ticker import * |
|
13 | 13 | |
|
14 | 14 | def init(idfigure, wintitle, width, height, facecolor="w"): |
|
15 | 15 | |
|
16 | 16 | matplotlib.pyplot.ioff() |
|
17 | 17 | fig = matplotlib.pyplot.matplotlib.pyplot.figure(num=idfigure, facecolor=facecolor) |
|
18 | 18 | fig.canvas.manager.set_window_title(wintitle) |
|
19 | 19 | fig.canvas.manager.resize(width, height) |
|
20 | 20 | matplotlib.pyplot.ion() |
|
21 | 21 | |
|
22 | 22 | return fig |
|
23 | 23 | |
|
24 | 24 | def setWinTitle(fig, title): |
|
25 | 25 | |
|
26 | 26 | fig.canvas.manager.set_window_title(title) |
|
27 | 27 | |
|
28 | 28 | def setTitle(idfigure, title): |
|
29 | 29 | fig = matplotlib.pyplot.figure(idfigure) |
|
30 | 30 | fig.suptitle(title) |
|
31 | 31 | |
|
32 | 32 | def makeAxes(idfigure, nrow, ncol, xpos, ypos, colspan, rowspan): |
|
33 | 33 | fig = matplotlib.pyplot.figure(idfigure) |
|
34 | 34 | ax = matplotlib.pyplot.subplot2grid((nrow, ncol), (xpos, ypos), colspan=colspan, rowspan=rowspan) |
|
35 | 35 | return ax |
|
36 | 36 | |
|
37 | 37 | def setTextFromAxes(idfigure, ax, title): |
|
38 | 38 | fig = matplotlib.pyplot.figure(idfigure) |
|
39 | 39 | ax.annotate(title, xy=(.1, .99), |
|
40 | 40 | xycoords='figure fraction', |
|
41 | 41 | horizontalalignment='left', verticalalignment='top', |
|
42 | 42 | fontsize=10) |
|
43 | 43 | |
|
44 | 44 | def pline(ax, x, y, xmin, xmax, ymin, ymax, xlabel, ylabel, title, firsttime): |
|
45 | 45 | |
|
46 | 46 | if firsttime: |
|
47 | 47 | ax.plot(x, y) |
|
48 | 48 | ax.set_xlim([xmin,xmax]) |
|
49 | 49 | ax.set_ylim([ymin,ymax]) |
|
50 | 50 | ax.set_xlabel(xlabel, size=8) |
|
51 | 51 | ax.set_ylabel(ylabel, size=8) |
|
52 | 52 | ax.set_title(title, size=10) |
|
53 | 53 | matplotlib.pyplot.tight_layout() |
|
54 | 54 | else: |
|
55 | 55 | ax.lines[0].set_data(x,y) |
|
56 | 56 | |
|
57 | 57 | def draw(idfigure): |
|
58 | 58 | |
|
59 | 59 | fig = matplotlib.pyplot.figure(idfigure) |
|
60 | 60 | fig.canvas.draw() |
|
61 | 61 | |
|
62 | 62 | def pcolor(ax, x, y, z, xmin, xmax, ymin, ymax, zmin, zmax, xlabel, ylabel, title, firsttime, mesh): |
|
63 | 63 | |
|
64 | 64 | if firsttime: |
|
65 | 65 | divider = make_axes_locatable(ax) |
|
66 | 66 | ax_cb = divider.new_horizontal(size="4%", pad=0.05) |
|
67 | 67 | fig1 = ax.get_figure() |
|
68 | 68 | fig1.add_axes(ax_cb) |
|
69 | 69 | |
|
70 | 70 | ax.set_xlim([xmin,xmax]) |
|
71 | 71 | ax.set_ylim([ymin,ymax]) |
|
72 | 72 | ax.set_xlabel(xlabel) |
|
73 | 73 | ax.set_ylabel(ylabel) |
|
74 | 74 | ax.set_title(title) |
|
75 | 75 | print x |
|
76 | 76 | imesh=ax.pcolormesh(x,y,z.T,vmin=zmin,vmax=zmax) |
|
77 | 77 | matplotlib.pyplot.colorbar(imesh, cax=ax_cb) |
|
78 | 78 | ax_cb.yaxis.tick_right() |
|
79 | 79 | for tl in ax_cb.get_yticklabels(): |
|
80 | 80 | tl.set_visible(True) |
|
81 | 81 | ax_cb.yaxis.tick_right() |
|
82 | 82 | matplotlib.pyplot.tight_layout() |
|
83 | 83 | return imesh |
|
84 | 84 | else: |
|
85 | 85 | # ax.set_xlim([xmin,xmax]) |
|
86 | 86 | # ax.set_ylim([ymin,ymax]) |
|
87 | 87 | ax.set_xlabel(xlabel) |
|
88 | 88 | ax.set_ylabel(ylabel) |
|
89 | 89 | ax.set_title(title) |
|
90 | 90 | |
|
91 | 91 | z = z.T |
|
92 | 92 | # z = z[0:-1,0:-1] |
|
93 | 93 | mesh.set_array(z.ravel()) |
|
94 | 94 | |
|
95 | 95 | return mesh |
|
96 | 96 | |
|
97 | 97 | ########################################### |
|
98 | 98 | #Actualizacion de las funciones del driver |
|
99 | 99 | ########################################### |
|
100 | 100 | |
|
101 | 101 | def createFigure(idfigure, wintitle, width, height, facecolor="w"): |
|
102 | 102 | |
|
103 | 103 | matplotlib.pyplot.ioff() |
|
104 | 104 | fig = matplotlib.pyplot.matplotlib.pyplot.figure(num=idfigure, facecolor=facecolor) |
|
105 | 105 | fig.canvas.manager.set_window_title(wintitle) |
|
106 | 106 | fig.canvas.manager.resize(width, height) |
|
107 | 107 | matplotlib.pyplot.ion() |
|
108 | 108 | |
|
109 | 109 | return fig |
|
110 | 110 | |
|
111 | 111 | def closeFigure(): |
|
112 | 112 | |
|
113 | 113 | matplotlib.pyplot.ioff() |
|
114 | 114 | matplotlib.pyplot.show() |
|
115 | 115 | |
|
116 | 116 | return |
|
117 | 117 | |
|
118 | 118 | def saveFigure(fig, filename): |
|
119 | 119 | fig.savefig(filename) |
|
120 | 120 | |
|
121 | 121 | def setWinTitle(fig, title): |
|
122 | 122 | |
|
123 | 123 | fig.canvas.manager.set_window_title(title) |
|
124 | 124 | |
|
125 | 125 | def setTitle(fig, title): |
|
126 | 126 | |
|
127 | 127 | fig.suptitle(title) |
|
128 | 128 | |
|
129 | 129 | def createAxes(fig, nrow, ncol, xpos, ypos, colspan, rowspan): |
|
130 | 130 | |
|
131 | 131 | matplotlib.pyplot.figure(fig.number) |
|
132 | 132 | axes = matplotlib.pyplot.subplot2grid((nrow, ncol), |
|
133 | 133 | (xpos, ypos), |
|
134 | 134 | colspan=colspan, |
|
135 | 135 | rowspan=rowspan) |
|
136 | 136 | return axes |
|
137 | 137 | |
|
138 | 138 | def setAxesText(ax, text): |
|
139 | 139 | |
|
140 | 140 | ax.annotate(text, |
|
141 | 141 | xy = (.1, .99), |
|
142 | 142 | xycoords = 'figure fraction', |
|
143 | 143 | horizontalalignment = 'left', |
|
144 | 144 | verticalalignment = 'top', |
|
145 | 145 | fontsize = 10) |
|
146 | 146 | |
|
147 | 147 | def printLabels(ax, xlabel, ylabel, title): |
|
148 | 148 | |
|
149 | 149 | ax.set_xlabel(xlabel, size=11) |
|
150 | 150 | ax.set_ylabel(ylabel, size=11) |
|
151 | 151 | ax.set_title(title, size=12) |
|
152 | 152 | |
|
153 | 153 | def createPline(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title='', |
|
154 | 154 | ticksize=9, xtick_visible=True, ytick_visible=True, |
|
155 | 155 | nxticks=4, nyticks=10, |
|
156 | 156 | grid=None): |
|
157 | 157 | |
|
158 | 158 | """ |
|
159 | 159 | |
|
160 | 160 | Input: |
|
161 | 161 | grid : None, 'both', 'x', 'y' |
|
162 | 162 | """ |
|
163 | 163 | |
|
164 | 164 | ax.plot(x, y) |
|
165 | 165 | ax.set_xlim([xmin,xmax]) |
|
166 | 166 | ax.set_ylim([ymin,ymax]) |
|
167 | 167 | |
|
168 | 168 | printLabels(ax, xlabel, ylabel, title) |
|
169 | 169 | |
|
170 | 170 | ###################################################### |
|
171 | 171 | xtickspos = numpy.arange(nxticks)*int((xmax-xmin)/nxticks) + int(xmin) |
|
172 | 172 | ax.set_xticks(xtickspos) |
|
173 | 173 | |
|
174 | 174 | for tick in ax.get_xticklabels(): |
|
175 | 175 | tick.set_visible(xtick_visible) |
|
176 | 176 | |
|
177 | 177 | for tick in ax.xaxis.get_major_ticks(): |
|
178 | 178 | tick.label.set_fontsize(ticksize) |
|
179 | 179 | |
|
180 | 180 | ###################################################### |
|
181 | 181 | for tick in ax.get_yticklabels(): |
|
182 | 182 | tick.set_visible(ytick_visible) |
|
183 | 183 | |
|
184 | 184 | for tick in ax.yaxis.get_major_ticks(): |
|
185 | 185 | tick.label.set_fontsize(ticksize) |
|
186 | ||
|
187 | iplot = ax.lines[-1] | |
|
186 | 188 | |
|
187 | 189 | ###################################################### |
|
190 | if '0.' in matplotlib.__version__[0:2]: | |
|
191 | print "The matplotlib version has to be updated to 1.1 or newer" | |
|
192 | return iplot | |
|
193 | ||
|
194 | if '1.0.' in matplotlib.__version__[0:4]: | |
|
195 | print "The matplotlib version has to be updated to 1.1 or newer" | |
|
196 | return iplot | |
|
197 | ||
|
188 | 198 | if grid != None: |
|
189 | 199 | ax.grid(b=True, which='major', axis=grid) |
|
190 | 200 | |
|
191 | 201 | matplotlib.pyplot.tight_layout() |
|
192 | 202 | |
|
193 | iplot = ax.lines[-1] | |
|
194 | ||
|
195 | 203 | return iplot |
|
196 | 204 | |
|
197 | 205 | def pline(iplot, x, y, xlabel='', ylabel='', title=''): |
|
198 | 206 | |
|
199 | 207 | ax = iplot.get_axes() |
|
200 | 208 | |
|
201 | 209 | printLabels(ax, xlabel, ylabel, title) |
|
202 | 210 | |
|
203 | 211 | iplot.set_data(x, y) |
|
204 | 212 | |
|
205 | 213 | def createPcolor(ax, x, y, z, xmin, xmax, ymin, ymax, zmin, zmax, |
|
206 | 214 | xlabel='', ylabel='', title='', ticksize = 9, |
|
207 | 215 | cblabel='', cbsize="5%", |
|
208 | 216 | XAxisAsTime=False): |
|
209 | 217 | |
|
210 | 218 | divider = make_axes_locatable(ax) |
|
211 | 219 | ax_cb = divider.new_horizontal(size=cbsize, pad=0.05) |
|
212 | 220 | fig = ax.get_figure() |
|
213 | 221 | fig.add_axes(ax_cb) |
|
214 | 222 | |
|
215 | 223 | ax.set_xlim([xmin,xmax]) |
|
216 | 224 | ax.set_ylim([ymin,ymax]) |
|
217 | 225 | |
|
218 | 226 | printLabels(ax, xlabel, ylabel, title) |
|
219 | 227 | |
|
220 | 228 | imesh = ax.pcolormesh(x,y,z.T,vmin=zmin,vmax=zmax) |
|
221 | 229 | cb = matplotlib.pyplot.colorbar(imesh, cax=ax_cb) |
|
222 | 230 | cb.set_label(cblabel) |
|
223 | 231 | |
|
224 | 232 | # for tl in ax_cb.get_yticklabels(): |
|
225 | 233 | # tl.set_visible(True) |
|
226 | 234 | |
|
227 | 235 | for tick in ax.yaxis.get_major_ticks(): |
|
228 | 236 | tick.label.set_fontsize(ticksize) |
|
229 | 237 | |
|
230 | 238 | for tick in ax.xaxis.get_major_ticks(): |
|
231 | 239 | tick.label.set_fontsize(ticksize) |
|
232 | 240 | |
|
233 | 241 | for tick in cb.ax.get_yticklabels(): |
|
234 | 242 | tick.set_fontsize(ticksize) |
|
235 | 243 | |
|
236 | 244 | ax_cb.yaxis.tick_right() |
|
245 | ||
|
246 | if '0.' in matplotlib.__version__[0:2]: | |
|
247 | print "The matplotlib version has to be updated to 1.1 or newer" | |
|
248 | return imesh | |
|
249 | ||
|
250 | if '1.0.' in matplotlib.__version__[0:4]: | |
|
251 | print "The matplotlib version has to be updated to 1.1 or newer" | |
|
252 | return imesh | |
|
253 | ||
|
237 | 254 | matplotlib.pyplot.tight_layout() |
|
238 | 255 | |
|
239 | 256 | if XAxisAsTime: |
|
240 | 257 | |
|
241 | 258 | func = lambda x, pos: ('%s') %(datetime.datetime.fromtimestamp(x).strftime("%H:%M:%S")) |
|
242 | 259 | ax.xaxis.set_major_formatter(FuncFormatter(func)) |
|
243 | 260 | ax.xaxis.set_major_locator(LinearLocator(7)) |
|
244 | 261 | |
|
245 | 262 | # seconds = numpy.array([xmin, xmax]) |
|
246 | 263 | # datesList = map(datetime.datetime.fromtimestamp, seconds) |
|
247 | 264 | # ax.set_xlim([datesList[0],datesList[-1]]) |
|
248 | 265 | # ax.xaxis.set_major_locator(MinuteLocator(numpy.arange(0,61,10))) |
|
249 | 266 | # ax.xaxis.set_minor_locator(SecondLocator(numpy.arange(0,61,60))) |
|
250 | 267 | # ax.xaxis.set_major_formatter(DateFormatter("%H:%M:%S")) |
|
251 | 268 | # xdateList = map(datetime.datetime.fromtimestamp, x) |
|
252 | 269 | # xdate = matplotlib.dates.date2num(xdateList) |
|
253 | 270 | # x = xdate |
|
254 | 271 | |
|
255 | 272 | # labels = [] |
|
256 | 273 | # for item in ax.xaxis.get_ticklabels(): |
|
257 | 274 | # stri = item.get_text() |
|
258 | 275 | # text = datetime.datetime.fromtimestamp(float(stri)) |
|
259 | 276 | # labels.append(text) |
|
260 | 277 | # |
|
261 | 278 | # ax.xaxis.set_ticklabels(labels) |
|
262 | 279 | return imesh |
|
263 | 280 | |
|
264 | 281 | def pcolor(imesh, z, xlabel='', ylabel='', title=''): |
|
265 | 282 | |
|
266 | 283 | z = z.T |
|
267 | 284 | |
|
268 | 285 | ax = imesh.get_axes() |
|
269 | 286 | |
|
270 | 287 | printLabels(ax, xlabel, ylabel, title) |
|
271 | 288 | |
|
272 | 289 | imesh.set_array(z.ravel()) |
|
273 | 290 | |
|
274 | 291 | def addpcolor(ax, x, y, z, zmin, zmax, xlabel='', ylabel='', title=''): |
|
275 | 292 | |
|
276 | 293 | # xdateList = map(datetime.datetime.fromtimestamp, x) |
|
277 | 294 | # xdate = matplotlib.dates.date2num(xdateList) |
|
278 | 295 | |
|
279 | 296 | printLabels(ax, xlabel, ylabel, title) |
|
280 | 297 | |
|
281 | 298 | imesh = ax.pcolormesh(x,y,z.T,vmin=zmin,vmax=zmax) |
|
282 | 299 | |
|
283 | 300 | def draw(fig): |
|
284 | 301 | |
|
285 | 302 | if type(fig) == 'int': |
|
286 | 303 | raise ValueError, "This parameter should be of tpye matplotlib figure" |
|
287 | 304 | |
|
288 | 305 | fig.canvas.draw() No newline at end of file |
@@ -1,410 +1,421 | |||
|
1 | 1 | import numpy |
|
2 | 2 | import time, datetime |
|
3 | 3 | from graphics.figure import * |
|
4 | 4 | |
|
5 | 5 | class RTIPlot(Figure): |
|
6 | 6 | |
|
7 | 7 | __isConfig = None |
|
8 | 8 | __nsubplots = None |
|
9 | 9 | |
|
10 | 10 | WIDTHPROF = None |
|
11 | 11 | HEIGHTPROF = None |
|
12 | PREFIX = 'rti' | |
|
12 | 13 | |
|
13 | 14 | def __init__(self): |
|
14 | 15 | |
|
15 | 16 | self.__timerange = 24*60*60 |
|
16 | 17 | self.__isConfig = False |
|
17 | 18 | self.__nsubplots = 1 |
|
18 | 19 | |
|
19 | 20 | self.WIDTH = 800 |
|
20 |
self.HEIGHT = |
|
|
21 | self.HEIGHT = 200 | |
|
21 | 22 | self.WIDTHPROF = 120 |
|
22 | 23 | self.HEIGHTPROF = 0 |
|
23 | 24 | |
|
24 | 25 | def getSubplots(self): |
|
25 | 26 | |
|
26 | 27 | ncol = 1 |
|
27 | 28 | nrow = self.nplots |
|
28 | 29 | |
|
29 | 30 | return nrow, ncol |
|
30 | 31 | |
|
31 | 32 | def setup(self, idfigure, nplots, wintitle, showprofile=True): |
|
32 | 33 | |
|
33 | 34 | self.__showprofile = showprofile |
|
34 | 35 | self.nplots = nplots |
|
35 | 36 | |
|
36 | 37 | ncolspan = 1 |
|
37 | 38 | colspan = 1 |
|
38 | 39 | if showprofile: |
|
39 | 40 | ncolspan = 7 |
|
40 | 41 | colspan = 6 |
|
41 | 42 | self.__nsubplots = 2 |
|
42 | 43 | |
|
43 | 44 | self.createFigure(idfigure = idfigure, |
|
44 | 45 | wintitle = wintitle, |
|
45 | 46 | widthplot = self.WIDTH + self.WIDTHPROF, |
|
46 | 47 | heightplot = self.HEIGHT + self.HEIGHTPROF) |
|
47 | 48 | |
|
48 | 49 | nrow, ncol = self.getSubplots() |
|
49 | 50 | |
|
50 | 51 | counter = 0 |
|
51 | 52 | for y in range(nrow): |
|
52 | 53 | for x in range(ncol): |
|
53 | 54 | |
|
54 | 55 | if counter >= self.nplots: |
|
55 | 56 | break |
|
56 | 57 | |
|
57 | 58 | self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1) |
|
58 | 59 | |
|
59 | 60 | if showprofile: |
|
60 | 61 | self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1) |
|
61 | 62 | |
|
62 | 63 | counter += 1 |
|
63 | 64 | |
|
64 | 65 | def __getTimeLim(self, x, xmin, xmax): |
|
65 | 66 | |
|
66 | 67 | thisdatetime = datetime.datetime.fromtimestamp(numpy.min(x)) |
|
67 | 68 | thisdate = datetime.datetime.combine(thisdatetime.date(), datetime.time(0,0,0)) |
|
68 | 69 | |
|
69 | 70 | #################################################### |
|
70 | 71 | #If the x is out of xrange |
|
71 | 72 | if xmax < (thisdatetime - thisdate).seconds/(60*60.): |
|
72 | 73 | xmin = None |
|
73 | 74 | xmax = None |
|
74 | 75 | |
|
75 | 76 | if xmin == None: |
|
76 | 77 | td = thisdatetime - thisdate |
|
77 | 78 | xmin = td.seconds/(60*60.) |
|
78 | 79 | |
|
79 | 80 | if xmax == None: |
|
80 | 81 | xmax = xmin + self.__timerange/(60*60.) |
|
81 | 82 | |
|
82 | 83 | mindt = thisdate + datetime.timedelta(0,0,0,0,0, xmin) |
|
83 | 84 | tmin = time.mktime(mindt.timetuple()) |
|
84 | 85 | |
|
85 | 86 | maxdt = thisdate + datetime.timedelta(0,0,0,0,0, xmax) |
|
86 | 87 | tmax = time.mktime(maxdt.timetuple()) |
|
87 | 88 | |
|
88 | 89 | self.__timerange = tmax - tmin |
|
89 | 90 | |
|
90 | 91 | return tmin, tmax |
|
91 | 92 | |
|
92 | 93 | def run(self, dataOut, idfigure, wintitle="", channelList=None, showprofile='True', |
|
93 | 94 | xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None, |
|
94 | 95 | timerange=None, |
|
95 |
save=False, file |
|
|
96 | save=False, figpath='./', figfile=None): | |
|
96 | 97 | |
|
97 | 98 | """ |
|
98 | 99 | |
|
99 | 100 | Input: |
|
100 | 101 | dataOut : |
|
101 | 102 | idfigure : |
|
102 | 103 | wintitle : |
|
103 | 104 | channelList : |
|
104 | 105 | showProfile : |
|
105 | 106 | xmin : None, |
|
106 | 107 | xmax : None, |
|
107 | 108 | ymin : None, |
|
108 | 109 | ymax : None, |
|
109 | 110 | zmin : None, |
|
110 | 111 | zmax : None |
|
111 | 112 | """ |
|
112 | 113 | |
|
113 | 114 | if channelList == None: |
|
114 | 115 | channelIndexList = dataOut.channelIndexList |
|
115 | 116 | else: |
|
116 | 117 | channelIndexList = [] |
|
117 | 118 | for channel in channelList: |
|
118 | 119 | if channel not in dataOut.channelList: |
|
119 | 120 | raise ValueError, "Channel %d is not in dataOut.channelList" |
|
120 | 121 | channelIndexList.append(channel) |
|
121 | 122 | |
|
122 | 123 | if timerange != None: |
|
123 | 124 | self.__timerange = timerange |
|
124 | 125 | |
|
125 | 126 | tmin = None |
|
126 | 127 | tmax = None |
|
127 | 128 | x = dataOut.getDatatime() |
|
128 | 129 | y = dataOut.getHeiRange() |
|
129 | 130 | z = 10.*numpy.log10(dataOut.data_spc[channelIndexList,:,:]) |
|
130 | 131 | avg = numpy.average(z, axis=1) |
|
131 | 132 | |
|
132 | 133 | noise = dataOut.getNoise() |
|
133 | 134 | |
|
134 | 135 | if not self.__isConfig: |
|
135 | 136 | |
|
136 | 137 | nplots = len(channelIndexList) |
|
137 | 138 | |
|
138 | 139 | self.setup(idfigure=idfigure, |
|
139 | 140 | nplots=nplots, |
|
140 | 141 | wintitle=wintitle, |
|
141 | 142 | showprofile=showprofile) |
|
142 | 143 | |
|
143 | 144 | tmin, tmax = self.__getTimeLim(x, xmin, xmax) |
|
144 | 145 | if ymin == None: ymin = numpy.nanmin(y) |
|
145 | 146 | if ymax == None: ymax = numpy.nanmax(y) |
|
146 | 147 | if zmin == None: zmin = numpy.nanmin(avg)*0.9 |
|
147 | 148 | if zmax == None: zmax = numpy.nanmax(avg)*0.9 |
|
148 | 149 | |
|
149 | 150 | self.__isConfig = True |
|
150 | 151 | |
|
151 | 152 | thisDatetime = datetime.datetime.fromtimestamp(dataOut.utctime) |
|
152 | 153 | title = "RTI: %s" %(thisDatetime.strftime("%d-%b-%Y")) |
|
153 | 154 | xlabel = "Velocity (m/s)" |
|
154 | 155 | ylabel = "Range (Km)" |
|
155 | 156 | |
|
156 | 157 | self.setWinTitle(title) |
|
157 | 158 | |
|
158 | 159 | for i in range(self.nplots): |
|
159 | 160 | title = "Channel %d: %s" %(dataOut.channelList[i], thisDatetime.strftime("%d-%b-%Y %H:%M:%S")) |
|
160 | 161 | axes = self.axesList[i*self.__nsubplots] |
|
161 | 162 | z = avg[i].reshape((1,-1)) |
|
162 | 163 | axes.pcolor(x, y, z, |
|
163 | 164 | xmin=tmin, xmax=tmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax, |
|
164 | 165 | xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True, |
|
165 | 166 | ticksize=9, cblabel='', cbsize="1%") |
|
166 | 167 | |
|
167 | 168 | if self.__showprofile: |
|
168 | 169 | axes = self.axesList[i*self.__nsubplots +1] |
|
169 | 170 | axes.pline(avg[i], y, |
|
170 | 171 | xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax, |
|
171 | 172 | xlabel='dB', ylabel='', title='', |
|
172 | 173 | ytick_visible=False, |
|
173 | 174 | grid='x') |
|
174 | 175 | |
|
175 | 176 | self.draw() |
|
176 | 177 | |
|
177 | 178 | if save: |
|
178 | self.saveFigure(filename) | |
|
179 | date = thisDatetime.strftime("%Y%m%d") | |
|
180 | if figfile == None: | |
|
181 | figfile = self.getFilename(name = date) | |
|
182 | ||
|
183 | self.saveFigure(figpath, figfile) | |
|
179 | 184 | |
|
180 | 185 | if x[1] + (x[1]-x[0]) >= self.axesList[0].xmax: |
|
181 | 186 | self.__isConfig = False |
|
182 | 187 | |
|
183 | 188 | class SpectraPlot(Figure): |
|
184 | 189 | |
|
185 | 190 | __isConfig = None |
|
186 | 191 | __nsubplots = None |
|
187 | 192 | |
|
188 | 193 | WIDTHPROF = None |
|
189 | 194 | HEIGHTPROF = None |
|
195 | PREFIX = 'spc' | |
|
190 | 196 | |
|
191 | 197 | def __init__(self): |
|
192 | 198 | |
|
193 | 199 | self.__isConfig = False |
|
194 | 200 | self.__nsubplots = 1 |
|
195 | 201 | |
|
196 | 202 | self.WIDTH = 300 |
|
197 | 203 | self.HEIGHT = 400 |
|
198 | 204 | self.WIDTHPROF = 120 |
|
199 | 205 | self.HEIGHTPROF = 0 |
|
200 | 206 | |
|
201 | 207 | def getSubplots(self): |
|
202 | 208 | |
|
203 | 209 | ncol = int(numpy.sqrt(self.nplots)+0.9) |
|
204 | 210 | nrow = int(self.nplots*1./ncol + 0.9) |
|
205 | 211 | |
|
206 | 212 | return nrow, ncol |
|
207 | 213 | |
|
208 | 214 | def setup(self, idfigure, nplots, wintitle, showprofile=True): |
|
209 | 215 | |
|
210 | 216 | self.__showprofile = showprofile |
|
211 | 217 | self.nplots = nplots |
|
212 | 218 | |
|
213 | 219 | ncolspan = 1 |
|
214 | 220 | colspan = 1 |
|
215 | 221 | if showprofile: |
|
216 | 222 | ncolspan = 3 |
|
217 | 223 | colspan = 2 |
|
218 | 224 | self.__nsubplots = 2 |
|
219 | 225 | |
|
220 | 226 | self.createFigure(idfigure = idfigure, |
|
221 | 227 | wintitle = wintitle, |
|
222 | 228 | widthplot = self.WIDTH + self.WIDTHPROF, |
|
223 | 229 | heightplot = self.HEIGHT + self.HEIGHTPROF) |
|
224 | 230 | |
|
225 | 231 | nrow, ncol = self.getSubplots() |
|
226 | 232 | |
|
227 | 233 | counter = 0 |
|
228 | 234 | for y in range(nrow): |
|
229 | 235 | for x in range(ncol): |
|
230 | 236 | |
|
231 | 237 | if counter >= self.nplots: |
|
232 | 238 | break |
|
233 | 239 | |
|
234 | 240 | self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1) |
|
235 | 241 | |
|
236 | 242 | if showprofile: |
|
237 | 243 | self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1) |
|
238 | 244 | |
|
239 | 245 | counter += 1 |
|
240 | 246 | |
|
241 | 247 | def run(self, dataOut, idfigure, wintitle="", channelList=None, showprofile='True', |
|
242 |
xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None, |
|
|
248 | xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None, | |
|
249 | save=False, figpath='./', figfile=None): | |
|
243 | 250 | |
|
244 | 251 | """ |
|
245 | 252 | |
|
246 | 253 | Input: |
|
247 | 254 | dataOut : |
|
248 | 255 | idfigure : |
|
249 | 256 | wintitle : |
|
250 | 257 | channelList : |
|
251 | 258 | showProfile : |
|
252 | 259 | xmin : None, |
|
253 | 260 | xmax : None, |
|
254 | 261 | ymin : None, |
|
255 | 262 | ymax : None, |
|
256 | 263 | zmin : None, |
|
257 | 264 | zmax : None |
|
258 | 265 | """ |
|
259 | 266 | |
|
260 | 267 | if channelList == None: |
|
261 | 268 | channelIndexList = dataOut.channelIndexList |
|
262 | 269 | else: |
|
263 | 270 | channelIndexList = [] |
|
264 | 271 | for channel in channelList: |
|
265 | 272 | if channel not in dataOut.channelList: |
|
266 | 273 | raise ValueError, "Channel %d is not in dataOut.channelList" |
|
267 | 274 | channelIndexList.append(channel) |
|
268 | 275 | |
|
269 | 276 | x = dataOut.getVelRange(1) |
|
270 | 277 | y = dataOut.getHeiRange() |
|
271 | 278 | z = 10.*numpy.log10(dataOut.data_spc[channelIndexList,:,:]) |
|
272 | 279 | avg = numpy.average(z, axis=1) |
|
273 | 280 | |
|
274 | 281 | noise = dataOut.getNoise() |
|
275 | 282 | |
|
276 | 283 | if not self.__isConfig: |
|
277 | 284 | |
|
278 | 285 | nplots = len(channelIndexList) |
|
279 | 286 | |
|
280 | 287 | self.setup(idfigure=idfigure, |
|
281 | 288 | nplots=nplots, |
|
282 | 289 | wintitle=wintitle, |
|
283 | 290 | showprofile=showprofile) |
|
284 | 291 | |
|
285 | 292 | if xmin == None: xmin = numpy.nanmin(x) |
|
286 | 293 | if xmax == None: xmax = numpy.nanmax(x) |
|
287 | 294 | if ymin == None: ymin = numpy.nanmin(y) |
|
288 | 295 | if ymax == None: ymax = numpy.nanmax(y) |
|
289 | 296 | if zmin == None: zmin = numpy.nanmin(avg)*0.9 |
|
290 | 297 | if zmax == None: zmax = numpy.nanmax(avg)*0.9 |
|
291 | 298 | |
|
292 | 299 | self.__isConfig = True |
|
293 | 300 | |
|
294 | 301 | thisDatetime = datetime.datetime.fromtimestamp(dataOut.utctime) |
|
295 | 302 | title = "Spectra: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S")) |
|
296 | 303 | xlabel = "Velocity (m/s)" |
|
297 | 304 | ylabel = "Range (Km)" |
|
298 | 305 | |
|
299 | 306 | self.setWinTitle(title) |
|
300 | 307 | |
|
301 | 308 | for i in range(self.nplots): |
|
302 | 309 | title = "Channel %d: %4.2fdB" %(dataOut.channelList[i], noise[i]) |
|
303 | 310 | axes = self.axesList[i*self.__nsubplots] |
|
304 | 311 | axes.pcolor(x, y, z[i,:,:], |
|
305 | 312 | xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax, |
|
306 | 313 | xlabel=xlabel, ylabel=ylabel, title=title, |
|
307 | 314 | ticksize=9, cblabel='') |
|
308 | 315 | |
|
309 | 316 | if self.__showprofile: |
|
310 | 317 | axes = self.axesList[i*self.__nsubplots +1] |
|
311 | 318 | axes.pline(avg[i], y, |
|
312 | 319 | xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax, |
|
313 | 320 | xlabel='dB', ylabel='', title='', |
|
314 | 321 | ytick_visible=False, |
|
315 | 322 | grid='x') |
|
316 | 323 | |
|
317 | 324 | self.draw() |
|
318 | 325 | |
|
319 | 326 | if save: |
|
320 | self.saveFigure(filename) | |
|
327 | date = thisDatetime.strftime("%Y%m%d") | |
|
328 | if figfile == None: | |
|
329 | figfile = self.getFilename(name = date) | |
|
330 | ||
|
331 | self.saveFigure(figpath, figfile) | |
|
321 | 332 | |
|
322 | 333 | class Scope(Figure): |
|
323 | 334 | |
|
324 | 335 | __isConfig = None |
|
325 | 336 | |
|
326 | 337 | def __init__(self): |
|
327 | 338 | |
|
328 | 339 | self.__isConfig = False |
|
329 | 340 | self.WIDTH = 600 |
|
330 | 341 | self.HEIGHT = 200 |
|
331 | 342 | |
|
332 | 343 | def getSubplots(self): |
|
333 | 344 | |
|
334 | 345 | nrow = self.nplots |
|
335 | 346 | ncol = 3 |
|
336 | 347 | return nrow, ncol |
|
337 | 348 | |
|
338 | 349 | def setup(self, idfigure, nplots, wintitle): |
|
339 | 350 | |
|
340 | 351 | self.createFigure(idfigure, wintitle) |
|
341 | 352 | |
|
342 | 353 | nrow,ncol = self.getSubplots() |
|
343 | 354 | colspan = 3 |
|
344 | 355 | rowspan = 1 |
|
345 | 356 | |
|
346 | 357 | for i in range(nplots): |
|
347 | 358 | self.addAxes(nrow, ncol, i, 0, colspan, rowspan) |
|
348 | 359 | |
|
349 | 360 | self.nplots = nplots |
|
350 | 361 | |
|
351 | 362 | def run(self, dataOut, idfigure, wintitle="", channelList=None, |
|
352 | 363 | xmin=None, xmax=None, ymin=None, ymax=None, save=False, filename=None): |
|
353 | 364 | |
|
354 | 365 | """ |
|
355 | 366 | |
|
356 | 367 | Input: |
|
357 | 368 | dataOut : |
|
358 | 369 | idfigure : |
|
359 | 370 | wintitle : |
|
360 | 371 | channelList : |
|
361 | 372 | xmin : None, |
|
362 | 373 | xmax : None, |
|
363 | 374 | ymin : None, |
|
364 | 375 | ymax : None, |
|
365 | 376 | """ |
|
366 | 377 | |
|
367 | 378 | if channelList == None: |
|
368 | 379 | channelList = dataOut.channelList |
|
369 | 380 | |
|
370 | 381 | x = dataOut.heightList |
|
371 | 382 | y = dataOut.data[channelList,:] * numpy.conjugate(dataOut.data[channelList,:]) |
|
372 | 383 | y = y.real |
|
373 | 384 | |
|
374 | 385 | noise = dataOut.getNoise() |
|
375 | 386 | |
|
376 | 387 | if not self.__isConfig: |
|
377 | 388 | nplots = len(channelList) |
|
378 | 389 | |
|
379 | 390 | self.setup(idfigure=idfigure, |
|
380 | 391 | nplots=nplots, |
|
381 | 392 | wintitle=wintitle) |
|
382 | 393 | |
|
383 | 394 | if xmin == None: xmin = numpy.nanmin(x) |
|
384 | 395 | if xmax == None: xmax = numpy.nanmax(x) |
|
385 | 396 | if ymin == None: ymin = numpy.nanmin(y) |
|
386 | 397 | if ymax == None: ymax = numpy.nanmax(y) |
|
387 | 398 | |
|
388 | 399 | self.__isConfig = True |
|
389 | 400 | |
|
390 | 401 | |
|
391 | 402 | thisDatetime = datetime.datetime.fromtimestamp(dataOut.utctime) |
|
392 | 403 | title = "Scope: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S")) |
|
393 | 404 | xlabel = "Range (Km)" |
|
394 | 405 | ylabel = "Intensity" |
|
395 | 406 | |
|
396 | 407 | self.setWinTitle(title) |
|
397 | 408 | |
|
398 | 409 | for i in range(len(self.axesList)): |
|
399 | 410 | title = "Channel %d: %4.2fdB" %(i, noise[i]) |
|
400 | 411 | axes = self.axesList[i] |
|
401 | 412 | ychannel = y[i,:] |
|
402 | 413 | axes.pline(x, ychannel, |
|
403 | 414 | xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, |
|
404 | 415 | xlabel=xlabel, ylabel=ylabel, title=title) |
|
405 | 416 | |
|
406 | 417 | self.draw() |
|
407 | 418 | |
|
408 | 419 | if save: |
|
409 | 420 | self.saveFigure(filename) |
|
410 | 421 | No newline at end of file |
@@ -1,836 +1,903 | |||
|
1 | 1 | ''' |
|
2 | 2 | |
|
3 | 3 | $Author: dsuarez $ |
|
4 | 4 | $Id: Processor.py 1 2012-11-12 18:56:07Z dsuarez $ |
|
5 | 5 | ''' |
|
6 | 6 | import os |
|
7 | 7 | import numpy |
|
8 | 8 | import datetime |
|
9 | 9 | import time |
|
10 | 10 | |
|
11 | 11 | from jrodata import * |
|
12 | 12 | from jrodataIO import * |
|
13 | 13 | from jroplot import * |
|
14 | 14 | |
|
15 | 15 | class ProcessingUnit: |
|
16 | 16 | |
|
17 | 17 | """ |
|
18 | 18 | Esta es la clase base para el procesamiento de datos. |
|
19 | 19 | |
|
20 | 20 | Contiene el metodo "call" para llamar operaciones. Las operaciones pueden ser: |
|
21 | 21 | - Metodos internos (callMethod) |
|
22 | 22 | - Objetos del tipo Operation (callObject). Antes de ser llamados, estos objetos |
|
23 | 23 | tienen que ser agreagados con el metodo "add". |
|
24 | 24 | |
|
25 | 25 | """ |
|
26 | 26 | # objeto de datos de entrada (Voltage, Spectra o Correlation) |
|
27 | 27 | dataIn = None |
|
28 | 28 | |
|
29 | 29 | # objeto de datos de entrada (Voltage, Spectra o Correlation) |
|
30 | 30 | dataOut = None |
|
31 | 31 | |
|
32 | 32 | |
|
33 | 33 | objectDict = None |
|
34 | 34 | |
|
35 | 35 | def __init__(self): |
|
36 | 36 | |
|
37 | 37 | self.objectDict = {} |
|
38 | 38 | |
|
39 | 39 | def init(self): |
|
40 | 40 | |
|
41 | 41 | raise ValueError, "Not implemented" |
|
42 | 42 | |
|
43 | 43 | def addOperation(self, object, objId): |
|
44 | 44 | |
|
45 | 45 | """ |
|
46 | 46 | Agrega el objeto "object" a la lista de objetos "self.objectList" y retorna el |
|
47 | 47 | identificador asociado a este objeto. |
|
48 | 48 | |
|
49 | 49 | Input: |
|
50 | 50 | |
|
51 | 51 | object : objeto de la clase "Operation" |
|
52 | 52 | |
|
53 | 53 | Return: |
|
54 | 54 | |
|
55 | 55 | objId : identificador del objeto, necesario para ejecutar la operacion |
|
56 | 56 | """ |
|
57 | 57 | |
|
58 | 58 | self.objectDict[objId] = object |
|
59 | 59 | |
|
60 | 60 | return objId |
|
61 | 61 | |
|
62 | 62 | def operation(self, **kwargs): |
|
63 | 63 | |
|
64 | 64 | """ |
|
65 | 65 | Operacion directa sobre la data (dataout.data). Es necesario actualizar los valores de los |
|
66 | 66 | atributos del objeto dataOut |
|
67 | 67 | |
|
68 | 68 | Input: |
|
69 | 69 | |
|
70 | 70 | **kwargs : Diccionario de argumentos de la funcion a ejecutar |
|
71 | 71 | """ |
|
72 | 72 | |
|
73 | 73 | raise ValueError, "ImplementedError" |
|
74 | 74 | |
|
75 | 75 | def callMethod(self, name, **kwargs): |
|
76 | 76 | |
|
77 | 77 | """ |
|
78 | 78 | Ejecuta el metodo con el nombre "name" y con argumentos **kwargs de la propia clase. |
|
79 | 79 | |
|
80 | 80 | Input: |
|
81 | 81 | name : nombre del metodo a ejecutar |
|
82 | 82 | |
|
83 | 83 | **kwargs : diccionario con los nombres y valores de la funcion a ejecutar. |
|
84 | 84 | |
|
85 | 85 | """ |
|
86 | 86 | if name != 'run': |
|
87 | 87 | |
|
88 | 88 | if name == 'init' and self.dataIn.isEmpty(): |
|
89 | 89 | self.dataOut.flagNoData = True |
|
90 | 90 | return False |
|
91 | 91 | |
|
92 | 92 | if name != 'init' and self.dataOut.isEmpty(): |
|
93 | 93 | return False |
|
94 | 94 | |
|
95 | 95 | methodToCall = getattr(self, name) |
|
96 | 96 | |
|
97 | 97 | methodToCall(**kwargs) |
|
98 | 98 | |
|
99 | 99 | if name != 'run': |
|
100 | 100 | return True |
|
101 | 101 | |
|
102 | 102 | if self.dataOut.isEmpty(): |
|
103 | 103 | return False |
|
104 | 104 | |
|
105 | 105 | return True |
|
106 | 106 | |
|
107 | 107 | def callObject(self, objId, **kwargs): |
|
108 | 108 | |
|
109 | 109 | """ |
|
110 | 110 | Ejecuta la operacion asociada al identificador del objeto "objId" |
|
111 | 111 | |
|
112 | 112 | Input: |
|
113 | 113 | |
|
114 | 114 | objId : identificador del objeto a ejecutar |
|
115 | 115 | |
|
116 | 116 | **kwargs : diccionario con los nombres y valores de la funcion a ejecutar. |
|
117 | 117 | |
|
118 | 118 | Return: |
|
119 | 119 | |
|
120 | 120 | None |
|
121 | 121 | """ |
|
122 | 122 | |
|
123 | 123 | if self.dataOut.isEmpty(): |
|
124 | 124 | return False |
|
125 | 125 | |
|
126 | 126 | object = self.objectDict[objId] |
|
127 | 127 | |
|
128 | 128 | object.run(self.dataOut, **kwargs) |
|
129 | 129 | |
|
130 | 130 | return True |
|
131 | 131 | |
|
132 | 132 | def call(self, operationConf, **kwargs): |
|
133 | 133 | |
|
134 | 134 | """ |
|
135 | 135 | Return True si ejecuta la operacion "operationConf.name" con los |
|
136 | 136 | argumentos "**kwargs". False si la operacion no se ha ejecutado. |
|
137 | 137 | La operacion puede ser de dos tipos: |
|
138 | 138 | |
|
139 | 139 | 1. Un metodo propio de esta clase: |
|
140 | 140 | |
|
141 | 141 | operation.type = "self" |
|
142 | 142 | |
|
143 | 143 | 2. El metodo "run" de un objeto del tipo Operation o de un derivado de ella: |
|
144 | 144 | operation.type = "other". |
|
145 | 145 | |
|
146 | 146 | Este objeto de tipo Operation debe de haber sido agregado antes con el metodo: |
|
147 | 147 | "addOperation" e identificado con el operation.id |
|
148 | 148 | |
|
149 | 149 | |
|
150 | 150 | con el id de la operacion. |
|
151 | 151 | |
|
152 | 152 | Input: |
|
153 | 153 | |
|
154 | 154 | Operation : Objeto del tipo operacion con los atributos: name, type y id. |
|
155 | 155 | |
|
156 | 156 | """ |
|
157 | 157 | |
|
158 | 158 | if operationConf.type == 'self': |
|
159 | 159 | sts = self.callMethod(operationConf.name, **kwargs) |
|
160 | 160 | |
|
161 | 161 | if operationConf.type == 'other': |
|
162 | 162 | sts = self.callObject(operationConf.id, **kwargs) |
|
163 | 163 | |
|
164 | 164 | return sts |
|
165 | 165 | |
|
166 | 166 | def setInput(self, dataIn): |
|
167 | 167 | |
|
168 | 168 | self.dataIn = dataIn |
|
169 | 169 | |
|
170 | 170 | def getOutput(self): |
|
171 | 171 | |
|
172 | 172 | return self.dataOut |
|
173 | 173 | |
|
174 | 174 | class Operation(): |
|
175 | 175 | |
|
176 | 176 | """ |
|
177 | 177 | Clase base para definir las operaciones adicionales que se pueden agregar a la clase ProcessingUnit |
|
178 | 178 | y necesiten acumular informacion previa de los datos a procesar. De preferencia usar un buffer de |
|
179 | 179 | acumulacion dentro de esta clase |
|
180 | 180 | |
|
181 | 181 | Ejemplo: Integraciones coherentes, necesita la informacion previa de los n perfiles anteriores (bufffer) |
|
182 | 182 | |
|
183 | 183 | """ |
|
184 | 184 | |
|
185 | 185 | __buffer = None |
|
186 | 186 | __isConfig = False |
|
187 | 187 | |
|
188 | 188 | def __init__(self): |
|
189 | 189 | |
|
190 | 190 | pass |
|
191 | 191 | |
|
192 | 192 | def run(self, dataIn, **kwargs): |
|
193 | 193 | |
|
194 | 194 | """ |
|
195 | 195 | Realiza las operaciones necesarias sobre la dataIn.data y actualiza los atributos del objeto dataIn. |
|
196 | 196 | |
|
197 | 197 | Input: |
|
198 | 198 | |
|
199 | 199 | dataIn : objeto del tipo JROData |
|
200 | 200 | |
|
201 | 201 | Return: |
|
202 | 202 | |
|
203 | 203 | None |
|
204 | 204 | |
|
205 | 205 | Affected: |
|
206 | 206 | __buffer : buffer de recepcion de datos. |
|
207 | 207 | |
|
208 | 208 | """ |
|
209 | 209 | |
|
210 | 210 | raise ValueError, "ImplementedError" |
|
211 | 211 | |
|
212 | 212 | class VoltageProc(ProcessingUnit): |
|
213 | 213 | |
|
214 | 214 | |
|
215 | 215 | def __init__(self): |
|
216 | 216 | |
|
217 | 217 | self.objectDict = {} |
|
218 | 218 | self.dataOut = Voltage() |
|
219 | 219 | |
|
220 | 220 | def init(self): |
|
221 | 221 | |
|
222 | 222 | self.dataOut.copy(self.dataIn) |
|
223 | 223 | # No necesita copiar en cada init() los atributos de dataIn |
|
224 | 224 | # la copia deberia hacerse por cada nuevo bloque de datos |
|
225 | 225 | |
|
226 | 226 | def selectChannels(self, channelList): |
|
227 | 227 | |
|
228 | 228 | channelIndexList = [] |
|
229 | 229 | |
|
230 | 230 | for channel in channelList: |
|
231 | 231 | index = self.dataOut.channelList.index(channel) |
|
232 | 232 | channelIndexList.append(index) |
|
233 | 233 | |
|
234 | 234 | self.selectChannelsByIndex(channelIndexList) |
|
235 | 235 | |
|
236 | 236 | def selectChannelsByIndex(self, channelIndexList): |
|
237 | 237 | """ |
|
238 | 238 | Selecciona un bloque de datos en base a canales segun el channelIndexList |
|
239 | 239 | |
|
240 | 240 | Input: |
|
241 | 241 | channelIndexList : lista sencilla de canales a seleccionar por ej. [2,3,7] |
|
242 | 242 | |
|
243 | 243 | Affected: |
|
244 | 244 | self.dataOut.data |
|
245 | 245 | self.dataOut.channelIndexList |
|
246 | 246 | self.dataOut.nChannels |
|
247 | 247 | self.dataOut.m_ProcessingHeader.totalSpectra |
|
248 | 248 | self.dataOut.systemHeaderObj.numChannels |
|
249 | 249 | self.dataOut.m_ProcessingHeader.blockSize |
|
250 | 250 | |
|
251 | 251 | Return: |
|
252 | 252 | None |
|
253 | 253 | """ |
|
254 | 254 | |
|
255 | 255 | for channelIndex in channelIndexList: |
|
256 | 256 | if channelIndex not in self.dataOut.channelIndexList: |
|
257 | 257 | print channelIndexList |
|
258 | 258 | raise ValueError, "The value %d in channelIndexList is not valid" %channelIndex |
|
259 | 259 | |
|
260 | 260 | nChannels = len(channelIndexList) |
|
261 | 261 | |
|
262 | 262 | data = self.dataOut.data[channelIndexList,:] |
|
263 | 263 | |
|
264 | 264 | self.dataOut.data = data |
|
265 | 265 | self.dataOut.channelList = [self.dataOut.channelList[i] for i in channelIndexList] |
|
266 | 266 | # self.dataOut.nChannels = nChannels |
|
267 | 267 | |
|
268 | 268 | return 1 |
|
269 | 269 | |
|
270 | 270 | class CohInt(Operation): |
|
271 | 271 | |
|
272 | 272 | __profIndex = 0 |
|
273 | 273 | __withOverapping = False |
|
274 | 274 | |
|
275 | 275 | __byTime = False |
|
276 | 276 | __initime = None |
|
277 | 277 | __lastdatatime = None |
|
278 | 278 | __integrationtime = None |
|
279 | 279 | |
|
280 | 280 | __buffer = None |
|
281 | 281 | |
|
282 | 282 | __dataReady = False |
|
283 | 283 | |
|
284 | 284 | n = None |
|
285 | 285 | |
|
286 | 286 | |
|
287 | 287 | def __init__(self): |
|
288 | 288 | |
|
289 | 289 | self.__isConfig = False |
|
290 | 290 | |
|
291 | 291 | def setup(self, n=None, timeInterval=None, overlapping=False): |
|
292 | 292 | """ |
|
293 | 293 | Set the parameters of the integration class. |
|
294 | 294 | |
|
295 | 295 | Inputs: |
|
296 | 296 | |
|
297 | 297 | n : Number of coherent integrations |
|
298 | 298 | timeInterval : Time of integration. If the parameter "n" is selected this one does not work |
|
299 | 299 | overlapping : |
|
300 | 300 | |
|
301 | 301 | """ |
|
302 | 302 | |
|
303 | 303 | self.__initime = None |
|
304 | 304 | self.__lastdatatime = 0 |
|
305 | 305 | self.__buffer = None |
|
306 | 306 | self.__dataReady = False |
|
307 | 307 | |
|
308 | 308 | |
|
309 | 309 | if n == None and timeInterval == None: |
|
310 | 310 | raise ValueError, "n or timeInterval should be specified ..." |
|
311 | 311 | |
|
312 | 312 | if n != None: |
|
313 | 313 | self.n = n |
|
314 | 314 | self.__byTime = False |
|
315 | 315 | else: |
|
316 | 316 | self.__integrationtime = timeInterval * 60. #if (type(timeInterval)!=integer) -> change this line |
|
317 | 317 | self.n = 9999 |
|
318 | 318 | self.__byTime = True |
|
319 | 319 | |
|
320 | 320 | if overlapping: |
|
321 | 321 | self.__withOverapping = True |
|
322 | 322 | self.__buffer = None |
|
323 | 323 | else: |
|
324 | 324 | self.__withOverapping = False |
|
325 | 325 | self.__buffer = 0 |
|
326 | 326 | |
|
327 | 327 | self.__profIndex = 0 |
|
328 | 328 | |
|
329 | 329 | def putData(self, data): |
|
330 | 330 | |
|
331 | 331 | """ |
|
332 | 332 | Add a profile to the __buffer and increase in one the __profileIndex |
|
333 | 333 | |
|
334 | 334 | """ |
|
335 | 335 | |
|
336 | 336 | if not self.__withOverapping: |
|
337 | 337 | self.__buffer += data.copy() |
|
338 | 338 | self.__profIndex += 1 |
|
339 | 339 | return |
|
340 | 340 | |
|
341 | 341 | #Overlapping data |
|
342 | 342 | nChannels, nHeis = data.shape |
|
343 | 343 | data = numpy.reshape(data, (1, nChannels, nHeis)) |
|
344 | 344 | |
|
345 | 345 | #If the buffer is empty then it takes the data value |
|
346 | 346 | if self.__buffer == None: |
|
347 | 347 | self.__buffer = data |
|
348 | 348 | self.__profIndex += 1 |
|
349 | 349 | return |
|
350 | 350 | |
|
351 | 351 | #If the buffer length is lower than n then stakcing the data value |
|
352 | 352 | if self.__profIndex < self.n: |
|
353 | 353 | self.__buffer = numpy.vstack((self.__buffer, data)) |
|
354 | 354 | self.__profIndex += 1 |
|
355 | 355 | return |
|
356 | 356 | |
|
357 | 357 | #If the buffer length is equal to n then replacing the last buffer value with the data value |
|
358 | 358 | self.__buffer = numpy.roll(self.__buffer, -1, axis=0) |
|
359 | 359 | self.__buffer[self.n-1] = data |
|
360 | 360 | self.__profIndex = self.n |
|
361 | 361 | return |
|
362 | 362 | |
|
363 | 363 | |
|
364 | 364 | def pushData(self): |
|
365 | 365 | """ |
|
366 | 366 | Return the sum of the last profiles and the profiles used in the sum. |
|
367 | 367 | |
|
368 | 368 | Affected: |
|
369 | 369 | |
|
370 | 370 | self.__profileIndex |
|
371 | 371 | |
|
372 | 372 | """ |
|
373 | 373 | |
|
374 | 374 | if not self.__withOverapping: |
|
375 | 375 | data = self.__buffer |
|
376 | 376 | n = self.__profIndex |
|
377 | 377 | |
|
378 | 378 | self.__buffer = 0 |
|
379 | 379 | self.__profIndex = 0 |
|
380 | 380 | |
|
381 | 381 | return data, n |
|
382 | 382 | |
|
383 | 383 | #Integration with Overlapping |
|
384 | 384 | data = numpy.sum(self.__buffer, axis=0) |
|
385 | 385 | n = self.__profIndex |
|
386 | 386 | |
|
387 | 387 | return data, n |
|
388 | 388 | |
|
389 | 389 | def byProfiles(self, data): |
|
390 | 390 | |
|
391 | 391 | self.__dataReady = False |
|
392 | 392 | avgdata = None |
|
393 | 393 | n = None |
|
394 | 394 | |
|
395 | 395 | self.putData(data) |
|
396 | 396 | |
|
397 | 397 | if self.__profIndex == self.n: |
|
398 | 398 | |
|
399 | 399 | avgdata, n = self.pushData() |
|
400 | 400 | self.__dataReady = True |
|
401 | 401 | |
|
402 | 402 | return avgdata |
|
403 | 403 | |
|
404 | 404 | def byTime(self, data, datatime): |
|
405 | 405 | |
|
406 | 406 | self.__dataReady = False |
|
407 | 407 | avgdata = None |
|
408 | 408 | n = None |
|
409 | 409 | |
|
410 | 410 | self.putData(data) |
|
411 | 411 | |
|
412 | 412 | if (datatime - self.__initime) >= self.__integrationtime: |
|
413 | 413 | avgdata, n = self.pushData() |
|
414 | 414 | self.n = n |
|
415 | 415 | self.__dataReady = True |
|
416 | 416 | |
|
417 | 417 | return avgdata |
|
418 | 418 | |
|
419 | 419 | def integrate(self, data, datatime=None): |
|
420 | 420 | |
|
421 | 421 | if self.__initime == None: |
|
422 | 422 | self.__initime = datatime |
|
423 | 423 | |
|
424 | 424 | if self.__byTime: |
|
425 | 425 | avgdata = self.byTime(data, datatime) |
|
426 | 426 | else: |
|
427 | 427 | avgdata = self.byProfiles(data) |
|
428 | 428 | |
|
429 | 429 | |
|
430 | 430 | self.__lastdatatime = datatime |
|
431 | 431 | |
|
432 | 432 | if avgdata == None: |
|
433 | 433 | return None, None |
|
434 | 434 | |
|
435 | 435 | avgdatatime = self.__initime |
|
436 | 436 | |
|
437 | 437 | deltatime = datatime -self.__lastdatatime |
|
438 | 438 | |
|
439 | 439 | if not self.__withOverapping: |
|
440 | 440 | self.__initime = datatime |
|
441 | 441 | else: |
|
442 | 442 | self.__initime += deltatime |
|
443 | 443 | |
|
444 | 444 | return avgdata, avgdatatime |
|
445 | 445 | |
|
446 | 446 | def run(self, dataOut, n=None, timeInterval=None, overlapping=False): |
|
447 | 447 | |
|
448 | 448 | if not self.__isConfig: |
|
449 | 449 | self.setup(n, timeInterval, overlapping) |
|
450 | 450 | self.__isConfig = True |
|
451 | 451 | |
|
452 | 452 | avgdata, avgdatatime = self.integrate(dataOut.data, dataOut.utctime) |
|
453 | 453 | |
|
454 | 454 | # dataOut.timeInterval *= n |
|
455 | 455 | dataOut.flagNoData = True |
|
456 | 456 | |
|
457 | 457 | if self.__dataReady: |
|
458 | 458 | dataOut.data = avgdata |
|
459 | 459 | dataOut.nCohInt *= self.n |
|
460 | 460 | dataOut.utctime = avgdatatime |
|
461 | 461 | dataOut.timeInterval = dataOut.ippSeconds * dataOut.nCohInt |
|
462 | 462 | dataOut.flagNoData = False |
|
463 | 463 | |
|
464 | 464 | |
|
465 | 465 | class SpectraProc(ProcessingUnit): |
|
466 | 466 | |
|
467 | 467 | def __init__(self): |
|
468 | 468 | |
|
469 | 469 | self.objectDict = {} |
|
470 | 470 | self.buffer = None |
|
471 | 471 | self.firstdatatime = None |
|
472 | 472 | self.profIndex = 0 |
|
473 | 473 | self.dataOut = Spectra() |
|
474 | 474 | |
|
475 | 475 | def __updateObjFromInput(self): |
|
476 | 476 | |
|
477 | 477 | self.dataOut.radarControllerHeaderObj = self.dataIn.radarControllerHeaderObj.copy() |
|
478 | 478 | self.dataOut.systemHeaderObj = self.dataIn.systemHeaderObj.copy() |
|
479 | 479 | self.dataOut.channelList = self.dataIn.channelList |
|
480 | 480 | self.dataOut.heightList = self.dataIn.heightList |
|
481 | 481 | self.dataOut.dtype = self.dataIn.dtype |
|
482 | 482 | # self.dataOut.nHeights = self.dataIn.nHeights |
|
483 | 483 | # self.dataOut.nChannels = self.dataIn.nChannels |
|
484 | 484 | self.dataOut.nBaud = self.dataIn.nBaud |
|
485 | 485 | self.dataOut.nCode = self.dataIn.nCode |
|
486 | 486 | self.dataOut.code = self.dataIn.code |
|
487 | 487 | self.dataOut.nProfiles = self.dataOut.nFFTPoints |
|
488 | 488 | # self.dataOut.channelIndexList = self.dataIn.channelIndexList |
|
489 | 489 | self.dataOut.flagTimeBlock = self.dataIn.flagTimeBlock |
|
490 | 490 | self.dataOut.utctime = self.firstdatatime |
|
491 | 491 | self.dataOut.flagDecodeData = self.dataIn.flagDecodeData #asumo q la data esta decodificada |
|
492 | 492 | self.dataOut.flagDeflipData = self.dataIn.flagDeflipData #asumo q la data esta sin flip |
|
493 | 493 | self.dataOut.flagShiftFFT = self.dataIn.flagShiftFFT |
|
494 | 494 | self.dataOut.nCohInt = self.dataIn.nCohInt |
|
495 | 495 | self.dataOut.nIncohInt = 1 |
|
496 | 496 | self.dataOut.ippSeconds = self.dataIn.ippSeconds |
|
497 | self.dataOut.timeInterval = self.dataIn.timeInterval*self.dataOut.nFFTPoints | |
|
497 | self.dataOut.timeInterval = self.dataIn.timeInterval*self.dataOut.nFFTPoints**self.dataOut.nConInt**self.dataOut.nIncohInt | |
|
498 | 498 | |
|
499 | 499 | def __getFft(self): |
|
500 | 500 | """ |
|
501 | 501 | Convierte valores de Voltaje a Spectra |
|
502 | 502 | |
|
503 | 503 | Affected: |
|
504 | 504 | self.dataOut.data_spc |
|
505 | 505 | self.dataOut.data_cspc |
|
506 | 506 | self.dataOut.data_dc |
|
507 | 507 | self.dataOut.heightList |
|
508 | 508 | self.dataOut.m_BasicHeader |
|
509 | 509 | self.dataOut.m_ProcessingHeader |
|
510 | 510 | self.dataOut.radarControllerHeaderObj |
|
511 | 511 | self.dataOut.systemHeaderObj |
|
512 | 512 | self.profIndex |
|
513 | 513 | self.buffer |
|
514 | 514 | self.dataOut.flagNoData |
|
515 | 515 | self.dataOut.dtype |
|
516 | 516 | self.dataOut.nPairs |
|
517 | 517 | self.dataOut.nChannels |
|
518 | 518 | self.dataOut.nProfiles |
|
519 | 519 | self.dataOut.systemHeaderObj.numChannels |
|
520 | 520 | self.dataOut.m_ProcessingHeader.totalSpectra |
|
521 | 521 | self.dataOut.m_ProcessingHeader.profilesPerBlock |
|
522 | 522 | self.dataOut.m_ProcessingHeader.numHeights |
|
523 | 523 | self.dataOut.m_ProcessingHeader.spectraComb |
|
524 | 524 | self.dataOut.m_ProcessingHeader.shif_fft |
|
525 | 525 | """ |
|
526 | 526 | fft_volt = numpy.fft.fft(self.buffer,axis=1) |
|
527 | 527 | dc = fft_volt[:,0,:] |
|
528 | 528 | |
|
529 | 529 | #calculo de self-spectra |
|
530 | 530 | fft_volt = numpy.fft.fftshift(fft_volt,axes=(1,)) |
|
531 | 531 | spc = fft_volt * numpy.conjugate(fft_volt) |
|
532 | 532 | spc = spc.real |
|
533 | 533 | |
|
534 | 534 | blocksize = 0 |
|
535 | 535 | blocksize += dc.size |
|
536 | 536 | blocksize += spc.size |
|
537 | 537 | |
|
538 | 538 | cspc = None |
|
539 | 539 | pairIndex = 0 |
|
540 | 540 | if self.dataOut.pairsList != None: |
|
541 | 541 | #calculo de cross-spectra |
|
542 | 542 | cspc = numpy.zeros((self.dataOut.nPairs, self.dataOut.nFFTPoints, self.dataOut.nHeights), dtype='complex') |
|
543 | 543 | for pair in self.dataOut.pairsList: |
|
544 | 544 | cspc[pairIndex,:,:] = numpy.abs(fft_volt[pair[0],:,:] * numpy.conjugate(fft_volt[pair[1],:,:])) |
|
545 | 545 | pairIndex += 1 |
|
546 | 546 | blocksize += cspc.size |
|
547 | 547 | |
|
548 | 548 | self.dataOut.data_spc = spc |
|
549 | 549 | self.dataOut.data_cspc = cspc |
|
550 | 550 | self.dataOut.data_dc = dc |
|
551 | 551 | self.dataOut.blockSize = blocksize |
|
552 | 552 | |
|
553 | 553 | def init(self, nFFTPoints=None, pairsList=None): |
|
554 | 554 | |
|
555 | 555 | if self.dataIn.type == "Spectra": |
|
556 | 556 | self.dataOut.copy(self.dataIn) |
|
557 | 557 | return |
|
558 | 558 | |
|
559 | 559 | if self.dataIn.type == "Voltage": |
|
560 | 560 | |
|
561 | 561 | if nFFTPoints == None: |
|
562 | 562 | raise ValueError, "This SpectraProc.setup() need nFFTPoints input variable" |
|
563 | 563 | |
|
564 | 564 | if pairsList == None: |
|
565 | 565 | nPairs = 0 |
|
566 | 566 | else: |
|
567 | 567 | nPairs = len(pairsList) |
|
568 | 568 | |
|
569 | 569 | self.dataOut.nFFTPoints = nFFTPoints |
|
570 | 570 | self.dataOut.pairsList = pairsList |
|
571 | 571 | self.dataOut.nPairs = nPairs |
|
572 | 572 | |
|
573 | 573 | if self.buffer == None: |
|
574 | 574 | self.buffer = numpy.zeros((self.dataIn.nChannels, |
|
575 | 575 | self.dataOut.nFFTPoints, |
|
576 | 576 | self.dataIn.nHeights), |
|
577 | 577 | dtype='complex') |
|
578 | 578 | |
|
579 | 579 | |
|
580 | 580 | self.buffer[:,self.profIndex,:] = self.dataIn.data |
|
581 | 581 | self.profIndex += 1 |
|
582 | 582 | |
|
583 | 583 | if self.firstdatatime == None: |
|
584 | 584 | self.firstdatatime = self.dataIn.utctime |
|
585 | 585 | |
|
586 | 586 | if self.profIndex == self.dataOut.nFFTPoints: |
|
587 | 587 | self.__updateObjFromInput() |
|
588 | 588 | self.__getFft() |
|
589 | 589 | |
|
590 | 590 | self.dataOut.flagNoData = False |
|
591 | 591 | |
|
592 | 592 | self.buffer = None |
|
593 | 593 | self.firstdatatime = None |
|
594 | 594 | self.profIndex = 0 |
|
595 | 595 | |
|
596 | 596 | return |
|
597 | 597 | |
|
598 | 598 | raise ValuError, "The type object %s is not valid"%(self.dataIn.type) |
|
599 | 599 | |
|
600 | 600 | def selectChannels(self, channelList): |
|
601 | 601 | |
|
602 | 602 | channelIndexList = [] |
|
603 | 603 | |
|
604 | 604 | for channel in channelList: |
|
605 | 605 | index = self.dataOut.channelList.index(channel) |
|
606 | 606 | channelIndexList.append(index) |
|
607 | 607 | |
|
608 | 608 | self.selectChannelsByIndex(channelIndexList) |
|
609 | 609 | |
|
610 | 610 | def selectChannelsByIndex(self, channelIndexList): |
|
611 | 611 | """ |
|
612 | 612 | Selecciona un bloque de datos en base a canales segun el channelIndexList |
|
613 | 613 | |
|
614 | 614 | Input: |
|
615 | 615 | channelIndexList : lista sencilla de canales a seleccionar por ej. [2,3,7] |
|
616 | 616 | |
|
617 | 617 | Affected: |
|
618 | 618 | self.dataOut.data_spc |
|
619 | 619 | self.dataOut.channelIndexList |
|
620 | 620 | self.dataOut.nChannels |
|
621 | 621 | |
|
622 | 622 | Return: |
|
623 | 623 | None |
|
624 | 624 | """ |
|
625 | 625 | |
|
626 | 626 | for channelIndex in channelIndexList: |
|
627 | 627 | if channelIndex not in self.dataOut.channelIndexList: |
|
628 | 628 | print channelIndexList |
|
629 | 629 | raise ValueError, "The value %d in channelIndexList is not valid" %channelIndex |
|
630 | 630 | |
|
631 | 631 | nChannels = len(channelIndexList) |
|
632 | 632 | |
|
633 | 633 | data_spc = self.dataOut.data_spc[channelIndexList,:] |
|
634 | 634 | |
|
635 | 635 | self.dataOut.data_spc = data_spc |
|
636 | 636 | self.dataOut.channelList = [self.dataOut.channelList[i] for i in channelIndexList] |
|
637 | 637 | # self.dataOut.nChannels = nChannels |
|
638 | 638 | |
|
639 | 639 | return 1 |
|
640 | 640 | |
|
641 | 641 | |
|
642 | 642 | class IncohInt(Operation): |
|
643 | 643 | |
|
644 | 644 | |
|
645 | 645 | __profIndex = 0 |
|
646 | 646 | __withOverapping = False |
|
647 | 647 | |
|
648 | 648 | __byTime = False |
|
649 | 649 | __initime = None |
|
650 | 650 | __lastdatatime = None |
|
651 | 651 | __integrationtime = None |
|
652 | 652 | |
|
653 | __buffer = None | |
|
653 | __buffer_spc = None | |
|
654 | __buffer_cspc = None | |
|
655 | __buffer_dc = None | |
|
654 | 656 | |
|
655 | 657 | __dataReady = False |
|
656 | 658 | |
|
657 | 659 | n = None |
|
658 | 660 | |
|
659 | 661 | |
|
660 | 662 | def __init__(self): |
|
661 | 663 | |
|
662 | 664 | self.__isConfig = False |
|
663 | 665 | |
|
664 | 666 | def setup(self, n=None, timeInterval=None, overlapping=False): |
|
665 | 667 | """ |
|
666 | 668 | Set the parameters of the integration class. |
|
667 | 669 | |
|
668 | 670 | Inputs: |
|
669 | 671 | |
|
670 | 672 | n : Number of coherent integrations |
|
671 | 673 | timeInterval : Time of integration. If the parameter "n" is selected this one does not work |
|
672 | 674 | overlapping : |
|
673 | 675 | |
|
674 | 676 | """ |
|
675 | 677 | |
|
676 | 678 | self.__initime = None |
|
677 | 679 | self.__lastdatatime = 0 |
|
678 | self.__buffer = None | |
|
680 | self.__buffer_spc = None | |
|
681 | self.__buffer_cspc = None | |
|
682 | self.__buffer_dc = None | |
|
679 | 683 | self.__dataReady = False |
|
680 | 684 | |
|
681 | 685 | |
|
682 | 686 | if n == None and timeInterval == None: |
|
683 | 687 | raise ValueError, "n or timeInterval should be specified ..." |
|
684 | 688 | |
|
685 | 689 | if n != None: |
|
686 | 690 | self.n = n |
|
687 | 691 | self.__byTime = False |
|
688 | 692 | else: |
|
689 | 693 | self.__integrationtime = timeInterval * 60. #if (type(timeInterval)!=integer) -> change this line |
|
690 | 694 | self.n = 9999 |
|
691 | 695 | self.__byTime = True |
|
692 | 696 | |
|
693 | 697 | if overlapping: |
|
694 | 698 | self.__withOverapping = True |
|
695 | self.__buffer = None | |
|
696 | 699 | else: |
|
697 | 700 | self.__withOverapping = False |
|
698 | self.__buffer = 0 | |
|
701 | self.__buffer_spc = 0 | |
|
702 | self.__buffer_cspc = 0 | |
|
703 | self.__buffer_dc = 0 | |
|
699 | 704 | |
|
700 | 705 | self.__profIndex = 0 |
|
701 | 706 | |
|
702 | def putData(self, data): | |
|
707 | def putData(self, data_spc, data_cspc, data_dc): | |
|
703 | 708 | |
|
704 | 709 | """ |
|
705 | Add a profile to the __buffer and increase in one the __profileIndex | |
|
710 | Add a profile to the __buffer_spc and increase in one the __profileIndex | |
|
706 | 711 | |
|
707 | 712 | """ |
|
708 | 713 | |
|
709 | 714 | if not self.__withOverapping: |
|
710 |
self.__buffer += data |
|
|
715 | self.__buffer_spc += data_spc | |
|
716 | ||
|
717 | if data_cspc == None: | |
|
718 | self.__buffer_cspc = None | |
|
719 | else: | |
|
720 | self.__buffer_cspc += data_cspc | |
|
721 | ||
|
722 | if data_dc == None: | |
|
723 | self.__buffer_dc = None | |
|
724 | else: | |
|
725 | self.__buffer_dc += data_dc | |
|
726 | ||
|
711 | 727 | self.__profIndex += 1 |
|
712 | 728 | return |
|
713 | 729 | |
|
714 | 730 | #Overlapping data |
|
715 | nChannels, nFFTPoints, nHeis = data.shape | |
|
716 | data = numpy.reshape(data, (1, nChannels, nFFTPoints, nHeis)) | |
|
731 | nChannels, nFFTPoints, nHeis = data_spc.shape | |
|
732 | data_spc = numpy.reshape(data_spc, (1, nChannels, nFFTPoints, nHeis)) | |
|
733 | data_cspc = numpy.reshape(data_cspc, (1, -1, nFFTPoints, nHeis)) | |
|
734 | data_dc = numpy.reshape(data_dc, (1, -1, nHeis)) | |
|
717 | 735 | |
|
718 | 736 | #If the buffer is empty then it takes the data value |
|
719 | if self.__buffer == None: | |
|
720 | self.__buffer = data | |
|
737 | if self.__buffer_spc == None: | |
|
738 | self.__buffer_spc = data_spc.copy() | |
|
739 | ||
|
740 | if data_cspc == None: | |
|
741 | self.__buffer_cspc = None | |
|
742 | else: | |
|
743 | self.__buffer_cspc += data_cspc.copy() | |
|
744 | ||
|
745 | if data_dc == None: | |
|
746 | self.__buffer_dc = None | |
|
747 | else: | |
|
748 | self.__buffer_dc += data_dc.copy() | |
|
749 | ||
|
721 | 750 | self.__profIndex += 1 |
|
722 | 751 | return |
|
723 | 752 | |
|
724 | 753 | #If the buffer length is lower than n then stakcing the data value |
|
725 | 754 | if self.__profIndex < self.n: |
|
726 | self.__buffer = numpy.vstack((self.__buffer, data)) | |
|
755 | self.__buffer_spc = numpy.vstack((self.__buffer_spc, data_spc)) | |
|
756 | ||
|
757 | if self.__buffer_cspc != None: | |
|
758 | self.__buffer_cspc = numpy.vstack((self.__buffer_cspc, data_cspc)) | |
|
759 | ||
|
760 | if self.__buffer_dc != None: | |
|
761 | self.__buffer_dc = numpy.vstack((self.__buffer_dc, data_dc)) | |
|
762 | ||
|
727 | 763 | self.__profIndex += 1 |
|
728 | 764 | return |
|
729 | 765 | |
|
730 | 766 | #If the buffer length is equal to n then replacing the last buffer value with the data value |
|
731 | self.__buffer = numpy.roll(self.__buffer, -1, axis=0) | |
|
732 | self.__buffer[self.n-1] = data | |
|
767 | self.__buffer_spc = numpy.roll(self.__buffer_spc, -1, axis=0) | |
|
768 | self.__buffer_spc[self.n-1] = data_spc | |
|
769 | ||
|
770 | self.__buffer_cspc = numpy.roll(self.__buffer_cspc, -1, axis=0) | |
|
771 | self.__buffer_cspc[self.n-1] = data_cspc | |
|
772 | ||
|
773 | self.__buffer_dc = numpy.roll(self.__buffer_dc, -1, axis=0) | |
|
774 | self.__buffer_dc[self.n-1] = data_dc | |
|
775 | ||
|
733 | 776 | self.__profIndex = self.n |
|
734 | 777 | return |
|
735 | 778 | |
|
736 | 779 | |
|
737 | 780 | def pushData(self): |
|
738 | 781 | """ |
|
739 | 782 | Return the sum of the last profiles and the profiles used in the sum. |
|
740 | 783 | |
|
741 | 784 | Affected: |
|
742 | 785 | |
|
743 | 786 | self.__profileIndex |
|
744 | 787 | |
|
745 | 788 | """ |
|
789 | data_spc = None | |
|
790 | data_cspc = None | |
|
791 | data_dc = None | |
|
746 | 792 | |
|
747 | 793 | if not self.__withOverapping: |
|
748 | data = self.__buffer | |
|
794 | data_spc = self.__buffer_spc | |
|
795 | data_cspc = self.__buffer_cspc | |
|
796 | data_dc = self.__buffer_dc | |
|
797 | ||
|
749 | 798 | n = self.__profIndex |
|
750 | 799 | |
|
751 | self.__buffer = 0 | |
|
800 | self.__buffer_spc = 0 | |
|
801 | self.__buffer_cspc = 0 | |
|
802 | self.__buffer_dc = 0 | |
|
752 | 803 | self.__profIndex = 0 |
|
753 | 804 | |
|
754 | return data, n | |
|
805 | return data_spc, data_cspc, data_dc, n | |
|
755 | 806 | |
|
756 | 807 | #Integration with Overlapping |
|
757 | data = numpy.sum(self.__buffer, axis=0) | |
|
808 | data_spc = numpy.sum(self.__buffer_spc, axis=0) | |
|
809 | ||
|
810 | if self.__buffer_cspc != None: | |
|
811 | data_cspc = numpy.sum(self.__buffer_cspc, axis=0) | |
|
812 | ||
|
813 | if self.__buffer_dc != None: | |
|
814 | data_dc = numpy.sum(self.__buffer_dc, axis=0) | |
|
815 | ||
|
758 | 816 | n = self.__profIndex |
|
759 | 817 | |
|
760 | return data, n | |
|
818 | return data_spc, data_cspc, data_dc, n | |
|
761 | 819 | |
|
762 |
def byProfiles(self, |
|
|
820 | def byProfiles(self, *args): | |
|
763 | 821 | |
|
764 | 822 | self.__dataReady = False |
|
765 | avgdata = None | |
|
823 | avgdata_spc = None | |
|
824 | avgdata_cspc = None | |
|
825 | avgdata_dc = None | |
|
766 | 826 | n = None |
|
767 | 827 | |
|
768 |
self.putData( |
|
|
828 | self.putData(*args) | |
|
769 | 829 | |
|
770 | 830 | if self.__profIndex == self.n: |
|
771 | 831 | |
|
772 | avgdata, n = self.pushData() | |
|
832 | avgdata_spc, avgdata_cspc, avgdata_dc, n = self.pushData() | |
|
773 | 833 | self.__dataReady = True |
|
774 | 834 | |
|
775 | return avgdata | |
|
835 | return avgdata_spc, avgdata_cspc, avgdata_dc | |
|
776 | 836 | |
|
777 |
def byTime(self, data, |
|
|
837 | def byTime(self, datatime, *args): | |
|
778 | 838 | |
|
779 | 839 | self.__dataReady = False |
|
780 | avgdata = None | |
|
840 | avgdata_spc = None | |
|
841 | avgdata_cspc = None | |
|
842 | avgdata_dc = None | |
|
781 | 843 | n = None |
|
782 | 844 | |
|
783 |
self.putData( |
|
|
845 | self.putData(*args) | |
|
784 | 846 | |
|
785 | 847 | if (datatime - self.__initime) >= self.__integrationtime: |
|
786 | avgdata, n = self.pushData() | |
|
848 | avgdata_spc, avgdata_cspc, avgdata_dc, n = self.pushData() | |
|
787 | 849 | self.n = n |
|
788 | 850 | self.__dataReady = True |
|
789 | 851 | |
|
790 | return avgdata | |
|
852 | return avgdata_spc, avgdata_cspc, avgdata_dc | |
|
791 | 853 | |
|
792 |
def integrate(self, data, |
|
|
854 | def integrate(self, datatime, *args): | |
|
793 | 855 | |
|
794 | 856 | if self.__initime == None: |
|
795 | 857 | self.__initime = datatime |
|
796 | 858 | |
|
797 | 859 | if self.__byTime: |
|
798 |
avgdata = self.byTime(data, |
|
|
860 | avgdata_spc, avgdata_cspc, avgdata_dc = self.byTime(datatime, *args) | |
|
799 | 861 | else: |
|
800 |
avgdata = self.byProfiles( |
|
|
801 | ||
|
862 | avgdata_spc, avgdata_cspc, avgdata_dc = self.byProfiles(*args) | |
|
802 | 863 | |
|
803 | 864 | self.__lastdatatime = datatime |
|
804 | 865 | |
|
805 | if avgdata == None: | |
|
806 | return None, None | |
|
866 | if avgdata_spc == None: | |
|
867 | return None, None, None, None | |
|
807 | 868 | |
|
808 | 869 | avgdatatime = self.__initime |
|
809 | 870 | |
|
810 | 871 | deltatime = datatime -self.__lastdatatime |
|
811 | 872 | |
|
812 | 873 | if not self.__withOverapping: |
|
813 | 874 | self.__initime = datatime |
|
814 | 875 | else: |
|
815 | 876 | self.__initime += deltatime |
|
816 | 877 | |
|
817 |
return avgdata, avgdata |
|
|
878 | return avgdatatime, avgdata_spc, avgdata_cspc, avgdata_dc | |
|
818 | 879 | |
|
819 | 880 | def run(self, dataOut, n=None, timeInterval=None, overlapping=False): |
|
820 | 881 | |
|
821 | 882 | if not self.__isConfig: |
|
822 | 883 | self.setup(n, timeInterval, overlapping) |
|
823 | 884 | self.__isConfig = True |
|
824 | 885 | |
|
825 |
avgdata, avgdata |
|
|
886 | avgdatatime, avgdata_spc, avgdata_cspc, avgdata_dc = self.integrate(dataOut.utctime, | |
|
887 | dataOut.data_spc, | |
|
888 | dataOut.data_cspc, | |
|
889 | dataOut.data_dc) | |
|
826 | 890 | |
|
827 | 891 | # dataOut.timeInterval *= n |
|
828 | 892 | dataOut.flagNoData = True |
|
829 | 893 | |
|
830 | 894 | if self.__dataReady: |
|
831 | dataOut.data_spc = avgdata | |
|
895 | dataOut.data_spc = avgdata_spc | |
|
896 | dataOut.data_cspc = avgdata_cspc | |
|
897 | dataOut.data_dc = avgdata_dc | |
|
898 | ||
|
832 | 899 | dataOut.nIncohInt *= self.n |
|
833 | 900 | dataOut.utctime = avgdatatime |
|
834 | 901 | dataOut.timeInterval = dataOut.ippSeconds * dataOut.nCohInt * dataOut.nIncohInt * dataOut.nFFTPoints |
|
835 | 902 | dataOut.flagNoData = False |
|
836 | 903 | No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now