##// END OF EJS Templates
VoltageProcessor.py:...
Victor Sarmiento -
r97:4bafecca4fcc
parent child
Show More
@@ -1,452 +1,464
1 1 '''
2 2 Created on Feb 7, 2012
3 3
4 4 @author $Author$
5 5 @version $Id$
6 6 '''
7 7
8 8 import os, sys
9 9 import numpy
10 10
11 11 path = os.path.split(os.getcwd())[0]
12 12 sys.path.append(path)
13 13
14 14 from Model.Voltage import Voltage
15 15 from IO.VoltageIO import VoltageWriter
16 16 from Graphics.VoltagePlot import Osciloscope
17 17
18 18 class VoltageProcessor:
19 19 '''
20 20 classdocs
21 21 '''
22 22
23 23 def __init__(self, voltageInObj, voltageOutObj=None):
24 24 '''
25 25 Constructor
26 26 '''
27 27
28 28 self.voltageInObj = voltageInObj
29 29
30 30 if voltageOutObj == None:
31 31 self.voltageOutObj = Voltage()
32 32 else:
33 33 self.voltageOutObj = voltageOutObj
34 34
35 35 self.integratorIndex = None
36 36 self.decoderIndex = None
37 37 self.profSelectorIndex = None
38 38 self.writerIndex = None
39 39 self.plotterIndex = None
40 40
41 41 self.integratorList = []
42 42 self.decoderList = []
43 43 self.profileSelectorList = []
44 44 self.writerList = []
45 45 self.plotterList = []
46 46
47 47 def init(self):
48 48
49 49 self.integratorIndex = 0
50 50 self.decoderIndex = 0
51 51 self.profSelectorIndex = 0
52 52 self.writerIndex = 0
53 53 self.plotterIndex = 0
54 54 self.voltageOutObj.copy(self.voltageInObj)
55 55
56 56 def addWriter(self, wrpath):
57 57 objWriter = VoltageWriter(self.voltageOutObj)
58 58 objWriter.setup(wrpath)
59 59 self.writerList.append(objWriter)
60 60
61 61 def addPlotter(self):
62 62
63 63 plotObj = Osciloscope(self.voltageOutObj,self.plotterIndex)
64 64 self.plotterList.append(plotObj)
65 65
66 66 def addIntegrator(self, nCohInt):
67 67
68 68 objCohInt = CoherentIntegrator(nCohInt)
69 69 self.integratorList.append(objCohInt)
70 70
71 71 def addDecoder(self, code, ncode, nbaud):
72 72
73 73 objDecoder = Decoder(code,ncode,nbaud)
74 74 self.decoderList.append(objDecoder)
75 75
76 76 def addProfileSelector(self, nProfiles):
77 77
78 78 objProfSelector = ProfileSelector(nProfiles)
79 79 self.profileSelectorList.append(objProfSelector)
80 80
81 81 def writeData(self,wrpath):
82 82
83 83 if self.voltageOutObj.flagNoData:
84 84 return 0
85 85
86 86 if len(self.writerList) <= self.writerIndex:
87 87 self.addWriter(wrpath)
88 88
89 89 self.writerList[self.writerIndex].putData()
90 90
91 91 # myWrObj = self.writerList[self.writerIndex]
92 92 # myWrObj.putData()
93 93
94 94 self.writerIndex += 1
95 95
96 96 def plotData(self,idProfile, type, xmin=None, xmax=None, ymin=None, ymax=None, winTitle=''):
97 97 if self.voltageOutObj.flagNoData:
98 98 return 0
99 99
100 100 if len(self.plotterList) <= self.plotterIndex:
101 101 self.addPlotter()
102 102
103 103 self.plotterList[self.plotterIndex].plotData(type=type, xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,winTitle=winTitle)
104 104
105 105 self.plotterIndex += 1
106 106
107 107 def integrator(self, N):
108 108
109 109 if self.voltageOutObj.flagNoData:
110 110 return 0
111 111
112 112 if len(self.integratorList) <= self.integratorIndex:
113 113 self.addIntegrator(N)
114 114
115 115 myCohIntObj = self.integratorList[self.integratorIndex]
116 116 myCohIntObj.exe(self.voltageOutObj.data)
117 117
118 118 if myCohIntObj.flag:
119 119 self.voltageOutObj.data = myCohIntObj.data
120 120 self.voltageOutObj.m_ProcessingHeader.coherentInt *= N
121 121 self.voltageOutObj.flagNoData = False
122 122
123 123 else:
124 124 self.voltageOutObj.flagNoData = True
125 125
126 126 self.integratorIndex += 1
127 127
128 128 def decoder(self,code=None,type = 0):
129 129
130 130 if self.voltageOutObj.flagNoData:
131 131 return 0
132 132
133 133 if code == None:
134 134 code = self.voltageOutObj.m_RadarControllerHeader.code
135 135 ncode, nbaud = code.shape
136 136
137 137 if len(self.decoderList) <= self.decoderIndex:
138 138 self.addDecoder(code,ncode,nbaud)
139 139
140 140 myDecodObj = self.decoderList[self.decoderIndex]
141 141 myDecodObj.exe(data=self.voltageOutObj.data,type=type)
142 142
143 143 if myDecodObj.flag:
144 144 self.voltageOutObj.data = myDecodObj.data
145 145 self.voltageOutObj.flagNoData = False
146 146 else:
147 147 self.voltageOutObj.flagNoData = True
148 148
149 149 self.decoderIndex += 1
150 150
151 151
152 152 def filterByHei(self, window):
153 153 pass
154 154
155 155
156 156 def selectChannels(self, channelList):
157 157 """
158 158 Selecciona un bloque de datos en base a canales y pares segun el channelList y el pairList
159 159
160 160 Input:
161 channelList : lista sencilla de canales a seleccionar por ej. (2,3,7)
162 pairList : tupla de pares que se desea selecionar por ej. ( (0,1), (0,2) )
161 channelList : lista sencilla de canales a seleccionar por ej. [2,3,7]
163 162
164 163 Affected:
165 self.dataOutObj.datablock
164 self.dataOutObj.data
165 self.voltageOutObj.channelList
166 166 self.dataOutObj.nChannels
167 self.voltageOutObj.m_ProcessingHeader.totalSpectra
167 168 self.dataOutObj.m_SystemHeader.numChannels
168 169 self.voltageOutObj.m_ProcessingHeader.blockSize
169 170
170 171 Return:
171 172 None
172 173 """
173 if not(channelList):
174 return
174 if self.voltageOutObj.flagNoData:
175 return 0
175 176
176 channels = 0
177 for channel in channelList:
178 if channel not in self.voltageOutObj.channelList:
179 raise ValueError, "The value %d in channelList is not valid" %channel
180
181 nchannels = len(channelList)
177 182 profiles = self.voltageOutObj.nProfiles
178 heights = self.voltageOutObj.m_ProcessingHeader.numHeights
183 heights = self.voltageOutObj.nHeights #m_ProcessingHeader.numHeights
179 184
180 #self spectra
181 channels = len(channelList)
182 data = numpy.zeros( (channels,profiles,heights), dtype='complex' )
185 data = numpy.zeros( (nchannels,heights), dtype='complex' )
183 186 for index,channel in enumerate(channelList):
184 data[index,:,:] = self.voltageOutObj.data_spc[channel,:,:]
185
186 self.voltageOutObj.datablock = data
187
188 #fill the m_ProcessingHeader.spectraComb up
189 channels = len(channelList)
187 data[index,:] = self.voltageOutObj.data[channel,:]
190 188
189 self.voltageOutObj.data = data
191 190 self.voltageOutObj.channelList = channelList
192 191 self.voltageOutObj.nChannels = nchannels
193 192 self.voltageOutObj.m_ProcessingHeader.totalSpectra = nchannels
194 193 self.voltageOutObj.m_SystemHeader.numChannels = nchannels
195 194 self.voltageOutObj.m_ProcessingHeader.blockSize = data.size
195 return 1
196 196
197 197
198 198 def selectHeightsByValue(self, minHei, maxHei):
199 199 """
200 200 Selecciona un bloque de datos en base a un grupo de valores de alturas segun el rango
201 201 minHei <= height <= maxHei
202 202
203 203 Input:
204 204 minHei : valor minimo de altura a considerar
205 205 maxHei : valor maximo de altura a considerar
206 206
207 207 Affected:
208 208 Indirectamente son cambiados varios valores a travez del metodo selectHeightsByIndex
209 209
210 210 Return:
211 None
211 1 si el metodo se ejecuto con exito caso contrario devuelve 0
212 212 """
213 if self.voltageOutObj.flagNoData:
214 return 0
215
216 if (minHei < self.voltageOutObj.heightList[0]) or (minHei > maxHei):
217 raise ValueError, "some value in (%d,%d) is not valid" % (minHei, maxHei)
218
219 if (maxHei > self.voltageOutObj.heightList[-1]):
220 raise ValueError, "some value in (%d,%d) is not valid" % (minHei, maxHei)
221
213 222 minIndex = 0
214 223 maxIndex = 0
215 data = self.dataOutObj.heightList
224 data = self.voltageOutObj.heightList
216 225
217 226 for i,val in enumerate(data):
218 227 if val < minHei:
219 228 continue
220 229 else:
221 230 minIndex = i;
222 231 break
223 232
224 233 for i,val in enumerate(data):
225 234 if val <= maxHei:
226 235 maxIndex = i;
227 236 else:
228 237 break
229 238
230 239 self.selectHeightsByIndex(minIndex, maxIndex)
240 return 1
231 241
232 242
233 243 def selectHeightsByIndex(self, minIndex, maxIndex):
234 244 """
235 245 Selecciona un bloque de datos en base a un grupo indices de alturas segun el rango
236 246 minIndex <= index <= maxIndex
237 247
238 248 Input:
239 minIndex : valor minimo de altura a considerar
240 maxIndex : valor maximo de altura a considerar
249 minIndex : valor de indice minimo de altura a considerar
250 maxIndex : valor de indice maximo de altura a considerar
241 251
242 252 Affected:
243 self.voltageOutObj.datablock
244 self.voltageOutObj.m_ProcessingHeader.numHeights
245 self.voltageOutObj.m_ProcessingHeader.blockSize
253 self.voltageOutObj.data
246 254 self.voltageOutObj.heightList
247 255 self.voltageOutObj.nHeights
248 self.voltageOutObj.m_RadarControllerHeader.numHeights
256 self.voltageOutObj.m_ProcessingHeader.blockSize
257 self.voltageOutObj.m_ProcessingHeader.numHeights
258 self.voltageOutObj.m_ProcessingHeader.firstHeight
259 self.voltageOutObj.m_RadarControllerHeader
249 260
250 261 Return:
251 None
262 1 si el metodo se ejecuto con exito caso contrario devuelve 0
252 263 """
253 channels = self.voltageOutObj.nChannels
254 profiles = self.voltageOutObj.nProfiles
255 newheis = maxIndex - minIndex + 1
264 if self.voltageOutObj.flagNoData:
265 return 0
266
267 if (minIndex < 0) or (minIndex > maxIndex):
268 raise ValueError, "some value in (%d,%d) is not valid" % (minIndex, maxIndex)
269
270 if (maxIndex >= self.voltageOutObj.nHeights):
271 raise ValueError, "some value in (%d,%d) is not valid" % (minIndex, maxIndex)
272
273 nHeights = maxIndex - minIndex + 1
256 274 firstHeight = 0
257 275
258 276 #voltage
259 data = numpy.zeros( (channels,profiles,newheis), dtype='complex' )
260 for i in range(channels):
261 data[i] = self.voltageOutObj.data_spc[i,:,minIndex:maxIndex+1]
277 data = self.voltageOutObj.data[:,minIndex:maxIndex+1]
262 278
263 self.voltageOutObj.datablock = data
279 firstHeight = self.voltageOutObj.heightList[minIndex]
264 280
265 firstHeight = self.dataOutObj.heightList[minIndex]
266
267 self.voltageOutObj.nHeights = newheis
281 self.voltageOutObj.data = data
282 self.voltageOutObj.heightList = self.voltageOutObj.heightList[minIndex:maxIndex+1]
283 self.voltageOutObj.nHeights = nHeights
268 284 self.voltageOutObj.m_ProcessingHeader.blockSize = data.size
269 self.voltageOutObj.m_ProcessingHeader.numHeights = newheis
285 self.voltageOutObj.m_ProcessingHeader.numHeights = nHeights
270 286 self.voltageOutObj.m_ProcessingHeader.firstHeight = firstHeight
271 self.voltageOutObj.m_RadarControllerHeader = newheis
272
273 xi = firstHeight
274 step = self.voltageOutObj.m_ProcessingHeader.deltaHeight
275 xf = xi + newheis * step
276 self.voltageOutObj.heightList = numpy.arange(xi, xf, step)
287 self.voltageOutObj.m_RadarControllerHeader.numHeights = nHeights
288 return 1
277 289
278 290
279 291 def selectProfiles(self, minIndex, maxIndex, nProfiles):
280 292 """
281 Selecciona un bloque de datos en base a un grupo indices de alturas segun el rango
293 Selecciona un bloque de datos en base a un grupo indices de perfiles segun el rango
282 294 minIndex <= index <= maxIndex
283 295
284 296 Input:
285 minIndex : valor minimo de altura a considerar
286 maxIndex : valor maximo de altura a considerar
297 minIndex : valor de indice minimo de perfil a considerar
298 maxIndex : valor de indice maximo de perfil a considerar
299 nProfiles : numero de profiles
287 300
288 301 Affected:
289 self.voltageOutObj.datablock
290 self.voltageOutObj.m_ProcessingHeader.numHeights
291 self.voltageOutObj.heightList
302 self.voltageOutObj.flagNoData
303 self.profSelectorIndex
292 304
293 305 Return:
294 None
306 1 si el metodo se ejecuto con exito caso contrario devuelve 0
295 307 """
296 308
297 309 if self.voltageOutObj.flagNoData:
298 310 return 0
299 311
300 312 if self.profSelectorIndex >= len(self.profileSelectorList):
301 313 self.addProfileSelector(nProfiles)
302 314
303 315 profileSelectorObj = self.profileSelectorList[self.profSelectorIndex]
304 316
305 317 if profileSelectorObj.isProfileInRange(minIndex, maxIndex):
306 318 self.voltageOutObj.flagNoData = False
307 319 self.profSelectorIndex += 1
308 320 return 1
309 321
310 322 self.voltageOutObj.flagNoData = True
311 323 self.profSelectorIndex += 1
312 324
313 325 return 0
314 326
315 327 def selectNtxs(self, ntx):
316 328 pass
317 329
318 330
319 331 class Decoder:
320 332
321 333 def __init__(self,code, ncode, nbaud):
322 334
323 335 self.buffer = None
324 336 self.profCounter = 1
325 337 self.nCode = ncode
326 338 self.nBaud = nbaud
327 339 self.codeIndex = 0
328 340 self.code = code #this is a List
329 341 self.fft_code = None
330 342 self.flag = False
331 343 self.setCodeFft = False
332 344
333 345 def exe(self, data, ndata=None, type = 0):
334 346
335 347 if ndata == None: ndata = data.shape[1]
336 348
337 349 if type == 0:
338 350 self.convolutionInFreq(data,ndata)
339 351
340 352 if type == 1:
341 353 self.convolutionInTime(data, ndata)
342 354
343 355 def convolutionInFreq(self,data, ndata):
344 356
345 357 newcode = numpy.zeros(ndata)
346 358 newcode[0:self.nBaud] = self.code[self.codeIndex]
347 359
348 360 self.codeIndex += 1
349 361
350 362 fft_data = numpy.fft.fft(data, axis=1)
351 363 fft_code = numpy.conj(numpy.fft.fft(newcode))
352 364 fft_code = fft_code.reshape(1,len(fft_code))
353 365
354 366 conv = fft_data.copy()
355 367 conv.fill(0)
356 368
357 369 conv = fft_data*fft_code # This other way to calculate multiplication between bidimensional arrays
358 370 # for i in range(ndata):
359 371 # conv[i,:] = fft_data[i,:]*fft_code[i]
360 372
361 373 self.data = numpy.fft.ifft(conv,axis=1)
362 374 self.flag = True
363 375
364 376 if self.profCounter == self.nCode:
365 377 self.profCounter = 0
366 378 self.codeIndex = 0
367 379
368 380 self.profCounter += 1
369 381
370 382 def convolutionInTime(self, data, ndata):
371 383
372 384 nchannel = data.shape[1]
373 385 newcode = self.code[self.codeIndex]
374 386 self.codeIndex += 1
375 387 conv = data.copy()
376 388 for i in range(nchannel):
377 389 conv[i,:] = numpy.correlate(data[i,:], newcode, 'same')
378 390
379 391 self.data = conv
380 392 self.flag = True
381 393
382 394 if self.profCounter == self.nCode:
383 395 self.profCounter = 0
384 396 self.codeIndex = 0
385 397
386 398 self.profCounter += 1
387 399
388 400
389 401 class CoherentIntegrator:
390 402
391 403 def __init__(self, N):
392 404
393 405 self.profCounter = 1
394 406 self.data = None
395 407 self.buffer = None
396 408 self.flag = False
397 409 self.nCohInt = N
398 410
399 411 def exe(self, data):
400 412
401 413 if self.buffer == None:
402 414 self.buffer = data
403 415 else:
404 416 self.buffer = self.buffer + data
405 417
406 418 if self.profCounter == self.nCohInt:
407 419 self.data = self.buffer
408 420 self.buffer = None
409 421 self.profCounter = 0
410 422 self.flag = True
411 423 else:
412 424 self.flag = False
413 425
414 426 self.profCounter += 1
415 427
416 428 class ProfileSelector():
417 429
418 430 indexProfile = None
419 # Tamaño total de los perfiles
431 # Tamanho total de los perfiles
420 432 nProfiles = None
421 433
422 434 def __init__(self, nProfiles):
423 435
424 436 self.indexProfile = 0
425 437 self.nProfiles = nProfiles
426 438
427 439 def isProfileInRange(self, minIndex, maxIndex):
428 440
429 if minIndex < self.indexProfile:
441 if self.indexProfile < minIndex:
430 442 self.indexProfile += 1
431 443 return False
432 444
433 if maxIndex > self.indexProfile:
445 if self.indexProfile > maxIndex:
434 446 self.indexProfile += 1
435 447 return False
436 448
437 449 self.indexProfile += 1
438 450
439 451 return True
440 452
441 453 def isProfileInList(self, profileList):
442 454
443 455 if self.indexProfile not in profileList:
444 456 self.indexProfile += 1
445 457 return False
446 458
447 459 self.indexProfile += 1
448 460
449 461 return True
450 462
451 463
452 464 No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now