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