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