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