##// END OF EJS Templates
avance spectros en alturas
Alexander Valdez -
r163:dc0275970b25
parent child
Show More
@@ -1,781 +1,905
1 1 '''
2 2
3 3 $Author$
4 4 $Id$
5 5 '''
6 6
7 7 import os, sys
8 8 import numpy
9
10 import pyfits
9 11 import glob
10 12 import fnmatch
11 13 import time, datetime
12 14
13 15 path = os.path.split(os.getcwd())[0]
14 16 sys.path.append(path)
15 17
16 18 from JROHeaderIO import *
17 19 from JRODataIO import JRODataReader
18 20 from JRODataIO import JRODataWriter
19 21
20 22 from Data.JROData import Spectra
21 23
24 from Data.JROData import SpectraHeis
25
22 26 class SpectraReader(JRODataReader):
23 27 """
24 28 Esta clase permite leer datos de espectros desde archivos procesados (.pdata). La lectura
25 29 de los datos siempre se realiza por bloques. Los datos leidos (array de 3 dimensiones)
26 30 son almacenados en tres buffer's para el Self Spectra, el Cross Spectra y el DC Channel.
27 31
28 32 paresCanalesIguales * alturas * perfiles (Self Spectra)
29 33 paresCanalesDiferentes * alturas * perfiles (Cross Spectra)
30 34 canales * alturas (DC Channels)
31 35
32 36 Esta clase contiene instancias (objetos) de las clases BasicHeader, SystemHeader,
33 37 RadarControllerHeader y Spectra. Los tres primeros se usan para almacenar informacion de la
34 38 cabecera de datos (metadata), y el cuarto (Spectra) para obtener y almacenar un bloque de
35 39 datos desde el "buffer" cada vez que se ejecute el metodo "getData".
36 40
37 41 Example:
38 42 dpath = "/home/myuser/data"
39 43
40 44 startTime = datetime.datetime(2010,1,20,0,0,0,0,0,0)
41 45
42 46 endTime = datetime.datetime(2010,1,21,23,59,59,0,0,0)
43 47
44 48 readerObj = SpectraReader()
45 49
46 50 readerObj.setup(dpath, startTime, endTime)
47 51
48 52 while(True):
49 53
50 54 readerObj.getData()
51 55
52 56 print readerObj.data_spc
53 57
54 58 print readerObj.data_cspc
55 59
56 60 print readerObj.data_dc
57 61
58 62 if readerObj.flagNoMoreFiles:
59 63 break
60 64
61 65 """
62 66
63 67 pts2read_SelfSpectra = 0
64 68
65 69 pts2read_CrossSpectra = 0
66 70
67 71 pts2read_DCchannels = 0
68 72
69 73 ext = ".pdata"
70 74
71 75 optchar = "P"
72 76
73 77 dataOutObj = None
74 78
75 79 nRdChannels = None
76 80
77 81 nRdPairs = None
78 82
79 83 rdPairList = []
80 84
81 85
82 86 def __init__(self, dataOutObj=None):
83 87 """
84 88 Inicializador de la clase SpectraReader para la lectura de datos de espectros.
85 89
86 90 Inputs:
87 91 dataOutObj : Objeto de la clase Spectra. Este objeto sera utilizado para
88 92 almacenar un perfil de datos cada vez que se haga un requerimiento
89 93 (getData). El perfil sera obtenido a partir del buffer de datos,
90 94 si el buffer esta vacio se hara un nuevo proceso de lectura de un
91 95 bloque de datos.
92 96 Si este parametro no es pasado se creara uno internamente.
93 97
94 98 Affected:
95 99 self.dataOutObj
96 100
97 101 Return : None
98 102 """
99 103
100 104 self.pts2read_SelfSpectra = 0
101 105
102 106 self.pts2read_CrossSpectra = 0
103 107
104 108 self.pts2read_DCchannels = 0
105 109
106 110 self.datablock = None
107 111
108 112 self.utc = None
109 113
110 114 self.ext = ".pdata"
111 115
112 116 self.optchar = "P"
113 117
114 118 self.basicHeaderObj = BasicHeader()
115 119
116 120 self.systemHeaderObj = SystemHeader()
117 121
118 122 self.radarControllerHeaderObj = RadarControllerHeader()
119 123
120 124 self.processingHeaderObj = ProcessingHeader()
121 125
122 126 self.online = 0
123 127
124 128 self.fp = None
125 129
126 130 self.idFile = None
127 131
128 132 self.dtype = None
129 133
130 134 self.fileSizeByHeader = None
131 135
132 136 self.filenameList = []
133 137
134 138 self.filename = None
135 139
136 140 self.fileSize = None
137 141
138 142 self.firstHeaderSize = 0
139 143
140 144 self.basicHeaderSize = 24
141 145
142 146 self.pathList = []
143 147
144 148 self.lastUTTime = 0
145 149
146 150 self.maxTimeStep = 30
147 151
148 152 self.flagNoMoreFiles = 0
149 153
150 154 self.set = 0
151 155
152 156 self.path = None
153 157
154 158 self.delay = 3 #seconds
155 159
156 160 self.nTries = 3 #quantity tries
157 161
158 162 self.nFiles = 3 #number of files for searching
159 163
160 164 self.nReadBlocks = 0
161 165
162 166 self.flagIsNewFile = 1
163 167
164 168 self.ippSeconds = 0
165 169
166 170 self.flagTimeBlock = 0
167 171
168 172 self.flagIsNewBlock = 0
169 173
170 174 self.nTotalBlocks = 0
171 175
172 176 self.blocksize = 0
173 177
174 178
175 179 def createObjByDefault(self):
176 180
177 181 dataObj = Spectra()
178 182
179 183 return dataObj
180 184
181 185 def __hasNotDataInBuffer(self):
182 186 return 1
183 187
184 188
185 189 def getBlockDimension(self):
186 190 """
187 191 Obtiene la cantidad de puntos a leer por cada bloque de datos
188 192
189 193 Affected:
190 194 self.nRdChannels
191 195 self.nRdPairs
192 196 self.pts2read_SelfSpectra
193 197 self.pts2read_CrossSpectra
194 198 self.pts2read_DCchannels
195 199 self.blocksize
196 200 self.dataOutObj.nChannels
197 201 self.dataOutObj.nPairs
198 202
199 203 Return:
200 204 None
201 205 """
202 206 self.nRdChannels = 0
203 207 self.nRdPairs = 0
204 208 self.rdPairList = []
205 209
206 210 for i in range(0, self.processingHeaderObj.totalSpectra*2, 2):
207 211 if self.processingHeaderObj.spectraComb[i] == self.processingHeaderObj.spectraComb[i+1]:
208 212 self.nRdChannels = self.nRdChannels + 1 #par de canales iguales
209 213 else:
210 214 self.nRdPairs = self.nRdPairs + 1 #par de canales diferentes
211 215 self.rdPairList.append((self.processingHeaderObj.spectraComb[i], self.processingHeaderObj.spectraComb[i+1]))
212 216
213 217 pts2read = self.processingHeaderObj.nHeights * self.processingHeaderObj.profilesPerBlock
214 218
215 219 self.pts2read_SelfSpectra = int(self.nRdChannels * pts2read)
216 220 self.blocksize = self.pts2read_SelfSpectra
217 221
218 222 if self.processingHeaderObj.flag_cspc:
219 223 self.pts2read_CrossSpectra = int(self.nRdPairs * pts2read)
220 224 self.blocksize += self.pts2read_CrossSpectra
221 225
222 226 if self.processingHeaderObj.flag_dc:
223 227 self.pts2read_DCchannels = int(self.systemHeaderObj.nChannels * self.processingHeaderObj.nHeights)
224 228 self.blocksize += self.pts2read_DCchannels
225 229
226 230 # self.blocksize = self.pts2read_SelfSpectra + self.pts2read_CrossSpectra + self.pts2read_DCchannels
227 231
228 232
229 233 def readBlock(self):
230 234 """
231 235 Lee el bloque de datos desde la posicion actual del puntero del archivo
232 236 (self.fp) y actualiza todos los parametros relacionados al bloque de datos
233 237 (metadata + data). La data leida es almacenada en el buffer y el contador del buffer
234 238 es seteado a 0
235 239
236 240 Return: None
237 241
238 242 Variables afectadas:
239 243
240 244 self.flagIsNewFile
241 245 self.flagIsNewBlock
242 246 self.nTotalBlocks
243 247 self.data_spc
244 248 self.data_cspc
245 249 self.data_dc
246 250
247 251 Exceptions:
248 252 Si un bloque leido no es un bloque valido
249 253 """
250 254 blockOk_flag = False
251 255 fpointer = self.fp.tell()
252 256
253 257 spc = numpy.fromfile( self.fp, self.dtype[0], self.pts2read_SelfSpectra )
254 258 spc = spc.reshape( (self.nRdChannels, self.processingHeaderObj.nHeights, self.processingHeaderObj.profilesPerBlock) ) #transforma a un arreglo 3D
255 259
256 260 if self.processingHeaderObj.flag_cspc:
257 261 cspc = numpy.fromfile( self.fp, self.dtype, self.pts2read_CrossSpectra )
258 262 cspc = cspc.reshape( (self.nRdPairs, self.processingHeaderObj.nHeights, self.processingHeaderObj.profilesPerBlock) ) #transforma a un arreglo 3D
259 263
260 264 if self.processingHeaderObj.flag_dc:
261 265 dc = numpy.fromfile( self.fp, self.dtype, self.pts2read_DCchannels ) #int(self.processingHeaderObj.nHeights*self.systemHeaderObj.nChannels) )
262 266 dc = dc.reshape( (self.systemHeaderObj.nChannels, self.processingHeaderObj.nHeights) ) #transforma a un arreglo 2D
263 267
264 268
265 269 if not(self.processingHeaderObj.shif_fft):
266 270 spc = numpy.roll( spc, self.processingHeaderObj.profilesPerBlock/2, axis=2 ) #desplaza a la derecha en el eje 2 determinadas posiciones
267 271
268 272 if self.processingHeaderObj.flag_cspc:
269 273 cspc = numpy.roll( cspc, self.processingHeaderObj.profilesPerBlock/2, axis=2 ) #desplaza a la derecha en el eje 2 determinadas posiciones
270 274
271 275
272 276 spc = numpy.transpose( spc, (0,2,1) )
273 277 self.data_spc = spc
274 278
275 279 if self.processingHeaderObj.flag_cspc:
276 280 cspc = numpy.transpose( cspc, (0,2,1) )
277 281 self.data_cspc = cspc['real'] + cspc['imag']*1j
278 282 else:
279 283 self.data_cspc = None
280 284
281 285 if self.processingHeaderObj.flag_dc:
282 286 self.data_dc = dc['real'] + dc['imag']*1j
283 287 else:
284 288 self.data_dc = None
285 289
286 290 self.flagIsNewFile = 0
287 291 self.flagIsNewBlock = 1
288 292
289 293 self.nTotalBlocks += 1
290 294 self.nReadBlocks += 1
291 295
292 296 return 1
293 297
294 298
295 299 def getData(self):
296 300 """
297 301 Copia el buffer de lectura a la clase "Spectra",
298 302 con todos los parametros asociados a este (metadata). cuando no hay datos en el buffer de
299 303 lectura es necesario hacer una nueva lectura de los bloques de datos usando "readNextBlock"
300 304
301 305 Return:
302 306 0 : Si no hay mas archivos disponibles
303 307 1 : Si hizo una buena copia del buffer
304 308
305 309 Affected:
306 310 self.dataOutObj
307 311
308 312 self.flagTimeBlock
309 313 self.flagIsNewBlock
310 314 """
311 315
312 316 if self.flagNoMoreFiles: return 0
313 317
314 318 self.flagTimeBlock = 0
315 319 self.flagIsNewBlock = 0
316 320
317 321 if self.__hasNotDataInBuffer():
318 322
319 323 if not( self.readNextBlock() ):
320 324 return 0
321 325
322 326 # self.updateDataHeader()
323 327
324 328 if self.flagNoMoreFiles == 1:
325 329 print 'Process finished'
326 330 return 0
327 331
328 332 #data es un numpy array de 3 dmensiones (perfiles, alturas y canales)
329 333
330 334 if self.data_dc == None:
331 335 self.dataOutObj.flagNoData = True
332 336 return 0
333 337
334 338
335 339 self.dataOutObj.data_spc = self.data_spc
336 340
337 341 self.dataOutObj.data_cspc = self.data_cspc
338 342
339 343 self.dataOutObj.data_dc = self.data_dc
340 344
341 345 self.dataOutObj.flagTimeBlock = self.flagTimeBlock
342 346
343 347 self.dataOutObj.flagNoData = False
344 348
345 349 self.dataOutObj.dtype = self.dtype
346 350
347 351 self.dataOutObj.nChannels = self.nRdChannels
348 352
349 353 self.dataOutObj.nPairs = self.nRdPairs
350 354
351 355 self.dataOutObj.pairsList = self.rdPairList
352 356
353 357 self.dataOutObj.nHeights = self.processingHeaderObj.nHeights
354 358
355 359 self.dataOutObj.nProfiles = self.processingHeaderObj.profilesPerBlock
356 360
357 361 self.dataOutObj.nFFTPoints = self.processingHeaderObj.profilesPerBlock
358 362
359 363 self.dataOutObj.nIncohInt = self.processingHeaderObj.nIncohInt
360 364
361 365
362 366 xf = self.processingHeaderObj.firstHeight + self.processingHeaderObj.nHeights*self.processingHeaderObj.deltaHeight
363 367
364 368 self.dataOutObj.heightList = numpy.arange(self.processingHeaderObj.firstHeight, xf, self.processingHeaderObj.deltaHeight)
365 369
366 370 self.dataOutObj.channelList = range(self.systemHeaderObj.nChannels)
367 371
368 372 self.dataOutObj.channelIndexList = range(self.systemHeaderObj.nChannels)
369 373
370 374 self.dataOutObj.utctime = self.basicHeaderObj.utc + self.basicHeaderObj.miliSecond/1000.#+ self.profileIndex * self.ippSeconds
371 375
372 376 self.dataOutObj.ippSeconds = self.ippSeconds
373 377
374 378 self.dataOutObj.timeInterval = self.ippSeconds * self.processingHeaderObj.nCohInt * self.processingHeaderObj.nIncohInt * self.dataOutObj.nFFTPoints
375 379
376 380 self.dataOutObj.flagShiftFFT = self.processingHeaderObj.shif_fft
377 381
378 382 # self.profileIndex += 1
379 383
380 384 self.dataOutObj.systemHeaderObj = self.systemHeaderObj.copy()
381 385
382 386 self.dataOutObj.radarControllerHeaderObj = self.radarControllerHeaderObj.copy()
383 387
384 388 return self.dataOutObj.data_spc
385 389
386 390
387 391 class SpectraWriter(JRODataWriter):
388 392
389 393 """
390 394 Esta clase permite escribir datos de espectros a archivos procesados (.pdata). La escritura
391 395 de los datos siempre se realiza por bloques.
392 396 """
393 397
394 398 ext = ".pdata"
395 399
396 400 optchar = "P"
397 401
398 402 shape_spc_Buffer = None
399 403
400 404 shape_cspc_Buffer = None
401 405
402 406 shape_dc_Buffer = None
403 407
404 408 data_spc = None
405 409
406 410 data_cspc = None
407 411
408 412 data_dc = None
409 413
410 414 wrPairList = []
411 415
412 416 nWrPairs = 0
413 417
414 418 nWrChannels = 0
415 419
416 420 # dataOutObj = None
417 421
418 422 def __init__(self, dataOutObj=None):
419 423 """
420 424 Inicializador de la clase SpectraWriter para la escritura de datos de espectros.
421 425
422 426 Affected:
423 427 self.dataOutObj
424 428 self.basicHeaderObj
425 429 self.systemHeaderObj
426 430 self.radarControllerHeaderObj
427 431 self.processingHeaderObj
428 432
429 433 Return: None
430 434 """
431 435 if dataOutObj == None:
432 436 dataOutObj = Spectra()
433 437
434 438 if not( isinstance(dataOutObj, Spectra) ):
435 439 raise ValueError, "in SpectraReader, dataOutObj must be an Spectra class object"
436 440
437 441 self.dataOutObj = dataOutObj
438 442
439 443 self.nTotalBlocks = 0
440 444
441 445 self.nWrChannels = self.dataOutObj.nChannels
442 446
443 447 # if len(pairList) > 0:
444 448 # self.wrPairList = pairList
445 449 #
446 450 # self.nWrPairs = len(pairList)
447 451
448 452 self.wrPairList = self.dataOutObj.pairsList
449 453
450 454 self.nWrPairs = self.dataOutObj.nPairs
451 455
452
453
454
455
456 456 # self.data_spc = None
457 457 # self.data_cspc = None
458 458 # self.data_dc = None
459 459
460 460 # self.fp = None
461 461
462 462 # self.flagIsNewFile = 1
463 463 #
464 464 # self.nTotalBlocks = 0
465 465 #
466 466 # self.flagIsNewBlock = 0
467 467 #
468 468 # self.flagNoMoreFiles = 0
469 469 #
470 470 # self.setFile = None
471 471 #
472 472 # self.dtype = None
473 473 #
474 474 # self.path = None
475 475 #
476 476 # self.noMoreFiles = 0
477 477 #
478 478 # self.filename = None
479 479 #
480 480 # self.basicHeaderObj = BasicHeader()
481 481 #
482 482 # self.systemHeaderObj = SystemHeader()
483 483 #
484 484 # self.radarControllerHeaderObj = RadarControllerHeader()
485 485 #
486 486 # self.processingHeaderObj = ProcessingHeader()
487 487
488 488
489 489 def hasAllDataInBuffer(self):
490 490 return 1
491 491
492 492
493 493 def setBlockDimension(self):
494 494 """
495 495 Obtiene las formas dimensionales del los subbloques de datos que componen un bloque
496 496
497 497 Affected:
498 498 self.shape_spc_Buffer
499 499 self.shape_cspc_Buffer
500 500 self.shape_dc_Buffer
501 501
502 502 Return: None
503 503 """
504 504 self.shape_spc_Buffer = (self.dataOutObj.nChannels,
505 505 self.processingHeaderObj.nHeights,
506 506 self.processingHeaderObj.profilesPerBlock)
507 507
508 508 self.shape_cspc_Buffer = (self.dataOutObj.nPairs,
509 509 self.processingHeaderObj.nHeights,
510 510 self.processingHeaderObj.profilesPerBlock)
511 511
512 512 self.shape_dc_Buffer = (self.dataOutObj.nChannels,
513 513 self.processingHeaderObj.nHeights)
514 514
515 515
516 516 def writeBlock(self):
517 517 """
518 518 Escribe el buffer en el file designado
519 519
520 520 Affected:
521 521 self.data_spc
522 522 self.data_cspc
523 523 self.data_dc
524 524 self.flagIsNewFile
525 525 self.flagIsNewBlock
526 526 self.nTotalBlocks
527 527 self.nWriteBlocks
528 528
529 529 Return: None
530 530 """
531 531
532 532 spc = numpy.transpose( self.data_spc, (0,2,1) )
533 533 if not( self.processingHeaderObj.shif_fft ):
534 534 spc = numpy.roll( spc, self.processingHeaderObj.profilesPerBlock/2, axis=2 ) #desplaza a la derecha en el eje 2 determinadas posiciones
535 535 data = spc.reshape((-1))
536 536 data.tofile(self.fp)
537 537
538 538 if self.data_cspc != None:
539 539 data = numpy.zeros( self.shape_cspc_Buffer, self.dtype )
540 540 cspc = numpy.transpose( self.data_cspc, (0,2,1) )
541 541 if not( self.processingHeaderObj.shif_fft ):
542 542 cspc = numpy.roll( cspc, self.processingHeaderObj.profilesPerBlock/2, axis=2 ) #desplaza a la derecha en el eje 2 determinadas posiciones
543 543 data['real'] = cspc.real
544 544 data['imag'] = cspc.imag
545 545 data = data.reshape((-1))
546 546 data.tofile(self.fp)
547 547
548 548 if self.data_dc != None:
549 549 data = numpy.zeros( self.shape_dc_Buffer, self.dtype )
550 550 dc = self.data_dc
551 551 data['real'] = dc.real
552 552 data['imag'] = dc.imag
553 553 data = data.reshape((-1))
554 554 data.tofile(self.fp)
555 555
556 556 self.data_spc.fill(0)
557 557 self.data_dc.fill(0)
558 558 if self.data_cspc != None:
559 559 self.data_cspc.fill(0)
560 560
561 561 self.flagIsNewFile = 0
562 562 self.flagIsNewBlock = 1
563 563 self.nTotalBlocks += 1
564 564 self.nWriteBlocks += 1
565 565 self.blockIndex += 1
566 566
567 567
568 568 def putData(self):
569 569 """
570 570 Setea un bloque de datos y luego los escribe en un file
571 571
572 572 Affected:
573 573 self.data_spc
574 574 self.data_cspc
575 575 self.data_dc
576 576
577 577 Return:
578 578 0 : Si no hay data o no hay mas files que puedan escribirse
579 579 1 : Si se escribio la data de un bloque en un file
580 580 """
581 581 self.flagIsNewBlock = 0
582 582
583 583 if self.dataOutObj.flagNoData:
584 584 return 0
585 585
586 586 if self.dataOutObj.flagTimeBlock:
587 587 self.data_spc.fill(0)
588 588 self.data_cspc.fill(0)
589 589 self.data_dc.fill(0)
590 590 self.setNextFile()
591 591
592 592 if self.flagIsNewFile == 0:
593 593 self.getBasicHeader()
594 594
595 595 self.data_spc = self.dataOutObj.data_spc
596 596 self.data_cspc = self.dataOutObj.data_cspc
597 597 self.data_dc = self.dataOutObj.data_dc
598 598
599 599 # #self.processingHeaderObj.dataBlocksPerFile)
600 600 if self.hasAllDataInBuffer():
601 601 # self.getDataHeader()
602 602 self.writeNextBlock()
603 603
604 604 if self.flagNoMoreFiles:
605 605 #print 'Process finished'
606 606 return 0
607 607
608 608 return 1
609 609
610 610
611 611 def __getProcessFlags(self):
612 612
613 613 processFlags = 0
614 614
615 615 dtype0 = numpy.dtype([('real','<i1'),('imag','<i1')])
616 616 dtype1 = numpy.dtype([('real','<i2'),('imag','<i2')])
617 617 dtype2 = numpy.dtype([('real','<i4'),('imag','<i4')])
618 618 dtype3 = numpy.dtype([('real','<i8'),('imag','<i8')])
619 619 dtype4 = numpy.dtype([('real','<f4'),('imag','<f4')])
620 620 dtype5 = numpy.dtype([('real','<f8'),('imag','<f8')])
621 621
622 622 dtypeList = [dtype0, dtype1, dtype2, dtype3, dtype4, dtype5]
623 623
624 624
625 625
626 626 datatypeValueList = [PROCFLAG.DATATYPE_CHAR,
627 627 PROCFLAG.DATATYPE_SHORT,
628 628 PROCFLAG.DATATYPE_LONG,
629 629 PROCFLAG.DATATYPE_INT64,
630 630 PROCFLAG.DATATYPE_FLOAT,
631 631 PROCFLAG.DATATYPE_DOUBLE]
632 632
633 633
634 634 for index in range(len(dtypeList)):
635 635 if self.dataOutObj.dtype == dtypeList[index]:
636 636 dtypeValue = datatypeValueList[index]
637 637 break
638 638
639 639 processFlags += dtypeValue
640 640
641 641 if self.dataOutObj.flagDecodeData:
642 642 processFlags += PROCFLAG.DECODE_DATA
643 643
644 644 if self.dataOutObj.flagDeflipData:
645 645 processFlags += PROCFLAG.DEFLIP_DATA
646 646
647 647 if self.dataOutObj.code != None:
648 648 processFlags += PROCFLAG.DEFINE_PROCESS_CODE
649 649
650 650 if self.dataOutObj.nIncohInt > 1:
651 651 processFlags += PROCFLAG.INCOHERENT_INTEGRATION
652 652
653 653 if self.dataOutObj.data_dc != None:
654 654 processFlags += PROCFLAG.SAVE_CHANNELS_DC
655 655
656 656 return processFlags
657 657
658 658
659 659 def __getBlockSize(self):
660 660 '''
661 661 Este metodos determina el cantidad de bytes para un bloque de datos de tipo Spectra
662 662 '''
663 663
664 664 dtype0 = numpy.dtype([('real','<i1'),('imag','<i1')])
665 665 dtype1 = numpy.dtype([('real','<i2'),('imag','<i2')])
666 666 dtype2 = numpy.dtype([('real','<i4'),('imag','<i4')])
667 667 dtype3 = numpy.dtype([('real','<i8'),('imag','<i8')])
668 668 dtype4 = numpy.dtype([('real','<f4'),('imag','<f4')])
669 669 dtype5 = numpy.dtype([('real','<f8'),('imag','<f8')])
670 670
671 671 dtypeList = [dtype0, dtype1, dtype2, dtype3, dtype4, dtype5]
672 672 datatypeValueList = [1,2,4,8,4,8]
673 673 for index in range(len(dtypeList)):
674 674 if self.dataOutObj.dtype == dtypeList[index]:
675 675 datatypeValue = datatypeValueList[index]
676 676 break
677 677
678 678
679 679 pts2write = self.dataOutObj.nHeights * self.dataOutObj.nFFTPoints
680 680
681 681 pts2write_SelfSpectra = int(self.nWrChannels * pts2write)
682 682 blocksize = (pts2write_SelfSpectra*datatypeValue)
683 683
684 684 if self.dataOutObj.data_cspc != None:
685 685 pts2write_CrossSpectra = int(self.nWrPairs * pts2write)
686 686 blocksize += (pts2write_CrossSpectra*datatypeValue*2)
687 687
688 688 if self.dataOutObj.data_dc != None:
689 689 pts2write_DCchannels = int(self.nWrChannels * self.dataOutObj.nHeights)
690 690 blocksize += (pts2write_DCchannels*datatypeValue*2)
691 691
692 692 blocksize = blocksize #* datatypeValue * 2 #CORREGIR ESTO
693 693
694 694 return blocksize
695 695
696 696
697 697 def getBasicHeader(self):
698 698 self.basicHeaderObj.size = self.basicHeaderSize #bytes
699 699 self.basicHeaderObj.version = self.versionFile
700 700 self.basicHeaderObj.dataBlock = self.nTotalBlocks
701 701
702 702 utc = numpy.floor(self.dataOutObj.utctime)
703 703 milisecond = (self.dataOutObj.utctime - utc)* 1000.0
704 704
705 705 self.basicHeaderObj.utc = utc
706 706 self.basicHeaderObj.miliSecond = milisecond
707 707 self.basicHeaderObj.timeZone = 0
708 708 self.basicHeaderObj.dstFlag = 0
709 709 self.basicHeaderObj.errorCount = 0
710 710
711 711 def getDataHeader(self):
712 712
713 713 """
714 714 Obtiene una copia del First Header
715 715
716 716 Affected:
717 717 self.systemHeaderObj
718 718 self.radarControllerHeaderObj
719 719 self.dtype
720 720
721 721 Return:
722 722 None
723 723 """
724 724
725 725 self.systemHeaderObj = self.dataOutObj.systemHeaderObj.copy()
726 726 self.systemHeaderObj.nChannels = self.dataOutObj.nChannels
727 727 self.radarControllerHeaderObj = self.dataOutObj.radarControllerHeaderObj.copy()
728 728
729 729 self.getBasicHeader()
730 730
731 731 processingHeaderSize = 40 # bytes
732 732 self.processingHeaderObj.dtype = 0 # Voltage
733 733 self.processingHeaderObj.blockSize = self.__getBlockSize()
734 734 self.processingHeaderObj.profilesPerBlock = self.dataOutObj.nFFTPoints
735 735 self.processingHeaderObj.dataBlocksPerFile = self.blocksPerFile
736 736 self.processingHeaderObj.nWindows = 1 #podria ser 1 o self.dataOutObj.processingHeaderObj.nWindows
737 737 self.processingHeaderObj.processFlags = self.__getProcessFlags()
738 738 self.processingHeaderObj.nCohInt = self.dataOutObj.nCohInt# Se requiere para determinar el valor de timeInterval
739 739 self.processingHeaderObj.nIncohInt = self.dataOutObj.nIncohInt
740 740 self.processingHeaderObj.totalSpectra = self.dataOutObj.nPairs + self.dataOutObj.nChannels
741 741
742 742 if self.processingHeaderObj.totalSpectra > 0:
743 743 channelList = []
744 744 for channel in range(self.dataOutObj.nChannels):
745 745 channelList.append(channel)
746 746 channelList.append(channel)
747 747
748 748 pairsList = []
749 749 for pair in self.dataOutObj.pairsList:
750 750 pairsList.append(pair[0])
751 751 pairsList.append(pair[1])
752 752 spectraComb = channelList + pairsList
753 753 spectraComb = numpy.array(spectraComb,dtype="u1")
754 754 self.processingHeaderObj.spectraComb = spectraComb
755 755 sizeOfSpcComb = len(spectraComb)
756 756 processingHeaderSize += sizeOfSpcComb
757 757
758 758 if self.dataOutObj.code != None:
759 759 self.processingHeaderObj.code = self.dataOutObj.code
760 760 self.processingHeaderObj.nCode = self.dataOutObj.nCode
761 761 self.processingHeaderObj.nBaud = self.dataOutObj.nBaud
762 762 nCodeSize = 4 # bytes
763 763 nBaudSize = 4 # bytes
764 764 codeSize = 4 # bytes
765 765 sizeOfCode = int(nCodeSize + nBaudSize + codeSize * self.dataOutObj.nCode * self.dataOutObj.nBaud)
766 766 processingHeaderSize += sizeOfCode
767 767
768 768 if self.processingHeaderObj.nWindows != 0:
769 769 self.processingHeaderObj.firstHeight = self.dataOutObj.heightList[0]
770 770 self.processingHeaderObj.deltaHeight = self.dataOutObj.heightList[1] - self.dataOutObj.heightList[0]
771 771 self.processingHeaderObj.nHeights = self.dataOutObj.nHeights
772 772 self.processingHeaderObj.samplesWin = self.dataOutObj.nHeights
773 773 sizeOfFirstHeight = 4
774 774 sizeOfdeltaHeight = 4
775 775 sizeOfnHeights = 4
776 776 sizeOfWindows = (sizeOfFirstHeight + sizeOfdeltaHeight + sizeOfnHeights)*self.processingHeaderObj.nWindows
777 777 processingHeaderSize += sizeOfWindows
778 778
779 779 self.processingHeaderObj.size = processingHeaderSize
780 780
781 781
782
783 class FITS:
784 name=None
785 format=None
786 array =None
787 data =None
788 thdulist=None
789
790 def __init__(self):
791
792 pass
793
794 def setColF(self,name,format,array):
795 self.name=name
796 self.format=format
797 self.array=array
798 a1=numpy.array([self.array],dtype=numpy.float32)
799 self.col1 = pyfits.Column(name=self.name, format=self.format, array=a1)
800 return self.col1
801
802 # def setColP(self,name,format,data):
803 # self.name=name
804 # self.format=format
805 # self.data=data
806 # a2=numpy.array([self.data],dtype=numpy.float32)
807 # self.col2 = pyfits.Column(name=self.name, format=self.format, array=a2)
808 # return self.col2
809
810 def writeHeader(self,):
811 pass
812
813 def writeData(self,name,format,data):
814 self.name=name
815 self.format=format
816 self.data=data
817 a2=numpy.array([self.data],dtype=numpy.float32)
818 self.col2 = pyfits.Column(name=self.name, format=self.format, array=a2)
819 return self.col2
820
821 def cFImage(self,n):
822 self.hdu= pyfits.PrimaryHDU(n)
823 return self.hdu
824
825 def Ctable(self,col1,col2,col3,col4,col5,col6,col7,col8,col9):
826 self.cols=pyfits.ColDefs( [col1,col2,col3,col4,col5,col6,col7,col8,col9])
827 self.tbhdu = pyfits.new_table(self.cols)
828 return self.tbhdu
829
830 def CFile(self,hdu,tbhdu):
831 self.thdulist=pyfits.HDUList([hdu,tbhdu])
832
833 def wFile(self,filename):
834 self.thdulist.writeto(filename)
835
836 class SpectraHeisWriter():
837 i=0
838 def __init__(self, dataOutObj):
839 self.wrObj = FITS()
840 self.dataOutObj = dataOutObj
841
842 def isNumber(str):
843 """
844 Chequea si el conjunto de caracteres que componen un string puede ser convertidos a un numero.
845
846 Excepciones:
847 Si un determinado string no puede ser convertido a numero
848 Input:
849 str, string al cual se le analiza para determinar si convertible a un numero o no
850
851 Return:
852 True : si el string es uno numerico
853 False : no es un string numerico
854 """
855 try:
856 float( str )
857 return True
858 except:
859 return False
860
861 def setup(self, wrpath,):
862
863 if not(os.path.exists(wrpath)):
864 os.mkdir(wrpath)
865
866 self.wrpath = wrpath
867 self.setFile = 0
868
869 def putData(self):
870 # self.wrObj.writeHeader(nChannels=self.dataOutObj.nChannels, nFFTPoints=self.dataOutObj.nFFTPoints)
871 #name = self.dataOutObj.utctime
872 name= time.localtime( self.dataOutObj.utctime)
873 ext=".fits"
874 #folder='D%4.4d%3.3d'%(name.tm_year,name.tm_yday)
875 subfolder = 'D%4.4d%3.3d' % (name.tm_year,name.tm_yday)
876
877 doypath = os.path.join( self.wrpath, subfolder )
878 if not( os.path.exists(doypath) ):
879 os.mkdir(doypath)
880 self.setFile += 1
881 file = 'D%4.4d%3.3d%3.3d%s' % (name.tm_year,name.tm_yday,self.setFile,ext)
882
883 filename = os.path.join(self.wrpath,subfolder, file)
884
885 # print self.dataOutObj.ippSeconds
886 freq=numpy.arange(-1*self.dataOutObj.nHeights/2.,self.dataOutObj.nHeights/2.)/(2*self.dataOutObj.ippSeconds)
887
888 col1=self.wrObj.setColF(name="freq", format=str(self.dataOutObj.nFFTPoints)+'E', array=freq)
889 col2=self.wrObj.writeData(name="P_Ch1",format=str(self.dataOutObj.nFFTPoints)+'E',data=10*numpy.log10(self.dataOutObj.data_spc[0,:]))
890 col3=self.wrObj.writeData(name="P_Ch2",format=str(self.dataOutObj.nFFTPoints)+'E',data=10*numpy.log10(self.dataOutObj.data_spc[1,:]))
891 col4=self.wrObj.writeData(name="P_Ch3",format=str(self.dataOutObj.nFFTPoints)+'E',data=10*numpy.log10(self.dataOutObj.data_spc[2,:]))
892 col5=self.wrObj.writeData(name="P_Ch4",format=str(self.dataOutObj.nFFTPoints)+'E',data=10*numpy.log10(self.dataOutObj.data_spc[3,:]))
893 col6=self.wrObj.writeData(name="P_Ch5",format=str(self.dataOutObj.nFFTPoints)+'E',data=10*numpy.log10(self.dataOutObj.data_spc[4,:]))
894 col7=self.wrObj.writeData(name="P_Ch6",format=str(self.dataOutObj.nFFTPoints)+'E',data=10*numpy.log10(self.dataOutObj.data_spc[5,:]))
895 col8=self.wrObj.writeData(name="P_Ch7",format=str(self.dataOutObj.nFFTPoints)+'E',data=10*numpy.log10(self.dataOutObj.data_spc[6,:]))
896 col9=self.wrObj.writeData(name="P_Ch8",format=str(self.dataOutObj.nFFTPoints)+'E',data=10*numpy.log10(self.dataOutObj.data_spc[7,:]))
897 #n=numpy.arange((100))
898 n=self.dataOutObj.data_spc[6,:]
899 a=self.wrObj.cFImage(n)
900 b=self.wrObj.Ctable(col1,col2,col3,col4,col5,col6,col7,col8,col9)
901 self.wrObj.CFile(a,b)
902 self.wrObj.wFile(filename)
903 return 1
904
905 No newline at end of file
@@ -1,737 +1,770
1 1 '''
2 2
3 3 $Author$
4 4 $Id$
5 5 '''
6 6
7 7 import os, sys
8 8 import numpy
9 9 import time
10 10 import datetime
11 11 path = os.path.split(os.getcwd())[0]
12 12 sys.path.append(path)
13 13
14 14 from Data.JROData import Spectra, SpectraHeis
15 15 from IO.SpectraIO import SpectraWriter
16 16 from Graphics.schainPlotTypes import ScopeFigure, SpcFigure, RTIFigure
17 17 #from JRONoise import Noise
18 18
19 19 class SpectraProcessor:
20 20 '''
21 21 classdocs
22 22 '''
23 23
24 24 dataInObj = None
25 25
26 26 dataOutObj = None
27 27
28 28 noiseObj = None
29 29
30 30 integratorObjList = []
31 31
32 32 writerObjList = []
33 33
34 34 integratorObjIndex = None
35 35
36 36 writerObjIndex = None
37 37
38 38 profIndex = 0 # Se emplea cuando el objeto de entrada es un Voltage
39 39
40 40 firstdatatime = None
41 41
42 42 def __init__(self):
43 43 '''
44 44 Constructor
45 45 '''
46 46
47 47 self.integratorObjIndex = None
48 48 self.writerObjIndex = None
49 49 self.plotObjIndex = None
50 50 self.integratorOst = []
51 51 self.plotObjList = []
52 52 self.noiseObj = []
53 53 self.writerObjList = []
54 54 self.buffer = None
55 55 self.firstdatatime = None
56 56 self.profIndex = 0
57 57
58 58 def setup(self, dataInObj=None, dataOutObj=None, nFFTPoints=None, pairsList=None):
59 59
60 60 if dataInObj == None:
61 61 raise ValueError, "This SpectraProcessor.setup() function needs dataInObj input variable"
62 62
63 63 if dataInObj.type == "Voltage":
64 64 if nFFTPoints == None:
65 65 raise ValueError, "This SpectraProcessor.setup() function needs nFFTPoints input variable"
66 66
67 67
68 68
69 69 if dataInObj.type == "Spectra":
70 70 if nFFTPoints != None:
71 71 raise ValueError, "The nFFTPoints cannot be selected to this object type"
72 72
73 73 nFFTPoints = dataInObj.nFFTPoints
74 74
75 75 if pairsList == None:
76 76 pairsList = dataInObj.pairsList
77 77
78 78 if pairsList == None:
79 79 nPairs = 0
80 80 else:
81 81 nPairs = len(pairsList)
82 82
83 83 self.dataInObj = dataInObj
84 84
85 85 if dataOutObj == None:
86 86 dataOutObj = Spectra()
87 87
88 88 self.dataOutObj = dataOutObj
89 89 self.dataOutObj.nFFTPoints = nFFTPoints
90 90 self.dataOutObj.pairsList = pairsList
91 91 self.dataOutObj.nPairs = nPairs
92 92
93 93 return self.dataOutObj
94 94
95 95 def init(self):
96 96
97 97 self.dataOutObj.flagNoData = True
98 98
99 99 if self.dataInObj.flagNoData:
100 100 return 0
101 101
102 102 self.integratorObjIndex = 0
103 103 self.writerObjIndex = 0
104 104 self.plotObjIndex = 0
105 105
106 106
107 107 if self.dataInObj.type == "Spectra":
108 108
109 109 self.dataOutObj.copy(self.dataInObj)
110 110 self.dataOutObj.flagNoData = False
111 111 return
112 112
113 113 if self.dataInObj.type == "Voltage":
114 114
115 115 if self.buffer == None:
116 116 self.buffer = numpy.zeros((self.dataInObj.nChannels,
117 117 self.dataOutObj.nFFTPoints,
118 118 self.dataInObj.nHeights),
119 119 dtype='complex')
120 120
121 121 self.buffer[:,self.profIndex,:] = self.dataInObj.data
122 122 self.profIndex += 1
123 123
124 124 if self.firstdatatime == None:
125 125 self.firstdatatime = self.dataInObj.utctime
126 126
127 127 if self.profIndex == self.dataOutObj.nFFTPoints:
128 128
129 129 self.__updateObjFromInput()
130 130 self.__getFft()
131 131
132 132 self.dataOutObj.flagNoData = False
133 133
134 134 self.buffer = None
135 135 self.firstdatatime = None
136 136 self.profIndex = 0
137 137
138 138 return
139 139
140 140 #Other kind of data
141 141 raise ValueError, "The type object %(s) is not valid " %(self.dataOutObj.type)
142 142
143 143 def __getFft(self):
144 144 """
145 145 Convierte valores de Voltaje a Spectra
146 146
147 147 Affected:
148 148 self.dataOutObj.data_spc
149 149 self.dataOutObj.data_cspc
150 150 self.dataOutObj.data_dc
151 151 self.dataOutObj.heightList
152 152 self.dataOutObj.m_BasicHeader
153 153 self.dataOutObj.m_ProcessingHeader
154 154 self.dataOutObj.radarControllerHeaderObj
155 155 self.dataOutObj.systemHeaderObj
156 156 self.profIndex
157 157 self.buffer
158 158 self.dataOutObj.flagNoData
159 159 self.dataOutObj.dtype
160 160 self.dataOutObj.nPairs
161 161 self.dataOutObj.nChannels
162 162 self.dataOutObj.nProfiles
163 163 self.dataOutObj.systemHeaderObj.numChannels
164 164 self.dataOutObj.m_ProcessingHeader.totalSpectra
165 165 self.dataOutObj.m_ProcessingHeader.profilesPerBlock
166 166 self.dataOutObj.m_ProcessingHeader.numHeights
167 167 self.dataOutObj.m_ProcessingHeader.spectraComb
168 168 self.dataOutObj.m_ProcessingHeader.shif_fft
169 169 """
170 170
171 171 fft_volt = numpy.fft.fft(self.buffer,axis=1)
172 172 dc = fft_volt[:,0,:]
173 173
174 174 #calculo de self-spectra
175 175 fft_volt = numpy.fft.fftshift(fft_volt,axes=(1,))
176 176 spc = fft_volt * numpy.conjugate(fft_volt)
177 177 spc = spc.real
178 178
179 179 blocksize = 0
180 180 blocksize += dc.size
181 181 blocksize += spc.size
182 182
183 183 cspc = None
184 184 pairIndex = 0
185 185 if self.dataOutObj.pairsList != None:
186 186 #calculo de cross-spectra
187 187 cspc = numpy.zeros((self.dataOutObj.nPairs, self.dataOutObj.nFFTPoints, self.dataOutObj.nHeights), dtype='complex')
188 188 for pair in self.dataOutObj.pairsList:
189 189 cspc[pairIndex,:,:] = numpy.abs(fft_volt[pair[0],:,:] * numpy.conjugate(fft_volt[pair[1],:,:]))
190 190 pairIndex += 1
191 191 blocksize += cspc.size
192 192
193 193 self.dataOutObj.data_spc = spc
194 194 self.dataOutObj.data_cspc = cspc
195 195 self.dataOutObj.data_dc = dc
196 196 self.dataOutObj.blockSize = blocksize
197 197
198 198 # self.getNoise()
199 199
200 200 def __updateObjFromInput(self):
201 201
202 202 self.dataOutObj.radarControllerHeaderObj = self.dataInObj.radarControllerHeaderObj.copy()
203 203 self.dataOutObj.systemHeaderObj = self.dataInObj.systemHeaderObj.copy()
204 204 self.dataOutObj.channelList = self.dataInObj.channelList
205 205 self.dataOutObj.heightList = self.dataInObj.heightList
206 206 self.dataOutObj.dtype = self.dataInObj.dtype
207 207 self.dataOutObj.nHeights = self.dataInObj.nHeights
208 208 self.dataOutObj.nChannels = self.dataInObj.nChannels
209 209 self.dataOutObj.nBaud = self.dataInObj.nBaud
210 210 self.dataOutObj.nCode = self.dataInObj.nCode
211 211 self.dataOutObj.code = self.dataInObj.code
212 212 self.dataOutObj.nProfiles = self.dataOutObj.nFFTPoints
213 213 self.dataOutObj.channelIndexList = self.dataInObj.channelIndexList
214 214 self.dataOutObj.flagTimeBlock = self.dataInObj.flagTimeBlock
215 215 self.dataOutObj.utctime = self.firstdatatime
216 216 self.dataOutObj.flagDecodeData = self.dataInObj.flagDecodeData #asumo q la data esta decodificada
217 217 self.dataOutObj.flagDeflipData = self.dataInObj.flagDeflipData #asumo q la data esta sin flip
218 218 self.dataOutObj.flagShiftFFT = self.dataInObj.flagShiftFFT
219 219 self.dataOutObj.nCohInt = self.dataInObj.nCohInt
220 220 self.dataOutObj.nIncohInt = 1
221 221 self.dataOutObj.ippSeconds = self.dataInObj.ippSeconds
222 222 self.dataOutObj.timeInterval = self.dataInObj.timeInterval*self.dataOutObj.nFFTPoints
223 223
224 224 def addWriter(self, wrpath, blocksPerFile):
225 225
226 226 objWriter = SpectraWriter(self.dataOutObj)
227 227 objWriter.setup(wrpath, blocksPerFile)
228 228 self.writerObjList.append(objWriter)
229 229
230 230 def addIntegrator(self,N,timeInterval):
231 231
232 232 objIncohInt = IncoherentIntegration(N,timeInterval)
233 233 self.integratorObjList.append(objIncohInt)
234 234
235 235 def addCrossSpc(self, idfigure, nframes, wintitle, driver, colormap, colorbar, showprofile):
236 236 crossSpcObj = CrossSpcFigure(idfigure, nframes, wintitle, driver, colormap, colorbar, showprofile)
237 237 self.plotObjList.append(crossSpcObj)
238 238
239 239 def plotCrossSpc(self, idfigure=None,
240 240 xmin=None,
241 241 xmax=None,
242 242 ymin=None,
243 243 ymax=None,
244 244 minvalue=None,
245 245 maxvalue=None,
246 246 wintitle='',
247 247 driver='plplot',
248 248 colormap='br_green',
249 249 colorbar=True,
250 250 showprofile=False,
251 251 save=False,
252 252 gpath=None,
253 253 pairsList = None):
254 254
255 255 if self.dataOutObj.flagNoData:
256 256 return 0
257 257
258 258 if pairsList == None:
259 259 pairsList = self.dataOutObj.pairsList
260 260
261 261 nframes = len(pairsList)
262 262
263 263 x = numpy.arange(self.dataOutObj.nFFTPoints)
264 264
265 265 y = self.dataOutObj.heightList
266 266
267 267 data_spc = self.dataOutObj.data_spc
268 268 data_cspc = self.dataOutObj.data_cspc
269 269
270 270 data = []
271 271
272 272
273 273 if len(self.plotObjList) <= self.plotObjIndex:
274 274 self.addSpc(idfigure, nframes, wintitle, driver, colormap, colorbar, showprofile)
275 275
276 276
277 277
278 278
279 279
280 280 def addRti(self, idfigure, nframes, wintitle, driver, colormap, colorbar, showprofile):
281 281 rtiObj = RTIFigure(idfigure, nframes, wintitle, driver, colormap, colorbar, showprofile)
282 282 self.plotObjList.append(rtiObj)
283 283
284 284 def plotRti(self, idfigure=None,
285 285 starttime=None,
286 286 endtime=None,
287 287 rangemin=None,
288 288 rangemax=None,
289 289 minvalue=None,
290 290 maxvalue=None,
291 291 wintitle='',
292 292 driver='plplot',
293 293 colormap='br_greeen',
294 294 colorbar=True,
295 295 showprofile=False,
296 296 xrangestep=None,
297 297 save=False,
298 298 gpath=None,
299 299 ratio=1,
300 300 channelList=None):
301 301
302 302 if self.dataOutObj.flagNoData:
303 303 return 0
304 304
305 305 if channelList == None:
306 306 channelList = self.dataOutObj.channelList
307 307
308 308 nframes = len(channelList)
309 309
310 310 if len(self.plotObjList) <= self.plotObjIndex:
311 311 self.addRti(idfigure, nframes, wintitle, driver, colormap, colorbar, showprofile)
312 312
313 313 data = 10.*numpy.log10(self.dataOutObj.data_spc[channelList,:,:])
314 314
315 315 data = numpy.average(data, axis=1)
316 316
317 317 currenttime = self.dataOutObj.utctime - time.timezone
318 318
319 319 range = self.dataOutObj.heightList
320 320
321 321
322 322 figuretitle = "RTI Plot for Spectra Data" #+ date
323 323
324 324 cleardata = False
325 325
326 326 deltax = self.dataOutObj.timeInterval
327 327
328 328 plotObj = self.plotObjList[self.plotObjIndex]
329 329
330 330 plotObj.plotPcolor(data=data,
331 331 x=currenttime,
332 332 y=range,
333 333 channelList=channelList,
334 334 xmin=starttime,
335 335 xmax=endtime,
336 336 ymin=rangemin,
337 337 ymax=rangemax,
338 338 minvalue=minvalue,
339 339 maxvalue=maxvalue,
340 340 figuretitle=figuretitle,
341 341 xrangestep=xrangestep,
342 342 deltax=deltax,
343 343 save=save,
344 344 gpath=gpath,
345 345 ratio=ratio,
346 346 cleardata=cleardata
347 347 )
348 348
349 349
350 350
351 351 self.plotObjIndex += 1
352 352
353 353
354 354
355 355
356 356
357 357
358 358
359 359 def addSpc(self, idfigure, nframes, wintitle, driver, colormap, colorbar, showprofile):
360 360
361 361 spcObj = SpcFigure(idfigure, nframes, wintitle, driver, colormap, colorbar, showprofile)
362 362 self.plotObjList.append(spcObj)
363 363
364 364 def plotSpc(self, idfigure=None,
365 365 xmin=None,
366 366 xmax=None,
367 367 ymin=None,
368 368 ymax=None,
369 369 minvalue=None,
370 370 maxvalue=None,
371 371 wintitle='',
372 372 driver='plplot',
373 373 colormap='br_green',
374 374 colorbar=True,
375 375 showprofile=False,
376 376 save=False,
377 377 gpath=None,
378 378 ratio=1,
379 379 channelList=None):
380 380
381 381 if self.dataOutObj.flagNoData:
382 382 return 0
383 383
384 384 if channelList == None:
385 385 channelList = self.dataOutObj.channelList
386 386
387 387 nframes = len(channelList)
388 388
389 389 if len(self.plotObjList) <= self.plotObjIndex:
390 390 self.addSpc(idfigure, nframes, wintitle, driver, colormap, colorbar, showprofile)
391 391
392 392 x = numpy.arange(self.dataOutObj.nFFTPoints)
393 393
394 394 y = self.dataOutObj.heightList
395 395
396 396 data = 10.*numpy.log10(self.dataOutObj.data_spc[channelList,:,:])
397 397 # noisedB = 10.*numpy.log10(noise)
398 398 noisedB = numpy.arange(len(channelList)+1)
399 399 noisedB = noisedB *1.2
400 400 titleList = []
401 401 for i in range(len(noisedB)):
402 402 title = "%.2f"%noisedB[i]
403 403 titleList.append(title)
404 404
405 405 thisdatetime = datetime.datetime.fromtimestamp(self.dataOutObj.utctime)
406 406 dateTime = "%s"%(thisdatetime.strftime("%d-%b-%Y %H:%M:%S"))
407 407 figuretitle = "Spc Radar Data: %s"%dateTime
408 408
409 409 cleardata = True
410 410
411 411 plotObj = self.plotObjList[self.plotObjIndex]
412 412
413 413 plotObj.plotPcolor(data=data,
414 414 x=x,
415 415 y=y,
416 416 channelList=channelList,
417 417 xmin=xmin,
418 418 xmax=xmax,
419 419 ymin=ymin,
420 420 ymax=ymax,
421 421 minvalue=minvalue,
422 422 maxvalue=maxvalue,
423 423 figuretitle=figuretitle,
424 424 xrangestep=None,
425 425 deltax=None,
426 426 save=save,
427 427 gpath=gpath,
428 428 cleardata=cleardata
429 429 )
430 430
431 431 self.plotObjIndex += 1
432 432
433 433
434 434 def writeData(self, wrpath, blocksPerFile):
435 435
436 436 if self.dataOutObj.flagNoData:
437 437 return 0
438 438
439 439 if len(self.writerObjList) <= self.writerObjIndex:
440 440 self.addWriter(wrpath, blocksPerFile)
441 441
442 442 self.writerObjList[self.writerObjIndex].putData()
443 443
444 444 self.writerObjIndex += 1
445 445
446 446 def integrator(self, N=None, timeInterval=None):
447 447
448 448 if self.dataOutObj.flagNoData:
449 449 return 0
450 450
451 451 if len(self.integratorObjList) <= self.integratorObjIndex:
452 452 self.addIntegrator(N,timeInterval)
453 453
454 454 myIncohIntObj = self.integratorObjList[self.integratorObjIndex]
455 455 myIncohIntObj.exe(data=self.dataOutObj.data_spc,datatime=self.dataOutObj.utctime)
456 456
457 457 if myIncohIntObj.isReady:
458 458 self.dataOutObj.data_spc = myIncohIntObj.data
459 459 self.dataOutObj.timeInterval *= myCohIntObj.nIncohInt
460 460 self.dataOutObj.nIncohInt = myIncohIntObj.navg * self.dataInObj.nIncohInt
461 461 self.dataOutObj.utctime = myIncohIntObj.firstdatatime
462 462 self.dataOutObj.flagNoData = False
463 463
464 464 """Calcular el ruido"""
465 465 self.getNoise()
466 466 else:
467 467 self.dataOutObj.flagNoData = True
468 468
469 469 self.integratorObjIndex += 1
470 470
471 471
472 472 class SpectraHeisProcessor:
473 473
474 474 def __init__(self):
475 475
476 476 self.integratorObjIndex = None
477 477 self.writerObjIndex = None
478 478 self.plotObjIndex = None
479 479 self.integratorObjList = []
480 480 self.writerObjList = []
481 481 self.plotObjList = []
482 482 #self.noiseObj = Noise()
483 483
484 484 def setup(self, dataInObj, dataOutObj=None, nFFTPoints=None, pairList=None):
485 485
486 486 if nFFTPoints == None:
487 487 nFFTPoints = self.dataInObj.nHeights
488 488
489 489 self.dataInObj = dataInObj
490 490
491 491 if dataOutObj == None:
492 492 dataOutObj = SpectraHeis()
493 493
494 494 self.dataOutObj = dataOutObj
495 495
496 496 return self.dataOutObj
497 497
498 498 def init(self):
499 499
500 500 self.dataOutObj.flagNoData = True
501 501
502 502 if self.dataInObj.flagNoData:
503 503 return 0
504 504
505 505 self.integratorObjIndex = 0
506 506 self.writerObjIndex = 0
507 507 self.plotObjIndex = 0
508 508
509 509 if self.dataInObj.type == "Voltage":
510 510 self.__updateObjFromInput()
511 511 self.__getFft()
512 512 self.dataOutObj.flagNoData = False
513 513 return
514 514
515 515 #Other kind of data
516 516 if self.dataInObj.type == "SpectraHeis":
517 517 self.dataOutObj.copy(self.dataInObj)
518 518 self.dataOutObj.flagNoData = False
519 519 return
520 520
521 521 raise ValueError, "The type is not valid"
522 522
523 523 def __updateObjFromInput(self):
524 524
525 525 self.dataOutObj.radarControllerHeaderObj = self.dataInObj.radarControllerHeaderObj.copy()
526 526 self.dataOutObj.systemHeaderObj = self.dataInObj.systemHeaderObj.copy()
527 527 self.dataOutObj.channelList = self.dataInObj.channelList
528 528 self.dataOutObj.heightList = self.dataInObj.heightList
529 529 self.dataOutObj.dtype = self.dataInObj.dtype
530 530 self.dataOutObj.nHeights = self.dataInObj.nHeights
531 531 self.dataOutObj.nChannels = self.dataInObj.nChannels
532 532 self.dataOutObj.nBaud = self.dataInObj.nBaud
533 533 self.dataOutObj.nCode = self.dataInObj.nCode
534 534 self.dataOutObj.code = self.dataInObj.code
535 535 self.dataOutObj.nProfiles = 1
536 536 self.dataOutObj.nFFTPoints = self.dataInObj.nHeights
537 537 self.dataOutObj.channelIndexList = self.dataInObj.channelIndexList
538 538 self.dataOutObj.flagNoData = self.dataInObj.flagNoData
539 539 self.dataOutObj.flagTimeBlock = self.dataInObj.flagTimeBlock
540 540 self.dataOutObj.utctime = self.dataInObj.utctime
541 541 self.dataOutObj.flagDecodeData = self.dataInObj.flagDecodeData #asumo q la data esta decodificada
542 542 self.dataOutObj.flagDeflipData = self.dataInObj.flagDeflipData #asumo q la data esta sin flip
543 543 self.dataOutObj.flagShiftFFT = self.dataInObj.flagShiftFFT
544 544 self.dataOutObj.nIncohInt = 1
545 self.dataOutObj.ippSeconds= self.dataInObj.ippSeconds
546
547 # def addWriter(self,wrpath,blocksPerfile):
548 def addWriter(self,wrpath):
549 objWriter=SpectraHeisWriter(self.dataOutObj)
550 objWriter.setup(wrpath)
551 #objWriter.setup(wrpath,blocksPerfile)
552 self.writerObjList.append(objWriter)
553
554 # def writedata(self,wrpath,blocksPerfile):
555 def writedata(self,wrpath):
556 if self.dataOutObj.flagNoData:
557 return 0
558
559 if len(self.writerObjList) <= self.writerObjIndex:
560 #self.addWriter(wrpath, blocksPerFile)
561 self.addWriter(wrpath)
562
563 self.writerObjList[self.writerObjIndex].putData()
564
565 self.writerObjIndex += 1
545 566
546 567 def __getFft(self):
547 568
548 569 fft_volt = numpy.fft.fft(self.dataInObj.data, axis=1)
549 570 #print fft_volt
550 571 #calculo de self-spectra
551 572 fft_volt = numpy.fft.fftshift(fft_volt,axes=(1,))
552 573
553 574 spc = numpy.abs(fft_volt * numpy.conjugate(fft_volt))
554 575 self.dataOutObj.data_spc = spc
555 576
556 577 def getSpectra(self):
557 578
558 579 return self.dataOutObj.data_spc
559 580
560 581 def getFrecuencies(self):
561 582
562 583 print self.nFFTPoints
563 584 return numpy.arange(int(self.nFFTPoints))
564 585
565 586 def addIntegrator(self,N,timeInterval):
566 587
567 588 objIncohInt = IncoherentIntegration(N,timeInterval)
568 589 self.integratorObjList.append(objIncohInt)
569 590
570 591 def integrator(self, N=None, timeInterval=None):
571 592
572 593 if self.dataOutObj.flagNoData:
573 594 return 0
574 595
575 596 if len(self.integratorObjList) <= self.integratorObjIndex:
576 597 self.addIntegrator(N,timeInterval)
577 598
578 599 myIncohIntObj = self.integratorObjList[self.integratorObjIndex]
579 600 myIncohIntObj.exe(data=self.dataOutObj.data_spc,timeOfData=self.dataOutObj.utctime)
580 601
581 602 if myIncohIntObj.isReady:
582 603 self.dataOutObj.data_spc = myIncohIntObj.data
583 604 self.dataOutObj.nIncohInt = self.dataOutObj.nIncohInt*myIncohIntObj.navg
584 605 self.dataOutObj.flagNoData = False
585 606
586 607 #self.getNoise(type="hildebrand",parm=myIncohIntObj.navg)
587 608 # self.getNoise(type="sort", parm=16)
588 609
589 610 else:
590 611 self.dataOutObj.flagNoData = True
591 612
592 613 self.integratorObjIndex += 1
593 614
594 615
595 616 def addScope(self, idfigure, nframes, wintitle, driver):
596 617
597 618 if idfigure==None:
598 619 idfigure = self.plotObjIndex
599 620
600 621 scopeObj = ScopeFigure(idfigure, nframes, wintitle, driver)
601 622 self.plotObjList.append(scopeObj)
602 623
603 624 def plotScope(self,
604 625 idfigure=None,
605 626 minvalue=None,
606 627 maxvalue=None,
607 628 xmin=None,
608 629 xmax=None,
609 630 wintitle='',
610 631 driver='plplot',
611 632 save=False,
612 633 gpath=None,
613 634 titleList=None,
614 635 xlabelList=None,
615 636 ylabelList=None):
616 637
617 638 if self.dataOutObj.flagNoData:
618 639 return 0
619 640
620 641 nframes = len(self.dataOutObj.channelList)
621 642
622 643 if len(self.plotObjList) <= self.plotObjIndex:
623 644 self.addScope(idfigure, nframes, wintitle, driver)
624 645
625 646
626 647 data1D = self.dataOutObj.data_spc
627 648
628 649 x = numpy.arange(self.dataOutObj.nHeights)
629 650
630 651 thisDatetime = datetime.datetime.fromtimestamp(self.dataOutObj.utctime)
631 652
632 653 dateTime = "%s"%(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
633 654 date = "%s"%(thisDatetime.strftime("%d-%b-%Y"))
634 655
635 656 figureTitle = "Scope Plot Radar Data: " + date
636 657
637 658 plotObj = self.plotObjList[self.plotObjIndex]
638 659
639 660 plotObj.plot1DArray(data1D,
640 661 x,
641 662 self.dataOutObj.channelList,
642 663 xmin,
643 664 xmax,
644 665 minvalue,
645 666 maxvalue,
646 667 figureTitle,
647 668 save,
648 669 gpath)
649 670
650 671 self.plotObjIndex += 1
651 672
673 def rti(self):
674 if self.dataOutObj.flagNoData:
675 return 0
676
677 data=numpy.average(self.dataOutObj.data_spc,axis=1)
678 data[0]
679 print data[0]
680 x = numpy.arange(100000)
681
682 print "test"
683 #print self.dataOutObj.data_spc.average(axis=1)
684
652 685 class IncoherentIntegration:
653 686
654 687 integ_counter = None
655 688 data = None
656 689 navg = None
657 690 buffer = None
658 691 nIncohInt = None
659 692 firstdatatime = None
660 693
661 694 def __init__(self, N = None, timeInterval = None):
662 695 """
663 696 N
664 697 timeInterval - interval time [min], integer value
665 698 """
666 699
667 700 self.data = None
668 701 self.navg = None
669 702 self.buffer = None
670 703 self.timeOut = None
671 704 self.exitCondition = False
672 705 self.isReady = False
673 706 self.nIncohInt = N
674 707 self.integ_counter = 0
675 708 self.firstdatatime = None
676 709
677 710 if timeInterval!=None:
678 711 self.timeIntervalInSeconds = timeInterval * 60. #if (type(timeInterval)!=integer) -> change this line
679 712
680 713 if ((timeInterval==None) and (N==None)):
681 714 print 'N = None ; timeInterval = None'
682 715 sys.exit(0)
683 716 elif timeInterval == None:
684 717 self.timeFlag = False
685 718 else:
686 719 self.timeFlag = True
687 720
688 721
689 722 def exe(self,data,datatime):
690 723 """
691 724 data
692 725
693 726 datatime [seconds]
694 727 """
695 728 if self.firstdatatime == None or self.isReady:
696 729 self.firstdatatime = datatime
697 730
698 731 if self.timeFlag:
699 732 if self.timeOut == None:
700 733 self.timeOut = datatime + self.timeIntervalInSeconds
701 734
702 735 if datatime < self.timeOut:
703 736 if self.buffer == None:
704 737 self.buffer = data
705 738 else:
706 739 self.buffer = self.buffer + data
707 740 self.integ_counter += 1
708 741 else:
709 742 self.exitCondition = True
710 743
711 744 else:
712 745 if self.integ_counter < self.nIncohInt:
713 746 if self.buffer == None:
714 747 self.buffer = data
715 748 else:
716 749 self.buffer = self.buffer + data
717 750
718 751 self.integ_counter += 1
719 752
720 753 if self.integ_counter == self.nIncohInt:
721 754 self.exitCondition = True
722 755
723 756 if self.exitCondition:
724 757 self.data = self.buffer
725 758 self.navg = self.integ_counter
726 759 self.isReady = True
727 760 self.buffer = None
728 761 self.timeOut = None
729 762 self.integ_counter = 0
730 763 self.exitCondition = False
731 764
732 765 if self.timeFlag:
733 766 self.buffer = data
734 767 self.timeOut = datatime + self.timeIntervalInSeconds
735 768 else:
736 769 self.isReady = False
737 770 No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now