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