@@ -1,374 +1,373 | |||
|
1 | 1 | import numpy |
|
2 | 2 | import datetime |
|
3 | 3 | import sys |
|
4 | 4 | import matplotlib |
|
5 | if sys.platform == 'linux': | |
|
6 | matplotlib.use("GTKAgg") | |
|
7 | if sys.platform == 'darwin': | |
|
5 | ||
|
6 | if 'linux' in sys.platform: | |
|
7 | matplotlib.use("TKAgg") | |
|
8 | ||
|
9 | if 'darwin' in sys.platform: | |
|
8 | 10 | matplotlib.use("TKAgg") |
|
11 | ||
|
9 | 12 | import matplotlib.pyplot |
|
10 | import matplotlib.dates | |
|
11 | #import scitools.numpyutils | |
|
12 | from mpl_toolkits.axes_grid1 import make_axes_locatable | |
|
13 | 13 | |
|
14 | from matplotlib.dates import DayLocator, HourLocator, MinuteLocator, SecondLocator, DateFormatter | |
|
15 | from matplotlib.ticker import FuncFormatter | |
|
14 | from mpl_toolkits.axes_grid1 import make_axes_locatable | |
|
16 | 15 | from matplotlib.ticker import * |
|
17 | 16 | |
|
18 | 17 | ########################################### |
|
19 | 18 | #Actualizacion de las funciones del driver |
|
20 | 19 | ########################################### |
|
21 | 20 | |
|
22 | 21 | def createFigure(idfigure, wintitle, width, height, facecolor="w"): |
|
23 | 22 | |
|
24 | 23 | matplotlib.pyplot.ioff() |
|
25 | 24 | fig = matplotlib.pyplot.figure(num=idfigure, facecolor=facecolor) |
|
26 | 25 | fig.canvas.manager.set_window_title(wintitle) |
|
27 | 26 | fig.canvas.manager.resize(width, height) |
|
28 | 27 | matplotlib.pyplot.ion() |
|
29 | 28 | matplotlib.pyplot.show() |
|
30 | 29 | |
|
31 | 30 | return fig |
|
32 | 31 | |
|
33 | 32 | def closeFigure(): |
|
34 | 33 | |
|
35 | 34 | matplotlib.pyplot.ioff() |
|
36 | 35 | matplotlib.pyplot.show() |
|
37 | 36 | |
|
38 | 37 | return |
|
39 | 38 | |
|
40 | 39 | def saveFigure(fig, filename): |
|
41 | 40 | |
|
42 | 41 | matplotlib.pyplot.ioff() |
|
43 | 42 | fig.savefig(filename) |
|
44 | 43 | matplotlib.pyplot.ion() |
|
45 | 44 | |
|
46 | 45 | def setWinTitle(fig, title): |
|
47 | 46 | |
|
48 | 47 | fig.canvas.manager.set_window_title(title) |
|
49 | 48 | |
|
50 | 49 | def setTitle(fig, title): |
|
51 | 50 | |
|
52 | 51 | fig.suptitle(title) |
|
53 | 52 | |
|
54 | 53 | def createAxes(fig, nrow, ncol, xpos, ypos, colspan, rowspan): |
|
55 | 54 | |
|
56 | 55 | matplotlib.pyplot.ioff() |
|
57 | 56 | matplotlib.pyplot.figure(fig.number) |
|
58 | 57 | axes = matplotlib.pyplot.subplot2grid((nrow, ncol), |
|
59 | 58 | (xpos, ypos), |
|
60 | 59 | colspan=colspan, |
|
61 | 60 | rowspan=rowspan) |
|
62 | 61 | |
|
63 | 62 | matplotlib.pyplot.ion() |
|
64 | 63 | return axes |
|
65 | 64 | |
|
66 | 65 | def setAxesText(ax, text): |
|
67 | 66 | |
|
68 | 67 | ax.annotate(text, |
|
69 | 68 | xy = (.1, .99), |
|
70 | 69 | xycoords = 'figure fraction', |
|
71 | 70 | horizontalalignment = 'left', |
|
72 | 71 | verticalalignment = 'top', |
|
73 | 72 | fontsize = 10) |
|
74 | 73 | |
|
75 | 74 | def printLabels(ax, xlabel, ylabel, title): |
|
76 | 75 | |
|
77 | 76 | ax.set_xlabel(xlabel, size=11) |
|
78 | 77 | ax.set_ylabel(ylabel, size=11) |
|
79 | 78 | ax.set_title(title, size=12) |
|
80 | 79 | |
|
81 | 80 | def createPline(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title='', |
|
82 | 81 | ticksize=9, xtick_visible=True, ytick_visible=True, |
|
83 | 82 | nxticks=4, nyticks=10, |
|
84 | 83 | grid=None): |
|
85 | 84 | |
|
86 | 85 | """ |
|
87 | 86 | |
|
88 | 87 | Input: |
|
89 | 88 | grid : None, 'both', 'x', 'y' |
|
90 | 89 | """ |
|
91 | 90 | |
|
92 | 91 | matplotlib.pyplot.ioff() |
|
93 | 92 | |
|
94 | 93 | ax.set_xlim([xmin,xmax]) |
|
95 | 94 | ax.set_ylim([ymin,ymax]) |
|
96 | 95 | |
|
97 | 96 | printLabels(ax, xlabel, ylabel, title) |
|
98 | 97 | |
|
99 | 98 | ###################################################### |
|
100 | 99 | if (xmax-xmin)<=1: |
|
101 | 100 | xtickspos = numpy.linspace(xmin,xmax,nxticks) |
|
102 | 101 | xtickspos = numpy.array([float("%.1f"%i) for i in xtickspos]) |
|
103 | 102 | ax.set_xticks(xtickspos) |
|
104 | 103 | else: |
|
105 | 104 | xtickspos = numpy.arange(nxticks)*int((xmax-xmin)/(nxticks)) + int(xmin) |
|
106 | 105 | ax.set_xticks(xtickspos) |
|
107 | 106 | |
|
108 | 107 | for tick in ax.get_xticklabels(): |
|
109 | 108 | tick.set_visible(xtick_visible) |
|
110 | 109 | |
|
111 | 110 | for tick in ax.xaxis.get_major_ticks(): |
|
112 | 111 | tick.label.set_fontsize(ticksize) |
|
113 | 112 | |
|
114 | 113 | ###################################################### |
|
115 | 114 | for tick in ax.get_yticklabels(): |
|
116 | 115 | tick.set_visible(ytick_visible) |
|
117 | 116 | |
|
118 | 117 | for tick in ax.yaxis.get_major_ticks(): |
|
119 | 118 | tick.label.set_fontsize(ticksize) |
|
120 | 119 | |
|
121 | 120 | ax.plot(x, y) |
|
122 | 121 | iplot = ax.lines[-1] |
|
123 | 122 | |
|
124 | 123 | ###################################################### |
|
125 | 124 | if '0.' in matplotlib.__version__[0:2]: |
|
126 | 125 | print "The matplotlib version has to be updated to 1.1 or newer" |
|
127 | 126 | return iplot |
|
128 | 127 | |
|
129 | 128 | if '1.0.' in matplotlib.__version__[0:4]: |
|
130 | 129 | print "The matplotlib version has to be updated to 1.1 or newer" |
|
131 | 130 | return iplot |
|
132 | 131 | |
|
133 | 132 | if grid != None: |
|
134 | 133 | ax.grid(b=True, which='major', axis=grid) |
|
135 | 134 | |
|
136 | 135 | matplotlib.pyplot.tight_layout() |
|
137 | 136 | |
|
138 | 137 | matplotlib.pyplot.ion() |
|
139 | 138 | |
|
140 | 139 | return iplot |
|
141 | 140 | |
|
142 | 141 | def set_linedata(ax, x, y, idline): |
|
143 | 142 | |
|
144 | 143 | ax.lines[idline].set_data(x,y) |
|
145 | 144 | |
|
146 | 145 | def pline(iplot, x, y, xlabel='', ylabel='', title=''): |
|
147 | 146 | |
|
148 | 147 | ax = iplot.get_axes() |
|
149 | 148 | |
|
150 | 149 | printLabels(ax, xlabel, ylabel, title) |
|
151 | 150 | |
|
152 | 151 | set_linedata(ax, x, y, idline=0) |
|
153 | 152 | |
|
154 | 153 | def addpline(ax, x, y, color, linestyle, lw): |
|
155 | 154 | |
|
156 | 155 | ax.plot(x,y,color=color,linestyle=linestyle,lw=lw) |
|
157 | 156 | |
|
158 | 157 | |
|
159 | 158 | def createPcolor(ax, x, y, z, xmin, xmax, ymin, ymax, zmin, zmax, |
|
160 | 159 | xlabel='', ylabel='', title='', ticksize = 9, |
|
161 | 160 | colormap='jet',cblabel='', cbsize="5%", |
|
162 | 161 | XAxisAsTime=False): |
|
163 | 162 | |
|
164 | 163 | matplotlib.pyplot.ioff() |
|
165 | 164 | |
|
166 | 165 | divider = make_axes_locatable(ax) |
|
167 | 166 | ax_cb = divider.new_horizontal(size=cbsize, pad=0.05) |
|
168 | 167 | fig = ax.get_figure() |
|
169 | 168 | fig.add_axes(ax_cb) |
|
170 | 169 | |
|
171 | 170 | ax.set_xlim([xmin,xmax]) |
|
172 | 171 | ax.set_ylim([ymin,ymax]) |
|
173 | 172 | |
|
174 | 173 | printLabels(ax, xlabel, ylabel, title) |
|
175 | 174 | |
|
176 | 175 | imesh = ax.pcolormesh(x,y,z.T, vmin=zmin, vmax=zmax, cmap=matplotlib.pyplot.get_cmap(colormap)) |
|
177 | 176 | cb = matplotlib.pyplot.colorbar(imesh, cax=ax_cb) |
|
178 | 177 | cb.set_label(cblabel) |
|
179 | 178 | |
|
180 | 179 | # for tl in ax_cb.get_yticklabels(): |
|
181 | 180 | # tl.set_visible(True) |
|
182 | 181 | |
|
183 | 182 | for tick in ax.yaxis.get_major_ticks(): |
|
184 | 183 | tick.label.set_fontsize(ticksize) |
|
185 | 184 | |
|
186 | 185 | for tick in ax.xaxis.get_major_ticks(): |
|
187 | 186 | tick.label.set_fontsize(ticksize) |
|
188 | 187 | |
|
189 | 188 | for tick in cb.ax.get_yticklabels(): |
|
190 | 189 | tick.set_fontsize(ticksize) |
|
191 | 190 | |
|
192 | 191 | ax_cb.yaxis.tick_right() |
|
193 | 192 | |
|
194 | 193 | if '0.' in matplotlib.__version__[0:2]: |
|
195 | 194 | print "The matplotlib version has to be updated to 1.1 or newer" |
|
196 | 195 | return imesh |
|
197 | 196 | |
|
198 | 197 | if '1.0.' in matplotlib.__version__[0:4]: |
|
199 | 198 | print "The matplotlib version has to be updated to 1.1 or newer" |
|
200 | 199 | return imesh |
|
201 | 200 | |
|
202 | 201 | matplotlib.pyplot.tight_layout() |
|
203 | 202 | |
|
204 | 203 | if XAxisAsTime: |
|
205 | 204 | |
|
206 | 205 | func = lambda x, pos: ('%s') %(datetime.datetime.utcfromtimestamp(x).strftime("%H:%M:%S")) |
|
207 | 206 | ax.xaxis.set_major_formatter(FuncFormatter(func)) |
|
208 | 207 | ax.xaxis.set_major_locator(LinearLocator(7)) |
|
209 | 208 | |
|
210 | 209 | matplotlib.pyplot.ion() |
|
211 | 210 | return imesh |
|
212 | 211 | |
|
213 | 212 | def pcolor(imesh, z, xlabel='', ylabel='', title=''): |
|
214 | 213 | |
|
215 | 214 | z = z.T |
|
216 | 215 | |
|
217 | 216 | ax = imesh.get_axes() |
|
218 | 217 | |
|
219 | 218 | printLabels(ax, xlabel, ylabel, title) |
|
220 | 219 | |
|
221 | 220 | imesh.set_array(z.ravel()) |
|
222 | 221 | |
|
223 | 222 | def addpcolor(ax, x, y, z, zmin, zmax, xlabel='', ylabel='', title='', colormap='jet'): |
|
224 | 223 | |
|
225 | 224 | printLabels(ax, xlabel, ylabel, title) |
|
226 | 225 | |
|
227 | 226 | ax.collections.remove(ax.collections[0]) |
|
228 | 227 | |
|
229 | 228 | ax.pcolormesh(x,y,z.T,vmin=zmin,vmax=zmax, cmap=matplotlib.pyplot.get_cmap(colormap)) |
|
230 | 229 | |
|
231 | 230 | def createPmultiline(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title='', legendlabels=None, |
|
232 | 231 | ticksize=9, xtick_visible=True, ytick_visible=True, |
|
233 | 232 | nxticks=4, nyticks=10, |
|
234 | 233 | grid=None): |
|
235 | 234 | |
|
236 | 235 | """ |
|
237 | 236 | |
|
238 | 237 | Input: |
|
239 | 238 | grid : None, 'both', 'x', 'y' |
|
240 | 239 | """ |
|
241 | 240 | |
|
242 | 241 | matplotlib.pyplot.ioff() |
|
243 | 242 | |
|
244 | 243 | lines = ax.plot(x.T, y) |
|
245 | 244 | leg = ax.legend(lines, legendlabels, loc='upper right') |
|
246 | 245 | leg.get_frame().set_alpha(0.5) |
|
247 | 246 | ax.set_xlim([xmin,xmax]) |
|
248 | 247 | ax.set_ylim([ymin,ymax]) |
|
249 | 248 | printLabels(ax, xlabel, ylabel, title) |
|
250 | 249 | |
|
251 | 250 | xtickspos = numpy.arange(nxticks)*int((xmax-xmin)/(nxticks)) + int(xmin) |
|
252 | 251 | ax.set_xticks(xtickspos) |
|
253 | 252 | |
|
254 | 253 | for tick in ax.get_xticklabels(): |
|
255 | 254 | tick.set_visible(xtick_visible) |
|
256 | 255 | |
|
257 | 256 | for tick in ax.xaxis.get_major_ticks(): |
|
258 | 257 | tick.label.set_fontsize(ticksize) |
|
259 | 258 | |
|
260 | 259 | for tick in ax.get_yticklabels(): |
|
261 | 260 | tick.set_visible(ytick_visible) |
|
262 | 261 | |
|
263 | 262 | for tick in ax.yaxis.get_major_ticks(): |
|
264 | 263 | tick.label.set_fontsize(ticksize) |
|
265 | 264 | |
|
266 | 265 | iplot = ax.lines[-1] |
|
267 | 266 | |
|
268 | 267 | if '0.' in matplotlib.__version__[0:2]: |
|
269 | 268 | print "The matplotlib version has to be updated to 1.1 or newer" |
|
270 | 269 | return iplot |
|
271 | 270 | |
|
272 | 271 | if '1.0.' in matplotlib.__version__[0:4]: |
|
273 | 272 | print "The matplotlib version has to be updated to 1.1 or newer" |
|
274 | 273 | return iplot |
|
275 | 274 | |
|
276 | 275 | if grid != None: |
|
277 | 276 | ax.grid(b=True, which='major', axis=grid) |
|
278 | 277 | |
|
279 | 278 | matplotlib.pyplot.tight_layout() |
|
280 | 279 | |
|
281 | 280 | matplotlib.pyplot.ion() |
|
282 | 281 | |
|
283 | 282 | return iplot |
|
284 | 283 | |
|
285 | 284 | |
|
286 | 285 | def pmultiline(iplot, x, y, xlabel='', ylabel='', title=''): |
|
287 | 286 | |
|
288 | 287 | ax = iplot.get_axes() |
|
289 | 288 | |
|
290 | 289 | printLabels(ax, xlabel, ylabel, title) |
|
291 | 290 | |
|
292 | 291 | for i in range(len(ax.lines)): |
|
293 | 292 | line = ax.lines[i] |
|
294 | 293 | line.set_data(x[i,:],y) |
|
295 | 294 | |
|
296 | 295 | def createPmultilineYAxis(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title='', legendlabels=None, |
|
297 | 296 | ticksize=9, xtick_visible=True, ytick_visible=True, |
|
298 | 297 | nxticks=4, nyticks=10, marker='^', markersize=8, linestyle="solid", |
|
299 | 298 | grid=None, XAxisAsTime=False): |
|
300 | 299 | |
|
301 | 300 | """ |
|
302 | 301 | |
|
303 | 302 | Input: |
|
304 | 303 | grid : None, 'both', 'x', 'y' |
|
305 | 304 | """ |
|
306 | 305 | |
|
307 | 306 | matplotlib.pyplot.ioff() |
|
308 | 307 | |
|
309 | 308 | lines = ax.plot(x, y.T, marker=marker,markersize=markersize,linestyle=linestyle) |
|
310 | 309 | leg = ax.legend(lines, legendlabels, loc='upper left', bbox_to_anchor=(1.01, 1.00), numpoints=1, handlelength=1.5, \ |
|
311 | 310 | handletextpad=0.5, borderpad=0.5, labelspacing=0.5, borderaxespad=0.) |
|
312 | 311 | |
|
313 | 312 | for label in leg.get_texts(): label.set_fontsize(9) |
|
314 | 313 | |
|
315 | 314 | ax.set_xlim([xmin,xmax]) |
|
316 | 315 | ax.set_ylim([ymin,ymax]) |
|
317 | 316 | printLabels(ax, xlabel, ylabel, title) |
|
318 | 317 | |
|
319 | 318 | # xtickspos = numpy.arange(nxticks)*int((xmax-xmin)/(nxticks)) + int(xmin) |
|
320 | 319 | # ax.set_xticks(xtickspos) |
|
321 | 320 | |
|
322 | 321 | for tick in ax.get_xticklabels(): |
|
323 | 322 | tick.set_visible(xtick_visible) |
|
324 | 323 | |
|
325 | 324 | for tick in ax.xaxis.get_major_ticks(): |
|
326 | 325 | tick.label.set_fontsize(ticksize) |
|
327 | 326 | |
|
328 | 327 | for tick in ax.get_yticklabels(): |
|
329 | 328 | tick.set_visible(ytick_visible) |
|
330 | 329 | |
|
331 | 330 | for tick in ax.yaxis.get_major_ticks(): |
|
332 | 331 | tick.label.set_fontsize(ticksize) |
|
333 | 332 | |
|
334 | 333 | iplot = ax.lines[-1] |
|
335 | 334 | |
|
336 | 335 | if '0.' in matplotlib.__version__[0:2]: |
|
337 | 336 | print "The matplotlib version has to be updated to 1.1 or newer" |
|
338 | 337 | return iplot |
|
339 | 338 | |
|
340 | 339 | if '1.0.' in matplotlib.__version__[0:4]: |
|
341 | 340 | print "The matplotlib version has to be updated to 1.1 or newer" |
|
342 | 341 | return iplot |
|
343 | 342 | |
|
344 | 343 | if grid != None: |
|
345 | 344 | ax.grid(b=True, which='major', axis=grid) |
|
346 | 345 | |
|
347 | 346 | matplotlib.pyplot.tight_layout() |
|
348 | 347 | |
|
349 | 348 | if XAxisAsTime: |
|
350 | 349 | |
|
351 | 350 | func = lambda x, pos: ('%s') %(datetime.datetime.utcfromtimestamp(x).strftime("%H:%M:%S")) |
|
352 | 351 | ax.xaxis.set_major_formatter(FuncFormatter(func)) |
|
353 | 352 | ax.xaxis.set_major_locator(LinearLocator(7)) |
|
354 | 353 | |
|
355 | 354 | matplotlib.pyplot.ion() |
|
356 | 355 | |
|
357 | 356 | return iplot |
|
358 | 357 | |
|
359 | 358 | def pmultilineyaxis(iplot, x, y, xlabel='', ylabel='', title=''): |
|
360 | 359 | |
|
361 | 360 | ax = iplot.get_axes() |
|
362 | 361 | |
|
363 | 362 | printLabels(ax, xlabel, ylabel, title) |
|
364 | 363 | |
|
365 | 364 | for i in range(len(ax.lines)): |
|
366 | 365 | line = ax.lines[i] |
|
367 | 366 | line.set_data(x,y[i,:]) |
|
368 | 367 | |
|
369 | 368 | def draw(fig): |
|
370 | 369 | |
|
371 | 370 | if type(fig) == 'int': |
|
372 | 371 | raise ValueError, "This parameter should be of tpye matplotlib figure" |
|
373 | 372 | |
|
374 | 373 | fig.canvas.draw() No newline at end of file |
@@ -1,2562 +1,2573 | |||
|
1 | 1 | ''' |
|
2 | 2 | |
|
3 | 3 | $Author: murco $ |
|
4 | 4 | $Id: JRODataIO.py 169 2012-11-19 21:57:03Z murco $ |
|
5 | 5 | ''' |
|
6 | 6 | |
|
7 | 7 | import os, sys |
|
8 | 8 | import glob |
|
9 | 9 | import time |
|
10 | 10 | import numpy |
|
11 | 11 | import fnmatch |
|
12 | 12 | import time, datetime |
|
13 | 13 | |
|
14 | 14 | from jrodata import * |
|
15 | 15 | from jroheaderIO import * |
|
16 | 16 | from jroprocessing import * |
|
17 | 17 | |
|
18 | 18 | LOCALTIME = -18000 |
|
19 | 19 | |
|
20 | 20 | def isNumber(str): |
|
21 | 21 | """ |
|
22 | 22 | Chequea si el conjunto de caracteres que componen un string puede ser convertidos a un numero. |
|
23 | 23 | |
|
24 | 24 | Excepciones: |
|
25 | 25 | Si un determinado string no puede ser convertido a numero |
|
26 | 26 | Input: |
|
27 | 27 | str, string al cual se le analiza para determinar si convertible a un numero o no |
|
28 | 28 | |
|
29 | 29 | Return: |
|
30 | 30 | True : si el string es uno numerico |
|
31 | 31 | False : no es un string numerico |
|
32 | 32 | """ |
|
33 | 33 | try: |
|
34 | 34 | float( str ) |
|
35 | 35 | return True |
|
36 | 36 | except: |
|
37 | 37 | return False |
|
38 | 38 | |
|
39 | 39 | def isThisFileinRange(filename, startUTSeconds, endUTSeconds): |
|
40 | 40 | """ |
|
41 | 41 | Esta funcion determina si un archivo de datos se encuentra o no dentro del rango de fecha especificado. |
|
42 | 42 | |
|
43 | 43 | Inputs: |
|
44 | 44 | filename : nombre completo del archivo de datos en formato Jicamarca (.r) |
|
45 | 45 | |
|
46 | 46 | startUTSeconds : fecha inicial del rango seleccionado. La fecha esta dada en |
|
47 | 47 | segundos contados desde 01/01/1970. |
|
48 | 48 | endUTSeconds : fecha final del rango seleccionado. La fecha esta dada en |
|
49 | 49 | segundos contados desde 01/01/1970. |
|
50 | 50 | |
|
51 | 51 | Return: |
|
52 | 52 | Boolean : Retorna True si el archivo de datos contiene datos en el rango de |
|
53 | 53 | fecha especificado, de lo contrario retorna False. |
|
54 | 54 | |
|
55 | 55 | Excepciones: |
|
56 | 56 | Si el archivo no existe o no puede ser abierto |
|
57 | 57 | Si la cabecera no puede ser leida. |
|
58 | 58 | |
|
59 | 59 | """ |
|
60 | 60 | basicHeaderObj = BasicHeader(LOCALTIME) |
|
61 | 61 | |
|
62 | 62 | try: |
|
63 | 63 | fp = open(filename,'rb') |
|
64 | 64 | except: |
|
65 | 65 | raise IOError, "The file %s can't be opened" %(filename) |
|
66 | 66 | |
|
67 | 67 | sts = basicHeaderObj.read(fp) |
|
68 | 68 | fp.close() |
|
69 | 69 | |
|
70 | 70 | if not(sts): |
|
71 | 71 | print "Skipping the file %s because it has not a valid header" %(filename) |
|
72 | 72 | return 0 |
|
73 | 73 | |
|
74 | 74 | if not ((startUTSeconds <= basicHeaderObj.utc) and (endUTSeconds > basicHeaderObj.utc)): |
|
75 | 75 | return 0 |
|
76 | 76 | |
|
77 | 77 | return 1 |
|
78 | 78 | |
|
79 | 79 | def isFileinThisTime(filename, startTime, endTime): |
|
80 | 80 | """ |
|
81 | 81 | Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado. |
|
82 | 82 | |
|
83 | 83 | Inputs: |
|
84 | 84 | filename : nombre completo del archivo de datos en formato Jicamarca (.r) |
|
85 | 85 | |
|
86 | 86 | startTime : tiempo inicial del rango seleccionado en formato datetime.time |
|
87 | 87 | |
|
88 | 88 | endTime : tiempo final del rango seleccionado en formato datetime.time |
|
89 | 89 | |
|
90 | 90 | Return: |
|
91 | 91 | Boolean : Retorna True si el archivo de datos contiene datos en el rango de |
|
92 | 92 | fecha especificado, de lo contrario retorna False. |
|
93 | 93 | |
|
94 | 94 | Excepciones: |
|
95 | 95 | Si el archivo no existe o no puede ser abierto |
|
96 | 96 | Si la cabecera no puede ser leida. |
|
97 | 97 | |
|
98 | 98 | """ |
|
99 | 99 | |
|
100 | 100 | |
|
101 | 101 | try: |
|
102 | 102 | fp = open(filename,'rb') |
|
103 | 103 | except: |
|
104 | 104 | raise IOError, "The file %s can't be opened" %(filename) |
|
105 | 105 | |
|
106 | 106 | basicHeaderObj = BasicHeader(LOCALTIME) |
|
107 | 107 | sts = basicHeaderObj.read(fp) |
|
108 | 108 | fp.close() |
|
109 | 109 | |
|
110 | 110 | thisTime = basicHeaderObj.datatime.time() |
|
111 | 111 | |
|
112 | 112 | if not(sts): |
|
113 | 113 | print "Skipping the file %s because it has not a valid header" %(filename) |
|
114 | 114 | return 0 |
|
115 | 115 | |
|
116 | 116 | if not ((startTime <= thisTime) and (endTime > thisTime)): |
|
117 | 117 | return 0 |
|
118 | 118 | |
|
119 | 119 | return 1 |
|
120 | 120 | |
|
121 | 121 | def getlastFileFromPath(path, ext): |
|
122 | 122 | """ |
|
123 | 123 | Depura el fileList dejando solo los que cumplan el formato de "PYYYYDDDSSS.ext" |
|
124 | 124 | al final de la depuracion devuelve el ultimo file de la lista que quedo. |
|
125 | 125 | |
|
126 | 126 | Input: |
|
127 | 127 | fileList : lista conteniendo todos los files (sin path) que componen una determinada carpeta |
|
128 | 128 | ext : extension de los files contenidos en una carpeta |
|
129 | 129 | |
|
130 | 130 | Return: |
|
131 | 131 | El ultimo file de una determinada carpeta, no se considera el path. |
|
132 | 132 | """ |
|
133 | 133 | validFilelist = [] |
|
134 | 134 | fileList = os.listdir(path) |
|
135 | 135 | |
|
136 | 136 | # 0 1234 567 89A BCDE |
|
137 | 137 | # H YYYY DDD SSS .ext |
|
138 | 138 | |
|
139 | 139 | for file in fileList: |
|
140 | 140 | try: |
|
141 | 141 | year = int(file[1:5]) |
|
142 | 142 | doy = int(file[5:8]) |
|
143 | 143 | |
|
144 | 144 | |
|
145 | 145 | except: |
|
146 | 146 | continue |
|
147 | 147 | |
|
148 | 148 | if (os.path.splitext(file)[-1].lower() != ext.lower()): |
|
149 | 149 | continue |
|
150 | 150 | |
|
151 | 151 | validFilelist.append(file) |
|
152 | 152 | |
|
153 | 153 | if validFilelist: |
|
154 | 154 | validFilelist = sorted( validFilelist, key=str.lower ) |
|
155 | 155 | return validFilelist[-1] |
|
156 | 156 | |
|
157 | 157 | return None |
|
158 | 158 | |
|
159 | 159 | def checkForRealPath(path, year, doy, set, ext): |
|
160 | 160 | """ |
|
161 | 161 | Por ser Linux Case Sensitive entonces checkForRealPath encuentra el nombre correcto de un path, |
|
162 | 162 | Prueba por varias combinaciones de nombres entre mayusculas y minusculas para determinar |
|
163 | 163 | el path exacto de un determinado file. |
|
164 | 164 | |
|
165 | 165 | Example : |
|
166 | 166 | nombre correcto del file es .../.../D2009307/P2009307367.ext |
|
167 | 167 | |
|
168 | 168 | Entonces la funcion prueba con las siguientes combinaciones |
|
169 | 169 | .../.../y2009307367.ext |
|
170 | 170 | .../.../Y2009307367.ext |
|
171 | 171 | .../.../x2009307/y2009307367.ext |
|
172 | 172 | .../.../x2009307/Y2009307367.ext |
|
173 | 173 | .../.../X2009307/y2009307367.ext |
|
174 | 174 | .../.../X2009307/Y2009307367.ext |
|
175 | 175 | siendo para este caso, la ultima combinacion de letras, identica al file buscado |
|
176 | 176 | |
|
177 | 177 | Return: |
|
178 | 178 | Si encuentra la cobinacion adecuada devuelve el path completo y el nombre del file |
|
179 | 179 | caso contrario devuelve None como path y el la ultima combinacion de nombre en mayusculas |
|
180 | 180 | para el filename |
|
181 | 181 | """ |
|
182 | 182 | fullfilename = None |
|
183 | 183 | find_flag = False |
|
184 | 184 | filename = None |
|
185 | 185 | |
|
186 | 186 | prefixDirList = [None,'d','D'] |
|
187 | 187 | if ext.lower() == ".r": #voltage |
|
188 | 188 | prefixFileList = ['d','D'] |
|
189 | 189 | elif ext.lower() == ".pdata": #spectra |
|
190 | 190 | prefixFileList = ['p','P'] |
|
191 | 191 | else: |
|
192 | 192 | return None, filename |
|
193 | 193 | |
|
194 | 194 | #barrido por las combinaciones posibles |
|
195 | 195 | for prefixDir in prefixDirList: |
|
196 | 196 | thispath = path |
|
197 | 197 | if prefixDir != None: |
|
198 | 198 | #formo el nombre del directorio xYYYYDDD (x=d o x=D) |
|
199 | 199 | thispath = os.path.join(path, "%s%04d%03d" % ( prefixDir, year, doy )) |
|
200 | 200 | |
|
201 | 201 | for prefixFile in prefixFileList: #barrido por las dos combinaciones posibles de "D" |
|
202 | 202 | filename = "%s%04d%03d%03d%s" % ( prefixFile, year, doy, set, ext ) #formo el nombre del file xYYYYDDDSSS.ext |
|
203 | 203 | fullfilename = os.path.join( thispath, filename ) #formo el path completo |
|
204 | 204 | |
|
205 | 205 | if os.path.exists( fullfilename ): #verifico que exista |
|
206 | 206 | find_flag = True |
|
207 | 207 | break |
|
208 | 208 | if find_flag: |
|
209 | 209 | break |
|
210 | 210 | |
|
211 | 211 | if not(find_flag): |
|
212 | 212 | return None, filename |
|
213 | 213 | |
|
214 | 214 | return fullfilename, filename |
|
215 | 215 | |
|
216 | 216 | class JRODataIO: |
|
217 | 217 | |
|
218 | 218 | c = 3E8 |
|
219 | 219 | |
|
220 | 220 | isConfig = False |
|
221 | 221 | |
|
222 | 222 | basicHeaderObj = BasicHeader(LOCALTIME) |
|
223 | 223 | |
|
224 | 224 | systemHeaderObj = SystemHeader() |
|
225 | 225 | |
|
226 | 226 | radarControllerHeaderObj = RadarControllerHeader() |
|
227 | 227 | |
|
228 | 228 | processingHeaderObj = ProcessingHeader() |
|
229 | 229 | |
|
230 | 230 | online = 0 |
|
231 | 231 | |
|
232 | 232 | dtype = None |
|
233 | 233 | |
|
234 | 234 | pathList = [] |
|
235 | 235 | |
|
236 | 236 | filenameList = [] |
|
237 | 237 | |
|
238 | 238 | filename = None |
|
239 | 239 | |
|
240 | 240 | ext = None |
|
241 | 241 | |
|
242 | 242 | flagIsNewFile = 1 |
|
243 | 243 | |
|
244 | 244 | flagTimeBlock = 0 |
|
245 | 245 | |
|
246 | 246 | flagIsNewBlock = 0 |
|
247 | 247 | |
|
248 | 248 | fp = None |
|
249 | 249 | |
|
250 | 250 | firstHeaderSize = 0 |
|
251 | 251 | |
|
252 | 252 | basicHeaderSize = 24 |
|
253 | 253 | |
|
254 | 254 | versionFile = 1103 |
|
255 | 255 | |
|
256 | 256 | fileSize = None |
|
257 | 257 | |
|
258 | 258 | ippSeconds = None |
|
259 | 259 | |
|
260 | 260 | fileSizeByHeader = None |
|
261 | 261 | |
|
262 | 262 | fileIndex = None |
|
263 | 263 | |
|
264 | 264 | profileIndex = None |
|
265 | 265 | |
|
266 | 266 | blockIndex = None |
|
267 | 267 | |
|
268 | 268 | nTotalBlocks = None |
|
269 | 269 | |
|
270 | 270 | maxTimeStep = 30 |
|
271 | 271 | |
|
272 | 272 | lastUTTime = None |
|
273 | 273 | |
|
274 | 274 | datablock = None |
|
275 | 275 | |
|
276 | 276 | dataOut = None |
|
277 | 277 | |
|
278 | 278 | blocksize = None |
|
279 | 279 | |
|
280 | 280 | def __init__(self): |
|
281 | 281 | |
|
282 | 282 | raise ValueError, "Not implemented" |
|
283 | 283 | |
|
284 | 284 | def run(self): |
|
285 | 285 | |
|
286 | 286 | raise ValueError, "Not implemented" |
|
287 | 287 | |
|
288 | 288 | def getOutput(self): |
|
289 | 289 | |
|
290 | 290 | return self.dataOut |
|
291 | 291 | |
|
292 | 292 | class JRODataReader(JRODataIO, ProcessingUnit): |
|
293 | 293 | |
|
294 | 294 | nReadBlocks = 0 |
|
295 | 295 | |
|
296 | 296 | delay = 10 #number of seconds waiting a new file |
|
297 | 297 | |
|
298 | 298 | nTries = 3 #quantity tries |
|
299 | 299 | |
|
300 | 300 | nFiles = 3 #number of files for searching |
|
301 | 301 | |
|
302 | 302 | flagNoMoreFiles = 0 |
|
303 | 303 | |
|
304 | 304 | def __init__(self): |
|
305 | 305 | |
|
306 | 306 | """ |
|
307 | 307 | |
|
308 | 308 | """ |
|
309 | 309 | |
|
310 | 310 | raise ValueError, "This method has not been implemented" |
|
311 | 311 | |
|
312 | 312 | |
|
313 | 313 | def createObjByDefault(self): |
|
314 | 314 | """ |
|
315 | 315 | |
|
316 | 316 | """ |
|
317 | 317 | raise ValueError, "This method has not been implemented" |
|
318 | 318 | |
|
319 | 319 | def getBlockDimension(self): |
|
320 | 320 | |
|
321 | 321 | raise ValueError, "No implemented" |
|
322 | 322 | |
|
323 | 323 | def __searchFilesOffLine(self, |
|
324 | 324 | path, |
|
325 | 325 | startDate, |
|
326 | 326 | endDate, |
|
327 | 327 | startTime=datetime.time(0,0,0), |
|
328 | 328 | endTime=datetime.time(23,59,59), |
|
329 | 329 | set=None, |
|
330 | 330 | expLabel='', |
|
331 | 331 | ext='.r', |
|
332 | 332 | walk=True): |
|
333 | 333 | |
|
334 | 334 | pathList = [] |
|
335 | 335 | |
|
336 | 336 | if not walk: |
|
337 | 337 | pathList.append(path) |
|
338 | 338 | |
|
339 | 339 | else: |
|
340 | 340 | dirList = [] |
|
341 | 341 | for thisPath in os.listdir(path): |
|
342 | 342 | if os.path.isdir(os.path.join(path,thisPath)): |
|
343 | 343 | dirList.append(thisPath) |
|
344 | 344 | |
|
345 | 345 | if not(dirList): |
|
346 | 346 | return None, None |
|
347 | 347 | |
|
348 | 348 | thisDate = startDate |
|
349 | 349 | |
|
350 | 350 | while(thisDate <= endDate): |
|
351 | 351 | year = thisDate.timetuple().tm_year |
|
352 | 352 | doy = thisDate.timetuple().tm_yday |
|
353 | 353 | |
|
354 | 354 | match = fnmatch.filter(dirList, '?' + '%4.4d%3.3d' % (year,doy)) |
|
355 | 355 | if len(match) == 0: |
|
356 | 356 | thisDate += datetime.timedelta(1) |
|
357 | 357 | continue |
|
358 | 358 | |
|
359 | 359 | pathList.append(os.path.join(path,match[0],expLabel)) |
|
360 | 360 | thisDate += datetime.timedelta(1) |
|
361 | 361 | |
|
362 | 362 | if pathList == []: |
|
363 |
print "Any folder found |
|
|
363 | print "Any folder was found for the date range: %s-%s" %(startDate, endDate) | |
|
364 | 364 | return None, None |
|
365 | 365 | |
|
366 |
print "%d folder(s) found |
|
|
366 | print "%d folder(s) was(were) found for the date range: %s-%s" %(len(pathList), startDate, endDate) | |
|
367 | 367 | |
|
368 | 368 | filenameList = [] |
|
369 | 369 | for thisPath in pathList: |
|
370 | 370 | |
|
371 | 371 | fileList = glob.glob1(thisPath, "*%s" %ext) |
|
372 | 372 | fileList.sort() |
|
373 | 373 | |
|
374 | 374 | for file in fileList: |
|
375 | 375 | |
|
376 | 376 | filename = os.path.join(thisPath,file) |
|
377 | 377 | |
|
378 | 378 | if isFileinThisTime(filename, startTime, endTime): |
|
379 | 379 | filenameList.append(filename) |
|
380 | 380 | |
|
381 | 381 | if not(filenameList): |
|
382 |
print "Any file found |
|
|
382 | print "Any file was found for the time range %s - %s" %(startTime, endTime) | |
|
383 | 383 | return None, None |
|
384 | 384 | |
|
385 | print "%d file(s) was(were) found for the time range: %s - %s" %(len(filenameList), startTime, endTime) | |
|
386 | ||
|
385 | 387 | self.filenameList = filenameList |
|
386 | 388 | |
|
387 | 389 | return pathList, filenameList |
|
388 | 390 | |
|
389 | 391 | def __searchFilesOnLine(self, path, expLabel = "", ext = None, walk=True): |
|
390 | 392 | |
|
391 | 393 | """ |
|
392 | 394 | Busca el ultimo archivo de la ultima carpeta (determinada o no por startDateTime) y |
|
393 | 395 | devuelve el archivo encontrado ademas de otros datos. |
|
394 | 396 | |
|
395 | 397 | Input: |
|
396 | 398 | path : carpeta donde estan contenidos los files que contiene data |
|
397 | 399 | |
|
398 | 400 | expLabel : Nombre del subexperimento (subfolder) |
|
399 | 401 | |
|
400 | 402 | ext : extension de los files |
|
401 | 403 | |
|
402 | 404 | walk : Si es habilitado no realiza busquedas dentro de los ubdirectorios (doypath) |
|
403 | 405 | |
|
404 | 406 | Return: |
|
405 | 407 | directory : eL directorio donde esta el file encontrado |
|
406 | 408 | filename : el ultimo file de una determinada carpeta |
|
407 | 409 | year : el anho |
|
408 | 410 | doy : el numero de dia del anho |
|
409 | 411 | set : el set del archivo |
|
410 | 412 | |
|
411 | 413 | |
|
412 | 414 | """ |
|
413 | 415 | dirList = [] |
|
414 | 416 | |
|
415 | 417 | if walk: |
|
416 | 418 | |
|
417 | 419 | #Filtra solo los directorios |
|
418 | 420 | for thisPath in os.listdir(path): |
|
419 | 421 | if os.path.isdir(os.path.join(path, thisPath)): |
|
420 | 422 | dirList.append(thisPath) |
|
421 | 423 | |
|
422 | 424 | if not(dirList): |
|
423 | 425 | return None, None, None, None, None |
|
424 | 426 | |
|
425 | 427 | dirList = sorted( dirList, key=str.lower ) |
|
426 | 428 | |
|
427 | 429 | doypath = dirList[-1] |
|
428 | 430 | fullpath = os.path.join(path, doypath, expLabel) |
|
429 | 431 | |
|
430 | 432 | else: |
|
431 | 433 | fullpath = path |
|
432 | 434 | |
|
433 | 435 | filename = getlastFileFromPath(fullpath, ext) |
|
434 | 436 | |
|
435 | 437 | if not(filename): |
|
436 | 438 | return None, None, None, None, None |
|
437 | 439 | |
|
438 | 440 | if not(self.__verifyFile(os.path.join(fullpath, filename))): |
|
439 | 441 | return None, None, None, None, None |
|
440 | 442 | |
|
441 | 443 | year = int( filename[1:5] ) |
|
442 | 444 | doy = int( filename[5:8] ) |
|
443 | 445 | set = int( filename[8:11] ) |
|
444 | 446 | |
|
445 | 447 | return fullpath, filename, year, doy, set |
|
446 | 448 | |
|
447 | 449 | |
|
448 | 450 | |
|
449 | 451 | def __setNextFileOffline(self): |
|
450 | 452 | |
|
451 | 453 | idFile = self.fileIndex |
|
452 | 454 | |
|
453 | 455 | while (True): |
|
454 | 456 | idFile += 1 |
|
455 | 457 | if not(idFile < len(self.filenameList)): |
|
456 | 458 | self.flagNoMoreFiles = 1 |
|
457 | 459 | print "No more Files" |
|
458 | 460 | return 0 |
|
459 | 461 | |
|
460 | 462 | filename = self.filenameList[idFile] |
|
461 | 463 | |
|
462 | 464 | if not(self.__verifyFile(filename)): |
|
463 | 465 | continue |
|
464 | 466 | |
|
465 | 467 | fileSize = os.path.getsize(filename) |
|
466 | 468 | fp = open(filename,'rb') |
|
467 | 469 | break |
|
468 | 470 | |
|
469 | 471 | self.flagIsNewFile = 1 |
|
470 | 472 | self.fileIndex = idFile |
|
471 | 473 | self.filename = filename |
|
472 | 474 | self.fileSize = fileSize |
|
473 | 475 | self.fp = fp |
|
474 | 476 | |
|
475 | 477 | print "Setting the file: %s"%self.filename |
|
476 | 478 | |
|
477 | 479 | return 1 |
|
478 | 480 | |
|
479 | 481 | def __setNextFileOnline(self): |
|
480 | 482 | """ |
|
481 | 483 | Busca el siguiente file que tenga suficiente data para ser leida, dentro de un folder especifico, si |
|
482 | 484 | no encuentra un file valido espera un tiempo determinado y luego busca en los posibles n files |
|
483 | 485 | siguientes. |
|
484 | 486 | |
|
485 | 487 | Affected: |
|
486 | 488 | self.flagIsNewFile |
|
487 | 489 | self.filename |
|
488 | 490 | self.fileSize |
|
489 | 491 | self.fp |
|
490 | 492 | self.set |
|
491 | 493 | self.flagNoMoreFiles |
|
492 | 494 | |
|
493 | 495 | Return: |
|
494 | 496 | 0 : si luego de una busqueda del siguiente file valido este no pudo ser encontrado |
|
495 | 497 | 1 : si el file fue abierto con exito y esta listo a ser leido |
|
496 | 498 | |
|
497 | 499 | Excepciones: |
|
498 | 500 | Si un determinado file no puede ser abierto |
|
499 | 501 | """ |
|
500 | 502 | nFiles = 0 |
|
501 | 503 | fileOk_flag = False |
|
502 | 504 | firstTime_flag = True |
|
503 | 505 | |
|
504 | 506 | self.set += 1 |
|
505 | 507 | |
|
506 | 508 | #busca el 1er file disponible |
|
507 | 509 | fullfilename, filename = checkForRealPath( self.path, self.year, self.doy, self.set, self.ext ) |
|
508 | 510 | if fullfilename: |
|
509 | 511 | if self.__verifyFile(fullfilename, False): |
|
510 | 512 | fileOk_flag = True |
|
511 | 513 | |
|
512 | 514 | #si no encuentra un file entonces espera y vuelve a buscar |
|
513 | 515 | if not(fileOk_flag): |
|
514 | 516 | for nFiles in range(self.nFiles+1): #busco en los siguientes self.nFiles+1 files posibles |
|
515 | 517 | |
|
516 | 518 | if firstTime_flag: #si es la 1era vez entonces hace el for self.nTries veces |
|
517 | 519 | tries = self.nTries |
|
518 | 520 | else: |
|
519 | 521 | tries = 1 #si no es la 1era vez entonces solo lo hace una vez |
|
520 | 522 | |
|
521 | 523 | for nTries in range( tries ): |
|
522 | 524 | if firstTime_flag: |
|
523 | 525 | print "\tWaiting %0.2f sec for the file \"%s\" , try %03d ..." % ( self.delay, filename, nTries+1 ) |
|
524 | 526 | time.sleep( self.delay ) |
|
525 | 527 | else: |
|
526 | 528 | print "\tSearching next \"%s%04d%03d%03d%s\" file ..." % (self.optchar, self.year, self.doy, self.set, self.ext) |
|
527 | 529 | |
|
528 | 530 | fullfilename, filename = checkForRealPath( self.path, self.year, self.doy, self.set, self.ext ) |
|
529 | 531 | if fullfilename: |
|
530 | 532 | if self.__verifyFile(fullfilename): |
|
531 | 533 | fileOk_flag = True |
|
532 | 534 | break |
|
533 | 535 | |
|
534 | 536 | if fileOk_flag: |
|
535 | 537 | break |
|
536 | 538 | |
|
537 | 539 | firstTime_flag = False |
|
538 | 540 | |
|
539 | 541 | print "\tSkipping the file \"%s\" due to this file doesn't exist" % filename |
|
540 | 542 | self.set += 1 |
|
541 | 543 | |
|
542 | 544 | if nFiles == (self.nFiles-1): #si no encuentro el file buscado cambio de carpeta y busco en la siguiente carpeta |
|
543 | 545 | self.set = 0 |
|
544 | 546 | self.doy += 1 |
|
545 | 547 | |
|
546 | 548 | if fileOk_flag: |
|
547 | 549 | self.fileSize = os.path.getsize( fullfilename ) |
|
548 | 550 | self.filename = fullfilename |
|
549 | 551 | self.flagIsNewFile = 1 |
|
550 | 552 | if self.fp != None: self.fp.close() |
|
551 | 553 | self.fp = open(fullfilename, 'rb') |
|
552 | 554 | self.flagNoMoreFiles = 0 |
|
553 | 555 | print 'Setting the file: %s' % fullfilename |
|
554 | 556 | else: |
|
555 | 557 | self.fileSize = 0 |
|
556 | 558 | self.filename = None |
|
557 | 559 | self.flagIsNewFile = 0 |
|
558 | 560 | self.fp = None |
|
559 | 561 | self.flagNoMoreFiles = 1 |
|
560 | 562 | print 'No more Files' |
|
561 | 563 | |
|
562 | 564 | return fileOk_flag |
|
563 | 565 | |
|
564 | 566 | |
|
565 | 567 | def setNextFile(self): |
|
566 | 568 | if self.fp != None: |
|
567 | 569 | self.fp.close() |
|
568 | 570 | |
|
569 | 571 | if self.online: |
|
570 | 572 | newFile = self.__setNextFileOnline() |
|
571 | 573 | else: |
|
572 | 574 | newFile = self.__setNextFileOffline() |
|
573 | 575 | |
|
574 | 576 | if not(newFile): |
|
575 | 577 | return 0 |
|
576 | 578 | |
|
577 | 579 | self.__readFirstHeader() |
|
578 | 580 | self.nReadBlocks = 0 |
|
579 | 581 | return 1 |
|
580 | 582 | |
|
581 | 583 | def __waitNewBlock(self): |
|
582 | 584 | """ |
|
583 | 585 | Return 1 si se encontro un nuevo bloque de datos, 0 de otra forma. |
|
584 | 586 | |
|
585 | 587 | Si el modo de lectura es OffLine siempre retorn 0 |
|
586 | 588 | """ |
|
587 | 589 | if not self.online: |
|
588 | 590 | return 0 |
|
589 | 591 | |
|
590 | 592 | if (self.nReadBlocks >= self.processingHeaderObj.dataBlocksPerFile): |
|
591 | 593 | return 0 |
|
592 | 594 | |
|
593 | 595 | currentPointer = self.fp.tell() |
|
594 | 596 | |
|
595 | 597 | neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize |
|
596 | 598 | |
|
597 | 599 | for nTries in range( self.nTries ): |
|
598 | 600 | |
|
599 | 601 | self.fp.close() |
|
600 | 602 | self.fp = open( self.filename, 'rb' ) |
|
601 | 603 | self.fp.seek( currentPointer ) |
|
602 | 604 | |
|
603 | 605 | self.fileSize = os.path.getsize( self.filename ) |
|
604 | 606 | currentSize = self.fileSize - currentPointer |
|
605 | 607 | |
|
606 | 608 | if ( currentSize >= neededSize ): |
|
607 | 609 | self.__rdBasicHeader() |
|
608 | 610 | return 1 |
|
609 | 611 | |
|
610 | 612 | print "\tWaiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries+1) |
|
611 | 613 | time.sleep( self.delay ) |
|
612 | 614 | |
|
613 | 615 | |
|
614 | 616 | return 0 |
|
615 | 617 | |
|
616 | 618 | def __setNewBlock(self): |
|
617 | 619 | |
|
618 | 620 | if self.fp == None: |
|
619 | 621 | return 0 |
|
620 | 622 | |
|
621 | 623 | if self.flagIsNewFile: |
|
622 | 624 | return 1 |
|
623 | 625 | |
|
624 | 626 | self.lastUTTime = self.basicHeaderObj.utc |
|
625 | 627 | currentSize = self.fileSize - self.fp.tell() |
|
626 | 628 | neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize |
|
627 | 629 | |
|
628 | 630 | if (currentSize >= neededSize): |
|
629 | 631 | self.__rdBasicHeader() |
|
630 | 632 | return 1 |
|
631 | 633 | |
|
632 | 634 | if self.__waitNewBlock(): |
|
633 | 635 | return 1 |
|
634 | 636 | |
|
635 | 637 | if not(self.setNextFile()): |
|
636 | 638 | return 0 |
|
637 | 639 | |
|
638 | 640 | deltaTime = self.basicHeaderObj.utc - self.lastUTTime # |
|
639 | 641 | |
|
640 | 642 | self.flagTimeBlock = 0 |
|
641 | 643 | |
|
642 | 644 | if deltaTime > self.maxTimeStep: |
|
643 | 645 | self.flagTimeBlock = 1 |
|
644 | 646 | |
|
645 | 647 | return 1 |
|
646 | 648 | |
|
647 | 649 | |
|
648 | 650 | def readNextBlock(self): |
|
649 | 651 | if not(self.__setNewBlock()): |
|
650 | 652 | return 0 |
|
651 | 653 | |
|
652 | 654 | if not(self.readBlock()): |
|
653 | 655 | return 0 |
|
654 | 656 | |
|
655 | 657 | return 1 |
|
656 | 658 | |
|
657 | 659 | def __rdProcessingHeader(self, fp=None): |
|
658 | 660 | if fp == None: |
|
659 | 661 | fp = self.fp |
|
660 | 662 | |
|
661 | 663 | self.processingHeaderObj.read(fp) |
|
662 | 664 | |
|
663 | 665 | def __rdRadarControllerHeader(self, fp=None): |
|
664 | 666 | if fp == None: |
|
665 | 667 | fp = self.fp |
|
666 | 668 | |
|
667 | 669 | self.radarControllerHeaderObj.read(fp) |
|
668 | 670 | |
|
669 | 671 | def __rdSystemHeader(self, fp=None): |
|
670 | 672 | if fp == None: |
|
671 | 673 | fp = self.fp |
|
672 | 674 | |
|
673 | 675 | self.systemHeaderObj.read(fp) |
|
674 | 676 | |
|
675 | 677 | def __rdBasicHeader(self, fp=None): |
|
676 | 678 | if fp == None: |
|
677 | 679 | fp = self.fp |
|
678 | 680 | |
|
679 | 681 | self.basicHeaderObj.read(fp) |
|
680 | 682 | |
|
681 | 683 | |
|
682 | 684 | def __readFirstHeader(self): |
|
683 | 685 | self.__rdBasicHeader() |
|
684 | 686 | self.__rdSystemHeader() |
|
685 | 687 | self.__rdRadarControllerHeader() |
|
686 | 688 | self.__rdProcessingHeader() |
|
687 | 689 | |
|
688 | 690 | self.firstHeaderSize = self.basicHeaderObj.size |
|
689 | 691 | |
|
690 | 692 | datatype = int(numpy.log2((self.processingHeaderObj.processFlags & PROCFLAG.DATATYPE_MASK))-numpy.log2(PROCFLAG.DATATYPE_CHAR)) |
|
691 | 693 | if datatype == 0: |
|
692 | 694 | datatype_str = numpy.dtype([('real','<i1'),('imag','<i1')]) |
|
693 | 695 | elif datatype == 1: |
|
694 | 696 | datatype_str = numpy.dtype([('real','<i2'),('imag','<i2')]) |
|
695 | 697 | elif datatype == 2: |
|
696 | 698 | datatype_str = numpy.dtype([('real','<i4'),('imag','<i4')]) |
|
697 | 699 | elif datatype == 3: |
|
698 | 700 | datatype_str = numpy.dtype([('real','<i8'),('imag','<i8')]) |
|
699 | 701 | elif datatype == 4: |
|
700 | 702 | datatype_str = numpy.dtype([('real','<f4'),('imag','<f4')]) |
|
701 | 703 | elif datatype == 5: |
|
702 | 704 | datatype_str = numpy.dtype([('real','<f8'),('imag','<f8')]) |
|
703 | 705 | else: |
|
704 | 706 | raise ValueError, 'Data type was not defined' |
|
705 | 707 | |
|
706 | 708 | self.dtype = datatype_str |
|
707 | 709 | self.ippSeconds = 2 * 1000 * self.radarControllerHeaderObj.ipp / self.c |
|
708 | 710 | self.fileSizeByHeader = self.processingHeaderObj.dataBlocksPerFile * self.processingHeaderObj.blockSize + self.firstHeaderSize + self.basicHeaderSize*(self.processingHeaderObj.dataBlocksPerFile - 1) |
|
709 | 711 | # self.dataOut.channelList = numpy.arange(self.systemHeaderObj.numChannels) |
|
710 | 712 | # self.dataOut.channelIndexList = numpy.arange(self.systemHeaderObj.numChannels) |
|
711 | 713 | self.getBlockDimension() |
|
712 | 714 | |
|
713 | 715 | |
|
714 | 716 | def __verifyFile(self, filename, msgFlag=True): |
|
715 | 717 | msg = None |
|
716 | 718 | try: |
|
717 | 719 | fp = open(filename, 'rb') |
|
718 | 720 | currentPosition = fp.tell() |
|
719 | 721 | except: |
|
720 | 722 | if msgFlag: |
|
721 | 723 | print "The file %s can't be opened" % (filename) |
|
722 | 724 | return False |
|
723 | 725 | |
|
724 | 726 | neededSize = self.processingHeaderObj.blockSize + self.firstHeaderSize |
|
725 | 727 | |
|
726 | 728 | if neededSize == 0: |
|
727 | 729 | basicHeaderObj = BasicHeader(LOCALTIME) |
|
728 | 730 | systemHeaderObj = SystemHeader() |
|
729 | 731 | radarControllerHeaderObj = RadarControllerHeader() |
|
730 | 732 | processingHeaderObj = ProcessingHeader() |
|
731 | 733 | |
|
732 | 734 | try: |
|
733 | 735 | if not( basicHeaderObj.read(fp) ): raise IOError |
|
734 | 736 | if not( systemHeaderObj.read(fp) ): raise IOError |
|
735 | 737 | if not( radarControllerHeaderObj.read(fp) ): raise IOError |
|
736 | 738 | if not( processingHeaderObj.read(fp) ): raise IOError |
|
737 | 739 | data_type = int(numpy.log2((processingHeaderObj.processFlags & PROCFLAG.DATATYPE_MASK))-numpy.log2(PROCFLAG.DATATYPE_CHAR)) |
|
738 | 740 | |
|
739 | 741 | neededSize = processingHeaderObj.blockSize + basicHeaderObj.size |
|
740 | 742 | |
|
741 | 743 | except: |
|
742 | 744 | if msgFlag: |
|
743 | 745 | print "\tThe file %s is empty or it hasn't enough data" % filename |
|
744 | 746 | |
|
745 | 747 | fp.close() |
|
746 | 748 | return False |
|
747 | 749 | else: |
|
748 | 750 | msg = "\tSkipping the file %s due to it hasn't enough data" %filename |
|
749 | 751 | |
|
750 | 752 | fp.close() |
|
751 | 753 | fileSize = os.path.getsize(filename) |
|
752 | 754 | currentSize = fileSize - currentPosition |
|
753 | 755 | if currentSize < neededSize: |
|
754 | 756 | if msgFlag and (msg != None): |
|
755 | 757 | print msg #print"\tSkipping the file %s due to it hasn't enough data" %filename |
|
756 | 758 | return False |
|
757 | 759 | |
|
758 | 760 | return True |
|
759 | 761 | |
|
760 | 762 | def setup(self, |
|
761 | 763 | path=None, |
|
762 | 764 | startDate=None, |
|
763 | 765 | endDate=None, |
|
764 | 766 | startTime=datetime.time(0,0,0), |
|
765 | 767 | endTime=datetime.time(23,59,59), |
|
766 | 768 | set=0, |
|
767 | 769 | expLabel = "", |
|
768 | 770 | ext = None, |
|
769 | 771 | online = False, |
|
770 | 772 | delay = 60, |
|
771 | 773 | walk = True): |
|
772 | 774 | |
|
773 | 775 | if path == None: |
|
774 | 776 | raise ValueError, "The path is not valid" |
|
775 | 777 | |
|
776 | 778 | if ext == None: |
|
777 | 779 | ext = self.ext |
|
778 | 780 | |
|
779 | 781 | if online: |
|
780 | 782 | print "Searching files in online mode..." |
|
781 | 783 | |
|
782 | 784 | for nTries in range( self.nTries ): |
|
783 | 785 | fullpath, file, year, doy, set = self.__searchFilesOnLine(path=path, expLabel=expLabel, ext=ext, walk=walk) |
|
784 | 786 | |
|
785 | 787 | if fullpath: |
|
786 | 788 | break |
|
787 | 789 | |
|
788 | 790 | print '\tWaiting %0.2f sec for an valid file in %s: try %02d ...' % (self.delay, path, nTries+1) |
|
789 | 791 | time.sleep( self.delay ) |
|
790 | 792 | |
|
791 | 793 | if not(fullpath): |
|
792 | 794 | print "There 'isn't valied files in %s" % path |
|
793 | 795 | return None |
|
794 | 796 | |
|
795 | 797 | self.year = year |
|
796 | 798 | self.doy = doy |
|
797 | 799 | self.set = set - 1 |
|
798 | 800 | self.path = path |
|
799 | 801 | |
|
800 | 802 | else: |
|
801 | 803 | print "Searching files in offline mode ..." |
|
802 | 804 | pathList, filenameList = self.__searchFilesOffLine(path, startDate=startDate, endDate=endDate, |
|
803 | 805 | startTime=startTime, endTime=endTime, |
|
804 | 806 | set=set, expLabel=expLabel, ext=ext, |
|
805 | 807 | walk=walk) |
|
806 | 808 | |
|
807 | 809 | if not(pathList): |
|
808 | 810 | print "No *%s files into the folder %s \nfor the range: %s - %s"%(ext, path, |
|
809 | 811 | datetime.datetime.combine(startDate,startTime).ctime(), |
|
810 | 812 | datetime.datetime.combine(endDate,endTime).ctime()) |
|
811 | 813 | |
|
812 | 814 | sys.exit(-1) |
|
813 | 815 | |
|
814 | 816 | |
|
815 | 817 | self.fileIndex = -1 |
|
816 | 818 | self.pathList = pathList |
|
817 | 819 | self.filenameList = filenameList |
|
818 | 820 | |
|
819 | 821 | self.online = online |
|
820 | 822 | self.delay = delay |
|
821 | 823 | ext = ext.lower() |
|
822 | 824 | self.ext = ext |
|
823 | 825 | |
|
824 | 826 | if not(self.setNextFile()): |
|
825 | 827 | if (startDate!=None) and (endDate!=None): |
|
826 | 828 | print "No files in range: %s - %s" %(datetime.datetime.combine(startDate,startTime).ctime(), datetime.datetime.combine(endDate,endTime).ctime()) |
|
827 | 829 | elif startDate != None: |
|
828 | 830 | print "No files in range: %s" %(datetime.datetime.combine(startDate,startTime).ctime()) |
|
829 | 831 | else: |
|
830 | 832 | print "No files" |
|
831 | 833 | |
|
832 | 834 | sys.exit(-1) |
|
833 | 835 | |
|
834 | 836 | # self.updateDataHeader() |
|
835 | 837 | |
|
836 | 838 | return self.dataOut |
|
837 | 839 | |
|
838 | 840 | def getData(): |
|
839 | 841 | |
|
840 | 842 | raise ValueError, "This method has not been implemented" |
|
841 | 843 | |
|
842 | 844 | def hasNotDataInBuffer(): |
|
843 | 845 | |
|
844 | 846 | raise ValueError, "This method has not been implemented" |
|
845 | 847 | |
|
846 | 848 | def readBlock(): |
|
847 | 849 | |
|
848 | 850 | raise ValueError, "This method has not been implemented" |
|
849 | 851 | |
|
850 | 852 | def isEndProcess(self): |
|
851 | 853 | |
|
852 | 854 | return self.flagNoMoreFiles |
|
853 | 855 | |
|
854 | 856 | def printReadBlocks(self): |
|
855 | 857 | |
|
856 | 858 | print "Number of read blocks per file %04d" %self.nReadBlocks |
|
857 | 859 | |
|
858 | 860 | def printTotalBlocks(self): |
|
859 | 861 | |
|
860 | 862 | print "Number of read blocks %04d" %self.nTotalBlocks |
|
861 | 863 | |
|
862 | 864 | def printInfo(self): |
|
863 | 865 | |
|
864 | 866 | print self.basicHeaderObj.printInfo() |
|
865 | 867 | print self.systemHeaderObj.printInfo() |
|
866 | 868 | print self.radarControllerHeaderObj.printInfo() |
|
867 | 869 | print self.processingHeaderObj.printInfo() |
|
868 | 870 | |
|
869 | 871 | |
|
870 | 872 | def run(self, **kwargs): |
|
871 | 873 | |
|
872 | 874 | if not(self.isConfig): |
|
873 | 875 | |
|
874 | 876 | # self.dataOut = dataOut |
|
875 | 877 | self.setup(**kwargs) |
|
876 | 878 | self.isConfig = True |
|
877 | 879 | |
|
878 | 880 | self.getData() |
|
879 | 881 | |
|
880 | 882 | class JRODataWriter(JRODataIO, Operation): |
|
881 | 883 | |
|
882 | 884 | """ |
|
883 | 885 | Esta clase permite escribir datos a archivos procesados (.r o ,pdata). La escritura |
|
884 | 886 | de los datos siempre se realiza por bloques. |
|
885 | 887 | """ |
|
886 | 888 | |
|
887 | 889 | blockIndex = 0 |
|
888 | 890 | |
|
889 | 891 | path = None |
|
890 | 892 | |
|
891 | 893 | setFile = None |
|
892 | 894 | |
|
893 | 895 | profilesPerBlock = None |
|
894 | 896 | |
|
895 | 897 | blocksPerFile = None |
|
896 | 898 | |
|
897 | 899 | nWriteBlocks = 0 |
|
898 | 900 | |
|
899 | 901 | def __init__(self, dataOut=None): |
|
900 | 902 | raise ValueError, "Not implemented" |
|
901 | 903 | |
|
902 | 904 | |
|
903 | 905 | def hasAllDataInBuffer(self): |
|
904 | 906 | raise ValueError, "Not implemented" |
|
905 | 907 | |
|
906 | 908 | |
|
907 | 909 | def setBlockDimension(self): |
|
908 | 910 | raise ValueError, "Not implemented" |
|
909 | 911 | |
|
910 | 912 | |
|
911 | 913 | def writeBlock(self): |
|
912 | 914 | raise ValueError, "No implemented" |
|
913 | 915 | |
|
914 | 916 | |
|
915 | 917 | def putData(self): |
|
916 | 918 | raise ValueError, "No implemented" |
|
917 | 919 | |
|
918 | 920 | def getDataHeader(self): |
|
919 | 921 | """ |
|
920 | 922 | Obtiene una copia del First Header |
|
921 | 923 | |
|
922 | 924 | Affected: |
|
923 | 925 | |
|
924 | 926 | self.basicHeaderObj |
|
925 | 927 | self.systemHeaderObj |
|
926 | 928 | self.radarControllerHeaderObj |
|
927 | 929 | self.processingHeaderObj self. |
|
928 | 930 | |
|
929 | 931 | Return: |
|
930 | 932 | None |
|
931 | 933 | """ |
|
932 | 934 | |
|
933 | 935 | raise ValueError, "No implemented" |
|
934 | 936 | |
|
935 | 937 | def getBasicHeader(self): |
|
936 | 938 | |
|
937 | 939 | self.basicHeaderObj.size = self.basicHeaderSize #bytes |
|
938 | 940 | self.basicHeaderObj.version = self.versionFile |
|
939 | 941 | self.basicHeaderObj.dataBlock = self.nTotalBlocks |
|
940 | 942 | |
|
941 | 943 | utc = numpy.floor(self.dataOut.utctime) |
|
942 | 944 | milisecond = (self.dataOut.utctime - utc)* 1000.0 |
|
943 | 945 | |
|
944 | 946 | self.basicHeaderObj.utc = utc |
|
945 | 947 | self.basicHeaderObj.miliSecond = milisecond |
|
946 | 948 | self.basicHeaderObj.timeZone = 0 |
|
947 | 949 | self.basicHeaderObj.dstFlag = 0 |
|
948 | 950 | self.basicHeaderObj.errorCount = 0 |
|
949 | 951 | |
|
950 | 952 | def __writeFirstHeader(self): |
|
951 | 953 | """ |
|
952 | 954 | Escribe el primer header del file es decir el Basic header y el Long header (SystemHeader, RadarControllerHeader, ProcessingHeader) |
|
953 | 955 | |
|
954 | 956 | Affected: |
|
955 | 957 | __dataType |
|
956 | 958 | |
|
957 | 959 | Return: |
|
958 | 960 | None |
|
959 | 961 | """ |
|
960 | 962 | |
|
961 | 963 | # CALCULAR PARAMETROS |
|
962 | 964 | |
|
963 | 965 | sizeLongHeader = self.systemHeaderObj.size + self.radarControllerHeaderObj.size + self.processingHeaderObj.size |
|
964 | 966 | self.basicHeaderObj.size = self.basicHeaderSize + sizeLongHeader |
|
965 | 967 | |
|
966 | 968 | self.basicHeaderObj.write(self.fp) |
|
967 | 969 | self.systemHeaderObj.write(self.fp) |
|
968 | 970 | self.radarControllerHeaderObj.write(self.fp) |
|
969 | 971 | self.processingHeaderObj.write(self.fp) |
|
970 | 972 | |
|
971 | 973 | self.dtype = self.dataOut.dtype |
|
972 | 974 | |
|
973 | 975 | def __setNewBlock(self): |
|
974 | 976 | """ |
|
975 | 977 | Si es un nuevo file escribe el First Header caso contrario escribe solo el Basic Header |
|
976 | 978 | |
|
977 | 979 | Return: |
|
978 | 980 | 0 : si no pudo escribir nada |
|
979 | 981 | 1 : Si escribio el Basic el First Header |
|
980 | 982 | """ |
|
981 | 983 | if self.fp == None: |
|
982 | 984 | self.setNextFile() |
|
983 | 985 | |
|
984 | 986 | if self.flagIsNewFile: |
|
985 | 987 | return 1 |
|
986 | 988 | |
|
987 | 989 | if self.blockIndex < self.processingHeaderObj.dataBlocksPerFile: |
|
988 | 990 | self.basicHeaderObj.write(self.fp) |
|
989 | 991 | return 1 |
|
990 | 992 | |
|
991 | 993 | if not( self.setNextFile() ): |
|
992 | 994 | return 0 |
|
993 | 995 | |
|
994 | 996 | return 1 |
|
995 | 997 | |
|
996 | 998 | |
|
997 | 999 | def writeNextBlock(self): |
|
998 | 1000 | """ |
|
999 | 1001 | Selecciona el bloque siguiente de datos y los escribe en un file |
|
1000 | 1002 | |
|
1001 | 1003 | Return: |
|
1002 | 1004 | 0 : Si no hizo pudo escribir el bloque de datos |
|
1003 | 1005 | 1 : Si no pudo escribir el bloque de datos |
|
1004 | 1006 | """ |
|
1005 | 1007 | if not( self.__setNewBlock() ): |
|
1006 | 1008 | return 0 |
|
1007 | 1009 | |
|
1008 | 1010 | self.writeBlock() |
|
1009 | 1011 | |
|
1010 | 1012 | return 1 |
|
1011 | 1013 | |
|
1012 | 1014 | def setNextFile(self): |
|
1013 | 1015 | """ |
|
1014 | 1016 | Determina el siguiente file que sera escrito |
|
1015 | 1017 | |
|
1016 | 1018 | Affected: |
|
1017 | 1019 | self.filename |
|
1018 | 1020 | self.subfolder |
|
1019 | 1021 | self.fp |
|
1020 | 1022 | self.setFile |
|
1021 | 1023 | self.flagIsNewFile |
|
1022 | 1024 | |
|
1023 | 1025 | Return: |
|
1024 | 1026 | 0 : Si el archivo no puede ser escrito |
|
1025 | 1027 | 1 : Si el archivo esta listo para ser escrito |
|
1026 | 1028 | """ |
|
1027 | 1029 | ext = self.ext |
|
1028 | 1030 | path = self.path |
|
1029 | 1031 | |
|
1030 | 1032 | if self.fp != None: |
|
1031 | 1033 | self.fp.close() |
|
1032 | 1034 | |
|
1033 | 1035 | timeTuple = time.localtime( self.dataOut.dataUtcTime) |
|
1034 | 1036 | subfolder = 'D%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday) |
|
1035 | 1037 | |
|
1036 | 1038 | fullpath = os.path.join( path, subfolder ) |
|
1037 | 1039 | if not( os.path.exists(fullpath) ): |
|
1038 | 1040 | os.mkdir(fullpath) |
|
1039 | 1041 | self.setFile = -1 #inicializo mi contador de seteo |
|
1040 | 1042 | else: |
|
1041 | 1043 | filesList = os.listdir( fullpath ) |
|
1042 | 1044 | if len( filesList ) > 0: |
|
1043 | 1045 | filesList = sorted( filesList, key=str.lower ) |
|
1044 | 1046 | filen = filesList[-1] |
|
1045 | 1047 | # el filename debera tener el siguiente formato |
|
1046 | 1048 | # 0 1234 567 89A BCDE (hex) |
|
1047 | 1049 | # x YYYY DDD SSS .ext |
|
1048 | 1050 | if isNumber( filen[8:11] ): |
|
1049 | 1051 | self.setFile = int( filen[8:11] ) #inicializo mi contador de seteo al seteo del ultimo file |
|
1050 | 1052 | else: |
|
1051 | 1053 | self.setFile = -1 |
|
1052 | 1054 | else: |
|
1053 | 1055 | self.setFile = -1 #inicializo mi contador de seteo |
|
1054 | 1056 | |
|
1055 | 1057 | setFile = self.setFile |
|
1056 | 1058 | setFile += 1 |
|
1057 | 1059 | |
|
1058 | 1060 | file = '%s%4.4d%3.3d%3.3d%s' % (self.optchar, |
|
1059 | 1061 | timeTuple.tm_year, |
|
1060 | 1062 | timeTuple.tm_yday, |
|
1061 | 1063 | setFile, |
|
1062 | 1064 | ext ) |
|
1063 | 1065 | |
|
1064 | 1066 | filename = os.path.join( path, subfolder, file ) |
|
1065 | 1067 | |
|
1066 | 1068 | fp = open( filename,'wb' ) |
|
1067 | 1069 | |
|
1068 | 1070 | self.blockIndex = 0 |
|
1069 | 1071 | |
|
1070 | 1072 | #guardando atributos |
|
1071 | 1073 | self.filename = filename |
|
1072 | 1074 | self.subfolder = subfolder |
|
1073 | 1075 | self.fp = fp |
|
1074 | 1076 | self.setFile = setFile |
|
1075 | 1077 | self.flagIsNewFile = 1 |
|
1076 | 1078 | |
|
1077 | 1079 | self.getDataHeader() |
|
1078 | 1080 | |
|
1079 | 1081 | print 'Writing the file: %s'%self.filename |
|
1080 | 1082 | |
|
1081 | 1083 | self.__writeFirstHeader() |
|
1082 | 1084 | |
|
1083 | 1085 | return 1 |
|
1084 | 1086 | |
|
1085 | 1087 | def setup(self, dataOut, path, blocksPerFile, profilesPerBlock=None, set=0, ext=None): |
|
1086 | 1088 | """ |
|
1087 | 1089 | Setea el tipo de formato en la cual sera guardada la data y escribe el First Header |
|
1088 | 1090 | |
|
1089 | 1091 | Inputs: |
|
1090 | 1092 | path : el path destino en el cual se escribiran los files a crear |
|
1091 | 1093 | format : formato en el cual sera salvado un file |
|
1092 | 1094 | set : el setebo del file |
|
1093 | 1095 | |
|
1094 | 1096 | Return: |
|
1095 | 1097 | 0 : Si no realizo un buen seteo |
|
1096 | 1098 | 1 : Si realizo un buen seteo |
|
1097 | 1099 | """ |
|
1098 | 1100 | |
|
1099 | 1101 | if ext == None: |
|
1100 | 1102 | ext = self.ext |
|
1101 | 1103 | |
|
1102 | 1104 | ext = ext.lower() |
|
1103 | 1105 | |
|
1104 | 1106 | self.ext = ext |
|
1105 | 1107 | |
|
1106 | 1108 | self.path = path |
|
1107 | 1109 | |
|
1108 | 1110 | self.setFile = set - 1 |
|
1109 | 1111 | |
|
1110 | 1112 | self.blocksPerFile = blocksPerFile |
|
1111 | 1113 | |
|
1112 | 1114 | self.profilesPerBlock = profilesPerBlock |
|
1113 | 1115 | |
|
1114 | 1116 | self.dataOut = dataOut |
|
1115 | 1117 | |
|
1116 | 1118 | if not(self.setNextFile()): |
|
1117 | 1119 | print "There isn't a next file" |
|
1118 | 1120 | return 0 |
|
1119 | 1121 | |
|
1120 | 1122 | self.setBlockDimension() |
|
1121 | 1123 | |
|
1122 | 1124 | return 1 |
|
1123 | 1125 | |
|
1124 | 1126 | def run(self, dataOut, **kwargs): |
|
1125 | 1127 | |
|
1126 | 1128 | if not(self.isConfig): |
|
1127 | 1129 | |
|
1128 | 1130 | self.setup(dataOut, **kwargs) |
|
1129 | 1131 | self.isConfig = True |
|
1130 | 1132 | |
|
1131 | 1133 | self.putData() |
|
1132 | 1134 | |
|
1133 | 1135 | class VoltageReader(JRODataReader): |
|
1134 | 1136 | """ |
|
1135 | 1137 | Esta clase permite leer datos de voltage desde archivos en formato rawdata (.r). La lectura |
|
1136 | 1138 | de los datos siempre se realiza por bloques. Los datos leidos (array de 3 dimensiones: |
|
1137 | 1139 | perfiles*alturas*canales) son almacenados en la variable "buffer". |
|
1138 | 1140 | |
|
1139 | 1141 | perfiles * alturas * canales |
|
1140 | 1142 | |
|
1141 | 1143 | Esta clase contiene instancias (objetos) de las clases BasicHeader, SystemHeader, |
|
1142 | 1144 | RadarControllerHeader y Voltage. Los tres primeros se usan para almacenar informacion de la |
|
1143 | 1145 | cabecera de datos (metadata), y el cuarto (Voltage) para obtener y almacenar un perfil de |
|
1144 | 1146 | datos desde el "buffer" cada vez que se ejecute el metodo "getData". |
|
1145 | 1147 | |
|
1146 | 1148 | Example: |
|
1147 | 1149 | |
|
1148 | 1150 | dpath = "/home/myuser/data" |
|
1149 | 1151 | |
|
1150 | 1152 | startTime = datetime.datetime(2010,1,20,0,0,0,0,0,0) |
|
1151 | 1153 | |
|
1152 | 1154 | endTime = datetime.datetime(2010,1,21,23,59,59,0,0,0) |
|
1153 | 1155 | |
|
1154 | 1156 | readerObj = VoltageReader() |
|
1155 | 1157 | |
|
1156 | 1158 | readerObj.setup(dpath, startTime, endTime) |
|
1157 | 1159 | |
|
1158 | 1160 | while(True): |
|
1159 | 1161 | |
|
1160 | 1162 | #to get one profile |
|
1161 | 1163 | profile = readerObj.getData() |
|
1162 | 1164 | |
|
1163 | 1165 | #print the profile |
|
1164 | 1166 | print profile |
|
1165 | 1167 | |
|
1166 | 1168 | #If you want to see all datablock |
|
1167 | 1169 | print readerObj.datablock |
|
1168 | 1170 | |
|
1169 | 1171 | if readerObj.flagNoMoreFiles: |
|
1170 | 1172 | break |
|
1171 | 1173 | |
|
1172 | 1174 | """ |
|
1173 | 1175 | |
|
1174 | 1176 | ext = ".r" |
|
1175 | 1177 | |
|
1176 | 1178 | optchar = "D" |
|
1177 | 1179 | dataOut = None |
|
1178 | 1180 | |
|
1179 | 1181 | |
|
1180 | 1182 | def __init__(self): |
|
1181 | 1183 | """ |
|
1182 | 1184 | Inicializador de la clase VoltageReader para la lectura de datos de voltage. |
|
1183 | 1185 | |
|
1184 | 1186 | Input: |
|
1185 | 1187 | dataOut : Objeto de la clase Voltage. Este objeto sera utilizado para |
|
1186 | 1188 | almacenar un perfil de datos cada vez que se haga un requerimiento |
|
1187 | 1189 | (getData). El perfil sera obtenido a partir del buffer de datos, |
|
1188 | 1190 | si el buffer esta vacio se hara un nuevo proceso de lectura de un |
|
1189 | 1191 | bloque de datos. |
|
1190 | 1192 | Si este parametro no es pasado se creara uno internamente. |
|
1191 | 1193 | |
|
1192 | 1194 | Variables afectadas: |
|
1193 | 1195 | self.dataOut |
|
1194 | 1196 | |
|
1195 | 1197 | Return: |
|
1196 | 1198 | None |
|
1197 | 1199 | """ |
|
1198 | 1200 | |
|
1199 | 1201 | self.isConfig = False |
|
1200 | 1202 | |
|
1201 | 1203 | self.datablock = None |
|
1202 | 1204 | |
|
1203 | 1205 | self.utc = 0 |
|
1204 | 1206 | |
|
1205 | 1207 | self.ext = ".r" |
|
1206 | 1208 | |
|
1207 | 1209 | self.optchar = "D" |
|
1208 | 1210 | |
|
1209 | 1211 | self.basicHeaderObj = BasicHeader(LOCALTIME) |
|
1210 | 1212 | |
|
1211 | 1213 | self.systemHeaderObj = SystemHeader() |
|
1212 | 1214 | |
|
1213 | 1215 | self.radarControllerHeaderObj = RadarControllerHeader() |
|
1214 | 1216 | |
|
1215 | 1217 | self.processingHeaderObj = ProcessingHeader() |
|
1216 | 1218 | |
|
1217 | 1219 | self.online = 0 |
|
1218 | 1220 | |
|
1219 | 1221 | self.fp = None |
|
1220 | 1222 | |
|
1221 | 1223 | self.idFile = None |
|
1222 | 1224 | |
|
1223 | 1225 | self.dtype = None |
|
1224 | 1226 | |
|
1225 | 1227 | self.fileSizeByHeader = None |
|
1226 | 1228 | |
|
1227 | 1229 | self.filenameList = [] |
|
1228 | 1230 | |
|
1229 | 1231 | self.filename = None |
|
1230 | 1232 | |
|
1231 | 1233 | self.fileSize = None |
|
1232 | 1234 | |
|
1233 | 1235 | self.firstHeaderSize = 0 |
|
1234 | 1236 | |
|
1235 | 1237 | self.basicHeaderSize = 24 |
|
1236 | 1238 | |
|
1237 | 1239 | self.pathList = [] |
|
1238 | 1240 | |
|
1239 | 1241 | self.filenameList = [] |
|
1240 | 1242 | |
|
1241 | 1243 | self.lastUTTime = 0 |
|
1242 | 1244 | |
|
1243 | 1245 | self.maxTimeStep = 30 |
|
1244 | 1246 | |
|
1245 | 1247 | self.flagNoMoreFiles = 0 |
|
1246 | 1248 | |
|
1247 | 1249 | self.set = 0 |
|
1248 | 1250 | |
|
1249 | 1251 | self.path = None |
|
1250 | 1252 | |
|
1251 | 1253 | self.profileIndex = 9999 |
|
1252 | 1254 | |
|
1253 | 1255 | self.delay = 3 #seconds |
|
1254 | 1256 | |
|
1255 | 1257 | self.nTries = 3 #quantity tries |
|
1256 | 1258 | |
|
1257 | 1259 | self.nFiles = 3 #number of files for searching |
|
1258 | 1260 | |
|
1259 | 1261 | self.nReadBlocks = 0 |
|
1260 | 1262 | |
|
1261 | 1263 | self.flagIsNewFile = 1 |
|
1262 | 1264 | |
|
1263 | 1265 | self.ippSeconds = 0 |
|
1264 | 1266 | |
|
1265 | 1267 | self.flagTimeBlock = 0 |
|
1266 | 1268 | |
|
1267 | 1269 | self.flagIsNewBlock = 0 |
|
1268 | 1270 | |
|
1269 | 1271 | self.nTotalBlocks = 0 |
|
1270 | 1272 | |
|
1271 | 1273 | self.blocksize = 0 |
|
1272 | 1274 | |
|
1273 | 1275 | self.dataOut = self.createObjByDefault() |
|
1274 | 1276 | |
|
1275 | 1277 | def createObjByDefault(self): |
|
1276 | 1278 | |
|
1277 | 1279 | dataObj = Voltage() |
|
1278 | 1280 | |
|
1279 | 1281 | return dataObj |
|
1280 | 1282 | |
|
1281 | 1283 | def __hasNotDataInBuffer(self): |
|
1282 | 1284 | if self.profileIndex >= self.processingHeaderObj.profilesPerBlock: |
|
1283 | 1285 | return 1 |
|
1284 | 1286 | return 0 |
|
1285 | 1287 | |
|
1286 | 1288 | |
|
1287 | 1289 | def getBlockDimension(self): |
|
1288 | 1290 | """ |
|
1289 | 1291 | Obtiene la cantidad de puntos a leer por cada bloque de datos |
|
1290 | 1292 | |
|
1291 | 1293 | Affected: |
|
1292 | 1294 | self.blocksize |
|
1293 | 1295 | |
|
1294 | 1296 | Return: |
|
1295 | 1297 | None |
|
1296 | 1298 | """ |
|
1297 | 1299 | pts2read = self.processingHeaderObj.profilesPerBlock * self.processingHeaderObj.nHeights * self.systemHeaderObj.nChannels |
|
1298 | 1300 | self.blocksize = pts2read |
|
1299 | 1301 | |
|
1300 | 1302 | |
|
1301 | 1303 | def readBlock(self): |
|
1302 | 1304 | """ |
|
1303 | 1305 | readBlock lee el bloque de datos desde la posicion actual del puntero del archivo |
|
1304 | 1306 | (self.fp) y actualiza todos los parametros relacionados al bloque de datos |
|
1305 | 1307 | (metadata + data). La data leida es almacenada en el buffer y el contador del buffer |
|
1306 | 1308 | es seteado a 0 |
|
1307 | 1309 | |
|
1308 | 1310 | Inputs: |
|
1309 | 1311 | None |
|
1310 | 1312 | |
|
1311 | 1313 | Return: |
|
1312 | 1314 | None |
|
1313 | 1315 | |
|
1314 | 1316 | Affected: |
|
1315 | 1317 | self.profileIndex |
|
1316 | 1318 | self.datablock |
|
1317 | 1319 | self.flagIsNewFile |
|
1318 | 1320 | self.flagIsNewBlock |
|
1319 | 1321 | self.nTotalBlocks |
|
1320 | 1322 | |
|
1321 | 1323 | Exceptions: |
|
1322 | 1324 | Si un bloque leido no es un bloque valido |
|
1323 | 1325 | """ |
|
1324 | 1326 | |
|
1325 | 1327 | junk = numpy.fromfile( self.fp, self.dtype, self.blocksize ) |
|
1326 | 1328 | |
|
1327 | 1329 | try: |
|
1328 | 1330 | junk = junk.reshape( (self.processingHeaderObj.profilesPerBlock, self.processingHeaderObj.nHeights, self.systemHeaderObj.nChannels) ) |
|
1329 | 1331 | except: |
|
1330 | 1332 | print "The read block (%3d) has not enough data" %self.nReadBlocks |
|
1331 | 1333 | return 0 |
|
1332 | 1334 | |
|
1333 | 1335 | junk = numpy.transpose(junk, (2,0,1)) |
|
1334 | 1336 | self.datablock = junk['real'] + junk['imag']*1j |
|
1335 | 1337 | |
|
1336 | 1338 | self.profileIndex = 0 |
|
1337 | 1339 | |
|
1338 | 1340 | self.flagIsNewFile = 0 |
|
1339 | 1341 | self.flagIsNewBlock = 1 |
|
1340 | 1342 | |
|
1341 | 1343 | self.nTotalBlocks += 1 |
|
1342 | 1344 | self.nReadBlocks += 1 |
|
1343 | 1345 | |
|
1344 | 1346 | return 1 |
|
1345 | 1347 | |
|
1346 | 1348 | |
|
1347 | 1349 | def getData(self): |
|
1348 | 1350 | """ |
|
1349 | 1351 | getData obtiene una unidad de datos del buffer de lectura y la copia a la clase "Voltage" |
|
1350 | 1352 | con todos los parametros asociados a este (metadata). cuando no hay datos en el buffer de |
|
1351 | 1353 | lectura es necesario hacer una nueva lectura de los bloques de datos usando "readNextBlock" |
|
1352 | 1354 | |
|
1353 | 1355 | Ademas incrementa el contador del buffer en 1. |
|
1354 | 1356 | |
|
1355 | 1357 | Return: |
|
1356 | 1358 | data : retorna un perfil de voltages (alturas * canales) copiados desde el |
|
1357 | 1359 | buffer. Si no hay mas archivos a leer retorna None. |
|
1358 | 1360 | |
|
1359 | 1361 | Variables afectadas: |
|
1360 | 1362 | self.dataOut |
|
1361 | 1363 | self.profileIndex |
|
1362 | 1364 | |
|
1363 | 1365 | Affected: |
|
1364 | 1366 | self.dataOut |
|
1365 | 1367 | self.profileIndex |
|
1366 | 1368 | self.flagTimeBlock |
|
1367 | 1369 | self.flagIsNewBlock |
|
1368 | 1370 | """ |
|
1369 | 1371 | |
|
1370 | 1372 | if self.flagNoMoreFiles: |
|
1371 | 1373 | self.dataOut.flagNoData = True |
|
1372 | 1374 | print 'Process finished' |
|
1373 | 1375 | return 0 |
|
1374 | 1376 | |
|
1375 | 1377 | self.flagTimeBlock = 0 |
|
1376 | 1378 | self.flagIsNewBlock = 0 |
|
1377 | 1379 | |
|
1378 | 1380 | if self.__hasNotDataInBuffer(): |
|
1379 | 1381 | |
|
1380 | 1382 | if not( self.readNextBlock() ): |
|
1381 | 1383 | return 0 |
|
1382 | 1384 | |
|
1383 | 1385 | self.dataOut.dtype = self.dtype |
|
1384 | 1386 | |
|
1385 | 1387 | self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock |
|
1386 | 1388 | |
|
1387 | 1389 | xf = self.processingHeaderObj.firstHeight + self.processingHeaderObj.nHeights*self.processingHeaderObj.deltaHeight |
|
1388 | 1390 | |
|
1389 | 1391 | self.dataOut.heightList = numpy.arange(self.processingHeaderObj.firstHeight, xf, self.processingHeaderObj.deltaHeight) |
|
1390 | 1392 | |
|
1391 | 1393 | self.dataOut.channelList = range(self.systemHeaderObj.nChannels) |
|
1392 | 1394 | |
|
1393 | 1395 | self.dataOut.flagTimeBlock = self.flagTimeBlock |
|
1394 | 1396 | |
|
1395 | 1397 | self.dataOut.ippSeconds = self.ippSeconds |
|
1396 | 1398 | |
|
1397 | 1399 | self.dataOut.timeInterval = self.ippSeconds * self.processingHeaderObj.nCohInt |
|
1398 | 1400 | |
|
1399 | 1401 | self.dataOut.nCohInt = self.processingHeaderObj.nCohInt |
|
1400 | 1402 | |
|
1401 | 1403 | self.dataOut.flagShiftFFT = False |
|
1402 | 1404 | |
|
1403 | 1405 | if self.radarControllerHeaderObj.code != None: |
|
1404 | 1406 | |
|
1405 | 1407 | self.dataOut.nCode = self.radarControllerHeaderObj.nCode |
|
1406 | 1408 | |
|
1407 | 1409 | self.dataOut.nBaud = self.radarControllerHeaderObj.nBaud |
|
1408 | 1410 | |
|
1409 | 1411 | self.dataOut.code = self.radarControllerHeaderObj.code |
|
1410 | 1412 | |
|
1411 | 1413 | self.dataOut.systemHeaderObj = self.systemHeaderObj.copy() |
|
1412 | 1414 | |
|
1413 | 1415 | self.dataOut.radarControllerHeaderObj = self.radarControllerHeaderObj.copy() |
|
1414 | 1416 | |
|
1415 | 1417 | self.dataOut.flagDecodeData = False #asumo q la data no esta decodificada |
|
1416 | 1418 | |
|
1417 | 1419 | self.dataOut.flagDeflipData = False #asumo q la data no esta sin flip |
|
1418 | 1420 | |
|
1419 | 1421 | self.dataOut.flagShiftFFT = False |
|
1420 | 1422 | |
|
1421 | 1423 | |
|
1422 | 1424 | # self.updateDataHeader() |
|
1423 | 1425 | |
|
1424 | 1426 | #data es un numpy array de 3 dmensiones (perfiles, alturas y canales) |
|
1425 | 1427 | |
|
1426 | 1428 | if self.datablock == None: |
|
1427 | 1429 | self.dataOut.flagNoData = True |
|
1428 | 1430 | return 0 |
|
1429 | 1431 | |
|
1430 | 1432 | self.dataOut.data = self.datablock[:,self.profileIndex,:] |
|
1431 | 1433 | |
|
1432 | 1434 | self.dataOut.utctime = self.basicHeaderObj.utc + self.basicHeaderObj.miliSecond/1000. + self.profileIndex * self.ippSeconds |
|
1433 | 1435 | |
|
1434 | 1436 | self.profileIndex += 1 |
|
1435 | 1437 | |
|
1436 | 1438 | self.dataOut.flagNoData = False |
|
1437 | 1439 | |
|
1438 | 1440 | # print self.profileIndex, self.dataOut.utctime |
|
1439 | 1441 | # if self.profileIndex == 800: |
|
1440 | 1442 | # a=1 |
|
1441 | 1443 | |
|
1442 | 1444 | |
|
1443 | 1445 | return self.dataOut.data |
|
1444 | 1446 | |
|
1445 | 1447 | |
|
1446 | 1448 | class VoltageWriter(JRODataWriter): |
|
1447 | 1449 | """ |
|
1448 | 1450 | Esta clase permite escribir datos de voltajes a archivos procesados (.r). La escritura |
|
1449 | 1451 | de los datos siempre se realiza por bloques. |
|
1450 | 1452 | """ |
|
1451 | 1453 | |
|
1452 | 1454 | ext = ".r" |
|
1453 | 1455 | |
|
1454 | 1456 | optchar = "D" |
|
1455 | 1457 | |
|
1456 | 1458 | shapeBuffer = None |
|
1457 | 1459 | |
|
1458 | 1460 | |
|
1459 | 1461 | def __init__(self): |
|
1460 | 1462 | """ |
|
1461 | 1463 | Inicializador de la clase VoltageWriter para la escritura de datos de espectros. |
|
1462 | 1464 | |
|
1463 | 1465 | Affected: |
|
1464 | 1466 | self.dataOut |
|
1465 | 1467 | |
|
1466 | 1468 | Return: None |
|
1467 | 1469 | """ |
|
1468 | 1470 | |
|
1469 | 1471 | self.nTotalBlocks = 0 |
|
1470 | 1472 | |
|
1471 | 1473 | self.profileIndex = 0 |
|
1472 | 1474 | |
|
1473 | 1475 | self.isConfig = False |
|
1474 | 1476 | |
|
1475 | 1477 | self.fp = None |
|
1476 | 1478 | |
|
1477 | 1479 | self.flagIsNewFile = 1 |
|
1478 | 1480 | |
|
1479 | 1481 | self.nTotalBlocks = 0 |
|
1480 | 1482 | |
|
1481 | 1483 | self.flagIsNewBlock = 0 |
|
1482 | 1484 | |
|
1483 | 1485 | self.setFile = None |
|
1484 | 1486 | |
|
1485 | 1487 | self.dtype = None |
|
1486 | 1488 | |
|
1487 | 1489 | self.path = None |
|
1488 | 1490 | |
|
1489 | 1491 | self.filename = None |
|
1490 | 1492 | |
|
1491 | 1493 | self.basicHeaderObj = BasicHeader(LOCALTIME) |
|
1492 | 1494 | |
|
1493 | 1495 | self.systemHeaderObj = SystemHeader() |
|
1494 | 1496 | |
|
1495 | 1497 | self.radarControllerHeaderObj = RadarControllerHeader() |
|
1496 | 1498 | |
|
1497 | 1499 | self.processingHeaderObj = ProcessingHeader() |
|
1498 | 1500 | |
|
1499 | 1501 | def hasAllDataInBuffer(self): |
|
1500 | 1502 | if self.profileIndex >= self.processingHeaderObj.profilesPerBlock: |
|
1501 | 1503 | return 1 |
|
1502 | 1504 | return 0 |
|
1503 | 1505 | |
|
1504 | 1506 | |
|
1505 | 1507 | def setBlockDimension(self): |
|
1506 | 1508 | """ |
|
1507 | 1509 | Obtiene las formas dimensionales del los subbloques de datos que componen un bloque |
|
1508 | 1510 | |
|
1509 | 1511 | Affected: |
|
1510 | 1512 | self.shape_spc_Buffer |
|
1511 | 1513 | self.shape_cspc_Buffer |
|
1512 | 1514 | self.shape_dc_Buffer |
|
1513 | 1515 | |
|
1514 | 1516 | Return: None |
|
1515 | 1517 | """ |
|
1516 | 1518 | self.shapeBuffer = (self.processingHeaderObj.profilesPerBlock, |
|
1517 | 1519 | self.processingHeaderObj.nHeights, |
|
1518 | 1520 | self.systemHeaderObj.nChannels) |
|
1519 | 1521 | |
|
1520 | 1522 | self.datablock = numpy.zeros((self.systemHeaderObj.nChannels, |
|
1521 | 1523 | self.processingHeaderObj.profilesPerBlock, |
|
1522 | 1524 | self.processingHeaderObj.nHeights), |
|
1523 | 1525 | dtype=numpy.dtype('complex')) |
|
1524 | 1526 | |
|
1525 | 1527 | |
|
1526 | 1528 | def writeBlock(self): |
|
1527 | 1529 | """ |
|
1528 | 1530 | Escribe el buffer en el file designado |
|
1529 | 1531 | |
|
1530 | 1532 | Affected: |
|
1531 | 1533 | self.profileIndex |
|
1532 | 1534 | self.flagIsNewFile |
|
1533 | 1535 | self.flagIsNewBlock |
|
1534 | 1536 | self.nTotalBlocks |
|
1535 | 1537 | self.blockIndex |
|
1536 | 1538 | |
|
1537 | 1539 | Return: None |
|
1538 | 1540 | """ |
|
1539 | 1541 | data = numpy.zeros( self.shapeBuffer, self.dtype ) |
|
1540 | 1542 | |
|
1541 | 1543 | junk = numpy.transpose(self.datablock, (1,2,0)) |
|
1542 | 1544 | |
|
1543 | 1545 | data['real'] = junk.real |
|
1544 | 1546 | data['imag'] = junk.imag |
|
1545 | 1547 | |
|
1546 | 1548 | data = data.reshape( (-1) ) |
|
1547 | 1549 | |
|
1548 | 1550 | data.tofile( self.fp ) |
|
1549 | 1551 | |
|
1550 | 1552 | self.datablock.fill(0) |
|
1551 | 1553 | |
|
1552 | 1554 | self.profileIndex = 0 |
|
1553 | 1555 | self.flagIsNewFile = 0 |
|
1554 | 1556 | self.flagIsNewBlock = 1 |
|
1555 | 1557 | |
|
1556 | 1558 | self.blockIndex += 1 |
|
1557 | 1559 | self.nTotalBlocks += 1 |
|
1558 | 1560 | |
|
1559 | 1561 | def putData(self): |
|
1560 | 1562 | """ |
|
1561 | 1563 | Setea un bloque de datos y luego los escribe en un file |
|
1562 | 1564 | |
|
1563 | 1565 | Affected: |
|
1564 | 1566 | self.flagIsNewBlock |
|
1565 | 1567 | self.profileIndex |
|
1566 | 1568 | |
|
1567 | 1569 | Return: |
|
1568 | 1570 | 0 : Si no hay data o no hay mas files que puedan escribirse |
|
1569 | 1571 | 1 : Si se escribio la data de un bloque en un file |
|
1570 | 1572 | """ |
|
1571 | 1573 | if self.dataOut.flagNoData: |
|
1572 | 1574 | return 0 |
|
1573 | 1575 | |
|
1574 | 1576 | self.flagIsNewBlock = 0 |
|
1575 | 1577 | |
|
1576 | 1578 | if self.dataOut.flagTimeBlock: |
|
1577 | 1579 | |
|
1578 | 1580 | self.datablock.fill(0) |
|
1579 | 1581 | self.profileIndex = 0 |
|
1580 | 1582 | self.setNextFile() |
|
1581 | 1583 | |
|
1582 | 1584 | if self.profileIndex == 0: |
|
1583 | 1585 | self.getBasicHeader() |
|
1584 | 1586 | |
|
1585 | 1587 | self.datablock[:,self.profileIndex,:] = self.dataOut.data |
|
1586 | 1588 | |
|
1587 | 1589 | self.profileIndex += 1 |
|
1588 | 1590 | |
|
1589 | 1591 | if self.hasAllDataInBuffer(): |
|
1590 | 1592 | #if self.flagIsNewFile: |
|
1591 | 1593 | self.writeNextBlock() |
|
1592 | 1594 | # self.getDataHeader() |
|
1593 | 1595 | |
|
1594 | 1596 | return 1 |
|
1595 | 1597 | |
|
1596 | 1598 | def __getProcessFlags(self): |
|
1597 | 1599 | |
|
1598 | 1600 | processFlags = 0 |
|
1599 | 1601 | |
|
1600 | 1602 | dtype0 = numpy.dtype([('real','<i1'),('imag','<i1')]) |
|
1601 | 1603 | dtype1 = numpy.dtype([('real','<i2'),('imag','<i2')]) |
|
1602 | 1604 | dtype2 = numpy.dtype([('real','<i4'),('imag','<i4')]) |
|
1603 | 1605 | dtype3 = numpy.dtype([('real','<i8'),('imag','<i8')]) |
|
1604 | 1606 | dtype4 = numpy.dtype([('real','<f4'),('imag','<f4')]) |
|
1605 | 1607 | dtype5 = numpy.dtype([('real','<f8'),('imag','<f8')]) |
|
1606 | 1608 | |
|
1607 | 1609 | dtypeList = [dtype0, dtype1, dtype2, dtype3, dtype4, dtype5] |
|
1608 | 1610 | |
|
1609 | 1611 | |
|
1610 | 1612 | |
|
1611 | 1613 | datatypeValueList = [PROCFLAG.DATATYPE_CHAR, |
|
1612 | 1614 | PROCFLAG.DATATYPE_SHORT, |
|
1613 | 1615 | PROCFLAG.DATATYPE_LONG, |
|
1614 | 1616 | PROCFLAG.DATATYPE_INT64, |
|
1615 | 1617 | PROCFLAG.DATATYPE_FLOAT, |
|
1616 | 1618 | PROCFLAG.DATATYPE_DOUBLE] |
|
1617 | 1619 | |
|
1618 | 1620 | |
|
1619 | 1621 | for index in range(len(dtypeList)): |
|
1620 | 1622 | if self.dataOut.dtype == dtypeList[index]: |
|
1621 | 1623 | dtypeValue = datatypeValueList[index] |
|
1622 | 1624 | break |
|
1623 | 1625 | |
|
1624 | 1626 | processFlags += dtypeValue |
|
1625 | 1627 | |
|
1626 | 1628 | if self.dataOut.flagDecodeData: |
|
1627 | 1629 | processFlags += PROCFLAG.DECODE_DATA |
|
1628 | 1630 | |
|
1629 | 1631 | if self.dataOut.flagDeflipData: |
|
1630 | 1632 | processFlags += PROCFLAG.DEFLIP_DATA |
|
1631 | 1633 | |
|
1632 | 1634 | if self.dataOut.code != None: |
|
1633 | 1635 | processFlags += PROCFLAG.DEFINE_PROCESS_CODE |
|
1634 | 1636 | |
|
1635 | 1637 | if self.dataOut.nCohInt > 1: |
|
1636 | 1638 | processFlags += PROCFLAG.COHERENT_INTEGRATION |
|
1637 | 1639 | |
|
1638 | 1640 | return processFlags |
|
1639 | 1641 | |
|
1640 | 1642 | |
|
1641 | 1643 | def __getBlockSize(self): |
|
1642 | 1644 | ''' |
|
1643 | 1645 | Este metodos determina el cantidad de bytes para un bloque de datos de tipo Voltage |
|
1644 | 1646 | ''' |
|
1645 | 1647 | |
|
1646 | 1648 | dtype0 = numpy.dtype([('real','<i1'),('imag','<i1')]) |
|
1647 | 1649 | dtype1 = numpy.dtype([('real','<i2'),('imag','<i2')]) |
|
1648 | 1650 | dtype2 = numpy.dtype([('real','<i4'),('imag','<i4')]) |
|
1649 | 1651 | dtype3 = numpy.dtype([('real','<i8'),('imag','<i8')]) |
|
1650 | 1652 | dtype4 = numpy.dtype([('real','<f4'),('imag','<f4')]) |
|
1651 | 1653 | dtype5 = numpy.dtype([('real','<f8'),('imag','<f8')]) |
|
1652 | 1654 | |
|
1653 | 1655 | dtypeList = [dtype0, dtype1, dtype2, dtype3, dtype4, dtype5] |
|
1654 | 1656 | datatypeValueList = [1,2,4,8,4,8] |
|
1655 | 1657 | for index in range(len(dtypeList)): |
|
1656 | 1658 | if self.dataOut.dtype == dtypeList[index]: |
|
1657 | 1659 | datatypeValue = datatypeValueList[index] |
|
1658 | 1660 | break |
|
1659 | 1661 | |
|
1660 | 1662 | blocksize = int(self.dataOut.nHeights * self.dataOut.nChannels * self.dataOut.nProfiles * datatypeValue * 2) |
|
1661 | 1663 | |
|
1662 | 1664 | return blocksize |
|
1663 | 1665 | |
|
1664 | 1666 | def getDataHeader(self): |
|
1665 | 1667 | |
|
1666 | 1668 | """ |
|
1667 | 1669 | Obtiene una copia del First Header |
|
1668 | 1670 | |
|
1669 | 1671 | Affected: |
|
1670 | 1672 | self.systemHeaderObj |
|
1671 | 1673 | self.radarControllerHeaderObj |
|
1672 | 1674 | self.dtype |
|
1673 | 1675 | |
|
1674 | 1676 | Return: |
|
1675 | 1677 | None |
|
1676 | 1678 | """ |
|
1677 | 1679 | |
|
1678 | 1680 | self.systemHeaderObj = self.dataOut.systemHeaderObj.copy() |
|
1679 | 1681 | self.systemHeaderObj.nChannels = self.dataOut.nChannels |
|
1680 | 1682 | self.radarControllerHeaderObj = self.dataOut.radarControllerHeaderObj.copy() |
|
1681 | 1683 | |
|
1682 | 1684 | self.getBasicHeader() |
|
1683 | 1685 | |
|
1684 | 1686 | processingHeaderSize = 40 # bytes |
|
1685 | 1687 | self.processingHeaderObj.dtype = 0 # Voltage |
|
1686 | 1688 | self.processingHeaderObj.blockSize = self.__getBlockSize() |
|
1687 | 1689 | self.processingHeaderObj.profilesPerBlock = self.profilesPerBlock |
|
1688 | 1690 | self.processingHeaderObj.dataBlocksPerFile = self.blocksPerFile |
|
1689 | 1691 | self.processingHeaderObj.nWindows = 1 #podria ser 1 o self.dataOut.processingHeaderObj.nWindows |
|
1690 | 1692 | self.processingHeaderObj.processFlags = self.__getProcessFlags() |
|
1691 | 1693 | self.processingHeaderObj.nCohInt = self.dataOut.nCohInt |
|
1692 | 1694 | self.processingHeaderObj.nIncohInt = 1 # Cuando la data de origen es de tipo Voltage |
|
1693 | 1695 | self.processingHeaderObj.totalSpectra = 0 # Cuando la data de origen es de tipo Voltage |
|
1694 | 1696 | |
|
1695 | 1697 | if self.dataOut.code != None: |
|
1696 | 1698 | self.processingHeaderObj.code = self.dataOut.code |
|
1697 | 1699 | self.processingHeaderObj.nCode = self.dataOut.nCode |
|
1698 | 1700 | self.processingHeaderObj.nBaud = self.dataOut.nBaud |
|
1699 | 1701 | codesize = int(8 + 4 * self.dataOut.nCode * self.dataOut.nBaud) |
|
1700 | 1702 | processingHeaderSize += codesize |
|
1701 | 1703 | |
|
1702 | 1704 | if self.processingHeaderObj.nWindows != 0: |
|
1703 | 1705 | self.processingHeaderObj.firstHeight = self.dataOut.heightList[0] |
|
1704 | 1706 | self.processingHeaderObj.deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0] |
|
1705 | 1707 | self.processingHeaderObj.nHeights = self.dataOut.nHeights |
|
1706 | 1708 | self.processingHeaderObj.samplesWin = self.dataOut.nHeights |
|
1707 | 1709 | processingHeaderSize += 12 |
|
1708 | 1710 | |
|
1709 | 1711 | self.processingHeaderObj.size = processingHeaderSize |
|
1710 | 1712 | |
|
1711 | 1713 | class SpectraReader(JRODataReader): |
|
1712 | 1714 | """ |
|
1713 | 1715 | Esta clase permite leer datos de espectros desde archivos procesados (.pdata). La lectura |
|
1714 | 1716 | de los datos siempre se realiza por bloques. Los datos leidos (array de 3 dimensiones) |
|
1715 | 1717 | son almacenados en tres buffer's para el Self Spectra, el Cross Spectra y el DC Channel. |
|
1716 | 1718 | |
|
1717 | 1719 | paresCanalesIguales * alturas * perfiles (Self Spectra) |
|
1718 | 1720 | paresCanalesDiferentes * alturas * perfiles (Cross Spectra) |
|
1719 | 1721 | canales * alturas (DC Channels) |
|
1720 | 1722 | |
|
1721 | 1723 | Esta clase contiene instancias (objetos) de las clases BasicHeader, SystemHeader, |
|
1722 | 1724 | RadarControllerHeader y Spectra. Los tres primeros se usan para almacenar informacion de la |
|
1723 | 1725 | cabecera de datos (metadata), y el cuarto (Spectra) para obtener y almacenar un bloque de |
|
1724 | 1726 | datos desde el "buffer" cada vez que se ejecute el metodo "getData". |
|
1725 | 1727 | |
|
1726 | 1728 | Example: |
|
1727 | 1729 | dpath = "/home/myuser/data" |
|
1728 | 1730 | |
|
1729 | 1731 | startTime = datetime.datetime(2010,1,20,0,0,0,0,0,0) |
|
1730 | 1732 | |
|
1731 | 1733 | endTime = datetime.datetime(2010,1,21,23,59,59,0,0,0) |
|
1732 | 1734 | |
|
1733 | 1735 | readerObj = SpectraReader() |
|
1734 | 1736 | |
|
1735 | 1737 | readerObj.setup(dpath, startTime, endTime) |
|
1736 | 1738 | |
|
1737 | 1739 | while(True): |
|
1738 | 1740 | |
|
1739 | 1741 | readerObj.getData() |
|
1740 | 1742 | |
|
1741 | 1743 | print readerObj.data_spc |
|
1742 | 1744 | |
|
1743 | 1745 | print readerObj.data_cspc |
|
1744 | 1746 | |
|
1745 | 1747 | print readerObj.data_dc |
|
1746 | 1748 | |
|
1747 | 1749 | if readerObj.flagNoMoreFiles: |
|
1748 | 1750 | break |
|
1749 | 1751 | |
|
1750 | 1752 | """ |
|
1751 | 1753 | |
|
1752 | 1754 | pts2read_SelfSpectra = 0 |
|
1753 | 1755 | |
|
1754 | 1756 | pts2read_CrossSpectra = 0 |
|
1755 | 1757 | |
|
1756 | 1758 | pts2read_DCchannels = 0 |
|
1757 | 1759 | |
|
1758 | 1760 | ext = ".pdata" |
|
1759 | 1761 | |
|
1760 | 1762 | optchar = "P" |
|
1761 | 1763 | |
|
1762 | 1764 | dataOut = None |
|
1763 | 1765 | |
|
1764 | 1766 | nRdChannels = None |
|
1765 | 1767 | |
|
1766 | 1768 | nRdPairs = None |
|
1767 | 1769 | |
|
1768 | 1770 | rdPairList = [] |
|
1769 | 1771 | |
|
1770 | 1772 | |
|
1771 | 1773 | def __init__(self): |
|
1772 | 1774 | """ |
|
1773 | 1775 | Inicializador de la clase SpectraReader para la lectura de datos de espectros. |
|
1774 | 1776 | |
|
1775 | 1777 | Inputs: |
|
1776 | 1778 | dataOut : Objeto de la clase Spectra. Este objeto sera utilizado para |
|
1777 | 1779 | almacenar un perfil de datos cada vez que se haga un requerimiento |
|
1778 | 1780 | (getData). El perfil sera obtenido a partir del buffer de datos, |
|
1779 | 1781 | si el buffer esta vacio se hara un nuevo proceso de lectura de un |
|
1780 | 1782 | bloque de datos. |
|
1781 | 1783 | Si este parametro no es pasado se creara uno internamente. |
|
1782 | 1784 | |
|
1783 | 1785 | Affected: |
|
1784 | 1786 | self.dataOut |
|
1785 | 1787 | |
|
1786 | 1788 | Return : None |
|
1787 | 1789 | """ |
|
1788 | 1790 | |
|
1789 | 1791 | self.isConfig = False |
|
1790 | 1792 | |
|
1791 | 1793 | self.pts2read_SelfSpectra = 0 |
|
1792 | 1794 | |
|
1793 | 1795 | self.pts2read_CrossSpectra = 0 |
|
1794 | 1796 | |
|
1795 | 1797 | self.pts2read_DCchannels = 0 |
|
1796 | 1798 | |
|
1797 | 1799 | self.datablock = None |
|
1798 | 1800 | |
|
1799 | 1801 | self.utc = None |
|
1800 | 1802 | |
|
1801 | 1803 | self.ext = ".pdata" |
|
1802 | 1804 | |
|
1803 | 1805 | self.optchar = "P" |
|
1804 | 1806 | |
|
1805 | 1807 | self.basicHeaderObj = BasicHeader(LOCALTIME) |
|
1806 | 1808 | |
|
1807 | 1809 | self.systemHeaderObj = SystemHeader() |
|
1808 | 1810 | |
|
1809 | 1811 | self.radarControllerHeaderObj = RadarControllerHeader() |
|
1810 | 1812 | |
|
1811 | 1813 | self.processingHeaderObj = ProcessingHeader() |
|
1812 | 1814 | |
|
1813 | 1815 | self.online = 0 |
|
1814 | 1816 | |
|
1815 | 1817 | self.fp = None |
|
1816 | 1818 | |
|
1817 | 1819 | self.idFile = None |
|
1818 | 1820 | |
|
1819 | 1821 | self.dtype = None |
|
1820 | 1822 | |
|
1821 | 1823 | self.fileSizeByHeader = None |
|
1822 | 1824 | |
|
1823 | 1825 | self.filenameList = [] |
|
1824 | 1826 | |
|
1825 | 1827 | self.filename = None |
|
1826 | 1828 | |
|
1827 | 1829 | self.fileSize = None |
|
1828 | 1830 | |
|
1829 | 1831 | self.firstHeaderSize = 0 |
|
1830 | 1832 | |
|
1831 | 1833 | self.basicHeaderSize = 24 |
|
1832 | 1834 | |
|
1833 | 1835 | self.pathList = [] |
|
1834 | 1836 | |
|
1835 | 1837 | self.lastUTTime = 0 |
|
1836 | 1838 | |
|
1837 | 1839 | self.maxTimeStep = 30 |
|
1838 | 1840 | |
|
1839 | 1841 | self.flagNoMoreFiles = 0 |
|
1840 | 1842 | |
|
1841 | 1843 | self.set = 0 |
|
1842 | 1844 | |
|
1843 | 1845 | self.path = None |
|
1844 | 1846 | |
|
1845 | 1847 | self.delay = 3 #seconds |
|
1846 | 1848 | |
|
1847 | 1849 | self.nTries = 3 #quantity tries |
|
1848 | 1850 | |
|
1849 | 1851 | self.nFiles = 3 #number of files for searching |
|
1850 | 1852 | |
|
1851 | 1853 | self.nReadBlocks = 0 |
|
1852 | 1854 | |
|
1853 | 1855 | self.flagIsNewFile = 1 |
|
1854 | 1856 | |
|
1855 | 1857 | self.ippSeconds = 0 |
|
1856 | 1858 | |
|
1857 | 1859 | self.flagTimeBlock = 0 |
|
1858 | 1860 | |
|
1859 | 1861 | self.flagIsNewBlock = 0 |
|
1860 | 1862 | |
|
1861 | 1863 | self.nTotalBlocks = 0 |
|
1862 | 1864 | |
|
1863 | 1865 | self.blocksize = 0 |
|
1864 | 1866 | |
|
1865 | 1867 | self.dataOut = self.createObjByDefault() |
|
1866 | 1868 | |
|
1867 | 1869 | |
|
1868 | 1870 | def createObjByDefault(self): |
|
1869 | 1871 | |
|
1870 | 1872 | dataObj = Spectra() |
|
1871 | 1873 | |
|
1872 | 1874 | return dataObj |
|
1873 | 1875 | |
|
1874 | 1876 | def __hasNotDataInBuffer(self): |
|
1875 | 1877 | return 1 |
|
1876 | 1878 | |
|
1877 | 1879 | |
|
1878 | 1880 | def getBlockDimension(self): |
|
1879 | 1881 | """ |
|
1880 | 1882 | Obtiene la cantidad de puntos a leer por cada bloque de datos |
|
1881 | 1883 | |
|
1882 | 1884 | Affected: |
|
1883 | 1885 | self.nRdChannels |
|
1884 | 1886 | self.nRdPairs |
|
1885 | 1887 | self.pts2read_SelfSpectra |
|
1886 | 1888 | self.pts2read_CrossSpectra |
|
1887 | 1889 | self.pts2read_DCchannels |
|
1888 | 1890 | self.blocksize |
|
1889 | 1891 | self.dataOut.nChannels |
|
1890 | 1892 | self.dataOut.nPairs |
|
1891 | 1893 | |
|
1892 | 1894 | Return: |
|
1893 | 1895 | None |
|
1894 | 1896 | """ |
|
1895 | 1897 | self.nRdChannels = 0 |
|
1896 | 1898 | self.nRdPairs = 0 |
|
1897 | 1899 | self.rdPairList = [] |
|
1898 | 1900 | |
|
1899 | 1901 | for i in range(0, self.processingHeaderObj.totalSpectra*2, 2): |
|
1900 | 1902 | if self.processingHeaderObj.spectraComb[i] == self.processingHeaderObj.spectraComb[i+1]: |
|
1901 | 1903 | self.nRdChannels = self.nRdChannels + 1 #par de canales iguales |
|
1902 | 1904 | else: |
|
1903 | 1905 | self.nRdPairs = self.nRdPairs + 1 #par de canales diferentes |
|
1904 | 1906 | self.rdPairList.append((self.processingHeaderObj.spectraComb[i], self.processingHeaderObj.spectraComb[i+1])) |
|
1905 | 1907 | |
|
1906 | 1908 | pts2read = self.processingHeaderObj.nHeights * self.processingHeaderObj.profilesPerBlock |
|
1907 | 1909 | |
|
1908 | 1910 | self.pts2read_SelfSpectra = int(self.nRdChannels * pts2read) |
|
1909 | 1911 | self.blocksize = self.pts2read_SelfSpectra |
|
1910 | 1912 | |
|
1911 | 1913 | if self.processingHeaderObj.flag_cspc: |
|
1912 | 1914 | self.pts2read_CrossSpectra = int(self.nRdPairs * pts2read) |
|
1913 | 1915 | self.blocksize += self.pts2read_CrossSpectra |
|
1914 | 1916 | |
|
1915 | 1917 | if self.processingHeaderObj.flag_dc: |
|
1916 | 1918 | self.pts2read_DCchannels = int(self.systemHeaderObj.nChannels * self.processingHeaderObj.nHeights) |
|
1917 | 1919 | self.blocksize += self.pts2read_DCchannels |
|
1918 | 1920 | |
|
1919 | 1921 | # self.blocksize = self.pts2read_SelfSpectra + self.pts2read_CrossSpectra + self.pts2read_DCchannels |
|
1920 | 1922 | |
|
1921 | 1923 | |
|
1922 | 1924 | def readBlock(self): |
|
1923 | 1925 | """ |
|
1924 | 1926 | Lee el bloque de datos desde la posicion actual del puntero del archivo |
|
1925 | 1927 | (self.fp) y actualiza todos los parametros relacionados al bloque de datos |
|
1926 | 1928 | (metadata + data). La data leida es almacenada en el buffer y el contador del buffer |
|
1927 | 1929 | es seteado a 0 |
|
1928 | 1930 | |
|
1929 | 1931 | Return: None |
|
1930 | 1932 | |
|
1931 | 1933 | Variables afectadas: |
|
1932 | 1934 | |
|
1933 | 1935 | self.flagIsNewFile |
|
1934 | 1936 | self.flagIsNewBlock |
|
1935 | 1937 | self.nTotalBlocks |
|
1936 | 1938 | self.data_spc |
|
1937 | 1939 | self.data_cspc |
|
1938 | 1940 | self.data_dc |
|
1939 | 1941 | |
|
1940 | 1942 | Exceptions: |
|
1941 | 1943 | Si un bloque leido no es un bloque valido |
|
1942 | 1944 | """ |
|
1943 | 1945 | blockOk_flag = False |
|
1944 | 1946 | fpointer = self.fp.tell() |
|
1945 | 1947 | |
|
1946 | 1948 | spc = numpy.fromfile( self.fp, self.dtype[0], self.pts2read_SelfSpectra ) |
|
1947 | 1949 | spc = spc.reshape( (self.nRdChannels, self.processingHeaderObj.nHeights, self.processingHeaderObj.profilesPerBlock) ) #transforma a un arreglo 3D |
|
1948 | 1950 | |
|
1949 | 1951 | if self.processingHeaderObj.flag_cspc: |
|
1950 | 1952 | cspc = numpy.fromfile( self.fp, self.dtype, self.pts2read_CrossSpectra ) |
|
1951 | 1953 | cspc = cspc.reshape( (self.nRdPairs, self.processingHeaderObj.nHeights, self.processingHeaderObj.profilesPerBlock) ) #transforma a un arreglo 3D |
|
1952 | 1954 | |
|
1953 | 1955 | if self.processingHeaderObj.flag_dc: |
|
1954 | 1956 | dc = numpy.fromfile( self.fp, self.dtype, self.pts2read_DCchannels ) #int(self.processingHeaderObj.nHeights*self.systemHeaderObj.nChannels) ) |
|
1955 | 1957 | dc = dc.reshape( (self.systemHeaderObj.nChannels, self.processingHeaderObj.nHeights) ) #transforma a un arreglo 2D |
|
1956 | 1958 | |
|
1957 | 1959 | |
|
1958 | 1960 | if not(self.processingHeaderObj.shif_fft): |
|
1959 | 1961 | #desplaza a la derecha en el eje 2 determinadas posiciones |
|
1960 | 1962 | shift = int(self.processingHeaderObj.profilesPerBlock/2) |
|
1961 | 1963 | spc = numpy.roll( spc, shift , axis=2 ) |
|
1962 | 1964 | |
|
1963 | 1965 | if self.processingHeaderObj.flag_cspc: |
|
1964 | 1966 | #desplaza a la derecha en el eje 2 determinadas posiciones |
|
1965 | 1967 | cspc = numpy.roll( cspc, shift, axis=2 ) |
|
1966 | 1968 | |
|
1967 | 1969 | |
|
1968 | 1970 | spc = numpy.transpose( spc, (0,2,1) ) |
|
1969 | 1971 | self.data_spc = spc |
|
1970 | 1972 | |
|
1971 | 1973 | if self.processingHeaderObj.flag_cspc: |
|
1972 | 1974 | cspc = numpy.transpose( cspc, (0,2,1) ) |
|
1973 | 1975 | self.data_cspc = cspc['real'] + cspc['imag']*1j |
|
1974 | 1976 | else: |
|
1975 | 1977 | self.data_cspc = None |
|
1976 | 1978 | |
|
1977 | 1979 | if self.processingHeaderObj.flag_dc: |
|
1978 | 1980 | self.data_dc = dc['real'] + dc['imag']*1j |
|
1979 | 1981 | else: |
|
1980 | 1982 | self.data_dc = None |
|
1981 | 1983 | |
|
1982 | 1984 | self.flagIsNewFile = 0 |
|
1983 | 1985 | self.flagIsNewBlock = 1 |
|
1984 | 1986 | |
|
1985 | 1987 | self.nTotalBlocks += 1 |
|
1986 | 1988 | self.nReadBlocks += 1 |
|
1987 | 1989 | |
|
1988 | 1990 | return 1 |
|
1989 | 1991 | |
|
1990 | 1992 | |
|
1991 | 1993 | def getData(self): |
|
1992 | 1994 | """ |
|
1993 | 1995 | Copia el buffer de lectura a la clase "Spectra", |
|
1994 | 1996 | con todos los parametros asociados a este (metadata). cuando no hay datos en el buffer de |
|
1995 | 1997 | lectura es necesario hacer una nueva lectura de los bloques de datos usando "readNextBlock" |
|
1996 | 1998 | |
|
1997 | 1999 | Return: |
|
1998 | 2000 | 0 : Si no hay mas archivos disponibles |
|
1999 | 2001 | 1 : Si hizo una buena copia del buffer |
|
2000 | 2002 | |
|
2001 | 2003 | Affected: |
|
2002 | 2004 | self.dataOut |
|
2003 | 2005 | |
|
2004 | 2006 | self.flagTimeBlock |
|
2005 | 2007 | self.flagIsNewBlock |
|
2006 | 2008 | """ |
|
2007 | 2009 | |
|
2008 | 2010 | if self.flagNoMoreFiles: |
|
2009 | 2011 | self.dataOut.flagNoData = True |
|
2010 | 2012 | print 'Process finished' |
|
2011 | 2013 | return 0 |
|
2012 | 2014 | |
|
2013 | 2015 | self.flagTimeBlock = 0 |
|
2014 | 2016 | self.flagIsNewBlock = 0 |
|
2015 | 2017 | |
|
2016 | 2018 | if self.__hasNotDataInBuffer(): |
|
2017 | 2019 | |
|
2018 | 2020 | if not( self.readNextBlock() ): |
|
2019 | 2021 | self.dataOut.flagNoData = True |
|
2020 | 2022 | return 0 |
|
2021 | 2023 | |
|
2022 | 2024 | # self.updateDataHeader() |
|
2023 | 2025 | |
|
2024 | 2026 | #data es un numpy array de 3 dmensiones (perfiles, alturas y canales) |
|
2025 | 2027 | |
|
2026 | 2028 | if self.data_dc == None: |
|
2027 | 2029 | self.dataOut.flagNoData = True |
|
2028 | 2030 | return 0 |
|
2029 | 2031 | |
|
2030 | 2032 | self.dataOut.data_spc = self.data_spc |
|
2031 | 2033 | |
|
2032 | 2034 | self.dataOut.data_cspc = self.data_cspc |
|
2033 | 2035 | |
|
2034 | 2036 | self.dataOut.data_dc = self.data_dc |
|
2035 | 2037 | |
|
2036 | 2038 | self.dataOut.flagTimeBlock = self.flagTimeBlock |
|
2037 | 2039 | |
|
2038 | 2040 | self.dataOut.flagNoData = False |
|
2039 | 2041 | |
|
2040 | 2042 | self.dataOut.dtype = self.dtype |
|
2041 | 2043 | |
|
2042 | 2044 | # self.dataOut.nChannels = self.nRdChannels |
|
2043 | 2045 | |
|
2044 | 2046 | self.dataOut.nPairs = self.nRdPairs |
|
2045 | 2047 | |
|
2046 | 2048 | self.dataOut.pairsList = self.rdPairList |
|
2047 | 2049 | |
|
2048 | 2050 | # self.dataOut.nHeights = self.processingHeaderObj.nHeights |
|
2049 | 2051 | |
|
2050 | 2052 | self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock |
|
2051 | 2053 | |
|
2052 | 2054 | self.dataOut.nFFTPoints = self.processingHeaderObj.profilesPerBlock |
|
2053 | 2055 | |
|
2054 | 2056 | self.dataOut.nCohInt = self.processingHeaderObj.nCohInt |
|
2055 | 2057 | |
|
2056 | 2058 | self.dataOut.nIncohInt = self.processingHeaderObj.nIncohInt |
|
2057 | 2059 | |
|
2058 | 2060 | xf = self.processingHeaderObj.firstHeight + self.processingHeaderObj.nHeights*self.processingHeaderObj.deltaHeight |
|
2059 | 2061 | |
|
2060 | 2062 | self.dataOut.heightList = numpy.arange(self.processingHeaderObj.firstHeight, xf, self.processingHeaderObj.deltaHeight) |
|
2061 | 2063 | |
|
2062 | 2064 | self.dataOut.channelList = range(self.systemHeaderObj.nChannels) |
|
2063 | 2065 | |
|
2064 | 2066 | # self.dataOut.channelIndexList = range(self.systemHeaderObj.nChannels) |
|
2065 | 2067 | |
|
2066 | 2068 | self.dataOut.utctime = self.basicHeaderObj.utc + self.basicHeaderObj.miliSecond/1000.#+ self.profileIndex * self.ippSeconds |
|
2067 | 2069 | |
|
2068 | 2070 | self.dataOut.ippSeconds = self.ippSeconds |
|
2069 | 2071 | |
|
2070 | 2072 | self.dataOut.timeInterval = self.ippSeconds * self.processingHeaderObj.nCohInt * self.processingHeaderObj.nIncohInt * self.dataOut.nFFTPoints |
|
2071 | 2073 | |
|
2072 | 2074 | # self.profileIndex += 1 |
|
2073 | 2075 | |
|
2074 | 2076 | self.dataOut.systemHeaderObj = self.systemHeaderObj.copy() |
|
2075 | 2077 | |
|
2076 | 2078 | self.dataOut.radarControllerHeaderObj = self.radarControllerHeaderObj.copy() |
|
2077 | 2079 | |
|
2078 | 2080 | self.dataOut.flagShiftFFT = self.processingHeaderObj.shif_fft |
|
2079 | 2081 | |
|
2080 |
self.dataOut.flagDecodeData = |
|
|
2082 | self.dataOut.flagDecodeData = False #asumo q la data no esta decodificada | |
|
2081 | 2083 | |
|
2082 | 2084 | self.dataOut.flagDeflipData = True #asumo q la data no esta sin flip |
|
2083 | 2085 |
|
|
2086 | if self.processingHeaderObj.code != None: | |
|
2087 | ||
|
2088 | self.dataOut.nCode = self.processingHeaderObj.nCode | |
|
2089 | ||
|
2090 | self.dataOut.nBaud = self.processingHeaderObj.nBaud | |
|
2091 | ||
|
2092 | self.dataOut.code = self.processingHeaderObj.code | |
|
2093 | ||
|
2094 | self.dataOut.flagDecodeData = True | |
|
2084 | 2095 | |
|
2085 | 2096 | return self.dataOut.data_spc |
|
2086 | 2097 | |
|
2087 | 2098 | |
|
2088 | 2099 | class SpectraWriter(JRODataWriter): |
|
2089 | 2100 | |
|
2090 | 2101 | """ |
|
2091 | 2102 | Esta clase permite escribir datos de espectros a archivos procesados (.pdata). La escritura |
|
2092 | 2103 | de los datos siempre se realiza por bloques. |
|
2093 | 2104 | """ |
|
2094 | 2105 | |
|
2095 | 2106 | ext = ".pdata" |
|
2096 | 2107 | |
|
2097 | 2108 | optchar = "P" |
|
2098 | 2109 | |
|
2099 | 2110 | shape_spc_Buffer = None |
|
2100 | 2111 | |
|
2101 | 2112 | shape_cspc_Buffer = None |
|
2102 | 2113 | |
|
2103 | 2114 | shape_dc_Buffer = None |
|
2104 | 2115 | |
|
2105 | 2116 | data_spc = None |
|
2106 | 2117 | |
|
2107 | 2118 | data_cspc = None |
|
2108 | 2119 | |
|
2109 | 2120 | data_dc = None |
|
2110 | 2121 | |
|
2111 | 2122 | # dataOut = None |
|
2112 | 2123 | |
|
2113 | 2124 | def __init__(self): |
|
2114 | 2125 | """ |
|
2115 | 2126 | Inicializador de la clase SpectraWriter para la escritura de datos de espectros. |
|
2116 | 2127 | |
|
2117 | 2128 | Affected: |
|
2118 | 2129 | self.dataOut |
|
2119 | 2130 | self.basicHeaderObj |
|
2120 | 2131 | self.systemHeaderObj |
|
2121 | 2132 | self.radarControllerHeaderObj |
|
2122 | 2133 | self.processingHeaderObj |
|
2123 | 2134 | |
|
2124 | 2135 | Return: None |
|
2125 | 2136 | """ |
|
2126 | 2137 | |
|
2127 | 2138 | self.isConfig = False |
|
2128 | 2139 | |
|
2129 | 2140 | self.nTotalBlocks = 0 |
|
2130 | 2141 | |
|
2131 | 2142 | self.data_spc = None |
|
2132 | 2143 | |
|
2133 | 2144 | self.data_cspc = None |
|
2134 | 2145 | |
|
2135 | 2146 | self.data_dc = None |
|
2136 | 2147 | |
|
2137 | 2148 | self.fp = None |
|
2138 | 2149 | |
|
2139 | 2150 | self.flagIsNewFile = 1 |
|
2140 | 2151 | |
|
2141 | 2152 | self.nTotalBlocks = 0 |
|
2142 | 2153 | |
|
2143 | 2154 | self.flagIsNewBlock = 0 |
|
2144 | 2155 | |
|
2145 | 2156 | self.setFile = None |
|
2146 | 2157 | |
|
2147 | 2158 | self.dtype = None |
|
2148 | 2159 | |
|
2149 | 2160 | self.path = None |
|
2150 | 2161 | |
|
2151 | 2162 | self.noMoreFiles = 0 |
|
2152 | 2163 | |
|
2153 | 2164 | self.filename = None |
|
2154 | 2165 | |
|
2155 | 2166 | self.basicHeaderObj = BasicHeader(LOCALTIME) |
|
2156 | 2167 | |
|
2157 | 2168 | self.systemHeaderObj = SystemHeader() |
|
2158 | 2169 | |
|
2159 | 2170 | self.radarControllerHeaderObj = RadarControllerHeader() |
|
2160 | 2171 | |
|
2161 | 2172 | self.processingHeaderObj = ProcessingHeader() |
|
2162 | 2173 | |
|
2163 | 2174 | |
|
2164 | 2175 | def hasAllDataInBuffer(self): |
|
2165 | 2176 | return 1 |
|
2166 | 2177 | |
|
2167 | 2178 | |
|
2168 | 2179 | def setBlockDimension(self): |
|
2169 | 2180 | """ |
|
2170 | 2181 | Obtiene las formas dimensionales del los subbloques de datos que componen un bloque |
|
2171 | 2182 | |
|
2172 | 2183 | Affected: |
|
2173 | 2184 | self.shape_spc_Buffer |
|
2174 | 2185 | self.shape_cspc_Buffer |
|
2175 | 2186 | self.shape_dc_Buffer |
|
2176 | 2187 | |
|
2177 | 2188 | Return: None |
|
2178 | 2189 | """ |
|
2179 | 2190 | self.shape_spc_Buffer = (self.dataOut.nChannels, |
|
2180 | 2191 | self.processingHeaderObj.nHeights, |
|
2181 | 2192 | self.processingHeaderObj.profilesPerBlock) |
|
2182 | 2193 | |
|
2183 | 2194 | self.shape_cspc_Buffer = (self.dataOut.nPairs, |
|
2184 | 2195 | self.processingHeaderObj.nHeights, |
|
2185 | 2196 | self.processingHeaderObj.profilesPerBlock) |
|
2186 | 2197 | |
|
2187 | 2198 | self.shape_dc_Buffer = (self.dataOut.nChannels, |
|
2188 | 2199 | self.processingHeaderObj.nHeights) |
|
2189 | 2200 | |
|
2190 | 2201 | |
|
2191 | 2202 | def writeBlock(self): |
|
2192 | 2203 | """ |
|
2193 | 2204 | Escribe el buffer en el file designado |
|
2194 | 2205 | |
|
2195 | 2206 | Affected: |
|
2196 | 2207 | self.data_spc |
|
2197 | 2208 | self.data_cspc |
|
2198 | 2209 | self.data_dc |
|
2199 | 2210 | self.flagIsNewFile |
|
2200 | 2211 | self.flagIsNewBlock |
|
2201 | 2212 | self.nTotalBlocks |
|
2202 | 2213 | self.nWriteBlocks |
|
2203 | 2214 | |
|
2204 | 2215 | Return: None |
|
2205 | 2216 | """ |
|
2206 | 2217 | |
|
2207 | 2218 | spc = numpy.transpose( self.data_spc, (0,2,1) ) |
|
2208 | 2219 | if not( self.processingHeaderObj.shif_fft ): |
|
2209 | 2220 | spc = numpy.roll( spc, self.processingHeaderObj.profilesPerBlock/2, axis=2 ) #desplaza a la derecha en el eje 2 determinadas posiciones |
|
2210 | 2221 | data = spc.reshape((-1)) |
|
2211 | 2222 | data.tofile(self.fp) |
|
2212 | 2223 | |
|
2213 | 2224 | if self.data_cspc != None: |
|
2214 | 2225 | data = numpy.zeros( self.shape_cspc_Buffer, self.dtype ) |
|
2215 | 2226 | cspc = numpy.transpose( self.data_cspc, (0,2,1) ) |
|
2216 | 2227 | if not( self.processingHeaderObj.shif_fft ): |
|
2217 | 2228 | cspc = numpy.roll( cspc, self.processingHeaderObj.profilesPerBlock/2, axis=2 ) #desplaza a la derecha en el eje 2 determinadas posiciones |
|
2218 | 2229 | data['real'] = cspc.real |
|
2219 | 2230 | data['imag'] = cspc.imag |
|
2220 | 2231 | data = data.reshape((-1)) |
|
2221 | 2232 | data.tofile(self.fp) |
|
2222 | 2233 | |
|
2223 | 2234 | if self.data_dc != None: |
|
2224 | 2235 | data = numpy.zeros( self.shape_dc_Buffer, self.dtype ) |
|
2225 | 2236 | dc = self.data_dc |
|
2226 | 2237 | data['real'] = dc.real |
|
2227 | 2238 | data['imag'] = dc.imag |
|
2228 | 2239 | data = data.reshape((-1)) |
|
2229 | 2240 | data.tofile(self.fp) |
|
2230 | 2241 | |
|
2231 | 2242 | self.data_spc.fill(0) |
|
2232 | 2243 | self.data_dc.fill(0) |
|
2233 | 2244 | if self.data_cspc != None: |
|
2234 | 2245 | self.data_cspc.fill(0) |
|
2235 | 2246 | |
|
2236 | 2247 | self.flagIsNewFile = 0 |
|
2237 | 2248 | self.flagIsNewBlock = 1 |
|
2238 | 2249 | self.nTotalBlocks += 1 |
|
2239 | 2250 | self.nWriteBlocks += 1 |
|
2240 | 2251 | self.blockIndex += 1 |
|
2241 | 2252 | |
|
2242 | 2253 | |
|
2243 | 2254 | def putData(self): |
|
2244 | 2255 | """ |
|
2245 | 2256 | Setea un bloque de datos y luego los escribe en un file |
|
2246 | 2257 | |
|
2247 | 2258 | Affected: |
|
2248 | 2259 | self.data_spc |
|
2249 | 2260 | self.data_cspc |
|
2250 | 2261 | self.data_dc |
|
2251 | 2262 | |
|
2252 | 2263 | Return: |
|
2253 | 2264 | 0 : Si no hay data o no hay mas files que puedan escribirse |
|
2254 | 2265 | 1 : Si se escribio la data de un bloque en un file |
|
2255 | 2266 | """ |
|
2256 | 2267 | |
|
2257 | 2268 | if self.dataOut.flagNoData: |
|
2258 | 2269 | return 0 |
|
2259 | 2270 | |
|
2260 | 2271 | self.flagIsNewBlock = 0 |
|
2261 | 2272 | |
|
2262 | 2273 | if self.dataOut.flagTimeBlock: |
|
2263 | 2274 | self.data_spc.fill(0) |
|
2264 | 2275 | self.data_cspc.fill(0) |
|
2265 | 2276 | self.data_dc.fill(0) |
|
2266 | 2277 | self.setNextFile() |
|
2267 | 2278 | |
|
2268 | 2279 | if self.flagIsNewFile == 0: |
|
2269 | 2280 | self.getBasicHeader() |
|
2270 | 2281 | |
|
2271 | 2282 | self.data_spc = self.dataOut.data_spc |
|
2272 | 2283 | self.data_cspc = self.dataOut.data_cspc |
|
2273 | 2284 | self.data_dc = self.dataOut.data_dc |
|
2274 | 2285 | |
|
2275 | 2286 | # #self.processingHeaderObj.dataBlocksPerFile) |
|
2276 | 2287 | if self.hasAllDataInBuffer(): |
|
2277 | 2288 | # self.getDataHeader() |
|
2278 | 2289 | self.writeNextBlock() |
|
2279 | 2290 | |
|
2280 | 2291 | return 1 |
|
2281 | 2292 | |
|
2282 | 2293 | |
|
2283 | 2294 | def __getProcessFlags(self): |
|
2284 | 2295 | |
|
2285 | 2296 | processFlags = 0 |
|
2286 | 2297 | |
|
2287 | 2298 | dtype0 = numpy.dtype([('real','<i1'),('imag','<i1')]) |
|
2288 | 2299 | dtype1 = numpy.dtype([('real','<i2'),('imag','<i2')]) |
|
2289 | 2300 | dtype2 = numpy.dtype([('real','<i4'),('imag','<i4')]) |
|
2290 | 2301 | dtype3 = numpy.dtype([('real','<i8'),('imag','<i8')]) |
|
2291 | 2302 | dtype4 = numpy.dtype([('real','<f4'),('imag','<f4')]) |
|
2292 | 2303 | dtype5 = numpy.dtype([('real','<f8'),('imag','<f8')]) |
|
2293 | 2304 | |
|
2294 | 2305 | dtypeList = [dtype0, dtype1, dtype2, dtype3, dtype4, dtype5] |
|
2295 | 2306 | |
|
2296 | 2307 | |
|
2297 | 2308 | |
|
2298 | 2309 | datatypeValueList = [PROCFLAG.DATATYPE_CHAR, |
|
2299 | 2310 | PROCFLAG.DATATYPE_SHORT, |
|
2300 | 2311 | PROCFLAG.DATATYPE_LONG, |
|
2301 | 2312 | PROCFLAG.DATATYPE_INT64, |
|
2302 | 2313 | PROCFLAG.DATATYPE_FLOAT, |
|
2303 | 2314 | PROCFLAG.DATATYPE_DOUBLE] |
|
2304 | 2315 | |
|
2305 | 2316 | |
|
2306 | 2317 | for index in range(len(dtypeList)): |
|
2307 | 2318 | if self.dataOut.dtype == dtypeList[index]: |
|
2308 | 2319 | dtypeValue = datatypeValueList[index] |
|
2309 | 2320 | break |
|
2310 | 2321 | |
|
2311 | 2322 | processFlags += dtypeValue |
|
2312 | 2323 | |
|
2313 | 2324 | if self.dataOut.flagDecodeData: |
|
2314 | 2325 | processFlags += PROCFLAG.DECODE_DATA |
|
2315 | 2326 | |
|
2316 | 2327 | if self.dataOut.flagDeflipData: |
|
2317 | 2328 | processFlags += PROCFLAG.DEFLIP_DATA |
|
2318 | 2329 | |
|
2319 | 2330 | if self.dataOut.code != None: |
|
2320 | 2331 | processFlags += PROCFLAG.DEFINE_PROCESS_CODE |
|
2321 | 2332 | |
|
2322 | 2333 | if self.dataOut.nIncohInt > 1: |
|
2323 | 2334 | processFlags += PROCFLAG.INCOHERENT_INTEGRATION |
|
2324 | 2335 | |
|
2325 | 2336 | if self.dataOut.data_dc != None: |
|
2326 | 2337 | processFlags += PROCFLAG.SAVE_CHANNELS_DC |
|
2327 | 2338 | |
|
2328 | 2339 | return processFlags |
|
2329 | 2340 | |
|
2330 | 2341 | |
|
2331 | 2342 | def __getBlockSize(self): |
|
2332 | 2343 | ''' |
|
2333 | 2344 | Este metodos determina el cantidad de bytes para un bloque de datos de tipo Spectra |
|
2334 | 2345 | ''' |
|
2335 | 2346 | |
|
2336 | 2347 | dtype0 = numpy.dtype([('real','<i1'),('imag','<i1')]) |
|
2337 | 2348 | dtype1 = numpy.dtype([('real','<i2'),('imag','<i2')]) |
|
2338 | 2349 | dtype2 = numpy.dtype([('real','<i4'),('imag','<i4')]) |
|
2339 | 2350 | dtype3 = numpy.dtype([('real','<i8'),('imag','<i8')]) |
|
2340 | 2351 | dtype4 = numpy.dtype([('real','<f4'),('imag','<f4')]) |
|
2341 | 2352 | dtype5 = numpy.dtype([('real','<f8'),('imag','<f8')]) |
|
2342 | 2353 | |
|
2343 | 2354 | dtypeList = [dtype0, dtype1, dtype2, dtype3, dtype4, dtype5] |
|
2344 | 2355 | datatypeValueList = [1,2,4,8,4,8] |
|
2345 | 2356 | for index in range(len(dtypeList)): |
|
2346 | 2357 | if self.dataOut.dtype == dtypeList[index]: |
|
2347 | 2358 | datatypeValue = datatypeValueList[index] |
|
2348 | 2359 | break |
|
2349 | 2360 | |
|
2350 | 2361 | |
|
2351 | 2362 | pts2write = self.dataOut.nHeights * self.dataOut.nFFTPoints |
|
2352 | 2363 | |
|
2353 | 2364 | pts2write_SelfSpectra = int(self.dataOut.nChannels * pts2write) |
|
2354 | 2365 | blocksize = (pts2write_SelfSpectra*datatypeValue) |
|
2355 | 2366 | |
|
2356 | 2367 | if self.dataOut.data_cspc != None: |
|
2357 | 2368 | pts2write_CrossSpectra = int(self.dataOut.nPairs * pts2write) |
|
2358 | 2369 | blocksize += (pts2write_CrossSpectra*datatypeValue*2) |
|
2359 | 2370 | |
|
2360 | 2371 | if self.dataOut.data_dc != None: |
|
2361 | 2372 | pts2write_DCchannels = int(self.dataOut.nChannels * self.dataOut.nHeights) |
|
2362 | 2373 | blocksize += (pts2write_DCchannels*datatypeValue*2) |
|
2363 | 2374 | |
|
2364 | 2375 | blocksize = blocksize #* datatypeValue * 2 #CORREGIR ESTO |
|
2365 | 2376 | |
|
2366 | 2377 | return blocksize |
|
2367 | 2378 | |
|
2368 | 2379 | def getDataHeader(self): |
|
2369 | 2380 | |
|
2370 | 2381 | """ |
|
2371 | 2382 | Obtiene una copia del First Header |
|
2372 | 2383 | |
|
2373 | 2384 | Affected: |
|
2374 | 2385 | self.systemHeaderObj |
|
2375 | 2386 | self.radarControllerHeaderObj |
|
2376 | 2387 | self.dtype |
|
2377 | 2388 | |
|
2378 | 2389 | Return: |
|
2379 | 2390 | None |
|
2380 | 2391 | """ |
|
2381 | 2392 | |
|
2382 | 2393 | self.systemHeaderObj = self.dataOut.systemHeaderObj.copy() |
|
2383 | 2394 | self.systemHeaderObj.nChannels = self.dataOut.nChannels |
|
2384 | 2395 | self.radarControllerHeaderObj = self.dataOut.radarControllerHeaderObj.copy() |
|
2385 | 2396 | |
|
2386 | 2397 | self.getBasicHeader() |
|
2387 | 2398 | |
|
2388 | 2399 | processingHeaderSize = 40 # bytes |
|
2389 | 2400 | self.processingHeaderObj.dtype = 0 # Voltage |
|
2390 | 2401 | self.processingHeaderObj.blockSize = self.__getBlockSize() |
|
2391 | 2402 | self.processingHeaderObj.profilesPerBlock = self.dataOut.nFFTPoints |
|
2392 | 2403 | self.processingHeaderObj.dataBlocksPerFile = self.blocksPerFile |
|
2393 | 2404 | self.processingHeaderObj.nWindows = 1 #podria ser 1 o self.dataOut.processingHeaderObj.nWindows |
|
2394 | 2405 | self.processingHeaderObj.processFlags = self.__getProcessFlags() |
|
2395 | 2406 | self.processingHeaderObj.nCohInt = self.dataOut.nCohInt# Se requiere para determinar el valor de timeInterval |
|
2396 | 2407 | self.processingHeaderObj.nIncohInt = self.dataOut.nIncohInt |
|
2397 | 2408 | self.processingHeaderObj.totalSpectra = self.dataOut.nPairs + self.dataOut.nChannels |
|
2398 | 2409 | |
|
2399 | 2410 | if self.processingHeaderObj.totalSpectra > 0: |
|
2400 | 2411 | channelList = [] |
|
2401 | 2412 | for channel in range(self.dataOut.nChannels): |
|
2402 | 2413 | channelList.append(channel) |
|
2403 | 2414 | channelList.append(channel) |
|
2404 | 2415 | |
|
2405 | 2416 | pairsList = [] |
|
2406 | 2417 | for pair in self.dataOut.pairsList: |
|
2407 | 2418 | pairsList.append(pair[0]) |
|
2408 | 2419 | pairsList.append(pair[1]) |
|
2409 | 2420 | spectraComb = channelList + pairsList |
|
2410 | 2421 | spectraComb = numpy.array(spectraComb,dtype="u1") |
|
2411 | 2422 | self.processingHeaderObj.spectraComb = spectraComb |
|
2412 | 2423 | sizeOfSpcComb = len(spectraComb) |
|
2413 | 2424 | processingHeaderSize += sizeOfSpcComb |
|
2414 | 2425 | |
|
2415 | 2426 | if self.dataOut.code != None: |
|
2416 | 2427 | self.processingHeaderObj.code = self.dataOut.code |
|
2417 | 2428 | self.processingHeaderObj.nCode = self.dataOut.nCode |
|
2418 | 2429 | self.processingHeaderObj.nBaud = self.dataOut.nBaud |
|
2419 | 2430 | nCodeSize = 4 # bytes |
|
2420 | 2431 | nBaudSize = 4 # bytes |
|
2421 | 2432 | codeSize = 4 # bytes |
|
2422 | 2433 | sizeOfCode = int(nCodeSize + nBaudSize + codeSize * self.dataOut.nCode * self.dataOut.nBaud) |
|
2423 | 2434 | processingHeaderSize += sizeOfCode |
|
2424 | 2435 | |
|
2425 | 2436 | if self.processingHeaderObj.nWindows != 0: |
|
2426 | 2437 | self.processingHeaderObj.firstHeight = self.dataOut.heightList[0] |
|
2427 | 2438 | self.processingHeaderObj.deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0] |
|
2428 | 2439 | self.processingHeaderObj.nHeights = self.dataOut.nHeights |
|
2429 | 2440 | self.processingHeaderObj.samplesWin = self.dataOut.nHeights |
|
2430 | 2441 | sizeOfFirstHeight = 4 |
|
2431 | 2442 | sizeOfdeltaHeight = 4 |
|
2432 | 2443 | sizeOfnHeights = 4 |
|
2433 | 2444 | sizeOfWindows = (sizeOfFirstHeight + sizeOfdeltaHeight + sizeOfnHeights)*self.processingHeaderObj.nWindows |
|
2434 | 2445 | processingHeaderSize += sizeOfWindows |
|
2435 | 2446 | |
|
2436 | 2447 | self.processingHeaderObj.size = processingHeaderSize |
|
2437 | 2448 | |
|
2438 | 2449 | class SpectraHeisWriter(): |
|
2439 | 2450 | |
|
2440 | 2451 | i=0 |
|
2441 | 2452 | |
|
2442 | 2453 | def __init__(self, dataOut): |
|
2443 | 2454 | |
|
2444 | 2455 | self.wrObj = FITS() |
|
2445 | 2456 | self.dataOut = dataOut |
|
2446 | 2457 | |
|
2447 | 2458 | def isNumber(str): |
|
2448 | 2459 | """ |
|
2449 | 2460 | Chequea si el conjunto de caracteres que componen un string puede ser convertidos a un numero. |
|
2450 | 2461 | |
|
2451 | 2462 | Excepciones: |
|
2452 | 2463 | Si un determinado string no puede ser convertido a numero |
|
2453 | 2464 | Input: |
|
2454 | 2465 | str, string al cual se le analiza para determinar si convertible a un numero o no |
|
2455 | 2466 | |
|
2456 | 2467 | Return: |
|
2457 | 2468 | True : si el string es uno numerico |
|
2458 | 2469 | False : no es un string numerico |
|
2459 | 2470 | """ |
|
2460 | 2471 | try: |
|
2461 | 2472 | float( str ) |
|
2462 | 2473 | return True |
|
2463 | 2474 | except: |
|
2464 | 2475 | return False |
|
2465 | 2476 | |
|
2466 | 2477 | def setup(self, wrpath,): |
|
2467 | 2478 | |
|
2468 | 2479 | if not(os.path.exists(wrpath)): |
|
2469 | 2480 | os.mkdir(wrpath) |
|
2470 | 2481 | |
|
2471 | 2482 | self.wrpath = wrpath |
|
2472 | 2483 | self.setFile = 0 |
|
2473 | 2484 | |
|
2474 | 2485 | def putData(self): |
|
2475 | 2486 | # self.wrObj.writeHeader(nChannels=self.dataOut.nChannels, nFFTPoints=self.dataOut.nFFTPoints) |
|
2476 | 2487 | #name = self.dataOut.utctime |
|
2477 | 2488 | name= time.localtime( self.dataOut.utctime) |
|
2478 | 2489 | ext=".fits" |
|
2479 | 2490 | #folder='D%4.4d%3.3d'%(name.tm_year,name.tm_yday) |
|
2480 | 2491 | subfolder = 'D%4.4d%3.3d' % (name.tm_year,name.tm_yday) |
|
2481 | 2492 | |
|
2482 | 2493 | fullpath = os.path.join( self.wrpath, subfolder ) |
|
2483 | 2494 | if not( os.path.exists(fullpath) ): |
|
2484 | 2495 | os.mkdir(fullpath) |
|
2485 | 2496 | self.setFile += 1 |
|
2486 | 2497 | file = 'D%4.4d%3.3d%3.3d%s' % (name.tm_year,name.tm_yday,self.setFile,ext) |
|
2487 | 2498 | |
|
2488 | 2499 | filename = os.path.join(self.wrpath,subfolder, file) |
|
2489 | 2500 | |
|
2490 | 2501 | # print self.dataOut.ippSeconds |
|
2491 | 2502 | freq=numpy.arange(-1*self.dataOut.nHeights/2.,self.dataOut.nHeights/2.)/(2*self.dataOut.ippSeconds) |
|
2492 | 2503 | |
|
2493 | 2504 | col1=self.wrObj.setColF(name="freq", format=str(self.dataOut.nFFTPoints)+'E', array=freq) |
|
2494 | 2505 | col2=self.wrObj.writeData(name="P_Ch1",format=str(self.dataOut.nFFTPoints)+'E',data=10*numpy.log10(self.dataOut.data_spc[0,:])) |
|
2495 | 2506 | col3=self.wrObj.writeData(name="P_Ch2",format=str(self.dataOut.nFFTPoints)+'E',data=10*numpy.log10(self.dataOut.data_spc[1,:])) |
|
2496 | 2507 | col4=self.wrObj.writeData(name="P_Ch3",format=str(self.dataOut.nFFTPoints)+'E',data=10*numpy.log10(self.dataOut.data_spc[2,:])) |
|
2497 | 2508 | col5=self.wrObj.writeData(name="P_Ch4",format=str(self.dataOut.nFFTPoints)+'E',data=10*numpy.log10(self.dataOut.data_spc[3,:])) |
|
2498 | 2509 | col6=self.wrObj.writeData(name="P_Ch5",format=str(self.dataOut.nFFTPoints)+'E',data=10*numpy.log10(self.dataOut.data_spc[4,:])) |
|
2499 | 2510 | col7=self.wrObj.writeData(name="P_Ch6",format=str(self.dataOut.nFFTPoints)+'E',data=10*numpy.log10(self.dataOut.data_spc[5,:])) |
|
2500 | 2511 | col8=self.wrObj.writeData(name="P_Ch7",format=str(self.dataOut.nFFTPoints)+'E',data=10*numpy.log10(self.dataOut.data_spc[6,:])) |
|
2501 | 2512 | col9=self.wrObj.writeData(name="P_Ch8",format=str(self.dataOut.nFFTPoints)+'E',data=10*numpy.log10(self.dataOut.data_spc[7,:])) |
|
2502 | 2513 | #n=numpy.arange((100)) |
|
2503 | 2514 | n=self.dataOut.data_spc[6,:] |
|
2504 | 2515 | a=self.wrObj.cFImage(n) |
|
2505 | 2516 | b=self.wrObj.Ctable(col1,col2,col3,col4,col5,col6,col7,col8,col9) |
|
2506 | 2517 | self.wrObj.CFile(a,b) |
|
2507 | 2518 | self.wrObj.wFile(filename) |
|
2508 | 2519 | return 1 |
|
2509 | 2520 | |
|
2510 | 2521 | class FITS: |
|
2511 | 2522 | |
|
2512 | 2523 | name=None |
|
2513 | 2524 | format=None |
|
2514 | 2525 | array =None |
|
2515 | 2526 | data =None |
|
2516 | 2527 | thdulist=None |
|
2517 | 2528 | |
|
2518 | 2529 | def __init__(self): |
|
2519 | 2530 | |
|
2520 | 2531 | pass |
|
2521 | 2532 | |
|
2522 | 2533 | def setColF(self,name,format,array): |
|
2523 | 2534 | self.name=name |
|
2524 | 2535 | self.format=format |
|
2525 | 2536 | self.array=array |
|
2526 | 2537 | a1=numpy.array([self.array],dtype=numpy.float32) |
|
2527 | 2538 | self.col1 = pyfits.Column(name=self.name, format=self.format, array=a1) |
|
2528 | 2539 | return self.col1 |
|
2529 | 2540 | |
|
2530 | 2541 | # def setColP(self,name,format,data): |
|
2531 | 2542 | # self.name=name |
|
2532 | 2543 | # self.format=format |
|
2533 | 2544 | # self.data=data |
|
2534 | 2545 | # a2=numpy.array([self.data],dtype=numpy.float32) |
|
2535 | 2546 | # self.col2 = pyfits.Column(name=self.name, format=self.format, array=a2) |
|
2536 | 2547 | # return self.col2 |
|
2537 | 2548 | |
|
2538 | 2549 | def writeHeader(self,): |
|
2539 | 2550 | pass |
|
2540 | 2551 | |
|
2541 | 2552 | def writeData(self,name,format,data): |
|
2542 | 2553 | self.name=name |
|
2543 | 2554 | self.format=format |
|
2544 | 2555 | self.data=data |
|
2545 | 2556 | a2=numpy.array([self.data],dtype=numpy.float32) |
|
2546 | 2557 | self.col2 = pyfits.Column(name=self.name, format=self.format, array=a2) |
|
2547 | 2558 | return self.col2 |
|
2548 | 2559 | |
|
2549 | 2560 | def cFImage(self,n): |
|
2550 | 2561 | self.hdu= pyfits.PrimaryHDU(n) |
|
2551 | 2562 | return self.hdu |
|
2552 | 2563 | |
|
2553 | 2564 | def Ctable(self,col1,col2,col3,col4,col5,col6,col7,col8,col9): |
|
2554 | 2565 | self.cols=pyfits.ColDefs( [col1,col2,col3,col4,col5,col6,col7,col8,col9]) |
|
2555 | 2566 | self.tbhdu = pyfits.new_table(self.cols) |
|
2556 | 2567 | return self.tbhdu |
|
2557 | 2568 | |
|
2558 | 2569 | def CFile(self,hdu,tbhdu): |
|
2559 | 2570 | self.thdulist=pyfits.HDUList([hdu,tbhdu]) |
|
2560 | 2571 | |
|
2561 | 2572 | def wFile(self,filename): |
|
2562 | 2573 | self.thdulist.writeto(filename) No newline at end of file |
@@ -1,1006 +1,1012 | |||
|
1 | 1 | import numpy |
|
2 | 2 | import time, datetime |
|
3 | 3 | from graphics.figure import * |
|
4 | 4 | |
|
5 | 5 | class CrossSpectraPlot(Figure): |
|
6 | 6 | |
|
7 | 7 | __isConfig = None |
|
8 | 8 | __nsubplots = None |
|
9 | 9 | |
|
10 | 10 | WIDTH = None |
|
11 | 11 | HEIGHT = None |
|
12 | 12 | WIDTHPROF = None |
|
13 | 13 | HEIGHTPROF = None |
|
14 | 14 | PREFIX = 'cspc' |
|
15 | 15 | |
|
16 | 16 | def __init__(self): |
|
17 | 17 | |
|
18 | 18 | self.__isConfig = False |
|
19 | 19 | self.__nsubplots = 4 |
|
20 | 20 | |
|
21 | 21 | self.WIDTH = 250 |
|
22 | 22 | self.HEIGHT = 250 |
|
23 | 23 | self.WIDTHPROF = 0 |
|
24 | 24 | self.HEIGHTPROF = 0 |
|
25 | 25 | |
|
26 | 26 | def getSubplots(self): |
|
27 | 27 | |
|
28 | 28 | ncol = 4 |
|
29 | 29 | nrow = self.nplots |
|
30 | 30 | |
|
31 | 31 | return nrow, ncol |
|
32 | 32 | |
|
33 | 33 | def setup(self, idfigure, nplots, wintitle, showprofile=True): |
|
34 | 34 | |
|
35 | 35 | self.__showprofile = showprofile |
|
36 | 36 | self.nplots = nplots |
|
37 | 37 | |
|
38 | 38 | ncolspan = 1 |
|
39 | 39 | colspan = 1 |
|
40 | 40 | |
|
41 | 41 | self.createFigure(idfigure = idfigure, |
|
42 | 42 | wintitle = wintitle, |
|
43 | 43 | widthplot = self.WIDTH + self.WIDTHPROF, |
|
44 | 44 | heightplot = self.HEIGHT + self.HEIGHTPROF) |
|
45 | 45 | |
|
46 | 46 | nrow, ncol = self.getSubplots() |
|
47 | 47 | |
|
48 | 48 | counter = 0 |
|
49 | 49 | for y in range(nrow): |
|
50 | 50 | for x in range(ncol): |
|
51 | 51 | self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1) |
|
52 | 52 | |
|
53 | 53 | counter += 1 |
|
54 | 54 | |
|
55 | 55 | def run(self, dataOut, idfigure, wintitle="", pairsList=None, showprofile='True', |
|
56 | 56 | xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None, |
|
57 |
save=False, figpath='./', figfile=None |
|
|
57 | save=False, figpath='./', figfile=None, | |
|
58 | power_cmap='jet', coherence_cmap='jet', phase_cmap='RdBu_r'): | |
|
58 | 59 | |
|
59 | 60 | """ |
|
60 | 61 | |
|
61 | 62 | Input: |
|
62 | 63 | dataOut : |
|
63 | 64 | idfigure : |
|
64 | 65 | wintitle : |
|
65 | 66 | channelList : |
|
66 | 67 | showProfile : |
|
67 | 68 | xmin : None, |
|
68 | 69 | xmax : None, |
|
69 | 70 | ymin : None, |
|
70 | 71 | ymax : None, |
|
71 | 72 | zmin : None, |
|
72 | 73 | zmax : None |
|
73 | 74 | """ |
|
74 | 75 | |
|
75 | 76 | if pairsList == None: |
|
76 | 77 | pairsIndexList = dataOut.pairsIndexList |
|
77 | 78 | else: |
|
78 | 79 | pairsIndexList = [] |
|
79 | 80 | for pair in pairsList: |
|
80 | 81 | if pair not in dataOut.pairsList: |
|
81 | 82 | raise ValueError, "Pair %s is not in dataOut.pairsList" %(pair) |
|
82 | 83 | pairsIndexList.append(dataOut.pairsList.index(pair)) |
|
83 | 84 | |
|
84 | 85 | if pairsIndexList == []: |
|
85 | 86 | return |
|
86 | 87 | |
|
87 | 88 | if len(pairsIndexList) > 4: |
|
88 | 89 | pairsIndexList = pairsIndexList[0:4] |
|
89 | 90 | factor = dataOut.normFactor |
|
90 | 91 | x = dataOut.getVelRange(1) |
|
91 | 92 | y = dataOut.getHeiRange() |
|
92 | 93 | z = dataOut.data_spc[:,:,:]/factor |
|
93 | z = numpy.where(numpy.isfinite(z), z, numpy.NAN) | |
|
94 |
avg = numpy.a |
|
|
94 | # z = numpy.where(numpy.isfinite(z), z, numpy.NAN) | |
|
95 | avg = numpy.abs(numpy.average(z, axis=1)) | |
|
95 | 96 | noise = dataOut.getNoise()/factor |
|
96 | 97 | |
|
97 | 98 | zdB = 10*numpy.log10(z) |
|
98 | 99 | avgdB = 10*numpy.log10(avg) |
|
99 | 100 | noisedB = 10*numpy.log10(noise) |
|
100 | 101 | |
|
101 | 102 | |
|
102 | 103 | thisDatetime = dataOut.datatime |
|
103 | 104 | title = "Cross-Spectra: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S")) |
|
104 | 105 | xlabel = "Velocity (m/s)" |
|
105 | 106 | ylabel = "Range (Km)" |
|
106 | 107 | |
|
107 | 108 | if not self.__isConfig: |
|
108 | 109 | |
|
109 | 110 | nplots = len(pairsIndexList) |
|
110 | 111 | |
|
111 | 112 | self.setup(idfigure=idfigure, |
|
112 | 113 | nplots=nplots, |
|
113 | 114 | wintitle=wintitle, |
|
114 | 115 | showprofile=showprofile) |
|
115 | 116 | |
|
116 | 117 | if xmin == None: xmin = numpy.nanmin(x) |
|
117 | 118 | if xmax == None: xmax = numpy.nanmax(x) |
|
118 | 119 | if ymin == None: ymin = numpy.nanmin(y) |
|
119 | 120 | if ymax == None: ymax = numpy.nanmax(y) |
|
120 | 121 | if zmin == None: zmin = numpy.nanmin(avgdB)*0.9 |
|
121 | 122 | if zmax == None: zmax = numpy.nanmax(avgdB)*0.9 |
|
122 | 123 | |
|
123 | 124 | self.__isConfig = True |
|
124 | 125 | |
|
125 | 126 | self.setWinTitle(title) |
|
126 | 127 | |
|
127 | 128 | for i in range(self.nplots): |
|
128 | 129 | pair = dataOut.pairsList[pairsIndexList[i]] |
|
129 | 130 | |
|
130 | 131 | title = "Channel %d: %4.2fdB" %(pair[0], noisedB[pair[0]]) |
|
131 | 132 | zdB = 10.*numpy.log10(dataOut.data_spc[pair[0],:,:]/factor) |
|
132 | 133 | axes0 = self.axesList[i*self.__nsubplots] |
|
133 | 134 | axes0.pcolor(x, y, zdB, |
|
134 | 135 | xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax, |
|
135 | 136 | xlabel=xlabel, ylabel=ylabel, title=title, |
|
136 | ticksize=9, cblabel='') | |
|
137 | ticksize=9, colormap=power_cmap, cblabel='') | |
|
137 | 138 | |
|
138 | 139 | title = "Channel %d: %4.2fdB" %(pair[1], noisedB[pair[1]]) |
|
139 | 140 | zdB = 10.*numpy.log10(dataOut.data_spc[pair[1],:,:]/factor) |
|
140 | 141 | axes0 = self.axesList[i*self.__nsubplots+1] |
|
141 | 142 | axes0.pcolor(x, y, zdB, |
|
142 | 143 | xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax, |
|
143 | 144 | xlabel=xlabel, ylabel=ylabel, title=title, |
|
144 | ticksize=9, cblabel='') | |
|
145 | ticksize=9, colormap=power_cmap, cblabel='') | |
|
145 | 146 | |
|
146 | 147 | coherenceComplex = dataOut.data_cspc[pairsIndexList[i],:,:]/numpy.sqrt(dataOut.data_spc[pair[0],:,:]*dataOut.data_spc[pair[1],:,:]) |
|
147 | 148 | coherence = numpy.abs(coherenceComplex) |
|
148 | phase = numpy.arctan(-1*coherenceComplex.imag/coherenceComplex.real)*180/numpy.pi | |
|
149 | ||
|
149 | # phase = numpy.arctan(-1*coherenceComplex.imag/coherenceComplex.real)*180/numpy.pi | |
|
150 | phase = numpy.arctan2(coherenceComplex.imag, coherenceComplex.real)*180/numpy.pi | |
|
150 | 151 | |
|
151 | 152 | title = "Coherence %d%d" %(pair[0], pair[1]) |
|
152 | 153 | axes0 = self.axesList[i*self.__nsubplots+2] |
|
153 | 154 | axes0.pcolor(x, y, coherence, |
|
154 | 155 | xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=0, zmax=1, |
|
155 | 156 | xlabel=xlabel, ylabel=ylabel, title=title, |
|
156 | ticksize=9, cblabel='') | |
|
157 | ticksize=9, colormap=coherence_cmap, cblabel='') | |
|
157 | 158 | |
|
158 | 159 | title = "Phase %d%d" %(pair[0], pair[1]) |
|
159 | 160 | axes0 = self.axesList[i*self.__nsubplots+3] |
|
160 | 161 | axes0.pcolor(x, y, phase, |
|
161 | 162 | xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=-180, zmax=180, |
|
162 | 163 | xlabel=xlabel, ylabel=ylabel, title=title, |
|
163 |
ticksize=9, cblabel='' |
|
|
164 | ticksize=9, colormap=phase_cmap, cblabel='') | |
|
164 | 165 | |
|
165 | 166 | |
|
166 | 167 | |
|
167 | 168 | self.draw() |
|
168 | 169 | |
|
169 | 170 | if save: |
|
170 | 171 | date = thisDatetime.strftime("%Y%m%d_%H%M%S") |
|
171 | 172 | if figfile == None: |
|
172 | 173 | figfile = self.getFilename(name = date) |
|
173 | 174 | |
|
174 | 175 | self.saveFigure(figpath, figfile) |
|
175 | 176 | |
|
176 | 177 | |
|
177 | 178 | class RTIPlot(Figure): |
|
178 | 179 | |
|
179 | 180 | __isConfig = None |
|
180 | 181 | __nsubplots = None |
|
181 | 182 | __missing = 1E30 |
|
182 | 183 | WIDTHPROF = None |
|
183 | 184 | HEIGHTPROF = None |
|
184 | 185 | PREFIX = 'rti' |
|
185 | 186 | |
|
186 | 187 | def __init__(self): |
|
187 | 188 | |
|
188 | 189 | self.timerange = 2*60*60 |
|
189 | 190 | self.__isConfig = False |
|
190 | 191 | self.__nsubplots = 1 |
|
191 | 192 | |
|
192 | 193 | self.WIDTH = 800 |
|
193 | 194 | self.HEIGHT = 200 |
|
194 | 195 | self.WIDTHPROF = 120 |
|
195 | 196 | self.HEIGHTPROF = 0 |
|
196 | 197 | self.x_buffer = None |
|
197 | 198 | self.avgdB_buffer = None |
|
198 | 199 | |
|
199 | 200 | def getSubplots(self): |
|
200 | 201 | |
|
201 | 202 | ncol = 1 |
|
202 | 203 | nrow = self.nplots |
|
203 | 204 | |
|
204 | 205 | return nrow, ncol |
|
205 | 206 | |
|
206 | 207 | def setup(self, idfigure, nplots, wintitle, showprofile=True): |
|
207 | 208 | |
|
208 | 209 | self.__showprofile = showprofile |
|
209 | 210 | self.nplots = nplots |
|
210 | 211 | |
|
211 | 212 | ncolspan = 1 |
|
212 | 213 | colspan = 1 |
|
213 | 214 | if showprofile: |
|
214 | 215 | ncolspan = 7 |
|
215 | 216 | colspan = 6 |
|
216 | 217 | self.__nsubplots = 2 |
|
217 | 218 | |
|
218 | 219 | self.createFigure(idfigure = idfigure, |
|
219 | 220 | wintitle = wintitle, |
|
220 | 221 | widthplot = self.WIDTH + self.WIDTHPROF, |
|
221 | 222 | heightplot = self.HEIGHT + self.HEIGHTPROF) |
|
222 | 223 | |
|
223 | 224 | nrow, ncol = self.getSubplots() |
|
224 | 225 | |
|
225 | 226 | counter = 0 |
|
226 | 227 | for y in range(nrow): |
|
227 | 228 | for x in range(ncol): |
|
228 | 229 | |
|
229 | 230 | if counter >= self.nplots: |
|
230 | 231 | break |
|
231 | 232 | |
|
232 | 233 | self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1) |
|
233 | 234 | |
|
234 | 235 | if showprofile: |
|
235 | 236 | self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1) |
|
236 | 237 | |
|
237 | 238 | counter += 1 |
|
238 | 239 | |
|
239 | 240 | def run(self, dataOut, idfigure, wintitle="", channelList=None, showprofile='True', |
|
240 | 241 | xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None, |
|
241 | 242 | timerange=None, |
|
242 | 243 | save=False, figpath='./', figfile=None): |
|
243 | 244 | |
|
244 | 245 | """ |
|
245 | 246 | |
|
246 | 247 | Input: |
|
247 | 248 | dataOut : |
|
248 | 249 | idfigure : |
|
249 | 250 | wintitle : |
|
250 | 251 | channelList : |
|
251 | 252 | showProfile : |
|
252 | 253 | xmin : None, |
|
253 | 254 | xmax : None, |
|
254 | 255 | ymin : None, |
|
255 | 256 | ymax : None, |
|
256 | 257 | zmin : None, |
|
257 | 258 | zmax : None |
|
258 | 259 | """ |
|
259 | 260 | |
|
260 | 261 | if channelList == None: |
|
261 | 262 | channelIndexList = dataOut.channelIndexList |
|
262 | 263 | else: |
|
263 | 264 | channelIndexList = [] |
|
264 | 265 | for channel in channelList: |
|
265 | 266 | if channel not in dataOut.channelList: |
|
266 | 267 | raise ValueError, "Channel %d is not in dataOut.channelList" |
|
267 | 268 | channelIndexList.append(dataOut.channelList.index(channel)) |
|
268 | 269 | |
|
269 | 270 | if timerange != None: |
|
270 | 271 | self.timerange = timerange |
|
271 | 272 | |
|
272 | 273 | tmin = None |
|
273 | 274 | tmax = None |
|
274 | 275 | factor = dataOut.normFactor |
|
275 | 276 | x = dataOut.getTimeRange() |
|
276 | 277 | y = dataOut.getHeiRange() |
|
277 | 278 | |
|
278 | 279 | z = dataOut.data_spc[channelIndexList,:,:]/factor |
|
279 | 280 | z = numpy.where(numpy.isfinite(z), z, numpy.NAN) |
|
280 | 281 | avg = numpy.average(z, axis=1) |
|
281 | 282 | |
|
282 | 283 | avgdB = 10.*numpy.log10(avg) |
|
283 | 284 | |
|
284 | 285 | |
|
285 | 286 | thisDatetime = dataOut.datatime |
|
286 | 287 | title = "RTI: %s" %(thisDatetime.strftime("%d-%b-%Y")) |
|
287 | 288 | xlabel = "Velocity (m/s)" |
|
288 | 289 | ylabel = "Range (Km)" |
|
289 | 290 | |
|
290 | 291 | if not self.__isConfig: |
|
291 | 292 | |
|
292 | 293 | nplots = len(channelIndexList) |
|
293 | 294 | |
|
294 | 295 | self.setup(idfigure=idfigure, |
|
295 | 296 | nplots=nplots, |
|
296 | 297 | wintitle=wintitle, |
|
297 | 298 | showprofile=showprofile) |
|
298 | 299 | |
|
299 | 300 | tmin, tmax = self.getTimeLim(x, xmin, xmax) |
|
300 | 301 | if ymin == None: ymin = numpy.nanmin(y) |
|
301 | 302 | if ymax == None: ymax = numpy.nanmax(y) |
|
302 | 303 | if zmin == None: zmin = numpy.nanmin(avgdB)*0.9 |
|
303 | 304 | if zmax == None: zmax = numpy.nanmax(avgdB)*0.9 |
|
304 | 305 | |
|
305 | 306 | self.name = thisDatetime.strftime("%Y%m%d_%H%M%S") |
|
306 | 307 | self.x_buffer = numpy.array([]) |
|
307 | 308 | self.avgdB_buffer = numpy.array([]) |
|
308 | 309 | self.__isConfig = True |
|
309 | 310 | |
|
310 | 311 | |
|
311 | 312 | self.setWinTitle(title) |
|
312 | 313 | |
|
313 | 314 | if len(self.avgdB_buffer)==0: |
|
314 | 315 | self.avgdB_buffer = avgdB |
|
315 | 316 | newxdim = 1 |
|
316 | 317 | newydim = -1 |
|
317 | 318 | else: |
|
318 | 319 | if x[0]>self.x_buffer[-1]: |
|
319 | 320 | gap = avgdB.copy() |
|
320 | 321 | gap[:] = self.__missing |
|
321 | 322 | self.avgdB_buffer = numpy.hstack((self.avgdB_buffer, gap)) |
|
322 | 323 | |
|
323 | 324 | self.avgdB_buffer = numpy.hstack((self.avgdB_buffer, avgdB)) |
|
324 | 325 | newxdim = -1 |
|
325 | 326 | newydim = len(y) |
|
326 | 327 | |
|
327 | 328 | self.x_buffer = numpy.hstack((self.x_buffer, x)) |
|
328 | 329 | |
|
329 | 330 | self.avgdB_buffer = numpy.ma.masked_inside(self.avgdB_buffer,0.99*self.__missing,1.01*self.__missing) |
|
330 | 331 | |
|
331 | 332 | for i in range(self.nplots): |
|
332 | 333 | title = "Channel %d: %s" %(dataOut.channelList[i], thisDatetime.strftime("%d-%b-%Y %H:%M:%S")) |
|
333 | 334 | axes = self.axesList[i*self.__nsubplots] |
|
334 | 335 | zdB = self.avgdB_buffer[i].reshape(newxdim,newydim) |
|
335 | 336 | axes.pcolor(self.x_buffer, y, zdB, |
|
336 | 337 | xmin=tmin, xmax=tmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax, |
|
337 | 338 | xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True, |
|
338 | 339 | ticksize=9, cblabel='', cbsize="1%") |
|
339 | 340 | |
|
340 | 341 | if self.__showprofile: |
|
341 | 342 | axes = self.axesList[i*self.__nsubplots +1] |
|
342 | 343 | axes.pline(avgdB[i], y, |
|
343 | 344 | xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax, |
|
344 | 345 | xlabel='dB', ylabel='', title='', |
|
345 | 346 | ytick_visible=False, |
|
346 | 347 | grid='x') |
|
347 | 348 | |
|
348 | 349 | self.draw() |
|
349 | 350 | |
|
350 | 351 | if save: |
|
351 | 352 | |
|
352 | 353 | if figfile == None: |
|
353 | 354 | figfile = self.getFilename(name = self.name) |
|
354 | 355 | |
|
355 | 356 | self.saveFigure(figpath, figfile) |
|
356 | 357 | |
|
357 | 358 | if x[1] + (x[1]-x[0]) >= self.axesList[0].xmax: |
|
358 | 359 | self.__isConfig = False |
|
359 | 360 | |
|
360 | 361 | class SpectraPlot(Figure): |
|
361 | 362 | |
|
362 | 363 | __isConfig = None |
|
363 | 364 | __nsubplots = None |
|
364 | 365 | |
|
365 | 366 | WIDTHPROF = None |
|
366 | 367 | HEIGHTPROF = None |
|
367 | 368 | PREFIX = 'spc' |
|
368 | 369 | |
|
369 | 370 | def __init__(self): |
|
370 | 371 | |
|
371 | 372 | self.__isConfig = False |
|
372 | 373 | self.__nsubplots = 1 |
|
373 | 374 | |
|
374 | 375 | self.WIDTH = 230 |
|
375 | 376 | self.HEIGHT = 250 |
|
376 | 377 | self.WIDTHPROF = 120 |
|
377 | 378 | self.HEIGHTPROF = 0 |
|
378 | 379 | |
|
379 | 380 | def getSubplots(self): |
|
380 | 381 | |
|
381 | 382 | ncol = int(numpy.sqrt(self.nplots)+0.9) |
|
382 | 383 | nrow = int(self.nplots*1./ncol + 0.9) |
|
383 | 384 | |
|
384 | 385 | return nrow, ncol |
|
385 | 386 | |
|
386 | 387 | def setup(self, idfigure, nplots, wintitle, showprofile=True): |
|
387 | 388 | |
|
388 | 389 | self.__showprofile = showprofile |
|
389 | 390 | self.nplots = nplots |
|
390 | 391 | |
|
391 | 392 | ncolspan = 1 |
|
392 | 393 | colspan = 1 |
|
393 | 394 | if showprofile: |
|
394 | 395 | ncolspan = 3 |
|
395 | 396 | colspan = 2 |
|
396 | 397 | self.__nsubplots = 2 |
|
397 | 398 | |
|
398 | 399 | self.createFigure(idfigure = idfigure, |
|
399 | 400 | wintitle = wintitle, |
|
400 | 401 | widthplot = self.WIDTH + self.WIDTHPROF, |
|
401 | 402 | heightplot = self.HEIGHT + self.HEIGHTPROF) |
|
402 | 403 | |
|
403 | 404 | nrow, ncol = self.getSubplots() |
|
404 | 405 | |
|
405 | 406 | counter = 0 |
|
406 | 407 | for y in range(nrow): |
|
407 | 408 | for x in range(ncol): |
|
408 | 409 | |
|
409 | 410 | if counter >= self.nplots: |
|
410 | 411 | break |
|
411 | 412 | |
|
412 | 413 | self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1) |
|
413 | 414 | |
|
414 | 415 | if showprofile: |
|
415 | 416 | self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1) |
|
416 | 417 | |
|
417 | 418 | counter += 1 |
|
418 | 419 | |
|
419 | 420 | def run(self, dataOut, idfigure, wintitle="", channelList=None, showprofile='True', |
|
420 | 421 | xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None, |
|
421 | 422 | save=False, figpath='./', figfile=None): |
|
422 | 423 | |
|
423 | 424 | """ |
|
424 | 425 | |
|
425 | 426 | Input: |
|
426 | 427 | dataOut : |
|
427 | 428 | idfigure : |
|
428 | 429 | wintitle : |
|
429 | 430 | channelList : |
|
430 | 431 | showProfile : |
|
431 | 432 | xmin : None, |
|
432 | 433 | xmax : None, |
|
433 | 434 | ymin : None, |
|
434 | 435 | ymax : None, |
|
435 | 436 | zmin : None, |
|
436 | 437 | zmax : None |
|
437 | 438 | """ |
|
438 | 439 | |
|
439 | 440 | if channelList == None: |
|
440 | 441 | channelIndexList = dataOut.channelIndexList |
|
441 | 442 | else: |
|
442 | 443 | channelIndexList = [] |
|
443 | 444 | for channel in channelList: |
|
444 | 445 | if channel not in dataOut.channelList: |
|
445 | 446 | raise ValueError, "Channel %d is not in dataOut.channelList" |
|
446 | 447 | channelIndexList.append(dataOut.channelList.index(channel)) |
|
447 | 448 | factor = dataOut.normFactor |
|
448 | 449 | x = dataOut.getVelRange(1) |
|
449 | 450 | y = dataOut.getHeiRange() |
|
450 | 451 | |
|
451 | 452 | z = dataOut.data_spc[channelIndexList,:,:]/factor |
|
452 | 453 | z = numpy.where(numpy.isfinite(z), z, numpy.NAN) |
|
453 | 454 | avg = numpy.average(z, axis=1) |
|
454 | 455 | noise = dataOut.getNoise()/factor |
|
455 | 456 | |
|
456 | 457 | zdB = 10*numpy.log10(z) |
|
457 | 458 | avgdB = 10*numpy.log10(avg) |
|
458 | 459 | noisedB = 10*numpy.log10(noise) |
|
459 | 460 | |
|
460 | 461 | thisDatetime = dataOut.datatime |
|
461 | 462 | title = "Spectra: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S")) |
|
462 | 463 | xlabel = "Velocity (m/s)" |
|
463 | 464 | ylabel = "Range (Km)" |
|
464 | 465 | |
|
465 | 466 | if not self.__isConfig: |
|
466 | 467 | |
|
467 | 468 | nplots = len(channelIndexList) |
|
468 | 469 | |
|
469 | 470 | self.setup(idfigure=idfigure, |
|
470 | 471 | nplots=nplots, |
|
471 | 472 | wintitle=wintitle, |
|
472 | 473 | showprofile=showprofile) |
|
473 | 474 | |
|
474 | 475 | if xmin == None: xmin = numpy.nanmin(x) |
|
475 | 476 | if xmax == None: xmax = numpy.nanmax(x) |
|
476 | 477 | if ymin == None: ymin = numpy.nanmin(y) |
|
477 | 478 | if ymax == None: ymax = numpy.nanmax(y) |
|
478 | 479 | if zmin == None: zmin = numpy.nanmin(avgdB)*0.9 |
|
479 | 480 | if zmax == None: zmax = numpy.nanmax(avgdB)*0.9 |
|
480 | 481 | |
|
481 | 482 | self.__isConfig = True |
|
482 | 483 | |
|
483 | 484 | self.setWinTitle(title) |
|
484 | 485 | |
|
485 | 486 | for i in range(self.nplots): |
|
486 | 487 | title = "Channel %d: %4.2fdB" %(dataOut.channelList[i], noisedB[i]) |
|
487 | 488 | axes = self.axesList[i*self.__nsubplots] |
|
488 | 489 | axes.pcolor(x, y, zdB[i,:,:], |
|
489 | 490 | xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax, |
|
490 | 491 | xlabel=xlabel, ylabel=ylabel, title=title, |
|
491 | 492 | ticksize=9, cblabel='') |
|
492 | 493 | |
|
493 | 494 | if self.__showprofile: |
|
494 | 495 | axes = self.axesList[i*self.__nsubplots +1] |
|
495 | 496 | axes.pline(avgdB[i], y, |
|
496 | 497 | xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax, |
|
497 | 498 | xlabel='dB', ylabel='', title='', |
|
498 | 499 | ytick_visible=False, |
|
499 | 500 | grid='x') |
|
500 | 501 | |
|
501 | 502 | noiseline = numpy.repeat(noisedB[i], len(y)) |
|
502 | 503 | axes.addpline(noiseline, y, idline=1, color="black", linestyle="dashed", lw=2) |
|
503 | 504 | |
|
504 | 505 | self.draw() |
|
505 | 506 | |
|
506 | 507 | if save: |
|
507 | 508 | date = thisDatetime.strftime("%Y%m%d_%H%M%S") |
|
508 | 509 | if figfile == None: |
|
509 | 510 | figfile = self.getFilename(name = date) |
|
510 | 511 | |
|
511 | 512 | self.saveFigure(figpath, figfile) |
|
512 | 513 | |
|
513 | 514 | class Scope(Figure): |
|
514 | 515 | |
|
515 | 516 | __isConfig = None |
|
516 | 517 | |
|
517 | 518 | def __init__(self): |
|
518 | 519 | |
|
519 | 520 | self.__isConfig = False |
|
520 | 521 | self.WIDTH = 600 |
|
521 | 522 | self.HEIGHT = 200 |
|
522 | 523 | |
|
523 | 524 | def getSubplots(self): |
|
524 | 525 | |
|
525 | 526 | nrow = self.nplots |
|
526 | 527 | ncol = 3 |
|
527 | 528 | return nrow, ncol |
|
528 | 529 | |
|
529 | 530 | def setup(self, idfigure, nplots, wintitle): |
|
530 | 531 | |
|
531 | 532 | self.nplots = nplots |
|
532 | 533 | |
|
533 | 534 | self.createFigure(idfigure, wintitle) |
|
534 | 535 | |
|
535 | 536 | nrow,ncol = self.getSubplots() |
|
536 | 537 | colspan = 3 |
|
537 | 538 | rowspan = 1 |
|
538 | 539 | |
|
539 | 540 | for i in range(nplots): |
|
540 | 541 | self.addAxes(nrow, ncol, i, 0, colspan, rowspan) |
|
541 | 542 | |
|
542 | 543 | |
|
543 | 544 | |
|
544 | 545 | def run(self, dataOut, idfigure, wintitle="", channelList=None, |
|
545 | 546 | xmin=None, xmax=None, ymin=None, ymax=None, save=False, |
|
546 | 547 | figpath='./', figfile=None): |
|
547 | 548 | |
|
548 | 549 | """ |
|
549 | 550 | |
|
550 | 551 | Input: |
|
551 | 552 | dataOut : |
|
552 | 553 | idfigure : |
|
553 | 554 | wintitle : |
|
554 | 555 | channelList : |
|
555 | 556 | xmin : None, |
|
556 | 557 | xmax : None, |
|
557 | 558 | ymin : None, |
|
558 | 559 | ymax : None, |
|
559 | 560 | """ |
|
560 | 561 | |
|
561 | 562 | if channelList == None: |
|
562 | 563 | channelIndexList = dataOut.channelIndexList |
|
563 | 564 | else: |
|
564 | 565 | channelIndexList = [] |
|
565 | 566 | for channel in channelList: |
|
566 | 567 | if channel not in dataOut.channelList: |
|
567 | 568 | raise ValueError, "Channel %d is not in dataOut.channelList" |
|
568 | 569 | channelIndexList.append(dataOut.channelList.index(channel)) |
|
569 | 570 | |
|
570 | 571 | x = dataOut.heightList |
|
571 | 572 | y = dataOut.data[channelIndexList,:] * numpy.conjugate(dataOut.data[channelIndexList,:]) |
|
572 | 573 | y = y.real |
|
573 | 574 | |
|
574 | 575 | thisDatetime = dataOut.datatime |
|
575 | 576 | title = "Scope: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S")) |
|
576 | 577 | xlabel = "Range (Km)" |
|
577 | 578 | ylabel = "Intensity" |
|
578 | 579 | |
|
579 | 580 | if not self.__isConfig: |
|
580 | 581 | nplots = len(channelIndexList) |
|
581 | 582 | |
|
582 | 583 | self.setup(idfigure=idfigure, |
|
583 | 584 | nplots=nplots, |
|
584 | 585 | wintitle=wintitle) |
|
585 | 586 | |
|
586 | 587 | if xmin == None: xmin = numpy.nanmin(x) |
|
587 | 588 | if xmax == None: xmax = numpy.nanmax(x) |
|
588 | 589 | if ymin == None: ymin = numpy.nanmin(y) |
|
589 | 590 | if ymax == None: ymax = numpy.nanmax(y) |
|
590 | 591 | |
|
591 | 592 | self.__isConfig = True |
|
592 | 593 | |
|
593 | 594 | self.setWinTitle(title) |
|
594 | 595 | |
|
595 | 596 | for i in range(len(self.axesList)): |
|
596 | 597 | title = "Channel %d" %(i) |
|
597 | 598 | axes = self.axesList[i] |
|
598 | 599 | ychannel = y[i,:] |
|
599 | 600 | axes.pline(x, ychannel, |
|
600 | 601 | xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, |
|
601 | 602 | xlabel=xlabel, ylabel=ylabel, title=title) |
|
602 | 603 | |
|
603 | 604 | self.draw() |
|
604 | 605 | |
|
605 | 606 | if save: |
|
606 | 607 | date = thisDatetime.strftime("%Y%m%d_%H%M%S") |
|
607 | 608 | if figfile == None: |
|
608 | 609 | figfile = self.getFilename(name = date) |
|
609 | 610 | |
|
610 | 611 | self.saveFigure(figpath, figfile) |
|
611 | 612 | |
|
612 | 613 | class ProfilePlot(Figure): |
|
613 | 614 | __isConfig = None |
|
614 | 615 | __nsubplots = None |
|
615 | 616 | |
|
616 | 617 | WIDTHPROF = None |
|
617 | 618 | HEIGHTPROF = None |
|
618 | 619 | PREFIX = 'spcprofile' |
|
619 | 620 | |
|
620 | 621 | def __init__(self): |
|
621 | 622 | self.__isConfig = False |
|
622 | 623 | self.__nsubplots = 1 |
|
623 | 624 | |
|
624 | 625 | self.WIDTH = 300 |
|
625 | 626 | self.HEIGHT = 500 |
|
626 | 627 | |
|
627 | 628 | def getSubplots(self): |
|
628 | 629 | ncol = 1 |
|
629 | 630 | nrow = 1 |
|
630 | 631 | |
|
631 | 632 | return nrow, ncol |
|
632 | 633 | |
|
633 | 634 | def setup(self, idfigure, nplots, wintitle): |
|
634 | 635 | |
|
635 | 636 | self.nplots = nplots |
|
636 | 637 | |
|
637 | 638 | ncolspan = 1 |
|
638 | 639 | colspan = 1 |
|
639 | 640 | |
|
640 | 641 | self.createFigure(idfigure = idfigure, |
|
641 | 642 | wintitle = wintitle, |
|
642 | 643 | widthplot = self.WIDTH, |
|
643 | 644 | heightplot = self.HEIGHT) |
|
644 | 645 | |
|
645 | 646 | nrow, ncol = self.getSubplots() |
|
646 | 647 | |
|
647 | 648 | counter = 0 |
|
648 | 649 | for y in range(nrow): |
|
649 | 650 | for x in range(ncol): |
|
650 | 651 | self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1) |
|
651 | 652 | |
|
652 | 653 | def run(self, dataOut, idfigure, wintitle="", channelList=None, |
|
653 | 654 | xmin=None, xmax=None, ymin=None, ymax=None, |
|
654 | 655 | save=False, figpath='./', figfile=None): |
|
655 | 656 | |
|
656 | 657 | if channelList == None: |
|
657 | 658 | channelIndexList = dataOut.channelIndexList |
|
658 | 659 | channelList = dataOut.channelList |
|
659 | 660 | else: |
|
660 | 661 | channelIndexList = [] |
|
661 | 662 | for channel in channelList: |
|
662 | 663 | if channel not in dataOut.channelList: |
|
663 | 664 | raise ValueError, "Channel %d is not in dataOut.channelList" |
|
664 | 665 | channelIndexList.append(dataOut.channelList.index(channel)) |
|
665 | 666 | |
|
666 | 667 | factor = dataOut.normFactor |
|
667 | 668 | y = dataOut.getHeiRange() |
|
668 | 669 | x = dataOut.data_spc[channelIndexList,:,:]/factor |
|
669 | 670 | x = numpy.where(numpy.isfinite(x), x, numpy.NAN) |
|
670 | 671 | avg = numpy.average(x, axis=1) |
|
671 | 672 | |
|
672 | 673 | avgdB = 10*numpy.log10(avg) |
|
673 | 674 | |
|
674 | 675 | thisDatetime = dataOut.datatime |
|
675 | 676 | title = "Power Profile" |
|
676 | 677 | xlabel = "dB" |
|
677 | 678 | ylabel = "Range (Km)" |
|
678 | 679 | |
|
679 | 680 | if not self.__isConfig: |
|
680 | 681 | |
|
681 | 682 | nplots = 1 |
|
682 | 683 | |
|
683 | 684 | self.setup(idfigure=idfigure, |
|
684 | 685 | nplots=nplots, |
|
685 | 686 | wintitle=wintitle) |
|
686 | 687 | |
|
687 | 688 | if ymin == None: ymin = numpy.nanmin(y) |
|
688 | 689 | if ymax == None: ymax = numpy.nanmax(y) |
|
689 | 690 | if xmin == None: xmin = numpy.nanmin(avgdB)*0.9 |
|
690 | 691 | if xmax == None: xmax = numpy.nanmax(avgdB)*0.9 |
|
691 | 692 | |
|
692 | 693 | self.__isConfig = True |
|
693 | 694 | |
|
694 | 695 | self.setWinTitle(title) |
|
695 | 696 | |
|
696 | 697 | |
|
697 | 698 | title = "Power Profile: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S")) |
|
698 | 699 | axes = self.axesList[0] |
|
699 | 700 | |
|
700 | 701 | legendlabels = ["channel %d"%x for x in channelList] |
|
701 | 702 | axes.pmultiline(avgdB, y, |
|
702 | 703 | xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, |
|
703 | 704 | xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels, |
|
704 | 705 | ytick_visible=True, nxticks=5, |
|
705 | 706 | grid='x') |
|
706 | 707 | |
|
707 | 708 | self.draw() |
|
708 | 709 | |
|
709 | 710 | if save: |
|
710 | 711 | date = thisDatetime.strftime("%Y%m%d") |
|
711 | 712 | if figfile == None: |
|
712 | 713 | figfile = self.getFilename(name = date) |
|
713 | 714 | |
|
714 | 715 | self.saveFigure(figpath, figfile) |
|
715 | 716 | |
|
716 | 717 | class CoherenceMap(Figure): |
|
717 | 718 | __isConfig = None |
|
718 | 719 | __nsubplots = None |
|
719 | 720 | |
|
720 | 721 | WIDTHPROF = None |
|
721 | 722 | HEIGHTPROF = None |
|
722 |
PREFIX = 'c |
|
|
723 | PREFIX = 'cmap' | |
|
723 | 724 | |
|
724 | 725 | def __init__(self): |
|
725 | 726 | self.timerange = 2*60*60 |
|
726 | 727 | self.__isConfig = False |
|
727 | 728 | self.__nsubplots = 1 |
|
728 | 729 | |
|
729 | 730 | self.WIDTH = 800 |
|
730 | 731 | self.HEIGHT = 200 |
|
731 | 732 | self.WIDTHPROF = 120 |
|
732 | 733 | self.HEIGHTPROF = 0 |
|
733 | 734 | |
|
734 | 735 | def getSubplots(self): |
|
735 | 736 | ncol = 1 |
|
736 | 737 | nrow = self.nplots*2 |
|
737 | 738 | |
|
738 | 739 | return nrow, ncol |
|
739 | 740 | |
|
740 | 741 | def setup(self, idfigure, nplots, wintitle, showprofile=True): |
|
741 | 742 | self.__showprofile = showprofile |
|
742 | 743 | self.nplots = nplots |
|
743 | 744 | |
|
744 | 745 | ncolspan = 1 |
|
745 | 746 | colspan = 1 |
|
746 | 747 | if showprofile: |
|
747 | 748 | ncolspan = 7 |
|
748 | 749 | colspan = 6 |
|
749 | 750 | self.__nsubplots = 2 |
|
750 | 751 | |
|
751 | 752 | self.createFigure(idfigure = idfigure, |
|
752 | 753 | wintitle = wintitle, |
|
753 | 754 | widthplot = self.WIDTH + self.WIDTHPROF, |
|
754 | 755 | heightplot = self.HEIGHT + self.HEIGHTPROF) |
|
755 | 756 | |
|
756 | 757 | nrow, ncol = self.getSubplots() |
|
757 | 758 | |
|
758 | 759 | for y in range(nrow): |
|
759 | 760 | for x in range(ncol): |
|
760 | 761 | |
|
761 | 762 | self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1) |
|
762 | 763 | |
|
763 | 764 | if showprofile: |
|
764 | 765 | self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1) |
|
765 | 766 | |
|
766 | 767 | def run(self, dataOut, idfigure, wintitle="", pairsList=None, showprofile='True', |
|
767 | 768 | xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None, |
|
768 | 769 | timerange=None, |
|
769 |
save=False, figpath='./', figfile=None |
|
|
770 | save=False, figpath='./', figfile=None, | |
|
771 | coherence_cmap='jet', phase_cmap='RdBu_r'): | |
|
770 | 772 | |
|
771 | 773 | if pairsList == None: |
|
772 | 774 | pairsIndexList = dataOut.pairsIndexList |
|
773 | 775 | else: |
|
774 | 776 | pairsIndexList = [] |
|
775 | 777 | for pair in pairsList: |
|
776 | 778 | if pair not in dataOut.pairsList: |
|
777 | 779 | raise ValueError, "Pair %s is not in dataOut.pairsList" %(pair) |
|
778 | 780 | pairsIndexList.append(dataOut.pairsList.index(pair)) |
|
779 | 781 | |
|
780 | 782 | if timerange != None: |
|
781 | 783 | self.timerange = timerange |
|
782 | 784 | |
|
783 | 785 | if pairsIndexList == []: |
|
784 | 786 | return |
|
785 | 787 | |
|
786 | 788 | if len(pairsIndexList) > 4: |
|
787 | 789 | pairsIndexList = pairsIndexList[0:4] |
|
788 | 790 | |
|
789 | 791 | tmin = None |
|
790 | 792 | tmax = None |
|
791 | 793 | x = dataOut.getTimeRange() |
|
792 | 794 | y = dataOut.getHeiRange() |
|
793 | 795 | |
|
794 | 796 | thisDatetime = dataOut.datatime |
|
795 | 797 | title = "CoherenceMap: %s" %(thisDatetime.strftime("%d-%b-%Y")) |
|
796 | 798 | xlabel = "" |
|
797 | 799 | ylabel = "Range (Km)" |
|
798 | 800 | |
|
799 | 801 | if not self.__isConfig: |
|
800 | 802 | nplots = len(pairsIndexList) |
|
801 | 803 | self.setup(idfigure=idfigure, |
|
802 | 804 | nplots=nplots, |
|
803 | 805 | wintitle=wintitle, |
|
804 | 806 | showprofile=showprofile) |
|
805 | 807 | |
|
806 | 808 | tmin, tmax = self.getTimeLim(x, xmin, xmax) |
|
807 | 809 | if ymin == None: ymin = numpy.nanmin(y) |
|
808 | 810 | if ymax == None: ymax = numpy.nanmax(y) |
|
809 | 811 | |
|
810 | 812 | self.name = thisDatetime.strftime("%Y%m%d_%H%M%S") |
|
811 | 813 | |
|
812 | 814 | self.__isConfig = True |
|
813 | 815 | |
|
814 | 816 | self.setWinTitle(title) |
|
815 | 817 | |
|
816 | 818 | for i in range(self.nplots): |
|
817 | 819 | |
|
818 | 820 | pair = dataOut.pairsList[pairsIndexList[i]] |
|
819 | 821 | coherenceComplex = dataOut.data_cspc[pairsIndexList[i],:,:]/numpy.sqrt(dataOut.data_spc[pair[0],:,:]*dataOut.data_spc[pair[1],:,:]) |
|
820 |
coherence = numpy.a |
|
|
821 |
|
|
|
822 | z = avg.reshape((1,-1)) | |
|
822 | avgcoherenceComplex = numpy.average(coherenceComplex, axis=0) | |
|
823 | coherence = numpy.abs(avgcoherenceComplex) | |
|
824 | # coherence = numpy.abs(coherenceComplex) | |
|
825 | # avg = numpy.average(coherence, axis=0) | |
|
826 | ||
|
827 | z = coherence.reshape((1,-1)) | |
|
823 | 828 | |
|
824 | 829 | counter = 0 |
|
825 | 830 | |
|
826 | 831 | title = "Coherence %d%d: %s" %(pair[0], pair[1], thisDatetime.strftime("%d-%b-%Y %H:%M:%S")) |
|
827 | 832 | axes = self.axesList[i*self.__nsubplots*2] |
|
828 | 833 | axes.pcolor(x, y, z, |
|
829 | 834 | xmin=tmin, xmax=tmax, ymin=ymin, ymax=ymax, zmin=0, zmax=1, |
|
830 | 835 | xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True, |
|
831 | ticksize=9, cblabel='', cbsize="1%") | |
|
836 | ticksize=9, cblabel='', colormap=coherence_cmap, cbsize="1%") | |
|
832 | 837 | |
|
833 | 838 | if self.__showprofile: |
|
834 | 839 | counter += 1 |
|
835 | 840 | axes = self.axesList[i*self.__nsubplots*2 + counter] |
|
836 |
axes.pline( |
|
|
841 | axes.pline(coherence, y, | |
|
837 | 842 | xmin=0, xmax=1, ymin=ymin, ymax=ymax, |
|
838 | 843 | xlabel='', ylabel='', title='', ticksize=7, |
|
839 | 844 | ytick_visible=False, nxticks=5, |
|
840 | 845 | grid='x') |
|
841 | 846 | |
|
842 | 847 | counter += 1 |
|
843 | phase = numpy.arctan(-1*coherenceComplex.imag/coherenceComplex.real)*180/numpy.pi | |
|
844 | avg = numpy.average(phase, axis=0) | |
|
845 | z = avg.reshape((1,-1)) | |
|
848 | # phase = numpy.arctan(-1*coherenceComplex.imag/coherenceComplex.real)*180/numpy.pi | |
|
849 | phase = numpy.arctan2(avgcoherenceComplex.imag, avgcoherenceComplex.real)*180/numpy.pi | |
|
850 | # avg = numpy.average(phase, axis=0) | |
|
851 | z = phase.reshape((1,-1)) | |
|
846 | 852 | |
|
847 | 853 | title = "Phase %d%d: %s" %(pair[0], pair[1], thisDatetime.strftime("%d-%b-%Y %H:%M:%S")) |
|
848 | 854 | axes = self.axesList[i*self.__nsubplots*2 + counter] |
|
849 | 855 | axes.pcolor(x, y, z, |
|
850 | 856 | xmin=tmin, xmax=tmax, ymin=ymin, ymax=ymax, zmin=-180, zmax=180, |
|
851 | 857 | xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True, |
|
852 |
ticksize=9, cblabel='', colormap= |
|
|
858 | ticksize=9, cblabel='', colormap=phase_cmap, cbsize="1%") | |
|
853 | 859 | |
|
854 | 860 | if self.__showprofile: |
|
855 | 861 | counter += 1 |
|
856 | 862 | axes = self.axesList[i*self.__nsubplots*2 + counter] |
|
857 |
axes.pline( |
|
|
863 | axes.pline(phase, y, | |
|
858 | 864 | xmin=-180, xmax=180, ymin=ymin, ymax=ymax, |
|
859 | 865 | xlabel='', ylabel='', title='', ticksize=7, |
|
860 | 866 | ytick_visible=False, nxticks=4, |
|
861 | 867 | grid='x') |
|
862 | 868 | |
|
863 | 869 | self.draw() |
|
864 | 870 | |
|
865 | 871 | if save: |
|
866 | 872 | |
|
867 | 873 | if figfile == None: |
|
868 | 874 | figfile = self.getFilename(name = self.name) |
|
869 | 875 | |
|
870 | 876 | self.saveFigure(figpath, figfile) |
|
871 | 877 | |
|
872 | 878 | if x[1] + (x[1]-x[0]) >= self.axesList[0].xmax: |
|
873 | 879 | self.__isConfig = False |
|
874 | 880 | |
|
875 | 881 | class RTIfromNoise(Figure): |
|
876 | 882 | |
|
877 | 883 | __isConfig = None |
|
878 | 884 | __nsubplots = None |
|
879 | 885 | |
|
880 | 886 | PREFIX = 'rtinoise' |
|
881 | 887 | |
|
882 | 888 | def __init__(self): |
|
883 | 889 | |
|
884 | 890 | self.timerange = 24*60*60 |
|
885 | 891 | self.__isConfig = False |
|
886 | 892 | self.__nsubplots = 1 |
|
887 | 893 | |
|
888 | 894 | self.WIDTH = 820 |
|
889 | 895 | self.HEIGHT = 200 |
|
890 | 896 | self.WIDTHPROF = 120 |
|
891 | 897 | self.HEIGHTPROF = 0 |
|
892 | 898 | self.xdata = None |
|
893 | 899 | self.ydata = None |
|
894 | 900 | |
|
895 | 901 | def getSubplots(self): |
|
896 | 902 | |
|
897 | 903 | ncol = 1 |
|
898 | 904 | nrow = 1 |
|
899 | 905 | |
|
900 | 906 | return nrow, ncol |
|
901 | 907 | |
|
902 | 908 | def setup(self, idfigure, nplots, wintitle, showprofile=True): |
|
903 | 909 | |
|
904 | 910 | self.__showprofile = showprofile |
|
905 | 911 | self.nplots = nplots |
|
906 | 912 | |
|
907 | 913 | ncolspan = 7 |
|
908 | 914 | colspan = 6 |
|
909 | 915 | self.__nsubplots = 2 |
|
910 | 916 | |
|
911 | 917 | self.createFigure(idfigure = idfigure, |
|
912 | 918 | wintitle = wintitle, |
|
913 | 919 | widthplot = self.WIDTH+self.WIDTHPROF, |
|
914 | 920 | heightplot = self.HEIGHT+self.HEIGHTPROF) |
|
915 | 921 | |
|
916 | 922 | nrow, ncol = self.getSubplots() |
|
917 | 923 | |
|
918 | 924 | self.addAxes(nrow, ncol*ncolspan, 0, 0, colspan, 1) |
|
919 | 925 | |
|
920 | 926 | |
|
921 | 927 | def run(self, dataOut, idfigure, wintitle="", channelList=None, showprofile='True', |
|
922 | 928 | xmin=None, xmax=None, ymin=None, ymax=None, |
|
923 | 929 | timerange=None, |
|
924 | 930 | save=False, figpath='./', figfile=None): |
|
925 | 931 | |
|
926 | 932 | if channelList == None: |
|
927 | 933 | channelIndexList = dataOut.channelIndexList |
|
928 | 934 | channelList = dataOut.channelList |
|
929 | 935 | else: |
|
930 | 936 | channelIndexList = [] |
|
931 | 937 | for channel in channelList: |
|
932 | 938 | if channel not in dataOut.channelList: |
|
933 | 939 | raise ValueError, "Channel %d is not in dataOut.channelList" |
|
934 | 940 | channelIndexList.append(dataOut.channelList.index(channel)) |
|
935 | 941 | |
|
936 | 942 | if timerange != None: |
|
937 | 943 | self.timerange = timerange |
|
938 | 944 | |
|
939 | 945 | tmin = None |
|
940 | 946 | tmax = None |
|
941 | 947 | x = dataOut.getTimeRange() |
|
942 | 948 | y = dataOut.getHeiRange() |
|
943 | 949 | factor = dataOut.normFactor |
|
944 | 950 | noise = dataOut.getNoise()/factor |
|
945 | 951 | noisedB = 10*numpy.log10(noise) |
|
946 | 952 | |
|
947 | 953 | thisDatetime = dataOut.datatime |
|
948 | 954 | title = "RTI Noise: %s" %(thisDatetime.strftime("%d-%b-%Y")) |
|
949 | 955 | xlabel = "" |
|
950 | 956 | ylabel = "Range (Km)" |
|
951 | 957 | |
|
952 | 958 | if not self.__isConfig: |
|
953 | 959 | |
|
954 | 960 | nplots = 1 |
|
955 | 961 | |
|
956 | 962 | self.setup(idfigure=idfigure, |
|
957 | 963 | nplots=nplots, |
|
958 | 964 | wintitle=wintitle, |
|
959 | 965 | showprofile=showprofile) |
|
960 | 966 | |
|
961 | 967 | tmin, tmax = self.getTimeLim(x, xmin, xmax) |
|
962 | 968 | if ymin == None: ymin = numpy.nanmin(noisedB) |
|
963 | 969 | if ymax == None: ymax = numpy.nanmax(noisedB) |
|
964 | 970 | |
|
965 | 971 | self.name = thisDatetime.strftime("%Y%m%d_%H%M%S") |
|
966 | 972 | self.__isConfig = True |
|
967 | 973 | |
|
968 | 974 | self.xdata = numpy.array([]) |
|
969 | 975 | self.ydata = numpy.array([]) |
|
970 | 976 | |
|
971 | 977 | self.setWinTitle(title) |
|
972 | 978 | |
|
973 | 979 | |
|
974 | 980 | title = "RTI Noise %s" %(thisDatetime.strftime("%d-%b-%Y")) |
|
975 | 981 | |
|
976 | 982 | legendlabels = ["channel %d"%idchannel for idchannel in channelList] |
|
977 | 983 | axes = self.axesList[0] |
|
978 | 984 | |
|
979 | 985 | self.xdata = numpy.hstack((self.xdata, x[0:1])) |
|
980 | 986 | |
|
981 | 987 | if len(self.ydata)==0: |
|
982 | 988 | self.ydata = noisedB[channelIndexList].reshape(-1,1) |
|
983 | 989 | else: |
|
984 | 990 | self.ydata = numpy.hstack((self.ydata, noisedB[channelIndexList].reshape(-1,1))) |
|
985 | 991 | |
|
986 | 992 | |
|
987 | 993 | axes.pmultilineyaxis(x=self.xdata, y=self.ydata, |
|
988 | 994 | xmin=tmin, xmax=tmax, ymin=ymin, ymax=ymax, |
|
989 | 995 | xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels, marker='x', markersize=8, linestyle="solid", |
|
990 | 996 | XAxisAsTime=True |
|
991 | 997 | ) |
|
992 | 998 | |
|
993 | 999 | self.draw() |
|
994 | 1000 | |
|
995 | 1001 | if save: |
|
996 | 1002 | |
|
997 | 1003 | if figfile == None: |
|
998 | 1004 | figfile = self.getFilename(name = self.name) |
|
999 | 1005 | |
|
1000 | 1006 | self.saveFigure(figpath, figfile) |
|
1001 | 1007 | |
|
1002 | 1008 | if x[1] + (x[1]-x[0]) >= self.axesList[0].xmax: |
|
1003 | 1009 | self.__isConfig = False |
|
1004 | 1010 | del self.xdata |
|
1005 | 1011 | del self.ydata |
|
1006 | 1012 | No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now