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