##// END OF EJS Templates
Remove numpy.complex
Juan C. Espinoza -
r1595:fed0fad1169f
parent child
Show More

The requested changes are too big and content was truncated. Show full diff

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