##// END OF EJS Templates
Show warning when decoding mode is equal to 3.
Ivan Valdez -
r721:38a818d617e2
parent child
Show More
@@ -1,1072 +1,1076
1 1 import numpy
2 2
3 3 from jroproc_base import ProcessingUnit, Operation
4 4 from schainpy.model.data.jrodata import Voltage
5 5
6 6 class VoltageProc(ProcessingUnit):
7 7
8 8
9 9 def __init__(self):
10 10
11 11 ProcessingUnit.__init__(self)
12 12
13 13 # self.objectDict = {}
14 14 self.dataOut = Voltage()
15 15 self.flip = 1
16 16
17 17 def run(self):
18 18 if self.dataIn.type == 'AMISR':
19 19 self.__updateObjFromAmisrInput()
20 20
21 21 if self.dataIn.type == 'Voltage':
22 22 self.dataOut.copy(self.dataIn)
23 23
24 24 # self.dataOut.copy(self.dataIn)
25 25
26 26 def __updateObjFromAmisrInput(self):
27 27
28 28 self.dataOut.timeZone = self.dataIn.timeZone
29 29 self.dataOut.dstFlag = self.dataIn.dstFlag
30 30 self.dataOut.errorCount = self.dataIn.errorCount
31 31 self.dataOut.useLocalTime = self.dataIn.useLocalTime
32 32
33 33 self.dataOut.flagNoData = self.dataIn.flagNoData
34 34 self.dataOut.data = self.dataIn.data
35 35 self.dataOut.utctime = self.dataIn.utctime
36 36 self.dataOut.channelList = self.dataIn.channelList
37 37 # self.dataOut.timeInterval = self.dataIn.timeInterval
38 38 self.dataOut.heightList = self.dataIn.heightList
39 39 self.dataOut.nProfiles = self.dataIn.nProfiles
40 40
41 41 self.dataOut.nCohInt = self.dataIn.nCohInt
42 42 self.dataOut.ippSeconds = self.dataIn.ippSeconds
43 43 self.dataOut.frequency = self.dataIn.frequency
44 44
45 45 self.dataOut.azimuth = self.dataIn.azimuth
46 46 self.dataOut.zenith = self.dataIn.zenith
47 47
48 48 self.dataOut.beam.codeList = self.dataIn.beam.codeList
49 49 self.dataOut.beam.azimuthList = self.dataIn.beam.azimuthList
50 50 self.dataOut.beam.zenithList = self.dataIn.beam.zenithList
51 51 #
52 52 # pass#
53 53 #
54 54 # def init(self):
55 55 #
56 56 #
57 57 # if self.dataIn.type == 'AMISR':
58 58 # self.__updateObjFromAmisrInput()
59 59 #
60 60 # if self.dataIn.type == 'Voltage':
61 61 # self.dataOut.copy(self.dataIn)
62 62 # # No necesita copiar en cada init() los atributos de dataIn
63 63 # # la copia deberia hacerse por cada nuevo bloque de datos
64 64
65 65 def selectChannels(self, channelList):
66 66
67 67 channelIndexList = []
68 68
69 69 for channel in channelList:
70 70 if channel not in self.dataOut.channelList:
71 71 raise ValueError, "Channel %d is not in %s" %(channel, str(self.dataOut.channelList))
72 72
73 73 index = self.dataOut.channelList.index(channel)
74 74 channelIndexList.append(index)
75 75
76 76 self.selectChannelsByIndex(channelIndexList)
77 77
78 78 def selectChannelsByIndex(self, channelIndexList):
79 79 """
80 80 Selecciona un bloque de datos en base a canales segun el channelIndexList
81 81
82 82 Input:
83 83 channelIndexList : lista sencilla de canales a seleccionar por ej. [2,3,7]
84 84
85 85 Affected:
86 86 self.dataOut.data
87 87 self.dataOut.channelIndexList
88 88 self.dataOut.nChannels
89 89 self.dataOut.m_ProcessingHeader.totalSpectra
90 90 self.dataOut.systemHeaderObj.numChannels
91 91 self.dataOut.m_ProcessingHeader.blockSize
92 92
93 93 Return:
94 94 None
95 95 """
96 96
97 97 for channelIndex in channelIndexList:
98 98 if channelIndex not in self.dataOut.channelIndexList:
99 99 print channelIndexList
100 100 raise ValueError, "The value %d in channelIndexList is not valid" %channelIndex
101 101
102 102 if self.dataOut.flagDataAsBlock:
103 103 """
104 104 Si la data es obtenida por bloques, dimension = [nChannels, nProfiles, nHeis]
105 105 """
106 106 data = self.dataOut.data[channelIndexList,:,:]
107 107 else:
108 108 data = self.dataOut.data[channelIndexList,:]
109 109
110 110 self.dataOut.data = data
111 111 self.dataOut.channelList = [self.dataOut.channelList[i] for i in channelIndexList]
112 112 # self.dataOut.nChannels = nChannels
113 113
114 114 return 1
115 115
116 116 def selectHeights(self, minHei=None, maxHei=None):
117 117 """
118 118 Selecciona un bloque de datos en base a un grupo de valores de alturas segun el rango
119 119 minHei <= height <= maxHei
120 120
121 121 Input:
122 122 minHei : valor minimo de altura a considerar
123 123 maxHei : valor maximo de altura a considerar
124 124
125 125 Affected:
126 126 Indirectamente son cambiados varios valores a travez del metodo selectHeightsByIndex
127 127
128 128 Return:
129 129 1 si el metodo se ejecuto con exito caso contrario devuelve 0
130 130 """
131 131
132 132 if minHei == None:
133 133 minHei = self.dataOut.heightList[0]
134 134
135 135 if maxHei == None:
136 136 maxHei = self.dataOut.heightList[-1]
137 137
138 138 if (minHei < self.dataOut.heightList[0]):
139 139 minHei = self.dataOut.heightList[0]
140 140
141 141 if (maxHei > self.dataOut.heightList[-1]):
142 142 maxHei = self.dataOut.heightList[-1]
143 143
144 144 minIndex = 0
145 145 maxIndex = 0
146 146 heights = self.dataOut.heightList
147 147
148 148 inda = numpy.where(heights >= minHei)
149 149 indb = numpy.where(heights <= maxHei)
150 150
151 151 try:
152 152 minIndex = inda[0][0]
153 153 except:
154 154 minIndex = 0
155 155
156 156 try:
157 157 maxIndex = indb[0][-1]
158 158 except:
159 159 maxIndex = len(heights)
160 160
161 161 self.selectHeightsByIndex(minIndex, maxIndex)
162 162
163 163 return 1
164 164
165 165
166 166 def selectHeightsByIndex(self, minIndex, maxIndex):
167 167 """
168 168 Selecciona un bloque de datos en base a un grupo indices de alturas segun el rango
169 169 minIndex <= index <= maxIndex
170 170
171 171 Input:
172 172 minIndex : valor de indice minimo de altura a considerar
173 173 maxIndex : valor de indice maximo de altura a considerar
174 174
175 175 Affected:
176 176 self.dataOut.data
177 177 self.dataOut.heightList
178 178
179 179 Return:
180 180 1 si el metodo se ejecuto con exito caso contrario devuelve 0
181 181 """
182 182
183 183 if (minIndex < 0) or (minIndex > maxIndex):
184 184 raise ValueError, "Height index range (%d,%d) is not valid" % (minIndex, maxIndex)
185 185
186 186 if (maxIndex >= self.dataOut.nHeights):
187 187 maxIndex = self.dataOut.nHeights
188 188
189 189 #voltage
190 190 if self.dataOut.flagDataAsBlock:
191 191 """
192 192 Si la data es obtenida por bloques, dimension = [nChannels, nProfiles, nHeis]
193 193 """
194 194 data = self.dataOut.data[:,:, minIndex:maxIndex]
195 195 else:
196 196 data = self.dataOut.data[:, minIndex:maxIndex]
197 197
198 198 # firstHeight = self.dataOut.heightList[minIndex]
199 199
200 200 self.dataOut.data = data
201 201 self.dataOut.heightList = self.dataOut.heightList[minIndex:maxIndex]
202 202
203 203 if self.dataOut.nHeights <= 1:
204 204 raise ValueError, "selectHeights: Too few heights. Current number of heights is %d" %(self.dataOut.nHeights)
205 205
206 206 return 1
207 207
208 208
209 209 def filterByHeights(self, window):
210 210
211 211 deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0]
212 212
213 213 if window == None:
214 214 window = (self.dataOut.radarControllerHeaderObj.txA/self.dataOut.radarControllerHeaderObj.nBaud) / deltaHeight
215 215
216 216 newdelta = deltaHeight * window
217 217 r = self.dataOut.nHeights % window
218 218 newheights = (self.dataOut.nHeights-r)/window
219 219
220 220 if newheights <= 1:
221 221 raise ValueError, "filterByHeights: Too few heights. Current number of heights is %d and window is %d" %(self.dataOut.nHeights, window)
222 222
223 223 if self.dataOut.flagDataAsBlock:
224 224 """
225 225 Si la data es obtenida por bloques, dimension = [nChannels, nProfiles, nHeis]
226 226 """
227 227 buffer = self.dataOut.data[:, :, 0:self.dataOut.nHeights-r]
228 228 buffer = buffer.reshape(self.dataOut.nChannels,self.dataOut.nProfiles,self.dataOut.nHeights/window,window)
229 229 buffer = numpy.sum(buffer,3)
230 230
231 231 else:
232 232 buffer = self.dataOut.data[:,0:self.dataOut.nHeights-r]
233 233 buffer = buffer.reshape(self.dataOut.nChannels,self.dataOut.nHeights/window,window)
234 234 buffer = numpy.sum(buffer,2)
235 235
236 236 self.dataOut.data = buffer
237 237 self.dataOut.heightList = self.dataOut.heightList[0] + numpy.arange( newheights )*newdelta
238 238 self.dataOut.windowOfFilter = window
239 239
240 240 def setH0(self, h0, deltaHeight = None):
241 241
242 242 if not deltaHeight:
243 243 deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0]
244 244
245 245 nHeights = self.dataOut.nHeights
246 246
247 247 newHeiRange = h0 + numpy.arange(nHeights)*deltaHeight
248 248
249 249 self.dataOut.heightList = newHeiRange
250 250
251 251 def deFlip(self, channelList = []):
252 252
253 253 data = self.dataOut.data.copy()
254 254
255 255 if self.dataOut.flagDataAsBlock:
256 256 flip = self.flip
257 257 profileList = range(self.dataOut.nProfiles)
258 258
259 259 if not channelList:
260 260 for thisProfile in profileList:
261 261 data[:,thisProfile,:] = data[:,thisProfile,:]*flip
262 262 flip *= -1.0
263 263 else:
264 264 for thisChannel in channelList:
265 265 if thisChannel not in self.dataOut.channelList:
266 266 continue
267 267
268 268 for thisProfile in profileList:
269 269 data[thisChannel,thisProfile,:] = data[thisChannel,thisProfile,:]*flip
270 270 flip *= -1.0
271 271
272 272 self.flip = flip
273 273
274 274 else:
275 275 if not channelList:
276 276 data[:,:] = data[:,:]*self.flip
277 277 else:
278 278 for thisChannel in channelList:
279 279 if thisChannel not in self.dataOut.channelList:
280 280 continue
281 281
282 282 data[thisChannel,:] = data[thisChannel,:]*self.flip
283 283
284 284 self.flip *= -1.
285 285
286 286 self.dataOut.data = data
287 287
288 288 def setRadarFrequency(self, frequency=None):
289 289
290 290 if frequency != None:
291 291 self.dataOut.frequency = frequency
292 292
293 293 return 1
294 294
295 295 class CohInt(Operation):
296 296
297 297 isConfig = False
298 298
299 299 __profIndex = 0
300 300 __withOverapping = False
301 301
302 302 __byTime = False
303 303 __initime = None
304 304 __lastdatatime = None
305 305 __integrationtime = None
306 306
307 307 __buffer = None
308 308
309 309 __dataReady = False
310 310
311 311 n = None
312 312
313 313
314 314 def __init__(self):
315 315
316 316 Operation.__init__(self)
317 317
318 318 # self.isConfig = False
319 319
320 320 def setup(self, n=None, timeInterval=None, overlapping=False, byblock=False):
321 321 """
322 322 Set the parameters of the integration class.
323 323
324 324 Inputs:
325 325
326 326 n : Number of coherent integrations
327 327 timeInterval : Time of integration. If the parameter "n" is selected this one does not work
328 328 overlapping :
329 329
330 330 """
331 331
332 332 self.__initime = None
333 333 self.__lastdatatime = 0
334 334 self.__buffer = None
335 335 self.__dataReady = False
336 336 self.byblock = byblock
337 337
338 338 if n == None and timeInterval == None:
339 339 raise ValueError, "n or timeInterval should be specified ..."
340 340
341 341 if n != None:
342 342 self.n = n
343 343 self.__byTime = False
344 344 else:
345 345 self.__integrationtime = timeInterval #* 60. #if (type(timeInterval)!=integer) -> change this line
346 346 self.n = 9999
347 347 self.__byTime = True
348 348
349 349 if overlapping:
350 350 self.__withOverapping = True
351 351 self.__buffer = None
352 352 else:
353 353 self.__withOverapping = False
354 354 self.__buffer = 0
355 355
356 356 self.__profIndex = 0
357 357
358 358 def putData(self, data):
359 359
360 360 """
361 361 Add a profile to the __buffer and increase in one the __profileIndex
362 362
363 363 """
364 364
365 365 if not self.__withOverapping:
366 366 self.__buffer += data.copy()
367 367 self.__profIndex += 1
368 368 return
369 369
370 370 #Overlapping data
371 371 nChannels, nHeis = data.shape
372 372 data = numpy.reshape(data, (1, nChannels, nHeis))
373 373
374 374 #If the buffer is empty then it takes the data value
375 375 if self.__buffer is None:
376 376 self.__buffer = data
377 377 self.__profIndex += 1
378 378 return
379 379
380 380 #If the buffer length is lower than n then stakcing the data value
381 381 if self.__profIndex < self.n:
382 382 self.__buffer = numpy.vstack((self.__buffer, data))
383 383 self.__profIndex += 1
384 384 return
385 385
386 386 #If the buffer length is equal to n then replacing the last buffer value with the data value
387 387 self.__buffer = numpy.roll(self.__buffer, -1, axis=0)
388 388 self.__buffer[self.n-1] = data
389 389 self.__profIndex = self.n
390 390 return
391 391
392 392
393 393 def pushData(self):
394 394 """
395 395 Return the sum of the last profiles and the profiles used in the sum.
396 396
397 397 Affected:
398 398
399 399 self.__profileIndex
400 400
401 401 """
402 402
403 403 if not self.__withOverapping:
404 404 data = self.__buffer
405 405 n = self.__profIndex
406 406
407 407 self.__buffer = 0
408 408 self.__profIndex = 0
409 409
410 410 return data, n
411 411
412 412 #Integration with Overlapping
413 413 data = numpy.sum(self.__buffer, axis=0)
414 414 n = self.__profIndex
415 415
416 416 return data, n
417 417
418 418 def byProfiles(self, data):
419 419
420 420 self.__dataReady = False
421 421 avgdata = None
422 422 # n = None
423 423
424 424 self.putData(data)
425 425
426 426 if self.__profIndex == self.n:
427 427
428 428 avgdata, n = self.pushData()
429 429 self.__dataReady = True
430 430
431 431 return avgdata
432 432
433 433 def byTime(self, data, datatime):
434 434
435 435 self.__dataReady = False
436 436 avgdata = None
437 437 n = None
438 438
439 439 self.putData(data)
440 440
441 441 if (datatime - self.__initime) >= self.__integrationtime:
442 442 avgdata, n = self.pushData()
443 443 self.n = n
444 444 self.__dataReady = True
445 445
446 446 return avgdata
447 447
448 448 def integrate(self, data, datatime=None):
449 449
450 450 if self.__initime == None:
451 451 self.__initime = datatime
452 452
453 453 if self.__byTime:
454 454 avgdata = self.byTime(data, datatime)
455 455 else:
456 456 avgdata = self.byProfiles(data)
457 457
458 458
459 459 self.__lastdatatime = datatime
460 460
461 461 if avgdata is None:
462 462 return None, None
463 463
464 464 avgdatatime = self.__initime
465 465
466 466 deltatime = datatime -self.__lastdatatime
467 467
468 468 if not self.__withOverapping:
469 469 self.__initime = datatime
470 470 else:
471 471 self.__initime += deltatime
472 472
473 473 return avgdata, avgdatatime
474 474
475 475 def integrateByBlock(self, dataOut):
476 476
477 477 times = int(dataOut.data.shape[1]/self.n)
478 478 avgdata = numpy.zeros((dataOut.nChannels, times, dataOut.nHeights), dtype=numpy.complex)
479 479
480 480 id_min = 0
481 481 id_max = self.n
482 482
483 483 for i in range(times):
484 484 junk = dataOut.data[:,id_min:id_max,:]
485 485 avgdata[:,i,:] = junk.sum(axis=1)
486 486 id_min += self.n
487 487 id_max += self.n
488 488
489 489 timeInterval = dataOut.ippSeconds*self.n
490 490 avgdatatime = (times - 1) * timeInterval + dataOut.utctime
491 491 self.__dataReady = True
492 492 return avgdata, avgdatatime
493 493
494 494 def run(self, dataOut, **kwargs):
495 495
496 496 if not self.isConfig:
497 497 self.setup(**kwargs)
498 498 self.isConfig = True
499 499
500 500 if dataOut.flagDataAsBlock:
501 501 """
502 502 Si la data es leida por bloques, dimension = [nChannels, nProfiles, nHeis]
503 503 """
504 504 avgdata, avgdatatime = self.integrateByBlock(dataOut)
505 505 dataOut.nProfiles /= self.n
506 506 else:
507 507 avgdata, avgdatatime = self.integrate(dataOut.data, dataOut.utctime)
508 508
509 509 # dataOut.timeInterval *= n
510 510 dataOut.flagNoData = True
511 511
512 512 if self.__dataReady:
513 513 dataOut.data = avgdata
514 514 dataOut.nCohInt *= self.n
515 515 dataOut.utctime = avgdatatime
516 516 # dataOut.timeInterval = dataOut.ippSeconds * dataOut.nCohInt
517 517 dataOut.flagNoData = False
518 518
519 519 class Decoder(Operation):
520 520
521 521 isConfig = False
522 522 __profIndex = 0
523 523
524 524 code = None
525 525
526 526 nCode = None
527 527 nBaud = None
528 528
529 529
530 530 def __init__(self):
531 531
532 532 Operation.__init__(self)
533 533
534 534 self.times = None
535 535 self.osamp = None
536 536 # self.__setValues = False
537 537 self.isConfig = False
538 538
539 539 def setup(self, code, osamp, dataOut):
540 540
541 541 self.__profIndex = 0
542 542
543 543 self.code = code
544 544
545 545 self.nCode = len(code)
546 546 self.nBaud = len(code[0])
547 547
548 548 if (osamp != None) and (osamp >1):
549 549 self.osamp = osamp
550 550 self.code = numpy.repeat(code, repeats=self.osamp, axis=1)
551 551 self.nBaud = self.nBaud*self.osamp
552 552
553 553 self.__nChannels = dataOut.nChannels
554 554 self.__nProfiles = dataOut.nProfiles
555 555 self.__nHeis = dataOut.nHeights
556 556
557 557 if self.__nHeis < self.nBaud:
558 558 raise ValueError, 'Number of heights (%d) should be greater than number of bauds (%d)' %(self.__nHeis, self.nBaud)
559 559
560 560 #Frequency
561 561 __codeBuffer = numpy.zeros((self.nCode, self.__nHeis), dtype=numpy.complex)
562 562
563 563 __codeBuffer[:,0:self.nBaud] = self.code
564 564
565 565 self.fft_code = numpy.conj(numpy.fft.fft(__codeBuffer, axis=1))
566 566
567 567 if dataOut.flagDataAsBlock:
568 568
569 569 self.ndatadec = self.__nHeis #- self.nBaud + 1
570 570
571 571 self.datadecTime = numpy.zeros((self.__nChannels, self.__nProfiles, self.ndatadec), dtype=numpy.complex)
572 572
573 573 else:
574 574
575 575 #Time
576 576 self.ndatadec = self.__nHeis #- self.nBaud + 1
577 577
578 578 self.datadecTime = numpy.zeros((self.__nChannels, self.ndatadec), dtype=numpy.complex)
579 579
580 580 def __convolutionInFreq(self, data):
581 581
582 582 fft_code = self.fft_code[self.__profIndex].reshape(1,-1)
583 583
584 584 fft_data = numpy.fft.fft(data, axis=1)
585 585
586 586 conv = fft_data*fft_code
587 587
588 588 data = numpy.fft.ifft(conv,axis=1)
589 589
590 590 return data
591 591
592 592 def __convolutionInFreqOpt(self, data):
593 593
594 594 raise NotImplementedError
595 595
596 596 def __convolutionInTime(self, data):
597 597
598 598 code = self.code[self.__profIndex]
599 599
600 600 for i in range(self.__nChannels):
601 601 self.datadecTime[i,:] = numpy.correlate(data[i,:], code, mode='full')[self.nBaud-1:]
602 602
603 603 return self.datadecTime
604 604
605 605 def __convolutionByBlockInTime(self, data):
606 606
607 607 repetitions = self.__nProfiles / self.nCode
608 608
609 609 junk = numpy.lib.stride_tricks.as_strided(self.code, (repetitions, self.code.size), (0, self.code.itemsize))
610 610 junk = junk.flatten()
611 611 code_block = numpy.reshape(junk, (self.nCode*repetitions, self.nBaud))
612 612
613 613 for i in range(self.__nChannels):
614 614 for j in range(self.__nProfiles):
615 615 self.datadecTime[i,j,:] = numpy.correlate(data[i,j,:], code_block[j,:], mode='full')[self.nBaud-1:]
616 616
617 617 return self.datadecTime
618 618
619 619 def __convolutionByBlockInFreq(self, data):
620 620
621 621 fft_code = self.fft_code[self.__profIndex].reshape(1,-1)
622 622
623 623 fft_data = numpy.fft.fft(data, axis=2)
624 624
625 625 conv = fft_data*fft_code
626 626
627 627 data = numpy.fft.ifft(conv,axis=2)
628 628
629 629 return data
630 630
631 631 def run(self, dataOut, code=None, nCode=None, nBaud=None, mode = 0, osamp=None, times=None):
632 632
633 633 if dataOut.flagDecodeData:
634 634 print "This data is already decoded, recoding again ..."
635 635
636 636 if not self.isConfig:
637 637
638 638 if code is None:
639 639 if dataOut.code is None:
640 640 raise ValueError, "Code could not be read from %s instance. Enter a value in Code parameter" %dataOut.type
641 641
642 642 code = dataOut.code
643 643 else:
644 644 code = numpy.array(code).reshape(nCode,nBaud)
645 645
646 646 self.setup(code, osamp, dataOut)
647 647
648 648 self.isConfig = True
649 649
650 650 if self.code is None:
651 651 print "Fail decoding: Code is not defined."
652 652 return
653 653
654 654 datadec = None
655 655
656 656 if dataOut.flagDataAsBlock:
657 657 """
658 658 Decoding when data have been read as block,
659 659 """
660 if mode == 3:
661 sys.stderr.write("Decoder Warning: mode=%d is not valid, using mode=0\n" %mode)
662 mode = 0
663
660 664 if mode == 0:
661 665 datadec = self.__convolutionByBlockInTime(dataOut.data)
662 666 if mode == 1:
663 667 datadec = self.__convolutionByBlockInFreq(dataOut.data)
664 668 else:
665 669 """
666 670 Decoding when data have been read profile by profile
667 671 """
668 672 if mode == 0:
669 673 datadec = self.__convolutionInTime(dataOut.data)
670 674
671 675 if mode == 1:
672 676 datadec = self.__convolutionInFreq(dataOut.data)
673 677
674 678 if mode == 2:
675 679 datadec = self.__convolutionInFreqOpt(dataOut.data)
676 680
677 681 if datadec is None:
678 682 raise ValueError, "Codification mode selected is not valid: mode=%d. Try selecting 0 or 1" %mode
679 683
680 684 dataOut.code = self.code
681 685 dataOut.nCode = self.nCode
682 686 dataOut.nBaud = self.nBaud
683 687
684 688 dataOut.data = datadec
685 689
686 690 dataOut.heightList = dataOut.heightList[0:datadec.shape[-1]]
687 691
688 692 dataOut.flagDecodeData = True #asumo q la data esta decodificada
689 693
690 694 if self.__profIndex == self.nCode-1:
691 695 self.__profIndex = 0
692 696 return 1
693 697
694 698 self.__profIndex += 1
695 699
696 700 return 1
697 701 # dataOut.flagDeflipData = True #asumo q la data no esta sin flip
698 702
699 703
700 704 class ProfileConcat(Operation):
701 705
702 706 isConfig = False
703 707 buffer = None
704 708
705 709 def __init__(self):
706 710
707 711 Operation.__init__(self)
708 712 self.profileIndex = 0
709 713
710 714 def reset(self):
711 715 self.buffer = numpy.zeros_like(self.buffer)
712 716 self.start_index = 0
713 717 self.times = 1
714 718
715 719 def setup(self, data, m, n=1):
716 720 self.buffer = numpy.zeros((data.shape[0],data.shape[1]*m),dtype=type(data[0,0]))
717 721 self.nHeights = data.nHeights
718 722 self.start_index = 0
719 723 self.times = 1
720 724
721 725 def concat(self, data):
722 726
723 727 self.buffer[:,self.start_index:self.profiles*self.times] = data.copy()
724 728 self.start_index = self.start_index + self.nHeights
725 729
726 730 def run(self, dataOut, m):
727 731
728 732 dataOut.flagNoData = True
729 733
730 734 if not self.isConfig:
731 735 self.setup(dataOut.data, m, 1)
732 736 self.isConfig = True
733 737
734 738 if dataOut.flagDataAsBlock:
735 739 raise ValueError, "ProfileConcat can only be used when voltage have been read profile by profile, getBlock = False"
736 740
737 741 else:
738 742 self.concat(dataOut.data)
739 743 self.times += 1
740 744 if self.times > m:
741 745 dataOut.data = self.buffer
742 746 self.reset()
743 747 dataOut.flagNoData = False
744 748 # se deben actualizar mas propiedades del header y del objeto dataOut, por ejemplo, las alturas
745 749 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
746 750 xf = dataOut.heightList[0] + dataOut.nHeights * deltaHeight * m
747 751 dataOut.heightList = numpy.arange(dataOut.heightList[0], xf, deltaHeight)
748 752 dataOut.ippSeconds *= m
749 753
750 754 class ProfileSelector(Operation):
751 755
752 756 profileIndex = None
753 757 # Tamanho total de los perfiles
754 758 nProfiles = None
755 759
756 760 def __init__(self):
757 761
758 762 Operation.__init__(self)
759 763 self.profileIndex = 0
760 764
761 765 def incIndex(self):
762 766
763 767 self.profileIndex += 1
764 768
765 769 if self.profileIndex >= self.nProfiles:
766 770 self.profileIndex = 0
767 771
768 772 def isThisProfileInRange(self, profileIndex, minIndex, maxIndex):
769 773
770 774 if profileIndex < minIndex:
771 775 return False
772 776
773 777 if profileIndex > maxIndex:
774 778 return False
775 779
776 780 return True
777 781
778 782 def isThisProfileInList(self, profileIndex, profileList):
779 783
780 784 if profileIndex not in profileList:
781 785 return False
782 786
783 787 return True
784 788
785 789 def run(self, dataOut, profileList=None, profileRangeList=None, beam=None, byblock=False, rangeList = None, nProfiles=None):
786 790
787 791 """
788 792 ProfileSelector:
789 793
790 794 Inputs:
791 795 profileList : Index of profiles selected. Example: profileList = (0,1,2,7,8)
792 796
793 797 profileRangeList : Minimum and maximum profile indexes. Example: profileRangeList = (4, 30)
794 798
795 799 rangeList : List of profile ranges. Example: rangeList = ((4, 30), (32, 64), (128, 256))
796 800
797 801 """
798 802
799 803 dataOut.flagNoData = True
800 804
801 805 if dataOut.flagDataAsBlock:
802 806 """
803 807 data dimension = [nChannels, nProfiles, nHeis]
804 808 """
805 809 if profileList != None:
806 810 dataOut.data = dataOut.data[:,profileList,:]
807 811 dataOut.nProfiles = len(profileList)
808 812 dataOut.profileIndex = dataOut.nProfiles - 1
809 813
810 814 if profileRangeList != None:
811 815 minIndex = profileRangeList[0]
812 816 maxIndex = profileRangeList[1]
813 817
814 818 dataOut.data = dataOut.data[:,minIndex:maxIndex+1,:]
815 819 dataOut.nProfiles = maxIndex - minIndex + 1
816 820 dataOut.profileIndex = dataOut.nProfiles - 1
817 821
818 822 if rangeList != None:
819 823 raise ValueError, "Profile Selector: Invalid argument rangeList. Not implemented for getByBlock yet"
820 824
821 825 dataOut.flagNoData = False
822 826
823 827 return True
824 828
825 829 """
826 830 data dimension = [nChannels, nHeis]
827 831 """
828 832
829 833 if nProfiles:
830 834 self.nProfiles = nProfiles
831 835 else:
832 836 self.nProfiles = dataOut.nProfiles
833 837
834 838 if profileList != None:
835 839
836 840 dataOut.nProfiles = len(profileList)
837 841
838 842 if self.isThisProfileInList(dataOut.profileIndex, profileList):
839 843 dataOut.flagNoData = False
840 844 dataOut.profileIndex = self.profileIndex
841 845
842 846 self.incIndex()
843 847 return True
844 848
845 849 if profileRangeList != None:
846 850
847 851 minIndex = profileRangeList[0]
848 852 maxIndex = profileRangeList[1]
849 853
850 854 dataOut.nProfiles = maxIndex - minIndex + 1
851 855
852 856 if self.isThisProfileInRange(dataOut.profileIndex, minIndex, maxIndex):
853 857 dataOut.flagNoData = False
854 858 dataOut.profileIndex = self.profileIndex
855 859
856 860 self.incIndex()
857 861 return True
858 862
859 863 if rangeList != None:
860 864
861 865 nProfiles = 0
862 866
863 867 for thisRange in rangeList:
864 868 minIndex = thisRange[0]
865 869 maxIndex = thisRange[1]
866 870
867 871 nProfiles += maxIndex - minIndex + 1
868 872
869 873 dataOut.nProfiles = nProfiles
870 874
871 875 for thisRange in rangeList:
872 876
873 877 minIndex = thisRange[0]
874 878 maxIndex = thisRange[1]
875 879
876 880 if self.isThisProfileInRange(dataOut.profileIndex, minIndex, maxIndex):
877 881
878 882 # print "profileIndex = ", dataOut.profileIndex
879 883
880 884 dataOut.flagNoData = False
881 885 dataOut.profileIndex = self.profileIndex
882 886
883 887 self.incIndex()
884 888 break
885 889 return True
886 890
887 891
888 892 if beam != None: #beam is only for AMISR data
889 893 if self.isThisProfileInList(dataOut.profileIndex, dataOut.beamRangeDict[beam]):
890 894 dataOut.flagNoData = False
891 895 dataOut.profileIndex = self.profileIndex
892 896
893 897 self.incIndex()
894 898
895 899 return True
896 900
897 901 raise ValueError, "ProfileSelector needs profileList, profileRangeList or rangeList parameter"
898 902
899 903 return False
900 904
901 905
902 906
903 907 class Reshaper(Operation):
904 908
905 909 def __init__(self):
906 910
907 911 Operation.__init__(self)
908 912 self.updateNewHeights = True
909 913
910 914 def run(self, dataOut, shape):
911 915
912 916 if not dataOut.flagDataAsBlock:
913 917 raise ValueError, "Reshaper can only be used when voltage have been read as Block, getBlock = True"
914 918
915 919 if len(shape) != 3:
916 920 raise ValueError, "shape len should be equal to 3, (nChannels, nProfiles, nHeis)"
917 921
918 922 shape_tuple = tuple(shape)
919 923 dataOut.data = numpy.reshape(dataOut.data, shape_tuple)
920 924 dataOut.flagNoData = False
921 925
922 926 if self.updateNewHeights:
923 927
924 928 old_nheights = dataOut.nHeights
925 929 new_nheights = dataOut.data.shape[2]
926 930 factor = 1.0*new_nheights / old_nheights
927 931
928 932 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
929 933
930 934 xf = dataOut.heightList[0] + dataOut.nHeights * deltaHeight * factor
931 935
932 936 dataOut.heightList = numpy.arange(dataOut.heightList[0], xf, deltaHeight)
933 937
934 938 dataOut.nProfiles = dataOut.data.shape[1]
935 939
936 940 dataOut.ippSeconds *= factor
937 941 #
938 942 # import collections
939 943 # from scipy.stats import mode
940 944 #
941 945 # class Synchronize(Operation):
942 946 #
943 947 # isConfig = False
944 948 # __profIndex = 0
945 949 #
946 950 # def __init__(self):
947 951 #
948 952 # Operation.__init__(self)
949 953 # # self.isConfig = False
950 954 # self.__powBuffer = None
951 955 # self.__startIndex = 0
952 956 # self.__pulseFound = False
953 957 #
954 958 # def __findTxPulse(self, dataOut, channel=0, pulse_with = None):
955 959 #
956 960 # #Read data
957 961 #
958 962 # powerdB = dataOut.getPower(channel = channel)
959 963 # noisedB = dataOut.getNoise(channel = channel)[0]
960 964 #
961 965 # self.__powBuffer.extend(powerdB.flatten())
962 966 #
963 967 # dataArray = numpy.array(self.__powBuffer)
964 968 #
965 969 # filteredPower = numpy.correlate(dataArray, dataArray[0:self.__nSamples], "same")
966 970 #
967 971 # maxValue = numpy.nanmax(filteredPower)
968 972 #
969 973 # if maxValue < noisedB + 10:
970 974 # #No se encuentra ningun pulso de transmision
971 975 # return None
972 976 #
973 977 # maxValuesIndex = numpy.where(filteredPower > maxValue - 0.1*abs(maxValue))[0]
974 978 #
975 979 # if len(maxValuesIndex) < 2:
976 980 # #Solo se encontro un solo pulso de transmision de un baudio, esperando por el siguiente TX
977 981 # return None
978 982 #
979 983 # phasedMaxValuesIndex = maxValuesIndex - self.__nSamples
980 984 #
981 985 # #Seleccionar solo valores con un espaciamiento de nSamples
982 986 # pulseIndex = numpy.intersect1d(maxValuesIndex, phasedMaxValuesIndex)
983 987 #
984 988 # if len(pulseIndex) < 2:
985 989 # #Solo se encontro un pulso de transmision con ancho mayor a 1
986 990 # return None
987 991 #
988 992 # spacing = pulseIndex[1:] - pulseIndex[:-1]
989 993 #
990 994 # #remover senales que se distancien menos de 10 unidades o muestras
991 995 # #(No deberian existir IPP menor a 10 unidades)
992 996 #
993 997 # realIndex = numpy.where(spacing > 10 )[0]
994 998 #
995 999 # if len(realIndex) < 2:
996 1000 # #Solo se encontro un pulso de transmision con ancho mayor a 1
997 1001 # return None
998 1002 #
999 1003 # #Eliminar pulsos anchos (deja solo la diferencia entre IPPs)
1000 1004 # realPulseIndex = pulseIndex[realIndex]
1001 1005 #
1002 1006 # period = mode(realPulseIndex[1:] - realPulseIndex[:-1])[0][0]
1003 1007 #
1004 1008 # print "IPP = %d samples" %period
1005 1009 #
1006 1010 # self.__newNSamples = dataOut.nHeights #int(period)
1007 1011 # self.__startIndex = int(realPulseIndex[0])
1008 1012 #
1009 1013 # return 1
1010 1014 #
1011 1015 #
1012 1016 # def setup(self, nSamples, nChannels, buffer_size = 4):
1013 1017 #
1014 1018 # self.__powBuffer = collections.deque(numpy.zeros( buffer_size*nSamples,dtype=numpy.float),
1015 1019 # maxlen = buffer_size*nSamples)
1016 1020 #
1017 1021 # bufferList = []
1018 1022 #
1019 1023 # for i in range(nChannels):
1020 1024 # bufferByChannel = collections.deque(numpy.zeros( buffer_size*nSamples, dtype=numpy.complex) + numpy.NAN,
1021 1025 # maxlen = buffer_size*nSamples)
1022 1026 #
1023 1027 # bufferList.append(bufferByChannel)
1024 1028 #
1025 1029 # self.__nSamples = nSamples
1026 1030 # self.__nChannels = nChannels
1027 1031 # self.__bufferList = bufferList
1028 1032 #
1029 1033 # def run(self, dataOut, channel = 0):
1030 1034 #
1031 1035 # if not self.isConfig:
1032 1036 # nSamples = dataOut.nHeights
1033 1037 # nChannels = dataOut.nChannels
1034 1038 # self.setup(nSamples, nChannels)
1035 1039 # self.isConfig = True
1036 1040 #
1037 1041 # #Append new data to internal buffer
1038 1042 # for thisChannel in range(self.__nChannels):
1039 1043 # bufferByChannel = self.__bufferList[thisChannel]
1040 1044 # bufferByChannel.extend(dataOut.data[thisChannel])
1041 1045 #
1042 1046 # if self.__pulseFound:
1043 1047 # self.__startIndex -= self.__nSamples
1044 1048 #
1045 1049 # #Finding Tx Pulse
1046 1050 # if not self.__pulseFound:
1047 1051 # indexFound = self.__findTxPulse(dataOut, channel)
1048 1052 #
1049 1053 # if indexFound == None:
1050 1054 # dataOut.flagNoData = True
1051 1055 # return
1052 1056 #
1053 1057 # self.__arrayBuffer = numpy.zeros((self.__nChannels, self.__newNSamples), dtype = numpy.complex)
1054 1058 # self.__pulseFound = True
1055 1059 # self.__startIndex = indexFound
1056 1060 #
1057 1061 # #If pulse was found ...
1058 1062 # for thisChannel in range(self.__nChannels):
1059 1063 # bufferByChannel = self.__bufferList[thisChannel]
1060 1064 # #print self.__startIndex
1061 1065 # x = numpy.array(bufferByChannel)
1062 1066 # self.__arrayBuffer[thisChannel] = x[self.__startIndex:self.__startIndex+self.__newNSamples]
1063 1067 #
1064 1068 # deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
1065 1069 # dataOut.heightList = numpy.arange(self.__newNSamples)*deltaHeight
1066 1070 # # dataOut.ippSeconds = (self.__newNSamples / deltaHeight)/1e6
1067 1071 #
1068 1072 # dataOut.data = self.__arrayBuffer
1069 1073 #
1070 1074 # self.__startIndex += self.__newNSamples
1071 1075 #
1072 1076 # return No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now