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