##// END OF EJS Templates
fix python3 compatibility in removeInterference operation
Juan C. Espinoza -
r1197:5a7f256d19e7
parent child
Show More
@@ -1,1060 +1,1060
1 1 import itertools
2 2
3 3 import numpy
4 4
5 5 from schainpy.model.proc.jroproc_base import ProcessingUnit, MPDecorator, Operation
6 6 from schainpy.model.data.jrodata import Spectra
7 7 from schainpy.model.data.jrodata import hildebrand_sekhon
8 8 from schainpy.utils import log
9 9
10 10 @MPDecorator
11 11 class SpectraProc(ProcessingUnit):
12 12
13 13
14 14 def __init__(self):
15 15
16 16 ProcessingUnit.__init__(self)
17 17
18 18 self.buffer = None
19 19 self.firstdatatime = None
20 20 self.profIndex = 0
21 21 self.dataOut = Spectra()
22 22 self.id_min = None
23 23 self.id_max = None
24 24 self.setupReq = False #Agregar a todas las unidades de proc
25 25
26 26 def __updateSpecFromVoltage(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 try:
33 33 self.dataOut.processingHeaderObj = self.dataIn.processingHeaderObj.copy()
34 34 except:
35 35 pass
36 36 self.dataOut.radarControllerHeaderObj = self.dataIn.radarControllerHeaderObj.copy()
37 37 self.dataOut.systemHeaderObj = self.dataIn.systemHeaderObj.copy()
38 38 self.dataOut.channelList = self.dataIn.channelList
39 39 self.dataOut.heightList = self.dataIn.heightList
40 40 self.dataOut.dtype = numpy.dtype([('real', '<f4'), ('imag', '<f4')])
41 41
42 42 self.dataOut.nBaud = self.dataIn.nBaud
43 43 self.dataOut.nCode = self.dataIn.nCode
44 44 self.dataOut.code = self.dataIn.code
45 45 self.dataOut.nProfiles = self.dataOut.nFFTPoints
46 46
47 47 self.dataOut.flagDiscontinuousBlock = self.dataIn.flagDiscontinuousBlock
48 48 self.dataOut.utctime = self.firstdatatime
49 49 # asumo q la data esta decodificada
50 50 self.dataOut.flagDecodeData = self.dataIn.flagDecodeData
51 51 # asumo q la data esta sin flip
52 52 self.dataOut.flagDeflipData = self.dataIn.flagDeflipData
53 53 self.dataOut.flagShiftFFT = False
54 54
55 55 self.dataOut.nCohInt = self.dataIn.nCohInt
56 56 self.dataOut.nIncohInt = 1
57 57
58 58 self.dataOut.windowOfFilter = self.dataIn.windowOfFilter
59 59
60 60 self.dataOut.frequency = self.dataIn.frequency
61 61 self.dataOut.realtime = self.dataIn.realtime
62 62
63 63 self.dataOut.azimuth = self.dataIn.azimuth
64 64 self.dataOut.zenith = self.dataIn.zenith
65 65
66 66 self.dataOut.beam.codeList = self.dataIn.beam.codeList
67 67 self.dataOut.beam.azimuthList = self.dataIn.beam.azimuthList
68 68 self.dataOut.beam.zenithList = self.dataIn.beam.zenithList
69 69
70 70 def __getFft(self):
71 71 """
72 72 Convierte valores de Voltaje a Spectra
73 73
74 74 Affected:
75 75 self.dataOut.data_spc
76 76 self.dataOut.data_cspc
77 77 self.dataOut.data_dc
78 78 self.dataOut.heightList
79 79 self.profIndex
80 80 self.buffer
81 81 self.dataOut.flagNoData
82 82 """
83 83 fft_volt = numpy.fft.fft(
84 84 self.buffer, n=self.dataOut.nFFTPoints, axis=1)
85 85 fft_volt = fft_volt.astype(numpy.dtype('complex'))
86 86 dc = fft_volt[:, 0, :]
87 87
88 88 # calculo de self-spectra
89 89 fft_volt = numpy.fft.fftshift(fft_volt, axes=(1,))
90 90 spc = fft_volt * numpy.conjugate(fft_volt)
91 91 spc = spc.real
92 92
93 93 blocksize = 0
94 94 blocksize += dc.size
95 95 blocksize += spc.size
96 96
97 97 cspc = None
98 98 pairIndex = 0
99 99 if self.dataOut.pairsList != None:
100 100 # calculo de cross-spectra
101 101 cspc = numpy.zeros(
102 102 (self.dataOut.nPairs, self.dataOut.nFFTPoints, self.dataOut.nHeights), dtype='complex')
103 103 for pair in self.dataOut.pairsList:
104 104 if pair[0] not in self.dataOut.channelList:
105 105 raise ValueError("Error getting CrossSpectra: pair 0 of %s is not in channelList = %s" % (
106 106 str(pair), str(self.dataOut.channelList)))
107 107 if pair[1] not in self.dataOut.channelList:
108 108 raise ValueError("Error getting CrossSpectra: pair 1 of %s is not in channelList = %s" % (
109 109 str(pair), str(self.dataOut.channelList)))
110 110
111 111 cspc[pairIndex, :, :] = fft_volt[pair[0], :, :] * \
112 112 numpy.conjugate(fft_volt[pair[1], :, :])
113 113 pairIndex += 1
114 114 blocksize += cspc.size
115 115
116 116 self.dataOut.data_spc = spc
117 117 self.dataOut.data_cspc = cspc
118 118 self.dataOut.data_dc = dc
119 119 self.dataOut.blockSize = blocksize
120 120 self.dataOut.flagShiftFFT = True
121 121
122 122 def run(self, nProfiles=None, nFFTPoints=None, pairsList=[], ippFactor=None, shift_fft=False):
123 123
124 124 if self.dataIn.type == "Spectra":
125 125 self.dataOut.copy(self.dataIn)
126 126 if shift_fft:
127 127 #desplaza a la derecha en el eje 2 determinadas posiciones
128 128 shift = int(self.dataOut.nFFTPoints/2)
129 129 self.dataOut.data_spc = numpy.roll(self.dataOut.data_spc, shift , axis=1)
130 130
131 131 if self.dataOut.data_cspc is not None:
132 132 #desplaza a la derecha en el eje 2 determinadas posiciones
133 133 self.dataOut.data_cspc = numpy.roll(self.dataOut.data_cspc, shift, axis=1)
134 134
135 135 return True
136 136
137 137 if self.dataIn.type == "Voltage":
138 138
139 139 self.dataOut.flagNoData = True
140 140
141 141 if nFFTPoints == None:
142 142 raise ValueError("This SpectraProc.run() need nFFTPoints input variable")
143 143
144 144 if nProfiles == None:
145 145 nProfiles = nFFTPoints
146 146
147 147 if ippFactor == None:
148 148 ippFactor = 1
149 149
150 150 self.dataOut.ippFactor = ippFactor
151 151
152 152 self.dataOut.nFFTPoints = nFFTPoints
153 153 self.dataOut.pairsList = pairsList
154 154
155 155 if self.buffer is None:
156 156 self.buffer = numpy.zeros((self.dataIn.nChannels,
157 157 nProfiles,
158 158 self.dataIn.nHeights),
159 159 dtype='complex')
160 160
161 161 if self.dataIn.flagDataAsBlock:
162 162 nVoltProfiles = self.dataIn.data.shape[1]
163 163
164 164 if nVoltProfiles == nProfiles:
165 165 self.buffer = self.dataIn.data.copy()
166 166 self.profIndex = nVoltProfiles
167 167
168 168 elif nVoltProfiles < nProfiles:
169 169
170 170 if self.profIndex == 0:
171 171 self.id_min = 0
172 172 self.id_max = nVoltProfiles
173 173
174 174 self.buffer[:, self.id_min:self.id_max,
175 175 :] = self.dataIn.data
176 176 self.profIndex += nVoltProfiles
177 177 self.id_min += nVoltProfiles
178 178 self.id_max += nVoltProfiles
179 179 else:
180 180 raise ValueError("The type object %s has %d profiles, it should just has %d profiles" % (
181 181 self.dataIn.type, self.dataIn.data.shape[1], nProfiles))
182 182 self.dataOut.flagNoData = True
183 183 return 0
184 184 else:
185 185 self.buffer[:, self.profIndex, :] = self.dataIn.data.copy()
186 186 self.profIndex += 1
187 187
188 188 if self.firstdatatime == None:
189 189 self.firstdatatime = self.dataIn.utctime
190 190
191 191 if self.profIndex == nProfiles:
192 192 self.__updateSpecFromVoltage()
193 193 self.__getFft()
194 194
195 195 self.dataOut.flagNoData = False
196 196 self.firstdatatime = None
197 197 self.profIndex = 0
198 198
199 199 return True
200 200
201 201 raise ValueError("The type of input object '%s' is not valid" % (
202 202 self.dataIn.type))
203 203
204 204 def __selectPairs(self, pairsList):
205 205
206 206 if not pairsList:
207 207 return
208 208
209 209 pairs = []
210 210 pairsIndex = []
211 211
212 212 for pair in pairsList:
213 213 if pair[0] not in self.dataOut.channelList or pair[1] not in self.dataOut.channelList:
214 214 continue
215 215 pairs.append(pair)
216 216 pairsIndex.append(pairs.index(pair))
217 217
218 218 self.dataOut.data_cspc = self.dataOut.data_cspc[pairsIndex]
219 219 self.dataOut.pairsList = pairs
220 220
221 221 return
222 222
223 223 def __selectPairsByChannel(self, channelList=None):
224 224
225 225 if channelList == None:
226 226 return
227 227
228 228 pairsIndexListSelected = []
229 229 for pairIndex in self.dataOut.pairsIndexList:
230 230 # First pair
231 231 if self.dataOut.pairsList[pairIndex][0] not in channelList:
232 232 continue
233 233 # Second pair
234 234 if self.dataOut.pairsList[pairIndex][1] not in channelList:
235 235 continue
236 236
237 237 pairsIndexListSelected.append(pairIndex)
238 238
239 239 if not pairsIndexListSelected:
240 240 self.dataOut.data_cspc = None
241 241 self.dataOut.pairsList = []
242 242 return
243 243
244 244 self.dataOut.data_cspc = self.dataOut.data_cspc[pairsIndexListSelected]
245 245 self.dataOut.pairsList = [self.dataOut.pairsList[i]
246 246 for i in pairsIndexListSelected]
247 247
248 248 return
249 249
250 250 def selectChannels(self, channelList):
251 251
252 252 channelIndexList = []
253 253
254 254 for channel in channelList:
255 255 if channel not in self.dataOut.channelList:
256 256 raise ValueError("Error selecting channels, Channel %d is not valid.\nAvailable channels = %s" % (
257 257 channel, str(self.dataOut.channelList)))
258 258
259 259 index = self.dataOut.channelList.index(channel)
260 260 channelIndexList.append(index)
261 261
262 262 self.selectChannelsByIndex(channelIndexList)
263 263
264 264 def selectChannelsByIndex(self, channelIndexList):
265 265 """
266 266 Selecciona un bloque de datos en base a canales segun el channelIndexList
267 267
268 268 Input:
269 269 channelIndexList : lista sencilla de canales a seleccionar por ej. [2,3,7]
270 270
271 271 Affected:
272 272 self.dataOut.data_spc
273 273 self.dataOut.channelIndexList
274 274 self.dataOut.nChannels
275 275
276 276 Return:
277 277 None
278 278 """
279 279
280 280 for channelIndex in channelIndexList:
281 281 if channelIndex not in self.dataOut.channelIndexList:
282 282 raise ValueError("Error selecting channels: The value %d in channelIndexList is not valid.\nAvailable channel indexes = " % (
283 283 channelIndex, self.dataOut.channelIndexList))
284 284
285 285 # nChannels = len(channelIndexList)
286 286
287 287 data_spc = self.dataOut.data_spc[channelIndexList, :]
288 288 data_dc = self.dataOut.data_dc[channelIndexList, :]
289 289
290 290 self.dataOut.data_spc = data_spc
291 291 self.dataOut.data_dc = data_dc
292 292
293 293 self.dataOut.channelList = [
294 294 self.dataOut.channelList[i] for i in channelIndexList]
295 295 # self.dataOut.nChannels = nChannels
296 296
297 297 self.__selectPairsByChannel(self.dataOut.channelList)
298 298
299 299 return 1
300 300
301 301
302 302 def selectFFTs(self, minFFT, maxFFT ):
303 303 """
304 304 Selecciona un bloque de datos en base a un grupo de valores de puntos FFTs segun el rango
305 305 minFFT<= FFT <= maxFFT
306 306 """
307 307
308 308 if (minFFT > maxFFT):
309 309 raise ValueError("Error selecting heights: Height range (%d,%d) is not valid" % (minFFT, maxFFT))
310 310
311 311 if (minFFT < self.dataOut.getFreqRange()[0]):
312 312 minFFT = self.dataOut.getFreqRange()[0]
313 313
314 314 if (maxFFT > self.dataOut.getFreqRange()[-1]):
315 315 maxFFT = self.dataOut.getFreqRange()[-1]
316 316
317 317 minIndex = 0
318 318 maxIndex = 0
319 319 FFTs = self.dataOut.getFreqRange()
320 320
321 321 inda = numpy.where(FFTs >= minFFT)
322 322 indb = numpy.where(FFTs <= maxFFT)
323 323
324 324 try:
325 325 minIndex = inda[0][0]
326 326 except:
327 327 minIndex = 0
328 328
329 329 try:
330 330 maxIndex = indb[0][-1]
331 331 except:
332 332 maxIndex = len(FFTs)
333 333
334 334 self.selectFFTsByIndex(minIndex, maxIndex)
335 335
336 336 return 1
337 337
338 338
339 339 def setH0(self, h0, deltaHeight = None):
340 340
341 341 if not deltaHeight:
342 342 deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0]
343 343
344 344 nHeights = self.dataOut.nHeights
345 345
346 346 newHeiRange = h0 + numpy.arange(nHeights)*deltaHeight
347 347
348 348 self.dataOut.heightList = newHeiRange
349 349
350 350
351 351 def selectHeights(self, minHei, maxHei):
352 352 """
353 353 Selecciona un bloque de datos en base a un grupo de valores de alturas segun el rango
354 354 minHei <= height <= maxHei
355 355
356 356 Input:
357 357 minHei : valor minimo de altura a considerar
358 358 maxHei : valor maximo de altura a considerar
359 359
360 360 Affected:
361 361 Indirectamente son cambiados varios valores a travez del metodo selectHeightsByIndex
362 362
363 363 Return:
364 364 1 si el metodo se ejecuto con exito caso contrario devuelve 0
365 365 """
366 366
367 367
368 368 if (minHei > maxHei):
369 369 raise ValueError("Error selecting heights: Height range (%d,%d) is not valid" % (minHei, maxHei))
370 370
371 371 if (minHei < self.dataOut.heightList[0]):
372 372 minHei = self.dataOut.heightList[0]
373 373
374 374 if (maxHei > self.dataOut.heightList[-1]):
375 375 maxHei = self.dataOut.heightList[-1]
376 376
377 377 minIndex = 0
378 378 maxIndex = 0
379 379 heights = self.dataOut.heightList
380 380
381 381 inda = numpy.where(heights >= minHei)
382 382 indb = numpy.where(heights <= maxHei)
383 383
384 384 try:
385 385 minIndex = inda[0][0]
386 386 except:
387 387 minIndex = 0
388 388
389 389 try:
390 390 maxIndex = indb[0][-1]
391 391 except:
392 392 maxIndex = len(heights)
393 393
394 394 self.selectHeightsByIndex(minIndex, maxIndex)
395 395
396 396
397 397 return 1
398 398
399 399 def getBeaconSignal(self, tauindex=0, channelindex=0, hei_ref=None):
400 400 newheis = numpy.where(
401 401 self.dataOut.heightList > self.dataOut.radarControllerHeaderObj.Taus[tauindex])
402 402
403 403 if hei_ref != None:
404 404 newheis = numpy.where(self.dataOut.heightList > hei_ref)
405 405
406 406 minIndex = min(newheis[0])
407 407 maxIndex = max(newheis[0])
408 408 data_spc = self.dataOut.data_spc[:, :, minIndex:maxIndex + 1]
409 409 heightList = self.dataOut.heightList[minIndex:maxIndex + 1]
410 410
411 411 # determina indices
412 412 nheis = int(self.dataOut.radarControllerHeaderObj.txB /
413 413 (self.dataOut.heightList[1] - self.dataOut.heightList[0]))
414 414 avg_dB = 10 * \
415 415 numpy.log10(numpy.sum(data_spc[channelindex, :, :], axis=0))
416 416 beacon_dB = numpy.sort(avg_dB)[-nheis:]
417 417 beacon_heiIndexList = []
418 418 for val in avg_dB.tolist():
419 419 if val >= beacon_dB[0]:
420 420 beacon_heiIndexList.append(avg_dB.tolist().index(val))
421 421
422 422 #data_spc = data_spc[:,:,beacon_heiIndexList]
423 423 data_cspc = None
424 424 if self.dataOut.data_cspc is not None:
425 425 data_cspc = self.dataOut.data_cspc[:, :, minIndex:maxIndex + 1]
426 426 #data_cspc = data_cspc[:,:,beacon_heiIndexList]
427 427
428 428 data_dc = None
429 429 if self.dataOut.data_dc is not None:
430 430 data_dc = self.dataOut.data_dc[:, minIndex:maxIndex + 1]
431 431 #data_dc = data_dc[:,beacon_heiIndexList]
432 432
433 433 self.dataOut.data_spc = data_spc
434 434 self.dataOut.data_cspc = data_cspc
435 435 self.dataOut.data_dc = data_dc
436 436 self.dataOut.heightList = heightList
437 437 self.dataOut.beacon_heiIndexList = beacon_heiIndexList
438 438
439 439 return 1
440 440
441 441 def selectFFTsByIndex(self, minIndex, maxIndex):
442 442 """
443 443
444 444 """
445 445
446 446 if (minIndex < 0) or (minIndex > maxIndex):
447 447 raise ValueError("Error selecting heights: Index range (%d,%d) is not valid" % (minIndex, maxIndex))
448 448
449 449 if (maxIndex >= self.dataOut.nProfiles):
450 450 maxIndex = self.dataOut.nProfiles-1
451 451
452 452 #Spectra
453 453 data_spc = self.dataOut.data_spc[:,minIndex:maxIndex+1,:]
454 454
455 455 data_cspc = None
456 456 if self.dataOut.data_cspc is not None:
457 457 data_cspc = self.dataOut.data_cspc[:,minIndex:maxIndex+1,:]
458 458
459 459 data_dc = None
460 460 if self.dataOut.data_dc is not None:
461 461 data_dc = self.dataOut.data_dc[minIndex:maxIndex+1,:]
462 462
463 463 self.dataOut.data_spc = data_spc
464 464 self.dataOut.data_cspc = data_cspc
465 465 self.dataOut.data_dc = data_dc
466 466
467 467 self.dataOut.ippSeconds = self.dataOut.ippSeconds*(self.dataOut.nFFTPoints / numpy.shape(data_cspc)[1])
468 468 self.dataOut.nFFTPoints = numpy.shape(data_cspc)[1]
469 469 self.dataOut.profilesPerBlock = numpy.shape(data_cspc)[1]
470 470
471 471 return 1
472 472
473 473
474 474
475 475 def selectHeightsByIndex(self, minIndex, maxIndex):
476 476 """
477 477 Selecciona un bloque de datos en base a un grupo indices de alturas segun el rango
478 478 minIndex <= index <= maxIndex
479 479
480 480 Input:
481 481 minIndex : valor de indice minimo de altura a considerar
482 482 maxIndex : valor de indice maximo de altura a considerar
483 483
484 484 Affected:
485 485 self.dataOut.data_spc
486 486 self.dataOut.data_cspc
487 487 self.dataOut.data_dc
488 488 self.dataOut.heightList
489 489
490 490 Return:
491 491 1 si el metodo se ejecuto con exito caso contrario devuelve 0
492 492 """
493 493
494 494 if (minIndex < 0) or (minIndex > maxIndex):
495 495 raise ValueError("Error selecting heights: Index range (%d,%d) is not valid" % (
496 496 minIndex, maxIndex))
497 497
498 498 if (maxIndex >= self.dataOut.nHeights):
499 499 maxIndex = self.dataOut.nHeights - 1
500 500
501 501 # Spectra
502 502 data_spc = self.dataOut.data_spc[:, :, minIndex:maxIndex + 1]
503 503
504 504 data_cspc = None
505 505 if self.dataOut.data_cspc is not None:
506 506 data_cspc = self.dataOut.data_cspc[:, :, minIndex:maxIndex + 1]
507 507
508 508 data_dc = None
509 509 if self.dataOut.data_dc is not None:
510 510 data_dc = self.dataOut.data_dc[:, minIndex:maxIndex + 1]
511 511
512 512 self.dataOut.data_spc = data_spc
513 513 self.dataOut.data_cspc = data_cspc
514 514 self.dataOut.data_dc = data_dc
515 515
516 516 self.dataOut.heightList = self.dataOut.heightList[minIndex:maxIndex + 1]
517 517
518 518 return 1
519 519
520 520 def removeDC(self, mode=2):
521 521 jspectra = self.dataOut.data_spc
522 522 jcspectra = self.dataOut.data_cspc
523 523
524 524 num_chan = jspectra.shape[0]
525 525 num_hei = jspectra.shape[2]
526 526
527 527 if jcspectra is not None:
528 528 jcspectraExist = True
529 529 num_pairs = jcspectra.shape[0]
530 530 else:
531 531 jcspectraExist = False
532 532
533 533 freq_dc = int(jspectra.shape[1] / 2)
534 534 ind_vel = numpy.array([-2, -1, 1, 2]) + freq_dc
535 535 ind_vel = ind_vel.astype(int)
536 536
537 537 if ind_vel[0] < 0:
538 538 ind_vel[list(range(0, 1))] = ind_vel[list(range(0, 1))] + self.num_prof
539 539
540 540 if mode == 1:
541 541 jspectra[:, freq_dc, :] = (
542 542 jspectra[:, ind_vel[1], :] + jspectra[:, ind_vel[2], :]) / 2 # CORRECCION
543 543
544 544 if jcspectraExist:
545 545 jcspectra[:, freq_dc, :] = (
546 546 jcspectra[:, ind_vel[1], :] + jcspectra[:, ind_vel[2], :]) / 2
547 547
548 548 if mode == 2:
549 549
550 550 vel = numpy.array([-2, -1, 1, 2])
551 551 xx = numpy.zeros([4, 4])
552 552
553 553 for fil in range(4):
554 554 xx[fil, :] = vel[fil]**numpy.asarray(list(range(4)))
555 555
556 556 xx_inv = numpy.linalg.inv(xx)
557 557 xx_aux = xx_inv[0, :]
558 558
559 559 for ich in range(num_chan):
560 560 yy = jspectra[ich, ind_vel, :]
561 561 jspectra[ich, freq_dc, :] = numpy.dot(xx_aux, yy)
562 562
563 563 junkid = jspectra[ich, freq_dc, :] <= 0
564 564 cjunkid = sum(junkid)
565 565
566 566 if cjunkid.any():
567 567 jspectra[ich, freq_dc, junkid.nonzero()] = (
568 568 jspectra[ich, ind_vel[1], junkid] + jspectra[ich, ind_vel[2], junkid]) / 2
569 569
570 570 if jcspectraExist:
571 571 for ip in range(num_pairs):
572 572 yy = jcspectra[ip, ind_vel, :]
573 573 jcspectra[ip, freq_dc, :] = numpy.dot(xx_aux, yy)
574 574
575 575 self.dataOut.data_spc = jspectra
576 576 self.dataOut.data_cspc = jcspectra
577 577
578 578 return 1
579 579
580 580 def removeInterference2(self):
581 581
582 582 cspc = self.dataOut.data_cspc
583 583 spc = self.dataOut.data_spc
584 584 Heights = numpy.arange(cspc.shape[2])
585 585 realCspc = numpy.abs(cspc)
586 586
587 587 for i in range(cspc.shape[0]):
588 588 LinePower= numpy.sum(realCspc[i], axis=0)
589 589 Threshold = numpy.amax(LinePower)-numpy.sort(LinePower)[len(Heights)-int(len(Heights)*0.1)]
590 590 SelectedHeights = Heights[ numpy.where( LinePower < Threshold ) ]
591 591 InterferenceSum = numpy.sum( realCspc[i,:,SelectedHeights], axis=0 )
592 592 InterferenceThresholdMin = numpy.sort(InterferenceSum)[int(len(InterferenceSum)*0.98)]
593 593 InterferenceThresholdMax = numpy.sort(InterferenceSum)[int(len(InterferenceSum)*0.99)]
594 594
595 595
596 596 InterferenceRange = numpy.where( ([InterferenceSum > InterferenceThresholdMin]))# , InterferenceSum < InterferenceThresholdMax]) )
597 597 #InterferenceRange = numpy.where( ([InterferenceRange < InterferenceThresholdMax]))
598 598 if len(InterferenceRange)<int(cspc.shape[1]*0.3):
599 599 cspc[i,InterferenceRange,:] = numpy.NaN
600 600
601 601
602 602
603 603 self.dataOut.data_cspc = cspc
604 604
605 605 def removeInterference(self, interf = 2,hei_interf = None, nhei_interf = None, offhei_interf = None):
606 606
607 607 jspectra = self.dataOut.data_spc
608 608 jcspectra = self.dataOut.data_cspc
609 609 jnoise = self.dataOut.getNoise()
610 610 num_incoh = self.dataOut.nIncohInt
611 611
612 612 num_channel = jspectra.shape[0]
613 613 num_prof = jspectra.shape[1]
614 614 num_hei = jspectra.shape[2]
615 615
616 616 # hei_interf
617 617 if hei_interf is None:
618 count_hei = num_hei / 2 # Como es entero no importa
618 count_hei = int(num_hei / 2)
619 619 hei_interf = numpy.asmatrix(list(range(count_hei))) + num_hei - count_hei
620 620 hei_interf = numpy.asarray(hei_interf)[0]
621 621 # nhei_interf
622 622 if (nhei_interf == None):
623 623 nhei_interf = 5
624 624 if (nhei_interf < 1):
625 625 nhei_interf = 1
626 626 if (nhei_interf > count_hei):
627 627 nhei_interf = count_hei
628 628 if (offhei_interf == None):
629 629 offhei_interf = 0
630 630
631 631 ind_hei = list(range(num_hei))
632 632 # mask_prof = numpy.asarray(range(num_prof - 2)) + 1
633 633 # mask_prof[range(num_prof/2 - 1,len(mask_prof))] += 1
634 634 mask_prof = numpy.asarray(list(range(num_prof)))
635 635 num_mask_prof = mask_prof.size
636 636 comp_mask_prof = [0, num_prof / 2]
637 637
638 638 # noise_exist: Determina si la variable jnoise ha sido definida y contiene la informacion del ruido de cada canal
639 639 if (jnoise.size < num_channel or numpy.isnan(jnoise).any()):
640 640 jnoise = numpy.nan
641 641 noise_exist = jnoise[0] < numpy.Inf
642 642
643 643 # Subrutina de Remocion de la Interferencia
644 644 for ich in range(num_channel):
645 645 # Se ordena los espectros segun su potencia (menor a mayor)
646 646 power = jspectra[ich, mask_prof, :]
647 647 power = power[:, hei_interf]
648 648 power = power.sum(axis=0)
649 649 psort = power.ravel().argsort()
650 650
651 651 # Se estima la interferencia promedio en los Espectros de Potencia empleando
652 652 junkspc_interf = jspectra[ich, :, hei_interf[psort[list(range(
653 653 offhei_interf, nhei_interf + offhei_interf))]]]
654 654
655 655 if noise_exist:
656 656 # tmp_noise = jnoise[ich] / num_prof
657 657 tmp_noise = jnoise[ich]
658 658 junkspc_interf = junkspc_interf - tmp_noise
659 659 #junkspc_interf[:,comp_mask_prof] = 0
660 660
661 661 jspc_interf = junkspc_interf.sum(axis=0) / nhei_interf
662 662 jspc_interf = jspc_interf.transpose()
663 663 # Calculando el espectro de interferencia promedio
664 664 noiseid = numpy.where(
665 665 jspc_interf <= tmp_noise / numpy.sqrt(num_incoh))
666 666 noiseid = noiseid[0]
667 667 cnoiseid = noiseid.size
668 668 interfid = numpy.where(
669 669 jspc_interf > tmp_noise / numpy.sqrt(num_incoh))
670 670 interfid = interfid[0]
671 671 cinterfid = interfid.size
672 672
673 673 if (cnoiseid > 0):
674 674 jspc_interf[noiseid] = 0
675 675
676 676 # Expandiendo los perfiles a limpiar
677 677 if (cinterfid > 0):
678 678 new_interfid = (
679 679 numpy.r_[interfid - 1, interfid, interfid + 1] + num_prof) % num_prof
680 680 new_interfid = numpy.asarray(new_interfid)
681 681 new_interfid = {x for x in new_interfid}
682 682 new_interfid = numpy.array(list(new_interfid))
683 683 new_cinterfid = new_interfid.size
684 684 else:
685 685 new_cinterfid = 0
686 686
687 687 for ip in range(new_cinterfid):
688 688 ind = junkspc_interf[:, new_interfid[ip]].ravel().argsort()
689 689 jspc_interf[new_interfid[ip]
690 690 ] = junkspc_interf[ind[nhei_interf / 2], new_interfid[ip]]
691 691
692 692 jspectra[ich, :, ind_hei] = jspectra[ich, :,
693 693 ind_hei] - jspc_interf # Corregir indices
694 694
695 695 # Removiendo la interferencia del punto de mayor interferencia
696 696 ListAux = jspc_interf[mask_prof].tolist()
697 697 maxid = ListAux.index(max(ListAux))
698 698
699 699 if cinterfid > 0:
700 700 for ip in range(cinterfid * (interf == 2) - 1):
701 701 ind = (jspectra[ich, interfid[ip], :] < tmp_noise *
702 702 (1 + 1 / numpy.sqrt(num_incoh))).nonzero()
703 703 cind = len(ind)
704 704
705 705 if (cind > 0):
706 706 jspectra[ich, interfid[ip], ind] = tmp_noise * \
707 707 (1 + (numpy.random.uniform(cind) - 0.5) /
708 708 numpy.sqrt(num_incoh))
709 709
710 710 ind = numpy.array([-2, -1, 1, 2])
711 711 xx = numpy.zeros([4, 4])
712 712
713 713 for id1 in range(4):
714 714 xx[:, id1] = ind[id1]**numpy.asarray(list(range(4)))
715 715
716 716 xx_inv = numpy.linalg.inv(xx)
717 717 xx = xx_inv[:, 0]
718 718 ind = (ind + maxid + num_mask_prof) % num_mask_prof
719 719 yy = jspectra[ich, mask_prof[ind], :]
720 720 jspectra[ich, mask_prof[maxid], :] = numpy.dot(
721 721 yy.transpose(), xx)
722 722
723 723 indAux = (jspectra[ich, :, :] < tmp_noise *
724 724 (1 - 1 / numpy.sqrt(num_incoh))).nonzero()
725 725 jspectra[ich, indAux[0], indAux[1]] = tmp_noise * \
726 726 (1 - 1 / numpy.sqrt(num_incoh))
727 727
728 728 # Remocion de Interferencia en el Cross Spectra
729 729 if jcspectra is None:
730 730 return jspectra, jcspectra
731 num_pairs = jcspectra.size / (num_prof * num_hei)
731 num_pairs = int(jcspectra.size / (num_prof * num_hei))
732 732 jcspectra = jcspectra.reshape(num_pairs, num_prof, num_hei)
733 733
734 734 for ip in range(num_pairs):
735 735
736 736 #-------------------------------------------
737 737
738 738 cspower = numpy.abs(jcspectra[ip, mask_prof, :])
739 739 cspower = cspower[:, hei_interf]
740 740 cspower = cspower.sum(axis=0)
741 741
742 742 cspsort = cspower.ravel().argsort()
743 743 junkcspc_interf = jcspectra[ip, :, hei_interf[cspsort[list(range(
744 744 offhei_interf, nhei_interf + offhei_interf))]]]
745 745 junkcspc_interf = junkcspc_interf.transpose()
746 746 jcspc_interf = junkcspc_interf.sum(axis=1) / nhei_interf
747 747
748 748 ind = numpy.abs(jcspc_interf[mask_prof]).ravel().argsort()
749 749
750 750 median_real = numpy.median(numpy.real(
751 751 junkcspc_interf[mask_prof[ind[list(range(3 * num_prof / 4))]], :]))
752 752 median_imag = numpy.median(numpy.imag(
753 753 junkcspc_interf[mask_prof[ind[list(range(3 * num_prof / 4))]], :]))
754 754 junkcspc_interf[comp_mask_prof, :] = numpy.complex(
755 755 median_real, median_imag)
756 756
757 757 for iprof in range(num_prof):
758 758 ind = numpy.abs(junkcspc_interf[iprof, :]).ravel().argsort()
759 759 jcspc_interf[iprof] = junkcspc_interf[iprof,
760 760 ind[nhei_interf / 2]]
761 761
762 762 # Removiendo la Interferencia
763 763 jcspectra[ip, :, ind_hei] = jcspectra[ip,
764 764 :, ind_hei] - jcspc_interf
765 765
766 766 ListAux = numpy.abs(jcspc_interf[mask_prof]).tolist()
767 767 maxid = ListAux.index(max(ListAux))
768 768
769 769 ind = numpy.array([-2, -1, 1, 2])
770 770 xx = numpy.zeros([4, 4])
771 771
772 772 for id1 in range(4):
773 773 xx[:, id1] = ind[id1]**numpy.asarray(list(range(4)))
774 774
775 775 xx_inv = numpy.linalg.inv(xx)
776 776 xx = xx_inv[:, 0]
777 777
778 778 ind = (ind + maxid + num_mask_prof) % num_mask_prof
779 779 yy = jcspectra[ip, mask_prof[ind], :]
780 780 jcspectra[ip, mask_prof[maxid], :] = numpy.dot(yy.transpose(), xx)
781 781
782 782 # Guardar Resultados
783 783 self.dataOut.data_spc = jspectra
784 784 self.dataOut.data_cspc = jcspectra
785 785
786 786 return 1
787 787
788 788 def setRadarFrequency(self, frequency=None):
789 789
790 790 if frequency != None:
791 791 self.dataOut.frequency = frequency
792 792
793 793 return 1
794 794
795 795 def getNoise(self, minHei=None, maxHei=None, minVel=None, maxVel=None):
796 796 # validacion de rango
797 797 if minHei == None:
798 798 minHei = self.dataOut.heightList[0]
799 799
800 800 if maxHei == None:
801 801 maxHei = self.dataOut.heightList[-1]
802 802
803 803 if (minHei < self.dataOut.heightList[0]) or (minHei > maxHei):
804 804 print('minHei: %.2f is out of the heights range' % (minHei))
805 805 print('minHei is setting to %.2f' % (self.dataOut.heightList[0]))
806 806 minHei = self.dataOut.heightList[0]
807 807
808 808 if (maxHei > self.dataOut.heightList[-1]) or (maxHei < minHei):
809 809 print('maxHei: %.2f is out of the heights range' % (maxHei))
810 810 print('maxHei is setting to %.2f' % (self.dataOut.heightList[-1]))
811 811 maxHei = self.dataOut.heightList[-1]
812 812
813 813 # validacion de velocidades
814 814 velrange = self.dataOut.getVelRange(1)
815 815
816 816 if minVel == None:
817 817 minVel = velrange[0]
818 818
819 819 if maxVel == None:
820 820 maxVel = velrange[-1]
821 821
822 822 if (minVel < velrange[0]) or (minVel > maxVel):
823 823 print('minVel: %.2f is out of the velocity range' % (minVel))
824 824 print('minVel is setting to %.2f' % (velrange[0]))
825 825 minVel = velrange[0]
826 826
827 827 if (maxVel > velrange[-1]) or (maxVel < minVel):
828 828 print('maxVel: %.2f is out of the velocity range' % (maxVel))
829 829 print('maxVel is setting to %.2f' % (velrange[-1]))
830 830 maxVel = velrange[-1]
831 831
832 832 # seleccion de indices para rango
833 833 minIndex = 0
834 834 maxIndex = 0
835 835 heights = self.dataOut.heightList
836 836
837 837 inda = numpy.where(heights >= minHei)
838 838 indb = numpy.where(heights <= maxHei)
839 839
840 840 try:
841 841 minIndex = inda[0][0]
842 842 except:
843 843 minIndex = 0
844 844
845 845 try:
846 846 maxIndex = indb[0][-1]
847 847 except:
848 848 maxIndex = len(heights)
849 849
850 850 if (minIndex < 0) or (minIndex > maxIndex):
851 851 raise ValueError("some value in (%d,%d) is not valid" % (
852 852 minIndex, maxIndex))
853 853
854 854 if (maxIndex >= self.dataOut.nHeights):
855 855 maxIndex = self.dataOut.nHeights - 1
856 856
857 857 # seleccion de indices para velocidades
858 858 indminvel = numpy.where(velrange >= minVel)
859 859 indmaxvel = numpy.where(velrange <= maxVel)
860 860 try:
861 861 minIndexVel = indminvel[0][0]
862 862 except:
863 863 minIndexVel = 0
864 864
865 865 try:
866 866 maxIndexVel = indmaxvel[0][-1]
867 867 except:
868 868 maxIndexVel = len(velrange)
869 869
870 870 # seleccion del espectro
871 871 data_spc = self.dataOut.data_spc[:,
872 872 minIndexVel:maxIndexVel + 1, minIndex:maxIndex + 1]
873 873 # estimacion de ruido
874 874 noise = numpy.zeros(self.dataOut.nChannels)
875 875
876 876 for channel in range(self.dataOut.nChannels):
877 877 daux = data_spc[channel, :, :]
878 878 noise[channel] = hildebrand_sekhon(daux, self.dataOut.nIncohInt)
879 879
880 880 self.dataOut.noise_estimation = noise.copy()
881 881
882 882 return 1
883 883
884 884
885 885 class IncohInt(Operation):
886 886
887 887 __profIndex = 0
888 888 __withOverapping = False
889 889
890 890 __byTime = False
891 891 __initime = None
892 892 __lastdatatime = None
893 893 __integrationtime = None
894 894
895 895 __buffer_spc = None
896 896 __buffer_cspc = None
897 897 __buffer_dc = None
898 898
899 899 __dataReady = False
900 900
901 901 __timeInterval = None
902 902
903 903 n = None
904 904
905 905 def __init__(self):
906 906
907 907 Operation.__init__(self)
908 908
909 909 def setup(self, n=None, timeInterval=None, overlapping=False):
910 910 """
911 911 Set the parameters of the integration class.
912 912
913 913 Inputs:
914 914
915 915 n : Number of coherent integrations
916 916 timeInterval : Time of integration. If the parameter "n" is selected this one does not work
917 917 overlapping :
918 918
919 919 """
920 920
921 921 self.__initime = None
922 922 self.__lastdatatime = 0
923 923
924 924 self.__buffer_spc = 0
925 925 self.__buffer_cspc = 0
926 926 self.__buffer_dc = 0
927 927
928 928 self.__profIndex = 0
929 929 self.__dataReady = False
930 930 self.__byTime = False
931 931
932 932 if n is None and timeInterval is None:
933 933 raise ValueError("n or timeInterval should be specified ...")
934 934
935 935 if n is not None:
936 936 self.n = int(n)
937 937 else:
938 938
939 939 self.__integrationtime = int(timeInterval)
940 940 self.n = None
941 941 self.__byTime = True
942 942
943 943 def putData(self, data_spc, data_cspc, data_dc):
944 944 """
945 945 Add a profile to the __buffer_spc and increase in one the __profileIndex
946 946
947 947 """
948 948
949 949 self.__buffer_spc += data_spc
950 950
951 951 if data_cspc is None:
952 952 self.__buffer_cspc = None
953 953 else:
954 954 self.__buffer_cspc += data_cspc
955 955
956 956 if data_dc is None:
957 957 self.__buffer_dc = None
958 958 else:
959 959 self.__buffer_dc += data_dc
960 960
961 961 self.__profIndex += 1
962 962
963 963 return
964 964
965 965 def pushData(self):
966 966 """
967 967 Return the sum of the last profiles and the profiles used in the sum.
968 968
969 969 Affected:
970 970
971 971 self.__profileIndex
972 972
973 973 """
974 974
975 975 data_spc = self.__buffer_spc
976 976 data_cspc = self.__buffer_cspc
977 977 data_dc = self.__buffer_dc
978 978 n = self.__profIndex
979 979
980 980 self.__buffer_spc = 0
981 981 self.__buffer_cspc = 0
982 982 self.__buffer_dc = 0
983 983 self.__profIndex = 0
984 984
985 985 return data_spc, data_cspc, data_dc, n
986 986
987 987 def byProfiles(self, *args):
988 988
989 989 self.__dataReady = False
990 990 avgdata_spc = None
991 991 avgdata_cspc = None
992 992 avgdata_dc = None
993 993
994 994 self.putData(*args)
995 995
996 996 if self.__profIndex == self.n:
997 997
998 998 avgdata_spc, avgdata_cspc, avgdata_dc, n = self.pushData()
999 999 self.n = n
1000 1000 self.__dataReady = True
1001 1001
1002 1002 return avgdata_spc, avgdata_cspc, avgdata_dc
1003 1003
1004 1004 def byTime(self, datatime, *args):
1005 1005
1006 1006 self.__dataReady = False
1007 1007 avgdata_spc = None
1008 1008 avgdata_cspc = None
1009 1009 avgdata_dc = None
1010 1010
1011 1011 self.putData(*args)
1012 1012
1013 1013 if (datatime - self.__initime) >= self.__integrationtime:
1014 1014 avgdata_spc, avgdata_cspc, avgdata_dc, n = self.pushData()
1015 1015 self.n = n
1016 1016 self.__dataReady = True
1017 1017
1018 1018 return avgdata_spc, avgdata_cspc, avgdata_dc
1019 1019
1020 1020 def integrate(self, datatime, *args):
1021 1021
1022 1022 if self.__profIndex == 0:
1023 1023 self.__initime = datatime
1024 1024
1025 1025 if self.__byTime:
1026 1026 avgdata_spc, avgdata_cspc, avgdata_dc = self.byTime(
1027 1027 datatime, *args)
1028 1028 else:
1029 1029 avgdata_spc, avgdata_cspc, avgdata_dc = self.byProfiles(*args)
1030 1030
1031 1031 if not self.__dataReady:
1032 1032 return None, None, None, None
1033 1033
1034 1034 return self.__initime, avgdata_spc, avgdata_cspc, avgdata_dc
1035 1035
1036 1036 def run(self, dataOut, n=None, timeInterval=None, overlapping=False):
1037 1037 if n == 1:
1038 1038 return
1039 1039
1040 1040 dataOut.flagNoData = True
1041 1041
1042 1042 if not self.isConfig:
1043 1043 self.setup(n, timeInterval, overlapping)
1044 1044 self.isConfig = True
1045 1045
1046 1046 avgdatatime, avgdata_spc, avgdata_cspc, avgdata_dc = self.integrate(dataOut.utctime,
1047 1047 dataOut.data_spc,
1048 1048 dataOut.data_cspc,
1049 1049 dataOut.data_dc)
1050 1050
1051 1051 if self.__dataReady:
1052 1052
1053 1053 dataOut.data_spc = avgdata_spc
1054 1054 dataOut.data_cspc = avgdata_cspc
1055 1055 dataOut.data_dc = avgdata_dc
1056 1056 dataOut.nIncohInt *= self.n
1057 1057 dataOut.utctime = avgdatatime
1058 1058 dataOut.flagNoData = False
1059 1059
1060 1060 return dataOut No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now