##// END OF EJS Templates
Bug fixed: condicion para armar el buffer hace referencia al numero de puntos de FFT. La condicion debe estar ligada al numero de perfiles.
Daniel Valdez -
r421:367e0dd2bc1d
parent child
Show More
@@ -1,1656 +1,1656
1 1 '''
2 2
3 3 $Author: dsuarez $
4 4 $Id: Processor.py 1 2012-11-12 18:56:07Z dsuarez $
5 5 '''
6 6 import os
7 7 import numpy
8 8 import datetime
9 9 import time
10 10
11 11 from jrodata import *
12 12 from jrodataIO import *
13 13 from jroplot import *
14 14
15 15 try:
16 16 import cfunctions
17 17 except:
18 18 pass
19 19
20 20 class ProcessingUnit:
21 21
22 22 """
23 23 Esta es la clase base para el procesamiento de datos.
24 24
25 25 Contiene el metodo "call" para llamar operaciones. Las operaciones pueden ser:
26 26 - Metodos internos (callMethod)
27 27 - Objetos del tipo Operation (callObject). Antes de ser llamados, estos objetos
28 28 tienen que ser agreagados con el metodo "add".
29 29
30 30 """
31 31 # objeto de datos de entrada (Voltage, Spectra o Correlation)
32 32 dataIn = None
33 33
34 34 # objeto de datos de entrada (Voltage, Spectra o Correlation)
35 35 dataOut = None
36 36
37 37
38 38 objectDict = None
39 39
40 40 def __init__(self):
41 41
42 42 self.objectDict = {}
43 43
44 44 def init(self):
45 45
46 46 raise ValueError, "Not implemented"
47 47
48 48 def addOperation(self, object, objId):
49 49
50 50 """
51 51 Agrega el objeto "object" a la lista de objetos "self.objectList" y retorna el
52 52 identificador asociado a este objeto.
53 53
54 54 Input:
55 55
56 56 object : objeto de la clase "Operation"
57 57
58 58 Return:
59 59
60 60 objId : identificador del objeto, necesario para ejecutar la operacion
61 61 """
62 62
63 63 self.objectDict[objId] = object
64 64
65 65 return objId
66 66
67 67 def operation(self, **kwargs):
68 68
69 69 """
70 70 Operacion directa sobre la data (dataOut.data). Es necesario actualizar los valores de los
71 71 atributos del objeto dataOut
72 72
73 73 Input:
74 74
75 75 **kwargs : Diccionario de argumentos de la funcion a ejecutar
76 76 """
77 77
78 78 raise ValueError, "ImplementedError"
79 79
80 80 def callMethod(self, name, **kwargs):
81 81
82 82 """
83 83 Ejecuta el metodo con el nombre "name" y con argumentos **kwargs de la propia clase.
84 84
85 85 Input:
86 86 name : nombre del metodo a ejecutar
87 87
88 88 **kwargs : diccionario con los nombres y valores de la funcion a ejecutar.
89 89
90 90 """
91 91 if name != 'run':
92 92
93 93 if name == 'init' and self.dataIn.isEmpty():
94 94 self.dataOut.flagNoData = True
95 95 return False
96 96
97 97 if name != 'init' and self.dataOut.isEmpty():
98 98 return False
99 99
100 100 methodToCall = getattr(self, name)
101 101
102 102 methodToCall(**kwargs)
103 103
104 104 if name != 'run':
105 105 return True
106 106
107 107 if self.dataOut.isEmpty():
108 108 return False
109 109
110 110 return True
111 111
112 112 def callObject(self, objId, **kwargs):
113 113
114 114 """
115 115 Ejecuta la operacion asociada al identificador del objeto "objId"
116 116
117 117 Input:
118 118
119 119 objId : identificador del objeto a ejecutar
120 120
121 121 **kwargs : diccionario con los nombres y valores de la funcion a ejecutar.
122 122
123 123 Return:
124 124
125 125 None
126 126 """
127 127
128 128 if self.dataOut.isEmpty():
129 129 return False
130 130
131 131 object = self.objectDict[objId]
132 132
133 133 object.run(self.dataOut, **kwargs)
134 134
135 135 return True
136 136
137 137 def call(self, operationConf, **kwargs):
138 138
139 139 """
140 140 Return True si ejecuta la operacion "operationConf.name" con los
141 141 argumentos "**kwargs". False si la operacion no se ha ejecutado.
142 142 La operacion puede ser de dos tipos:
143 143
144 144 1. Un metodo propio de esta clase:
145 145
146 146 operation.type = "self"
147 147
148 148 2. El metodo "run" de un objeto del tipo Operation o de un derivado de ella:
149 149 operation.type = "other".
150 150
151 151 Este objeto de tipo Operation debe de haber sido agregado antes con el metodo:
152 152 "addOperation" e identificado con el operation.id
153 153
154 154
155 155 con el id de la operacion.
156 156
157 157 Input:
158 158
159 159 Operation : Objeto del tipo operacion con los atributos: name, type y id.
160 160
161 161 """
162 162
163 163 if operationConf.type == 'self':
164 164 sts = self.callMethod(operationConf.name, **kwargs)
165 165
166 166 if operationConf.type == 'other':
167 167 sts = self.callObject(operationConf.id, **kwargs)
168 168
169 169 return sts
170 170
171 171 def setInput(self, dataIn):
172 172
173 173 self.dataIn = dataIn
174 174
175 175 def getOutput(self):
176 176
177 177 return self.dataOut
178 178
179 179 class Operation():
180 180
181 181 """
182 182 Clase base para definir las operaciones adicionales que se pueden agregar a la clase ProcessingUnit
183 183 y necesiten acumular informacion previa de los datos a procesar. De preferencia usar un buffer de
184 184 acumulacion dentro de esta clase
185 185
186 186 Ejemplo: Integraciones coherentes, necesita la informacion previa de los n perfiles anteriores (bufffer)
187 187
188 188 """
189 189
190 190 __buffer = None
191 191 __isConfig = False
192 192
193 193 def __init__(self):
194 194
195 195 pass
196 196
197 197 def run(self, dataIn, **kwargs):
198 198
199 199 """
200 200 Realiza las operaciones necesarias sobre la dataIn.data y actualiza los atributos del objeto dataIn.
201 201
202 202 Input:
203 203
204 204 dataIn : objeto del tipo JROData
205 205
206 206 Return:
207 207
208 208 None
209 209
210 210 Affected:
211 211 __buffer : buffer de recepcion de datos.
212 212
213 213 """
214 214
215 215 raise ValueError, "ImplementedError"
216 216
217 217 class VoltageProc(ProcessingUnit):
218 218
219 219
220 220 def __init__(self):
221 221
222 222 self.objectDict = {}
223 223 self.dataOut = Voltage()
224 224 self.flip = 1
225 225
226 226 def init(self):
227 227
228 228 self.dataOut.copy(self.dataIn)
229 229 # No necesita copiar en cada init() los atributos de dataIn
230 230 # la copia deberia hacerse por cada nuevo bloque de datos
231 231
232 232 def selectChannels(self, channelList):
233 233
234 234 channelIndexList = []
235 235
236 236 for channel in channelList:
237 237 index = self.dataOut.channelList.index(channel)
238 238 channelIndexList.append(index)
239 239
240 240 self.selectChannelsByIndex(channelIndexList)
241 241
242 242 def selectChannelsByIndex(self, channelIndexList):
243 243 """
244 244 Selecciona un bloque de datos en base a canales segun el channelIndexList
245 245
246 246 Input:
247 247 channelIndexList : lista sencilla de canales a seleccionar por ej. [2,3,7]
248 248
249 249 Affected:
250 250 self.dataOut.data
251 251 self.dataOut.channelIndexList
252 252 self.dataOut.nChannels
253 253 self.dataOut.m_ProcessingHeader.totalSpectra
254 254 self.dataOut.systemHeaderObj.numChannels
255 255 self.dataOut.m_ProcessingHeader.blockSize
256 256
257 257 Return:
258 258 None
259 259 """
260 260
261 261 for channelIndex in channelIndexList:
262 262 if channelIndex not in self.dataOut.channelIndexList:
263 263 print channelIndexList
264 264 raise ValueError, "The value %d in channelIndexList is not valid" %channelIndex
265 265
266 266 nChannels = len(channelIndexList)
267 267
268 268 data = self.dataOut.data[channelIndexList,:]
269 269
270 270 self.dataOut.data = data
271 271 self.dataOut.channelList = [self.dataOut.channelList[i] for i in channelIndexList]
272 272 # self.dataOut.nChannels = nChannels
273 273
274 274 return 1
275 275
276 276 def selectHeights(self, minHei=None, maxHei=None):
277 277 """
278 278 Selecciona un bloque de datos en base a un grupo de valores de alturas segun el rango
279 279 minHei <= height <= maxHei
280 280
281 281 Input:
282 282 minHei : valor minimo de altura a considerar
283 283 maxHei : valor maximo de altura a considerar
284 284
285 285 Affected:
286 286 Indirectamente son cambiados varios valores a travez del metodo selectHeightsByIndex
287 287
288 288 Return:
289 289 1 si el metodo se ejecuto con exito caso contrario devuelve 0
290 290 """
291 291
292 292 if minHei == None:
293 293 minHei = self.dataOut.heightList[0]
294 294
295 295 if maxHei == None:
296 296 maxHei = self.dataOut.heightList[-1]
297 297
298 298 if (minHei < self.dataOut.heightList[0]) or (minHei > maxHei):
299 299 raise ValueError, "some value in (%d,%d) is not valid" % (minHei, maxHei)
300 300
301 301
302 302 if (maxHei > self.dataOut.heightList[-1]):
303 303 maxHei = self.dataOut.heightList[-1]
304 304 # raise ValueError, "some value in (%d,%d) is not valid" % (minHei, maxHei)
305 305
306 306 minIndex = 0
307 307 maxIndex = 0
308 308 heights = self.dataOut.heightList
309 309
310 310 inda = numpy.where(heights >= minHei)
311 311 indb = numpy.where(heights <= maxHei)
312 312
313 313 try:
314 314 minIndex = inda[0][0]
315 315 except:
316 316 minIndex = 0
317 317
318 318 try:
319 319 maxIndex = indb[0][-1]
320 320 except:
321 321 maxIndex = len(heights)
322 322
323 323 self.selectHeightsByIndex(minIndex, maxIndex)
324 324
325 325 return 1
326 326
327 327
328 328 def selectHeightsByIndex(self, minIndex, maxIndex):
329 329 """
330 330 Selecciona un bloque de datos en base a un grupo indices de alturas segun el rango
331 331 minIndex <= index <= maxIndex
332 332
333 333 Input:
334 334 minIndex : valor de indice minimo de altura a considerar
335 335 maxIndex : valor de indice maximo de altura a considerar
336 336
337 337 Affected:
338 338 self.dataOut.data
339 339 self.dataOut.heightList
340 340
341 341 Return:
342 342 1 si el metodo se ejecuto con exito caso contrario devuelve 0
343 343 """
344 344
345 345 if (minIndex < 0) or (minIndex > maxIndex):
346 346 raise ValueError, "some value in (%d,%d) is not valid" % (minIndex, maxIndex)
347 347
348 348 if (maxIndex >= self.dataOut.nHeights):
349 349 maxIndex = self.dataOut.nHeights-1
350 350 # raise ValueError, "some value in (%d,%d) is not valid" % (minIndex, maxIndex)
351 351
352 352 nHeights = maxIndex - minIndex + 1
353 353
354 354 #voltage
355 355 data = self.dataOut.data[:,minIndex:maxIndex+1]
356 356
357 357 firstHeight = self.dataOut.heightList[minIndex]
358 358
359 359 self.dataOut.data = data
360 360 self.dataOut.heightList = self.dataOut.heightList[minIndex:maxIndex+1]
361 361
362 362 return 1
363 363
364 364
365 365 def filterByHeights(self, window):
366 366 deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0]
367 367
368 368 if window == None:
369 369 window = (self.dataOut.radarControllerHeaderObj.txA/self.dataOut.radarControllerHeaderObj.nBaud) / deltaHeight
370 370
371 371 newdelta = deltaHeight * window
372 372 r = self.dataOut.data.shape[1] % window
373 373 buffer = self.dataOut.data[:,0:self.dataOut.data.shape[1]-r]
374 374 buffer = buffer.reshape(self.dataOut.data.shape[0],self.dataOut.data.shape[1]/window,window)
375 375 buffer = numpy.sum(buffer,2)
376 376 self.dataOut.data = buffer
377 377 self.dataOut.heightList = numpy.arange(self.dataOut.heightList[0],newdelta*(self.dataOut.nHeights-r)/window,newdelta)
378 378 self.dataOut.windowOfFilter = window
379 379
380 380 def deFlip(self):
381 381 self.dataOut.data *= self.flip
382 382 self.flip *= -1.
383 383
384 384 def setRadarFrequency(self, frequency=None):
385 385 if frequency != None:
386 386 self.dataOut.frequency = frequency
387 387
388 388 return 1
389 389
390 390 class CohInt(Operation):
391 391
392 392 __isConfig = False
393 393
394 394 __profIndex = 0
395 395 __withOverapping = False
396 396
397 397 __byTime = False
398 398 __initime = None
399 399 __lastdatatime = None
400 400 __integrationtime = None
401 401
402 402 __buffer = None
403 403
404 404 __dataReady = False
405 405
406 406 n = None
407 407
408 408
409 409 def __init__(self):
410 410
411 411 self.__isConfig = False
412 412
413 413 def setup(self, n=None, timeInterval=None, overlapping=False):
414 414 """
415 415 Set the parameters of the integration class.
416 416
417 417 Inputs:
418 418
419 419 n : Number of coherent integrations
420 420 timeInterval : Time of integration. If the parameter "n" is selected this one does not work
421 421 overlapping :
422 422
423 423 """
424 424
425 425 self.__initime = None
426 426 self.__lastdatatime = 0
427 427 self.__buffer = None
428 428 self.__dataReady = False
429 429
430 430
431 431 if n == None and timeInterval == None:
432 432 raise ValueError, "n or timeInterval should be specified ..."
433 433
434 434 if n != None:
435 435 self.n = n
436 436 self.__byTime = False
437 437 else:
438 438 self.__integrationtime = timeInterval * 60. #if (type(timeInterval)!=integer) -> change this line
439 439 self.n = 9999
440 440 self.__byTime = True
441 441
442 442 if overlapping:
443 443 self.__withOverapping = True
444 444 self.__buffer = None
445 445 else:
446 446 self.__withOverapping = False
447 447 self.__buffer = 0
448 448
449 449 self.__profIndex = 0
450 450
451 451 def putData(self, data):
452 452
453 453 """
454 454 Add a profile to the __buffer and increase in one the __profileIndex
455 455
456 456 """
457 457
458 458 if not self.__withOverapping:
459 459 self.__buffer += data.copy()
460 460 self.__profIndex += 1
461 461 return
462 462
463 463 #Overlapping data
464 464 nChannels, nHeis = data.shape
465 465 data = numpy.reshape(data, (1, nChannels, nHeis))
466 466
467 467 #If the buffer is empty then it takes the data value
468 468 if self.__buffer == None:
469 469 self.__buffer = data
470 470 self.__profIndex += 1
471 471 return
472 472
473 473 #If the buffer length is lower than n then stakcing the data value
474 474 if self.__profIndex < self.n:
475 475 self.__buffer = numpy.vstack((self.__buffer, data))
476 476 self.__profIndex += 1
477 477 return
478 478
479 479 #If the buffer length is equal to n then replacing the last buffer value with the data value
480 480 self.__buffer = numpy.roll(self.__buffer, -1, axis=0)
481 481 self.__buffer[self.n-1] = data
482 482 self.__profIndex = self.n
483 483 return
484 484
485 485
486 486 def pushData(self):
487 487 """
488 488 Return the sum of the last profiles and the profiles used in the sum.
489 489
490 490 Affected:
491 491
492 492 self.__profileIndex
493 493
494 494 """
495 495
496 496 if not self.__withOverapping:
497 497 data = self.__buffer
498 498 n = self.__profIndex
499 499
500 500 self.__buffer = 0
501 501 self.__profIndex = 0
502 502
503 503 return data, n
504 504
505 505 #Integration with Overlapping
506 506 data = numpy.sum(self.__buffer, axis=0)
507 507 n = self.__profIndex
508 508
509 509 return data, n
510 510
511 511 def byProfiles(self, data):
512 512
513 513 self.__dataReady = False
514 514 avgdata = None
515 515 n = None
516 516
517 517 self.putData(data)
518 518
519 519 if self.__profIndex == self.n:
520 520
521 521 avgdata, n = self.pushData()
522 522 self.__dataReady = True
523 523
524 524 return avgdata
525 525
526 526 def byTime(self, data, datatime):
527 527
528 528 self.__dataReady = False
529 529 avgdata = None
530 530 n = None
531 531
532 532 self.putData(data)
533 533
534 534 if (datatime - self.__initime) >= self.__integrationtime:
535 535 avgdata, n = self.pushData()
536 536 self.n = n
537 537 self.__dataReady = True
538 538
539 539 return avgdata
540 540
541 541 def integrate(self, data, datatime=None):
542 542
543 543 if self.__initime == None:
544 544 self.__initime = datatime
545 545
546 546 if self.__byTime:
547 547 avgdata = self.byTime(data, datatime)
548 548 else:
549 549 avgdata = self.byProfiles(data)
550 550
551 551
552 552 self.__lastdatatime = datatime
553 553
554 554 if avgdata == None:
555 555 return None, None
556 556
557 557 avgdatatime = self.__initime
558 558
559 559 deltatime = datatime -self.__lastdatatime
560 560
561 561 if not self.__withOverapping:
562 562 self.__initime = datatime
563 563 else:
564 564 self.__initime += deltatime
565 565
566 566 return avgdata, avgdatatime
567 567
568 568 def run(self, dataOut, **kwargs):
569 569
570 570 if not self.__isConfig:
571 571 self.setup(**kwargs)
572 572 self.__isConfig = True
573 573
574 574 avgdata, avgdatatime = self.integrate(dataOut.data, dataOut.utctime)
575 575
576 576 # dataOut.timeInterval *= n
577 577 dataOut.flagNoData = True
578 578
579 579 if self.__dataReady:
580 580 dataOut.data = avgdata
581 581 dataOut.nCohInt *= self.n
582 582 dataOut.utctime = avgdatatime
583 583 dataOut.timeInterval = dataOut.ippSeconds * dataOut.nCohInt
584 584 dataOut.flagNoData = False
585 585
586 586
587 587 class Decoder(Operation):
588 588
589 589 __isConfig = False
590 590 __profIndex = 0
591 591
592 592 code = None
593 593
594 594 nCode = None
595 595 nBaud = None
596 596
597 597 def __init__(self):
598 598
599 599 self.__isConfig = False
600 600
601 601 def setup(self, code, shape):
602 602
603 603 self.__profIndex = 0
604 604
605 605 self.code = code
606 606
607 607 self.nCode = len(code)
608 608 self.nBaud = len(code[0])
609 609
610 610 self.__nChannels, self.__nHeis = shape
611 611
612 612 __codeBuffer = numpy.zeros((self.nCode, self.__nHeis), dtype=numpy.complex)
613 613
614 614 __codeBuffer[:,0:self.nBaud] = self.code
615 615
616 616 self.fft_code = numpy.conj(numpy.fft.fft(__codeBuffer, axis=1))
617 617
618 618 self.ndatadec = self.__nHeis - self.nBaud + 1
619 619
620 620 self.datadecTime = numpy.zeros((self.__nChannels, self.ndatadec), dtype=numpy.complex)
621 621
622 622 def convolutionInFreq(self, data):
623 623
624 624 fft_code = self.fft_code[self.__profIndex].reshape(1,-1)
625 625
626 626 fft_data = numpy.fft.fft(data, axis=1)
627 627
628 628 conv = fft_data*fft_code
629 629
630 630 data = numpy.fft.ifft(conv,axis=1)
631 631
632 632 datadec = data[:,:-self.nBaud+1]
633 633
634 634 return datadec
635 635
636 636 def convolutionInFreqOpt(self, data):
637 637
638 638 fft_code = self.fft_code[self.__profIndex].reshape(1,-1)
639 639
640 640 data = cfunctions.decoder(fft_code, data)
641 641
642 642 datadec = data[:,:-self.nBaud+1]
643 643
644 644 return datadec
645 645
646 646 def convolutionInTime(self, data):
647 647
648 648 code = self.code[self.__profIndex]
649 649
650 650 for i in range(self.__nChannels):
651 651 self.datadecTime[i,:] = numpy.correlate(data[i,:], code, mode='valid')
652 652
653 653 return self.datadecTime
654 654
655 655 def run(self, dataOut, code=None, nCode=None, nBaud=None, mode = 0):
656 656
657 657 if not self.__isConfig:
658 658
659 659 if code == None:
660 660 code = dataOut.code
661 661 else:
662 662 code = numpy.array(code).reshape(nCode,nBaud)
663 663 dataOut.code = code
664 664 dataOut.nCode = nCode
665 665 dataOut.nBaud = nBaud
666 666
667 667 if code == None:
668 668 return 1
669 669
670 670 self.setup(code, dataOut.data.shape)
671 671 self.__isConfig = True
672 672
673 673 if mode == 0:
674 674 datadec = self.convolutionInTime(dataOut.data)
675 675
676 676 if mode == 1:
677 677 datadec = self.convolutionInFreq(dataOut.data)
678 678
679 679 if mode == 2:
680 680 datadec = self.convolutionInFreqOpt(dataOut.data)
681 681
682 682 dataOut.data = datadec
683 683
684 684 dataOut.heightList = dataOut.heightList[0:self.ndatadec]
685 685
686 686 dataOut.flagDecodeData = True #asumo q la data no esta decodificada
687 687
688 688 if self.__profIndex == self.nCode-1:
689 689 self.__profIndex = 0
690 690 return 1
691 691
692 692 self.__profIndex += 1
693 693
694 694 return 1
695 695 # dataOut.flagDeflipData = True #asumo q la data no esta sin flip
696 696
697 697
698 698
699 699 class SpectraProc(ProcessingUnit):
700 700
701 701 def __init__(self):
702 702
703 703 self.objectDict = {}
704 704 self.buffer = None
705 705 self.firstdatatime = None
706 706 self.profIndex = 0
707 707 self.dataOut = Spectra()
708 708
709 709 def __updateObjFromInput(self):
710 710
711 711 self.dataOut.timeZone = self.dataIn.timeZone
712 712 self.dataOut.dstFlag = self.dataIn.dstFlag
713 713 self.dataOut.errorCount = self.dataIn.errorCount
714 714 self.dataOut.useLocalTime = self.dataIn.useLocalTime
715 715
716 716 self.dataOut.radarControllerHeaderObj = self.dataIn.radarControllerHeaderObj.copy()
717 717 self.dataOut.systemHeaderObj = self.dataIn.systemHeaderObj.copy()
718 718 self.dataOut.channelList = self.dataIn.channelList
719 719 self.dataOut.heightList = self.dataIn.heightList
720 720 self.dataOut.dtype = numpy.dtype([('real','<f4'),('imag','<f4')])
721 721 # self.dataOut.nHeights = self.dataIn.nHeights
722 722 # self.dataOut.nChannels = self.dataIn.nChannels
723 723 self.dataOut.nBaud = self.dataIn.nBaud
724 724 self.dataOut.nCode = self.dataIn.nCode
725 725 self.dataOut.code = self.dataIn.code
726 726 self.dataOut.nProfiles = self.dataOut.nFFTPoints
727 727 # self.dataOut.channelIndexList = self.dataIn.channelIndexList
728 728 self.dataOut.flagTimeBlock = self.dataIn.flagTimeBlock
729 729 self.dataOut.utctime = self.firstdatatime
730 730 self.dataOut.flagDecodeData = self.dataIn.flagDecodeData #asumo q la data esta decodificada
731 731 self.dataOut.flagDeflipData = self.dataIn.flagDeflipData #asumo q la data esta sin flip
732 732 # self.dataOut.flagShiftFFT = self.dataIn.flagShiftFFT
733 733 self.dataOut.nCohInt = self.dataIn.nCohInt
734 734 self.dataOut.nIncohInt = 1
735 735 self.dataOut.ippSeconds = self.dataIn.ippSeconds
736 736 self.dataOut.windowOfFilter = self.dataIn.windowOfFilter
737 737
738 738 self.dataOut.timeInterval = self.dataIn.timeInterval*self.dataOut.nFFTPoints*self.dataOut.nIncohInt
739 739 self.dataOut.frequency = self.dataIn.frequency
740 740 self.dataOut.realtime = self.dataIn.realtime
741 741
742 742 def __getFft(self):
743 743 """
744 744 Convierte valores de Voltaje a Spectra
745 745
746 746 Affected:
747 747 self.dataOut.data_spc
748 748 self.dataOut.data_cspc
749 749 self.dataOut.data_dc
750 750 self.dataOut.heightList
751 751 self.profIndex
752 752 self.buffer
753 753 self.dataOut.flagNoData
754 754 """
755 755 fft_volt = numpy.fft.fft(self.buffer,n=self.dataOut.nFFTPoints,axis=1)
756 756 fft_volt = fft_volt.astype(numpy.dtype('complex'))
757 757 dc = fft_volt[:,0,:]
758 758
759 759 #calculo de self-spectra
760 760 fft_volt = numpy.fft.fftshift(fft_volt,axes=(1,))
761 761 spc = fft_volt * numpy.conjugate(fft_volt)
762 762 spc = spc.real
763 763
764 764 blocksize = 0
765 765 blocksize += dc.size
766 766 blocksize += spc.size
767 767
768 768 cspc = None
769 769 pairIndex = 0
770 770 if self.dataOut.pairsList != None:
771 771 #calculo de cross-spectra
772 772 cspc = numpy.zeros((self.dataOut.nPairs, self.dataOut.nFFTPoints, self.dataOut.nHeights), dtype='complex')
773 773 for pair in self.dataOut.pairsList:
774 774 cspc[pairIndex,:,:] = fft_volt[pair[0],:,:] * numpy.conjugate(fft_volt[pair[1],:,:])
775 775 pairIndex += 1
776 776 blocksize += cspc.size
777 777
778 778 self.dataOut.data_spc = spc
779 779 self.dataOut.data_cspc = cspc
780 780 self.dataOut.data_dc = dc
781 781 self.dataOut.blockSize = blocksize
782 782 self.dataOut.flagShiftFFT = False
783 783
784 784 def init(self, nProfiles=None, nFFTPoints=None, pairsList=None):
785 785
786 786 self.dataOut.flagNoData = True
787 787
788 788 if self.dataIn.type == "Spectra":
789 789 self.dataOut.copy(self.dataIn)
790 790 return
791 791
792 792 if self.dataIn.type == "Voltage":
793 793
794 794 if nFFTPoints == None:
795 795 raise ValueError, "This SpectraProc.init() need nFFTPoints input variable"
796 796
797 797 if pairsList == None:
798 798 nPairs = 0
799 799 else:
800 800 nPairs = len(pairsList)
801 801
802 802 self.dataOut.nFFTPoints = nFFTPoints
803 803 self.dataOut.pairsList = pairsList
804 804 self.dataOut.nPairs = nPairs
805 805
806 806 if self.buffer == None:
807 807 self.buffer = numpy.zeros((self.dataIn.nChannels,
808 808 nProfiles,
809 809 self.dataIn.nHeights),
810 810 dtype='complex')
811 811
812 812
813 813 self.buffer[:,self.profIndex,:] = self.dataIn.data.copy()
814 814 self.profIndex += 1
815 815
816 816 if self.firstdatatime == None:
817 817 self.firstdatatime = self.dataIn.utctime
818 818
819 if self.profIndex == self.dataOut.nFFTPoints:
819 if self.profIndex == nProfiles:
820 820 self.__updateObjFromInput()
821 821 self.__getFft()
822 822
823 823 self.dataOut.flagNoData = False
824 824
825 825 self.buffer = None
826 826 self.firstdatatime = None
827 827 self.profIndex = 0
828 828
829 829 return
830 830
831 831 raise ValuError, "The type object %s is not valid"%(self.dataIn.type)
832 832
833 833 def selectChannels(self, channelList):
834 834
835 835 channelIndexList = []
836 836
837 837 for channel in channelList:
838 838 index = self.dataOut.channelList.index(channel)
839 839 channelIndexList.append(index)
840 840
841 841 self.selectChannelsByIndex(channelIndexList)
842 842
843 843 def selectChannelsByIndex(self, channelIndexList):
844 844 """
845 845 Selecciona un bloque de datos en base a canales segun el channelIndexList
846 846
847 847 Input:
848 848 channelIndexList : lista sencilla de canales a seleccionar por ej. [2,3,7]
849 849
850 850 Affected:
851 851 self.dataOut.data_spc
852 852 self.dataOut.channelIndexList
853 853 self.dataOut.nChannels
854 854
855 855 Return:
856 856 None
857 857 """
858 858
859 859 for channelIndex in channelIndexList:
860 860 if channelIndex not in self.dataOut.channelIndexList:
861 861 print channelIndexList
862 862 raise ValueError, "The value %d in channelIndexList is not valid" %channelIndex
863 863
864 864 nChannels = len(channelIndexList)
865 865
866 866 data_spc = self.dataOut.data_spc[channelIndexList,:]
867 867
868 868 self.dataOut.data_spc = data_spc
869 869 self.dataOut.channelList = [self.dataOut.channelList[i] for i in channelIndexList]
870 870 # self.dataOut.nChannels = nChannels
871 871
872 872 return 1
873 873
874 874 def selectHeights(self, minHei, maxHei):
875 875 """
876 876 Selecciona un bloque de datos en base a un grupo de valores de alturas segun el rango
877 877 minHei <= height <= maxHei
878 878
879 879 Input:
880 880 minHei : valor minimo de altura a considerar
881 881 maxHei : valor maximo de altura a considerar
882 882
883 883 Affected:
884 884 Indirectamente son cambiados varios valores a travez del metodo selectHeightsByIndex
885 885
886 886 Return:
887 887 1 si el metodo se ejecuto con exito caso contrario devuelve 0
888 888 """
889 889 if (minHei < self.dataOut.heightList[0]) or (minHei > maxHei):
890 890 raise ValueError, "some value in (%d,%d) is not valid" % (minHei, maxHei)
891 891
892 892 if (maxHei > self.dataOut.heightList[-1]):
893 893 maxHei = self.dataOut.heightList[-1]
894 894 # raise ValueError, "some value in (%d,%d) is not valid" % (minHei, maxHei)
895 895
896 896 minIndex = 0
897 897 maxIndex = 0
898 898 heights = self.dataOut.heightList
899 899
900 900 inda = numpy.where(heights >= minHei)
901 901 indb = numpy.where(heights <= maxHei)
902 902
903 903 try:
904 904 minIndex = inda[0][0]
905 905 except:
906 906 minIndex = 0
907 907
908 908 try:
909 909 maxIndex = indb[0][-1]
910 910 except:
911 911 maxIndex = len(heights)
912 912
913 913 self.selectHeightsByIndex(minIndex, maxIndex)
914 914
915 915 return 1
916 916
917 917
918 918 def selectHeightsByIndex(self, minIndex, maxIndex):
919 919 """
920 920 Selecciona un bloque de datos en base a un grupo indices de alturas segun el rango
921 921 minIndex <= index <= maxIndex
922 922
923 923 Input:
924 924 minIndex : valor de indice minimo de altura a considerar
925 925 maxIndex : valor de indice maximo de altura a considerar
926 926
927 927 Affected:
928 928 self.dataOut.data_spc
929 929 self.dataOut.data_cspc
930 930 self.dataOut.data_dc
931 931 self.dataOut.heightList
932 932
933 933 Return:
934 934 1 si el metodo se ejecuto con exito caso contrario devuelve 0
935 935 """
936 936
937 937 if (minIndex < 0) or (minIndex > maxIndex):
938 938 raise ValueError, "some value in (%d,%d) is not valid" % (minIndex, maxIndex)
939 939
940 940 if (maxIndex >= self.dataOut.nHeights):
941 941 maxIndex = self.dataOut.nHeights-1
942 942 # raise ValueError, "some value in (%d,%d) is not valid" % (minIndex, maxIndex)
943 943
944 944 nHeights = maxIndex - minIndex + 1
945 945
946 946 #Spectra
947 947 data_spc = self.dataOut.data_spc[:,:,minIndex:maxIndex+1]
948 948
949 949 data_cspc = None
950 950 if self.dataOut.data_cspc != None:
951 951 data_cspc = self.dataOut.data_cspc[:,:,minIndex:maxIndex+1]
952 952
953 953 data_dc = None
954 954 if self.dataOut.data_dc != None:
955 955 data_dc = self.dataOut.data_dc[:,minIndex:maxIndex+1]
956 956
957 957 self.dataOut.data_spc = data_spc
958 958 self.dataOut.data_cspc = data_cspc
959 959 self.dataOut.data_dc = data_dc
960 960
961 961 self.dataOut.heightList = self.dataOut.heightList[minIndex:maxIndex+1]
962 962
963 963 return 1
964 964
965 965 def removeDC(self, mode = 1):
966 966
967 967 dc_index = 0
968 968 freq_index = numpy.array([-2,-1,1,2])
969 969 data_spc = self.dataOut.data_spc
970 970 data_cspc = self.dataOut.data_cspc
971 971 data_dc = self.dataOut.data_dc
972 972
973 973 if self.dataOut.flagShiftFFT:
974 974 dc_index += self.dataOut.nFFTPoints/2
975 975 freq_index += self.dataOut.nFFTPoints/2
976 976
977 977 if mode == 1:
978 978 data_spc[dc_index] = (data_spc[:,freq_index[1],:] + data_spc[:,freq_index[2],:])/2
979 979 if data_cspc != None:
980 980 data_cspc[dc_index] = (data_cspc[:,freq_index[1],:] + data_cspc[:,freq_index[2],:])/2
981 981 return 1
982 982
983 983 if mode == 2:
984 984 pass
985 985
986 986 if mode == 3:
987 987 pass
988 988
989 989 raise ValueError, "mode parameter has to be 1, 2 or 3"
990 990
991 991 def removeInterference(self):
992 992
993 993 pass
994 994
995 995 def setRadarFrequency(self, frequency=None):
996 996 if frequency != None:
997 997 self.dataOut.frequency = frequency
998 998
999 999 return 1
1000 1000
1001 1001
1002 1002 class IncohInt(Operation):
1003 1003
1004 1004
1005 1005 __profIndex = 0
1006 1006 __withOverapping = False
1007 1007
1008 1008 __byTime = False
1009 1009 __initime = None
1010 1010 __lastdatatime = None
1011 1011 __integrationtime = None
1012 1012
1013 1013 __buffer_spc = None
1014 1014 __buffer_cspc = None
1015 1015 __buffer_dc = None
1016 1016
1017 1017 __dataReady = False
1018 1018
1019 1019 __timeInterval = None
1020 1020
1021 1021 n = None
1022 1022
1023 1023
1024 1024
1025 1025 def __init__(self):
1026 1026
1027 1027 self.__isConfig = False
1028 1028
1029 1029 def setup(self, n=None, timeInterval=None, overlapping=False):
1030 1030 """
1031 1031 Set the parameters of the integration class.
1032 1032
1033 1033 Inputs:
1034 1034
1035 1035 n : Number of coherent integrations
1036 1036 timeInterval : Time of integration. If the parameter "n" is selected this one does not work
1037 1037 overlapping :
1038 1038
1039 1039 """
1040 1040
1041 1041 self.__initime = None
1042 1042 self.__lastdatatime = 0
1043 1043 self.__buffer_spc = None
1044 1044 self.__buffer_cspc = None
1045 1045 self.__buffer_dc = None
1046 1046 self.__dataReady = False
1047 1047
1048 1048
1049 1049 if n == None and timeInterval == None:
1050 1050 raise ValueError, "n or timeInterval should be specified ..."
1051 1051
1052 1052 if n != None:
1053 1053 self.n = n
1054 1054 self.__byTime = False
1055 1055 else:
1056 1056 self.__integrationtime = timeInterval #if (type(timeInterval)!=integer) -> change this line
1057 1057 self.n = 9999
1058 1058 self.__byTime = True
1059 1059
1060 1060 if overlapping:
1061 1061 self.__withOverapping = True
1062 1062 else:
1063 1063 self.__withOverapping = False
1064 1064 self.__buffer_spc = 0
1065 1065 self.__buffer_cspc = 0
1066 1066 self.__buffer_dc = 0
1067 1067
1068 1068 self.__profIndex = 0
1069 1069
1070 1070 def putData(self, data_spc, data_cspc, data_dc):
1071 1071
1072 1072 """
1073 1073 Add a profile to the __buffer_spc and increase in one the __profileIndex
1074 1074
1075 1075 """
1076 1076
1077 1077 if not self.__withOverapping:
1078 1078 self.__buffer_spc += data_spc
1079 1079
1080 1080 if data_cspc == None:
1081 1081 self.__buffer_cspc = None
1082 1082 else:
1083 1083 self.__buffer_cspc += data_cspc
1084 1084
1085 1085 if data_dc == None:
1086 1086 self.__buffer_dc = None
1087 1087 else:
1088 1088 self.__buffer_dc += data_dc
1089 1089
1090 1090 self.__profIndex += 1
1091 1091 return
1092 1092
1093 1093 #Overlapping data
1094 1094 nChannels, nFFTPoints, nHeis = data_spc.shape
1095 1095 data_spc = numpy.reshape(data_spc, (1, nChannels, nFFTPoints, nHeis))
1096 1096 if data_cspc != None:
1097 1097 data_cspc = numpy.reshape(data_cspc, (1, -1, nFFTPoints, nHeis))
1098 1098 if data_dc != None:
1099 1099 data_dc = numpy.reshape(data_dc, (1, -1, nHeis))
1100 1100
1101 1101 #If the buffer is empty then it takes the data value
1102 1102 if self.__buffer_spc == None:
1103 1103 self.__buffer_spc = data_spc
1104 1104
1105 1105 if data_cspc == None:
1106 1106 self.__buffer_cspc = None
1107 1107 else:
1108 1108 self.__buffer_cspc += data_cspc
1109 1109
1110 1110 if data_dc == None:
1111 1111 self.__buffer_dc = None
1112 1112 else:
1113 1113 self.__buffer_dc += data_dc
1114 1114
1115 1115 self.__profIndex += 1
1116 1116 return
1117 1117
1118 1118 #If the buffer length is lower than n then stakcing the data value
1119 1119 if self.__profIndex < self.n:
1120 1120 self.__buffer_spc = numpy.vstack((self.__buffer_spc, data_spc))
1121 1121
1122 1122 if data_cspc != None:
1123 1123 self.__buffer_cspc = numpy.vstack((self.__buffer_cspc, data_cspc))
1124 1124
1125 1125 if data_dc != None:
1126 1126 self.__buffer_dc = numpy.vstack((self.__buffer_dc, data_dc))
1127 1127
1128 1128 self.__profIndex += 1
1129 1129 return
1130 1130
1131 1131 #If the buffer length is equal to n then replacing the last buffer value with the data value
1132 1132 self.__buffer_spc = numpy.roll(self.__buffer_spc, -1, axis=0)
1133 1133 self.__buffer_spc[self.n-1] = data_spc
1134 1134
1135 1135 if data_cspc != None:
1136 1136 self.__buffer_cspc = numpy.roll(self.__buffer_cspc, -1, axis=0)
1137 1137 self.__buffer_cspc[self.n-1] = data_cspc
1138 1138
1139 1139 if data_dc != None:
1140 1140 self.__buffer_dc = numpy.roll(self.__buffer_dc, -1, axis=0)
1141 1141 self.__buffer_dc[self.n-1] = data_dc
1142 1142
1143 1143 self.__profIndex = self.n
1144 1144 return
1145 1145
1146 1146
1147 1147 def pushData(self):
1148 1148 """
1149 1149 Return the sum of the last profiles and the profiles used in the sum.
1150 1150
1151 1151 Affected:
1152 1152
1153 1153 self.__profileIndex
1154 1154
1155 1155 """
1156 1156 data_spc = None
1157 1157 data_cspc = None
1158 1158 data_dc = None
1159 1159
1160 1160 if not self.__withOverapping:
1161 1161 data_spc = self.__buffer_spc
1162 1162 data_cspc = self.__buffer_cspc
1163 1163 data_dc = self.__buffer_dc
1164 1164
1165 1165 n = self.__profIndex
1166 1166
1167 1167 self.__buffer_spc = 0
1168 1168 self.__buffer_cspc = 0
1169 1169 self.__buffer_dc = 0
1170 1170 self.__profIndex = 0
1171 1171
1172 1172 return data_spc, data_cspc, data_dc, n
1173 1173
1174 1174 #Integration with Overlapping
1175 1175 data_spc = numpy.sum(self.__buffer_spc, axis=0)
1176 1176
1177 1177 if self.__buffer_cspc != None:
1178 1178 data_cspc = numpy.sum(self.__buffer_cspc, axis=0)
1179 1179
1180 1180 if self.__buffer_dc != None:
1181 1181 data_dc = numpy.sum(self.__buffer_dc, axis=0)
1182 1182
1183 1183 n = self.__profIndex
1184 1184
1185 1185 return data_spc, data_cspc, data_dc, n
1186 1186
1187 1187 def byProfiles(self, *args):
1188 1188
1189 1189 self.__dataReady = False
1190 1190 avgdata_spc = None
1191 1191 avgdata_cspc = None
1192 1192 avgdata_dc = None
1193 1193 n = None
1194 1194
1195 1195 self.putData(*args)
1196 1196
1197 1197 if self.__profIndex == self.n:
1198 1198
1199 1199 avgdata_spc, avgdata_cspc, avgdata_dc, n = self.pushData()
1200 1200 self.__dataReady = True
1201 1201
1202 1202 return avgdata_spc, avgdata_cspc, avgdata_dc
1203 1203
1204 1204 def byTime(self, datatime, *args):
1205 1205
1206 1206 self.__dataReady = False
1207 1207 avgdata_spc = None
1208 1208 avgdata_cspc = None
1209 1209 avgdata_dc = None
1210 1210 n = None
1211 1211
1212 1212 self.putData(*args)
1213 1213
1214 1214 if (datatime - self.__initime) >= self.__integrationtime:
1215 1215 avgdata_spc, avgdata_cspc, avgdata_dc, n = self.pushData()
1216 1216 self.n = n
1217 1217 self.__dataReady = True
1218 1218
1219 1219 return avgdata_spc, avgdata_cspc, avgdata_dc
1220 1220
1221 1221 def integrate(self, datatime, *args):
1222 1222
1223 1223 if self.__initime == None:
1224 1224 self.__initime = datatime
1225 1225
1226 1226 if self.__byTime:
1227 1227 avgdata_spc, avgdata_cspc, avgdata_dc = self.byTime(datatime, *args)
1228 1228 else:
1229 1229 avgdata_spc, avgdata_cspc, avgdata_dc = self.byProfiles(*args)
1230 1230
1231 1231 self.__lastdatatime = datatime
1232 1232
1233 1233 if avgdata_spc == None:
1234 1234 return None, None, None, None
1235 1235
1236 1236 avgdatatime = self.__initime
1237 1237 try:
1238 1238 self.__timeInterval = (self.__lastdatatime - self.__initime)/(self.n - 1)
1239 1239 except:
1240 1240 self.__timeInterval = self.__lastdatatime - self.__initime
1241 1241
1242 1242 deltatime = datatime -self.__lastdatatime
1243 1243
1244 1244 if not self.__withOverapping:
1245 1245 self.__initime = datatime
1246 1246 else:
1247 1247 self.__initime += deltatime
1248 1248
1249 1249 return avgdatatime, avgdata_spc, avgdata_cspc, avgdata_dc
1250 1250
1251 1251 def run(self, dataOut, n=None, timeInterval=None, overlapping=False):
1252 1252
1253 1253 if n==1:
1254 1254 dataOut.flagNoData = False
1255 1255 return
1256 1256
1257 1257 if not self.__isConfig:
1258 1258 self.setup(n, timeInterval, overlapping)
1259 1259 self.__isConfig = True
1260 1260
1261 1261 avgdatatime, avgdata_spc, avgdata_cspc, avgdata_dc = self.integrate(dataOut.utctime,
1262 1262 dataOut.data_spc,
1263 1263 dataOut.data_cspc,
1264 1264 dataOut.data_dc)
1265 1265
1266 1266 # dataOut.timeInterval *= n
1267 1267 dataOut.flagNoData = True
1268 1268
1269 1269 if self.__dataReady:
1270 1270
1271 1271 dataOut.data_spc = avgdata_spc
1272 1272 dataOut.data_cspc = avgdata_cspc
1273 1273 dataOut.data_dc = avgdata_dc
1274 1274
1275 1275 dataOut.nIncohInt *= self.n
1276 1276 dataOut.utctime = avgdatatime
1277 1277 #dataOut.timeInterval = dataOut.ippSeconds * dataOut.nCohInt * dataOut.nIncohInt * dataOut.nFFTPoints
1278 1278 dataOut.timeInterval = self.__timeInterval*self.n
1279 1279 dataOut.flagNoData = False
1280 1280
1281 1281 class ProfileSelector(Operation):
1282 1282
1283 1283 profileIndex = None
1284 1284 # Tamanho total de los perfiles
1285 1285 nProfiles = None
1286 1286
1287 1287 def __init__(self):
1288 1288
1289 1289 self.profileIndex = 0
1290 1290
1291 1291 def incIndex(self):
1292 1292 self.profileIndex += 1
1293 1293
1294 1294 if self.profileIndex >= self.nProfiles:
1295 1295 self.profileIndex = 0
1296 1296
1297 1297 def isProfileInRange(self, minIndex, maxIndex):
1298 1298
1299 1299 if self.profileIndex < minIndex:
1300 1300 return False
1301 1301
1302 1302 if self.profileIndex > maxIndex:
1303 1303 return False
1304 1304
1305 1305 return True
1306 1306
1307 1307 def isProfileInList(self, profileList):
1308 1308
1309 1309 if self.profileIndex not in profileList:
1310 1310 return False
1311 1311
1312 1312 return True
1313 1313
1314 1314 def run(self, dataOut, profileList=None, profileRangeList=None):
1315 1315
1316 1316 dataOut.flagNoData = True
1317 1317 self.nProfiles = dataOut.nProfiles
1318 1318
1319 1319 if profileList != None:
1320 1320 if self.isProfileInList(profileList):
1321 1321 dataOut.flagNoData = False
1322 1322
1323 1323 self.incIndex()
1324 1324 return 1
1325 1325
1326 1326
1327 1327 elif profileRangeList != None:
1328 1328 minIndex = profileRangeList[0]
1329 1329 maxIndex = profileRangeList[1]
1330 1330 if self.isProfileInRange(minIndex, maxIndex):
1331 1331 dataOut.flagNoData = False
1332 1332
1333 1333 self.incIndex()
1334 1334 return 1
1335 1335
1336 1336 else:
1337 1337 raise ValueError, "ProfileSelector needs profileList or profileRangeList"
1338 1338
1339 1339 return 0
1340 1340
1341 1341 class SpectraHeisProc(ProcessingUnit):
1342 1342 def __init__(self):
1343 1343 self.objectDict = {}
1344 1344 # self.buffer = None
1345 1345 # self.firstdatatime = None
1346 1346 # self.profIndex = 0
1347 1347 self.dataOut = SpectraHeis()
1348 1348
1349 1349 def __updateObjFromInput(self):
1350 1350 self.dataOut.timeZone = self.dataIn.timeZone
1351 1351 self.dataOut.dstFlag = self.dataIn.dstFlag
1352 1352 self.dataOut.errorCount = self.dataIn.errorCount
1353 1353 self.dataOut.useLocalTime = self.dataIn.useLocalTime
1354 1354
1355 1355 self.dataOut.radarControllerHeaderObj = self.dataIn.radarControllerHeaderObj.copy()#
1356 1356 self.dataOut.systemHeaderObj = self.dataIn.systemHeaderObj.copy()#
1357 1357 self.dataOut.channelList = self.dataIn.channelList
1358 1358 self.dataOut.heightList = self.dataIn.heightList
1359 1359 # self.dataOut.dtype = self.dataIn.dtype
1360 1360 self.dataOut.dtype = numpy.dtype([('real','<f4'),('imag','<f4')])
1361 1361 # self.dataOut.nHeights = self.dataIn.nHeights
1362 1362 # self.dataOut.nChannels = self.dataIn.nChannels
1363 1363 self.dataOut.nBaud = self.dataIn.nBaud
1364 1364 self.dataOut.nCode = self.dataIn.nCode
1365 1365 self.dataOut.code = self.dataIn.code
1366 1366 # self.dataOut.nProfiles = 1
1367 1367 # self.dataOut.nProfiles = self.dataOut.nFFTPoints
1368 1368 self.dataOut.nFFTPoints = self.dataIn.nHeights
1369 1369 # self.dataOut.channelIndexList = self.dataIn.channelIndexList
1370 1370 # self.dataOut.flagNoData = self.dataIn.flagNoData
1371 1371 self.dataOut.flagTimeBlock = self.dataIn.flagTimeBlock
1372 1372 self.dataOut.utctime = self.dataIn.utctime
1373 1373 # self.dataOut.utctime = self.firstdatatime
1374 1374 self.dataOut.flagDecodeData = self.dataIn.flagDecodeData #asumo q la data esta decodificada
1375 1375 self.dataOut.flagDeflipData = self.dataIn.flagDeflipData #asumo q la data esta sin flip
1376 1376 # self.dataOut.flagShiftFFT = self.dataIn.flagShiftFFT
1377 1377 self.dataOut.nCohInt = self.dataIn.nCohInt
1378 1378 self.dataOut.nIncohInt = 1
1379 1379 self.dataOut.ippSeconds= self.dataIn.ippSeconds
1380 1380 self.dataOut.windowOfFilter = self.dataIn.windowOfFilter
1381 1381
1382 1382 self.dataOut.timeInterval = self.dataIn.timeInterval*self.dataOut.nIncohInt
1383 1383 # self.dataOut.set=self.dataIn.set
1384 1384 # self.dataOut.deltaHeight=self.dataIn.deltaHeight
1385 1385
1386 1386
1387 1387 def __getFft(self):
1388 1388
1389 1389 fft_volt = numpy.fft.fft(self.dataIn.data, axis=1)
1390 1390 fft_volt = numpy.fft.fftshift(fft_volt,axes=(1,))
1391 1391 spc = numpy.abs(fft_volt * numpy.conjugate(fft_volt))/(self.dataOut.nFFTPoints)
1392 1392 self.dataOut.data_spc = spc
1393 1393
1394 1394 def init(self):
1395 1395
1396 1396 self.dataOut.flagNoData = True
1397 1397
1398 1398 if self.dataIn.type == "SpectraHeis":
1399 1399 self.dataOut.copy(self.dataIn)
1400 1400 return
1401 1401
1402 1402 if self.dataIn.type == "Voltage":
1403 1403 self.__updateObjFromInput()
1404 1404 self.__getFft()
1405 1405 self.dataOut.flagNoData = False
1406 1406
1407 1407 return
1408 1408
1409 1409 raise ValuError, "The type object %s is not valid"%(self.dataIn.type)
1410 1410
1411 1411
1412 1412 def selectChannels(self, channelList):
1413 1413
1414 1414 channelIndexList = []
1415 1415
1416 1416 for channel in channelList:
1417 1417 index = self.dataOut.channelList.index(channel)
1418 1418 channelIndexList.append(index)
1419 1419
1420 1420 self.selectChannelsByIndex(channelIndexList)
1421 1421
1422 1422 def selectChannelsByIndex(self, channelIndexList):
1423 1423 """
1424 1424 Selecciona un bloque de datos en base a canales segun el channelIndexList
1425 1425
1426 1426 Input:
1427 1427 channelIndexList : lista sencilla de canales a seleccionar por ej. [2,3,7]
1428 1428
1429 1429 Affected:
1430 1430 self.dataOut.data
1431 1431 self.dataOut.channelIndexList
1432 1432 self.dataOut.nChannels
1433 1433 self.dataOut.m_ProcessingHeader.totalSpectra
1434 1434 self.dataOut.systemHeaderObj.numChannels
1435 1435 self.dataOut.m_ProcessingHeader.blockSize
1436 1436
1437 1437 Return:
1438 1438 None
1439 1439 """
1440 1440
1441 1441 for channelIndex in channelIndexList:
1442 1442 if channelIndex not in self.dataOut.channelIndexList:
1443 1443 print channelIndexList
1444 1444 raise ValueError, "The value %d in channelIndexList is not valid" %channelIndex
1445 1445
1446 1446 nChannels = len(channelIndexList)
1447 1447
1448 1448 data_spc = self.dataOut.data_spc[channelIndexList,:]
1449 1449
1450 1450 self.dataOut.data_spc = data_spc
1451 1451 self.dataOut.channelList = [self.dataOut.channelList[i] for i in channelIndexList]
1452 1452
1453 1453 return 1
1454 1454
1455 1455 class IncohInt4SpectraHeis(Operation):
1456 1456
1457 1457 __isConfig = False
1458 1458
1459 1459 __profIndex = 0
1460 1460 __withOverapping = False
1461 1461
1462 1462 __byTime = False
1463 1463 __initime = None
1464 1464 __lastdatatime = None
1465 1465 __integrationtime = None
1466 1466
1467 1467 __buffer = None
1468 1468
1469 1469 __dataReady = False
1470 1470
1471 1471 n = None
1472 1472
1473 1473
1474 1474 def __init__(self):
1475 1475
1476 1476 self.__isConfig = False
1477 1477
1478 1478 def setup(self, n=None, timeInterval=None, overlapping=False):
1479 1479 """
1480 1480 Set the parameters of the integration class.
1481 1481
1482 1482 Inputs:
1483 1483
1484 1484 n : Number of coherent integrations
1485 1485 timeInterval : Time of integration. If the parameter "n" is selected this one does not work
1486 1486 overlapping :
1487 1487
1488 1488 """
1489 1489
1490 1490 self.__initime = None
1491 1491 self.__lastdatatime = 0
1492 1492 self.__buffer = None
1493 1493 self.__dataReady = False
1494 1494
1495 1495
1496 1496 if n == None and timeInterval == None:
1497 1497 raise ValueError, "n or timeInterval should be specified ..."
1498 1498
1499 1499 if n != None:
1500 1500 self.n = n
1501 1501 self.__byTime = False
1502 1502 else:
1503 1503 self.__integrationtime = timeInterval #* 60. #if (type(timeInterval)!=integer) -> change this line
1504 1504 self.n = 9999
1505 1505 self.__byTime = True
1506 1506
1507 1507 if overlapping:
1508 1508 self.__withOverapping = True
1509 1509 self.__buffer = None
1510 1510 else:
1511 1511 self.__withOverapping = False
1512 1512 self.__buffer = 0
1513 1513
1514 1514 self.__profIndex = 0
1515 1515
1516 1516 def putData(self, data):
1517 1517
1518 1518 """
1519 1519 Add a profile to the __buffer and increase in one the __profileIndex
1520 1520
1521 1521 """
1522 1522
1523 1523 if not self.__withOverapping:
1524 1524 self.__buffer += data.copy()
1525 1525 self.__profIndex += 1
1526 1526 return
1527 1527
1528 1528 #Overlapping data
1529 1529 nChannels, nHeis = data.shape
1530 1530 data = numpy.reshape(data, (1, nChannels, nHeis))
1531 1531
1532 1532 #If the buffer is empty then it takes the data value
1533 1533 if self.__buffer == None:
1534 1534 self.__buffer = data
1535 1535 self.__profIndex += 1
1536 1536 return
1537 1537
1538 1538 #If the buffer length is lower than n then stakcing the data value
1539 1539 if self.__profIndex < self.n:
1540 1540 self.__buffer = numpy.vstack((self.__buffer, data))
1541 1541 self.__profIndex += 1
1542 1542 return
1543 1543
1544 1544 #If the buffer length is equal to n then replacing the last buffer value with the data value
1545 1545 self.__buffer = numpy.roll(self.__buffer, -1, axis=0)
1546 1546 self.__buffer[self.n-1] = data
1547 1547 self.__profIndex = self.n
1548 1548 return
1549 1549
1550 1550
1551 1551 def pushData(self):
1552 1552 """
1553 1553 Return the sum of the last profiles and the profiles used in the sum.
1554 1554
1555 1555 Affected:
1556 1556
1557 1557 self.__profileIndex
1558 1558
1559 1559 """
1560 1560
1561 1561 if not self.__withOverapping:
1562 1562 data = self.__buffer
1563 1563 n = self.__profIndex
1564 1564
1565 1565 self.__buffer = 0
1566 1566 self.__profIndex = 0
1567 1567
1568 1568 return data, n
1569 1569
1570 1570 #Integration with Overlapping
1571 1571 data = numpy.sum(self.__buffer, axis=0)
1572 1572 n = self.__profIndex
1573 1573
1574 1574 return data, n
1575 1575
1576 1576 def byProfiles(self, data):
1577 1577
1578 1578 self.__dataReady = False
1579 1579 avgdata = None
1580 1580 n = None
1581 1581
1582 1582 self.putData(data)
1583 1583
1584 1584 if self.__profIndex == self.n:
1585 1585
1586 1586 avgdata, n = self.pushData()
1587 1587 self.__dataReady = True
1588 1588
1589 1589 return avgdata
1590 1590
1591 1591 def byTime(self, data, datatime):
1592 1592
1593 1593 self.__dataReady = False
1594 1594 avgdata = None
1595 1595 n = None
1596 1596
1597 1597 self.putData(data)
1598 1598
1599 1599 if (datatime - self.__initime) >= self.__integrationtime:
1600 1600 avgdata, n = self.pushData()
1601 1601 self.n = n
1602 1602 self.__dataReady = True
1603 1603
1604 1604 return avgdata
1605 1605
1606 1606 def integrate(self, data, datatime=None):
1607 1607
1608 1608 if self.__initime == None:
1609 1609 self.__initime = datatime
1610 1610
1611 1611 if self.__byTime:
1612 1612 avgdata = self.byTime(data, datatime)
1613 1613 else:
1614 1614 avgdata = self.byProfiles(data)
1615 1615
1616 1616
1617 1617 self.__lastdatatime = datatime
1618 1618
1619 1619 if avgdata == None:
1620 1620 return None, None
1621 1621
1622 1622 avgdatatime = self.__initime
1623 1623
1624 1624 deltatime = datatime -self.__lastdatatime
1625 1625
1626 1626 if not self.__withOverapping:
1627 1627 self.__initime = datatime
1628 1628 else:
1629 1629 self.__initime += deltatime
1630 1630
1631 1631 return avgdata, avgdatatime
1632 1632
1633 1633 def run(self, dataOut, **kwargs):
1634 1634
1635 1635 if not self.__isConfig:
1636 1636 self.setup(**kwargs)
1637 1637 self.__isConfig = True
1638 1638
1639 1639 avgdata, avgdatatime = self.integrate(dataOut.data_spc, dataOut.utctime)
1640 1640
1641 1641 # dataOut.timeInterval *= n
1642 1642 dataOut.flagNoData = True
1643 1643
1644 1644 if self.__dataReady:
1645 1645 dataOut.data_spc = avgdata
1646 1646 dataOut.nIncohInt *= self.n
1647 1647 # dataOut.nCohInt *= self.n
1648 1648 dataOut.utctime = avgdatatime
1649 1649 dataOut.timeInterval = dataOut.ippSeconds * dataOut.nIncohInt
1650 1650 # dataOut.timeInterval = self.__timeInterval*self.n
1651 1651 dataOut.flagNoData = False
1652 1652
1653 1653
1654 1654
1655 1655
1656 1656 No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now