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