jroproc_voltage.py
1333 lines
| 40.2 KiB
| text/x-python
|
PythonLexer
|
r723 | import sys | ||
|
r487 | import numpy | ||
|
r837 | from scipy import interpolate | ||
|
r1173 | from schainpy.model.proc.jroproc_base import ProcessingUnit, MPDecorator, Operation | ||
|
r568 | from schainpy.model.data.jrodata import Voltage | ||
|
r1168 | from schainpy.utils import log | ||
|
r1173 | from time import time | ||
|
r1168 | |||
|
r487 | |||
|
r1173 | @MPDecorator | ||
|
r1116 | class VoltageProc(ProcessingUnit): | ||
|
r1173 | |||
METHODS = {} #yong | ||||
|
r897 | |||
|
r1173 | def __init__(self):#, **kwargs): #yong | ||
|
r897 | |||
|
r1173 | ProcessingUnit.__init__(self)#, **kwargs) | ||
|
r897 | |||
|
r1116 | # self.objectDict = {} | ||
|
r487 | self.dataOut = Voltage() | ||
self.flip = 1 | ||||
|
r1173 | self.setupReq = False #yong | ||
|
r487 | |||
def run(self): | ||||
|
r1173 | |||
|
r491 | if self.dataIn.type == 'AMISR': | ||
self.__updateObjFromAmisrInput() | ||||
|
r897 | |||
|
r491 | if self.dataIn.type == 'Voltage': | ||
self.dataOut.copy(self.dataIn) | ||||
|
r897 | |||
|
r1116 | # self.dataOut.copy(self.dataIn) | ||
|
r487 | |||
|
r491 | def __updateObjFromAmisrInput(self): | ||
|
r897 | |||
|
r491 | self.dataOut.timeZone = self.dataIn.timeZone | ||
self.dataOut.dstFlag = self.dataIn.dstFlag | ||||
self.dataOut.errorCount = self.dataIn.errorCount | ||||
self.dataOut.useLocalTime = self.dataIn.useLocalTime | ||||
|
r897 | |||
|
r491 | self.dataOut.flagNoData = self.dataIn.flagNoData | ||
self.dataOut.data = self.dataIn.data | ||||
self.dataOut.utctime = self.dataIn.utctime | ||||
self.dataOut.channelList = self.dataIn.channelList | ||||
|
r1116 | # self.dataOut.timeInterval = self.dataIn.timeInterval | ||
|
r491 | self.dataOut.heightList = self.dataIn.heightList | ||
self.dataOut.nProfiles = self.dataIn.nProfiles | ||||
|
r897 | |||
|
r491 | self.dataOut.nCohInt = self.dataIn.nCohInt | ||
self.dataOut.ippSeconds = self.dataIn.ippSeconds | ||||
self.dataOut.frequency = self.dataIn.frequency | ||||
|
r897 | |||
|
r499 | self.dataOut.azimuth = self.dataIn.azimuth | ||
self.dataOut.zenith = self.dataIn.zenith | ||||
|
r897 | |||
|
r501 | self.dataOut.beam.codeList = self.dataIn.beam.codeList | ||
self.dataOut.beam.azimuthList = self.dataIn.beam.azimuthList | ||||
self.dataOut.beam.zenithList = self.dataIn.beam.zenithList | ||||
|
r1116 | # | ||
# pass# | ||||
# | ||||
# def init(self): | ||||
# | ||||
# | ||||
# if self.dataIn.type == 'AMISR': | ||||
# self.__updateObjFromAmisrInput() | ||||
# | ||||
# if self.dataIn.type == 'Voltage': | ||||
# self.dataOut.copy(self.dataIn) | ||||
# # No necesita copiar en cada init() los atributos de dataIn | ||||
# # la copia deberia hacerse por cada nuevo bloque de datos | ||||
|
r897 | |||
|
r487 | def selectChannels(self, channelList): | ||
|
r897 | |||
|
r487 | channelIndexList = [] | ||
|
r897 | |||
|
r487 | for channel in channelList: | ||
|
r586 | if channel not in self.dataOut.channelList: | ||
|
r1167 | raise ValueError("Channel %d is not in %s" %(channel, str(self.dataOut.channelList))) | ||
|
r897 | |||
|
r487 | index = self.dataOut.channelList.index(channel) | ||
channelIndexList.append(index) | ||||
|
r897 | |||
|
r487 | self.selectChannelsByIndex(channelIndexList) | ||
|
r897 | |||
|
r487 | def selectChannelsByIndex(self, channelIndexList): | ||
""" | ||||
|
r897 | Selecciona un bloque de datos en base a canales segun el channelIndexList | ||
|
r487 | Input: | ||
|
r897 | channelIndexList : lista sencilla de canales a seleccionar por ej. [2,3,7] | ||
|
r487 | Affected: | ||
self.dataOut.data | ||||
self.dataOut.channelIndexList | ||||
self.dataOut.nChannels | ||||
self.dataOut.m_ProcessingHeader.totalSpectra | ||||
self.dataOut.systemHeaderObj.numChannels | ||||
self.dataOut.m_ProcessingHeader.blockSize | ||||
|
r897 | |||
|
r487 | Return: | ||
None | ||||
""" | ||||
for channelIndex in channelIndexList: | ||||
if channelIndex not in self.dataOut.channelIndexList: | ||||
|
r1167 | print(channelIndexList) | ||
raise ValueError("The value %d in channelIndexList is not valid" %channelIndex) | ||||
|
r897 | |||
|
r545 | if self.dataOut.flagDataAsBlock: | ||
|
r530 | """ | ||
Si la data es obtenida por bloques, dimension = [nChannels, nProfiles, nHeis] | ||||
""" | ||||
data = self.dataOut.data[channelIndexList,:,:] | ||||
else: | ||||
data = self.dataOut.data[channelIndexList,:] | ||||
|
r897 | |||
|
r487 | self.dataOut.data = data | ||
self.dataOut.channelList = [self.dataOut.channelList[i] for i in channelIndexList] | ||||
|
r1116 | # self.dataOut.nChannels = nChannels | ||
|
r897 | |||
|
r487 | return 1 | ||
|
r897 | |||
|
r487 | def selectHeights(self, minHei=None, maxHei=None): | ||
""" | ||||
Selecciona un bloque de datos en base a un grupo de valores de alturas segun el rango | ||||
minHei <= height <= maxHei | ||||
|
r897 | |||
|
r487 | Input: | ||
|
r897 | minHei : valor minimo de altura a considerar | ||
|
r487 | maxHei : valor maximo de altura a considerar | ||
|
r897 | |||
|
r487 | Affected: | ||
Indirectamente son cambiados varios valores a travez del metodo selectHeightsByIndex | ||||
|
r897 | |||
|
r487 | Return: | ||
1 si el metodo se ejecuto con exito caso contrario devuelve 0 | ||||
""" | ||||
|
r897 | |||
|
r487 | if minHei == None: | ||
minHei = self.dataOut.heightList[0] | ||||
|
r897 | |||
|
r487 | if maxHei == None: | ||
maxHei = self.dataOut.heightList[-1] | ||||
|
r897 | |||
|
r624 | if (minHei < self.dataOut.heightList[0]): | ||
minHei = self.dataOut.heightList[0] | ||||
|
r684 | |||
|
r487 | if (maxHei > self.dataOut.heightList[-1]): | ||
maxHei = self.dataOut.heightList[-1] | ||||
|
r897 | |||
|
r487 | minIndex = 0 | ||
maxIndex = 0 | ||||
heights = self.dataOut.heightList | ||||
|
r897 | |||
|
r487 | inda = numpy.where(heights >= minHei) | ||
indb = numpy.where(heights <= maxHei) | ||||
|
r897 | |||
|
r487 | try: | ||
minIndex = inda[0][0] | ||||
except: | ||||
minIndex = 0 | ||||
|
r897 | |||
|
r487 | try: | ||
maxIndex = indb[0][-1] | ||||
except: | ||||
maxIndex = len(heights) | ||||
self.selectHeightsByIndex(minIndex, maxIndex) | ||||
|
r897 | |||
|
r487 | return 1 | ||
|
r897 | |||
|
r487 | def selectHeightsByIndex(self, minIndex, maxIndex): | ||
""" | ||||
Selecciona un bloque de datos en base a un grupo indices de alturas segun el rango | ||||
minIndex <= index <= maxIndex | ||||
|
r897 | |||
|
r487 | Input: | ||
|
r897 | minIndex : valor de indice minimo de altura a considerar | ||
|
r487 | maxIndex : valor de indice maximo de altura a considerar | ||
|
r897 | |||
|
r487 | Affected: | ||
self.dataOut.data | ||||
self.dataOut.heightList | ||||
|
r897 | |||
|
r487 | Return: | ||
1 si el metodo se ejecuto con exito caso contrario devuelve 0 | ||||
""" | ||||
|
r897 | |||
|
r487 | if (minIndex < 0) or (minIndex > maxIndex): | ||
|
r1167 | raise ValueError("Height index range (%d,%d) is not valid" % (minIndex, maxIndex)) | ||
|
r897 | |||
|
r487 | if (maxIndex >= self.dataOut.nHeights): | ||
|
r534 | maxIndex = self.dataOut.nHeights | ||
|
r897 | |||
|
r487 | #voltage | ||
|
r532 | if self.dataOut.flagDataAsBlock: | ||
|
r530 | """ | ||
Si la data es obtenida por bloques, dimension = [nChannels, nProfiles, nHeis] | ||||
""" | ||||
|
r660 | data = self.dataOut.data[:,:, minIndex:maxIndex] | ||
|
r530 | else: | ||
|
r660 | data = self.dataOut.data[:, minIndex:maxIndex] | ||
|
r487 | |||
|
r1116 | # firstHeight = self.dataOut.heightList[minIndex] | ||
|
r487 | |||
self.dataOut.data = data | ||||
|
r534 | self.dataOut.heightList = self.dataOut.heightList[minIndex:maxIndex] | ||
|
r897 | |||
|
r596 | if self.dataOut.nHeights <= 1: | ||
|
r1167 | raise ValueError("selectHeights: Too few heights. Current number of heights is %d" %(self.dataOut.nHeights)) | ||
|
r897 | |||
|
r487 | return 1 | ||
|
r897 | |||
|
r568 | def filterByHeights(self, window): | ||
|
r897 | |||
|
r487 | deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0] | ||
|
r897 | |||
|
r487 | if window == None: | ||
window = (self.dataOut.radarControllerHeaderObj.txA/self.dataOut.radarControllerHeaderObj.nBaud) / deltaHeight | ||||
|
r897 | |||
|
r487 | newdelta = deltaHeight * window | ||
|
r530 | r = self.dataOut.nHeights % window | ||
|
r596 | newheights = (self.dataOut.nHeights-r)/window | ||
|
r897 | |||
|
r596 | if newheights <= 1: | ||
|
r1167 | raise ValueError("filterByHeights: Too few heights. Current number of heights is %d and window is %d" %(self.dataOut.nHeights, window)) | ||
|
r897 | |||
|
r545 | if self.dataOut.flagDataAsBlock: | ||
|
r530 | """ | ||
Si la data es obtenida por bloques, dimension = [nChannels, nProfiles, nHeis] | ||||
""" | ||||
buffer = self.dataOut.data[:, :, 0:self.dataOut.nHeights-r] | ||||
|
r564 | buffer = buffer.reshape(self.dataOut.nChannels,self.dataOut.nProfiles,self.dataOut.nHeights/window,window) | ||
|
r530 | buffer = numpy.sum(buffer,3) | ||
|
r897 | |||
|
r530 | else: | ||
|
r897 | buffer = self.dataOut.data[:,0:self.dataOut.nHeights-r] | ||
|
r530 | buffer = buffer.reshape(self.dataOut.nChannels,self.dataOut.nHeights/window,window) | ||
buffer = numpy.sum(buffer,2) | ||||
|
r596 | self.dataOut.data = buffer | ||
self.dataOut.heightList = self.dataOut.heightList[0] + numpy.arange( newheights )*newdelta | ||||
|
r487 | self.dataOut.windowOfFilter = window | ||
|
r568 | |||
def setH0(self, h0, deltaHeight = None): | ||||
|
r897 | |||
|
r568 | if not deltaHeight: | ||
deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0] | ||||
|
r897 | |||
|
r568 | nHeights = self.dataOut.nHeights | ||
|
r897 | |||
|
r568 | newHeiRange = h0 + numpy.arange(nHeights)*deltaHeight | ||
|
r897 | |||
|
r568 | self.dataOut.heightList = newHeiRange | ||
|
r897 | |||
|
r530 | def deFlip(self, channelList = []): | ||
|
r897 | |||
|
r530 | data = self.dataOut.data.copy() | ||
|
r897 | |||
|
r530 | if self.dataOut.flagDataAsBlock: | ||
flip = self.flip | ||||
|
r1167 | profileList = list(range(self.dataOut.nProfiles)) | ||
|
r897 | |||
|
r587 | if not channelList: | ||
|
r530 | for thisProfile in profileList: | ||
data[:,thisProfile,:] = data[:,thisProfile,:]*flip | ||||
flip *= -1.0 | ||||
else: | ||||
for thisChannel in channelList: | ||||
|
r586 | if thisChannel not in self.dataOut.channelList: | ||
continue | ||||
|
r897 | |||
|
r530 | for thisProfile in profileList: | ||
data[thisChannel,thisProfile,:] = data[thisChannel,thisProfile,:]*flip | ||||
flip *= -1.0 | ||||
|
r897 | |||
|
r530 | self.flip = flip | ||
|
r897 | |||
|
r530 | else: | ||
|
r587 | if not channelList: | ||
|
r530 | data[:,:] = data[:,:]*self.flip | ||
else: | ||||
for thisChannel in channelList: | ||||
|
r586 | if thisChannel not in self.dataOut.channelList: | ||
continue | ||||
|
r897 | |||
|
r530 | data[thisChannel,:] = data[thisChannel,:]*self.flip | ||
|
r897 | |||
|
r530 | self.flip *= -1. | ||
|
r897 | |||
|
r530 | self.dataOut.data = data | ||
|
r897 | |||
|
r487 | def setRadarFrequency(self, frequency=None): | ||
|
r897 | |||
|
r487 | if frequency != None: | ||
self.dataOut.frequency = frequency | ||||
|
r897 | |||
|
r487 | return 1 | ||
|
r897 | |||
|
r836 | def interpolateHeights(self, topLim, botLim): | ||
#69 al 72 para julia | ||||
#82-84 para meteoros | ||||
if len(numpy.shape(self.dataOut.data))==2: | ||||
sampInterp = (self.dataOut.data[:,botLim-1] + self.dataOut.data[:,topLim+1])/2 | ||||
sampInterp = numpy.transpose(numpy.tile(sampInterp,(topLim-botLim + 1,1))) | ||||
|
r837 | #self.dataOut.data[:,botLim:limSup+1] = sampInterp | ||
self.dataOut.data[:,botLim:topLim+1] = sampInterp | ||||
|
r836 | else: | ||
|
r837 | nHeights = self.dataOut.data.shape[2] | ||
x = numpy.hstack((numpy.arange(botLim),numpy.arange(topLim+1,nHeights))) | ||||
|
r1167 | y = self.dataOut.data[:,:,list(range(botLim))+list(range(topLim+1,nHeights))] | ||
|
r837 | f = interpolate.interp1d(x, y, axis = 2) | ||
xnew = numpy.arange(botLim,topLim+1) | ||||
ynew = f(xnew) | ||||
|
r897 | |||
|
r837 | self.dataOut.data[:,:,botLim:topLim+1] = ynew | ||
|
r1116 | # import collections | ||
|
r1173 | @MPDecorator | ||
|
r487 | class CohInt(Operation): | ||
|
r897 | |||
|
r487 | isConfig = False | ||
__profIndex = 0 | ||||
__byTime = False | ||||
__initime = None | ||||
__lastdatatime = None | ||||
__integrationtime = None | ||||
__buffer = None | ||||
|
r1116 | __bufferStride = [] | ||
|
r487 | __dataReady = False | ||
|
r1116 | __profIndexStride = 0 | ||
__dataToPutStride = False | ||||
|
r487 | n = None | ||
|
r897 | |||
|
r1173 | def __init__(self):#, **kwargs): | ||
|
r897 | |||
|
r1173 | Operation.__init__(self)#, **kwargs) | ||
|
r897 | |||
|
r1116 | # self.isConfig = False | ||
|
r897 | |||
|
r1116 | def setup(self, n=None, timeInterval=None, stride=None, overlapping=False, byblock=False): | ||
|
r487 | """ | ||
Set the parameters of the integration class. | ||||
|
r897 | |||
|
r487 | Inputs: | ||
|
r897 | |||
|
r953 | n : Number of coherent integrations | ||
timeInterval : Time of integration. If the parameter "n" is selected this one does not work | ||||
overlapping : | ||||
|
r487 | """ | ||
|
r897 | |||
|
r487 | self.__initime = None | ||
self.__lastdatatime = 0 | ||||
self.__buffer = None | ||||
self.__dataReady = False | ||||
|
r495 | self.byblock = byblock | ||
|
r1116 | self.stride = stride | ||
|
r897 | |||
|
r487 | if n == None and timeInterval == None: | ||
|
r1167 | raise ValueError("n or timeInterval should be specified ...") | ||
|
r897 | |||
|
r487 | if n != None: | ||
self.n = n | ||||
self.__byTime = False | ||||
else: | ||||
|
r510 | self.__integrationtime = timeInterval #* 60. #if (type(timeInterval)!=integer) -> change this line | ||
|
r487 | self.n = 9999 | ||
self.__byTime = True | ||||
|
r897 | |||
|
r487 | if overlapping: | ||
|
r1116 | self.__withOverlapping = True | ||
|
r487 | self.__buffer = None | ||
else: | ||||
|
r1116 | self.__withOverlapping = False | ||
|
r487 | self.__buffer = 0 | ||
|
r897 | |||
|
r487 | self.__profIndex = 0 | ||
|
r897 | |||
|
r487 | def putData(self, data): | ||
|
r897 | |||
|
r487 | """ | ||
Add a profile to the __buffer and increase in one the __profileIndex | ||||
|
r897 | |||
|
r487 | """ | ||
|
r897 | |||
|
r1116 | if not self.__withOverlapping: | ||
|
r487 | self.__buffer += data.copy() | ||
|
r897 | self.__profIndex += 1 | ||
|
r487 | return | ||
|
r897 | |||
|
r487 | #Overlapping data | ||
nChannels, nHeis = data.shape | ||||
data = numpy.reshape(data, (1, nChannels, nHeis)) | ||||
|
r897 | |||
|
r487 | #If the buffer is empty then it takes the data value | ||
|
r611 | if self.__buffer is None: | ||
|
r487 | self.__buffer = data | ||
self.__profIndex += 1 | ||||
return | ||||
|
r897 | |||
|
r487 | #If the buffer length is lower than n then stakcing the data value | ||
if self.__profIndex < self.n: | ||||
self.__buffer = numpy.vstack((self.__buffer, data)) | ||||
self.__profIndex += 1 | ||||
return | ||||
|
r897 | |||
#If the buffer length is equal to n then replacing the last buffer value with the data value | ||||
|
r487 | self.__buffer = numpy.roll(self.__buffer, -1, axis=0) | ||
self.__buffer[self.n-1] = data | ||||
self.__profIndex = self.n | ||||
return | ||||
|
r897 | |||
|
r487 | def pushData(self): | ||
""" | ||||
Return the sum of the last profiles and the profiles used in the sum. | ||||
|
r897 | |||
|
r487 | Affected: | ||
|
r897 | |||
|
r487 | self.__profileIndex | ||
|
r897 | |||
|
r487 | """ | ||
|
r897 | |||
|
r1116 | if not self.__withOverlapping: | ||
|
r487 | data = self.__buffer | ||
n = self.__profIndex | ||||
|
r897 | |||
|
r487 | self.__buffer = 0 | ||
self.__profIndex = 0 | ||||
|
r897 | |||
|
r487 | return data, n | ||
|
r897 | |||
|
r487 | #Integration with Overlapping | ||
data = numpy.sum(self.__buffer, axis=0) | ||||
|
r1116 | # print data | ||
# raise | ||||
|
r487 | n = self.__profIndex | ||
|
r897 | |||
|
r487 | return data, n | ||
|
r897 | |||
|
r487 | def byProfiles(self, data): | ||
|
r897 | |||
|
r487 | self.__dataReady = False | ||
avgdata = None | ||||
|
r1116 | # n = None | ||
# print data | ||||
# raise | ||||
|
r487 | self.putData(data) | ||
|
r897 | |||
|
r487 | if self.__profIndex == self.n: | ||
avgdata, n = self.pushData() | ||||
self.__dataReady = True | ||||
|
r897 | |||
|
r487 | return avgdata | ||
|
r897 | |||
|
r487 | def byTime(self, data, datatime): | ||
|
r897 | |||
|
r487 | self.__dataReady = False | ||
avgdata = None | ||||
n = None | ||||
|
r897 | |||
|
r487 | self.putData(data) | ||
|
r897 | |||
|
r487 | if (datatime - self.__initime) >= self.__integrationtime: | ||
avgdata, n = self.pushData() | ||||
self.n = n | ||||
self.__dataReady = True | ||||
|
r897 | |||
|
r487 | return avgdata | ||
|
r897 | |||
|
r1116 | def integrateByStride(self, data, datatime): | ||
# print data | ||||
if self.__profIndex == 0: | ||||
self.__buffer = [[data.copy(), datatime]] | ||||
else: | ||||
|
r1117 | self.__buffer.append([data.copy(),datatime]) | ||
|
r1116 | self.__profIndex += 1 | ||
self.__dataReady = False | ||||
if self.__profIndex == self.n * self.stride : | ||||
self.__dataToPutStride = True | ||||
self.__profIndexStride = 0 | ||||
self.__profIndex = 0 | ||||
self.__bufferStride = [] | ||||
for i in range(self.stride): | ||||
current = self.__buffer[i::self.stride] | ||||
data = numpy.sum([t[0] for t in current], axis=0) | ||||
avgdatatime = numpy.average([t[1] for t in current]) | ||||
# print data | ||||
self.__bufferStride.append((data, avgdatatime)) | ||||
if self.__dataToPutStride: | ||||
|
r1117 | self.__dataReady = True | ||
|
r1116 | self.__profIndexStride += 1 | ||
if self.__profIndexStride == self.stride: | ||||
self.__dataToPutStride = False | ||||
# print self.__bufferStride[self.__profIndexStride - 1] | ||||
# raise | ||||
|
r1117 | return self.__bufferStride[self.__profIndexStride - 1] | ||
|
r1116 | |||
return None, None | ||||
|
r487 | def integrate(self, data, datatime=None): | ||
|
r897 | |||
|
r487 | if self.__initime == None: | ||
self.__initime = datatime | ||||
|
r897 | |||
|
r487 | if self.__byTime: | ||
avgdata = self.byTime(data, datatime) | ||||
else: | ||||
avgdata = self.byProfiles(data) | ||||
|
r897 | |||
|
r487 | self.__lastdatatime = datatime | ||
|
r897 | |||
|
r611 | if avgdata is None: | ||
|
r487 | return None, None | ||
|
r897 | |||
|
r487 | avgdatatime = self.__initime | ||
|
r897 | |||
|
r1116 | deltatime = datatime - self.__lastdatatime | ||
if not self.__withOverlapping: | ||||
|
r487 | self.__initime = datatime | ||
else: | ||||
self.__initime += deltatime | ||||
|
r897 | |||
|
r487 | return avgdata, avgdatatime | ||
|
r897 | |||
|
r495 | def integrateByBlock(self, dataOut): | ||
|
r897 | |||
|
r495 | times = int(dataOut.data.shape[1]/self.n) | ||
avgdata = numpy.zeros((dataOut.nChannels, times, dataOut.nHeights), dtype=numpy.complex) | ||||
|
r897 | |||
|
r495 | id_min = 0 | ||
id_max = self.n | ||||
|
r897 | |||
|
r495 | for i in range(times): | ||
junk = dataOut.data[:,id_min:id_max,:] | ||||
avgdata[:,i,:] = junk.sum(axis=1) | ||||
id_min += self.n | ||||
|
r897 | id_max += self.n | ||
|
r495 | timeInterval = dataOut.ippSeconds*self.n | ||
|
r897 | avgdatatime = (times - 1) * timeInterval + dataOut.utctime | ||
|
r495 | self.__dataReady = True | ||
return avgdata, avgdatatime | ||||
|
r975 | |||
|
r1116 | def run(self, dataOut, n=None, timeInterval=None, stride=None, overlapping=False, byblock=False, **kwargs): | ||
|
r1173 | |||
|
r487 | if not self.isConfig: | ||
|
r1116 | self.setup(n=n, stride=stride, timeInterval=timeInterval, overlapping=overlapping, byblock=byblock, **kwargs) | ||
|
r487 | self.isConfig = True | ||
|
r897 | |||
|
r530 | if dataOut.flagDataAsBlock: | ||
""" | ||||
Si la data es leida por bloques, dimension = [nChannels, nProfiles, nHeis] | ||||
""" | ||||
|
r495 | avgdata, avgdatatime = self.integrateByBlock(dataOut) | ||
|
r720 | dataOut.nProfiles /= self.n | ||
|
r897 | else: | ||
|
r1116 | if stride is None: | ||
avgdata, avgdatatime = self.integrate(dataOut.data, dataOut.utctime) | ||||
else: | ||||
avgdata, avgdatatime = self.integrateByStride(dataOut.data, dataOut.utctime) | ||||
|
r897 | |||
|
r1116 | |||
# dataOut.timeInterval *= n | ||||
|
r487 | dataOut.flagNoData = True | ||
|
r897 | |||
|
r487 | if self.__dataReady: | ||
dataOut.data = avgdata | ||||
dataOut.nCohInt *= self.n | ||||
dataOut.utctime = avgdatatime | ||||
|
r1116 | # print avgdata, avgdatatime | ||
# raise | ||||
# dataOut.timeInterval = dataOut.ippSeconds * dataOut.nCohInt | ||||
|
r487 | dataOut.flagNoData = False | ||
|
r1173 | return dataOut | ||
@MPDecorator | ||||
|
r487 | class Decoder(Operation): | ||
|
r897 | |||
|
r487 | isConfig = False | ||
__profIndex = 0 | ||||
|
r897 | |||
|
r487 | code = None | ||
|
r897 | |||
nCode = None | ||||
|
r487 | nBaud = None | ||
|
r897 | |||
|
r1173 | def __init__(self):#, **kwargs): | ||
|
r897 | |||
|
r1173 | Operation.__init__(self)#, **kwargs) | ||
|
r897 | |||
|
r495 | self.times = None | ||
self.osamp = None | ||||
|
r1004 | # self.__setValues = False | ||
|
r1173 | # self.isConfig = False | ||
self.setupReq = False | ||||
|
r530 | def setup(self, code, osamp, dataOut): | ||
|
r897 | |||
|
r487 | self.__profIndex = 0 | ||
|
r897 | |||
|
r487 | self.code = code | ||
|
r897 | |||
self.nCode = len(code) | ||||
|
r487 | self.nBaud = len(code[0]) | ||
|
r897 | |||
|
r530 | if (osamp != None) and (osamp >1): | ||
|
r495 | self.osamp = osamp | ||
|
r530 | self.code = numpy.repeat(code, repeats=self.osamp, axis=1) | ||
|
r495 | self.nBaud = self.nBaud*self.osamp | ||
|
r897 | |||
|
r530 | self.__nChannels = dataOut.nChannels | ||
self.__nProfiles = dataOut.nProfiles | ||||
self.__nHeis = dataOut.nHeights | ||||
|
r897 | |||
|
r655 | if self.__nHeis < self.nBaud: | ||
|
r1167 | raise ValueError('Number of heights (%d) should be greater than number of bauds (%d)' %(self.__nHeis, self.nBaud)) | ||
|
r897 | |||
|
r660 | #Frequency | ||
__codeBuffer = numpy.zeros((self.nCode, self.__nHeis), dtype=numpy.complex) | ||||
|
r897 | |||
|
r660 | __codeBuffer[:,0:self.nBaud] = self.code | ||
|
r897 | |||
|
r660 | self.fft_code = numpy.conj(numpy.fft.fft(__codeBuffer, axis=1)) | ||
|
r897 | |||
|
r530 | if dataOut.flagDataAsBlock: | ||
|
r897 | |||
|
r534 | self.ndatadec = self.__nHeis #- self.nBaud + 1 | ||
|
r897 | |||
|
r530 | self.datadecTime = numpy.zeros((self.__nChannels, self.__nProfiles, self.ndatadec), dtype=numpy.complex) | ||
|
r897 | |||
|
r530 | else: | ||
|
r897 | |||
|
r660 | #Time | ||
|
r534 | self.ndatadec = self.__nHeis #- self.nBaud + 1 | ||
|
r897 | |||
self.datadecTime = numpy.zeros((self.__nChannels, self.ndatadec), dtype=numpy.complex) | ||||
|
r660 | def __convolutionInFreq(self, data): | ||
|
r897 | |||
|
r487 | fft_code = self.fft_code[self.__profIndex].reshape(1,-1) | ||
|
r897 | |||
|
r487 | fft_data = numpy.fft.fft(data, axis=1) | ||
|
r897 | |||
|
r487 | conv = fft_data*fft_code | ||
|
r897 | |||
|
r487 | data = numpy.fft.ifft(conv,axis=1) | ||
|
r897 | |||
|
r660 | return data | ||
|
r897 | |||
|
r660 | def __convolutionInFreqOpt(self, data): | ||
|
r897 | |||
|
r568 | raise NotImplementedError | ||
|
r897 | |||
|
r660 | def __convolutionInTime(self, data): | ||
|
r897 | |||
|
r487 | code = self.code[self.__profIndex] | ||
for i in range(self.__nChannels): | ||||
|
r611 | self.datadecTime[i,:] = numpy.correlate(data[i,:], code, mode='full')[self.nBaud-1:] | ||
|
r897 | |||
|
r487 | return self.datadecTime | ||
|
r897 | |||
|
r660 | def __convolutionByBlockInTime(self, data): | ||
|
r897 | |||
|
r530 | repetitions = self.__nProfiles / self.nCode | ||
|
r997 | |||
|
r530 | junk = numpy.lib.stride_tricks.as_strided(self.code, (repetitions, self.code.size), (0, self.code.itemsize)) | ||
|
r495 | junk = junk.flatten() | ||
|
r530 | code_block = numpy.reshape(junk, (self.nCode*repetitions, self.nBaud)) | ||
|
r1167 | profilesList = range(self.__nProfiles) | ||
|
r1004 | |||
|
r1050 | for i in range(self.__nChannels): | ||
for j in profilesList: | ||||
self.datadecTime[i,j,:] = numpy.correlate(data[i,j,:], code_block[j,:], mode='full')[self.nBaud-1:] | ||||
return self.datadecTime | ||||
|
r897 | |||
|
r660 | def __convolutionByBlockInFreq(self, data): | ||
|
r897 | |||
|
r1167 | raise NotImplementedError("Decoder by frequency fro Blocks not implemented") | ||
|
r749 | |||
|
r660 | fft_code = self.fft_code[self.__profIndex].reshape(1,-1) | ||
|
r897 | |||
|
r660 | fft_data = numpy.fft.fft(data, axis=2) | ||
|
r897 | |||
|
r660 | conv = fft_data*fft_code | ||
|
r897 | |||
|
r660 | data = numpy.fft.ifft(conv,axis=2) | ||
|
r897 | |||
|
r660 | return data | ||
|
r897 | |||
|
r1004 | |||
|
r605 | def run(self, dataOut, code=None, nCode=None, nBaud=None, mode = 0, osamp=None, times=None): | ||
|
r897 | |||
|
r624 | if dataOut.flagDecodeData: | ||
|
r1167 | print("This data is already decoded, recoding again ...") | ||
|
r897 | |||
|
r495 | if not self.isConfig: | ||
|
r897 | |||
|
r611 | if code is None: | ||
|
r674 | if dataOut.code is None: | ||
|
r1167 | raise ValueError("Code could not be read from %s instance. Enter a value in Code parameter" %dataOut.type) | ||
|
r897 | |||
|
r530 | code = dataOut.code | ||
else: | ||||
code = numpy.array(code).reshape(nCode,nBaud) | ||||
self.setup(code, osamp, dataOut) | ||||
|
r897 | |||
|
r487 | self.isConfig = True | ||
|
r897 | |||
|
r723 | if mode == 3: | ||
sys.stderr.write("Decoder Warning: mode=%d is not valid, using mode=0\n" %mode) | ||||
|
r897 | |||
|
r749 | if times != None: | ||
sys.stderr.write("Decoder Warning: Argument 'times' in not used anymore\n") | ||||
|
r897 | |||
|
r624 | if self.code is None: | ||
|
r1167 | print("Fail decoding: Code is not defined.") | ||
|
r624 | return | ||
|
r897 | |||
|
r997 | self.__nProfiles = dataOut.nProfiles | ||
|
r660 | datadec = None | ||
|
r997 | |||
|
r749 | if mode == 3: | ||
mode = 0 | ||||
|
r897 | |||
|
r530 | if dataOut.flagDataAsBlock: | ||
""" | ||||
Decoding when data have been read as block, | ||||
""" | ||||
|
r897 | |||
|
r660 | if mode == 0: | ||
datadec = self.__convolutionByBlockInTime(dataOut.data) | ||||
if mode == 1: | ||||
datadec = self.__convolutionByBlockInFreq(dataOut.data) | ||||
|
r530 | else: | ||
""" | ||||
Decoding when data have been read profile by profile | ||||
""" | ||||
if mode == 0: | ||||
|
r660 | datadec = self.__convolutionInTime(dataOut.data) | ||
|
r897 | |||
|
r530 | if mode == 1: | ||
|
r660 | datadec = self.__convolutionInFreq(dataOut.data) | ||
|
r897 | |||
|
r530 | if mode == 2: | ||
|
r660 | datadec = self.__convolutionInFreqOpt(dataOut.data) | ||
|
r897 | |||
|
r660 | if datadec is None: | ||
|
r1167 | raise ValueError("Codification mode selected is not valid: mode=%d. Try selecting 0 or 1" %mode) | ||
|
r897 | |||
|
r544 | dataOut.code = self.code | ||
dataOut.nCode = self.nCode | ||||
dataOut.nBaud = self.nBaud | ||||
|
r897 | |||
|
r487 | dataOut.data = datadec | ||
|
r897 | |||
|
r660 | dataOut.heightList = dataOut.heightList[0:datadec.shape[-1]] | ||
|
r897 | |||
|
r530 | dataOut.flagDecodeData = True #asumo q la data esta decodificada | ||
|
r487 | |||
|
r897 | if self.__profIndex == self.nCode-1: | ||
self.__profIndex = 0 | ||||
|
r1173 | return dataOut | ||
|
r897 | |||
|
r487 | self.__profIndex += 1 | ||
|
r897 | |||
|
r1173 | return dataOut | ||
|
r1124 | # dataOut.flagDeflipData = True #asumo q la data no esta sin flip | ||
|
r495 | |||
|
r1173 | @MPDecorator | ||
|
r495 | class ProfileConcat(Operation): | ||
|
r897 | |||
|
r495 | isConfig = False | ||
buffer = None | ||||
|
r897 | |||
|
r1173 | def __init__(self):#, **kwargs): | ||
|
r897 | |||
|
r1173 | Operation.__init__(self)#, **kwargs) | ||
|
r495 | self.profileIndex = 0 | ||
|
r897 | |||
|
r495 | def reset(self): | ||
self.buffer = numpy.zeros_like(self.buffer) | ||||
self.start_index = 0 | ||||
self.times = 1 | ||||
|
r897 | |||
|
r495 | def setup(self, data, m, n=1): | ||
self.buffer = numpy.zeros((data.shape[0],data.shape[1]*m),dtype=type(data[0,0])) | ||||
|
r808 | self.nHeights = data.shape[1]#.nHeights | ||
|
r495 | self.start_index = 0 | ||
self.times = 1 | ||||
|
r897 | |||
|
r495 | def concat(self, data): | ||
|
r897 | |||
|
r808 | self.buffer[:,self.start_index:self.nHeights*self.times] = data.copy() | ||
|
r897 | self.start_index = self.start_index + self.nHeights | ||
|
r495 | def run(self, dataOut, m): | ||
|
r897 | |||
|
r495 | dataOut.flagNoData = True | ||
|
r897 | |||
|
r495 | if not self.isConfig: | ||
self.setup(dataOut.data, m, 1) | ||||
self.isConfig = True | ||||
|
r897 | |||
|
r530 | if dataOut.flagDataAsBlock: | ||
|
r1167 | raise ValueError("ProfileConcat can only be used when voltage have been read profile by profile, getBlock = False") | ||
|
r897 | |||
|
r530 | else: | ||
self.concat(dataOut.data) | ||||
self.times += 1 | ||||
if self.times > m: | ||||
dataOut.data = self.buffer | ||||
self.reset() | ||||
dataOut.flagNoData = False | ||||
# se deben actualizar mas propiedades del header y del objeto dataOut, por ejemplo, las alturas | ||||
|
r897 | deltaHeight = dataOut.heightList[1] - dataOut.heightList[0] | ||
|
r534 | xf = dataOut.heightList[0] + dataOut.nHeights * deltaHeight * m | ||
|
r897 | dataOut.heightList = numpy.arange(dataOut.heightList[0], xf, deltaHeight) | ||
|
r534 | dataOut.ippSeconds *= m | ||
|
r1173 | return dataOut | ||
@MPDecorator | ||||
|
r495 | class ProfileSelector(Operation): | ||
|
r897 | |||
|
r495 | profileIndex = None | ||
# Tamanho total de los perfiles | ||||
nProfiles = None | ||||
|
r897 | |||
|
r1173 | def __init__(self):#, **kwargs): | ||
|
r897 | |||
|
r1173 | Operation.__init__(self)#, **kwargs) | ||
|
r495 | self.profileIndex = 0 | ||
|
r897 | |||
|
r754 | def incProfileIndex(self): | ||
|
r897 | |||
|
r495 | self.profileIndex += 1 | ||
|
r897 | |||
|
r495 | if self.profileIndex >= self.nProfiles: | ||
self.profileIndex = 0 | ||||
|
r897 | |||
|
r534 | def isThisProfileInRange(self, profileIndex, minIndex, maxIndex): | ||
|
r897 | |||
|
r534 | if profileIndex < minIndex: | ||
|
r495 | return False | ||
|
r897 | |||
|
r534 | if profileIndex > maxIndex: | ||
|
r495 | return False | ||
|
r897 | |||
|
r495 | return True | ||
|
r897 | |||
|
r534 | def isThisProfileInList(self, profileIndex, profileList): | ||
|
r897 | |||
|
r534 | if profileIndex not in profileList: | ||
|
r495 | return False | ||
|
r897 | |||
|
r495 | return True | ||
|
r897 | |||
|
r600 | def run(self, dataOut, profileList=None, profileRangeList=None, beam=None, byblock=False, rangeList = None, nProfiles=None): | ||
|
r530 | |||
""" | ||||
ProfileSelector: | ||||
|
r897 | |||
|
r534 | Inputs: | ||
|
r897 | profileList : Index of profiles selected. Example: profileList = (0,1,2,7,8) | ||
|
r534 | profileRangeList : Minimum and maximum profile indexes. Example: profileRangeList = (4, 30) | ||
|
r897 | |||
|
r534 | rangeList : List of profile ranges. Example: rangeList = ((4, 30), (32, 64), (128, 256)) | ||
|
r897 | |||
|
r530 | """ | ||
|
r897 | |||
|
r756 | if rangeList is not None: | ||
if type(rangeList[0]) not in (tuple, list): | ||||
rangeList = [rangeList] | ||||
|
r897 | |||
|
r495 | dataOut.flagNoData = True | ||
|
r897 | |||
|
r530 | if dataOut.flagDataAsBlock: | ||
""" | ||||
data dimension = [nChannels, nProfiles, nHeis] | ||||
""" | ||||
|
r495 | if profileList != None: | ||
dataOut.data = dataOut.data[:,profileList,:] | ||||
|
r897 | |||
|
r596 | if profileRangeList != None: | ||
|
r534 | minIndex = profileRangeList[0] | ||
maxIndex = profileRangeList[1] | ||||
|
r1167 | profileList = list(range(minIndex, maxIndex+1)) | ||
|
r897 | |||
|
r534 | dataOut.data = dataOut.data[:,minIndex:maxIndex+1,:] | ||
|
r897 | |||
|
r596 | if rangeList != None: | ||
|
r897 | |||
|
r749 | profileList = [] | ||
|
r897 | |||
|
r749 | for thisRange in rangeList: | ||
minIndex = thisRange[0] | ||||
maxIndex = thisRange[1] | ||||
|
r897 | |||
|
r1167 | profileList.extend(list(range(minIndex, maxIndex+1))) | ||
|
r897 | |||
|
r749 | dataOut.data = dataOut.data[:,profileList,:] | ||
|
r897 | |||
|
r749 | dataOut.nProfiles = len(profileList) | ||
dataOut.profileIndex = dataOut.nProfiles - 1 | ||||
|
r495 | dataOut.flagNoData = False | ||
|
r897 | |||
|
r530 | return True | ||
|
r897 | |||
|
r660 | """ | ||
data dimension = [nChannels, nHeis] | ||||
""" | ||||
|
r897 | |||
|
r660 | if profileList != None: | ||
|
r897 | |||
|
r660 | if self.isThisProfileInList(dataOut.profileIndex, profileList): | ||
|
r897 | |||
|
r749 | self.nProfiles = len(profileList) | ||
dataOut.nProfiles = self.nProfiles | ||||
|
r660 | dataOut.profileIndex = self.profileIndex | ||
|
r749 | dataOut.flagNoData = False | ||
|
r897 | |||
|
r754 | self.incProfileIndex() | ||
|
r660 | return True | ||
|
r897 | |||
|
r660 | if profileRangeList != None: | ||
|
r897 | |||
|
r660 | minIndex = profileRangeList[0] | ||
maxIndex = profileRangeList[1] | ||||
|
r897 | |||
|
r660 | if self.isThisProfileInRange(dataOut.profileIndex, minIndex, maxIndex): | ||
|
r897 | |||
|
r749 | self.nProfiles = maxIndex - minIndex + 1 | ||
dataOut.nProfiles = self.nProfiles | ||||
|
r660 | dataOut.profileIndex = self.profileIndex | ||
|
r749 | dataOut.flagNoData = False | ||
|
r897 | |||
|
r754 | self.incProfileIndex() | ||
|
r660 | return True | ||
|
r897 | |||
|
r660 | if rangeList != None: | ||
|
r897 | |||
|
r660 | nProfiles = 0 | ||
|
r897 | |||
|
r660 | for thisRange in rangeList: | ||
minIndex = thisRange[0] | ||||
maxIndex = thisRange[1] | ||||
|
r897 | |||
|
r660 | nProfiles += maxIndex - minIndex + 1 | ||
|
r897 | |||
|
r660 | for thisRange in rangeList: | ||
|
r897 | |||
|
r660 | minIndex = thisRange[0] | ||
maxIndex = thisRange[1] | ||||
|
r897 | |||
|
r660 | if self.isThisProfileInRange(dataOut.profileIndex, minIndex, maxIndex): | ||
|
r897 | |||
|
r749 | self.nProfiles = nProfiles | ||
dataOut.nProfiles = self.nProfiles | ||||
|
r534 | dataOut.profileIndex = self.profileIndex | ||
|
r749 | dataOut.flagNoData = False | ||
|
r897 | |||
|
r754 | self.incProfileIndex() | ||
|
r897 | |||
|
r660 | break | ||
|
r897 | |||
|
r660 | return True | ||
|
r897 | |||
|
r660 | if beam != None: #beam is only for AMISR data | ||
if self.isThisProfileInList(dataOut.profileIndex, dataOut.beamRangeDict[beam]): | ||||
dataOut.flagNoData = False | ||||
dataOut.profileIndex = self.profileIndex | ||||
|
r897 | |||
|
r754 | self.incProfileIndex() | ||
|
r897 | |||
|
r660 | return True | ||
|
r897 | |||
|
r1167 | raise ValueError("ProfileSelector needs profileList, profileRangeList or rangeList parameter") | ||
|
r897 | |||
|
r1173 | #return False | ||
return dataOut | ||||
@MPDecorator | ||||
|
r495 | class Reshaper(Operation): | ||
|
r897 | |||
|
r1173 | def __init__(self):#, **kwargs): | ||
|
r897 | |||
|
r1173 | Operation.__init__(self)#, **kwargs) | ||
|
r897 | |||
|
r749 | self.__buffer = None | ||
self.__nitems = 0 | ||||
|
r897 | |||
|
r749 | def __appendProfile(self, dataOut, nTxs): | ||
|
r897 | |||
|
r749 | if self.__buffer is None: | ||
shape = (dataOut.nChannels, int(dataOut.nHeights/nTxs) ) | ||||
self.__buffer = numpy.empty(shape, dtype = dataOut.data.dtype) | ||||
|
r897 | |||
|
r749 | ini = dataOut.nHeights * self.__nitems | ||
end = ini + dataOut.nHeights | ||||
|
r897 | |||
|
r749 | self.__buffer[:, ini:end] = dataOut.data | ||
|
r897 | |||
|
r749 | self.__nitems += 1 | ||
|
r897 | |||
|
r749 | return int(self.__nitems*nTxs) | ||
|
r897 | |||
|
r749 | def __getBuffer(self): | ||
|
r897 | |||
|
r749 | if self.__nitems == int(1./self.__nTxs): | ||
|
r897 | |||
|
r749 | self.__nitems = 0 | ||
|
r897 | |||
|
r749 | return self.__buffer.copy() | ||
|
r897 | |||
|
r749 | return None | ||
|
r897 | |||
|
r749 | def __checkInputs(self, dataOut, shape, nTxs): | ||
|
r897 | |||
|
r749 | if shape is None and nTxs is None: | ||
|
r1167 | raise ValueError("Reshaper: shape of factor should be defined") | ||
|
r897 | |||
|
r749 | if nTxs: | ||
if nTxs < 0: | ||||
|
r1167 | raise ValueError("nTxs should be greater than 0") | ||
|
r897 | |||
|
r749 | if nTxs < 1 and dataOut.nProfiles % (1./nTxs) != 0: | ||
|
r1167 | raise ValueError("nProfiles= %d is not divisibled by (1./nTxs) = %f" %(dataOut.nProfiles, (1./nTxs))) | ||
|
r897 | |||
|
r749 | shape = [dataOut.nChannels, dataOut.nProfiles*nTxs, dataOut.nHeights/nTxs] | ||
|
r897 | |||
|
r790 | return shape, nTxs | ||
|
r897 | |||
|
r749 | if len(shape) != 2 and len(shape) != 3: | ||
|
r1167 | raise ValueError("shape dimension should be equal to 2 or 3. shape = (nProfiles, nHeis) or (nChannels, nProfiles, nHeis). Actually shape = (%d, %d, %d)" %(dataOut.nChannels, dataOut.nProfiles, dataOut.nHeights)) | ||
|
r897 | |||
|
r749 | if len(shape) == 2: | ||
shape_tuple = [dataOut.nChannels] | ||||
shape_tuple.extend(shape) | ||||
else: | ||||
shape_tuple = list(shape) | ||||
|
r897 | |||
|
r790 | nTxs = 1.0*shape_tuple[1]/dataOut.nProfiles | ||
|
r897 | |||
|
r749 | return shape_tuple, nTxs | ||
|
r897 | |||
|
r749 | def run(self, dataOut, shape=None, nTxs=None): | ||
|
r897 | |||
|
r749 | shape_tuple, self.__nTxs = self.__checkInputs(dataOut, shape, nTxs) | ||
|
r897 | |||
|
r749 | dataOut.flagNoData = True | ||
profileIndex = None | ||||
|
r897 | |||
|
r749 | if dataOut.flagDataAsBlock: | ||
|
r897 | |||
|
r749 | dataOut.data = numpy.reshape(dataOut.data, shape_tuple) | ||
dataOut.flagNoData = False | ||||
|
r897 | |||
|
r790 | profileIndex = int(dataOut.nProfiles*self.__nTxs) - 1 | ||
|
r897 | |||
|
r749 | else: | ||
|
r897 | |||
|
r749 | if self.__nTxs < 1: | ||
|
r897 | |||
|
r749 | self.__appendProfile(dataOut, self.__nTxs) | ||
new_data = self.__getBuffer() | ||||
|
r897 | |||
|
r749 | if new_data is not None: | ||
dataOut.data = new_data | ||||
dataOut.flagNoData = False | ||||
|
r897 | |||
|
r749 | profileIndex = dataOut.profileIndex*nTxs | ||
|
r897 | |||
|
r749 | else: | ||
|
r1167 | raise ValueError("nTxs should be greater than 0 and lower than 1, or use VoltageReader(..., getblock=True)") | ||
|
r897 | |||
|
r749 | deltaHeight = dataOut.heightList[1] - dataOut.heightList[0] | ||
|
r897 | |||
|
r749 | dataOut.heightList = numpy.arange(dataOut.nHeights/self.__nTxs) * deltaHeight + dataOut.heightList[0] | ||
|
r897 | |||
|
r749 | dataOut.nProfiles = int(dataOut.nProfiles*self.__nTxs) | ||
|
r897 | |||
|
r749 | dataOut.profileIndex = profileIndex | ||
|
r897 | |||
|
r749 | dataOut.ippSeconds /= self.__nTxs | ||
|
r897 | |||
|
r1173 | return dataOut | ||
@MPDecorator | ||||
|
r823 | class SplitProfiles(Operation): | ||
|
r897 | |||
|
r1173 | def __init__(self):#, **kwargs): | ||
|
r897 | |||
|
r1173 | Operation.__init__(self)#, **kwargs) | ||
|
r897 | |||
|
r823 | def run(self, dataOut, n): | ||
|
r897 | |||
|
r823 | dataOut.flagNoData = True | ||
profileIndex = None | ||||
|
r897 | |||
|
r823 | if dataOut.flagDataAsBlock: | ||
|
r897 | |||
|
r823 | #nchannels, nprofiles, nsamples | ||
shape = dataOut.data.shape | ||||
|
r897 | |||
|
r823 | if shape[2] % n != 0: | ||
|
r1167 | raise ValueError("Could not split the data, n=%d has to be multiple of %d" %(n, shape[2])) | ||
|
r1173 | |||
|
r1168 | new_shape = shape[0], shape[1]*n, int(shape[2]/n) | ||
|
r1173 | |||
|
r823 | dataOut.data = numpy.reshape(dataOut.data, new_shape) | ||
dataOut.flagNoData = False | ||||
|
r897 | |||
|
r823 | profileIndex = int(dataOut.nProfiles/n) - 1 | ||
|
r897 | |||
|
r823 | else: | ||
|
r897 | |||
|
r1167 | raise ValueError("Could not split the data when is read Profile by Profile. Use VoltageReader(..., getblock=True)") | ||
|
r897 | |||
|
r823 | deltaHeight = dataOut.heightList[1] - dataOut.heightList[0] | ||
|
r897 | |||
|
r823 | dataOut.heightList = numpy.arange(dataOut.nHeights/n) * deltaHeight + dataOut.heightList[0] | ||
|
r897 | |||
|
r823 | dataOut.nProfiles = int(dataOut.nProfiles*n) | ||
|
r897 | |||
|
r823 | dataOut.profileIndex = profileIndex | ||
|
r897 | |||
|
r823 | dataOut.ippSeconds /= n | ||
|
r897 | |||
|
r1173 | return dataOut | ||
@MPDecorator | ||||
|
r823 | class CombineProfiles(Operation): | ||
|
r1173 | def __init__(self):#, **kwargs): | ||
|
r897 | |||
|
r1173 | Operation.__init__(self)#, **kwargs) | ||
|
r897 | |||
|
r823 | self.__remData = None | ||
self.__profileIndex = 0 | ||||
def run(self, dataOut, n): | ||||
|
r897 | |||
|
r823 | dataOut.flagNoData = True | ||
profileIndex = None | ||||
|
r897 | |||
|
r823 | if dataOut.flagDataAsBlock: | ||
|
r897 | |||
|
r823 | #nchannels, nprofiles, nsamples | ||
shape = dataOut.data.shape | ||||
new_shape = shape[0], shape[1]/n, shape[2]*n | ||||
|
r897 | |||
|
r823 | if shape[1] % n != 0: | ||
|
r1167 | raise ValueError("Could not split the data, n=%d has to be multiple of %d" %(n, shape[1])) | ||
|
r897 | |||
|
r823 | dataOut.data = numpy.reshape(dataOut.data, new_shape) | ||
dataOut.flagNoData = False | ||||
|
r897 | |||
|
r823 | profileIndex = int(dataOut.nProfiles*n) - 1 | ||
|
r897 | |||
|
r823 | else: | ||
|
r897 | |||
|
r823 | #nchannels, nsamples | ||
if self.__remData is None: | ||||
newData = dataOut.data | ||||
else: | ||||
newData = numpy.concatenate((self.__remData, dataOut.data), axis=1) | ||||
|
r897 | |||
|
r823 | self.__profileIndex += 1 | ||
|
r897 | |||
|
r823 | if self.__profileIndex < n: | ||
self.__remData = newData | ||||
#continue | ||||
return | ||||
|
r897 | |||
|
r823 | self.__profileIndex = 0 | ||
self.__remData = None | ||||
|
r897 | |||
|
r823 | dataOut.data = newData | ||
dataOut.flagNoData = False | ||||
|
r897 | |||
|
r823 | profileIndex = dataOut.profileIndex/n | ||
|
r897 | |||
|
r823 | deltaHeight = dataOut.heightList[1] - dataOut.heightList[0] | ||
|
r897 | |||
|
r823 | dataOut.heightList = numpy.arange(dataOut.nHeights*n) * deltaHeight + dataOut.heightList[0] | ||
|
r897 | |||
|
r823 | dataOut.nProfiles = int(dataOut.nProfiles/n) | ||
|
r897 | |||
|
r823 | dataOut.profileIndex = profileIndex | ||
|
r897 | |||
|
r823 | dataOut.ippSeconds *= n | ||
|
r836 | |||
|
r1173 | return dataOut | ||
|
r624 | # import collections | ||
# from scipy.stats import mode | ||||
|
r897 | # | ||
|
r624 | # class Synchronize(Operation): | ||
|
r897 | # | ||
|
r624 | # isConfig = False | ||
# __profIndex = 0 | ||||
|
r897 | # | ||
# def __init__(self, **kwargs): | ||||
# | ||||
# Operation.__init__(self, **kwargs) | ||||
|
r624 | # # self.isConfig = False | ||
# self.__powBuffer = None | ||||
# self.__startIndex = 0 | ||||
# self.__pulseFound = False | ||||
|
r897 | # | ||
|
r624 | # def __findTxPulse(self, dataOut, channel=0, pulse_with = None): | ||
|
r897 | # | ||
|
r624 | # #Read data | ||
|
r897 | # | ||
|
r624 | # powerdB = dataOut.getPower(channel = channel) | ||
# noisedB = dataOut.getNoise(channel = channel)[0] | ||||
|
r897 | # | ||
|
r624 | # self.__powBuffer.extend(powerdB.flatten()) | ||
|
r897 | # | ||
|
r624 | # dataArray = numpy.array(self.__powBuffer) | ||
|
r897 | # | ||
|
r624 | # filteredPower = numpy.correlate(dataArray, dataArray[0:self.__nSamples], "same") | ||
|
r897 | # | ||
|
r624 | # maxValue = numpy.nanmax(filteredPower) | ||
|
r897 | # | ||
|
r624 | # if maxValue < noisedB + 10: | ||
# #No se encuentra ningun pulso de transmision | ||||
# return None | ||||
|
r897 | # | ||
|
r624 | # maxValuesIndex = numpy.where(filteredPower > maxValue - 0.1*abs(maxValue))[0] | ||
|
r897 | # | ||
|
r624 | # if len(maxValuesIndex) < 2: | ||
# #Solo se encontro un solo pulso de transmision de un baudio, esperando por el siguiente TX | ||||
# return None | ||||
|
r897 | # | ||
|
r624 | # phasedMaxValuesIndex = maxValuesIndex - self.__nSamples | ||
|
r897 | # | ||
|
r624 | # #Seleccionar solo valores con un espaciamiento de nSamples | ||
# pulseIndex = numpy.intersect1d(maxValuesIndex, phasedMaxValuesIndex) | ||||
|
r897 | # | ||
|
r624 | # if len(pulseIndex) < 2: | ||
# #Solo se encontro un pulso de transmision con ancho mayor a 1 | ||||
# return None | ||||
|
r897 | # | ||
|
r624 | # spacing = pulseIndex[1:] - pulseIndex[:-1] | ||
|
r897 | # | ||
|
r624 | # #remover senales que se distancien menos de 10 unidades o muestras | ||
# #(No deberian existir IPP menor a 10 unidades) | ||||
|
r897 | # | ||
|
r624 | # realIndex = numpy.where(spacing > 10 )[0] | ||
|
r897 | # | ||
|
r624 | # if len(realIndex) < 2: | ||
# #Solo se encontro un pulso de transmision con ancho mayor a 1 | ||||
# return None | ||||
|
r897 | # | ||
|
r624 | # #Eliminar pulsos anchos (deja solo la diferencia entre IPPs) | ||
# realPulseIndex = pulseIndex[realIndex] | ||||
|
r897 | # | ||
|
r624 | # period = mode(realPulseIndex[1:] - realPulseIndex[:-1])[0][0] | ||
|
r897 | # | ||
|
r624 | # print "IPP = %d samples" %period | ||
|
r897 | # | ||
|
r624 | # self.__newNSamples = dataOut.nHeights #int(period) | ||
# self.__startIndex = int(realPulseIndex[0]) | ||||
|
r897 | # | ||
|
r624 | # return 1 | ||
|
r897 | # | ||
# | ||||
|
r624 | # def setup(self, nSamples, nChannels, buffer_size = 4): | ||
|
r897 | # | ||
|
r624 | # self.__powBuffer = collections.deque(numpy.zeros( buffer_size*nSamples,dtype=numpy.float), | ||
# maxlen = buffer_size*nSamples) | ||||
|
r897 | # | ||
|
r624 | # bufferList = [] | ||
|
r897 | # | ||
|
r624 | # for i in range(nChannels): | ||
# bufferByChannel = collections.deque(numpy.zeros( buffer_size*nSamples, dtype=numpy.complex) + numpy.NAN, | ||||
# maxlen = buffer_size*nSamples) | ||||
|
r897 | # | ||
|
r624 | # bufferList.append(bufferByChannel) | ||
|
r897 | # | ||
|
r624 | # self.__nSamples = nSamples | ||
# self.__nChannels = nChannels | ||||
# self.__bufferList = bufferList | ||||
|
r897 | # | ||
|
r624 | # def run(self, dataOut, channel = 0): | ||
|
r897 | # | ||
|
r624 | # if not self.isConfig: | ||
# nSamples = dataOut.nHeights | ||||
# nChannels = dataOut.nChannels | ||||
# self.setup(nSamples, nChannels) | ||||
# self.isConfig = True | ||||
|
r897 | # | ||
|
r624 | # #Append new data to internal buffer | ||
# for thisChannel in range(self.__nChannels): | ||||
# bufferByChannel = self.__bufferList[thisChannel] | ||||
# bufferByChannel.extend(dataOut.data[thisChannel]) | ||||
|
r897 | # | ||
|
r624 | # if self.__pulseFound: | ||
# self.__startIndex -= self.__nSamples | ||||
|
r897 | # | ||
|
r624 | # #Finding Tx Pulse | ||
# if not self.__pulseFound: | ||||
# indexFound = self.__findTxPulse(dataOut, channel) | ||||
|
r897 | # | ||
|
r624 | # if indexFound == None: | ||
# dataOut.flagNoData = True | ||||
# return | ||||
|
r897 | # | ||
|
r624 | # self.__arrayBuffer = numpy.zeros((self.__nChannels, self.__newNSamples), dtype = numpy.complex) | ||
# self.__pulseFound = True | ||||
# self.__startIndex = indexFound | ||||
|
r897 | # | ||
|
r624 | # #If pulse was found ... | ||
# for thisChannel in range(self.__nChannels): | ||||
# bufferByChannel = self.__bufferList[thisChannel] | ||||
|
r897 | # #print self.__startIndex | ||
|
r624 | # x = numpy.array(bufferByChannel) | ||
# self.__arrayBuffer[thisChannel] = x[self.__startIndex:self.__startIndex+self.__newNSamples] | ||||
|
r897 | # | ||
|
r624 | # deltaHeight = dataOut.heightList[1] - dataOut.heightList[0] | ||
# dataOut.heightList = numpy.arange(self.__newNSamples)*deltaHeight | ||||
# # dataOut.ippSeconds = (self.__newNSamples / deltaHeight)/1e6 | ||||
|
r897 | # | ||
|
r624 | # dataOut.data = self.__arrayBuffer | ||
|
r897 | # | ||
|
r624 | # self.__startIndex += self.__newNSamples | ||
|
r897 | # | ||
|
r1173 | # return | ||