##// END OF EJS Templates
Changing the Model for the input Objects in Processing package...
Daniel Valdez -
r107:33fbc5472d1b
parent child
Show More
@@ -1,23 +1,13
1 1 [Read0]
2 2 id = 0
3 3 type = VoltageReader
4 setup = str,/Users/jro/Documents/RadarData/EW_Drifts,datetime,2011-11-20-0-0-1,datetime,2011-12-31-0-0-1,int,0,str,,str,None,int,0
4 setup = str,/Users/jro/Documents/RadarData/MST_ISR/MST,datetime,2009-01-01-0-0-1,datetime,2009-12-31-0-0-1,int,0,str,,str,None,int,0
5 5 getData = None,None
6 6
7 7 [Processing0]
8 8 id = 1
9 type = Voltage
10 input = 0
11 setup = None,None
12 init = None,None
13 integrator = int,4,None,None
14 plotData = float,None,float,None,float,None,float,None,str,iq,str,Test Data Voltage 2,int,1
15
16 [Processing1]
17 id = 2
18 9 type = Spectra
19 input = 1
20 setup = int,8,None,None
10 input = 0
11 setup = int,16,None,None
21 12 init = None,None
22 integrator = int,4,int,3
23 plotData = float,None,float,None,float,None,float,None,str,Test Spectra Data,int,2
13 plotData = float,None,float,None,float,None,float,None,str,Test Spectra Data,int,1
@@ -1,902 +1,903
1 1 """
2 2 Created on Feb 7, 2012
3 3
4 4 @autor $Author$
5 5 @version $Id$
6 6
7 7 """
8 8
9 9 import numpy
10 10 import plplot
11 11
12 12 def cmap1_init(colormap="gray"):
13 13
14 14 if colormap == None:
15 15 return
16 16
17 17 ncolor = None
18 18 rgb_lvl = None
19 19
20 20 # Routine for defining a specific color map 1 in HLS space.
21 21 # if gray is true, use basic grayscale variation from half-dark to light.
22 22 # otherwise use false color variation from blue (240 deg) to red (360 deg).
23 23
24 24 # Independent variable of control points.
25 25 i = numpy.array((0., 1.))
26 26 if colormap=="gray":
27 27 ncolor = 256
28 28 # Hue for control points. Doesn't matter since saturation is zero.
29 29 h = numpy.array((0., 0.))
30 30 # Lightness ranging from half-dark (for interest) to light.
31 31 l = numpy.array((0.5, 1.))
32 32 # Gray scale has zero saturation
33 33 s = numpy.array((0., 0.))
34 34
35 35 # number of cmap1 colours is 256 in this case.
36 36 plplot.plscmap1n(ncolor)
37 37 # Interpolate between control points to set up cmap1.
38 38 plplot.plscmap1l(0, i, h, l, s)
39 39
40 40 return None
41 41
42 42 if colormap=="br_green":
43 43 ncolor = 256
44 44 # Hue ranges from blue (240 deg) to red (0 or 360 deg)
45 45 h = numpy.array((240., 0.))
46 46 # Lightness and saturation are constant (values taken from C example).
47 47 l = numpy.array((0.6, 0.6))
48 48 s = numpy.array((0.8, 0.8))
49 49
50 50 # number of cmap1 colours is 256 in this case.
51 51 plplot.plscmap1n(ncolor)
52 52 # Interpolate between control points to set up cmap1.
53 53 plplot.plscmap1l(0, i, h, l, s)
54 54
55 55 return None
56 56
57 57 if colormap=="tricolor":
58 58 ncolor = 3
59 59 # Hue ranges from blue (240 deg) to red (0 or 360 deg)
60 60 h = numpy.array((240., 0.))
61 61 # Lightness and saturation are constant (values taken from C example).
62 62 l = numpy.array((0.6, 0.6))
63 63 s = numpy.array((0.8, 0.8))
64 64
65 65 # number of cmap1 colours is 256 in this case.
66 66 plplot.plscmap1n(ncolor)
67 67 # Interpolate between control points to set up cmap1.
68 68 plplot.plscmap1l(0, i, h, l, s)
69 69
70 70 return None
71 71
72 72 if colormap == 'rgb' or colormap == 'rgb666':
73 73
74 74 color_sz = 6
75 75 ncolor = color_sz*color_sz*color_sz
76 76 pos = numpy.zeros((ncolor))
77 77 r = numpy.zeros((ncolor))
78 78 g = numpy.zeros((ncolor))
79 79 b = numpy.zeros((ncolor))
80 80 ind = 0
81 81 for ri in range(color_sz):
82 82 for gi in range(color_sz):
83 83 for bi in range(color_sz):
84 84 r[ind] = ri/(color_sz-1.0)
85 85 g[ind] = gi/(color_sz-1.0)
86 86 b[ind] = bi/(color_sz-1.0)
87 87 pos[ind] = ind/(ncolor-1.0)
88 88 ind += 1
89 89 rgb_lvl = [6,6,6] #Levels for RGB colors
90 90
91 91 if colormap == 'rgb676':
92 92 ncolor = 6*7*6
93 93 pos = numpy.zeros((ncolor))
94 94 r = numpy.zeros((ncolor))
95 95 g = numpy.zeros((ncolor))
96 96 b = numpy.zeros((ncolor))
97 97 ind = 0
98 98 for ri in range(8):
99 99 for gi in range(8):
100 100 for bi in range(4):
101 101 r[ind] = ri/(6-1.0)
102 102 g[ind] = gi/(7-1.0)
103 103 b[ind] = bi/(6-1.0)
104 104 pos[ind] = ind/(ncolor-1.0)
105 105 ind += 1
106 106 rgb_lvl = [6,7,6] #Levels for RGB colors
107 107
108 108 if colormap == 'rgb685':
109 109 ncolor = 6*8*5
110 110 pos = numpy.zeros((ncolor))
111 111 r = numpy.zeros((ncolor))
112 112 g = numpy.zeros((ncolor))
113 113 b = numpy.zeros((ncolor))
114 114 ind = 0
115 115 for ri in range(8):
116 116 for gi in range(8):
117 117 for bi in range(4):
118 118 r[ind] = ri/(6-1.0)
119 119 g[ind] = gi/(8-1.0)
120 120 b[ind] = bi/(5-1.0)
121 121 pos[ind] = ind/(ncolor-1.0)
122 122 ind += 1
123 123 rgb_lvl = [6,8,5] #Levels for RGB colors
124 124
125 125 if colormap == 'rgb884':
126 126 ncolor = 8*8*4
127 127 pos = numpy.zeros((ncolor))
128 128 r = numpy.zeros((ncolor))
129 129 g = numpy.zeros((ncolor))
130 130 b = numpy.zeros((ncolor))
131 131 ind = 0
132 132 for ri in range(8):
133 133 for gi in range(8):
134 134 for bi in range(4):
135 135 r[ind] = ri/(8-1.0)
136 136 g[ind] = gi/(8-1.0)
137 137 b[ind] = bi/(4-1.0)
138 138 pos[ind] = ind/(ncolor-1.0)
139 139 ind += 1
140 140 rgb_lvl = [8,8,4] #Levels for RGB colors
141 141
142 142 if ncolor == None:
143 143 raise ValueError, "The colormap selected is not valid"
144 144
145 145 plplot.plscmap1n(ncolor)
146 146 plplot.plscmap1l(1, pos, r, g, b)
147 147
148 148 return rgb_lvl
149 149
150 150 def setColormap(colormap="br_green"):
151 151 cmap1_init(colormap)
152 152
153 153 class BaseGraph:
154 154 """
155 155
156 156 """
157 157 hasNotRange = True
158 158
159 159 xrange = None
160 160 yrange = None
161 161 zrange = None
162 162
163 163 xlabel = None
164 164 ylabel = None
165 165 title = None
166 166
167 167 legends = None
168 168
169 169 __name = None
170 170
171 171 __colormap = None
172 172 __colbox = None
173 173 __colleg = None
174 174
175 175 __xpos = None
176 176 __ypos = None
177 177
178 178 __xopt = None #"bcnst"
179 179 __yopt = None #"bcnstv"
180 180
181 181 __xlpos = None
182 182 __ylpos = None
183 183
184 184 __xrangeIsTime = False
185 185
186 186 #Advanced
187 187 __xg = None
188 188 __yg = None
189 189
190 190 def __init__(self):
191 191 """
192 192
193 193 """
194 194 self.hasNotRange = True
195 195
196 196 self.xrange = None
197 197 self.yrange = None
198 198 self.zrange = None
199 199
200 200 self.xlabel = None
201 201 self.ylabel = None
202 202 self.title = None
203 203
204 204 self.legends = None
205 205
206 206 self.__name = None
207 207
208 208 self.__colormap = None
209 209 self.__colbox = None
210 210 self.__colleg = None
211 211
212 212 self.__xpos = None
213 213 self.__ypos = None
214 214
215 215 self.__xopt = None #"bcnst"
216 216 self.__yopt = None #"bcnstv"
217 217
218 218 self.__xlpos = None
219 219 self.__ylpos = None
220 220
221 221 self.__xrangeIsTime = False
222 222
223 223 #Advanced
224 224 self.__xg = None
225 225 self.__yg = None
226 226
227 227 def setName(self, name):
228 228 self.__name = name
229 229
230 230 def setScreenPos(self, xpos, ypos):
231 231 self.__xpos = xpos
232 232 self.__ypos = ypos
233 233
234 234 def setOpt(self, xopt, yopt):
235 235 self.__xopt = xopt
236 236 self.__yopt = yopt
237 237
238 238 def setXAxisAsTime(self):
239 239 self.__xrangeIsTime = True
240 240
241 241
242 242 def setup(self, title=None, xlabel=None, ylabel=None, colormap=None):
243 243 """
244 244 """
245 245 self.title = title
246 246 self.xlabel = xlabel
247 247 self.ylabel = ylabel
248 248 self.__colormap = colormap
249 249
250 250 def plotBox(self, xmin, xmax, ymin, ymax, xopt=None, yopt=None, nolabels=False):
251 251 """
252 252
253 253 """
254 254 if self.__xrangeIsTime:
255 255 plplot.pltimefmt("%H:%M")
256 256
257 257 plplot.plvpor(self.__xpos[0], self.__xpos[1], self.__ypos[0], self.__ypos[1])
258 258 plplot.plwind(float(xmin),
259 259 float(xmax),
260 260 float(ymin),
261 261 float(ymax)
262 262 )
263 263
264 264 if xopt == None: xopt = self.__xopt
265 265 if yopt == None: yopt = self.__yopt
266 266
267 267 plplot.plbox(xopt, 0.0, 0, yopt, 0.0, 0)
268 268
269 269 if not(nolabels):
270 270 plplot.pllab(self.xlabel, self.ylabel, self.title)
271 271
272 272
273 273 def colorbarPlot(self, xmin=0., xmax=1., ymin=0., ymax=1.):
274 274 data = numpy.arange(256)
275 275 data = numpy.reshape(data, (1,-1))
276 276
277 277 plplot.plimage(data,
278 278 float(xmin),
279 279 float(xmax),
280 280 float(ymin),
281 281 float(ymax),
282 282 0.,
283 283 255.,
284 284 float(xmin),
285 285 float(xmax),
286 286 float(ymin),
287 287 float(ymax))
288 288
289 289 def basicXYPlot(self, x, y, xmin=None, xmax=None, ymin=None, ymax=None):
290 290
291 291 if xmin == None: xmin = x[0]
292 292 if xmax == None: xmax = x[-1]
293 293 if ymin == None: ymin = y[0]
294 294 if ymax == None: ymax = y[-1]
295 295
296 296 plplot.plline(x, y)
297 297
298 298 def basicXYwithErrorPlot(self):
299 299 pass
300 300
301 301 def basicLineTimePlot(self, x, y, xmin=None, xmax=None, ymin=None, ymax=None, colline=1):
302 302
303 303 if xmin == None: xmin = x[0]
304 304 if xmax == None: xmax = x[-1]
305 305 if ymin == None: ymin = y[0]
306 306 if ymax == None: ymax = y[-1]
307 307
308 308 plplot.plcol0(colline)
309 309 plplot.plline(x, y)
310 310 plplot.plcol0(1)
311 311
312 312 def basicPcolorPlot(self, data, x, y, xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None):
313 313 """
314 314 """
315 315 if xmin == None: xmin = x[0]
316 316 if xmax == None: xmax = x[-1]
317 317 if ymin == None: ymin = y[0]
318 318 if ymax == None: ymax = y[-1]
319 319 if zmin == None: zmin = numpy.nanmin(data)
320 320 if zmax == None: zmax = numpy.nanmax(data)
321 321
322 322 plplot.plimage(data,
323 323 float(x[0]),
324 324 float(x[-1]),
325 325 float(y[0]),
326 326 float(y[-1]),
327 327 float(zmin),
328 328 float(zmax),
329 329 float(xmin),
330 330 float(xmax),
331 331 float(ymin),
332 332 float(ymax)
333 333 )
334 334
335 335 def __getBoxpltr(self, x, y, deltax=None, deltay=None):
336 336
337 337 if not(len(x)>1 and len(y)>1):
338 338 raise ValueError, "x axis and y axis are empty"
339 339
340 340 if deltax == None: deltax = x[-1] - x[-2]
341 341 if deltay == None: deltay = y[-1] - y[-2]
342 342
343 343 x1 = numpy.append(x, x[-1] + deltax)
344 344 y1 = numpy.append(y, y[-1] + deltay)
345 345
346 346 xg = (numpy.multiply.outer(x1, numpy.ones(len(y1))))
347 347 yg = (numpy.multiply.outer(numpy.ones(len(x1)), y1))
348 348
349 349 self.__xg = xg
350 350 self.__yg = yg
351 351
352 352 def advPcolorPlot(self, data, x, y, zmin=0., zmax=0.):
353 353 """
354 354 """
355 355
356 356 if self.__xg == None and self.__yg == None:
357 357 self.__getBoxpltr(x, y)
358 358
359 359 plplot.plimagefr(data, x[0], x[-1], y[0], y[-1], 0., 0., zmin, zmax, plplot.pltr2, self.__xg, self.__yg)
360 360
361 361
362 362 class LinearPlot:
363 363
364 364 linearGraphObj = BaseGraph()
365 365
366 366 __szchar = 1.0
367 367
368 368 __xrange = None
369 369
370 370 __yrange = None
371 371
372 372 __subpage = 0
373 373 m_BaseGraph= BaseGraph()
374 374
375 375
376 376
377 377 def __init__(self):
378 378
379 379
380 380 key = "linearplot"
381 381 self.linearGraphObj = BaseGraph()
382 382 self.linearGraphObj.setName(key)
383 383
384 384 self.__subpage = 0
385 385
386 386 def __iniSubpage(self):
387 387
388 388 if plplot.plgdev() == '':
389 389 raise ValueError, "Plot device has not been initialize"
390 390
391 391 plplot.pladv(self.__subpage)
392 392 plplot.plschr(0.0, self.__szchar)
393 393
394 394 setColormap()
395 395
396 396 def setScreenPos(self, width='small'):
397 397
398 398 if width == 'small':
399 399 xi = 0.12; yi = 0.14; xw = 0.78; yw = 0.80
400 400
401 401 if width == 'medium':
402 402 xi = 0.07; yi = 0.10; xw = 0.90; yw = 0.60
403 403
404 404 xf = xi + xw
405 405 yf = yi + yw
406 406
407 407 self.linearGraphObj.setScreenPos([xi, xf], [yi, yf])
408 408
409 409 def setup(self, subpage, title="", xlabel="", ylabel="", XAxisAsTime=False):
410 410 """
411 411 """
412 412
413 413 self.linearGraphObj.setOpt("bcnts","bcntsv")
414 414 self.linearGraphObj.setup(title,
415 415 xlabel,
416 416 ylabel
417 417 )
418 418
419 419 self.setScreenPos(width='medium')
420 420
421 421 if XAxisAsTime:
422 422 self.linearGraphObj.setXAxisAsTime()
423 423
424 424 self.__subpage = subpage
425 425 # def setRanges(self, xrange, yrange, zrange):
426 426 #
427 427 # self.linearGraphObj.setRanges(xrange, yrange, zrange)
428 428
429 429 def plotData(self, x, y=None, xmin=None, xmax=None, ymin=None, ymax=None, colline=1):
430 430 """
431 431 Inputs:
432 432
433 433 x : Numpy array of dimension 1
434 434 y : Numpy array of dimension 1
435 435
436 436 """
437 437
438 438 try:
439 439 nX = numpy.shape(x)
440 440 except:
441 441 raise ValueError, "x is not a numpy array"
442 442
443 443 if y == None: y = numpy.arange(nX)
444 444
445 445 if xmin == None: xmin = x[0]
446 446 if xmax == None: xmax = x[-1]
447 447 if ymin == None: ymin = y[0]
448 448 if ymax == None: ymax = y[-1]
449 449
450 450 self.__iniSubpage()
451 451 self.linearGraphObj.plotBox(xmin, xmax, ymin, ymax)
452 452 self.linearGraphObj.basicLineTimePlot(x, y, xmin, xmax, ymin, ymax, colline)
453 453
454 454 def plotComplexData(self, x, y, xmin=None, xmax=None, ymin=None, ymax=None, colline=1, type='power'):
455 455 """
456 456 Inputs:
457 457
458 458 x : Numpy array of dimension 1
459 459 y : Complex numpy array of dimension 1
460 460
461 461 """
462 462
463 463 try:
464 464 nX = numpy.shape(x)
465 465 except:
466 466 raise ValueError, "x is not a numpy array"
467 467
468 468 try:
469 469 nY = numpy.shape(y)
470 470 except:
471 471 raise ValueError, "y is not a numpy array"
472 472
473 473 if xmin == None: xmin = x[0]
474 474 if xmax == None: xmax = x[-1]
475 475 if ymin == None: ymin = y[0]
476 476 if ymax == None: ymax = y[-1]
477 477
478 478 self.__iniSubpage()
479 479 self.linearGraphObj.plotBox(xmin, xmax, ymin, ymax)
480 480
481 481 if type.lower() == 'power':
482 482 self.linearGraphObj.basicLineTimePlot(x, abs(y), xmin, xmax, ymin, ymax, colline)
483 483
484 484 if type.lower() == 'iq':
485 485
486 486 self.linearGraphObj.basicLineTimePlot(x, y.real, xmin, xmax, ymin, ymax, colline)
487 487 self.linearGraphObj.basicLineTimePlot(x, y.imag, xmin, xmax, ymin, ymax, colline+1)
488 488
489 489 class ColorPlot:
490 490
491 491 colorGraphObj = BaseGraph()
492 492
493 493 graphObjDict = {}
494 494
495 495 __subpage = 0
496 496
497 497 __showColorbar = False
498 498
499 499 __showPowerProfile = True
500 500
501 501 __szchar = 0.65
502 502
503 503 __xrange = None
504 504
505 505 __yrange = None
506 506
507 507 __zrange = None
508 508 m_BaseGraph= BaseGraph()
509 509
510 510
511 511
512 512 def __init__(self):
513 513
514 514 self.graphObjDict = {}
515 515
516 516 self.__subpage = 0
517 517 self.__showColorbar = False
518 518 self.__showPowerProfile = True
519 519
520 520 self.__szchar = 0.65
521 521 self.__xrange = None
522 522 self.__yrange = None
523 523 self.__zrange = None
524 524
525 525 key = "colorplot"
526 526 self.colorGraphObj = BaseGraph()
527 527 self.colorGraphObj.setName(key)
528 528
529 529 def setup(self, subpage, title="", xlabel="Frequency", ylabel="Range", colormap="br_green", showColorbar=False, showPowerProfile=False, XAxisAsTime=False):
530 530 """
531 531 """
532 532
533 533 self.colorGraphObj.setOpt("bcnts","bcntsv")
534 534 self.colorGraphObj.setup(title,
535 535 xlabel,
536 536 ylabel
537 537 )
538 538
539 539 self.__subpage = subpage
540 540 self.__colormap = colormap
541 541 self.__showColorbar = showColorbar
542 542 self.__showPowerProfile = showPowerProfile
543 543
544 544 if showColorbar:
545 545 key = "colorbar"
546 546
547 547 cmapObj = BaseGraph()
548 548 cmapObj.setName(key)
549 549 cmapObj.setOpt("bc","bcmtv")
550 550 cmapObj.setup(title="dBs",
551 551 xlabel="",
552 552 ylabel="",
553 553 colormap=colormap)
554 554
555 555 self.graphObjDict[key] = cmapObj
556 556
557 557
558 558 if showPowerProfile:
559 559 key = "powerprof"
560 560
561 561 powObj = BaseGraph()
562 562 powObj.setName(key)
563 563 powObj.setOpt("bcntg","bc")
564 564 powObj.setup(title="Power Profile",
565 565 xlabel="dB",
566 566 ylabel="")
567 567
568 568 self.graphObjDict[key] = powObj
569 569
570 570 self.setScreenPos(width='small')
571 571
572 572 if XAxisAsTime:
573 573 self.colorGraphObj.setXAxisAsTime()
574 574
575 575 def __iniSubpage(self):
576 576
577 577 if plplot.plgdev() == '':
578 578 raise ValueError, "Plot device has not been initialize"
579 579
580 580 plplot.pladv(self.__subpage)
581 581 plplot.plschr(0.0, self.__szchar)
582 582
583 583 setColormap(self.__colormap)
584 584
585 585 def setScreenPos(self, width='small'):
586 586
587 587 if width == 'small':
588 588 xi = 0.13; yi = 0.12; xw = 0.86; yw = 0.70; xcmapw = 0.04; xpoww = 0.25; deltaxcmap = 0.02; deltaxpow = 0.06
589 589
590 590 if width == 'medium':
591 591 xi = 0.07; yi = 0.10; xw = 0.90; yw = 0.60; xcmapw = 0.04; xpoww = 0.24; deltaxcmap = 0.02; deltaxpow = 0.06
592 592
593 593 if self.__showColorbar:
594 594 xw -= xcmapw + deltaxcmap
595 595
596 596 if self.__showPowerProfile:
597 597 xw -= xpoww + deltaxpow
598 598
599 599 xf = xi + xw
600 600 yf = yi + yw
601 601 xcmapf = xf
602 602
603 603 self.colorGraphObj.setScreenPos([xi, xf], [yi, yf])
604 604
605 605 if self.__showColorbar:
606 606 xcmapi = xf + deltaxcmap
607 607 xcmapf = xcmapi + xcmapw
608 608
609 609 key = "colorbar"
610 610 cmapObj = self.graphObjDict[key]
611 611 cmapObj.setScreenPos([xcmapi, xcmapf], [yi, yf])
612 612
613 613 if self.__showPowerProfile:
614 614
615 615 xpowi = xcmapf + deltaxpow
616 616 xpowf = xpowi + xpoww
617 617
618 618 key = "powerprof"
619 619 powObj = self.graphObjDict[key]
620 620 powObj.setScreenPos([xpowi, xpowf], [yi, yf])
621 621
622 622
623 623
624 def plotData(self, data, x=None, y=None, xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None):
624 def plotData(self, data, x=None, y=None, xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None, title = ''):
625 625 """
626 626 Inputs:
627 627
628 628 x : Numpy array of dimension 1
629 629 y : Numpy array of dimension 1
630 630
631 631 """
632 632
633 633 try:
634 634 nX, nY = numpy.shape(data)
635 635 except:
636 636 raise ValueError, "data is not a numpy array"
637 637
638 638 if x == None: x = numpy.arange(nX)
639 639 if y == None: y = numpy.arange(nY)
640 640
641 641 if xmin == None: xmin = x[0]
642 642 if xmax == None: xmax = x[-1]
643 643 if ymin == None: ymin = y[0]
644 644 if ymax == None: ymax = y[-1]
645 645 if zmin == None: zmin = numpy.nanmin(data)
646 646 if zmax == None: zmax = numpy.nanmax(data)
647 647
648 648 plplot.plschr(0.0, self.__szchar)
649 649 self.__iniSubpage()
650 self.colorGraphObj.title = title
650 651 self.colorGraphObj.plotBox(xmin, xmax, ymin, ymax)
651 652 self.colorGraphObj.basicPcolorPlot(data, x, y, xmin, xmax, ymin, ymax, zmin, zmax)
652 653
653 654 if self.__showColorbar:
654 655
655 656
656 657 key = "colorbar"
657 658 cmapObj = self.graphObjDict[key]
658 659
659 660 plplot.plschr(0.0, self.__szchar-0.05)
660 661 cmapObj.plotBox(0., 1., zmin, zmax)
661 662 cmapObj.colorbarPlot(0., 1., zmin, zmax)
662 663
663 664 if self.__showPowerProfile:
664 power = numpy.max(data, axis=0)
665 power = numpy.average(data, axis=0)
665 666
666 667 step = (ymax - ymin)/(nY-1)
667 668 heis = numpy.arange(ymin, ymax + step, step)
668 669
669 670 key = "powerprof"
670 671 powObj = self.graphObjDict[key]
671 672
672 673 plplot.pllsty(2)
673 674 plplot.plschr(0.0, self.__szchar-0.05)
674 675 powObj.plotBox(zmin, zmax, ymin, ymax, nolabels=True)
675 676
676 677 plplot.pllsty(1)
677 678 plplot.plschr(0.0, self.__szchar)
678 679 powObj.plotBox(zmin, zmax, ymin, ymax, xopt='bc', yopt='bc')
679 680
680 681 plplot.plcol0(9)
681 682 powObj.basicXYPlot(power, heis)
682 683 plplot.plcol0(1)
683 684
684 685
685 686 class ColorPlotX:
686 687
687 688
688 689 graphObjDict = {}
689 690 showColorbar = False
690 691 showPowerProfile = True
691 692
692 693 __szchar = 0.7
693 694 __xrange = None
694 695 __yrange = None
695 696 __zrange = None
696 697
697 698 colorGraphObj = BaseGraph()
698 699
699 700 def __init__(self):
700 701
701 702 key = "colorplot"
702 703 self.colorGraphObj.setName(key)
703 704
704 705 self.__subpage = 0
705 706
706 707 self.graphObjDict[key] = self.colorGraphObj
707 708
708 709 def __iniSubpage(self):
709 710
710 711 if plplot.plgdev() == '':
711 712 raise ValueError, "Plot device has not been initialize"
712 713
713 714 plplot.pladv(self.__subpage)
714 715 plplot.plschr(0.0, self.__szchar)
715 716
716 717 setColormap(self.__colormap)
717 718
718 719 def setScreenPos(self, xi = 0.12, yi = 0.14, xw = 0.78, yw = 0.80, xcmapw = 0.05, xpoww = 0.24, deltaxcmap = 0.02, deltaxpow = 0.06):
719 720
720 721 if self.showColorbar:
721 722 xw -= xcmapw + deltaxcmap
722 723
723 724 if self.showPowerProfile:
724 725 xw -= xpoww + deltaxpow
725 726
726 727 xf = xi + xw
727 728 yf = yi + yw
728 729 xcmapf = xf
729 730
730 731 self.colorGraphObj.setScreenPos([xi, xf], [yi, yf])
731 732
732 733 if self.showColorbar:
733 734 xcmapi = xf + deltaxcmap
734 735 xcmapf = xcmapi + xcmapw
735 736
736 737 key = "colorbar"
737 738 cmapObj = self.graphObjDict[key]
738 739 cmapObj.setScreenPos([xcmapi, xcmapf], [yi, yf])
739 740
740 741 if self.showPowerProfile:
741 742
742 743 xpowi = xcmapf + deltaxpow
743 744 xpowf = xpowi + xpoww
744 745
745 746 key = "powerprof"
746 747 powObj = self.graphObjDict[key]
747 748 powObj.setScreenPos([xpowi, xpowf], [yi, yf])
748 749
749 750 def setRanges(self, xrange, yrange, zrange):
750 751
751 752 self.colorGraphObj.setRanges(xrange, yrange, zrange)
752 753
753 754 keyList = self.graphObjDict.keys()
754 755
755 756 key = "colorbar"
756 757 if key in keyList:
757 758 cmapObj = self.graphObjDict[key]
758 759 cmapObj.setRanges([0., 1.], zrange)
759 760
760 761 key = "powerprof"
761 762 if key in keyList:
762 763 powObj = self.graphObjDict[key]
763 764 powObj.setRanges(zrange, yrange)
764 765
765 766 def setup(self, subpage, title="", xlabel="", ylabel="", colormap="jet", showColorbar=False, showPowerProfile=False, XAxisAsTime=False):
766 767 """
767 768 """
768 769
769 770 self.colorGraphObj.setSubpage(subpage)
770 771 self.colorGraphObj.setSzchar(self.__szchar)
771 772 self.colorGraphObj.setOpt("bcnts","bcntsv")
772 773 self.colorGraphObj.setup(title,
773 774 xlabel,
774 775 ylabel,
775 776 colormap)
776 777
777 778 if showColorbar:
778 779 key = "colorbar"
779 780
780 781 cmapObj = BaseGraph()
781 782 cmapObj.setName(key)
782 783 cmapObj.setSubpage(subpage)
783 784 cmapObj.setSzchar(self.__szchar)
784 785 cmapObj.setOpt("bc","bcmt")
785 786 cmapObj.setup(title="dBs",
786 787 xlabel="",
787 788 ylabel="",
788 789 colormap=colormap)
789 790
790 791 self.graphObjDict[key] = cmapObj
791 792
792 793
793 794 if showPowerProfile:
794 795 key = "powerprof"
795 796
796 797 powObj = BaseGraph()
797 798 powObj.setName(key)
798 799 powObj.setSubpage(subpage)
799 800 powObj.setSzchar(self.__szchar)
800 801 plplot.pllsty(2)
801 802 powObj.setOpt("bcntg","bc")
802 803 plplot.pllsty(1)
803 804 powObj.setup(title="Power Profile",
804 805 xlabel="dBs",
805 806 ylabel="")
806 807
807 808 self.graphObjDict[key] = powObj
808 809
809 810 self.showColorbar = showColorbar
810 811 self.showPowerProfile = showPowerProfile
811 812 self.setScreenPos()
812 813
813 814 if XAxisAsTime:
814 815 self.colorGraphObj.setXAxisAsTime()
815 816 #self.setScreenPos(xi = 0.05, yi = 0.18, xw = 0.92, yw = 0.74, xcmapw = 0.015, xpoww = 0.14, deltaxcmap = 0.01, deltaxpow = 0.02)
816 817
817 818
818 819 def plotData(self, data, x=None, y=None, xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None):
819 820 """
820 821 """
821 822
822 823 try:
823 824 nX, nY = numpy.shape(data)
824 825 except:
825 826 raise ValueError, "data is not a numpy array"
826 827
827 828 if x == None: x = numpy.arange(nX)
828 829 if y == None: y = numpy.arange(nY)
829 830
830 831 if xmin == None: xmin = x[0]
831 832 if xmax == None: xmax = x[-1]
832 833 if ymin == None: ymin = y[0]
833 834 if ymax == None: ymax = y[-1]
834 835 if zmin == None: zmin = numpy.nanmin(data)
835 836 if zmax == None: zmax = numpy.nanmax(data)
836 837
837 838 if self.colorGraphObj.hasNotRange:
838 839 self.setRanges([xmin, xmax], [ymin,ymax], [zmin,zmax])
839 840
840 841 self.colorGraphObj.initSubpage()
841 842 self.colorGraphObj.basicPcolorPlot(data, x, y, xmin, xmax, ymin, ymax, self.colorGraphObj.zrange[0], self.colorGraphObj.zrange[1])
842 843
843 844 if self.showColorbar:
844 845 key = "colorbar"
845 846 cmapObj = self.graphObjDict[key]
846 847 cmapObj.colorbarPlot()
847 848
848 849 if self.showPowerProfile:
849 850 power = numpy.average(data, axis=1)
850 851
851 852 step = (ymax - ymin)/(nY-1)
852 853 heis = numpy.arange(ymin, ymax + step, step)
853 854
854 855 key = "powerprof"
855 856 powObj = self.graphObjDict[key]
856 857 powObj.basicXYPlot(power, heis)
857 858
858 859 if __name__ == '__main__':
859 860
860 861 import numpy
861 862 plplot.plsetopt("geometry", "%dx%d" %(350*2, 300*2))
862 863 plplot.plsdev("xwin")
863 864 plplot.plscolbg(255,255,255)
864 865 plplot.plscol0(1,0,0,0)
865 866 plplot.plspause(False)
866 867 plplot.plinit()
867 868 plplot.plssub(2, 2)
868 869
869 870 nx = 64
870 871 ny = 100
871 872
872 873 data = numpy.random.uniform(-50,50,(nx,ny))
873 874
874 875 baseObj = ColorPlot()
875 876 specObj = ColorPlot()
876 877 baseObj1 = ColorPlot()
877 878 specObj1 = ColorPlot()
878 879
879 880 baseObj.setup(1, "Spectrum", "Frequency", "Range", "br_green", True, True)
880 881 specObj.setup(2, "Spectrum", "Frequency", "Range", "br_green", False, True)
881 882
882 883 baseObj1.setup(3, "Spectrum", "Frequency", "Range", "br_green", False, True)
883 884 specObj1.setup(4, "Spectrum", "Frequency", "Range", "br_green", False, True)
884 885
885 886 data = numpy.random.uniform(-50,50,(nx,ny))
886 887
887 888 plplot.plbop()
888 889 baseObj.plotData(data)
889 890
890 891 specObj.plotData(data)
891 892
892 893 baseObj1.plotData(data)
893 894
894 895 specObj1.plotData(data)
895 896
896 897 plplot.plflush()
897 898
898 899 plplot.plspause(1)
899 900 plplot.plend()
900 901 exit(0)
901 902
902 903
@@ -1,203 +1,204
1 1 '''
2 2 Created on Feb 7, 2012
3 3
4 4 @author $Author$
5 5 @version $Id$
6 6 '''
7 7
8 8 import os, sys
9 9 import numpy
10 10 import datetime
11 11 import plplot
12 12
13 13 path = os.path.split(os.getcwd())[0]
14 14 sys.path.append(path)
15 15
16 16 from Graphics.BaseGraph import *
17 17 from Model.Spectra import Spectra
18 18
19 19 class Spectrum:
20 20
21 21 __isPlotConfig = False
22 22
23 23 __isPlotIni = False
24 24
25 25 __xrange = None
26 26
27 27 __yrange = None
28 28
29 29 nGraphs = 0
30 30
31 31 indexPlot = None
32 32
33 33 graphObjList = []
34 34
35 35 spectraObj = Spectra
36 36
37 37 colorGraphObj = ColorPlot()
38 38 m_Spectra= Spectra()
39 39
40 40
41 41 m_ColorPlot= ColorPlot()
42 42
43 43
44 44
45 45
46 46
47 47 def __init__(self, Spectra, index=0):
48 48
49 49 """
50 50
51 51 Inputs:
52 52
53 53 type: "power" ->> Potencia
54 54 "iq" ->> Real + Imaginario
55 55 """
56 56
57 57 self.__isPlotConfig = False
58 58
59 59 self.__isPlotIni = False
60 60
61 61 self.__xrange = None
62 62
63 63 self.__yrange = None
64 64
65 65 self.nGraphs = 0
66 66
67 67 self.indexPlot = index
68 68
69 69 self.graphObjList = []
70 70
71 71 self.spectraObj = Spectra
72 72
73 73
74 74 def __addGraph(self, subpage, title="", xlabel="", ylabel="", showColorbar=False, showPowerProfile=True, XAxisAsTime=False):
75 75
76 76 graphObj = ColorPlot()
77 77 graphObj.setup(subpage,
78 78 title,
79 79 xlabel,
80 80 ylabel,
81 81 showColorbar=showColorbar,
82 82 showPowerProfile=showPowerProfile,
83 83 XAxisAsTime=XAxisAsTime)
84 84
85 85 self.graphObjList.append(graphObj)
86 86
87 87
88 88 def setup(self, titleList=None, xlabelList=None, ylabelList=None, showColorbar=False, showPowerProfile=True, XAxisAsTime=False):
89 89
90 90 nChan = int(self.spectraObj.m_SystemHeader.numChannels)
91 91 channels = range(nChan)
92 92
93 93 myXlabel = "Radial Velocity (m/s)"
94 94 myYlabel = "Range (km)"
95 95
96 96 for i in channels:
97 97 if titleList != None:
98 98 myTitle = titleList[i]
99 99 myXlabel = xlabelList[i]
100 100 myYlabel = ylabelList[i]
101 101
102 102 # if self.spectraObj.m_NoiseObj != None:
103 103 # noise = '%4.2fdB' %(self.spectraObj.m_NoiseObj[i])
104 104 # else:
105 105 noise = '--'
106 106
107 107 myTitle = "Channel: %d - Noise: %s" %(i, noise)
108 108
109 109 self.__addGraph(i+1,
110 110 title=myTitle,
111 111 xlabel=myXlabel,
112 112 ylabel=myYlabel,
113 113 showColorbar=showColorbar,
114 114 showPowerProfile=showPowerProfile,
115 115 XAxisAsTime=XAxisAsTime)
116 116
117 117 self.nGraphs = nChan
118 118 self.__isPlotConfig = True
119 119
120 120 def iniPlot(self, winTitle=""):
121 121
122 122 nx = int(numpy.sqrt(self.nGraphs)+1)
123 123 #ny = int(self.nGraphs/nx)
124 124
125 125 plplot.plsstrm(self.indexPlot)
126 126 plplot.plparseopts([winTitle], plplot.PL_PARSE_FULL)
127 127 plplot.plsetopt("geometry", "%dx%d" %(300*nx, 240*nx))
128 128 plplot.plsdev("xwin")
129 129 plplot.plscolbg(255,255,255)
130 130 plplot.plscol0(1,0,0,0)
131 131 plplot.plinit()
132 132 plplot.plspause(False)
133 133 plplot.pladv(0)
134 134 plplot.plssub(nx, nx)
135 135
136 136 self.__nx = nx
137 137 self.__ny = nx
138 138 self.__isPlotIni = True
139 139
140 140
141 141 def plotData(self, xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None, titleList=None, xlabelList=None, ylabelList=None, showColorbar=False, showPowerProfile=True, XAxisAsTime=False, winTitle="Spectra"):
142 142
143 143 if not(self.__isPlotConfig):
144 144 self.setup(titleList,
145 145 xlabelList,
146 146 ylabelList,
147 147 showColorbar,
148 148 showPowerProfile,
149 149 XAxisAsTime)
150 150
151 151 if not(self.__isPlotIni):
152 152 self.iniPlot(winTitle)
153 153
154 154 plplot.plsstrm(self.indexPlot)
155 155
156 156 data = 10.*numpy.log10(self.spectraObj.data_spc)
157
157 noise = 10.*numpy.log10(self.spectraObj.noise)
158 158 #data.shape = Channels x Heights x Profiles
159 159 # data = numpy.transpose( data, (0,2,1) )
160 160 #data.shape = Channels x Profiles x Heights
161 161
162 162 nChan, nX, nY = numpy.shape(data)
163 163
164 164 x = numpy.arange(nX)
165 165 y = self.spectraObj.heightList
166 166
167 167 thisDatetime = datetime.datetime.fromtimestamp(self.spectraObj.m_BasicHeader.utc)
168 168 txtDate = "Self Spectra - Date: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
169 169
170 170 if xmin == None: xmin = x[0]
171 171 if xmax == None: xmax = x[-1]
172 172 if ymin == None: ymin = y[0]
173 173 if ymax == None: ymax = y[-1]
174 174 if zmin == None: zmin = numpy.nanmin(abs(data))
175 175 if zmax == None: zmax = numpy.nanmax(abs(data))
176 176
177 177 plplot.plbop()
178 178
179 179 plplot.plssub(self.__nx, self.__ny)
180 180 for i in range(self.nGraphs):
181 181 self.graphObjList[i].plotData(data[i,:,:],
182 182 x,
183 183 y,
184 184 xmin=xmin,
185 185 xmax=xmax,
186 186 ymin=ymin,
187 187 ymax=ymax,
188 188 zmin=zmin,
189 zmax=zmax)
189 zmax=zmax,
190 title = "Channel: %d - Noise: %.2f" %(i, noise[i]))
190 191
191 192 plplot.plssub(1,0)
192 193 plplot.pladv(0)
193 194 plplot.plvpor(0., 1., 0., 1.)
194 195 plplot.plmtex("t",-1., 0.5, 0.5, txtDate)
195 196 plplot.plflush()
196 197 plplot.pleop()
197 198
198 199 def end(self):
199 200 plplot.plend()
200 201
201 202
202 203 if __name__ == '__main__':
203 204 pass No newline at end of file
@@ -1,1219 +1,1235
1 1 '''
2 2 Created on 23/01/2012
3 3
4 4 @author $Author$
5 5 @version $Id$
6 6 @version $Id$
7 7 '''
8 8
9 9 import os, sys
10 10 import glob
11 11 import time
12 12 import numpy
13 13 import fnmatch
14 14 import time, datetime
15 15
16 16 path = os.path.split(os.getcwd())[0]
17 17 sys.path.append(path)
18 18
19 19 from Model.JROHeader import *
20 20 from Model.JROData import JROData
21 21
22 22 def checkForRealPath(path, year, doy, set, ext):
23 23 """
24 24 Por ser Linux Case Sensitive entonces checkForRealPath encuentra el nombre correcto de un path,
25 25 Prueba por varias combinaciones de nombres entre mayusculas y minusculas para determinar
26 26 el path exacto de un determinado file.
27 27
28 28 Example :
29 29 nombre correcto del file es .../.../D2009307/P2009307367.ext
30 30
31 31 Entonces la funcion prueba con las siguientes combinaciones
32 32 .../.../x2009307/y2009307367.ext
33 33 .../.../x2009307/Y2009307367.ext
34 34 .../.../X2009307/y2009307367.ext
35 35 .../.../X2009307/Y2009307367.ext
36 36 siendo para este caso, la ultima combinacion de letras, identica al file buscado
37 37
38 38 Return:
39 39 Si encuentra la cobinacion adecuada devuelve el path completo y el nombre del file
40 40 caso contrario devuelve None como path y el la ultima combinacion de nombre en mayusculas
41 41 para el filename
42 42 """
43 43 filepath = None
44 44 find_flag = False
45 45 filename = None
46 46
47 47 if ext.lower() == ".r": #voltage
48 48 header1 = "dD"
49 49 header2 = "dD"
50 50 elif ext.lower() == ".pdata": #spectra
51 51 header1 = "dD"
52 52 header2 = "pP"
53 53 else:
54 54 return None, filename
55 55
56 56 for dir in header1: #barrido por las dos combinaciones posibles de "D"
57 57 for fil in header2: #barrido por las dos combinaciones posibles de "D"
58 58 doypath = "%s%04d%03d" % ( dir, year, doy ) #formo el nombre del directorio xYYYYDDD (x=d o x=D)
59 59 filename = "%s%04d%03d%03d%s" % ( fil, year, doy, set, ext ) #formo el nombre del file xYYYYDDDSSS.ext
60 60 filepath = os.path.join( path, doypath, filename ) #formo el path completo
61 61 if os.path.exists( filepath ): #verifico que exista
62 62 find_flag = True
63 63 break
64 64 if find_flag:
65 65 break
66 66
67 67 if not(find_flag):
68 68 return None, filename
69 69
70 70 return filepath, filename
71 71
72 72
73 73 def isNumber(str):
74 74 """
75 75 Chequea si el conjunto de caracteres que componen un string puede ser convertidos a un numero.
76 76
77 77 Excepciones:
78 78 Si un determinado string no puede ser convertido a numero
79 79 Input:
80 80 str, string al cual se le analiza para determinar si convertible a un numero o no
81 81
82 82 Return:
83 83 True : si el string es uno numerico
84 84 False : no es un string numerico
85 85 """
86 86 try:
87 87 float( str )
88 88 return True
89 89 except:
90 90 return False
91 91
92 92
93 93 def isThisFileinRange(filename, startUTSeconds, endUTSeconds):
94 94 """
95 95 Esta funcion determina si un archivo de datos se encuentra o no dentro del rango de fecha especificado.
96 96
97 97 Inputs:
98 98 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
99 99
100 100 startUTSeconds : fecha inicial del rango seleccionado. La fecha esta dada en
101 101 segundos contados desde 01/01/1970.
102 102 endUTSeconds : fecha final del rango seleccionado. La fecha esta dada en
103 103 segundos contados desde 01/01/1970.
104 104
105 105 Return:
106 106 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
107 107 fecha especificado, de lo contrario retorna False.
108 108
109 109 Excepciones:
110 110 Si el archivo no existe o no puede ser abierto
111 111 Si la cabecera no puede ser leida.
112 112
113 113 """
114 114 m_BasicHeader = BasicHeader()
115 115
116 116 try:
117 117 fp = open(filename,'rb')
118 118 except:
119 119 raise IOError, "The file %s can't be opened" %(filename)
120 120
121 121 sts = m_BasicHeader.read(fp)
122 122 fp.close()
123 123
124 124 if not(sts):
125 125 print "Skipping the file %s because it has not a valid header" %(filename)
126 126 return 0
127 127
128 128 if not ((startUTSeconds <= m_BasicHeader.utc) and (endUTSeconds > m_BasicHeader.utc)):
129 129 return 0
130 130
131 131 return 1
132 132
133 133
134 134 def getlastFileFromPath(path, ext):
135 135 """
136 136 Depura el fileList dejando solo los que cumplan el formato de "PYYYYDDDSSS.ext"
137 137 al final de la depuracion devuelve el ultimo file de la lista que quedo.
138 138
139 139 Input:
140 140 fileList : lista conteniendo todos los files (sin path) que componen una determinada carpeta
141 141 ext : extension de los files contenidos en una carpeta
142 142
143 143 Return:
144 144 El ultimo file de una determinada carpeta, no se considera el path.
145 145 """
146 146 validFilelist = []
147 147 fileList = os.listdir(path)
148 148
149 149 # 0 1234 567 89A BCDE
150 150 # H YYYY DDD SSS .ext
151 151
152 152 for file in fileList:
153 153 try:
154 154 year = int(file[1:5])
155 155 doy = int(file[5:8])
156 156
157 157 if (os.path.splitext(file)[-1].upper() != ext.upper()) : continue
158 158 except:
159 159 continue
160 160
161 161 validFilelist.append(file)
162 162
163 163 if validFilelist:
164 164 validFilelist = sorted( validFilelist, key=str.lower )
165 165 return validFilelist[-1]
166 166
167 167 return None
168 168
169 169 class JRODataIO:
170 170
171 171 #speed of light
172 172 c = 3E8
173 173
174 174 m_BasicHeader = BasicHeader()
175 175
176 176 m_SystemHeader = SystemHeader()
177 177
178 178 m_RadarControllerHeader = RadarControllerHeader()
179 179
180 180 m_ProcessingHeader = ProcessingHeader()
181 181
182 m_DataObj = None
182 dataOutObj = None
183 183
184 184 online = 0
185 185
186 186 fp = None
187 187
188 188 dataType = None
189 189
190 190 fileSizeByHeader = None
191 191
192 192 filenameList = []
193 193
194 194 filename = None
195 195
196 196 fileSize = None
197 197
198 198 firstHeaderSize = 0
199 199
200 200 basicHeaderSize = 24
201 201
202 202 nTotalBlocks = 0
203 203
204 204 ippSeconds = 0
205 205
206 206 blocksize = 0
207 207
208 208 set = 0
209 209
210 210 ext = None
211 211
212 212 path = None
213 213
214 214 maxTimeStep = 30
215 215
216 216
217 217 delay = 3 #seconds
218 218
219 219 nTries = 3 #quantity tries
220 220
221 221 nFiles = 3 #number of files for searching
222 222
223 223
224 224 flagNoMoreFiles = 0
225 225
226 226 flagIsNewFile = 1
227 227
228 228 flagResetProcessing = 0
229 229
230 230 flagIsNewBlock = 0
231 231
232 232 def __init__(self):
233 233 pass
234 234
235 235 class JRODataReader(JRODataIO):
236 236
237 237 """
238 238 Esta clase es usada como la clase padre de las clases VoltageReader y SpectraReader.
239 239 Contiene todos lo metodos necesarios para leer datos desde archivos en formato
240 240 jicamarca o pdata (.r o .pdata). La lectura de los datos siempre se realiza por bloques. Los datos
241 241 leidos son array de 3 dimensiones:
242 242
243 243 Para Voltajes - perfiles * alturas * canales
244 244
245 245 Para Spectra - paresCanalesIguales * alturas * perfiles (Self Spectra)
246 246 paresCanalesDiferentes * alturas * perfiles (Cross Spectra)
247 247 canales * alturas (DC Channels)
248 248
249 249 y son almacenados en su buffer respectivo.
250 250
251 251 Esta clase contiene instancias (objetos) de las clases BasicHeader, SystemHeader,
252 252 RadarControllerHeader y DataObj. Los tres primeros se usan para almacenar informacion de la
253 253 cabecera de datos (metadata), y el cuarto (DataObj) para obtener y almacenar los datos desde
254 254 el buffer de datos cada vez que se ejecute el metodo "getData".
255 255 """
256 256
257 257 nReadBlocks = 0
258 258
259 def __init__(self, m_DataObj=None):
259 def __init__(self, dataOutObj=None):
260 260
261 261 raise ValueError, "This class can't be instanced"
262 262
263 263
264 264 def hasNotDataInBuffer(self):
265 265
266 266 raise ValueError, "Not implemented"
267 267
268 268 def getBlockDimension(self):
269 269
270 270 raise ValueError, "No implemented"
271 271
272 272 def readBlock(self):
273 273
274 274 self.nTotalBlocks += 1
275 275 self.nReadBlocks += 1
276 276
277 277 raise ValueError, "This method has not been implemented"
278 278
279 279 def getData( self ):
280 280
281 281 raise ValueError, "This method has not been implemented"
282 282
283 def createObjByDefault(self):
284 """
285 Los objetos creados por defecto por cada clase (Voltaje o Espectro) difieren en el tipo
286 raise ValueError, "This method has not been implemented
287 """
288 raise ValueError, "This method has not been implemented"
289
290 def setup(self, dataOutObj=None, path=None, startDateTime=None, endDateTime=None, set=0, expLabel = "", ext = None, online = 0):
291 """
292 setup configura los parametros de lectura de la clase DataReader.
293
294 Si el modo de lectura es offline, primero se realiza una busqueda de todos los archivos
295 que coincidan con los parametros especificados; esta lista de archivos son almacenados en
296 self.filenameList.
297
298 Input:
299 path : Directorios donde se ubican los datos a leer. Dentro de este
300 directorio deberia de estar subdirectorios de la forma:
301
302 path/D[yyyy][ddd]/expLabel/P[yyyy][ddd][sss][ext]
303
304 startDateTime : Fecha inicial. Rechaza todos los archivos donde
305 file end time < startDatetime (obejto datetime.datetime)
306
307 endDateTime : Fecha final. Si no es None, rechaza todos los archivos donde
308 file end time < startDatetime (obejto datetime.datetime)
309
310 set : Set del primer archivo a leer. Por defecto None
311
312 expLabel : Nombre del subdirectorio de datos. Por defecto ""
313
314 ext : Extension de los archivos a leer. Por defecto .r
315
316 online : Si es == a 0 entonces busca files que cumplan con las condiciones dadas
317
318 Return:
319 0 : Si no encuentra files que cumplan con las condiciones dadas
320 1 : Si encuentra files que cumplan con las condiciones dadas
321
322 Affected:
323 self.startUTCSeconds
324 self.endUTCSeconds
325 self.startYear
326 self.endYear
327 self.startDoy
328 self.endDoy
329 self.pathList
330 self.filenameList
331 self.online
332 """
333 if path == None:
334 raise ValueError, "The path is not valid"
335
336 if ext == None:
337 ext = self.ext
338
339 if dataOutObj == None:
340 dataOutObj = self.createObjByDefault()
341
342 self.dataOutObj = dataOutObj
343
344 if online:
345 print "Searching files ..."
346 doypath, file, year, doy, set = self.__searchFilesOnLine(path, startDateTime, endDateTime, expLabel, ext)
347
348 if not(doypath):
349 for nTries in range( self.nTries ):
350 print '\tWaiting %0.2f sec for valid file in %s: try %02d ...' % (self.delay, path, nTries+1)
351 time.sleep( self.delay )
352 doypath, file, year, doy, set = self.__searchFilesOnLine(path, startDateTime, endDateTime, expLabel, ext)
353 if doypath:
354 break
355
356 if not(doypath):
357 print "There 'isn't valied files in %s" % path
358 return None
359
360 self.year = year
361 self.doy = doy
362 self.set = set - 1
363 self.path = path
364
365 else: # offline
366 pathList, filenameList = self.__searchFilesOffLine(path, startDateTime, endDateTime, set, expLabel, ext)
367 if not(pathList):
368 print "No files in range: %s - %s" %(startDateTime.ctime(), endDateTime.ctime())
369 return None
370
371 self.fileIndex = -1
372 self.pathList = pathList
373 self.filenameList = filenameList
374
375 self.online = online
376 self.ext = ext
377
378 ext = ext.lower()
379
380 if not( self.setNextFile() ):
381 if (startDateTime != None) and (endDateTime != None):
382 print "No files in range: %s - %s" %(startDateTime.ctime(), endDateTime.ctime())
383 elif startDateTime != None:
384 print "No files in : %s" % startDateTime.ctime()
385 else:
386 print "No files"
387 return None
388
389 if startDateTime != None:
390 self.startUTCSeconds = time.mktime(startDateTime.timetuple())
391 self.startYear = startDateTime.timetuple().tm_year
392 self.startDoy = startDateTime.timetuple().tm_yday
393
394 if endDateTime != None:
395 self.endUTCSeconds = time.mktime(endDateTime.timetuple())
396 self.endYear = endDateTime.timetuple().tm_year
397 self.endDoy = endDateTime.timetuple().tm_yday
398 #call fillHeaderValues() - to Data Object
399
400 self.updateDataHeader()
401
402 return self.dataOutObj
283 403
284 404 def __rdSystemHeader(self, fp=None):
285 405
286 406 if fp == None:
287 407 fp = self.fp
288 408
289 409 self.m_SystemHeader.read(fp)
290 410
291 411
292 412 def __rdRadarControllerHeader(self, fp=None):
293 413 if fp == None:
294 414 fp = self.fp
295 415
296 416 self.m_RadarControllerHeader.read(fp)
297 417
298 418
299 419 def __rdProcessingHeader(self, fp=None):
300 420 if fp == None:
301 421 fp = self.fp
302 422
303 423 self.m_ProcessingHeader.read(fp)
304 424
305 425
306 426 def __rdBasicHeader(self, fp=None):
307 427
308 428 if fp == None:
309 429 fp = self.fp
310 430
311 431 self.m_BasicHeader.read(fp)
312 432
313 433 def __readFirstHeader(self):
314 434 """
315 435 Lectura del First Header, es decir el Basic Header y el Long Header
316 436
317 437 Affected:
318 438 self.m_BasicHeader
319 439 self.m_SystemHeader
320 440 self.m_RadarControllerHeader
321 441 self.m_ProcessingHeader
322 442 self.firstHeaderSize
323 443 self.dataType
324 444 self.fileSizeByHeader
325 445 self.ippSeconds
326 446
327 447 Return:
328 448 None
329 449 """
330 450 self.__rdBasicHeader()
331 451 self.__rdSystemHeader()
332 452 self.__rdRadarControllerHeader()
333 453 self.__rdProcessingHeader()
334 454 self.firstHeaderSize = self.m_BasicHeader.size
335 455
336 456 datatype = int(numpy.log2((self.m_ProcessingHeader.processFlags & PROCFLAG.DATATYPE_MASK))-numpy.log2(PROCFLAG.DATATYPE_CHAR))
337 457 if datatype == 0:
338 458 datatype_str = numpy.dtype([('real','<i1'),('imag','<i1')])
339 459
340 460 elif datatype == 1:
341 461 datatype_str = numpy.dtype([('real','<i2'),('imag','<i2')])
342 462
343 463 elif datatype == 2:
344 464 datatype_str = numpy.dtype([('real','<i4'),('imag','<i4')])
345 465
346 466 elif datatype == 3:
347 467 datatype_str = numpy.dtype([('real','<i8'),('imag','<i8')])
348 468
349 469 elif datatype == 4:
350 470 datatype_str = numpy.dtype([('real','<f4'),('imag','<f4')])
351 471
352 472 elif datatype == 5:
353 473 datatype_str = numpy.dtype([('real','<f8'),('imag','<f8')])
354 474
355 475 else:
356 476 raise ValueError, 'Data type was not defined'
357 477
358 478 self.dataType = datatype_str
359 479 self.ippSeconds = 2 * 1000 * self.m_RadarControllerHeader.ipp / self.c
360 480
361 481 self.fileSizeByHeader = self.m_ProcessingHeader.dataBlocksPerFile * self.m_ProcessingHeader.blockSize + self.firstHeaderSize + self.basicHeaderSize*(self.m_ProcessingHeader.dataBlocksPerFile - 1)
362
482 self.dataOutObj.channelList = numpy.arange(self.m_SystemHeader.numChannels)
483 self.dataOutObj.channelIndexList = numpy.arange(self.m_SystemHeader.numChannels)
484
363 485 self.getBlockDimension()
364 486
365 487
366 488 def __setNewBlock(self):
367 489 """
368 490 Lee el Basic Header y posiciona le file pointer en la posicion inicial del bloque a leer
369 491
370 492 Affected:
371 493 self.m_BasicHeader
372 494 self.flagNoContinuousBlock
373 495 self.ns
374 496
375 497 Return:
376 498 0 : Si el file no tiene un Basic Header que pueda ser leido
377 499 1 : Si se pudo leer el Basic Header
378 500 """
379 501 if self.fp == None:
380 502 return 0
381 503
382 504 if self.flagIsNewFile:
383 505 return 1
384 506
385 507 self.lastUTTime = self.m_BasicHeader.utc
386 508
387 509 currentSize = self.fileSize - self.fp.tell()
388 510 neededSize = self.m_ProcessingHeader.blockSize + self.basicHeaderSize
389 511
390 512 #If there is enough data setting new data block
391 513 if ( currentSize >= neededSize ):
392 514 self.__rdBasicHeader()
393 515 return 1
394 516
395 517 #si es OnLine y ademas aun no se han leido un bloque completo entonces se espera por uno valido
396 518 if self.online and (self.nReadBlocks < self.m_ProcessingHeader.dataBlocksPerFile):
397 519
398 520 fpointer = self.fp.tell()
399 521
400 522 for nTries in range( self.nTries ):
401 523 #self.fp.close()
402 524
403 525 print "\tWaiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries+1)
404 526 time.sleep( self.delay )
405 527
406 528 #self.fp = open( self.filename, 'rb' )
407 529 #self.fp.seek( fpointer )
408 530
409 531 self.fileSize = os.path.getsize( self.filename )
410 532 currentSize = self.fileSize - fpointer
411 533
412 534 if ( currentSize >= neededSize ):
413 535 self.__rdBasicHeader()
414 536 return 1
415 537
416 538 #Setting new file
417 539 if not( self.setNextFile() ):
418 540 return 0
419 541
420 542 deltaTime = self.m_BasicHeader.utc - self.lastUTTime # check this
421 543
422 544 self.flagResetProcessing = 0
423 545
424 546 if deltaTime > self.maxTimeStep:
425 547 self.flagResetProcessing = 1
426 548
427 549 return 1
428 550
429 551 def readNextBlock(self):
430 552 """
431 553 Establece un nuevo bloque de datos a leer y los lee, si es que no existiese
432 554 mas bloques disponibles en el archivo actual salta al siguiente.
433 555
434 556 Affected:
435 557 self.lastUTTime
436 558
437 559 Return: None
438 560 """
439 561
440 562 if not(self.__setNewBlock()):
441 563 return 0
442 564
443 565 if not(self.readBlock()):
444 566 return 0
445 567
446 568 return 1
447 569
448 570 def __setNextFileOnline(self):
449 571 """
450 572 Busca el siguiente file que tenga suficiente data para ser leida, dentro de un folder especifico, si
451 573 no encuentra un file valido espera un tiempo determinado y luego busca en los posibles n files
452 574 siguientes.
453 575
454 576 Affected:
455 577 self.flagIsNewFile
456 578 self.filename
457 579 self.fileSize
458 580 self.fp
459 581 self.set
460 582 self.flagNoMoreFiles
461 583
462 584 Return:
463 585 0 : si luego de una busqueda del siguiente file valido este no pudo ser encontrado
464 586 1 : si el file fue abierto con exito y esta listo a ser leido
465 587
466 588 Excepciones:
467 589 Si un determinado file no puede ser abierto
468 590 """
469 591 nFiles = 0
470 592 fileOk_flag = False
471 593 firstTime_flag = True
472 594
473 595 self.set += 1
474 596
475 597 #busca el 1er file disponible
476 598 file, filename = checkForRealPath( self.path, self.year, self.doy, self.set, self.ext )
477 599 if file:
478 600 if self.__verifyFile(file, False):
479 601 fileOk_flag = True
480 602
481 603 #si no encuentra un file entonces espera y vuelve a buscar
482 604 if not(fileOk_flag):
483 605 for nFiles in range(self.nFiles+1): #busco en los siguientes self.nFiles+1 files posibles
484 606
485 607 if firstTime_flag: #si es la 1era vez entonces hace el for self.nTries veces
486 608 tries = self.nTries
487 609 else:
488 610 tries = 1 #si no es la 1era vez entonces solo lo hace una vez
489 611
490 612 for nTries in range( tries ):
491 613 if firstTime_flag:
492 614 print "\tWaiting %0.2f sec for new \"%s\" file, try %03d ..." % ( self.delay, filename, nTries+1 )
493 615 time.sleep( self.delay )
494 616 else:
495 617 print "\tSearching next \"%s%04d%03d%03d%s\" file ..." % (self.optchar, self.year, self.doy, self.set, self.ext)
496 618
497 619 file, filename = checkForRealPath( self.path, self.year, self.doy, self.set, self.ext )
498 620 if file:
499 621 if self.__verifyFile(file):
500 622 fileOk_flag = True
501 623 break
502 624
503 625 if fileOk_flag:
504 626 break
505 627
506 628 firstTime_flag = False
507 629
508 630 print "\tSkipping the file \"%s\" due to this file doesn't exist yet" % filename
509 631 self.set += 1
510 632
511 633 if nFiles == (self.nFiles-1): #si no encuentro el file buscado cambio de carpeta y busco en la siguiente carpeta
512 634 self.set = 0
513 635 self.doy += 1
514 636
515 637 if fileOk_flag:
516 638 self.fileSize = os.path.getsize( file )
517 639 self.filename = file
518 640 self.flagIsNewFile = 1
519 641 if self.fp != None: self.fp.close()
520 642 self.fp = open(file)
521 643 self.flagNoMoreFiles = 0
522 644 print 'Setting the file: %s' % file
523 645 else:
524 646 self.fileSize = 0
525 647 self.filename = None
526 648 self.flagIsNewFile = 0
527 649 self.fp = None
528 650 self.flagNoMoreFiles = 1
529 651 print 'No more Files'
530 652
531 653 return fileOk_flag
532 654
533 655
534 656 def __setNextFileOffline(self):
535 657 """
536 658 Busca el siguiente file dentro de un folder que tenga suficiente data para ser leida
537 659
538 660 Affected:
539 661 self.flagIsNewFile
540 662 self.fileIndex
541 663 self.filename
542 664 self.fileSize
543 665 self.fp
544 666
545 667 Return:
546 668 0 : si un determinado file no puede ser abierto
547 669 1 : si el file fue abierto con exito
548 670
549 671 Excepciones:
550 672 Si un determinado file no puede ser abierto
551 673 """
552 674 idFile = self.fileIndex
553 675
554 676 while(True):
555 677
556 678 idFile += 1
557 679
558 680 if not(idFile < len(self.filenameList)):
559 681 self.flagNoMoreFiles = 1
560 682 print 'No more Files'
561 683 return 0
562 684
563 685 filename = self.filenameList[idFile]
564 686
565 687 if not(self.__verifyFile(filename)):
566 688 continue
567 689
568 690 fileSize = os.path.getsize(filename)
569 691 fp = open(filename,'rb')
570 692 break
571 693
572 694 self.flagIsNewFile = 1
573 695 self.fileIndex = idFile
574 696 self.filename = filename
575 697 self.fileSize = fileSize
576 698 self.fp = fp
577 699
578 700 print 'Setting the file: %s'%self.filename
579 701
580 702 return 1
581 703
582 704
583 705 def setNextFile(self):
584 706 """
585 707 Determina el siguiente file a leer y si hay uno disponible lee el First Header
586 708
587 709 Affected:
588 710 self.m_BasicHeader
589 711 self.m_SystemHeader
590 712 self.m_RadarControllerHeader
591 713 self.m_ProcessingHeader
592 714 self.firstHeaderSize
593 715
594 716 Return:
595 717 0 : Si no hay files disponibles
596 718 1 : Si hay mas files disponibles
597 719 """
598 720 if self.fp != None:
599 721 self.fp.close()
600 722
601 723 if self.online:
602 724 newFile = self.__setNextFileOnline()
603 725 else:
604 726 newFile = self.__setNextFileOffline()
605 727
606 728 if not(newFile):
607 729 return 0
608 730
609 731 self.__readFirstHeader()
610 732 self.nReadBlocks = 0
611 733 return 1
612 734
613 735 def __searchFilesOnLine(self, path, startDateTime=None, endDateTime=None, expLabel = "", ext = None):
614 736 """
615 737 Busca el ultimo archivo de la ultima carpeta (determinada o no por startDateTime) y
616 738 devuelve el archivo encontrado ademas de otros datos.
617 739
618 740 Input:
619 741 path : carpeta donde estan contenidos los files que contiene data
620 742 startDateTime : punto especifico en el tiempo del cual se requiere la data
621 743 ext : extension de los files
622 744
623 745 Return:
624 746 year : el anho
625 747 doy : el numero de dia del anho
626 748 set : el set del archivo
627 749 filename : el ultimo file de una determinada carpeta
628 750 directory : eL directorio donde esta el file encontrado
629 751 """
630 752 dirList = []
631 753 pathList = []
632 754 directory = None
633 755
634 756 for thisPath in os.listdir(path):
635 757 if os.path.isdir(os.path.join(path,thisPath)):
636 758 dirList.append(thisPath)
637 759
638 760 if not(dirList):
639 761 return None, None, None, None, None
640 762
641 763 dirList = sorted( dirList, key=str.lower )
642 764
643 765 if startDateTime:
644 766 thisDateTime = startDateTime
645 767 if endDateTime == None: endDateTime = startDateTime
646 768
647 769 while(thisDateTime <= endDateTime):
648 770 year = thisDateTime.timetuple().tm_year
649 771 doy = thisDateTime.timetuple().tm_yday
650 772
651 773 match = fnmatch.filter(dirList, '?' + '%4.4d%3.3d' % (year,doy))
652 774 if len(match) == 0:
653 775 thisDateTime += datetime.timedelta(1)
654 776 continue
655 777
656 778 pathList.append(os.path.join(path,match[0], expLabel))
657 779 thisDateTime += datetime.timedelta(1)
658 780
659 781 if not(pathList):
660 782 print "\tNo files in range: %s - %s" %(startDateTime.ctime(), endDateTime.ctime())
661 783 return None, None, None, None, None
662 784
663 785 directory = pathList[0]
664 786
665 787 else:
666 788 directory = dirList[-1]
667 789 directory = os.path.join(path,directory)
668 790
669 791 filename = getlastFileFromPath(directory, ext)
670 792
671 793 if not(filename):
672 794 return None, None, None, None, None
673 795
674 796 if not(self.__verifyFile(os.path.join(directory, filename))):
675 797 return None, None, None, None, None
676 798
677 799 year = int( filename[1:5] )
678 800 doy = int( filename[5:8] )
679 801 set = int( filename[8:11] )
680 802
681 803 return directory, filename, year, doy, set
682 804
683 805
684 806 def __searchFilesOffLine(self, path, startDateTime, endDateTime, set=None, expLabel = "", ext = ".r"):
685 807 """
686 808 Realiza una busqueda de los archivos que coincidan con los parametros
687 809 especificados y se encuentren ubicados en el path indicado. Para realizar una busqueda
688 810 correcta la estructura de directorios debe ser la siguiente:
689 811
690 812 ...path/D[yyyy][ddd]/expLabel/D[yyyy][ddd][sss].ext
691 813
692 814 [yyyy]: anio
693 815 [ddd] : dia del anio
694 816 [sss] : set del archivo
695 817
696 818 Inputs:
697 819 path : Directorio de datos donde se realizara la busqueda. Todos los
698 820 ficheros que concidan con el criterio de busqueda seran
699 821 almacenados en una lista y luego retornados.
700 822 startDateTime : Fecha inicial. Rechaza todos los archivos donde
701 823 file end time < startDateTime (obejto datetime.datetime)
702 824
703 825 endDateTime : Fecha final. Rechaza todos los archivos donde
704 826 file start time > endDateTime (obejto datetime.datetime)
705 827
706 828 set : Set del primer archivo a leer. Por defecto None
707 829
708 830 expLabel : Nombre del subdirectorio de datos. Por defecto ""
709 831
710 832 ext : Extension de los archivos a leer. Por defecto .r
711 833
712 834 Return:
713 835
714 836 (pathList, filenameList)
715 837
716 838 pathList : Lista de directorios donde se encontraron archivos dentro
717 839 de los parametros especificados
718 840 filenameList : Lista de archivos (ruta completa) que coincidieron con los
719 841 parametros especificados.
720 842
721 843 Variables afectadas:
722 844
723 845 self.filenameList: Lista de archivos (ruta completa) que la clase utiliza
724 846 como fuente para leer los bloque de datos, si se termina
725 847 de leer todos los bloques de datos de un determinado
726 848 archivo se pasa al siguiente archivo de la lista.
727 849
728 850 Excepciones:
729 851
730 852 """
731 853
732 854 print "Searching files ..."
733 855
734 856 dirList = []
735 857 for thisPath in os.listdir(path):
736 858 if os.path.isdir(os.path.join(path,thisPath)):
737 859 dirList.append(thisPath)
738 860
739 861 if not(dirList):
740 862 return None, None
741 863
742 864 pathList = []
743 865
744 866 thisDateTime = startDateTime
745 867
746 868 while(thisDateTime <= endDateTime):
747 869 year = thisDateTime.timetuple().tm_year
748 870 doy = thisDateTime.timetuple().tm_yday
749 871
750 872 match = fnmatch.filter(dirList, '?' + '%4.4d%3.3d' % (year,doy))
751 873 if len(match) == 0:
752 874 thisDateTime += datetime.timedelta(1)
753 875 continue
754 876
755 877 pathList.append(os.path.join(path,match[0],expLabel))
756 878 thisDateTime += datetime.timedelta(1)
757 879
758 880 startUtSeconds = time.mktime(startDateTime.timetuple())
759 881 endUtSeconds = time.mktime(endDateTime.timetuple())
760 882
761 883 filenameList = []
762 884 for thisPath in pathList:
763 885 fileList = glob.glob1(thisPath, "*%s" %ext)
764 886 fileList.sort()
765 887 for file in fileList:
766 888 filename = os.path.join(thisPath,file)
767 889 if isThisFileinRange(filename, startUtSeconds, endUtSeconds):
768 890 filenameList.append(filename)
769 891
770 892 if not(filenameList):
771 893 return None, None
772 894
773 895 self.filenameList = filenameList
774 896
775 897 return pathList, filenameList
776 898
777 899 def __verifyFile(self, filename, msgFlag=True):
778 900 """
779 901 Verifica que el filename tenga data valida, para ello leo el FirstHeader del file
780 902
781 903 Return:
782 904 0 : file no valido para ser leido
783 905 1 : file valido para ser leido
784 906 """
785 907 msg = None
786 908
787 909 try:
788 910 fp = open( filename,'rb' ) #lectura binaria
789 911 currentPosition = fp.tell()
790 912 except:
791 913 if msgFlag:
792 914 print "The file %s can't be opened" % (filename)
793 915 return False
794 916
795 917 neededSize = self.m_ProcessingHeader.blockSize + self.firstHeaderSize
796 918
797 919 if neededSize == 0:
798 920
799 921 m_BasicHeader = BasicHeader()
800 922 m_SystemHeader = SystemHeader()
801 923 m_RadarControllerHeader = RadarControllerHeader()
802 924 m_ProcessingHeader = ProcessingHeader()
803 925
804 926 try:
805 927 if not( m_BasicHeader.read(fp) ): raise ValueError
806 928 if not( m_SystemHeader.read(fp) ): raise ValueError
807 929 if not( m_RadarControllerHeader.read(fp) ): raise ValueError
808 930 if not( m_ProcessingHeader.read(fp) ): raise ValueError
809 931 data_type = int(numpy.log2((m_ProcessingHeader.processFlags & PROCFLAG.DATATYPE_MASK))-numpy.log2(PROCFLAG.DATATYPE_CHAR))
810 932
811 933 neededSize = m_ProcessingHeader.blockSize + m_BasicHeader.size
812 934
813 935 except:
814 936 if msgFlag:
815 937 print "\tThe file %s is empty or it hasn't enough data" % filename
816 938
817 939 fp.close()
818 940 return False
819 941
820 942 else:
821 943 msg = "\tSkipping the file %s due to it hasn't enough data" %filename
822 944
823 945 fp.close()
824 946 fileSize = os.path.getsize(filename)
825 947 currentSize = fileSize - currentPosition
826 948
827 949 if currentSize < neededSize:
828 950 if msgFlag and (msg != None):
829 951 print msg #print"\tSkipping the file %s due to it hasn't enough data" %filename
830 952 return False
831 953
832 954 return True
833 955
834 956 def updateDataHeader(self):
835 957
836 self.m_DataObj.m_BasicHeader = self.m_BasicHeader.copy()
837 self.m_DataObj.m_ProcessingHeader = self.m_ProcessingHeader.copy()
838 self.m_DataObj.m_RadarControllerHeader = self.m_RadarControllerHeader.copy()
839 self.m_DataObj.m_SystemHeader = self.m_SystemHeader.copy()
840
841 self.m_DataObj.dataType = self.dataType
842 self.m_DataObj.updateObjFromHeader()
843
844 def setup(self, path, startDateTime=None, endDateTime=None, set=0, expLabel = "", ext = None, online = 0):
845 """
846 setup configura los parametros de lectura de la clase DataReader.
847
848 Si el modo de lectura es offline, primero se realiza una busqueda de todos los archivos
849 que coincidan con los parametros especificados; esta lista de archivos son almacenados en
850 self.filenameList.
851
852 Input:
853 path : Directorios donde se ubican los datos a leer. Dentro de este
854 directorio deberia de estar subdirectorios de la forma:
855
856 path/D[yyyy][ddd]/expLabel/P[yyyy][ddd][sss][ext]
857
858 startDateTime : Fecha inicial. Rechaza todos los archivos donde
859 file end time < startDatetime (obejto datetime.datetime)
860
861 endDateTime : Fecha final. Si no es None, rechaza todos los archivos donde
862 file end time < startDatetime (obejto datetime.datetime)
863
864 set : Set del primer archivo a leer. Por defecto None
865
866 expLabel : Nombre del subdirectorio de datos. Por defecto ""
867
868 ext : Extension de los archivos a leer. Por defecto .r
869
870 online : Si es == a 0 entonces busca files que cumplan con las condiciones dadas
871
872 Return:
873 0 : Si no encuentra files que cumplan con las condiciones dadas
874 1 : Si encuentra files que cumplan con las condiciones dadas
875
876 Affected:
877 self.startUTCSeconds
878 self.endUTCSeconds
879 self.startYear
880 self.endYear
881 self.startDoy
882 self.endDoy
883 self.pathList
884 self.filenameList
885 self.online
886 """
958 self.dataOutObj.m_BasicHeader = self.m_BasicHeader.copy()
959 self.dataOutObj.m_ProcessingHeader = self.m_ProcessingHeader.copy()
960 self.dataOutObj.m_RadarControllerHeader = self.m_RadarControllerHeader.copy()
961 self.dataOutObj.m_SystemHeader = self.m_SystemHeader.copy()
887 962
888 if ext == None:
889 ext = self.ext
890
891 if online:
892 print "Searching files ..."
893 doypath, file, year, doy, set = self.__searchFilesOnLine(path, startDateTime, endDateTime, expLabel, ext)
963 self.dataOutObj.dataType = self.dataType
964 self.dataOutObj.updateObjFromHeader()
894 965
895 if not(doypath):
896 for nTries in range( self.nTries ):
897 print '\tWaiting %0.2f sec for valid file in %s: try %02d ...' % (self.delay, path, nTries+1)
898 time.sleep( self.delay )
899 doypath, file, year, doy, set = self.__searchFilesOnLine(path, startDateTime, endDateTime, expLabel, ext)
900 if doypath:
901 break
902
903 if not(doypath):
904 print "There 'isn't valied files in %s" % path
905 return 0
906
907 self.year = year
908 self.doy = doy
909 self.set = set - 1
910 self.path = path
911
912 else: # offline
913 pathList, filenameList = self.__searchFilesOffLine(path, startDateTime, endDateTime, set, expLabel, ext)
914 if not(pathList):
915 print "No files in range: %s - %s" %(startDateTime.ctime(), endDateTime.ctime())
916 return 0
917
918 self.fileIndex = -1
919 self.pathList = pathList
920 self.filenameList = filenameList
921
922 self.online = online
923 self.ext = ext
924
925 ext = ext.lower()
926
927 if not( self.setNextFile() ):
928 if (startDateTime != None) and (endDateTime != None):
929 print "No files in range: %s - %s" %(startDateTime.ctime(), endDateTime.ctime())
930 elif startDateTime != None:
931 print "No files in : %s" % startDateTime.ctime()
932 else:
933 print "No files"
934 return 0
935
936 if startDateTime != None:
937 self.startUTCSeconds = time.mktime(startDateTime.timetuple())
938 self.startYear = startDateTime.timetuple().tm_year
939 self.startDoy = startDateTime.timetuple().tm_yday
940
941 if endDateTime != None:
942 self.endUTCSeconds = time.mktime(endDateTime.timetuple())
943 self.endYear = endDateTime.timetuple().tm_year
944 self.endDoy = endDateTime.timetuple().tm_yday
945 #call fillHeaderValues() - to Data Object
946
947 self.updateDataHeader()
948
949 return 1
950 966
951 967 class JRODataWriter(JRODataIO):
952 968
953 969 """
954 970 Esta clase permite escribir datos a archivos procesados (.r o ,pdata). La escritura
955 971 de los datos siempre se realiza por bloques.
956 972 """
957 973
958 974 nWriteBlocks = 0
959 975
960 976 setFile = None
961 977
962 978
963 def __init__(self, m_DataObj=None):
979 def __init__(self, dataOutObj=None):
964 980 raise ValueError, "Not implemented"
965 981
966 982
967 983 def hasAllDataInBuffer(self):
968 984 raise ValueError, "Not implemented"
969 985
970 986
971 987 def setBlockDimension(self):
972 988 raise ValueError, "Not implemented"
973 989
974 990
975 991 def writeBlock(self):
976 992 raise ValueError, "No implemented"
977 993
978 994
979 995 def putData(self):
980 996 raise ValueError, "No implemented"
981 997
982 998
983 999 def __writeFirstHeader(self):
984 1000 """
985 1001 Escribe el primer header del file es decir el Basic header y el Long header (SystemHeader, RadarControllerHeader, ProcessingHeader)
986 1002
987 1003 Affected:
988 1004 __dataType
989 1005
990 1006 Return:
991 1007 None
992 1008 """
993 1009 self.__writeBasicHeader()
994 1010 self.__wrSystemHeader()
995 1011 self.__wrRadarControllerHeader()
996 1012 self.__wrProcessingHeader()
997 self.dataType = self.m_DataObj.dataType
1013 self.dataType = self.dataOutObj.dataType
998 1014
999 1015
1000 1016 def __writeBasicHeader(self, fp=None):
1001 1017 """
1002 1018 Escribe solo el Basic header en el file creado
1003 1019
1004 1020 Return:
1005 1021 None
1006 1022 """
1007 1023 if fp == None:
1008 1024 fp = self.fp
1009 1025
1010 self.m_DataObj.m_BasicHeader.write(fp)
1026 self.dataOutObj.m_BasicHeader.write(fp)
1011 1027
1012 1028
1013 1029 def __wrSystemHeader(self, fp=None):
1014 1030 """
1015 1031 Escribe solo el System header en el file creado
1016 1032
1017 1033 Return:
1018 1034 None
1019 1035 """
1020 1036 if fp == None:
1021 1037 fp = self.fp
1022 1038
1023 self.m_DataObj.m_SystemHeader.write(fp)
1039 self.dataOutObj.m_SystemHeader.write(fp)
1024 1040
1025 1041
1026 1042 def __wrRadarControllerHeader(self, fp=None):
1027 1043 """
1028 1044 Escribe solo el RadarController header en el file creado
1029 1045
1030 1046 Return:
1031 1047 None
1032 1048 """
1033 1049 if fp == None:
1034 1050 fp = self.fp
1035 1051
1036 self.m_DataObj.m_RadarControllerHeader.write(fp)
1052 self.dataOutObj.m_RadarControllerHeader.write(fp)
1037 1053
1038 1054
1039 1055 def __wrProcessingHeader(self, fp=None):
1040 1056 """
1041 1057 Escribe solo el Processing header en el file creado
1042 1058
1043 1059 Return:
1044 1060 None
1045 1061 """
1046 1062 if fp == None:
1047 1063 fp = self.fp
1048 1064
1049 self.m_DataObj.m_ProcessingHeader.write(fp)
1065 self.dataOutObj.m_ProcessingHeader.write(fp)
1050 1066
1051 1067
1052 1068 def setNextFile(self):
1053 1069 """
1054 1070 Determina el siguiente file que sera escrito
1055 1071
1056 1072 Affected:
1057 1073 self.filename
1058 1074 self.subfolder
1059 1075 self.fp
1060 1076 self.setFile
1061 1077 self.flagIsNewFile
1062 1078
1063 1079 Return:
1064 1080 0 : Si el archivo no puede ser escrito
1065 1081 1 : Si el archivo esta listo para ser escrito
1066 1082 """
1067 1083 ext = self.ext
1068 1084 path = self.path
1069 1085
1070 1086 if self.fp != None:
1071 1087 self.fp.close()
1072 1088
1073 timeTuple = time.localtime( self.m_DataObj.m_BasicHeader.utc )
1089 timeTuple = time.localtime( self.dataOutObj.m_BasicHeader.utc )
1074 1090 subfolder = 'D%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday)
1075 1091
1076 1092 doypath = os.path.join( path, subfolder )
1077 1093 if not( os.path.exists(doypath) ):
1078 1094 os.mkdir(doypath)
1079 1095 self.setFile = -1 #inicializo mi contador de seteo
1080 1096 else:
1081 1097 filesList = os.listdir( doypath )
1082 1098 if len( filesList ) > 0:
1083 1099 filesList = sorted( filesList, key=str.lower )
1084 1100 filen = filesList[-1]
1085 1101 # el filename debera tener el siguiente formato
1086 1102 # 0 1234 567 89A BCDE (hex)
1087 1103 # x YYYY DDD SSS .ext
1088 1104 if isNumber( filen[8:11] ):
1089 1105 self.setFile = int( filen[8:11] ) #inicializo mi contador de seteo al seteo del ultimo file
1090 1106 else:
1091 1107 self.setFile = -1
1092 1108 else:
1093 1109 self.setFile = -1 #inicializo mi contador de seteo
1094 1110
1095 1111 setFile = self.setFile
1096 1112 setFile += 1
1097 1113
1098 1114 file = '%s%4.4d%3.3d%3.3d%s' % (self.optchar,
1099 1115 timeTuple.tm_year,
1100 1116 timeTuple.tm_yday,
1101 1117 setFile,
1102 1118 ext )
1103 1119
1104 1120 filename = os.path.join( path, subfolder, file )
1105 1121
1106 1122 fp = open( filename,'wb' )
1107 1123
1108 1124 self.nWriteBlocks = 0
1109 1125
1110 1126 #guardando atributos
1111 1127 self.filename = filename
1112 1128 self.subfolder = subfolder
1113 1129 self.fp = fp
1114 1130 self.setFile = setFile
1115 1131 self.flagIsNewFile = 1
1116 1132
1117 1133 print 'Writing the file: %s'%self.filename
1118 1134
1119 1135 self.__writeFirstHeader()
1120 1136
1121 1137 return 1
1122 1138
1123 1139
1124 1140 def __setNewBlock(self):
1125 1141 """
1126 1142 Si es un nuevo file escribe el First Header caso contrario escribe solo el Basic Header
1127 1143
1128 1144 Return:
1129 1145 0 : si no pudo escribir nada
1130 1146 1 : Si escribio el Basic el First Header
1131 1147 """
1132 1148 if self.fp == None:
1133 1149 self.setNextFile()
1134 1150
1135 1151 if self.flagIsNewFile:
1136 1152 return 1
1137 1153
1138 1154 if self.nWriteBlocks < self.m_ProcessingHeader.dataBlocksPerFile:
1139 1155 self.__writeBasicHeader()
1140 1156 return 1
1141 1157
1142 1158 if not( self.setNextFile() ):
1143 1159 return 0
1144 1160
1145 1161 return 1
1146 1162
1147 1163
1148 1164 def writeNextBlock(self):
1149 1165 """
1150 1166 Selecciona el bloque siguiente de datos y los escribe en un file
1151 1167
1152 1168 Return:
1153 1169 0 : Si no hizo pudo escribir el bloque de datos
1154 1170 1 : Si no pudo escribir el bloque de datos
1155 1171 """
1156 1172 if not( self.__setNewBlock() ):
1157 1173 return 0
1158 1174
1159 1175 self.writeBlock()
1160 1176
1161 1177 return 1
1162 1178
1163 1179
1164 1180 def getDataHeader(self):
1165 1181 """
1166 1182 Obtiene una copia del First Header
1167 1183
1168 1184 Affected:
1169 1185 self.m_BasicHeader
1170 1186 self.m_SystemHeader
1171 1187 self.m_RadarControllerHeader
1172 1188 self.m_ProcessingHeader
1173 1189 self.dataType
1174 1190
1175 1191 Return:
1176 1192 None
1177 1193 """
1178 self.m_DataObj.updateHeaderFromObj()
1194 self.dataOutObj.updateHeaderFromObj()
1179 1195
1180 self.m_BasicHeader = self.m_DataObj.m_BasicHeader.copy()
1181 self.m_SystemHeader = self.m_DataObj.m_SystemHeader.copy()
1182 self.m_RadarControllerHeader = self.m_DataObj.m_RadarControllerHeader.copy()
1183 self.m_ProcessingHeader = self.m_DataObj.m_ProcessingHeader.copy()
1196 self.m_BasicHeader = self.dataOutObj.m_BasicHeader.copy()
1197 self.m_SystemHeader = self.dataOutObj.m_SystemHeader.copy()
1198 self.m_RadarControllerHeader = self.dataOutObj.m_RadarControllerHeader.copy()
1199 self.m_ProcessingHeader = self.dataOutObj.m_ProcessingHeader.copy()
1184 1200
1185 self.dataType = self.m_DataObj.dataType
1201 self.dataType = self.dataOutObj.dataType
1186 1202
1187 1203
1188 1204 def setup(self, path, set=0, ext=None):
1189 1205 """
1190 1206 Setea el tipo de formato en la cual sera guardada la data y escribe el First Header
1191 1207
1192 1208 Inputs:
1193 1209 path : el path destino en el cual se escribiran los files a crear
1194 1210 format : formato en el cual sera salvado un file
1195 1211 set : el setebo del file
1196 1212
1197 1213 Return:
1198 1214 0 : Si no realizo un buen seteo
1199 1215 1 : Si realizo un buen seteo
1200 1216 """
1201 1217
1202 1218 if ext == None:
1203 1219 ext = self.ext
1204 1220
1205 1221 ext = ext.lower()
1206 1222
1207 1223 self.path = path
1208 1224 self.setFile = set - 1
1209 1225 self.ext = ext
1210 1226 #self.format = format
1211 1227 self.getDataHeader()
1212 1228
1213 1229 self.setBlockDimension()
1214 1230
1215 1231 if not( self.setNextFile() ):
1216 1232 print "There isn't a next file"
1217 1233 return 0
1218 1234
1219 1235 return 1
@@ -1,551 +1,535
1 1 '''
2 2 File: SpectraIO.py
3 3 Created on 20/02/2012
4 4
5 5 @author $Author$
6 6 @version $Id$
7 7 '''
8 8
9 9 import os, sys
10 10 import numpy
11 11 import glob
12 12 import fnmatch
13 13 import time, datetime
14 14
15 15 path = os.path.split(os.getcwd())[0]
16 16 sys.path.append(path)
17 17
18 18 from Model.JROHeader import *
19 19 from Model.Spectra import Spectra
20 20
21 21 from JRODataIO import JRODataReader
22 22 from JRODataIO import JRODataWriter
23 23 from JRODataIO import isNumber
24 24
25 25
26 26 class SpectraReader(JRODataReader):
27 27 """
28 28 Esta clase permite leer datos de espectros desde archivos procesados (.pdata). La lectura
29 29 de los datos siempre se realiza por bloques. Los datos leidos (array de 3 dimensiones)
30 30 son almacenados en tres buffer's para el Self Spectra, el Cross Spectra y el DC Channel.
31 31
32 32 paresCanalesIguales * alturas * perfiles (Self Spectra)
33 33 paresCanalesDiferentes * alturas * perfiles (Cross Spectra)
34 34 canales * alturas (DC Channels)
35 35
36 36 Esta clase contiene instancias (objetos) de las clases BasicHeader, SystemHeader,
37 37 RadarControllerHeader y Spectra. Los tres primeros se usan para almacenar informacion de la
38 38 cabecera de datos (metadata), y el cuarto (Spectra) para obtener y almacenar un bloque de
39 39 datos desde el "buffer" cada vez que se ejecute el metodo "getData".
40 40
41 41 Example:
42 42 dpath = "/home/myuser/data"
43 43
44 44 startTime = datetime.datetime(2010,1,20,0,0,0,0,0,0)
45 45
46 46 endTime = datetime.datetime(2010,1,21,23,59,59,0,0,0)
47 47
48 48 readerObj = SpectraReader()
49 49
50 50 readerObj.setup(dpath, startTime, endTime)
51 51
52 52 while(True):
53 53
54 54 readerObj.getData()
55 55
56 print readerObj.m_Spectra.data
56 print readerObj.dataOutObj.data
57 57
58 58 if readerObj.flagNoMoreFiles:
59 59 break
60 60
61 61 """
62 m_DataObj = None
62 dataOutObj = None
63 63
64 64 data_spc = None
65 65 data_cspc = None
66 66 data_dc = None
67 67
68 68 pts2read_SelfSpectra = 0
69 69 pts2read_CrossSpectra = 0
70 70 pts2read_DCchannels = 0
71 71
72 72 nChannels = 0
73
74 73 nPairs = 0
75 74
76 #pairList = None
77
78 channelList = None
75 flag_cspc = False
79 76
80 def __init__(self, m_Spectra=None):
77 def __init__(self, dataOutObj=None):
81 78 """
82 79 Inicializador de la clase SpectraReader para la lectura de datos de espectros.
83 80
84 81 Inputs:
85 m_Spectra : Objeto de la clase Spectra. Este objeto sera utilizado para
82 dataOutObj : Objeto de la clase Spectra. Este objeto sera utilizado para
86 83 almacenar un perfil de datos cada vez que se haga un requerimiento
87 84 (getData). El perfil sera obtenido a partir del buffer de datos,
88 85 si el buffer esta vacio se hara un nuevo proceso de lectura de un
89 86 bloque de datos.
90 87 Si este parametro no es pasado se creara uno internamente.
91 88
92 89 Affected:
93 self.m_DataObj
90 self.dataOutObj
94 91
95 92 Return : None
96 93 """
97 if m_Spectra == None:
98 m_Spectra = Spectra()
99
100 if not( isinstance(m_Spectra, Spectra) ):
101 raise ValueError, "in SpectraReader, m_Spectra must be an Spectra class object"
102
103 self.m_DataObj = m_Spectra
104 94
105 95 self.data_spc = None
106 96 self.data_cspc = None
107 97 self.data_dc = None
108 98
109 99 self.pts2read_SelfSpectra = 0
110 100 self.pts2read_CrossSpectra = 0
111 101 self.pts2read_DCs = 0
112 102
113 103 self.nChannels = 0
114 104
115 105 self.nPairs = 0
116 106
117 107 self.ext = ".pdata"
118 108
119 109 self.optchar = "P"
120 110
121 111 ######################
122 112
123 113 self.m_BasicHeader = BasicHeader()
124 114
125 115 self.m_SystemHeader = SystemHeader()
126 116
127 117 self.m_RadarControllerHeader = RadarControllerHeader()
128 118
129 119 self.m_ProcessingHeader = ProcessingHeader()
130 120
131 121 self.online = 0
132 122
133 123 self.fp = None
134 124
135 125 self.fileSizeByHeader = None
136 126
137 127 self.filenameList = []
138 128
139 129 self.filename = None
140 130
141 131 self.fileSize = None
142 132
143 133 self.firstHeaderSize = 0
144 134
145 135 self.basicHeaderSize = 24
146 136
147 137 self.dataType = None
148 138
149 139 self.maxTimeStep = 30
150 140
151 141 self.flagNoMoreFiles = 0
152 142
153 143 self.set = 0
154 144
155 145 self.path = None
156 146
157 147 self.delay = 3 #seconds
158 148
159 149 self.nTries = 3 #quantity tries
160 150
161 151 self.nFiles = 3 #number of files for searching
162 152
163 153 self.nReadBlocks = 0
164 154
165 155 self.flagIsNewFile = 1
166 156
167 157 self.ippSeconds = 0
168 158
169 159 self.flagResetProcessing = 0
170 160
171 161 self.flagIsNewBlock = 0
172 162
173 163 self.nTotalBlocks = 0
174 164
175 165 self.blocksize = 0
176 166
177 #pairList = None
178
179 channelList = None
167 def createObjByDefault(self):
180 168
181 self.flag_cspc = False
182
183
169 dataObj = Spectra()
170
171 return dataObj
172
184 173 def __hasNotDataInBuffer(self):
185 174 return 1
186 175
187 176
188 177 def getBlockDimension(self):
189 178 """
190 179 Obtiene la cantidad de puntos a leer por cada bloque de datos
191 180
192 181 Affected:
193 182 self.nChannels
194 183 self.nPairs
195 184 self.pts2read_SelfSpectra
196 185 self.pts2read_CrossSpectra
197 186 self.pts2read_DCchannels
198 187 self.blocksize
199 self.m_DataObj.nChannels
200 self.m_DataObj.nPairs
188 self.dataOutObj.nChannels
189 self.dataOutObj.nPairs
201 190
202 191 Return:
203 192 None
204 193 """
205 194 self.nChannels = 0
206 195 self.nPairs = 0
207 196 self.pairList = []
208 197
209 198 for i in range( 0, self.m_ProcessingHeader.totalSpectra*2, 2 ):
210 199 if self.m_ProcessingHeader.spectraComb[i] == self.m_ProcessingHeader.spectraComb[i+1]:
211 200 self.nChannels = self.nChannels + 1 #par de canales iguales
212 201 else:
213 202 self.nPairs = self.nPairs + 1 #par de canales diferentes
214 203 self.pairList.append( (self.m_ProcessingHeader.spectraComb[i], self.m_ProcessingHeader.spectraComb[i+1]) )
215
216 if self.nPairs > 0:
217 self.flag_cspc = True
218 204
219 205 pts2read = self.m_ProcessingHeader.numHeights * self.m_ProcessingHeader.profilesPerBlock
220 206
221 207 self.pts2read_SelfSpectra = int(self.nChannels * pts2read)
222 208 self.blocksize = self.pts2read_SelfSpectra
223 209
224 if self.flag_cspc:
210 if self.m_ProcessingHeader.flag_cspc:
225 211 self.pts2read_CrossSpectra = int(self.nPairs * pts2read)
226 212 self.blocksize += self.pts2read_CrossSpectra
227 213
228 214 if self.m_ProcessingHeader.flag_dc:
229 215 self.pts2read_DCchannels = int(self.m_SystemHeader.numChannels * self.m_ProcessingHeader.numHeights)
230 216 self.blocksize += self.pts2read_DCchannels
231 217
232 218 # self.blocksize = self.pts2read_SelfSpectra + self.pts2read_CrossSpectra + self.pts2read_DCchannels
233
234 self.channelList = numpy.arange(self.nChannels)
235 219
236 220
237 221 def readBlock(self):
238 222 """
239 223 Lee el bloque de datos desde la posicion actual del puntero del archivo
240 224 (self.fp) y actualiza todos los parametros relacionados al bloque de datos
241 225 (metadata + data). La data leida es almacenada en el buffer y el contador del buffer
242 226 es seteado a 0
243 227
244 228 Return: None
245 229
246 230 Variables afectadas:
247 231 self.datablockIndex
248 232 self.flagIsNewFile
249 233 self.flagIsNewBlock
250 234 self.nTotalBlocks
251 235 self.data_spc
252 236 self.data_cspc
253 237 self.data_dc
254 238
255 239 Exceptions:
256 240 Si un bloque leido no es un bloque valido
257 241 """
258 242 blockOk_flag = False
259 243 fpointer = self.fp.tell()
260 244
261 245 spc = numpy.fromfile( self.fp, self.dataType[0], self.pts2read_SelfSpectra )
262 246 spc = spc.reshape( (self.nChannels, self.m_ProcessingHeader.numHeights, self.m_ProcessingHeader.profilesPerBlock) ) #transforma a un arreglo 3D
263 247
264 248 if self.flag_cspc:
265 249 cspc = numpy.fromfile( self.fp, self.dataType, self.pts2read_CrossSpectra )
266 250 cspc = cspc.reshape( (self.nPairs, self.m_ProcessingHeader.numHeights, self.m_ProcessingHeader.profilesPerBlock) ) #transforma a un arreglo 3D
267 251
268 252 if self.m_ProcessingHeader.flag_dc:
269 253 dc = numpy.fromfile( self.fp, self.dataType, self.pts2read_DCchannels ) #int(self.m_ProcessingHeader.numHeights*self.m_SystemHeader.numChannels) )
270 254 dc = dc.reshape( (self.m_SystemHeader.numChannels, self.m_ProcessingHeader.numHeights) ) #transforma a un arreglo 2D
271 255
272 256
273 257 if not(self.m_ProcessingHeader.shif_fft):
274 258 spc = numpy.roll( spc, self.m_ProcessingHeader.profilesPerBlock/2, axis=2 ) #desplaza a la derecha en el eje 2 determinadas posiciones
275 259
276 260 if self.flag_cspc:
277 261 cspc = numpy.roll( cspc, self.m_ProcessingHeader.profilesPerBlock/2, axis=2 ) #desplaza a la derecha en el eje 2 determinadas posiciones
278 262
279 263
280 264 spc = numpy.transpose( spc, (0,2,1) )
281 265 self.data_spc = spc
282 266
283 267 if self.flag_cspc:
284 268 cspc = numpy.transpose( cspc, (0,2,1) )
285 269 self.data_cspc = cspc['real'] + cspc['imag']*1j
286 270 else:
287 271 self.data_cspc = None
288 272
289 273 if self.m_ProcessingHeader.flag_dc:
290 274 self.data_dc = dc['real'] + dc['imag']*1j
291 275 else:
292 276 self.data_dc = None
293 277
294 278 self.datablockIndex = 0
295 279 self.flagIsNewFile = 0
296 280 self.flagIsNewBlock = 1
297 281
298 282 self.nTotalBlocks += 1
299 283 self.nReadBlocks += 1
300 284
301 285 return 1
302 286
303 287
304 288 def getData(self):
305 289 """
306 290 Copia el buffer de lectura a la clase "Spectra",
307 291 con todos los parametros asociados a este (metadata). cuando no hay datos en el buffer de
308 292 lectura es necesario hacer una nueva lectura de los bloques de datos usando "readNextBlock"
309 293
310 294 Return:
311 295 0 : Si no hay mas archivos disponibles
312 296 1 : Si hizo una buena copia del buffer
313 297
314 298 Affected:
315 self.m_DataObj
299 self.dataOutObj
316 300 self.datablockIndex
317 301 self.flagResetProcessing
318 302 self.flagIsNewBlock
319 303 """
320 304
321 305 if self.flagNoMoreFiles: return 0
322 306
323 307 self.flagResetProcessing = 0
324 308 self.flagIsNewBlock = 0
325 309
326 310 if self.__hasNotDataInBuffer():
327 311
328 312 if not( self.readNextBlock() ):
329 313 return 0
330 314
331 315 self.updateDataHeader()
332 316
333 317 if self.flagNoMoreFiles == 1:
334 318 print 'Process finished'
335 319 return 0
336 320
337 321 #data es un numpy array de 3 dmensiones (perfiles, alturas y canales)
338 322
339 323 if self.data_dc == None:
340 self.m_DataObj.flagNoData = True
324 self.dataOutObj.flagNoData = True
341 325 return 0
342 326
343 self.m_DataObj.flagNoData = False
344 self.m_DataObj.flagResetProcessing = self.flagResetProcessing
327 self.dataOutObj.flagNoData = False
328 self.dataOutObj.flagResetProcessing = self.flagResetProcessing
345 329
346 self.m_DataObj.data_spc = self.data_spc
347 self.m_DataObj.data_cspc = self.data_cspc
348 self.m_DataObj.data_dc = self.data_dc
330 self.dataOutObj.data_spc = self.data_spc
331 self.dataOutObj.data_cspc = self.data_cspc
332 self.dataOutObj.data_dc = self.data_dc
349 333
350 334 return 1
351 335
352 336
353 337 class SpectraWriter(JRODataWriter):
354 338
355 339 """
356 340 Esta clase permite escribir datos de espectros a archivos procesados (.pdata). La escritura
357 341 de los datos siempre se realiza por bloques.
358 342 """
359 343
360 m_DataObj = None
344 dataOutObj = None
361 345
362 346 shape_spc_Buffer = None
363 347 shape_cspc_Buffer = None
364 348 shape_dc_Buffer = None
365 349
366 350 data_spc = None
367 351 data_cspc = None
368 352 data_dc = None
369 353
370 354
371 def __init__(self, m_Spectra=None):
355 def __init__(self, dataOutObj=None):
372 356 """
373 357 Inicializador de la clase SpectraWriter para la escritura de datos de espectros.
374 358
375 359 Affected:
376 self.m_DataObj
360 self.dataOutObj
377 361 self.m_BasicHeader
378 362 self.m_SystemHeader
379 363 self.m_RadarControllerHeader
380 364 self.m_ProcessingHeader
381 365
382 366 Return: None
383 367 """
384 if m_Spectra == None:
385 m_Spectra = Spectra()
368 if dataOutObj == None:
369 dataOutObj = Spectra()
386 370
387 if not( isinstance(m_Spectra, Spectra) ):
388 raise ValueError, "in SpectraReader, m_Spectra must be an Spectra class object"
371 if not( isinstance(dataOutObj, Spectra) ):
372 raise ValueError, "in SpectraReader, dataOutObj must be an Spectra class object"
389 373
390 self.m_DataObj = m_Spectra
374 self.dataOutObj = dataOutObj
391 375
392 376 self.ext = ".pdata"
393 377
394 378 self.optchar = "P"
395 379
396 380 self.shape_spc_Buffer = None
397 381 self.shape_cspc_Buffer = None
398 382 self.shape_dc_Buffer = None
399 383
400 384 self.data_spc = None
401 385 self.data_cspc = None
402 386 self.data_dc = None
403 387
404 388 ####################################
405 389
406 390 self.fp = None
407 391
408 392 self.nWriteBlocks = 0
409 393
410 394 self.flagIsNewFile = 1
411 395
412 396 self.nTotalBlocks = 0
413 397
414 398 self.flagIsNewBlock = 0
415 399
416 400 self.flagNoMoreFiles = 0
417 401
418 402 self.setFile = None
419 403
420 404 self.dataType = None
421 405
422 406 self.path = None
423 407
424 408 self.noMoreFiles = 0
425 409
426 410 self.filename = None
427 411
428 412 self.m_BasicHeader= BasicHeader()
429 413
430 414 self.m_SystemHeader = SystemHeader()
431 415
432 416 self.m_RadarControllerHeader = RadarControllerHeader()
433 417
434 418 self.m_ProcessingHeader = ProcessingHeader()
435 419
436 420
437 421 def hasAllDataInBuffer(self):
438 422 return 1
439 423
440 424
441 425 def setBlockDimension(self):
442 426 """
443 427 Obtiene las formas dimensionales del los subbloques de datos que componen un bloque
444 428
445 429 Affected:
446 430 self.shape_spc_Buffer
447 431 self.shape_cspc_Buffer
448 432 self.shape_dc_Buffer
449 433
450 434 Return: None
451 435 """
452 self.shape_spc_Buffer = (self.m_DataObj.nChannels,
436 self.shape_spc_Buffer = (self.dataOutObj.nChannels,
453 437 self.m_ProcessingHeader.numHeights,
454 438 self.m_ProcessingHeader.profilesPerBlock)
455 439
456 self.shape_cspc_Buffer = (self.m_DataObj.nPairs,
440 self.shape_cspc_Buffer = (self.dataOutObj.nPairs,
457 441 self.m_ProcessingHeader.numHeights,
458 442 self.m_ProcessingHeader.profilesPerBlock)
459 443
460 444 self.shape_dc_Buffer = (self.m_SystemHeader.numChannels,
461 445 self.m_ProcessingHeader.numHeights)
462 446
463 447
464 448 def writeBlock(self):
465 449 """
466 450 Escribe el buffer en el file designado
467 451
468 452 Affected:
469 453 self.data_spc
470 454 self.data_cspc
471 455 self.data_dc
472 456 self.flagIsNewFile
473 457 self.flagIsNewBlock
474 458 self.nTotalBlocks
475 459 self.nWriteBlocks
476 460
477 461 Return: None
478 462 """
479 463
480 464 spc = numpy.transpose( self.data_spc, (0,2,1) )
481 465 if not( self.m_ProcessingHeader.shif_fft ):
482 466 spc = numpy.roll( spc, self.m_ProcessingHeader.profilesPerBlock/2, axis=2 ) #desplaza a la derecha en el eje 2 determinadas posiciones
483 467 data = spc.reshape((-1))
484 468 data.tofile(self.fp)
485 469
486 470 if self.data_cspc != None:
487 471 data = numpy.zeros( self.shape_cspc_Buffer, self.dataType )
488 472 cspc = numpy.transpose( self.data_cspc, (0,2,1) )
489 473 if not( self.m_ProcessingHeader.shif_fft ):
490 474 cspc = numpy.roll( cspc, self.m_ProcessingHeader.profilesPerBlock/2, axis=2 ) #desplaza a la derecha en el eje 2 determinadas posiciones
491 475 data['real'] = cspc.real
492 476 data['imag'] = cspc.imag
493 477 data = data.reshape((-1))
494 478 data.tofile(self.fp)
495 479
496 480 data = numpy.zeros( self.shape_dc_Buffer, self.dataType )
497 481 dc = self.data_dc
498 482 data['real'] = dc.real
499 483 data['imag'] = dc.imag
500 484 data = data.reshape((-1))
501 485 data.tofile(self.fp)
502 486
503 487 self.data_spc.fill(0)
504 488 self.data_dc.fill(0)
505 489 if self.data_cspc != None:
506 490 self.data_cspc.fill(0)
507 491
508 492 self.flagIsNewFile = 0
509 493 self.flagIsNewBlock = 1
510 494 self.nTotalBlocks += 1
511 495 self.nWriteBlocks += 1
512 496
513 497
514 498 def putData(self):
515 499 """
516 500 Setea un bloque de datos y luego los escribe en un file
517 501
518 502 Affected:
519 503 self.data_spc
520 504 self.data_cspc
521 505 self.data_dc
522 506
523 507 Return:
524 508 0 : Si no hay data o no hay mas files que puedan escribirse
525 509 1 : Si se escribio la data de un bloque en un file
526 510 """
527 511 self.flagIsNewBlock = 0
528 512
529 if self.m_DataObj.flagNoData:
513 if self.dataOutObj.flagNoData:
530 514 return 0
531 515
532 if self.m_DataObj.flagResetProcessing:
516 if self.dataOutObj.flagResetProcessing:
533 517 self.data_spc.fill(0)
534 518 self.data_cspc.fill(0)
535 519 self.data_dc.fill(0)
536 520 self.setNextFile()
537 521
538 self.data_spc = self.m_DataObj.data_spc
539 self.data_cspc = self.m_DataObj.data_cspc
540 self.data_dc = self.m_DataObj.data_dc
522 self.data_spc = self.dataOutObj.data_spc
523 self.data_cspc = self.dataOutObj.data_cspc
524 self.data_dc = self.dataOutObj.data_dc
541 525
542 526 # #self.m_ProcessingHeader.dataBlocksPerFile)
543 527 if self.hasAllDataInBuffer():
544 528 self.getDataHeader()
545 529 self.writeNextBlock()
546 530
547 531 if self.flagNoMoreFiles:
548 532 #print 'Process finished'
549 533 return 0
550 534
551 535 return 1 No newline at end of file
@@ -1,434 +1,433
1 1 '''
2 2 Created on 23/01/2012
3 3
4 4 @author $Author$
5 5 @version $Id$
6 6 '''
7 7
8 8 import os, sys
9 9 import numpy
10 10 import glob
11 11 import fnmatch
12 12 import time, datetime
13 13
14 14 path = os.path.split(os.getcwd())[0]
15 15 sys.path.append(path)
16 16
17 17 from Model.JROHeader import *
18 18 from Model.Voltage import Voltage
19 19
20 20 from IO.JRODataIO import JRODataReader
21 21 from IO.JRODataIO import JRODataWriter
22 22
23 23
24 24 class VoltageReader(JRODataReader):
25 25 """
26 26 Esta clase permite leer datos de voltage desde archivos en formato rawdata (.r). La lectura
27 27 de los datos siempre se realiza por bloques. Los datos leidos (array de 3 dimensiones:
28 28 perfiles*alturas*canales) son almacenados en la variable "buffer".
29 29
30 30 perfiles * alturas * canales
31 31
32 32 Esta clase contiene instancias (objetos) de las clases BasicHeader, SystemHeader,
33 33 RadarControllerHeader y Voltage. Los tres primeros se usan para almacenar informacion de la
34 34 cabecera de datos (metadata), y el cuarto (Voltage) para obtener y almacenar un perfil de
35 35 datos desde el "buffer" cada vez que se ejecute el metodo "getData".
36 36
37 37 Example:
38 38
39 39 dpath = "/home/myuser/data"
40 40
41 41 startTime = datetime.datetime(2010,1,20,0,0,0,0,0,0)
42 42
43 43 endTime = datetime.datetime(2010,1,21,23,59,59,0,0,0)
44 44
45 45 readerObj = VoltageReader()
46 46
47 47 readerObj.setup(dpath, startTime, endTime)
48 48
49 49 while(True):
50 50
51 51 #to get one profile
52 52 profile = readerObj.getData()
53 53
54 54 #print the profile
55 55 print profile
56 56
57 57 #If you want to see all datablock
58 58 print readerObj.datablock
59 59
60 60 if readerObj.flagNoMoreFiles:
61 61 break
62 62
63 63 """
64 m_DataObj = None
64 dataOutObj = None
65 65
66 66 datablock = None
67 67
68 68 ext = ".r"
69 69
70 70 optchar = "D"
71 71
72 72
73 def __init__(self, m_Voltage=None):
73 def __init__(self, dataOutObj=None):
74 74 """
75 75 Inicializador de la clase VoltageReader para la lectura de datos de voltage.
76 76
77 77 Input:
78 m_Voltage : Objeto de la clase Voltage. Este objeto sera utilizado para
78 dataOutObj : Objeto de la clase Voltage. Este objeto sera utilizado para
79 79 almacenar un perfil de datos cada vez que se haga un requerimiento
80 80 (getData). El perfil sera obtenido a partir del buffer de datos,
81 81 si el buffer esta vacio se hara un nuevo proceso de lectura de un
82 82 bloque de datos.
83 83 Si este parametro no es pasado se creara uno internamente.
84 84
85 85 Variables afectadas:
86 self.m_DataObj
86 self.dataOutObj
87 87
88 88 Return:
89 89 None
90 90 """
91 if m_Voltage == None:
92 m_Voltage = Voltage()
93
94 if not(isinstance(m_Voltage, Voltage)):
95 raise ValueError, "in VoltageReader, m_Voltage must be an Voltage class object"
96
97 self.m_DataObj = m_Voltage
98 91
99 92 self.datablock = None
100 93
101 94 self.utc = 0
102 95
103 96 self.ext = ".r"
104 97
105 98 self.optchar = "D"
106 99
107 100 self.m_BasicHeader = BasicHeader()
108 101
109 102 self.m_SystemHeader = SystemHeader()
110 103
111 104 self.m_RadarControllerHeader = RadarControllerHeader()
112 105
113 106 self.m_ProcessingHeader = ProcessingHeader()
114 107
115 108 self.online = 0
116 109
117 110 self.fp = None
118 111
119 112 self.idFile = None
120 113
121 114 self.startDateTime = None
122 115
123 116 self.endDateTime = None
124 117
125 118 self.dataType = None
126 119
127 120 self.fileSizeByHeader = None
128 121
129 122 self.filenameList = []
130 123
131 124 self.filename = None
132 125
133 126 self.fileSize = None
134 127
135 128 self.firstHeaderSize = 0
136 129
137 130 self.basicHeaderSize = 24
138 131
139 132 self.pathList = []
140 133
141 134 self.filenameList = []
142 135
143 136 self.lastUTTime = 0
144 137
145 138 self.maxTimeStep = 30
146 139
147 140 self.flagNoMoreFiles = 0
148 141
149 142 self.set = 0
150 143
151 144 self.path = None
152 145
153 146 self.profileIndex = 9999
154 147
155 148 self.delay = 3 #seconds
156 149
157 150 self.nTries = 3 #quantity tries
158 151
159 152 self.nFiles = 3 #number of files for searching
160 153
161 154 self.nReadBlocks = 0
162 155
163 156 self.flagIsNewFile = 1
164 157
165 158 self.ippSeconds = 0
166 159
167 160 self.flagResetProcessing = 0
168 161
169 162 self.flagIsNewBlock = 0
170 163
171 164 self.nTotalBlocks = 0
172 165
173 166 self.blocksize = 0
174 167
168 def createObjByDefault(self):
169
170 dataObj = Voltage()
171
172 return dataObj
173
175 174 def __hasNotDataInBuffer(self):
176 175 if self.profileIndex >= self.m_ProcessingHeader.profilesPerBlock:
177 176 return 1
178 177 return 0
179 178
180 179
181 180 def getBlockDimension(self):
182 181 """
183 182 Obtiene la cantidad de puntos a leer por cada bloque de datos
184 183
185 184 Affected:
186 185 self.blocksize
187 186
188 187 Return:
189 188 None
190 189 """
191 190 pts2read = self.m_ProcessingHeader.profilesPerBlock * self.m_ProcessingHeader.numHeights * self.m_SystemHeader.numChannels
192 191 self.blocksize = pts2read
193 192
194 193
195 194 def readBlock(self):
196 195 """
197 196 readBlock lee el bloque de datos desde la posicion actual del puntero del archivo
198 197 (self.fp) y actualiza todos los parametros relacionados al bloque de datos
199 198 (metadata + data). La data leida es almacenada en el buffer y el contador del buffer
200 199 es seteado a 0
201 200
202 201 Inputs:
203 202 None
204 203
205 204 Return:
206 205 None
207 206
208 207 Affected:
209 208 self.profileIndex
210 209 self.datablock
211 210 self.flagIsNewFile
212 211 self.flagIsNewBlock
213 212 self.nTotalBlocks
214 213
215 214 Exceptions:
216 215 Si un bloque leido no es un bloque valido
217 216 """
218 217
219 218 junk = numpy.fromfile( self.fp, self.dataType, self.blocksize )
220 219
221 220 try:
222 221 junk = junk.reshape( (self.m_ProcessingHeader.profilesPerBlock, self.m_ProcessingHeader.numHeights, self.m_SystemHeader.numChannels) )
223 222 except:
224 223 print "The read block (%3d) has not enough data" %self.nReadBlocks
225 224 return 0
226 225
227 226 junk = numpy.transpose(junk, (2,0,1))
228 227 self.datablock = junk['real'] + junk['imag']*1j
229 228
230 229 self.profileIndex = 0
231 230
232 231 self.flagIsNewFile = 0
233 232 self.flagIsNewBlock = 1
234 233
235 234 self.nTotalBlocks += 1
236 235 self.nReadBlocks += 1
237 236
238 237 return 1
239 238
240 239
241 240 def getData(self):
242 241 """
243 242 getData obtiene una unidad de datos del buffer de lectura y la copia a la clase "Voltage"
244 243 con todos los parametros asociados a este (metadata). cuando no hay datos en el buffer de
245 244 lectura es necesario hacer una nueva lectura de los bloques de datos usando "readNextBlock"
246 245
247 246 Ademas incrementa el contador del buffer en 1.
248 247
249 248 Return:
250 249 data : retorna un perfil de voltages (alturas * canales) copiados desde el
251 250 buffer. Si no hay mas archivos a leer retorna None.
252 251
253 252 Variables afectadas:
254 self.m_DataObj
253 self.dataOutObj
255 254 self.profileIndex
256 255
257 256 Affected:
258 self.m_DataObj
257 self.dataOutObj
259 258 self.profileIndex
260 259 self.flagResetProcessing
261 260 self.flagIsNewBlock
262 261 """
263 262 if self.flagNoMoreFiles: return 0
264 263
265 264 self.flagResetProcessing = 0
266 265 self.flagIsNewBlock = 0
267 266
268 267 if self.__hasNotDataInBuffer():
269 268
270 269 if not( self.readNextBlock() ):
271 270 return 0
272 271
273 272 self.updateDataHeader()
274 273
275 274 if self.flagNoMoreFiles == 1:
276 275 print 'Process finished'
277 276 return 0
278 277
279 278 #data es un numpy array de 3 dmensiones (perfiles, alturas y canales)
280 279
281 280 if self.datablock == None:
282 self.m_DataObj.flagNoData = True
281 self.dataOutObj.flagNoData = True
283 282 return 0
284 283
285 284 time = self.m_BasicHeader.utc + self.profileIndex * self.ippSeconds
286 self.m_DataObj.m_BasicHeader.utc = time
285 self.dataOutObj.m_BasicHeader.utc = time
287 286
288 self.m_DataObj.flagNoData = False
289 self.m_DataObj.flagResetProcessing = self.flagResetProcessing
287 self.dataOutObj.flagNoData = False
288 self.dataOutObj.flagResetProcessing = self.flagResetProcessing
290 289
291 self.m_DataObj.data = self.datablock[:,self.profileIndex,:]
290 self.dataOutObj.data = self.datablock[:,self.profileIndex,:]
292 291
293 292 self.profileIndex += 1
294 293
295 294 #call setData - to Data Object
296 295
297 return 1 #self.m_DataObj.data
296 return 1 #self.dataOutObj.data
298 297
299 298
300 299 class VoltageWriter(JRODataWriter):
301 300 """
302 301 Esta clase permite escribir datos de voltajes a archivos procesados (.r). La escritura
303 302 de los datos siempre se realiza por bloques.
304 303 """
305 304 __configHeaderFile = 'wrSetHeadet.txt'
306 305
307 m_DataObj = None
306 dataOutObj = None
308 307
309 308 ext = ".r"
310 309
311 310 optchar = "D"
312 311
313 312 datablock = None
314 313
315 314 profileIndex = 0
316 315
317 316 shapeBuffer = None
318 317
319 318
320 def __init__(self, m_Voltage=None):
319 def __init__(self, dataOutObj=None):
321 320 """
322 321 Inicializador de la clase VoltageWriter para la escritura de datos de espectros.
323 322
324 323 Affected:
325 self.m_DataObj
324 self.dataOutObj
326 325
327 326 Return: None
328 327 """
329 if m_Voltage == None:
330 m_Voltage = Voltage()
328 if dataOutObj == None:
329 dataOutObj = Voltage()
331 330
332 if not( isinstance(m_Voltage, Voltage) ):
333 raise ValueError, "in VoltageReader, m_Voltage must be an Spectra class object"
331 if not( isinstance(dataOutObj, Voltage) ):
332 raise ValueError, "in VoltageReader, dataOutObj must be an Spectra class object"
334 333
335 self.m_DataObj = m_Voltage
334 self.dataOutObj = dataOutObj
336 335
337 336
338 337 def hasAllDataInBuffer(self):
339 338 if self.profileIndex >= self.m_ProcessingHeader.profilesPerBlock:
340 339 return 1
341 340 return 0
342 341
343 342
344 343 def setBlockDimension(self):
345 344 """
346 345 Obtiene las formas dimensionales del los subbloques de datos que componen un bloque
347 346
348 347 Affected:
349 348 self.shape_spc_Buffer
350 349 self.shape_cspc_Buffer
351 350 self.shape_dc_Buffer
352 351
353 352 Return: None
354 353 """
355 354 self.shapeBuffer = (self.m_ProcessingHeader.profilesPerBlock,
356 355 self.m_ProcessingHeader.numHeights,
357 356 self.m_SystemHeader.numChannels )
358 357
359 358 self.datablock = numpy.zeros((self.m_SystemHeader.numChannels,
360 359 self.m_ProcessingHeader.profilesPerBlock,
361 360 self.m_ProcessingHeader.numHeights),
362 361 dtype=numpy.dtype('complex'))
363 362
364 363
365 364 def writeBlock(self):
366 365 """
367 366 Escribe el buffer en el file designado
368 367
369 368 Affected:
370 369 self.profileIndex
371 370 self.flagIsNewFile
372 371 self.flagIsNewBlock
373 372 self.nTotalBlocks
374 373 self.nWriteBlocks
375 374
376 375 Return: None
377 376 """
378 377 data = numpy.zeros( self.shapeBuffer, self.dataType )
379 378
380 379 junk = numpy.transpose(self.datablock, (1,2,0))
381 380
382 381 data['real'] = junk.real
383 382 data['imag'] = junk.imag
384 383
385 384 data = data.reshape( (-1) )
386 385
387 386 data.tofile( self.fp )
388 387
389 388 self.datablock.fill(0)
390 389 self.profileIndex = 0
391 390 self.flagIsNewFile = 0
392 391 self.flagIsNewBlock = 1
393 392 self.nTotalBlocks += 1
394 393 self.nWriteBlocks += 1
395 394
396 395
397 396 def putData(self):
398 397 """
399 398 Setea un bloque de datos y luego los escribe en un file
400 399
401 400 Affected:
402 401 self.flagIsNewBlock
403 402 self.profileIndex
404 403
405 404 Return:
406 405 0 : Si no hay data o no hay mas files que puedan escribirse
407 406 1 : Si se escribio la data de un bloque en un file
408 407 """
409 408 self.flagIsNewBlock = 0
410 409
411 if self.m_DataObj.flagNoData:
410 if self.dataOutObj.flagNoData:
412 411 return 0
413 412
414 if self.m_DataObj.flagResetProcessing:
413 if self.dataOutObj.flagResetProcessing:
415 414
416 415 self.datablock.fill(0)
417 416 self.profileIndex = 0
418 417 self.setNextFile()
419 418
420 self.datablock[:,self.profileIndex,:] = self.m_DataObj.data
419 self.datablock[:,self.profileIndex,:] = self.dataOutObj.data
421 420
422 421 self.profileIndex += 1
423 422
424 423 if self.hasAllDataInBuffer():
425 424 #if self.flagIsNewFile:
426 425 self.getDataHeader()
427 426 self.writeNextBlock()
428 427
429 428 if self.flagNoMoreFiles:
430 429 #print 'Process finished'
431 430 return 0
432 431
433 432 return 1
434 433 No newline at end of file
@@ -1,109 +1,105
1 1 '''
2 2 Created on Feb 7, 2012
3 3
4 4 @author $Author$
5 5 @version $Id$
6 6 '''
7 7 import copy
8 8 import numpy
9 9
10 10 from JROHeader import RadarControllerHeader, ProcessingHeader, SystemHeader, BasicHeader
11 11
12 12 class Data:
13 13 '''
14 14 classdocs
15 15 '''
16 16
17 17 def __init__(self):
18 18 '''
19 19 Constructor
20 20 '''
21 21 raise ValueError, "This class has not been implemented"
22 22
23 23 def copy(self, objIn=None):
24 24
25 25 if objIn == None:
26 26 return copy.deepcopy(self)
27 27
28 28 for key in objIn.__dict__.keys():
29 29 self.__dict__[key] = objIn.__dict__[key]
30 30
31 31 def deepcopy(self):
32 32
33 33 return copy.deepcopy(self)
34 34
35 class Noise(Data):
36 '''
37 classdocs
38 '''
39 35
40 def __init__(self):
41 '''
42 Constructor
43 '''
44 pass
45 36
46 37 class JROData(Data):
47 38 '''
48 39 classdocs
49 40 '''
50 41
51 42 m_RadarControllerHeader = RadarControllerHeader()
52 43
53 44 m_ProcessingHeader = ProcessingHeader()
54 45
55 46 m_SystemHeader = SystemHeader()
56 47
57 48 m_BasicHeader = BasicHeader()
58 49
59 m_NoiseObj = Noise()
50 noise = None
60 51
61 52 type = None
62 53
63 54 dataType = None
64 55
65 56 nHeights = None
66 57
67 58 nProfiles = None
68 59
69 60 nChannels = None
70 61
71 62 heightList = None
72 63
73 64 channelList = None
74 65
66 channelIndexList = None
67
68 pairList = None
69
75 70 flagNoData = False
76 71
77 72 flagResetProcessing = False
78 73
79 74 def __init__(self):
80 75 '''
81 76 Constructor
82 77 '''
83 78 raise ValueError, "This class has not been implemented"
84 79
85 80 def updateHeaderFromObj(self):
86 81
87 82 xi = self.heightList[0]
88 83 step = self.heightList[1] - self.heightList[0]
89 84
90 85 self.m_ProcessingHeader.firstHeight = xi
91 86 self.m_ProcessingHeader.deltaHeight = step
92 87
93 88 self.m_ProcessingHeader.numHeights = self.nHeights
94 89 self.m_SystemHeader.numChannels = self.nChannels
95 90 self.m_SystemHeader.numProfiles = self.nProfiles
96 91
97 92 def updateObjFromHeader(self):
98 93
99 94 xi = self.m_ProcessingHeader.firstHeight
100 95 step = self.m_ProcessingHeader.deltaHeight
101 96 xf = xi + self.m_ProcessingHeader.numHeights*step
102 97
103 98 self.heightList = numpy.arange(xi, xf, step)
99 self.channelIndexList = numpy.arange(self.m_SystemHeader.numChannels)
104 100 self.channelList = numpy.arange(self.m_SystemHeader.numChannels)
105 101
106 102 self.nHeights = len(self.heightList)
107 103 self.nProfiles = self.m_SystemHeader.numProfiles
108 104 self.nChannels = len(self.channelList)
109 105 No newline at end of file
@@ -1,486 +1,505
1 1 '''
2 2 Created on 23/01/2012
3 3
4 4 @author $Author: vsarmiento $
5 5 @version $Id: HeaderIO.py 37 2012-03-26 22:55:13Z vsarmiento $
6 6 '''
7 7
8 8 import numpy
9 9 import copy
10 10
11 11 class Header:
12 12
13 13 def __init__(self):
14 14 raise
15 15
16 16 def copy(self):
17 17 return copy.deepcopy(self)
18 18
19 19 def read():
20 20 pass
21 21
22 22 def write():
23 23 pass
24 24
25 25 class BasicHeader(Header):
26 26
27 27 size = None
28 28 version = None
29 29 dataBlock = None
30 30 utc = None
31 31 miliSecond = None
32 32 timeZone = None
33 33 dstFlag = None
34 34 errorCount = None
35 35 struct = None
36 36
37 37 def __init__(self):
38 38 self.size = 0
39 39 self.version = 0
40 40 self.dataBlock = 0
41 41 self.utc = 0
42 42 self.miliSecond = 0
43 43 self.timeZone = 0
44 44 self.dstFlag = 0
45 45 self.errorCount = 0
46 46 self.struct = numpy.dtype([
47 47 ('nSize','<u4'),
48 48 ('nVersion','<u2'),
49 49 ('nDataBlockId','<u4'),
50 50 ('nUtime','<u4'),
51 51 ('nMilsec','<u2'),
52 52 ('nTimezone','<i2'),
53 53 ('nDstflag','<i2'),
54 54 ('nErrorCount','<u4')
55 55 ])
56 56
57 57
58 58 def read(self, fp):
59 59 try:
60 60 header = numpy.fromfile(fp, self.struct,1)
61 61 self.size = header['nSize'][0]
62 62 self.version = header['nVersion'][0]
63 63 self.dataBlock = header['nDataBlockId'][0]
64 64 self.utc = header['nUtime'][0]
65 65 self.miliSecond = header['nMilsec'][0]
66 66 self.timeZone = header['nTimezone'][0]
67 67 self.dstFlag = header['nDstflag'][0]
68 68 self.errorCount = header['nErrorCount'][0]
69 69 except:
70 70 return 0
71 71
72 72 return 1
73 73
74 74 def write(self, fp):
75 75 headerTuple = (self.size,self.version,self.dataBlock,self.utc,self.miliSecond,self.timeZone,self.dstFlag,self.errorCount)
76 76 header = numpy.array(headerTuple,self.struct)
77 77 header.tofile(fp)
78 78
79 79 return 1
80 80
81 81 class SystemHeader(Header):
82 82
83 83 size = None
84 84 numSamples = None
85 85 numProfiles = None
86 86 numChannels = None
87 87 adcResolution = None
88 88 pciDioBusWidth = None
89 89 struct = None
90 90
91 91 def __init__(self):
92 92 self.size = 0
93 93 self.numSamples = 0
94 94 self.numProfiles = 0
95 95 self.numChannels = 0
96 96 self.adcResolution = 0
97 97 self.pciDioBusWidth = 0
98 98 self.struct = numpy.dtype([
99 99 ('nSize','<u4'),
100 100 ('nNumSamples','<u4'),
101 101 ('nNumProfiles','<u4'),
102 102 ('nNumChannels','<u4'),
103 103 ('nADCResolution','<u4'),
104 104 ('nPCDIOBusWidth','<u4'),
105 105 ])
106 106
107 107
108 108 def read(self, fp):
109 109 try:
110 110 header = numpy.fromfile(fp,self.struct,1)
111 111 self.size = header['nSize'][0]
112 112 self.numSamples = header['nNumSamples'][0]
113 113 self.numProfiles = header['nNumProfiles'][0]
114 114 self.numChannels = header['nNumChannels'][0]
115 115 self.adcResolution = header['nADCResolution'][0]
116 116 self.pciDioBusWidth = header['nPCDIOBusWidth'][0]
117 117 except:
118 118 return 0
119 119
120 120 return 1
121 121
122 122 def write(self, fp):
123 123 headerTuple = (self.size,self.numSamples,self.numProfiles,self.numChannels,self.adcResolution,self.pciDioBusWidth)
124 124 header = numpy.array(headerTuple,self.struct)
125 125 header.tofile(fp)
126 126
127 127 return 1
128 128
129 129 class RadarControllerHeader(Header):
130 130
131 131 size = None
132 132 expType = None
133 133 nTx = None
134 134 ipp = None
135 135 txA = None
136 136 txB = None
137 137 numWindows = None
138 138 numTaus = None
139 139 codeType = None
140 140 line6Function = None
141 141 line5Function = None
142 142 fClock = None
143 143 prePulseBefore = None
144 144 prePulserAfter = None
145 145 rangeIpp = None
146 146 rangeTxA = None
147 147 rangeTxB = None
148 148 struct = None
149 149
150 150 def __init__(self):
151 151 self.size = 0
152 152 self.expType = 0
153 153 self.nTx = 0
154 154 self.ipp = 0
155 155 self.txA = 0
156 156 self.txB = 0
157 157 self.numWindows = 0
158 158 self.numTaus = 0
159 159 self.codeType = 0
160 160 self.line6Function = 0
161 161 self.line5Function = 0
162 162 self.fClock = 0
163 163 self.prePulseBefore = 0
164 164 self.prePulserAfter = 0
165 165 self.rangeIpp = 0
166 166 self.rangeTxA = 0
167 167 self.rangeTxB = 0
168 168 self.struct = numpy.dtype([
169 169 ('nSize','<u4'),
170 170 ('nExpType','<u4'),
171 171 ('nNTx','<u4'),
172 172 ('fIpp','<f4'),
173 173 ('fTxA','<f4'),
174 174 ('fTxB','<f4'),
175 175 ('nNumWindows','<u4'),
176 176 ('nNumTaus','<u4'),
177 177 ('nCodeType','<u4'),
178 178 ('nLine6Function','<u4'),
179 179 ('nLine5Function','<u4'),
180 180 ('fClock','<f4'),
181 181 ('nPrePulseBefore','<u4'),
182 182 ('nPrePulseAfter','<u4'),
183 183 ('sRangeIPP','<a20'),
184 184 ('sRangeTxA','<a20'),
185 185 ('sRangeTxB','<a20'),
186 186 ])
187 187
188 188 self.samplingWindowStruct = numpy.dtype([('h0','<f4'),('dh','<f4'),('nsa','<u4')])
189 189
190 190 self.samplingWindow = None
191 191 self.numHeights = None
192 192 self.firstHeight = None
193 193 self.deltaHeight = None
194 194 self.samplesWin = None
195 195
196 196 self.numCode = None
197 197 self.numBaud = None
198 198 self.code = None
199 199 self.flip1 = None
200 200 self.flip2 = None
201 201
202 202 self.dynamic = numpy.array([],numpy.dtype('byte'))
203 203
204 204
205 205 def read(self, fp):
206 206 try:
207 207 startFp = fp.tell()
208 208 header = numpy.fromfile(fp,self.struct,1)
209 209 self.size = header['nSize'][0]
210 210 self.expType = header['nExpType'][0]
211 211 self.nTx = header['nNTx'][0]
212 212 self.ipp = header['fIpp'][0]
213 213 self.txA = header['fTxA'][0]
214 214 self.txB = header['fTxB'][0]
215 215 self.numWindows = header['nNumWindows'][0]
216 216 self.numTaus = header['nNumTaus'][0]
217 217 self.codeType = header['nCodeType'][0]
218 218 self.line6Function = header['nLine6Function'][0]
219 219 self.line5Function = header['nLine5Function'][0]
220 220 self.fClock = header['fClock'][0]
221 221 self.prePulseBefore = header['nPrePulseBefore'][0]
222 222 self.prePulserAfter = header['nPrePulseAfter'][0]
223 223 self.rangeIpp = header['sRangeIPP'][0]
224 224 self.rangeTxA = header['sRangeTxA'][0]
225 225 self.rangeTxB = header['sRangeTxB'][0]
226 226 # jump Dynamic Radar Controller Header
227 227 jumpFp = self.size - 116
228 228 self.dynamic = numpy.fromfile(fp,numpy.dtype('byte'),jumpFp)
229 229 #pointer backward to dynamic header and read
230 230 backFp = fp.tell() - jumpFp
231 231 fp.seek(backFp)
232 232
233 233 self.samplingWindow = numpy.fromfile(fp,self.samplingWindowStruct,self.numWindows)
234 234 self.numHeights = numpy.sum(self.samplingWindow['nsa'])
235 235 self.firstHeight = self.samplingWindow['h0']
236 236 self.deltaHeight = self.samplingWindow['dh']
237 237 self.samplesWin = self.samplingWindow['nsa']
238 238
239 239 self.Taus = numpy.fromfile(fp,'<f4',self.numTaus)
240 240
241 241 if self.codeType != 0:
242 242 self.numCode = numpy.fromfile(fp,'<u4',1)
243 243 self.numBaud = numpy.fromfile(fp,'<u4',1)
244 244 self.code = numpy.empty([self.numCode,self.numBaud],dtype='u1')
245 245 tempList = []
246 246 for ic in range(self.numCode):
247 247 temp = numpy.fromfile(fp,'u1',4*numpy.ceil(self.numBaud/32.))
248 248 tempList.append(temp)
249 249 self.code[ic] = numpy.unpackbits(temp[::-1])[-1*self.numBaud:]
250 250 self.code = 2.0*self.code - 1.0
251 251
252 252 if self.line5Function == RCfunction.FLIP:
253 253 self.flip1 = numpy.fromfile(fp,'<u4',1)
254 254
255 255 if self.line6Function == RCfunction.FLIP:
256 256 self.flip2 = numpy.fromfile(fp,'<u4',1)
257 257
258 258 endFp = self.size + startFp
259 259 jumpFp = endFp - fp.tell()
260 260 if jumpFp > 0:
261 261 fp.seek(jumpFp)
262 262
263 263 except:
264 264 return 0
265 265
266 266 return 1
267 267
268 268 def write(self, fp):
269 269 headerTuple = (self.size,
270 270 self.expType,
271 271 self.nTx,
272 272 self.ipp,
273 273 self.txA,
274 274 self.txB,
275 275 self.numWindows,
276 276 self.numTaus,
277 277 self.codeType,
278 278 self.line6Function,
279 279 self.line5Function,
280 280 self.fClock,
281 281 self.prePulseBefore,
282 282 self.prePulserAfter,
283 283 self.rangeIpp,
284 284 self.rangeTxA,
285 285 self.rangeTxB)
286 286
287 287 header = numpy.array(headerTuple,self.struct)
288 288 header.tofile(fp)
289 289
290 290 dynamic = self.dynamic
291 291 dynamic.tofile(fp)
292 292
293 293 return 1
294 294
295 295
296 296
297 297 class ProcessingHeader(Header):
298 298
299 299 size = None
300 300 dataType = None
301 301 blockSize = None
302 302 profilesPerBlock = None
303 303 dataBlocksPerFile = None
304 304 numWindows = None
305 305 processFlags = None
306 306 coherentInt = None
307 307 incoherentInt = None
308 308 totalSpectra = None
309 309 struct = None
310 flag_dc = None
311 flag_cspc = None
310 312
311 313 def __init__(self):
312 314 self.size = 0
313 315 self.dataType = 0
314 316 self.blockSize = 0
315 317 self.profilesPerBlock = 0
316 318 self.dataBlocksPerFile = 0
317 319 self.numWindows = 0
318 320 self.processFlags = 0
319 321 self.coherentInt = 0
320 322 self.incoherentInt = 0
321 323 self.totalSpectra = 0
322 324 self.struct = numpy.dtype([
323 325 ('nSize','<u4'),
324 326 ('nDataType','<u4'),
325 327 ('nSizeOfDataBlock','<u4'),
326 328 ('nProfilesperBlock','<u4'),
327 329 ('nDataBlocksperFile','<u4'),
328 330 ('nNumWindows','<u4'),
329 331 ('nProcessFlags','<u4'),
330 332 ('nCoherentIntegrations','<u4'),
331 333 ('nIncoherentIntegrations','<u4'),
332 334 ('nTotalSpectra','<u4')
333 335 ])
334 336 self.samplingWindow = 0
335 337 self.structSamplingWindow = numpy.dtype([('h0','<f4'),('dh','<f4'),('nsa','<u4')])
336 338 self.numHeights = 0
337 339 self.firstHeight = 0
338 340 self.deltaHeight = 0
339 341 self.samplesWin = 0
340 342 self.spectraComb = 0
341 343 self.numCode = 0
342 344 self.code = 0
343 345 self.numBaud = 0
344 346 self.shif_fft = False
345 347 self.flag_dc = False
348 self.flag_cspc = False
346 349
347 350 def read(self, fp):
348 351 try:
349 352 header = numpy.fromfile(fp,self.struct,1)
350 353 self.size = header['nSize'][0]
351 354 self.dataType = header['nDataType'][0]
352 355 self.blockSize = header['nSizeOfDataBlock'][0]
353 356 self.profilesPerBlock = header['nProfilesperBlock'][0]
354 357 self.dataBlocksPerFile = header['nDataBlocksperFile'][0]
355 358 self.numWindows = header['nNumWindows'][0]
356 359 self.processFlags = header['nProcessFlags']
357 360 self.coherentInt = header['nCoherentIntegrations'][0]
358 361 self.incoherentInt = header['nIncoherentIntegrations'][0]
359 362 self.totalSpectra = header['nTotalSpectra'][0]
360 363 self.samplingWindow = numpy.fromfile(fp,self.structSamplingWindow,self.numWindows)
361 364 self.numHeights = numpy.sum(self.samplingWindow['nsa'])
362 365 self.firstHeight = self.samplingWindow['h0']
363 366 self.deltaHeight = self.samplingWindow['dh']
364 367 self.samplesWin = self.samplingWindow['nsa']
365 368 self.spectraComb = numpy.fromfile(fp,'u1',2*self.totalSpectra)
366 369
367 370 if ((self.processFlags & PROCFLAG.DEFINE_PROCESS_CODE) == PROCFLAG.DEFINE_PROCESS_CODE):
368 371 self.numCode = numpy.fromfile(fp,'<u4',1)
369 372 self.numBaud = numpy.fromfile(fp,'<u4',1)
370 373 self.code = numpy.fromfile(fp,'<f4',self.numCode*self.numBaud).reshape(self.numBaud,self.numCode)
371 374
372 375 if ((self.processFlags & PROCFLAG.SHIFT_FFT_DATA) == PROCFLAG.SHIFT_FFT_DATA):
373 376 self.shif_fft = True
374 377 else:
375 378 self.shif_fft = False
376 379
377 380 if ((self.processFlags & PROCFLAG.SAVE_CHANNELS_DC) == PROCFLAG.SAVE_CHANNELS_DC):
378 381 self.flag_dc = True
382
383 nChannels = 0
384 nPairs = 0
385 pairList = []
386
387 for i in range( 0, self.totalSpectra*2, 2 ):
388 if self.spectraComb[i] == self.spectraComb[i+1]:
389 nChannels = nChannels + 1 #par de canales iguales
390 else:
391 nPairs = nPairs + 1 #par de canales diferentes
392 pairList.append( (self.spectraComb[i], self.spectraComb[i+1]) )
393
394 self.flag_cspc = False
395 if nPairs > 0:
396 self.flag_cspc = True
397
379 398 except:
380 399 return 0
381 400
382 401 return 1
383 402
384 403 def write(self, fp):
385 404 headerTuple = (self.size,
386 405 self.dataType,
387 406 self.blockSize,
388 407 self.profilesPerBlock,
389 408 self.dataBlocksPerFile,
390 409 self.numWindows,
391 410 self.processFlags,
392 411 self.coherentInt,
393 412 self.incoherentInt,
394 413 self.totalSpectra)
395 414
396 415 header = numpy.array(headerTuple,self.struct)
397 416 header.tofile(fp)
398 417
399 418 if self.numWindows != 0:
400 419 sampleWindowTuple = (self.firstHeight,self.deltaHeight,self.samplesWin)
401 420 samplingWindow = numpy.array(sampleWindowTuple,self.structSamplingWindow)
402 421 samplingWindow.tofile(fp)
403 422
404 423
405 424 if self.totalSpectra != 0:
406 425 spectraComb = numpy.array([],numpy.dtype('u1'))
407 426 spectraComb = self.spectraComb
408 427 spectraComb.tofile(fp)
409 428
410 429
411 430 if self.processFlags & PROCFLAG.DEFINE_PROCESS_CODE == PROCFLAG.DEFINE_PROCESS_CODE:
412 431 numCode = self.numCode
413 432 numCode.tofile(fp)
414 433
415 434 numBaud = self.numBaud
416 435 numBaud.tofile(fp)
417 436
418 437 code = self.code.reshape(numCode*numBaud)
419 438 code.tofile(fp)
420 439
421 440 return 1
422 441
423 442 class RCfunction:
424 443 NONE=0
425 444 FLIP=1
426 445 CODE=2
427 446 SAMPLING=3
428 447 LIN6DIV256=4
429 448 SYNCHRO=5
430 449
431 450 class nCodeType:
432 451 NONE=0
433 452 USERDEFINE=1
434 453 BARKER2=2
435 454 BARKER3=3
436 455 BARKER4=4
437 456 BARKER5=5
438 457 BARKER7=6
439 458 BARKER11=7
440 459 BARKER13=8
441 460 AC128=9
442 461 COMPLEMENTARYCODE2=10
443 462 COMPLEMENTARYCODE4=11
444 463 COMPLEMENTARYCODE8=12
445 464 COMPLEMENTARYCODE16=13
446 465 COMPLEMENTARYCODE32=14
447 466 COMPLEMENTARYCODE64=15
448 467 COMPLEMENTARYCODE128=16
449 468 CODE_BINARY28=17
450 469
451 470 class PROCFLAG:
452 471 COHERENT_INTEGRATION = numpy.uint32(0x00000001)
453 472 DECODE_DATA = numpy.uint32(0x00000002)
454 473 SPECTRA_CALC = numpy.uint32(0x00000004)
455 474 INCOHERENT_INTEGRATION = numpy.uint32(0x00000008)
456 475 POST_COHERENT_INTEGRATION = numpy.uint32(0x00000010)
457 476 SHIFT_FFT_DATA = numpy.uint32(0x00000020)
458 477
459 478 DATATYPE_CHAR = numpy.uint32(0x00000040)
460 479 DATATYPE_SHORT = numpy.uint32(0x00000080)
461 480 DATATYPE_LONG = numpy.uint32(0x00000100)
462 481 DATATYPE_INT64 = numpy.uint32(0x00000200)
463 482 DATATYPE_FLOAT = numpy.uint32(0x00000400)
464 483 DATATYPE_DOUBLE = numpy.uint32(0x00000800)
465 484
466 485 DATAARRANGE_CONTIGUOUS_CH = numpy.uint32(0x00001000)
467 486 DATAARRANGE_CONTIGUOUS_H = numpy.uint32(0x00002000)
468 487 DATAARRANGE_CONTIGUOUS_P = numpy.uint32(0x00004000)
469 488
470 489 SAVE_CHANNELS_DC = numpy.uint32(0x00008000)
471 490 DEFLIP_DATA = numpy.uint32(0x00010000)
472 491 DEFINE_PROCESS_CODE = numpy.uint32(0x00020000)
473 492
474 493 ACQ_SYS_NATALIA = numpy.uint32(0x00040000)
475 494 ACQ_SYS_ECHOTEK = numpy.uint32(0x00080000)
476 495 ACQ_SYS_ADRXD = numpy.uint32(0x000C0000)
477 496 ACQ_SYS_JULIA = numpy.uint32(0x00100000)
478 497 ACQ_SYS_XXXXXX = numpy.uint32(0x00140000)
479 498
480 499 EXP_NAME_ESP = numpy.uint32(0x00200000)
481 500 CHANNEL_NAMES_ESP = numpy.uint32(0x00400000)
482 501
483 502 OPERATION_MASK = numpy.uint32(0x0000003F)
484 503 DATATYPE_MASK = numpy.uint32(0x00000FC0)
485 504 DATAARRANGE_MASK = numpy.uint32(0x00007000)
486 505 ACQ_SYS_MASK = numpy.uint32(0x001C0000) No newline at end of file
@@ -1,78 +1,76
1 1 '''
2 2 Created on Feb 7, 2012
3 3
4 4 @author $Author$
5 5 @version $Id$
6 6 '''
7 7
8 from JROData import JROData, Noise
8 from JROData import JROData
9 9 from JROHeader import RadarControllerHeader, ProcessingHeader, SystemHeader, BasicHeader
10 10
11 11 class Spectra(JROData):
12 12 '''
13 13 classdocs
14 14 '''
15 15
16 16 data_spc = None
17 17
18 18 data_cspc = None
19 19
20 20 data_dc = None
21 21
22 22 nFFTPoints = None
23 23
24 24 nPairs = None
25 25
26 26 pairsList = None
27 27
28 28
29 29 def __init__(self):
30 30 '''
31 31 Constructor
32 32 '''
33 33
34 34 self.m_RadarControllerHeader = RadarControllerHeader()
35 35
36 36 self.m_ProcessingHeader = ProcessingHeader()
37 37
38 38 self.m_SystemHeader = SystemHeader()
39 39
40 40 self.m_BasicHeader = BasicHeader()
41 41
42 self.m_NoiseObj = Noise()
43
44 42 self.type = "Spectra"
45 43
46 44 self.dataType = None
47 45
48 46 self.nHeights = 0
49 47
50 48 self.nChannels = 0
51 49
52 50 self.channelList = None
53 51
54 52 self.heightList = None
55 53
56 54 self.flagNoData = True
57 55
58 56 self.flagResetProcessing = False
59 57
60 58
61 59 #data es un numpy array de 3 dmensiones (perfiles, alturas y canales)
62 60 self.data_spc = None
63 61
64 62 self.data_cspc = None
65 63
66 64 self.data_dc = None
67 65
68 66 self.nFFTPoints = None
69 67
70 68 self.nAvg = None
71 69
72 70 self.nPairs = 0
73 71
74 72 self.pairsList = None
75 73
76 74
77 75
78 76 No newline at end of file
@@ -1,63 +1,61
1 1 '''
2 2 Created on Feb 7, 2012
3 3
4 4 @author $Author$
5 5 @version $Id$
6 6 '''
7 7
8 from JROData import JROData, Noise
8 from JROData import JROData
9 9 from JROHeader import RadarControllerHeader, ProcessingHeader, SystemHeader, BasicHeader
10 10
11 11 class Voltage(JROData):
12 12 '''
13 13 classdocs
14 14 '''
15 15
16 16 data = None
17 17
18 18 nProfiles = None
19 19
20 20 profileIndex = None
21 21
22 22 def __init__(self):
23 23 '''
24 24 Constructor
25 25 '''
26 26
27 27 self.m_RadarControllerHeader = RadarControllerHeader()
28 28
29 29 self.m_ProcessingHeader = ProcessingHeader()
30 30
31 31 self.m_SystemHeader = SystemHeader()
32 32
33 33 self.m_BasicHeader = BasicHeader()
34 34
35 self.m_NoiseObj = Noise()
36
37 35 self.type = "Voltage"
38 36
39 37 #data es un numpy array de 3 dmensiones (perfiles, alturas y canales)
40 38 self.data = None
41 39
42 40 self.dataType = None
43 41
44 42 self.nHeights = 0
45 43
46 44 self.nChannels = 0
47 45
48 46 self.channelList = None
49 47
50 48 self.heightList = None
51 49
52 50 self.flagNoData = True
53 51
54 52 self.flagResetProcessing = False
55 53
56 54 self.nAvg = None
57 55
58 56 self.profileIndex = None
59 57
60 58 self.nProfiles = None
61 59
62 60
63 61 No newline at end of file
@@ -1,112 +1,213
1 1 import numpy
2 2 from Model.Spectra import Spectra
3 3
4 def hildebrand_sekhon(Data, navg=1):
4 def hildebrand_sekhon(Data, navg):
5 5 """
6 6 This method is for the objective determination of de noise level in Doppler spectra. This
7 7 implementation technique is based on the fact that the standard deviation of the spectral
8 8 densities is equal to the mean spectral density for white Gaussian noise
9 9
10 10 Inputs:
11 11 Data : heights
12 12 navg : numbers of averages
13 13
14 14 Return:
15 15 -1 : any error
16 16 anoise : noise's level
17 17 """
18
18 19 divisor = 8
19 20 ratio = 7 / divisor
20 21 data = Data.reshape(-1)
21 22 npts = data.size #numbers of points of the data
22 23
23 24 if npts < 32:
24 25 print "error in noise - requires at least 32 points"
25 26 return -1.0
26 27
27 28 # data sorted in ascending order
28 29 nmin = int(npts/divisor + ratio);
29 30 s = 0.0
30 31 s2 = 0.0
31 32 data2 = data[:npts]
32 33 data2.sort()
33 34
34 35 for i in range(nmin):
35 36 s += data2[i]
36 37 s2 += data2[i]**2;
37 38
38 39 icount = nmin
39 40 iflag = 0
40 41
41 42 for i in range(nmin, npts):
42 43 s += data2[i];
43 44 s2 += data2[i]**2
44 45 icount=icount+1;
45 46 p = s / float(icount);
46 47 p2 = p**2;
47 48 q = s2 / float(icount) - p2;
48 49 leftc = p2;
49 50 rightc = q * float(navg);
50 51
51 52 if leftc > rightc:
52 53 iflag = 1; #No weather signal
53 54 # Signal detect: R2 < 1 (R2 = leftc/rightc)
54 55 if(leftc < rightc):
55 56 if iflag:
56 57 break
57 58
58 59 anoise = 0.0;
59 60 for j in range(i):
60 61 anoise += data2[j];
61 62
62 63 anoise = anoise / float(i);
63 64
64 65 return anoise;
65 66
67 def sorting_bruce(Data, navg):
68 sortdata = numpy.sort(Data)
69 lenOfData = len(Data)
70 nums_min = lenOfData/10
71
72 if (lenOfData/10) > 0:
73 nums_min = lenOfData/10
74 else:
75 nums_min = 0
76
77 rtest = 1.0 + 1.0/navg
78
79 sum = 0.
80
81 sumq = 0.
82
83 j = 0
84
85 cont = 1
86
87 while((cont==1)and(j<lenOfData)):
88
89 sum += sortdata[j]
90
91 sumq += sortdata[j]**2
92
93 j += 1
94
95 if j > nums_min:
96 if ((sumq*j) <= (rtest*sum**2)):
97 lnoise = sum / j
98 else:
99 j = j - 1
100 sum = sum - sordata[j]
101 sumq = sumq - sordata[j]**2
102 cont = 0
103
104 if j == nums_min:
105 lnoise = sum /j
106
107 return lnoise
66 108
67 109 class Noise:
68 110 """
69 111 Clase que implementa los metodos necesarios para deternimar el nivel de ruido en un Spectro Doppler
70 112 """
71 m_DataObj = None
113 data = None
114 noise = None
115 dim = None
72 116
117 def __init__(self, data=None):
118 """
119 Inicializador de la clase Noise para la la determinacion del nivel de ruido en un Spectro Doppler.
73 120
74 def __init__(self, m_Spectra=None):
121 Inputs:
122 data: Numpy array de la forma nChan x nHeis x nProfiles
123
124 Affected:
125 self.noise
126
127 Return:
128 None
129 """
130
131 self.data = data
132 self.dim = None
133 self.nChannels = None
134 self.noise = None
135
136 def setNoise(self, data):
75 137 """
76 138 Inicializador de la clase Noise para la la determinacion del nivel de ruido en un Spectro Doppler.
77 139
140 Inputs:
141 data: Numpy array de la forma nChan x nHeis x nProfiles
142
78 143 Affected:
79 self.m_DataObj
144 self.noise
80 145
81 146 Return:
82 147 None
83 148 """
84 if m_Spectra == None:
85 m_Spectra = Spectra()
86 149
87 if not(isinstance(m_Spectra, Spectra)):
88 raise ValueError, "in Noise class, m_Spectra must be an Spectra class object"
150 if data == None:
151 return 0
152
153 shape = data.shape
154 self.dim = len(shape)
155 if self.dim == 3:
156 nChan, nProfiles, nHeis = shape
157 elif self.dim == 2:
158 nChan, nHeis = shape
159 else:
160 raise ValueError, ""
89 161
90 self.m_DataObj = m_Spectra
162 self.nChannels = nChan
163 self.data = data.copy()
164 self.noise = numpy.zeros(nChan)
91 165
166 return 1
167
92 168
93 def getNoiseLevelByHildebrandSekhon(self):
169 def byHildebrand(self, navg=1):
94 170 """
95 171 Determino el nivel de ruido usando el metodo Hildebrand-Sekhon
96 172
97 173 Return:
98 noise level
174 noiselevel
99 175 """
100 data = self.m_DataObj.data_spc
176
101 177 daux = None
102 178
103 for channel in range(self.m_DataObj.nChannels):
179 for channel in range(self.nChannels):
180 daux = self.data[channel,:,:]
181 self.noise[channel] = hildebrand_sekhon(daux, navg)
182 return self.noise
183
184 def byWindow(self, heiIndexMin, heiIndexMax, freqIndexMin, freqIndexMax):
185 """
186 Determina el ruido del canal utilizando la ventana indicada con las coordenadas:
187 (heiIndexMIn, freqIndexMin) hasta (heiIndexMax, freqIndexMAx)
188
189 Inputs:
190 heiIndexMin: Limite inferior del eje de alturas
191 heiIndexMax: Limite superior del eje de alturas
192 freqIndexMin: Limite inferior del eje de frecuencia
193 freqIndexMax: Limite supoerior del eje de frecuencia
194 """
195
196 data = self.data[:, heiIndexMin:heiIndexMax, freqIndexMin:freqIndexMax]
197
198 for channel in range(self.nChannels):
104 199 daux = data[channel,:,:]
105 noiselevel = hildebrand_sekhon(daux)
106 print noiselevel
200 self.noise[channel] = numpy.average(daux)
107 201
202 return self.noise
203
204 def bySort(self,navg = 1):
205 daux = None
206
207 for channel in range(self.nChannels):
208 daux = self.data[channel,:,:]
209 self.noise[channel] = sorting_bruce(daux, navg)
210
211 return self.noise
108 212
109 for pair in range(self.m_DataObj.nPairs):
110 daux = data[pair,:,:]
111 noiselevel = hildebrand_sekhon(daux)
112 print noiselevel
213 No newline at end of file
@@ -1,620 +1,660
1 1 '''
2 2 Created on Feb 7, 2012
3 3
4 4 @author $Author$
5 5 @version $Id$
6 6 '''
7 7 import os, sys
8 8 import numpy
9 9
10 10 path = os.path.split(os.getcwd())[0]
11 11 sys.path.append(path)
12 12
13 13 from Model.Spectra import Spectra
14 14 from IO.SpectraIO import SpectraWriter
15 15 from Graphics.SpectraPlot import Spectrum
16
16 from JRONoise import Noise
17 17
18 18 class SpectraProcessor:
19 19 '''
20 20 classdocs
21 21 '''
22 22
23 23 dataInObj = None
24 24
25 25 dataOutObj = None
26 26
27 integratorObjIndex = None
28
29 decoderObjIndex = None
30
31 writerObjIndex = None
32
33 plotterObjIndex = None
27 noiseObj = None
34 28
35 29 integratorObjList = []
36 30
37 31 decoderObjList = []
38 32
39 33 writerObjList = []
40 34
41 35 plotterObjList = []
42 36
37 integratorObjIndex = None
38
39 decoderObjIndex = None
40
41 writerObjIndex = None
42
43 plotterObjIndex = None
44
43 45 buffer = None
44 46
45 ptsId = 0
47 profIndex = 0
46 48
47 49 nFFTPoints = None
48 50
51 nChannels = None
52
53 nHeights = None
54
55 nPairs = None
56
49 57 pairList = None
50 58
51 59
52 def __init__(self, dataInObj=None, dataOutObj=None):
60 def __init__(self):
53 61 '''
54 62 Constructor
55 63 '''
56 self.dataInObj = dataInObj
57 64
58 if dataOutObj == None:
59 self.dataOutObj = Spectra()
60 else:
61 self.dataOutObj = dataOutObj
62
63 65 self.integratorObjIndex = None
64 66 self.decoderObjIndex = None
65 67 self.writerObjIndex = None
66 68 self.plotterObjIndex = None
67 69
68 70 self.integratorObjList = []
69 71 self.decoderObjList = []
70 72 self.writerObjList = []
71 73 self.plotterObjList = []
72 74
75 self.noiseObj = Noise()
73 76 self.buffer = None
74 self.ptsId = 0
75
76 def setIO(self,inputObject, outputObject):
77
78 # if not( isinstance(inputObject, Voltage) ):
79 # print 'InputObject must be an instance from Voltage()'
80 # sys.exit(0)
77 self.profIndex = 0
81 78
82 if not( isinstance(outputObject, Spectra) ):
83 print 'OutputObject must be an instance from Spectra()'
84 sys.exit(0)
79 def setup(self, dataInObj=None, dataOutObj=None, nFFTPoints=None, pairList=None):
85 80
86 self.dataInObj = inputObject
87 self.dataOutObj = outputObject
81 if dataInObj == None:
82 raise ValueError, ""
88 83
89 def setup(self,nFFTPoints=None, pairList=None):
90 84 if nFFTPoints == None:
91 nFFTPoints = self.dataOutObj.nFFTPoints
85 raise ValueError, ""
86
87 self.dataInObj = dataInObj
92 88
89 if dataOutObj == None:
90 dataOutObj = Spectra()
91
92 self.dataOutObj = dataOutObj
93 self.noiseObj = Noise()
94
95 ##########################################
93 96 self.nFFTPoints = nFFTPoints
97 self.nChannels = self.dataInObj.nChannels
98 self.nHeights = self.dataInObj.nHeights
94 99 self.pairList = pairList
100 if pairList != None:
101 self.nPairs = len(pairList)
102 else:
103 self.nPairs = 0
104
105 self.dataOutObj.heightList = self.dataInObj.heightList
106 self.dataOutObj.channelIndexList = self.dataInObj.channelIndexList
107 self.dataOutObj.m_BasicHeader = self.dataInObj.m_BasicHeader.copy()
108 self.dataOutObj.m_ProcessingHeader = self.dataInObj.m_ProcessingHeader.copy()
109 self.dataOutObj.m_RadarControllerHeader = self.dataInObj.m_RadarControllerHeader.copy()
110 self.dataOutObj.m_SystemHeader = self.dataInObj.m_SystemHeader.copy()
111
112 self.dataOutObj.dataType = self.dataInObj.dataType
113 self.dataOutObj.nPairs = self.nPairs
114 self.dataOutObj.nChannels = self.nChannels
115 self.dataOutObj.nProfiles = self.nFFTPoints
116 self.dataOutObj.nHeights = self.nHeights
117 self.dataOutObj.nFFTPoints = self.nFFTPoints
118 #self.dataOutObj.data = None
119
120 self.dataOutObj.m_SystemHeader.numChannels = self.nChannels
121 self.dataOutObj.m_SystemHeader.nProfiles = self.nFFTPoints
122
123 self.dataOutObj.m_ProcessingHeader.totalSpectra = self.nChannels + self.nPairs
124 self.dataOutObj.m_ProcessingHeader.profilesPerBlock = self.nFFTPoints
125 self.dataOutObj.m_ProcessingHeader.numHeights = self.nHeights
126 self.dataOutObj.m_ProcessingHeader.shif_fft = True
127
128 spectraComb = numpy.zeros( (self.nChannels+self.nPairs)*2,numpy.dtype('u1'))
129 k = 0
130 for i in range( 0,self.nChannels*2,2 ):
131 spectraComb[i] = k
132 spectraComb[i+1] = k
133 k += 1
134
135 k *= 2
136
137 if self.pairList != None:
138
139 for pair in self.pairList:
140 spectraComb[k] = pair[0]
141 spectraComb[k+1] = pair[1]
142 k += 2
143
144 self.dataOutObj.m_ProcessingHeader.spectraComb = spectraComb
145
146 return self.dataOutObj
95 147
96 # def init(self, nFFTPoints, pairList=None):
97 148 def init(self):
98 149
99 150 self.integratorObjIndex = 0
100 151 self.decoderObjIndex = 0
101 152 self.writerObjIndex = 0
102 153 self.plotterObjIndex = 0
103 154
104 # if nFFTPoints == None:
105 # nFFTPoints = self.dataOutObj.nFFTPoints
106 #
107 # self.nFFTPoints = nFFTPoints
108 # self.pairList = pairList
109 #
110 if not( isinstance(self.dataInObj, Spectra) ):
111 self.__getFft()
112 else:
155 if self.dataInObj.type == "Voltage":
156
157 if self.buffer == None:
158 self.buffer = numpy.zeros((self.nChannels,
159 self.nFFTPoints,
160 self.nHeights),
161 dtype='complex')
162
163 self.buffer[:,self.profIndex,:] = self.dataInObj.data
164 self.profIndex += 1
165
166 if self.profIndex == self.nFFTPoints:
167 self.__getFft()
168 self.dataOutObj.flagNoData = False
169
170 self.buffer = None
171 self.profIndex = 0
172 return
173
174 self.dataOutObj.flagNoData = True
175
176 return
177
178 #Other kind of data
179 if self.dataInObj.type == "Spectra":
113 180 self.dataOutObj.copy(self.dataInObj)
114
115
181 self.dataOutObj.flagNoData = False
182 return
183
184 raise ValueError, "The datatype is not valid"
185
116 186 def __getFft(self):
117 187 """
118 188 Convierte valores de Voltaje a Spectra
119 189
120 190 Affected:
121 191 self.dataOutObj.data_spc
122 192 self.dataOutObj.data_cspc
123 193 self.dataOutObj.data_dc
124 194 self.dataOutObj.heightList
125 195 self.dataOutObj.m_BasicHeader
126 196 self.dataOutObj.m_ProcessingHeader
127 197 self.dataOutObj.m_RadarControllerHeader
128 198 self.dataOutObj.m_SystemHeader
129 self.ptsId
199 self.profIndex
130 200 self.buffer
131 201 self.dataOutObj.flagNoData
132 202 self.dataOutObj.dataType
133 203 self.dataOutObj.nPairs
134 204 self.dataOutObj.nChannels
135 205 self.dataOutObj.nProfiles
136 206 self.dataOutObj.m_SystemHeader.numChannels
137 207 self.dataOutObj.m_ProcessingHeader.totalSpectra
138 208 self.dataOutObj.m_ProcessingHeader.profilesPerBlock
139 209 self.dataOutObj.m_ProcessingHeader.numHeights
140 210 self.dataOutObj.m_ProcessingHeader.spectraComb
141 211 self.dataOutObj.m_ProcessingHeader.shif_fft
142 212 """
143 213 if self.dataInObj.flagNoData:
144 214 return 0
145
146 blocksize = 0
147 nFFTPoints = self.nFFTPoints
148 nChannels, nheis = self.dataInObj.data.shape
149
150 if self.buffer == None:
151 self.buffer = numpy.zeros((nChannels, nFFTPoints, nheis), dtype='complex')
152
153 self.buffer[:,self.ptsId,:] = self.dataInObj.data
154 self.ptsId += 1
155
156 if self.ptsId < self.nFFTPoints:
157 self.dataOutObj.flagNoData = True
158 return
159 215
160 216 fft_volt = numpy.fft.fft(self.buffer,axis=1)
161 217 dc = fft_volt[:,0,:]
162 218
163 219 #calculo de self-spectra
164 220 fft_volt = numpy.fft.fftshift(fft_volt,axes=(1,))
165 221 spc = numpy.abs(fft_volt * numpy.conjugate(fft_volt))
166 222
223 blocksize = 0
167 224 blocksize += dc.size
168 225 blocksize += spc.size
169 226
170 227 cspc = None
171 nPair = 0
228 pairIndex = 0
172 229 if self.pairList != None:
173 230 #calculo de cross-spectra
174 nPairs = len(self.pairList)
175 cspc = numpy.zeros((nPairs, nFFTPoints, nheis), dtype='complex')
231 cspc = numpy.zeros((self.nPairs, self.nFFTPoints, self.nHeights), dtype='complex')
176 232 for pair in self.pairList:
177 cspc[nPair,:,:] = numpy.abs(fft_volt[pair[0],:,:] * numpy.conjugate(fft_volt[pair[1],:,:]))
178 nPair += 1
233 cspc[pairIndex,:,:] = numpy.abs(fft_volt[pair[0],:,:] * numpy.conjugate(fft_volt[pair[1],:,:]))
234 pairIndex += 1
179 235 blocksize += cspc.size
180 236
181 237 self.dataOutObj.data_spc = spc
182 238 self.dataOutObj.data_cspc = cspc
183 239 self.dataOutObj.data_dc = dc
184
185 self.ptsId = 0
186 self.buffer = None
187 self.dataOutObj.flagNoData = False
188
189 self.dataOutObj.heightList = self.dataInObj.heightList
190 self.dataOutObj.channelList = self.dataInObj.channelList
191 self.dataOutObj.m_BasicHeader = self.dataInObj.m_BasicHeader.copy()
192 self.dataOutObj.m_ProcessingHeader = self.dataInObj.m_ProcessingHeader.copy()
193 self.dataOutObj.m_RadarControllerHeader = self.dataInObj.m_RadarControllerHeader.copy()
194 self.dataOutObj.m_SystemHeader = self.dataInObj.m_SystemHeader.copy()
195
196 self.dataOutObj.dataType = self.dataInObj.dataType
197 self.dataOutObj.nPairs = nPair
198 self.dataOutObj.nChannels = nChannels
199 self.dataOutObj.nProfiles = nFFTPoints
200 self.dataOutObj.nHeights = nheis
201 self.dataOutObj.nFFTPoints = nFFTPoints
202 #self.dataOutObj.data = None
203
204 self.dataOutObj.m_SystemHeader.numChannels = nChannels
205 self.dataOutObj.m_SystemHeader.nProfiles = nFFTPoints
206
207 240 self.dataOutObj.m_ProcessingHeader.blockSize = blocksize
208 self.dataOutObj.m_ProcessingHeader.totalSpectra = nChannels + nPair
209 self.dataOutObj.m_ProcessingHeader.profilesPerBlock = nFFTPoints
210 self.dataOutObj.m_ProcessingHeader.numHeights = nheis
211 self.dataOutObj.m_ProcessingHeader.shif_fft = True
212
213 spectraComb = numpy.zeros( (nChannels+nPair)*2,numpy.dtype('u1'))
214 k = 0
215 for i in range( 0,nChannels*2,2 ):
216 spectraComb[i] = k
217 spectraComb[i+1] = k
218 k += 1
219
220 k *= 2
221
222 if self.pairList != None:
223
224 for pair in self.pairList:
225 spectraComb[k] = pair[0]
226 spectraComb[k+1] = pair[1]
227 k += 2
228
229 self.dataOutObj.m_ProcessingHeader.spectraComb = spectraComb
230 241
231 242
232 243 def addWriter(self,wrpath):
233 244 objWriter = SpectraWriter(self.dataOutObj)
234 245 objWriter.setup(wrpath)
235 246 self.writerObjList.append(objWriter)
236 247
237 248
238 249 def addPlotter(self, index=None):
239 250
240 251 if index==None:
241 252 index = self.plotterObjIndex
242 253
243 254 plotObj = Spectrum(self.dataOutObj, index)
244 255 self.plotterObjList.append(plotObj)
245 256
246 257
247 258 def addIntegrator(self,N,timeInterval):
248 259
249 260 objIncohInt = IncoherentIntegration(N,timeInterval)
250 261 self.integratorObjList.append(objIncohInt)
251 262
252 263 def writeData(self, wrpath):
253 264 if self.dataOutObj.flagNoData:
254 265 return 0
255 266
256 267 if len(self.writerObjList) <= self.writerObjIndex:
257 268 self.addWriter(wrpath)
258 269
259 270 self.writerObjList[self.writerObjIndex].putData()
260 271
261 272 self.writerObjIndex += 1
262 273
263 274 def plotData(self,xmin=None, xmax=None, ymin=None, ymax=None, winTitle='', index=None):
264 275 if self.dataOutObj.flagNoData:
265 276 return 0
266 277
267 278 if len(self.plotterObjList) <= self.plotterObjIndex:
268 279 self.addPlotter(index)
269 280
270 281 self.plotterObjList[self.plotterObjIndex].plotData(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,winTitle=winTitle)
271 282
272 283 self.plotterObjIndex += 1
273 284
274 285 def integrator(self, N=None, timeInterval=None):
275 286
276 287 if self.dataOutObj.flagNoData:
277 288 return 0
278 289
279 290 if len(self.integratorObjList) <= self.integratorObjIndex:
280 291 self.addIntegrator(N,timeInterval)
281 292
282 293 myIncohIntObj = self.integratorObjList[self.integratorObjIndex]
283 294 myIncohIntObj.exe(data=self.dataOutObj.data_spc,timeOfData=self.dataOutObj.m_BasicHeader.utc)
284 295
285 296 if myIncohIntObj.isReady:
286 297 self.dataOutObj.data_spc = myIncohIntObj.data
287 298 self.dataOutObj.nAvg = myIncohIntObj.navg
288 299 self.dataOutObj.m_ProcessingHeader.incoherentInt *= myIncohIntObj.navg
289 300 #print "myIncohIntObj.navg: ",myIncohIntObj.navg
290 301 self.dataOutObj.flagNoData = False
291 302
303 self.getNoise(type="hildebrand",parm=myIncohIntObj.navg)
304 # self.getNoise(type="sort", parm=16)
305
292 306 else:
293 307 self.dataOutObj.flagNoData = True
294 308
295 309 self.integratorObjIndex += 1
296
310
311 """Calcular el ruido"""
312 # self.getNoise(type="hildebrand", parm=1)
313
297 314 def removeDC(self, type):
298 315
299 316 if self.dataOutObj.flagNoData:
300 317 return 0
301 pass
302 318
303 319 def removeInterference(self):
304 320
305 321 if self.dataOutObj.flagNoData:
306 322 return 0
307 pass
308 323
309 324 def removeSatellites(self):
310 325
311 326 if self.dataOutObj.flagNoData:
312 327 return 0
313 pass
314 328
315 def selectChannels(self, channelList, pairList=None):
329 def getNoise(self, type="hildebrand", parm=None):
330
331 self.noiseObj.setNoise(self.dataOutObj.data_spc)
332
333 if type == "hildebrand":
334 noise = self.noiseObj.byHildebrand(parm)
335
336 if type == "window":
337 noise = self.noiseObj.byWindow(parm)
338
339 if type == "sort":
340 noise = self.noiseObj.bySort(parm)
341
342 self.dataOutObj.noise = noise
343 print 10*numpy.log10(noise)
344
345 def selectChannels(self, channelList, pairList=[]):
346
347 channelIndexList = []
348
349 for channel in channelList:
350 if channel in self.dataOutObj.channelList:
351 index = self.dataOutObj.channelList.index(channel)
352 channelIndexList.append(index)
353
354 pairIndexList = []
355
356 for pair in pairList:
357 if pair in self.dataOutObj.pairList:
358 index = self.dataOutObj.pairList.index(pair)
359 pairIndexList.append(index)
360
361 self.selectChannelsByIndex(channelIndexList, pairIndexList)
362
363 def selectChannelsByIndex(self, channelIndexList, pairIndexList=[]):
316 364 """
317 Selecciona un bloque de datos en base a canales y pares segun el channelList y el pairList
365 Selecciona un bloque de datos en base a canales y pares segun el
366 channelIndexList y el pairIndexList
318 367
319 368 Input:
320 channelList : lista sencilla de canales a seleccionar por ej. (2,3,7)
321 pairList : tupla de pares que se desea selecionar por ej. ( (0,1), (0,2) )
369 channelIndexList : lista de indices de los canales a seleccionar por ej.
370
371 Si tenemos los canales
372
373 self.channelList = (2,3,5,7)
374
375 y deseamos escoger los canales (3,7)
376 entonces colocaremos el parametro
377
378 channelndexList = (1,3)
379
380 pairIndexList : tupla de indice depares que se desea selecionar por ej.
381
382 Si tenemos los pares :
383
384 ( (0,1), (0,2), (1,3), (2,5) )
385
386 y deseamos seleccionar los pares ((0,2), (2,5))
387 entonces colocaremos el parametro
388
389 pairIndexList = (1,3)
322 390
323 391 Affected:
324 392 self.dataOutObj.data_spc
325 393 self.dataOutObj.data_cspc
326 394 self.dataOutObj.data_dc
327 395 self.dataOutObj.nChannels
328 396 self.dataOutObj.nPairs
329 397 self.dataOutObj.m_ProcessingHeader.spectraComb
330 398 self.dataOutObj.m_SystemHeader.numChannels
331 399
400 self.dataOutObj.noise
332 401 Return:
333 402 None
334 403 """
335 404
336 405 if self.dataOutObj.flagNoData:
337 406 return 0
338 407
339 channelIndexList = []
340 for channel in channelList:
341 if channel in self.dataOutObj.channelList:
342 index = self.dataOutObj.channelList.index(channel)
343 channelIndexList.append(index)
344 continue
345
346 raise ValueError, "The value %d in channelList is not valid" %channel
347
348 nProfiles = self.dataOutObj.nProfiles
349 #dataType = self.dataOutObj.dataType
350 nHeights = self.dataOutObj.nHeights #m_ProcessingHeader.numHeights
351 blocksize = 0
352
353 #self spectra
408 if pairIndexList == []:
409 pairIndexList = numpy.arange(len(self.dataOutObj.pairList))
410
354 411 nChannels = len(channelIndexList)
355 spc = numpy.zeros( (nChannels,nProfiles,nHeights), dtype='float' ) #dataType[0] )
356
357 for index, channel in enumerate(channelIndexList):
358 spc[index,:,:] = self.dataOutObj.data_spc[index,:,:]
412 nPairs = len(pairIndexList)
359 413
360 #DC channel
361 dc = numpy.zeros( (nChannels,nHeights), dtype='complex' )
362 for index, channel in enumerate(channelIndexList):
363 dc[index,:] = self.dataOutObj.data_dc[channel,:]
364
365 blocksize += dc.size
414 blocksize = 0
415 #self spectra
416 spc = self.dataOutObj.data_spc[channelIndexList,:,:]
366 417 blocksize += spc.size
367
368 nPairs = 0
369 cspc = None
370 418
371 if pairList == None:
372 pairList = self.pairList
373
374 if (pairList != None) and (self.dataOutObj.data_cspc != None):
375 #cross spectra
376 nPairs = len(pairList)
377 cspc = numpy.zeros( (nPairs,nProfiles,nHeights), dtype='complex' )
378
379 spectraComb = self.dataOutObj.m_ProcessingHeader.spectraComb
380 totalSpectra = len(spectraComb)
381 nchan = self.dataOutObj.nChannels
382 pairIndexList = []
383
384 for pair in pairList: #busco el par en la lista de pares del Spectra Combinations
385 for index in range(0,totalSpectra,2):
386 if pair[0] == spectraComb[index] and pair[1] == spectraComb[index+1]:
387 pairIndexList.append( index/2 - nchan )
388
389 for index, pair in enumerate(pairIndexList):
390 cspc[index,:,:] = self.dataOutObj.data_cspc[pair,:,:]
419 cspc = None
420 if pairIndexList != []:
421 cspc = self.dataOutObj.data_cspc[pairIndexList,:,:]
391 422 blocksize += cspc.size
392
393 else:
394 pairList = self.pairList
395 cspc = self.dataOutObj.data_cspc
396 if cspc != None:
397 blocksize += cspc.size
398 423
424 #DC channel
425 dc = None
426 if self.dataOutObj.m_ProcessingHeader.flag_dc:
427 dc = self.dataOutObj.data_dc[channelIndexList,:]
428 blocksize += dc.size
429
430 #Almacenar las combinaciones de canales y cros espectros
431
399 432 spectraComb = numpy.zeros( (nChannels+nPairs)*2,numpy.dtype('u1'))
400 433 i = 0
401 for val in channelList:
402 spectraComb[i] = val
403 spectraComb[i+1] = val
434 for spcChannel in channelIndexList:
435 spectraComb[i] = spcChannel
436 spectraComb[i+1] = spcChannel
404 437 i += 2
405 438
406 439 if pairList != None:
407 440 for pair in pairList:
408 441 spectraComb[i] = pair[0]
409 442 spectraComb[i+1] = pair[1]
410 443 i += 2
411
444
445 #######
446
412 447 self.dataOutObj.data_spc = spc
413 448 self.dataOutObj.data_cspc = cspc
414 449 self.dataOutObj.data_dc = dc
415 450 self.dataOutObj.nChannels = nChannels
416 451 self.dataOutObj.nPairs = nPairs
417 452
418 self.dataOutObj.channelList = channelList
453 self.dataOutObj.channelIndexList = channelIndexList
419 454
420 455 self.dataOutObj.m_ProcessingHeader.spectraComb = spectraComb
421 456 self.dataOutObj.m_ProcessingHeader.totalSpectra = nChannels + nPairs
422 457 self.dataOutObj.m_SystemHeader.numChannels = nChannels
423 458 self.dataOutObj.nChannels = nChannels
424 459 self.dataOutObj.m_ProcessingHeader.blockSize = blocksize
425
460
461 if cspc == None:
462 self.dataOutObj.m_ProcessingHeader.flag_dc = False
463 if dc == None:
464 self.dataOutObj.m_ProcessingHeader.flag_cpsc = False
465
426 466 def selectHeightsByValue(self, minHei, maxHei):
427 467 """
428 468 Selecciona un bloque de datos en base a un grupo de valores de alturas segun el rango
429 469 minHei <= height <= maxHei
430 470
431 471 Input:
432 472 minHei : valor minimo de altura a considerar
433 473 maxHei : valor maximo de altura a considerar
434 474
435 475 Affected:
436 476 Indirectamente son cambiados varios valores a travez del metodo selectHeightsByIndex
437 477
438 478 Return:
439 479 None
440 480 """
441 481
442 482 if self.dataOutObj.flagNoData:
443 483 return 0
444 484
445 485 if (minHei < self.dataOutObj.heightList[0]) or (minHei > maxHei):
446 486 raise ValueError, "some value in (%d,%d) is not valid" % (minHei, maxHei)
447 487
448 488 if (maxHei > self.dataOutObj.heightList[-1]):
449 489 raise ValueError, "some value in (%d,%d) is not valid" % (minHei, maxHei)
450 490
451 491 minIndex = 0
452 492 maxIndex = 0
453 493 data = self.dataOutObj.heightList
454 494
455 495 for i,val in enumerate(data):
456 496 if val < minHei:
457 497 continue
458 498 else:
459 499 minIndex = i;
460 500 break
461 501
462 502 for i,val in enumerate(data):
463 503 if val <= maxHei:
464 504 maxIndex = i;
465 505 else:
466 506 break
467 507
468 508 self.selectHeightsByIndex(minIndex, maxIndex)
469 509
470 510 def selectHeightsByIndex(self, minIndex, maxIndex):
471 511 """
472 512 Selecciona un bloque de datos en base a un grupo indices de alturas segun el rango
473 513 minIndex <= index <= maxIndex
474 514
475 515 Input:
476 516 minIndex : valor minimo de altura a considerar
477 517 maxIndex : valor maximo de altura a considerar
478 518
479 519 Affected:
480 520 self.dataOutObj.data_spc
481 521 self.dataOutObj.data_cspc
482 522 self.dataOutObj.data_dc
483 523 self.dataOutObj.heightList
484 524 self.dataOutObj.nHeights
485 525 self.dataOutObj.m_ProcessingHeader.numHeights
486 526 self.dataOutObj.m_ProcessingHeader.blockSize
487 527 self.dataOutObj.m_ProcessingHeader.firstHeight
488 528 self.dataOutObj.m_RadarControllerHeader.numHeights
489 529
490 530 Return:
491 531 None
492 532 """
493 533
494 534 if self.dataOutObj.flagNoData:
495 535 return 0
496 536
497 537 if (minIndex < 0) or (minIndex > maxIndex):
498 538 raise ValueError, "some value in (%d,%d) is not valid" % (minIndex, maxIndex)
499 539
500 540 if (maxIndex >= self.dataOutObj.nHeights):
501 541 raise ValueError, "some value in (%d,%d) is not valid" % (minIndex, maxIndex)
502 542
503 543 nChannels = self.dataOutObj.nChannels
504 544 nPairs = self.dataOutObj.nPairs
505 545 nProfiles = self.dataOutObj.nProfiles
506 546 dataType = self.dataOutObj.dataType
507 547 nHeights = maxIndex - minIndex + 1
508 548 blockSize = 0
509 549
510 550 #self spectra
511 551 spc = self.dataOutObj.data_spc[:,:,minIndex:maxIndex+1]
512 552 blockSize += spc.size
513 553
514 554 #cross spectra
515 555 cspc = None
516 556 if self.dataOutObj.data_cspc != None:
517 557 cspc = self.dataOutObj.data_cspc[:,:,minIndex:maxIndex+1]
518 558 blockSize += cspc.size
519 559
520 560 #DC channel
521 561 dc = self.dataOutObj.data_dc[:,minIndex:maxIndex+1]
522 562 blockSize += dc.size
523 563
524 564 self.dataOutObj.data_spc = spc
525 565 if cspc != None:
526 566 self.dataOutObj.data_cspc = cspc
527 567 self.dataOutObj.data_dc = dc
528 568
529 569 firstHeight = self.dataOutObj.heightList[minIndex]
530 570
531 571 self.dataOutObj.nHeights = nHeights
532 572 self.dataOutObj.m_ProcessingHeader.blockSize = blockSize
533 573 self.dataOutObj.m_ProcessingHeader.numHeights = nHeights
534 574 self.dataOutObj.m_ProcessingHeader.firstHeight = firstHeight
535 575 self.dataOutObj.m_RadarControllerHeader.numHeights = nHeights
536 576
537 577 self.dataOutObj.heightList = self.dataOutObj.heightList[minIndex:maxIndex+1]
538 578
539 579
540 580 class IncoherentIntegration:
541 581
542 582 integ_counter = None
543 583 data = None
544 584 navg = None
545 585 buffer = None
546 586 nIncohInt = None
547 587
548 588 def __init__(self, N = None, timeInterval = None):
549 589 """
550 590 N
551 591 timeInterval - interval time [min], integer value
552 592 """
553 593
554 594 self.data = None
555 595 self.navg = None
556 596 self.buffer = None
557 597 self.timeOut = None
558 598 self.exitCondition = False
559 599 self.isReady = False
560 600 self.nIncohInt = N
561 601 self.integ_counter = 0
562 602 if timeInterval!=None:
563 603 self.timeIntervalInSeconds = timeInterval * 60. #if (type(timeInterval)!=integer) -> change this line
564 604
565 605 if ((timeInterval==None) and (N==None)):
566 606 print 'N = None ; timeInterval = None'
567 607 sys.exit(0)
568 608 elif timeInterval == None:
569 609 self.timeFlag = False
570 610 else:
571 611 self.timeFlag = True
572 612
573 613
574 614 def exe(self,data,timeOfData):
575 615 """
576 616 data
577 617
578 618 timeOfData [seconds]
579 619 """
580 620
581 621 if self.timeFlag:
582 622 if self.timeOut == None:
583 623 self.timeOut = timeOfData + self.timeIntervalInSeconds
584 624
585 625 if timeOfData < self.timeOut:
586 626 if self.buffer == None:
587 627 self.buffer = data
588 628 else:
589 629 self.buffer = self.buffer + data
590 630 self.integ_counter += 1
591 631 else:
592 632 self.exitCondition = True
593 633
594 634 else:
595 635 if self.integ_counter < self.nIncohInt:
596 636 if self.buffer == None:
597 637 self.buffer = data
598 638 else:
599 639 self.buffer = self.buffer + data
600 640
601 641 self.integ_counter += 1
602 642
603 643 if self.integ_counter == self.nIncohInt:
604 644 self.exitCondition = True
605 645
606 646 if self.exitCondition:
607 647 self.data = self.buffer
608 648 self.navg = self.integ_counter
609 649 self.isReady = True
610 650 self.buffer = None
611 651 self.timeOut = None
612 652 self.integ_counter = 0
613 653 self.exitCondition = False
614 654
615 655 if self.timeFlag:
616 656 self.buffer = data
617 657 self.timeOut = timeOfData + self.timeIntervalInSeconds
618 658 else:
619 659 self.isReady = False
620 660 No newline at end of file
@@ -1,591 +1,585
1 1 '''
2 2 Created on Feb 7, 2012
3 3
4 4 @author $Author$
5 5 @version $Id$
6 6 '''
7 7
8 8 import os, sys
9 9 import numpy
10 10
11 11 path = os.path.split(os.getcwd())[0]
12 12 sys.path.append(path)
13 13
14 14 from Model.Voltage import Voltage
15 15 from IO.VoltageIO import VoltageWriter
16 16 from Graphics.VoltagePlot import Osciloscope
17 17
18 18 class VoltageProcessor:
19 19 '''
20 20 classdocs
21 21 '''
22 22
23 23 dataInObj = None
24 24 dataOutObj = None
25
25
26 26 integratorObjIndex = None
27 27 decoderObjIndex = None
28 28 profSelectorObjIndex = None
29 29 writerObjIndex = None
30 30 plotterObjIndex = None
31 31 flipIndex = None
32 32
33 33 integratorObjList = []
34 34 decoderObjList = []
35 35 profileSelectorObjList = []
36 36 writerObjList = []
37 37 plotterObjList = []
38 38 m_Voltage= Voltage()
39 39
40 def __init__(self, dataInObj=None, dataOutObj=None):
40 def __init__(self):
41 41 '''
42 42 Constructor
43 43 '''
44
45 self.dataInObj = dataInObj
46
47 if dataOutObj == None:
48 self.dataOutObj = Voltage()
49 else:
50 self.dataOutObj = dataOutObj
51 44
52 45 self.integratorObjIndex = None
53 46 self.decoderObjIndex = None
54 47 self.profSelectorObjIndex = None
55 48 self.writerObjIndex = None
56 49 self.plotterObjIndex = None
57 50 self.flipIndex = 1
58 51 self.integratorObjList = []
59 52 self.decoderObjList = []
60 53 self.profileSelectorObjList = []
61 54 self.writerObjList = []
62 55 self.plotterObjList = []
63
64 def setIO(self,inputObject, outputObject):
65 56
66 if not( isinstance(inputObject, Voltage) ):
67 print 'InputObject must be an instance from Voltage()'
68 sys.exit(0)
57 def setup(self, dataInObj=None, dataOutObj=None):
69 58
70 if not( isinstance(outputObject, Voltage) ):
71 print 'OutputObject must be an instance from Voltage()'
72 sys.exit(0)
59 self.dataInObj = dataInObj
60
61 if dataOutObj == None:
62 dataOutObj = Voltage()
73 63
74 self.dataInObj = inputObject
75 self.dataOutObj = outputObject
64 dataOutObj.copy(dataInObj)
65
66 self.dataOutObj = dataOutObj
67
68 return self.dataOutObj
76 69
77 def setup(self):
78 pass
79 70
80 71 def init(self):
81 72
82 73 self.integratorObjIndex = 0
83 74 self.decoderObjIndex = 0
84 75 self.profSelectorObjIndex = 0
85 76 self.writerObjIndex = 0
86 77 self.plotterObjIndex = 0
87 78 self.dataOutObj.copy(self.dataInObj)
88 79
89 80 if self.profSelectorObjIndex != None:
90 81 for profSelObj in self.profileSelectorObjList:
91 82 profSelObj.incIndex()
92 83
93 84 def addWriter(self, wrpath):
94 85 objWriter = VoltageWriter(self.dataOutObj)
95 86 objWriter.setup(wrpath)
96 87 self.writerObjList.append(objWriter)
97 88
98 89 def addPlotter(self, index=None):
99 90 if index==None:
100 91 index = self.plotterObjIndex
101 92
102 93 plotObj = Osciloscope(self.dataOutObj, index)
103 94 self.plotterObjList.append(plotObj)
104 95
105 96 def addIntegrator(self, N,timeInterval):
106 97
107 98 objCohInt = CoherentIntegrator(N,timeInterval)
108 99 self.integratorObjList.append(objCohInt)
109 100
110 101 def addDecoder(self, code, ncode, nbaud):
111 102
112 103 objDecoder = Decoder(code,ncode,nbaud)
113 104 self.decoderObjList.append(objDecoder)
114 105
115 106 def addProfileSelector(self, nProfiles):
116 107
117 108 objProfSelector = ProfileSelector(nProfiles)
118 109 self.profileSelectorObjList.append(objProfSelector)
119 110
120 111 def writeData(self,wrpath):
121 112
122 113 if self.dataOutObj.flagNoData:
123 114 return 0
124 115
125 116 if len(self.writerObjList) <= self.writerObjIndex:
126 117 self.addWriter(wrpath)
127 118
128 119 self.writerObjList[self.writerObjIndex].putData()
129 120
130 121 # myWrObj = self.writerObjList[self.writerObjIndex]
131 122 # myWrObj.putData()
132 123
133 124 self.writerObjIndex += 1
134 125
135 126 def plotData(self,xmin=None, xmax=None, ymin=None, ymax=None, type='iq', winTitle='', index=None):
136 127 if self.dataOutObj.flagNoData:
137 128 return 0
138 129
139 130 if len(self.plotterObjList) <= self.plotterObjIndex:
140 131 self.addPlotter(index)
141 132
142 133 self.plotterObjList[self.plotterObjIndex].plotData(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,type=type, winTitle=winTitle)
143 134
144 135 self.plotterObjIndex += 1
145 136
146 137 def integrator(self, N=None, timeInterval=None):
147 138
148 139 if self.dataOutObj.flagNoData:
149 140 return 0
150 141
151 142 if len(self.integratorObjList) <= self.integratorObjIndex:
152 143 self.addIntegrator(N,timeInterval)
153 144
154 145 myCohIntObj = self.integratorObjList[self.integratorObjIndex]
155 146 myCohIntObj.exe(data=self.dataOutObj.data,timeOfData=self.dataOutObj.m_BasicHeader.utc)
156 147
157 148 if myCohIntObj.isReady:
158 149 self.dataOutObj.data = myCohIntObj.data
159 150 self.dataOutObj.nAvg = myCohIntObj.navg
160 151 self.dataOutObj.m_ProcessingHeader.coherentInt *= myCohIntObj.navg
161 152 #print "myCohIntObj.navg: ",myCohIntObj.navg
162 153 self.dataOutObj.flagNoData = False
163 154
164 155 else:
165 156 self.dataOutObj.flagNoData = True
166 157
167 158 self.integratorObjIndex += 1
168 159
169 160 def decoder(self,code=None,type = 0):
170 161
171 162 if self.dataOutObj.flagNoData:
172 163 return 0
173 164
174 165 if code == None:
175 166 code = self.dataOutObj.m_RadarControllerHeader.code
176 167 ncode, nbaud = code.shape
177 168
178 169 if len(self.decoderObjList) <= self.decoderObjIndex:
179 170 self.addDecoder(code,ncode,nbaud)
180 171
181 172 myDecodObj = self.decoderObjList[self.decoderObjIndex]
182 173 myDecodObj.exe(data=self.dataOutObj.data,type=type)
183 174
184 175 if myDecodObj.flag:
185 176 self.dataOutObj.data = myDecodObj.data
186 177 self.dataOutObj.flagNoData = False
187 178 else:
188 179 self.dataOutObj.flagNoData = True
189 180
190 181 self.decoderObjIndex += 1
191 182
192 183
193 184 def filterByHei(self, window):
194 185 if window == None:
195 186 window = self.dataOutObj.m_RadarControllerHeader.txA / self.dataOutObj.m_ProcessingHeader.deltaHeight[0]
196 187
197 188 newdelta = self.dataOutObj.m_ProcessingHeader.deltaHeight[0] * window
198 189 dim1 = self.dataOutObj.data.shape[0]
199 190 dim2 = self.dataOutObj.data.shape[1]
200 191 r = dim2 % window
201 192
202 193 buffer = self.dataOutObj.data[:,0:dim2-r]
203 194 buffer = buffer.reshape(dim1,dim2/window,window)
204 195 buffer = numpy.sum(buffer,2)
205 196 self.dataOutObj.data = buffer
206 197
207 198 self.dataOutObj.m_ProcessingHeader.deltaHeight = newdelta
208 199 self.dataOutObj.m_ProcessingHeader.numHeights = buffer.shape[1]
209 200
210 201 self.dataOutObj.nHeights = self.dataOutObj.m_ProcessingHeader.numHeights
211 202
212 203 #self.dataOutObj.heightList es un numpy.array
213 204 self.dataOutObj.heightList = numpy.arange(self.dataOutObj.m_ProcessingHeader.firstHeight[0],newdelta*self.dataOutObj.nHeights,newdelta)
214 205
215 206 def deFlip(self):
216 207 self.dataOutObj.data *= self.flipIndex
217 208 self.flipIndex *= -1.
218 209
219 210 def selectChannels(self, channelList):
211 pass
212
213 def selectChannelsByIndex(self, channelIndexList):
220 214 """
221 Selecciona un bloque de datos en base a canales segun el channelList
215 Selecciona un bloque de datos en base a canales segun el channelIndexList
222 216
223 217 Input:
224 channelList : lista sencilla de canales a seleccionar por ej. [2,3,7]
218 channelIndexList : lista sencilla de canales a seleccionar por ej. [2,3,7]
225 219
226 220 Affected:
227 221 self.dataOutObj.data
228 self.dataOutObj.channelList
222 self.dataOutObj.channelIndexList
229 223 self.dataOutObj.nChannels
230 224 self.dataOutObj.m_ProcessingHeader.totalSpectra
231 225 self.dataOutObj.m_SystemHeader.numChannels
232 226 self.dataOutObj.m_ProcessingHeader.blockSize
233 227
234 228 Return:
235 229 None
236 230 """
237 231 if self.dataOutObj.flagNoData:
238 232 return 0
239 233
240 for channel in channelList:
241 if channel not in self.dataOutObj.channelList:
242 raise ValueError, "The value %d in channelList is not valid" %channel
234 for channel in channelIndexList:
235 if channel not in self.dataOutObj.channelIndexList:
236 raise ValueError, "The value %d in channelIndexList is not valid" %channel
243 237
244 nChannels = len(channelList)
238 nChannels = len(channelIndexList)
245 239
246 data = self.dataOutObj.data[channelList,:]
240 data = self.dataOutObj.data[channelIndexList,:]
247 241
248 242 self.dataOutObj.data = data
249 self.dataOutObj.channelList = channelList
243 self.dataOutObj.channelIndexList = channelIndexList
250 244 self.dataOutObj.nChannels = nChannels
251 245
252 self.dataOutObj.m_ProcessingHeader.totalSpectra = nChannels
246 self.dataOutObj.m_ProcessingHeader.totalSpectra = 0
253 247 self.dataOutObj.m_SystemHeader.numChannels = nChannels
254 248 self.dataOutObj.m_ProcessingHeader.blockSize = data.size
255 249 return 1
256 250
257 251
258 252 def selectHeightsByValue(self, minHei, maxHei):
259 253 """
260 254 Selecciona un bloque de datos en base a un grupo de valores de alturas segun el rango
261 255 minHei <= height <= maxHei
262 256
263 257 Input:
264 258 minHei : valor minimo de altura a considerar
265 259 maxHei : valor maximo de altura a considerar
266 260
267 261 Affected:
268 262 Indirectamente son cambiados varios valores a travez del metodo selectHeightsByIndex
269 263
270 264 Return:
271 265 1 si el metodo se ejecuto con exito caso contrario devuelve 0
272 266 """
273 267 if self.dataOutObj.flagNoData:
274 268 return 0
275 269
276 270 if (minHei < self.dataOutObj.heightList[0]) or (minHei > maxHei):
277 271 raise ValueError, "some value in (%d,%d) is not valid" % (minHei, maxHei)
278 272
279 273 if (maxHei > self.dataOutObj.heightList[-1]):
280 274 raise ValueError, "some value in (%d,%d) is not valid" % (minHei, maxHei)
281 275
282 276 minIndex = 0
283 277 maxIndex = 0
284 278 data = self.dataOutObj.heightList
285 279
286 280 for i,val in enumerate(data):
287 281 if val < minHei:
288 282 continue
289 283 else:
290 284 minIndex = i;
291 285 break
292 286
293 287 for i,val in enumerate(data):
294 288 if val <= maxHei:
295 289 maxIndex = i;
296 290 else:
297 291 break
298 292
299 293 self.selectHeightsByIndex(minIndex, maxIndex)
300 294 return 1
301 295
302 296
303 297 def selectHeightsByIndex(self, minIndex, maxIndex):
304 298 """
305 299 Selecciona un bloque de datos en base a un grupo indices de alturas segun el rango
306 300 minIndex <= index <= maxIndex
307 301
308 302 Input:
309 303 minIndex : valor de indice minimo de altura a considerar
310 304 maxIndex : valor de indice maximo de altura a considerar
311 305
312 306 Affected:
313 307 self.dataOutObj.data
314 308 self.dataOutObj.heightList
315 309 self.dataOutObj.nHeights
316 310 self.dataOutObj.m_ProcessingHeader.blockSize
317 311 self.dataOutObj.m_ProcessingHeader.numHeights
318 312 self.dataOutObj.m_ProcessingHeader.firstHeight
319 313 self.dataOutObj.m_RadarControllerHeader
320 314
321 315 Return:
322 316 1 si el metodo se ejecuto con exito caso contrario devuelve 0
323 317 """
324 318 if self.dataOutObj.flagNoData:
325 319 return 0
326 320
327 321 if (minIndex < 0) or (minIndex > maxIndex):
328 322 raise ValueError, "some value in (%d,%d) is not valid" % (minIndex, maxIndex)
329 323
330 324 if (maxIndex >= self.dataOutObj.nHeights):
331 325 raise ValueError, "some value in (%d,%d) is not valid" % (minIndex, maxIndex)
332 326
333 327 nHeights = maxIndex - minIndex + 1
334 328
335 329 #voltage
336 330 data = self.dataOutObj.data[:,minIndex:maxIndex+1]
337 331
338 332 firstHeight = self.dataOutObj.heightList[minIndex]
339 333
340 334 self.dataOutObj.data = data
341 335 self.dataOutObj.heightList = self.dataOutObj.heightList[minIndex:maxIndex+1]
342 336 self.dataOutObj.nHeights = nHeights
343 337 self.dataOutObj.m_ProcessingHeader.blockSize = data.size
344 338 self.dataOutObj.m_ProcessingHeader.numHeights = nHeights
345 339 self.dataOutObj.m_ProcessingHeader.firstHeight = firstHeight
346 340 self.dataOutObj.m_RadarControllerHeader.numHeights = nHeights
347 341 return 1
348 342
349 343 def selectProfilesByValue(self,indexList, nProfiles):
350 344 if self.dataOutObj.flagNoData:
351 345 return 0
352 346
353 347 if self.profSelectorObjIndex >= len(self.profileSelectorObjList):
354 348 self.addProfileSelector(nProfiles)
355 349
356 350 profileSelectorObj = self.profileSelectorObjList[self.profSelectorObjIndex]
357 351
358 352 if not(profileSelectorObj.isProfileInList(indexList)):
359 353 self.dataOutObj.flagNoData = True
360 354 self.profSelectorObjIndex += 1
361 355 return 0
362 356
363 357 self.dataOutObj.flagNoData = False
364 358 self.profSelectorObjIndex += 1
365 359
366 360 return 1
367 361
368 362
369 363 def selectProfilesByIndex(self, minIndex, maxIndex, nProfiles):
370 364 """
371 365 Selecciona un bloque de datos en base a un grupo indices de perfiles segun el rango
372 366 minIndex <= index <= maxIndex
373 367
374 368 Input:
375 369 minIndex : valor de indice minimo de perfil a considerar
376 370 maxIndex : valor de indice maximo de perfil a considerar
377 371 nProfiles : numero de profiles
378 372
379 373 Affected:
380 374 self.dataOutObj.flagNoData
381 375 self.profSelectorObjIndex
382 376
383 377 Return:
384 378 1 si el metodo se ejecuto con exito caso contrario devuelve 0
385 379 """
386 380
387 381 if self.dataOutObj.flagNoData:
388 382 return 0
389 383
390 384 if self.profSelectorObjIndex >= len(self.profileSelectorObjList):
391 385 self.addProfileSelector(nProfiles)
392 386
393 387 profileSelectorObj = self.profileSelectorObjList[self.profSelectorObjIndex]
394 388
395 389 if not(profileSelectorObj.isProfileInRange(minIndex, maxIndex)):
396 390 self.dataOutObj.flagNoData = True
397 391 self.profSelectorObjIndex += 1
398 392 return 0
399 393
400 394 self.dataOutObj.flagNoData = False
401 395 self.profSelectorObjIndex += 1
402 396
403 397 return 1
404 398
405 399 def selectNtxs(self, ntx):
406 400 pass
407 401
408 402
409 403 class Decoder:
410 404
411 405 data = None
412 406 profCounter = 1
413 407 nCode = None
414 408 nBaud = None
415 409 codeIndex = 0
416 410 code = None
417 411 flag = False
418 412
419 413 def __init__(self,code, ncode, nbaud):
420 414
421 415 self.data = None
422 416 self.profCounter = 1
423 417 self.nCode = ncode
424 418 self.nBaud = nbaud
425 419 self.codeIndex = 0
426 420 self.code = code #this is a List
427 421 self.flag = False
428 422
429 423 def exe(self, data, ndata=None, type = 0):
430 424
431 425 if ndata == None: ndata = data.shape[1]
432 426
433 427 if type == 0:
434 428 self.convolutionInFreq(data,ndata)
435 429
436 430 if type == 1:
437 431 self.convolutionInTime(data, ndata)
438 432
439 433 def convolutionInFreq(self,data, ndata):
440 434
441 435 newcode = numpy.zeros(ndata)
442 436 newcode[0:self.nBaud] = self.code[self.codeIndex]
443 437
444 438 self.codeIndex += 1
445 439
446 440 fft_data = numpy.fft.fft(data, axis=1)
447 441 fft_code = numpy.conj(numpy.fft.fft(newcode))
448 442 fft_code = fft_code.reshape(1,len(fft_code))
449 443
450 444 conv = fft_data.copy()
451 445 conv.fill(0)
452 446
453 447 conv = fft_data*fft_code
454 448
455 449 self.data = numpy.fft.ifft(conv,axis=1)
456 450 self.flag = True
457 451
458 452 if self.profCounter == self.nCode:
459 453 self.profCounter = 0
460 454 self.codeIndex = 0
461 455
462 456 self.profCounter += 1
463 457
464 458 def convolutionInTime(self, data, ndata):
465 459
466 460 nchannel = data.shape[1]
467 461 newcode = self.code[self.codeIndex]
468 462 self.codeIndex += 1
469 463 conv = data.copy()
470 464 for i in range(nchannel):
471 465 conv[i,:] = numpy.correlate(data[i,:], newcode, 'same')
472 466
473 467 self.data = conv
474 468 self.flag = True
475 469
476 470 if self.profCounter == self.nCode:
477 471 self.profCounter = 0
478 472 self.codeIndex = 0
479 473
480 474 self.profCounter += 1
481 475
482 476
483 477 class CoherentIntegrator:
484 478
485 479 integ_counter = None
486 480 data = None
487 481 navg = None
488 482 buffer = None
489 483 nCohInt = None
490 484
491 485 def __init__(self, N=None,timeInterval=None):
492 486
493 487 self.data = None
494 488 self.navg = None
495 489 self.buffer = None
496 490 self.timeOut = None
497 491 self.exitCondition = False
498 492 self.isReady = False
499 493 self.nCohInt = N
500 494 self.integ_counter = 0
501 495 if timeInterval!=None:
502 496 self.timeIntervalInSeconds = timeInterval * 60. #if (type(timeInterval)!=integer) -> change this line
503 497
504 498 if ((timeInterval==None) and (N==None)):
505 499 print 'N = None ; timeInterval = None'
506 500 sys.exit(0)
507 501 elif timeInterval == None:
508 502 self.timeFlag = False
509 503 else:
510 504 self.timeFlag = True
511 505
512 506 def exe(self, data, timeOfData):
513 507
514 508 if self.timeFlag:
515 509 if self.timeOut == None:
516 510 self.timeOut = timeOfData + self.timeIntervalInSeconds
517 511
518 512 if timeOfData < self.timeOut:
519 513 if self.buffer == None:
520 514 self.buffer = data
521 515 else:
522 516 self.buffer = self.buffer + data
523 517 self.integ_counter += 1
524 518 else:
525 519 self.exitCondition = True
526 520
527 521 else:
528 522 if self.integ_counter < self.nCohInt:
529 523 if self.buffer == None:
530 524 self.buffer = data
531 525 else:
532 526 self.buffer = self.buffer + data
533 527
534 528 self.integ_counter += 1
535 529
536 530 if self.integ_counter == self.nCohInt:
537 531 self.exitCondition = True
538 532
539 533 if self.exitCondition:
540 534 self.data = self.buffer
541 535 self.navg = self.integ_counter
542 536 self.isReady = True
543 537 self.buffer = None
544 538 self.timeOut = None
545 539 self.integ_counter = 0
546 540 self.exitCondition = False
547 541
548 542 if self.timeFlag:
549 543 self.buffer = data
550 544 self.timeOut = timeOfData + self.timeIntervalInSeconds
551 545 else:
552 546 self.isReady = False
553 547
554 548
555 549
556 550 class ProfileSelector:
557 551
558 552 profileIndex = None
559 553 # Tamanho total de los perfiles
560 554 nProfiles = None
561 555
562 556 def __init__(self, nProfiles):
563 557
564 558 self.profileIndex = 0
565 559 self.nProfiles = nProfiles
566 560
567 561 def incIndex(self):
568 562 self.profileIndex += 1
569 563
570 564 if self.profileIndex >= self.nProfiles:
571 565 self.profileIndex = 0
572 566
573 567 def isProfileInRange(self, minIndex, maxIndex):
574 568
575 569 if self.profileIndex < minIndex:
576 570 return False
577 571
578 572 if self.profileIndex > maxIndex:
579 573 return False
580 574
581 575 return True
582 576
583 577 def isProfileInList(self, profileList):
584 578
585 579 if self.profileIndex not in profileList:
586 580 return False
587 581
588 582 return True
589 583
590 584
591 585 No newline at end of file
@@ -1,91 +1,109
1 1 '''
2 2 Created on 27/03/2012
3 3
4 4 @author $Author$
5 5 @version $Id$
6 6 '''
7 7 import os, sys
8 8 import time, datetime
9 9
10 10 from Model.Voltage import Voltage
11 11 from IO.VoltageIO import *
12 12 #from Graphics.VoltagePlot import Osciloscope
13 13
14 14 from Model.Spectra import Spectra
15 15 from IO.SpectraIO import *
16 16
17 17 from Processing.VoltageProcessor import *
18 18 from Processing.SpectraProcessor import *
19 19
20 20
21 21 class TestSChain():
22 22
23 23 def __init__(self):
24 24 self.setValues()
25 25 self.createObjects()
26 26 self.testSChain()
27 27
28 28
29 29 def setValues( self ):
30 30
31 31 self.path = "/home/dsuarez/Projects" #1
32 32 self.path = "/Users/jro/Documents/RadarData/EW_Drifts"
33 # self.path = "/Users/jro/Documents/RadarData/JULIA"
33 self.path = "/Users/jro/Documents/RadarData/MST_ISR/MST"
34 34 # self.startDateTime = datetime.datetime(2007,5,1,15,49,0)
35 35 # self.endDateTime = datetime.datetime(2007,5,1,23,0,0)
36 36
37 self.startDateTime = datetime.datetime(2011,10,1,0,0,0)
38 self.endDateTime = datetime.datetime(2011,12,31,0,20,0)
37 self.startDateTime = datetime.datetime(2009,01,1,0,0,0)
38 self.endDateTime = datetime.datetime(2009,01,31,0,20,0)
39
40 # self.startDateTime = datetime.datetime(2011,11,1,0,0,0)
41 # self.endDateTime = datetime.datetime(2011,12,31,0,20,0)
42
43
39 44 self.N = 4
40 45 self.npts = 8
41 46
42 def createObjects( self ):
47 def createObjects( self ):
43 48
44 self.voltObj1 = Voltage()
45 self.voltObj2 = Voltage()
46 self.specObj1 = Spectra()
47
48 self.readerObj = VoltageReader(self.voltObj1)
49 self.voltProcObj = VoltageProcessor(self.voltObj1, self.voltObj2)
50 self.specProcObj = SpectraProcessor(self.voltObj2, self.specObj1)
49 self.readerObj = VoltageReader()
50 self.voltProcObj = VoltageProcessor()
51 self.specProcObj = SpectraProcessor()
51 52
52 if not(self.readerObj.setup( self.path, self.startDateTime, self.endDateTime, expLabel='', online =0) ):
53 voltObj1 = self.readerObj.setup(
54 path = self.path,
55 startDateTime = self.startDateTime,
56 endDateTime = self.endDateTime,
57 expLabel = '',
58 online = 0)
59
60 if not(voltObj1):
53 61 sys.exit(0)
54 62
55 self.specProcObj.setup(nFFTPoints=8)
63 voltObj2 = self.voltProcObj.setup(dataInObj = voltObj1)
64
65 specObj1 = self.specProcObj.setup(dataInObj = voltObj2,
66 nFFTPoints = 16)
67
68 # voltObj2 = self.voltProcObj.setup(dataInObj = voltObj1,
69 # dataOutObj = voltObj2)
70 #
71 # specObj1 = self.specProcObj.setup(dataInObj = voltObj2,
72 # dataOutObj =specObj1,
73 # nFFTPoints=16)
56 74
57 75
58 76 def testSChain( self ):
59 77
60 78 ini = time.time()
61 79 while(True):
62 80 self.readerObj.getData()
63 81
64 82 self.voltProcObj.init()
65 83
66 self.voltProcObj.plotData(winTitle='VOLTAGE INPUT', index=1)
67
68 self.voltProcObj.integrator(4)
69
70 self.voltProcObj.plotData(winTitle='VOLTAGE AVG', index=2)
84 # self.voltProcObj.plotData(winTitle='VOLTAGE INPUT', index=1)
85 #
86 # self.voltProcObj.integrator(4)
87 #
88 # self.voltProcObj.plotData(winTitle='VOLTAGE AVG', index=2)
89 #
71 90
72
73 91 self.specProcObj.init()
74 92
75 self.specProcObj.integrator(N=2)
93 self.specProcObj.integrator(N=1)
76 94
77 self.specProcObj.plotData(winTitle='Spectra 1', index=3)
95 self.specProcObj.plotData(winTitle='Spectra 1', index=1)
78 96
79 97
80 98 if self.readerObj.flagNoMoreFiles:
81 99 break
82 100
83 101 if self.readerObj.flagIsNewBlock:
84 102 print 'Block No %04d, Time: %s' %(self.readerObj.nTotalBlocks,
85 103 datetime.datetime.fromtimestamp(self.readerObj.m_BasicHeader.utc),)
86 104
87 105
88 106 # self.plotObj.end()
89 107
90 108 if __name__ == '__main__':
91 109 TestSChain() No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now