VoltageProcessor.py
419 lines
| 13.0 KiB
| text/x-python
|
PythonLexer
|
r126 | ''' | ||
$Author$ | ||||
$Id$ | ||||
''' | ||||
|
r122 | import os | ||
import sys | ||||
import numpy | ||||
|
r136 | import datetime | ||
|
r140 | import time | ||
|
r122 | |||
path = os.path.split(os.getcwd())[0] | ||||
sys.path.append(path) | ||||
|
r137 | from Data.JROData import Voltage | ||
|
r123 | from IO.VoltageIO import VoltageWriter | ||
|
r140 | from Graphics.schainPlotTypes import ScopeFigure, RTIFigure | ||
|
r122 | |||
class VoltageProcessor: | ||||
|
r137 | |||
|
r122 | dataInObj = None | ||
dataOutObj = None | ||||
integratorObjIndex = None | ||||
writerObjIndex = None | ||||
integratorObjList = None | ||||
writerObjList = None | ||||
def __init__(self): | ||||
self.integratorObjIndex = None | ||||
self.writerObjIndex = None | ||||
|
r134 | self.plotObjIndex = None | ||
|
r122 | self.integratorObjList = [] | ||
self.writerObjList = [] | ||||
|
r134 | self.plotObjList = [] | ||
|
r122 | |||
def setup(self,dataInObj=None,dataOutObj=None): | ||||
self.dataInObj = dataInObj | ||||
if self.dataOutObj == None: | ||||
dataOutObj = Voltage() | ||||
self.dataOutObj = dataOutObj | ||||
return self.dataOutObj | ||||
def init(self): | ||||
self.integratorObjIndex = 0 | ||||
self.writerObjIndex = 0 | ||||
|
r134 | self.plotObjIndex = 0 | ||
|
r123 | |||
if not(self.dataInObj.flagNoData): | ||||
self.dataOutObj.copy(self.dataInObj) | ||||
|
r122 | # No necesita copiar en cada init() los atributos de dataInObj | ||
# la copia deberia hacerse por cada nuevo bloque de datos | ||||
|
r140 | |||
|
r141 | def addRti(self, idfigure, nframes, wintitle, driver, colormap, colorbar, showprofile): | ||
rtiObj = RTIFigure(idfigure, nframes, wintitle, driver, colormap, colorbar, showprofile) | ||||
|
r140 | self.plotObjList.append(rtiObj) | ||
def plotRti(self, idfigure=None, | ||||
starttime=None, | ||||
endtime=None, | ||||
rangemin=None, | ||||
rangemax=None, | ||||
minvalue=None, | ||||
maxvalue=None, | ||||
wintitle='', | ||||
driver='plplot', | ||||
|
r156 | colormap='br_greeen', | ||
|
r140 | colorbar=True, | ||
showprofile=False, | ||||
xrangestep=None, | ||||
save=False, | ||||
|
r158 | gpath=None, | ||
ratio=1, | ||||
channelList=None): | ||||
|
r140 | |||
if self.dataOutObj.flagNoData: | ||||
return 0 | ||||
|
r158 | if channelList == None: | ||
channelList = self.dataOutObj.channelList | ||||
nframes = len(channelList) | ||||
|
r140 | |||
if len(self.plotObjList) <= self.plotObjIndex: | ||||
self.addRti(idfigure, nframes, wintitle, driver, colormap, colorbar, showprofile) | ||||
|
r158 | |||
data = self.dataOutObj.data[channelList,:] * numpy.conjugate(self.dataOutObj.data[channelList,:]) | ||||
|
r140 | data = 10*numpy.log10(data.real) | ||
|
r153 | currenttime = self.dataOutObj.utctime - time.timezone | ||
|
r140 | |||
range = self.dataOutObj.heightList | ||||
|
r153 | thisdatetime = datetime.datetime.fromtimestamp(self.dataOutObj.utctime) | ||
|
r140 | dateTime = "%s"%(thisdatetime.strftime("%d-%b-%Y %H:%M:%S")) | ||
date = "%s"%(thisdatetime.strftime("%d-%b-%Y")) | ||||
|
r157 | print thisdatetime | ||
|
r140 | figuretitle = "RTI Plot Radar Data" #+ date | ||
plotObj = self.plotObjList[self.plotObjIndex] | ||||
|
r141 | cleardata = False | ||
|
r156 | deltax = self.dataOutObj.timeInterval | ||
plotObj.plotPcolor(data=data, | ||||
x=currenttime, | ||||
y=range, | ||||
channelList=channelList, | ||||
xmin=starttime, | ||||
xmax=endtime, | ||||
ymin=rangemin, | ||||
ymax=rangemax, | ||||
minvalue=minvalue, | ||||
maxvalue=maxvalue, | ||||
figuretitle=figuretitle, | ||||
xrangestep=xrangestep, | ||||
deltax=deltax, | ||||
save=save, | ||||
gpath=gpath, | ||||
|
r158 | ratio=ratio, | ||
cleardata=cleardata | ||||
) | ||||
|
r156 | |||
|
r140 | |||
self.plotObjIndex += 1 | ||||
|
r134 | def addScope(self, idfigure, nframes, wintitle, driver): | ||
if idfigure==None: | ||||
idfigure = self.plotObjIndex | ||||
scopeObj = ScopeFigure(idfigure, nframes, wintitle, driver) | ||||
self.plotObjList.append(scopeObj) | ||||
def plotScope(self, | ||||
idfigure=None, | ||||
minvalue=None, | ||||
maxvalue=None, | ||||
xmin=None, | ||||
xmax=None, | ||||
wintitle='', | ||||
driver='plplot', | ||||
save=False, | ||||
gpath=None, | ||||
|
r158 | ratio=1, | ||
|
r134 | type="power"): | ||
if self.dataOutObj.flagNoData: | ||||
return 0 | ||||
nframes = len(self.dataOutObj.channelList) | ||||
if len(self.plotObjList) <= self.plotObjIndex: | ||||
self.addScope(idfigure, nframes, wintitle, driver) | ||||
|
r136 | |||
if type=="power": | ||||
data1D = self.dataOutObj.data * numpy.conjugate(self.dataOutObj.data) | ||||
data1D = data1D.real | ||||
if type =="iq": | ||||
data1D = self.dataOutObj.data | ||||
|
r153 | thisDatetime = datetime.datetime.fromtimestamp(self.dataOutObj.utctime) | ||
|
r136 | |||
dateTime = "%s"%(thisDatetime.strftime("%d-%b-%Y %H:%M:%S")) | ||||
date = "%s"%(thisDatetime.strftime("%d-%b-%Y")) | ||||
|
r157 | figuretitle = "Scope Plot Radar Data: " + date | ||
|
r134 | |||
|
r136 | plotObj = self.plotObjList[self.plotObjIndex] | ||
|
r134 | |||
|
r156 | plotObj.plot1DArray(data1D=data1D, | ||
x=self.dataOutObj.heightList, | ||||
channelList=self.dataOutObj.channelList, | ||||
xmin=xmin, | ||||
xmax=xmax, | ||||
minvalue=minvalue, | ||||
maxvalue=maxvalue, | ||||
|
r157 | figuretitle=figuretitle, | ||
|
r156 | save=save, | ||
|
r158 | gpath=gpath, | ||
ratio=ratio) | ||||
|
r156 | |||
|
r136 | |||
|
r134 | self.plotObjIndex += 1 | ||
|
r122 | |||
|
r143 | def addIntegrator(self, *args): | ||
objCohInt = CoherentIntegrator(*args) | ||||
|
r122 | self.integratorObjList.append(objCohInt) | ||
|
r143 | def addWriter(self, *args): | ||
|
r134 | writerObj = VoltageWriter(self.dataOutObj) | ||
|
r143 | writerObj.setup(*args) | ||
|
r134 | self.writerObjList.append(writerObj) | ||
|
r123 | |||
|
r126 | def writeData(self, wrpath, blocksPerFile, profilesPerBlock): | ||
|
r123 | |||
if self.dataOutObj.flagNoData: | ||||
return 0 | ||||
if len(self.writerObjList) <= self.writerObjIndex: | ||||
|
r126 | self.addWriter(wrpath, blocksPerFile, profilesPerBlock) | ||
|
r123 | |||
self.writerObjList[self.writerObjIndex].putData() | ||||
self.writerObjIndex += 1 | ||||
|
r143 | def integrator(self, nCohInt=None, timeInterval=None, overlapping=False): | ||
|
r122 | if self.dataOutObj.flagNoData: | ||
return 0 | ||||
|
r143 | |||
|
r122 | if len(self.integratorObjList) <= self.integratorObjIndex: | ||
|
r143 | self.addIntegrator(nCohInt, timeInterval, overlapping) | ||
|
r122 | |||
myCohIntObj = self.integratorObjList[self.integratorObjIndex] | ||||
|
r143 | myCohIntObj.exe(data = self.dataOutObj.data, datatime=None) | ||
|
r157 | self.dataOutObj.timeInterval *= nCohInt | ||
|
r143 | self.dataOutObj.flagNoData = True | ||
if myCohIntObj.isReady: | ||||
|
r158 | self.dataOutObj.timeInterval = myCohIntObj.nCohInt * self.dataOutObj.timeInterval | ||
|
r143 | self.dataOutObj.flagNoData = False | ||
|
r157 | |||
def selectChannels(self, channelList): | ||||
|
r134 | |||
|
r157 | self.selectChannelsByIndex(channelList) | ||
def selectChannelsByIndex(self, channelIndexList): | ||||
""" | ||||
Selecciona un bloque de datos en base a canales segun el channelIndexList | ||||
Input: | ||||
channelIndexList : lista sencilla de canales a seleccionar por ej. [2,3,7] | ||||
Affected: | ||||
self.dataOutObj.data | ||||
self.dataOutObj.channelIndexList | ||||
self.dataOutObj.nChannels | ||||
self.dataOutObj.m_ProcessingHeader.totalSpectra | ||||
self.dataOutObj.systemHeaderObj.numChannels | ||||
self.dataOutObj.m_ProcessingHeader.blockSize | ||||
Return: | ||||
None | ||||
""" | ||||
if self.dataOutObj.flagNoData: | ||||
return 0 | ||||
|
r122 | |||
|
r157 | for channel in channelIndexList: | ||
if channel not in self.dataOutObj.channelIndexList: | ||||
raise ValueError, "The value %d in channelIndexList is not valid" %channel | ||||
nChannels = len(channelIndexList) | ||||
data = self.dataOutObj.data[channelIndexList,:] | ||||
self.dataOutObj.data = data | ||||
self.dataOutObj.channelIndexList = channelIndexList | ||||
self.dataOutObj.channelList = [self.dataOutObj.channelList[i] for i in channelIndexList] | ||||
self.dataOutObj.nChannels = nChannels | ||||
return 1 | ||||
|
r122 | |||
class CoherentIntegrator: | ||||
|
r143 | |||
__profIndex = 0 | ||||
__withOverapping = False | ||||
__isByTime = False | ||||
__initime = None | ||||
__integrationtime = None | ||||
__buffer = None | ||||
isReady = False | ||||
|
r122 | nCohInt = None | ||
|
r143 | |||
def __init__(self, nCohInt=None, timeInterval=None, overlapping=False): | ||||
""" | ||||
Set the parameters of the integration class. | ||||
Inputs: | ||||
|
r122 | |||
|
r143 | nCohInt : Number of coherent integrations | ||
timeInterval : Time of integration. If nCohInt is selected this parameter does not work | ||||
overlapping : | ||||
""" | ||||
self.__buffer = None | ||||
|
r122 | self.isReady = False | ||
|
r143 | if nCohInt == None and timeInterval == None: | ||
raise ValueError, "nCohInt or timeInterval should be specified ..." | ||||
|
r122 | |||
|
r143 | if nCohInt != None: | ||
self.nCohInt = nCohInt | ||||
self.__isByTime = False | ||||
|
r122 | else: | ||
|
r143 | self.__integrationtime = timeInterval * 60. #if (type(timeInterval)!=integer) -> change this line | ||
self.__isByTime = True | ||||
|
r122 | |||
|
r143 | if overlapping: | ||
self.__withOverapping = True | ||||
self.__buffer = None | ||||
|
r122 | else: | ||
|
r143 | self.__withOverapping = False | ||
self.__buffer = 0 | ||||
self.__profIndex = 0 | ||||
def putData(self, data): | ||||
""" | ||||
Add a profile to the __buffer and increase in one the __profileIndex | ||||
""" | ||||
if not self.__withOverapping: | ||||
self.__buffer += data | ||||
self.__profIndex += 1 | ||||
return | ||||
#Overlapping data | ||||
|
r148 | nChannels, nHeis = data.shape | ||
data = numpy.reshape(data, (1, nChannels, nHeis)) | ||||
|
r143 | |||
if self.__buffer == None: | ||||
self.__buffer = data | ||||
self.__profIndex += 1 | ||||
return | ||||
if self.__profIndex < self.nCohInt: | ||||
self.__buffer = numpy.vstack((self.__buffer, data)) | ||||
self.__profIndex += 1 | ||||
return | ||||
self.__buffer = numpy.roll(self.__buffer, -1, axis=0) | ||||
self.__buffer[self.nCohInt-1] = data | ||||
#self.__profIndex = self.nCohInt | ||||
return | ||||
def pushData(self): | ||||
""" | ||||
Return the sum of the last profiles and the profiles used in the sum. | ||||
Affected: | ||||
self.__profileIndex | ||||
""" | ||||
if not self.__withOverapping: | ||||
data = self.__buffer | ||||
nCohInt = self.__profIndex | ||||
self.__buffer = 0 | ||||
self.__profIndex = 0 | ||||
|
r122 | |||
|
r143 | return data, nCohInt | ||
#Overlapping data | ||||
data = numpy.sum(self.__buffer, axis=0) | ||||
nCohInt = self.__profIndex | ||||
return data, nCohInt | ||||
def byProfiles(self, data): | ||||
self.isReady = False | ||||
avg_data = None | ||||
self.putData(data) | ||||
if self.__profIndex == self.nCohInt: | ||||
avg_data, nCohInt = self.pushData() | ||||
|
r122 | self.isReady = True | ||
|
r143 | |||
return avg_data | ||||
def byTime(self, data, datatime): | ||||
self.isReady = False | ||||
avg_data = None | ||||
if self.__initime == None: | ||||
self.__initime = datatime | ||||
self.putData(data) | ||||
if (datatime - self.__initime) >= self.__integrationtime: | ||||
avg_data, nCohInt = self.pushData() | ||||
self.nCohInt = nCohInt | ||||
self.isReady = True | ||||
return avg_data | ||||
def exe(self, data, datatime=None): | ||||
if not self.__isByTime: | ||||
avg_data = self.byProfiles(data) | ||||
|
r122 | else: | ||
|
r143 | avg_data = self.byTime(data, datatime) | ||
self.data = avg_data | ||||
return avg_data | ||||
|
r122 | |||