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