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