VoltageProcessor.py
590 lines
| 18.5 KiB
| text/x-python
|
PythonLexer
|
r10 | ''' | |
Created on Feb 7, 2012 | |||
|
r16 | @author $Author$ | |
@version $Id$ | |||
|
r10 | ''' | |
|
r72 | import os, sys | |
import numpy | |||
path = os.path.split(os.getcwd())[0] | |||
sys.path.append(path) | |||
from Model.Voltage import Voltage | |||
from IO.VoltageIO import VoltageWriter | |||
from Graphics.VoltagePlot import Osciloscope | |||
|
r10 | class VoltageProcessor: | |
''' | |||
classdocs | |||
''' | |||
|
r99 | ||
dataInObj = None | |||
dataOutObj = None | |||
integratorObjIndex = None | |||
decoderObjIndex = None | |||
profSelectorObjIndex = None | |||
writerObjIndex = None | |||
plotterObjIndex = None | |||
|
r100 | flipIndex = None | |
|
r99 | ||
integratorObjList = [] | |||
decoderObjList = [] | |||
profileSelectorObjList = [] | |||
writerObjList = [] | |||
plotterObjList = [] | |||
m_Voltage= Voltage() | |||
|
r101 | ||
|
r103 | def __init__(self, dataInObj=None, dataOutObj=None): | |
|
r10 | ''' | |
Constructor | |||
''' | |||
|
r72 | ||
|
r99 | self.dataInObj = dataInObj | |
|
r72 | ||
|
r99 | if dataOutObj == None: | |
self.dataOutObj = Voltage() | |||
|
r72 | else: | |
|
r99 | self.dataOutObj = dataOutObj | |
|
r72 | ||
|
r99 | self.integratorObjIndex = None | |
self.decoderObjIndex = None | |||
self.profSelectorObjIndex = None | |||
self.writerObjIndex = None | |||
self.plotterObjIndex = None | |||
|
r100 | self.flipIndex = 1 | |
|
r99 | self.integratorObjList = [] | |
self.decoderObjList = [] | |||
self.profileSelectorObjList = [] | |||
self.writerObjList = [] | |||
self.plotterObjList = [] | |||
|
r72 | ||
|
r103 | def setIO(self,inputObject, outputObject): | |
if not( isinstance(inputObject, Voltage) ): | |||
print 'InputObject must be an instance from Voltage()' | |||
sys.exit(0) | |||
if not( isinstance(outputObject, Voltage) ): | |||
print 'OutputObject must be an instance from Voltage()' | |||
sys.exit(0) | |||
self.dataInObj = inputObject | |||
self.dataOutObj = outputObject | |||
def setup(self): | |||
pass | |||
|
r72 | def init(self): | |
|
r92 | ||
|
r99 | self.integratorObjIndex = 0 | |
self.decoderObjIndex = 0 | |||
self.profSelectorObjIndex = 0 | |||
self.writerObjIndex = 0 | |||
self.plotterObjIndex = 0 | |||
self.dataOutObj.copy(self.dataInObj) | |||
|
r100 | ||
if self.profSelectorObjIndex != None: | |||
for profSelObj in self.profileSelectorObjList: | |||
profSelObj.incIndex() | |||
|
r72 | ||
|
r92 | def addWriter(self, wrpath): | |
|
r99 | objWriter = VoltageWriter(self.dataOutObj) | |
|
r72 | objWriter.setup(wrpath) | |
|
r99 | self.writerObjList.append(objWriter) | |
|
r72 | ||
|
r103 | def addPlotter(self, index=None): | |
if index==None: | |||
index = self.plotterObjIndex | |||
|
r72 | ||
|
r103 | plotObj = Osciloscope(self.dataOutObj, index) | |
|
r99 | self.plotterObjList.append(plotObj) | |
|
r103 | ||
|
r104 | def addIntegrator(self, N,timeInterval): | |
|
r72 | ||
|
r104 | objCohInt = CoherentIntegrator(N,timeInterval) | |
|
r99 | self.integratorObjList.append(objCohInt) | |
|
r72 | ||
|
r92 | def addDecoder(self, code, ncode, nbaud): | |
|
r72 | ||
objDecoder = Decoder(code,ncode,nbaud) | |||
|
r99 | self.decoderObjList.append(objDecoder) | |
|
r72 | ||
|
r92 | def addProfileSelector(self, nProfiles): | |
objProfSelector = ProfileSelector(nProfiles) | |||
|
r99 | self.profileSelectorObjList.append(objProfSelector) | |
|
r92 | ||
|
r72 | def writeData(self,wrpath): | |
|
r92 | ||
|
r99 | if self.dataOutObj.flagNoData: | |
|
r92 | return 0 | |
|
r72 | ||
|
r99 | if len(self.writerObjList) <= self.writerObjIndex: | |
|
r72 | self.addWriter(wrpath) | |
|
r99 | self.writerObjList[self.writerObjIndex].putData() | |
|
r72 | ||
|
r99 | # myWrObj = self.writerObjList[self.writerObjIndex] | |
|
r72 | # myWrObj.putData() | |
|
r99 | self.writerObjIndex += 1 | |
|
r72 | ||
|
r103 | def plotData(self,xmin=None, xmax=None, ymin=None, ymax=None, type='iq', winTitle='', index=None): | |
|
r99 | if self.dataOutObj.flagNoData: | |
|
r92 | return 0 | |
|
r103 | ||
|
r99 | if len(self.plotterObjList) <= self.plotterObjIndex: | |
|
r103 | self.addPlotter(index) | |
|
r72 | ||
|
r103 | self.plotterObjList[self.plotterObjIndex].plotData(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,type=type, winTitle=winTitle) | |
|
r72 | ||
|
r99 | self.plotterObjIndex += 1 | |
|
r72 | ||
|
r104 | def integrator(self, N=None, timeInterval=None): | |
|
r92 | ||
|
r99 | if self.dataOutObj.flagNoData: | |
|
r92 | return 0 | |
|
r72 | ||
|
r99 | if len(self.integratorObjList) <= self.integratorObjIndex: | |
|
r104 | self.addIntegrator(N,timeInterval) | |
|
r72 | ||
|
r99 | myCohIntObj = self.integratorObjList[self.integratorObjIndex] | |
|
r104 | myCohIntObj.exe(data=self.dataOutObj.data,timeOfData=self.dataOutObj.m_BasicHeader.utc) | |
|
r72 | ||
|
r104 | if myCohIntObj.isReady: | |
|
r99 | self.dataOutObj.data = myCohIntObj.data | |
|
r104 | self.dataOutObj.nAvg = myCohIntObj.navg | |
self.dataOutObj.m_ProcessingHeader.coherentInt *= myCohIntObj.navg | |||
#print "myCohIntObj.navg: ",myCohIntObj.navg | |||
|
r99 | self.dataOutObj.flagNoData = False | |
|
r72 | ||
else: | |||
|
r99 | self.dataOutObj.flagNoData = True | |
|
r72 | ||
|
r99 | self.integratorObjIndex += 1 | |
|
r72 | ||
def decoder(self,code=None,type = 0): | |||
|
r92 | ||
|
r99 | if self.dataOutObj.flagNoData: | |
|
r92 | return 0 | |
|
r72 | if code == None: | |
|
r99 | code = self.dataOutObj.m_RadarControllerHeader.code | |
|
r72 | ncode, nbaud = code.shape | |
|
r99 | if len(self.decoderObjList) <= self.decoderObjIndex: | |
|
r72 | self.addDecoder(code,ncode,nbaud) | |
|
r99 | myDecodObj = self.decoderObjList[self.decoderObjIndex] | |
myDecodObj.exe(data=self.dataOutObj.data,type=type) | |||
|
r72 | ||
if myDecodObj.flag: | |||
|
r99 | self.dataOutObj.data = myDecodObj.data | |
self.dataOutObj.flagNoData = False | |||
|
r72 | else: | |
|
r99 | self.dataOutObj.flagNoData = True | |
|
r72 | ||
|
r99 | self.decoderObjIndex += 1 | |
|
r84 | ||
|
r72 | ||
|
r84 | def filterByHei(self, window): | |
|
r100 | if window == None: | |
window = self.dataOutObj.m_RadarControllerHeader.txA / self.dataOutObj.m_ProcessingHeader.deltaHeight[0] | |||
newdelta = self.dataOutObj.m_ProcessingHeader.deltaHeight[0] * window | |||
dim1 = self.dataOutObj.data.shape[0] | |||
dim2 = self.dataOutObj.data.shape[1] | |||
r = dim2 % window | |||
buffer = self.dataOutObj.data[:,0:dim2-r] | |||
buffer = buffer.reshape(dim1,dim2/window,window) | |||
buffer = numpy.sum(buffer,2) | |||
self.dataOutObj.data = buffer | |||
self.dataOutObj.m_ProcessingHeader.deltaHeight = newdelta | |||
self.dataOutObj.m_ProcessingHeader.numHeights = buffer.shape[1] | |||
self.dataOutObj.nHeights = self.dataOutObj.m_ProcessingHeader.numHeights | |||
#self.dataOutObj.heightList es un numpy.array | |||
self.dataOutObj.heightList = numpy.arange(self.dataOutObj.m_ProcessingHeader.firstHeight[0],newdelta*self.dataOutObj.nHeights,newdelta) | |||
def deFlip(self): | |||
self.dataOutObj.data *= self.flipIndex | |||
self.flipIndex *= -1. | |||
|
r72 | ||
|
r84 | def selectChannels(self, channelList): | |
""" | |||
|
r101 | Selecciona un bloque de datos en base a canales segun el channelList | |
|
r84 | ||
Input: | |||
|
r97 | channelList : lista sencilla de canales a seleccionar por ej. [2,3,7] | |
|
r84 | ||
Affected: | |||
|
r97 | self.dataOutObj.data | |
|
r99 | self.dataOutObj.channelList | |
|
r84 | self.dataOutObj.nChannels | |
|
r99 | self.dataOutObj.m_ProcessingHeader.totalSpectra | |
|
r84 | self.dataOutObj.m_SystemHeader.numChannels | |
|
r99 | self.dataOutObj.m_ProcessingHeader.blockSize | |
|
r84 | ||
Return: | |||
None | |||
""" | |||
|
r99 | if self.dataOutObj.flagNoData: | |
|
r97 | return 0 | |
for channel in channelList: | |||
|
r99 | if channel not in self.dataOutObj.channelList: | |
|
r97 | raise ValueError, "The value %d in channelList is not valid" %channel | |
|
r84 | ||
|
r101 | nChannels = len(channelList) | |
data = self.dataOutObj.data[channelList,:] | |||
|
r99 | self.dataOutObj.data = data | |
self.dataOutObj.channelList = channelList | |||
|
r101 | self.dataOutObj.nChannels = nChannels | |
self.dataOutObj.m_ProcessingHeader.totalSpectra = nChannels | |||
self.dataOutObj.m_SystemHeader.numChannels = nChannels | |||
|
r99 | self.dataOutObj.m_ProcessingHeader.blockSize = data.size | |
|
r97 | return 1 | |
|
r72 | ||
|
r84 | def selectHeightsByValue(self, minHei, maxHei): | |
""" | |||
Selecciona un bloque de datos en base a un grupo de valores de alturas segun el rango | |||
minHei <= height <= maxHei | |||
Input: | |||
minHei : valor minimo de altura a considerar | |||
maxHei : valor maximo de altura a considerar | |||
Affected: | |||
Indirectamente son cambiados varios valores a travez del metodo selectHeightsByIndex | |||
Return: | |||
|
r97 | 1 si el metodo se ejecuto con exito caso contrario devuelve 0 | |
|
r84 | """ | |
|
r99 | if self.dataOutObj.flagNoData: | |
|
r97 | return 0 | |
|
r99 | if (minHei < self.dataOutObj.heightList[0]) or (minHei > maxHei): | |
|
r97 | raise ValueError, "some value in (%d,%d) is not valid" % (minHei, maxHei) | |
|
r99 | if (maxHei > self.dataOutObj.heightList[-1]): | |
|
r97 | raise ValueError, "some value in (%d,%d) is not valid" % (minHei, maxHei) | |
|
r84 | minIndex = 0 | |
maxIndex = 0 | |||
|
r99 | data = self.dataOutObj.heightList | |
|
r84 | ||
for i,val in enumerate(data): | |||
if val < minHei: | |||
continue | |||
else: | |||
minIndex = i; | |||
break | |||
for i,val in enumerate(data): | |||
if val <= maxHei: | |||
maxIndex = i; | |||
else: | |||
break | |||
self.selectHeightsByIndex(minIndex, maxIndex) | |||
|
r97 | return 1 | |
|
r84 | ||
def selectHeightsByIndex(self, minIndex, maxIndex): | |||
""" | |||
Selecciona un bloque de datos en base a un grupo indices de alturas segun el rango | |||
minIndex <= index <= maxIndex | |||
Input: | |||
|
r97 | minIndex : valor de indice minimo de altura a considerar | |
maxIndex : valor de indice maximo de altura a considerar | |||
|
r84 | ||
Affected: | |||
|
r99 | self.dataOutObj.data | |
self.dataOutObj.heightList | |||
self.dataOutObj.nHeights | |||
self.dataOutObj.m_ProcessingHeader.blockSize | |||
self.dataOutObj.m_ProcessingHeader.numHeights | |||
self.dataOutObj.m_ProcessingHeader.firstHeight | |||
self.dataOutObj.m_RadarControllerHeader | |||
|
r84 | ||
Return: | |||
|
r97 | 1 si el metodo se ejecuto con exito caso contrario devuelve 0 | |
|
r84 | """ | |
|
r99 | if self.dataOutObj.flagNoData: | |
|
r97 | return 0 | |
if (minIndex < 0) or (minIndex > maxIndex): | |||
raise ValueError, "some value in (%d,%d) is not valid" % (minIndex, maxIndex) | |||
|
r99 | if (maxIndex >= self.dataOutObj.nHeights): | |
|
r97 | raise ValueError, "some value in (%d,%d) is not valid" % (minIndex, maxIndex) | |
nHeights = maxIndex - minIndex + 1 | |||
|
r84 | ||
#voltage | |||
|
r99 | data = self.dataOutObj.data[:,minIndex:maxIndex+1] | |
|
r84 | ||
|
r99 | firstHeight = self.dataOutObj.heightList[minIndex] | |
|
r84 | ||
|
r99 | self.dataOutObj.data = data | |
self.dataOutObj.heightList = self.dataOutObj.heightList[minIndex:maxIndex+1] | |||
self.dataOutObj.nHeights = nHeights | |||
self.dataOutObj.m_ProcessingHeader.blockSize = data.size | |||
self.dataOutObj.m_ProcessingHeader.numHeights = nHeights | |||
self.dataOutObj.m_ProcessingHeader.firstHeight = firstHeight | |||
self.dataOutObj.m_RadarControllerHeader.numHeights = nHeights | |||
|
r97 | return 1 | |
|
r84 | ||
|
r100 | def selectProfilesByValue(self,indexList, nProfiles): | |
if self.dataOutObj.flagNoData: | |||
return 0 | |||
if self.profSelectorObjIndex >= len(self.profileSelectorObjList): | |||
self.addProfileSelector(nProfiles) | |||
profileSelectorObj = self.profileSelectorObjList[self.profSelectorObjIndex] | |||
if not(profileSelectorObj.isProfileInList(indexList)): | |||
self.dataOutObj.flagNoData = True | |||
self.profSelectorObjIndex += 1 | |||
return 0 | |||
self.dataOutObj.flagNoData = False | |||
self.profSelectorObjIndex += 1 | |||
return 1 | |||
|
r84 | ||
|
r100 | def selectProfilesByIndex(self, minIndex, maxIndex, nProfiles): | |
|
r84 | """ | |
|
r97 | Selecciona un bloque de datos en base a un grupo indices de perfiles segun el rango | |
|
r84 | minIndex <= index <= maxIndex | |
Input: | |||
|
r97 | minIndex : valor de indice minimo de perfil a considerar | |
maxIndex : valor de indice maximo de perfil a considerar | |||
nProfiles : numero de profiles | |||
|
r84 | ||
Affected: | |||
|
r99 | self.dataOutObj.flagNoData | |
self.profSelectorObjIndex | |||
|
r84 | ||
Return: | |||
|
r97 | 1 si el metodo se ejecuto con exito caso contrario devuelve 0 | |
|
r84 | """ | |
|
r92 | ||
|
r99 | if self.dataOutObj.flagNoData: | |
|
r92 | return 0 | |
|
r99 | if self.profSelectorObjIndex >= len(self.profileSelectorObjList): | |
|
r92 | self.addProfileSelector(nProfiles) | |
|
r99 | profileSelectorObj = self.profileSelectorObjList[self.profSelectorObjIndex] | |
|
r92 | ||
|
r100 | if not(profileSelectorObj.isProfileInRange(minIndex, maxIndex)): | |
self.dataOutObj.flagNoData = True | |||
|
r99 | self.profSelectorObjIndex += 1 | |
|
r100 | return 0 | |
|
r92 | ||
|
r100 | self.dataOutObj.flagNoData = False | |
|
r99 | self.profSelectorObjIndex += 1 | |
|
r92 | ||
|
r100 | return 1 | |
|
r72 | ||
|
r84 | def selectNtxs(self, ntx): | |
|
r72 | pass | |
class Decoder: | |||
|
r99 | ||
data = None | |||
profCounter = 1 | |||
|
r101 | nCode = None | |
nBaud = None | |||
|
r99 | codeIndex = 0 | |
|
r101 | code = None | |
|
r99 | flag = False | |
|
r92 | ||
|
r72 | def __init__(self,code, ncode, nbaud): | |
|
r92 | ||
|
r99 | self.data = None | |
|
r72 | self.profCounter = 1 | |
self.nCode = ncode | |||
self.nBaud = nbaud | |||
self.codeIndex = 0 | |||
self.code = code #this is a List | |||
self.flag = False | |||
def exe(self, data, ndata=None, type = 0): | |||
|
r92 | ||
|
r73 | if ndata == None: ndata = data.shape[1] | |
|
r72 | ||
if type == 0: | |||
self.convolutionInFreq(data,ndata) | |||
if type == 1: | |||
self.convolutionInTime(data, ndata) | |||
|
r92 | def convolutionInFreq(self,data, ndata): | |
|
r72 | ||
newcode = numpy.zeros(ndata) | |||
newcode[0:self.nBaud] = self.code[self.codeIndex] | |||
self.codeIndex += 1 | |||
|
r73 | fft_data = numpy.fft.fft(data, axis=1) | |
|
r72 | fft_code = numpy.conj(numpy.fft.fft(newcode)) | |
|
r73 | fft_code = fft_code.reshape(1,len(fft_code)) | |
|
r72 | ||
conv = fft_data.copy() | |||
conv.fill(0) | |||
|
r101 | conv = fft_data*fft_code | |
|
r72 | ||
|
r73 | self.data = numpy.fft.ifft(conv,axis=1) | |
|
r72 | self.flag = True | |
if self.profCounter == self.nCode: | |||
self.profCounter = 0 | |||
self.codeIndex = 0 | |||
self.profCounter += 1 | |||
def convolutionInTime(self, data, ndata): | |||
nchannel = data.shape[1] | |||
newcode = self.code[self.codeIndex] | |||
self.codeIndex += 1 | |||
conv = data.copy() | |||
for i in range(nchannel): | |||
|
r73 | conv[i,:] = numpy.correlate(data[i,:], newcode, 'same') | |
|
r72 | ||
self.data = conv | |||
self.flag = True | |||
if self.profCounter == self.nCode: | |||
self.profCounter = 0 | |||
self.codeIndex = 0 | |||
self.profCounter += 1 | |||
class CoherentIntegrator: | |||
|
r92 | ||
|
r104 | integ_counter = None | |
|
r99 | data = None | |
|
r104 | navg = None | |
|
r99 | buffer = None | |
|
r101 | nCohInt = None | |
|
r99 | ||
|
r104 | def __init__(self, N=None,timeInterval=None): | |
|
r92 | ||
|
r72 | self.data = None | |
|
r104 | self.navg = None | |
|
r72 | self.buffer = None | |
|
r104 | self.timeOut = None | |
self.exitCondition = False | |||
self.isReady = False | |||
|
r72 | self.nCohInt = N | |
|
r104 | self.integ_counter = 0 | |
if timeInterval!=None: | |||
self.timeIntervalInSeconds = timeInterval * 60. #if (type(timeInterval)!=integer) -> change this line | |||
if ((timeInterval==None) and (N==None)): | |||
print 'N = None ; timeInterval = None' | |||
sys.exit(0) | |||
elif timeInterval == None: | |||
self.timeFlag = False | |||
else: | |||
self.timeFlag = True | |||
|
r72 | ||
|
r104 | def exe(self, data, timeOfData): | |
|
r72 | ||
|
r104 | if self.timeFlag: | |
if self.timeOut == None: | |||
self.timeOut = timeOfData + self.timeIntervalInSeconds | |||
if timeOfData < self.timeOut: | |||
if self.buffer == None: | |||
self.buffer = data | |||
else: | |||
self.buffer = self.buffer + data | |||
self.integ_counter += 1 | |||
else: | |||
self.exitCondition = True | |||
|
r72 | else: | |
|
r104 | if self.integ_counter < self.nCohInt: | |
if self.buffer == None: | |||
self.buffer = data | |||
else: | |||
self.buffer = self.buffer + data | |||
self.integ_counter += 1 | |||
if self.integ_counter == self.nCohInt: | |||
self.exitCondition = True | |||
if self.exitCondition: | |||
|
r72 | self.data = self.buffer | |
|
r104 | self.navg = self.integ_counter | |
self.isReady = True | |||
|
r72 | self.buffer = None | |
|
r104 | self.timeOut = None | |
self.integ_counter = 0 | |||
self.exitCondition = False | |||
if self.timeFlag: | |||
self.buffer = data | |||
self.timeOut = timeOfData + self.timeIntervalInSeconds | |||
|
r72 | else: | |
|
r104 | self.isReady = False | |
|
r72 | ||
|
r104 | ||
|
r72 | ||
|
r99 | class ProfileSelector: | |
|
r92 | ||
|
r100 | profileIndex = None | |
|
r97 | # Tamanho total de los perfiles | |
|
r92 | nProfiles = None | |
def __init__(self, nProfiles): | |||
|
r100 | self.profileIndex = 0 | |
|
r92 | self.nProfiles = nProfiles | |
|
r100 | def incIndex(self): | |
self.profileIndex += 1 | |||
if self.profileIndex >= self.nProfiles: | |||
self.profileIndex = 0 | |||
|
r92 | def isProfileInRange(self, minIndex, maxIndex): | |
|
r100 | if self.profileIndex < minIndex: | |
|
r92 | return False | |
|
r100 | if self.profileIndex > maxIndex: | |
|
r92 | return False | |
return True | |||
def isProfileInList(self, profileList): | |||
|
r100 | if self.profileIndex not in profileList: | |
|
r92 | return False | |
return True | |||
|
r72 |