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