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