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