##// END OF EJS Templates
SpectraProcessor.py:...
Victor Sarmiento -
r85:2d16b3b6fea5
parent child
Show More
@@ -1,186 +1,499
1 '''
1 '''
2 Created on Feb 7, 2012
2 Created on Feb 7, 2012
3
3
4 @author $Author$
4 @author $Author$
5 @version $Id$
5 @version $Id$
6 '''
6 '''
7 import os, sys
7 import os, sys
8 import numpy
8 import numpy
9
9
10 path = os.path.split(os.getcwd())[0]
10 path = os.path.split(os.getcwd())[0]
11 sys.path.append(path)
11 sys.path.append(path)
12
12
13 from Model.Spectra import Spectra
13 from Model.Spectra import Spectra
14 from IO.SpectraIO import SpectraWriter
14 from IO.SpectraIO import SpectraWriter
15 from Graphics.SpectraPlot import Spectrum
15 from Graphics.SpectraPlot import Spectrum
16
16
17
17
18 class SpectraProcessor:
18 class SpectraProcessor:
19 '''
19 '''
20 classdocs
20 classdocs
21 '''
21 '''
22
22
23 def __init__(self, spectraInObj, spectraOutObj=None, npts = None):
23 def __init__(self, dataInObj, dataOutObj=None):
24 '''
24 '''
25 Constructor
25 Constructor
26 '''
26 '''
27 self.spectraInObj = spectraInObj
27 self.dataInObj = dataInObj
28
28
29 if spectraOutObj == None:
29 if dataOutObj == None:
30 self.spectraOutObj = Spectra()
30 self.dataOutObj = Spectra()
31 else:
31 else:
32 self.spectraOutObj = spectraOutObj
32 self.dataOutObj = dataOutObj
33
34
33
35 self.integratorIndex = None
34 self.integratorIndex = None
36 self.decoderIndex = None
35 self.decoderIndex = None
37 self.writerIndex = None
36 self.writerIndex = None
38 self.plotterIndex = None
37 self.plotterIndex = None
39
38
40 if npts != None:
41 self.spectraOutObj.nPoints = npts
42
43 self.npts = self.spectraOutObj.nPoints
44
45 self.integratorList = []
39 self.integratorList = []
46 self.decoderList = []
40 self.decoderList = []
47 self.writerList = []
41 self.writerList = []
48 self.plotterList = []
42 self.plotterList = []
49
43
50 self.buffer = None
44 self.buffer = None
51 self.ptsId = 0
45 self.ptsId = 0
52
46
53 def init(self):
47 def init(self, nFFTPoints, pairList=None):
48
54 self.integratorIndex = 0
49 self.integratorIndex = 0
55 self.decoderIndex = 0
50 self.decoderIndex = 0
56 self.writerIndex = 0
51 self.writerIndex = 0
57 self.plotterIndex = 0
52 self.plotterIndex = 0
58
53
59 if not( isinstance(self.spectraInObj, Spectra) ):
54 if nFFTPoints == None:
60 self.getFft()
55 nFFTPoints = self.dataOutObj.nPoints
61 else:
62 self.spectraOutObj.copy(self.spectraInObj)
63
56
57 self.nFFTPoints = nFFTPoints
58 self.pairList = pairList
64
59
65 def getFft(self):
60 if not( isinstance(self.dataInObj, Spectra) ):
61 self.__getFft()
62 else:
63 self.dataOutObj.copy(self.dataInObj)
64
65
66 def __getFft(self):
67 """
68 Convierte valores de Voltaje a Spectra
69
70 Affected:
71 self.dataOutObj.data_spc
72 self.dataOutObj.data_cspc
73 self.dataOutObj.data_dc
74 self.dataOutObj.heightList
75 self.dataOutObj.m_BasicHeader
76 self.dataOutObj.m_ProcessingHeader
77 self.dataOutObj.m_RadarControllerHeader
78 self.dataOutObj.m_SystemHeader
79 self.ptsId
80 self.buffer
81 self.dataOutObj.flagNoData
82 self.dataOutObj.dataType
83 self.dataOutObj.nPairs
84 self.dataOutObj.nChannels
85 self.dataOutObj.nProfiles
86 self.dataOutObj.m_SystemHeader.numChannels
87 self.dataOutObj.m_ProcessingHeader.totalSpectra
88 self.dataOutObj.m_ProcessingHeader.profilesPerBlock
89 self.dataOutObj.m_ProcessingHeader.numHeights
90 self.dataOutObj.m_ProcessingHeader.spectraComb
91 self.dataOutObj.m_ProcessingHeader.shif_fft
92 """
93 blocksize = 0
94 npoints = self.nFFTPoints
95 nchannels, nheis = self.dataInObj.data.shape
66
96
67 if self.buffer == None:
97 if self.buffer == None:
68 nheis = self.spectraInObj.data.shape[1]
98 self.buffer = numpy.zeros((nchannels, npoints, nheis), dtype='complex')
69 nchannel = self.spectraInObj.data.shape[0]
70 npoints = self.spectraOutObj.nPoints
71 self.buffer = numpy.zeros((nchannel,npoints,nheis),dtype='complex')
72
99
73 self.buffer[:,self.ptsId,:] = self.spectraInObj.data
100 self.buffer[:,self.ptsId,:] = self.dataInObj.data
74 self.ptsId += 1
101 self.ptsId += 1
75 self.spectraOutObj.flagNoData = True
102
76 if self.ptsId >= self.spectraOutObj.nPoints:
103 if self.ptsId < self.dataOutObj.nPoints:
77 data_spc = numpy.fft.fft(self.buffer,axis=1)
104 self.dataOutObj.flagNoData = True
78 data_spc = numpy.fft.fftshift(data_spc,axes=1)
105 return
79 self.ptsId = 0
106
80 self.buffer = None
107 fft_volt = numpy.fft.fft(self.buffer,axis=1)
108 dc = fft_volt[:,0,:]
81
109
82 #calculo de self-spectra
110 #calculo de self-spectra
83 self.spectraOutObj.data_spc = numpy.abs(data_spc * numpy.conjugate(data_spc))
111 fft_volt = numpy.fft.fftshift(fft_volt,axes=(1,))
112 spc = numpy.abs(fft_volt * numpy.conjugate(fft_volt))
84
113
114 blocksize += dc.size
115 blocksize += spc.size
116
117 cspc = None
118 npair = 0
119 if self.pairList != None:
85 #calculo de cross-spectra
120 #calculo de cross-spectra
86 #self.m_Spectra.data_cspc = self.__data_cspc
121 npairs = len(self.pairList)
122 cspc = numpy.zeros((npairs, npoints, nheis), dtype='complex')
123 for pair in self.pairList:
124 cspc[npair,:,:] = numpy.abs(fft_volt[pair[0],:,:] * numpy.conjugate(fft_volt[pair[1],:,:]))
125 npair += 1
126 blocksize += cspc.size
127
128 self.dataOutObj.data_spc = spc
129 self.dataOutObj.data_cspc = cspc
130 self.dataOutObj.data_dc = dc
131
132 self.ptsId = 0
133 self.buffer = None
134 self.dataOutObj.flagNoData = False
135
136 self.dataOutObj.heightList = self.dataInObj.heightList
137 self.dataOutObj.channelList = self.dataInObj.channelList
138 self.dataOutObj.m_BasicHeader = self.dataInObj.m_BasicHeader.copy()
139 self.dataOutObj.m_ProcessingHeader = self.dataInObj.m_ProcessingHeader.copy()
140 self.dataOutObj.m_RadarControllerHeader = self.dataInObj.m_RadarControllerHeader.copy()
141 self.dataOutObj.m_SystemHeader = self.dataInObj.m_SystemHeader.copy()
87
142
88 #escribiendo dc
143 self.dataOutObj.dataType = self.dataInObj.dataType
89 #self.m_Spectra.data_dc = self.__data_dc
144 self.dataOutObj.nPairs = npair
145 self.dataOutObj.nChannels = nchannels
146 self.dataOutObj.nProfiles = npoints
147 self.dataOutObj.nHeights = nheis
148 self.dataOutObj.nPoints = npoints
149 #self.dataOutObj.data = None
90
150
91 self.spectraOutObj.flagNoData = False
151 self.dataOutObj.m_SystemHeader.numChannels = nchannels
152 self.dataOutObj.m_SystemHeader.nProfiles = npoints
153
154 self.dataOutObj.m_ProcessingHeader.blockSize = blocksize
155 self.dataOutObj.m_ProcessingHeader.totalSpectra = nchannels + npair
156 self.dataOutObj.m_ProcessingHeader.profilesPerBlock = npoints
157 self.dataOutObj.m_ProcessingHeader.numHeights = nheis
158 self.dataOutObj.m_ProcessingHeader.shif_fft = True
159
160 spectraComb = numpy.zeros( (nchannels+npair)*2,numpy.dtype('u1'))
161 k = 0
162 for i in range( 0,nchannels*2,2 ):
163 spectraComb[i] = k
164 spectraComb[i+1] = k
165 k += 1
166
167 k *= 2
168
169 if self.pairList != None:
170
171 for pair in self.pairList:
172 spectraComb[k] = pair[0]
173 spectraComb[k+1] = pair[1]
174 k += 2
175
176 self.dataOutObj.m_ProcessingHeader.spectraComb = spectraComb
177
178 #self.selectHeightsByIndex( 0,10)
179 #self.selectHeightsByValue( 120,200 )
180 #self.selectChannels((2,4,5), self.pairList)
92
181
93 self.spectraOutObj.heights = self.spectraInObj.heights
94 self.spectraOutObj.m_BasicHeader = self.spectraInObj.m_BasicHeader.copy()
95 self.spectraOutObj.m_ProcessingHeader = self.spectraInObj.m_ProcessingHeader.copy()
96 self.spectraOutObj.m_RadarControllerHeader = self.spectraInObj.m_RadarControllerHeader.copy()
97 self.spectraOutObj.m_SystemHeader = self.spectraInObj.m_SystemHeader.copy()
98
182
99 def addWriter(self,wrpath):
183 def addWriter(self,wrpath):
100 objWriter = SpectraWriter(self.spectraOutObj)
184 objWriter = SpectraWriter(self.dataOutObj)
101 objWriter.setup(wrpath)
185 objWriter.setup(wrpath)
102 self.writerList.append(objWriter)
186 self.writerList.append(objWriter)
103
187
104
188
105 def addPlotter(self, index=None):
189 def addPlotter(self, index=None):
106
190
107 if index==None:
191 if index==None:
108 index = self.plotterIndex
192 index = self.plotterIndex
109
193
110 plotObj = Spectrum(self.spectraOutObj, index)
194 plotObj = Spectrum(self.dataOutObj, index)
111 self.plotterList.append(plotObj)
195 self.plotterList.append(plotObj)
112
196
113
197
114 def addIntegrator(self,N):
198 def addIntegrator(self,N):
115
199
116 objIncohInt = IncoherentIntegration(N)
200 objIncohInt = IncoherentIntegration(N)
117 self.integratorList.append(objIncohInt)
201 self.integratorList.append(objIncohInt)
118
202
119
203
120 def writeData(self):
204 def writeData(self, wrpath):
121 if self.voltageOutObj.flagNoData:
205 if self.dataOutObj.flagNoData:
122 return 0
206 return 0
123
207
124 if len(self.writerList) <= self.writerIndex:
208 if len(self.writerList) <= self.writerIndex:
125 self.addWriter(wrpath)
209 self.addWriter(wrpath)
126
210
127 self.writerList[self.writerIndex].putData()
211 self.writerList[self.writerIndex].putData()
128
212
129 self.writerIndex += 1
213 self.writerIndex += 1
130
214
131 def plotData(self,xmin=None, xmax=None, ymin=None, ymax=None, winTitle='', index=None):
215 def plotData(self,xmin=None, xmax=None, ymin=None, ymax=None, winTitle='', index=None):
132 if self.spectraOutObj.flagNoData:
216 if self.dataOutObj.flagNoData:
133 return 0
217 return 0
134
218
135 if len(self.plotterList) <= self.plotterIndex:
219 if len(self.plotterList) <= self.plotterIndex:
136 self.addPlotter(index)
220 self.addPlotter(index)
137
221
138 self.plotterList[self.plotterIndex].plotData(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,winTitle=winTitle)
222 self.plotterList[self.plotterIndex].plotData(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,winTitle=winTitle)
139
223
140 self.plotterIndex += 1
224 self.plotterIndex += 1
141
225
142 def integrator(self, N):
226 def integrator(self, N):
143 if self.spectraOutObj.flagNoData:
227 if self.dataOutObj.flagNoData:
144 return 0
228 return 0
145
229
146 if len(self.integratorList) <= self.integratorIndex:
230 if len(self.integratorList) <= self.integratorIndex:
147 self.addIntegrator(N)
231 self.addIntegrator(N)
148
232
149 myCohIntObj = self.integratorList[self.integratorIndex]
233 myCohIntObj = self.integratorList[self.integratorIndex]
150 myCohIntObj.exe(self.spectraOutObj.data_spc)
234 myCohIntObj.exe(self.dataOutObj.data_spc)
151
235
152 if myCohIntObj.flag:
236 if myCohIntObj.flag:
153 self.spectraOutObj.data_spc = myCohIntObj.data
237 self.dataOutObj.data_spc = myCohIntObj.data
154 self.spectraOutObj.m_ProcessingHeader.incoherentInt *= N
238 self.dataOutObj.m_ProcessingHeader.incoherentInt *= N
155 self.spectraOutObj.flagNoData = False
239 self.dataOutObj.flagNoData = False
156
240
157 else:
241 else:
158 self.spectraOutObj.flagNoData = True
242 self.dataOutObj.flagNoData = True
159
243
160 self.integratorIndex += 1
244 self.integratorIndex += 1
161
245
246 def removeDC(self, type):
247
248 if self.dataOutObj.flagNoData:
249 return 0
250 pass
251
252 def removeInterference(self):
253
254 if self.dataOutObj.flagNoData:
255 return 0
256 pass
257
258 def removeSatellites(self):
259
260 if self.dataOutObj.flagNoData:
261 return 0
262 pass
263
264 def selectChannels(self, channelList, pairList=None):
265 """
266 Selecciona un bloque de datos en base a canales y pares segun el channelList y el pairList
267
268 Input:
269 channelList : lista sencilla de canales a seleccionar por ej. (2,3,7)
270 pairList : tupla de pares que se desea selecionar por ej. ( (0,1), (0,2) )
271
272 Affected:
273 self.dataOutObj.data_spc
274 self.dataOutObj.data_cspc
275 self.dataOutObj.data_dc
276 self.dataOutObj.nChannels
277 self.dataOutObj.nPairs
278 self.dataOutObj.m_ProcessingHeader.spectraComb
279 self.dataOutObj.m_SystemHeader.numChannels
280
281 Return:
282 None
283 """
284
285 if self.dataOutObj.flagNoData:
286 return 0
287
288 nchannels = 0
289 npairs = 0
290 profiles = self.dataOutObj.nProfiles
291 dataType = self.dataOutObj.dataType
292 heights = self.dataOutObj.m_ProcessingHeader.numHeights
293 blocksize = 0
294
295 #self spectra
296 nchannels = len(channelList)
297 spc = numpy.zeros( (nchannels,profiles,heights), dataType[0] )
298
299 for index, channel in enumerate(channelList):
300 spc[index,:,:] = self.dataOutObj.data_spc[channel,:,:]
301
302 #DC channel
303 dc = numpy.zeros( (nchannels,heights), dtype='complex' )
304 for index, channel in enumerate(channelList):
305 dc[index,:] = self.dataOutObj.data_dc[channel,:]
306
307 blocksize += dc.size
308 blocksize += spc.size
309
310 npairs = 0
311 cspc = None
312
313 if pairList == None:
314 pairList = self.pairList
315
316 if pairList != None:
317 #cross spectra
318 npairs = len(pairList)
319 cspc = numpy.zeros( (npairs,profiles,heights), dtype='complex' )
320
321 spectraComb = self.dataOutObj.m_ProcessingHeader.spectraComb
322 totalSpectra = len(spectraComb)
323 nchan = self.dataOutObj.nChannels
324 indexList = []
325
326 for pair in pairList: #busco el par en la lista de pares del Spectra Combinations
327 for index in range(0,totalSpectra,2):
328 if pair[0] == spectraComb[index] and pair[1] == spectraComb[index+1]:
329 indexList.append( index/2 - nchan )
330
331 for index, pair in enumerate(indexList):
332 cspc[index,:,:] = self.dataOutObj.data_cspc[pair,:,:]
333 blocksize += cspc.size
334
335 else:
336 pairList = self.pairList
337 cspc = self.dataOutObj.data_cspc
338 if cspc != None:
339 blocksize += cspc.size
340
341 spectraComb = numpy.zeros( (nchannels+npairs)*2,numpy.dtype('u1'))
342 i = 0
343 for val in channelList:
344 spectraComb[i] = val
345 spectraComb[i+1] = val
346 i += 2
347
348 if pairList != None:
349 for pair in pairList:
350 spectraComb[i] = pair[0]
351 spectraComb[i+1] = pair[1]
352 i += 2
353
354 self.dataOutObj.data_spc = spc
355 self.dataOutObj.data_cspc = cspc
356 self.dataOutObj.data_dc = dc
357 self.dataOutObj.nChannels = nchannels
358 self.dataOutObj.nPairs = npairs
359
360 self.dataOutObj.channelList = channelList
361
362 self.dataOutObj.m_ProcessingHeader.spectraComb = spectraComb
363 self.dataOutObj.m_ProcessingHeader.totalSpectra = nchannels + npairs
364 self.dataOutObj.m_SystemHeader.numChannels = nchannels
365 self.dataOutObj.nChannels = nchannels
366 self.dataOutObj.m_ProcessingHeader.blockSize = blocksize
367
368
369 def selectHeightsByValue(self, minHei, maxHei):
370 """
371 Selecciona un bloque de datos en base a un grupo de valores de alturas segun el rango
372 minHei <= height <= maxHei
373
374 Input:
375 minHei : valor minimo de altura a considerar
376 maxHei : valor maximo de altura a considerar
377
378 Affected:
379 Indirectamente son cambiados varios valores a travez del metodo selectHeightsByIndex
380
381 Return:
382 None
383 """
384
385 if self.dataOutObj.flagNoData:
386 return 0
387
388 minIndex = 0
389 maxIndex = 0
390 data = self.dataOutObj.heightList
391
392 for i,val in enumerate(data):
393 if val < minHei:
394 continue
395 else:
396 minIndex = i;
397 break
398
399 for i,val in enumerate(data):
400 if val <= maxHei:
401 maxIndex = i;
402 else:
403 break
404
405 self.selectHeightsByIndex(minIndex, maxIndex)
406
407
408 def selectHeightsByIndex(self, minIndex, maxIndex):
409 """
410 Selecciona un bloque de datos en base a un grupo indices de alturas segun el rango
411 minIndex <= index <= maxIndex
412
413 Input:
414 minIndex : valor minimo de altura a considerar
415 maxIndex : valor maximo de altura a considerar
416
417 Affected:
418 self.dataOutObj.data_spc
419 self.dataOutObj.data_cspc
420 self.dataOutObj.data_dc
421 self.dataOutObj.heightList
422 self.dataOutObj.nHeights
423 self.dataOutObj.m_ProcessingHeader.numHeights
424 self.dataOutObj.m_ProcessingHeader.blockSize
425 self.dataOutObj.m_ProcessingHeader.firstHeight
426 self.dataOutObj.m_RadarControllerHeader.numHeights
427
428 Return:
429 None
430 """
431
432 if self.dataOutObj.flagNoData:
433 return 0
434
435 nchannels = self.dataOutObj.nChannels
436 npairs = self.dataOutObj.nPairs
437 profiles = self.dataOutObj.nProfiles
438 dataType = self.dataOutObj.dataType
439 newheis = maxIndex - minIndex + 1
440 blockSize = 0
441
442 #self spectra
443 spc = numpy.zeros( (nchannels,profiles,newheis), dataType[0] )
444 for i in range(nchannels):
445 spc[i,:,:] = self.dataOutObj.data_spc[i,:,minIndex:maxIndex+1]
446
447 #cross spectra
448 cspc = numpy.zeros( (npairs,profiles,newheis), dtype='complex')
449 for i in range(npairs):
450 cspc[i,:,:] = self.dataOutObj.data_cspc[i,:,minIndex:maxIndex+1]
451
452 #DC channel
453 dc = numpy.zeros( (nchannels,newheis), dtype='complex')
454 for i in range(nchannels):
455 dc[i] = self.dataOutObj.data_dc[i,minIndex:maxIndex+1]
456
457 self.dataOutObj.data_spc = spc
458 self.dataOutObj.data_cspc = cspc
459 self.dataOutObj.data_dc = dc
460
461 firstHeight = self.dataOutObj.heightList[minIndex]
462
463 self.dataOutObj.nHeights = newheis
464 self.dataOutObj.m_ProcessingHeader.blockSize = spc.size + cspc.size + dc.size
465 self.dataOutObj.m_ProcessingHeader.numHeights = newheis
466 self.dataOutObj.m_ProcessingHeader.firstHeight = firstHeight
467 self.dataOutObj.m_RadarControllerHeader.numHeights = newheis
468
469 xi = firstHeight
470 step = self.dataOutObj.m_ProcessingHeader.deltaHeight
471 xf = xi + newheis * step
472 self.dataOutObj.heightList = numpy.arange(xi, xf, step)
473
474
162 class IncoherentIntegration:
475 class IncoherentIntegration:
163 def __init__(self, N):
476 def __init__(self, N):
164 self.profCounter = 1
477 self.profCounter = 1
165 self.data = None
478 self.data = None
166 self.buffer = None
479 self.buffer = None
167 self.flag = False
480 self.flag = False
168 self.nIncohInt = N
481 self.nIncohInt = N
169
482
170 def exe(self,data):
483 def exe(self,data):
171
484
172 if self.buffer == None:
485 if self.buffer == None:
173 self.buffer = data
486 self.buffer = data
174 else:
487 else:
175 self.buffer = self.buffer + data
488 self.buffer = self.buffer + data
176
489
177 if self.profCounter == self.nIncohInt:
490 if self.profCounter == self.nIncohInt:
178 self.data = self.buffer
491 self.data = self.buffer
179 self.buffer = None
492 self.buffer = None
180 self.profCounter = 0
493 self.profCounter = 0
181 self.flag = True
494 self.flag = True
182 else:
495 else:
183 self.flag = False
496 self.flag = False
184
497
185 self.profCounter += 1
498 self.profCounter += 1
186
499
General Comments 0
You need to be logged in to leave comments. Login now