jroproc_voltage.py
1624 lines
| 50.7 KiB
| text/x-python
|
PythonLexer
|
r723 | import sys | |
r1296 | import numpy,math | ||
|
r837 | from scipy import interpolate | |
|
r1178 | from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation, MPDecorator | |
r1314 | from schainpy.model.data.jrodata import Voltage,hildebrand_sekhon | ||
|
r1168 | from schainpy.utils import log | |
|
r1173 | from time import time | |
|
r1675 | # voltage proc master | |
|
r1287 | ||
r1296 | class VoltageProc(ProcessingUnit): | ||
|
r1177 | def __init__(self): | |
|
r897 | ||
|
r1177 | ProcessingUnit.__init__(self) | |
|
r897 | ||
|
r487 | self.dataOut = Voltage() | |
self.flip = 1 | |||
|
r1177 | self.setupReq = False | |
|
r487 | ||
|
r1675 | def run(self, runNextUnit = 0): | |
|
r1173 | ||
|
r491 | if self.dataIn.type == 'AMISR': | |
self.__updateObjFromAmisrInput() | |||
|
r897 | ||
|
r491 | if self.dataIn.type == 'Voltage': | |
self.dataOut.copy(self.dataIn) | |||
|
r1675 | self.dataOut.runNextUnit = runNextUnit | |
|
r897 | ||
|
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 | |||
|
r1179 | #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 | |||
r1296 | |||
|
r1287 | ||
class selectChannels(Operation): | |||
r1296 | |||
|
r1287 | def run(self, dataOut, channelList): | |
|
r897 | ||
|
r487 | channelIndexList = [] | |
|
r1287 | self.dataOut = dataOut | |
|
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) | |||
self.selectChannelsByIndex(channelIndexList) | |||
|
r1287 | return self.dataOut | |
|
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 | raise ValueError("The value %d in channelIndexList is not valid" %channelIndex) | |
|
r897 | ||
|
r1287 | if self.dataOut.type == 'Voltage': | |
if self.dataOut.flagDataAsBlock: | |||
""" | |||
Si la data es obtenida por bloques, dimension = [nChannels, nProfiles, nHeis] | |||
""" | |||
data = self.dataOut.data[channelIndexList,:,:] | |||
else: | |||
data = self.dataOut.data[channelIndexList,:] | |||
self.dataOut.data = data | |||
# self.dataOut.channelList = [self.dataOut.channelList[i] for i in channelIndexList] | |||
self.dataOut.channelList = range(len(channelIndexList)) | |||
r1296 | |||
|
r1287 | elif self.dataOut.type == 'Spectra': | |
data_spc = self.dataOut.data_spc[channelIndexList, :] | |||
data_dc = self.dataOut.data_dc[channelIndexList, :] | |||
self.dataOut.data_spc = data_spc | |||
self.dataOut.data_dc = data_dc | |||
# self.dataOut.channelList = [self.dataOut.channelList[i] for i in channelIndexList] | |||
self.dataOut.channelList = range(len(channelIndexList)) | |||
self.__selectPairsByChannel(channelIndexList) | |||
|
r897 | ||
|
r487 | return 1 | |
|
r897 | ||
|
r1287 | def __selectPairsByChannel(self, channelList=None): | |
if channelList == None: | |||
return | |||
pairsIndexListSelected = [] | |||
for pairIndex in self.dataOut.pairsIndexList: | |||
# First pair | |||
if self.dataOut.pairsList[pairIndex][0] not in channelList: | |||
continue | |||
# Second pair | |||
if self.dataOut.pairsList[pairIndex][1] not in channelList: | |||
continue | |||
pairsIndexListSelected.append(pairIndex) | |||
if not pairsIndexListSelected: | |||
self.dataOut.data_cspc = None | |||
self.dataOut.pairsList = [] | |||
return | |||
self.dataOut.data_cspc = self.dataOut.data_cspc[pairsIndexListSelected] | |||
self.dataOut.pairsList = [self.dataOut.pairsList[i] | |||
for i in pairsIndexListSelected] | |||
return | |||
class selectHeights(Operation): | |||
r1296 | |||
r1344 | def run(self, dataOut, minHei=None, maxHei=None, minIndex=None, maxIndex=None): | ||
|
r487 | """ | |
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 | ||
|
r1287 | self.dataOut = dataOut | |
|
r1675 | if type(minHei) == int or type(minHei) == float: | |
v_minHei= True | |||
else: | |||
v_minHei= False | |||
|
r897 | ||
|
r1675 | if v_minHei and maxHei: | |
r1344 | if (minHei < self.dataOut.heightList[0]): | ||
minHei = self.dataOut.heightList[0] | |||
|
r897 | ||
r1344 | if (maxHei > self.dataOut.heightList[-1]): | ||
maxHei = self.dataOut.heightList[-1] | |||
|
r684 | ||
r1344 | minIndex = 0 | ||
maxIndex = 0 | |||
heights = self.dataOut.heightList | |||
inda = numpy.where(heights >= minHei) | |||
indb = numpy.where(heights <= maxHei) | |||
|
r897 | ||
r1344 | try: | ||
minIndex = inda[0][0] | |||
except: | |||
minIndex = 0 | |||
|
r897 | ||
r1344 | try: | ||
maxIndex = indb[0][-1] | |||
except: | |||
maxIndex = len(heights) | |||
|
r487 | self.selectHeightsByIndex(minIndex, maxIndex) | |
|
r897 | ||
|
r1287 | return self.dataOut | |
|
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 | ||
|
r1287 | if self.dataOut.type == 'Voltage': | |
if (minIndex < 0) or (minIndex > maxIndex): | |||
raise ValueError("Height index range (%d,%d) is not valid" % (minIndex, maxIndex)) | |||
|
r897 | ||
|
r1287 | if (maxIndex >= self.dataOut.nHeights): | |
maxIndex = self.dataOut.nHeights | |||
|
r897 | ||
|
r1287 | #voltage | |
if self.dataOut.flagDataAsBlock: | |||
""" | |||
Si la data es obtenida por bloques, dimension = [nChannels, nProfiles, nHeis] | |||
""" | |||
data = self.dataOut.data[:,:, minIndex:maxIndex] | |||
else: | |||
data = self.dataOut.data[:, minIndex:maxIndex] | |||
# firstHeight = self.dataOut.heightList[minIndex] | |||
self.dataOut.data = data | |||
self.dataOut.heightList = self.dataOut.heightList[minIndex:maxIndex] | |||
if self.dataOut.nHeights <= 1: | |||
raise ValueError("selectHeights: Too few heights. Current number of heights is %d" %(self.dataOut.nHeights)) | |||
elif self.dataOut.type == 'Spectra': | |||
if (minIndex < 0) or (minIndex > maxIndex): | |||
raise ValueError("Error selecting heights: Index range (%d,%d) is not valid" % ( | |||
minIndex, maxIndex)) | |||
|
r487 | ||
|
r1287 | if (maxIndex >= self.dataOut.nHeights): | |
maxIndex = self.dataOut.nHeights - 1 | |||
|
r487 | ||
|
r1287 | # Spectra | |
data_spc = self.dataOut.data_spc[:, :, minIndex:maxIndex + 1] | |||
|
r897 | ||
|
r1287 | data_cspc = None | |
if self.dataOut.data_cspc is not None: | |||
data_cspc = self.dataOut.data_cspc[:, :, minIndex:maxIndex + 1] | |||
|
r897 | ||
|
r1287 | data_dc = None | |
if self.dataOut.data_dc is not None: | |||
data_dc = self.dataOut.data_dc[:, minIndex:maxIndex + 1] | |||
self.dataOut.data_spc = data_spc | |||
self.dataOut.data_cspc = data_cspc | |||
self.dataOut.data_dc = data_dc | |||
self.dataOut.heightList = self.dataOut.heightList[minIndex:maxIndex + 1] | |||
r1296 | |||
|
r487 | return 1 | |
|
r897 | ||
|
r1287 | class filterByHeights(Operation): | |
def run(self, dataOut, window): | |||
|
r897 | ||
|
r1287 | deltaHeight = dataOut.heightList[1] - dataOut.heightList[0] | |
|
r897 | ||
|
r487 | if window == None: | |
|
r1287 | window = (dataOut.radarControllerHeaderObj.txA/dataOut.radarControllerHeaderObj.nBaud) / deltaHeight | |
|
r897 | ||
|
r487 | newdelta = deltaHeight * window | |
|
r1287 | r = dataOut.nHeights % window | |
newheights = (dataOut.nHeights-r)/window | |||
|
r897 | ||
|
r596 | if newheights <= 1: | |
|
r1287 | raise ValueError("filterByHeights: Too few heights. Current number of heights is %d and window is %d" %(dataOut.nHeights, window)) | |
|
r897 | ||
|
r1287 | if dataOut.flagDataAsBlock: | |
|
r530 | """ | |
Si la data es obtenida por bloques, dimension = [nChannels, nProfiles, nHeis] | |||
""" | |||
r1296 | buffer = dataOut.data[:, :, 0:int(dataOut.nHeights-r)] | ||
|
r1287 | buffer = buffer.reshape(dataOut.nChannels, dataOut.nProfiles, int(dataOut.nHeights/window), window) | |
|
r530 | buffer = numpy.sum(buffer,3) | |
|
r897 | ||
|
r530 | else: | |
|
r1287 | buffer = dataOut.data[:,0:int(dataOut.nHeights-r)] | |
buffer = buffer.reshape(dataOut.nChannels,int(dataOut.nHeights/window),int(window)) | |||
|
r530 | buffer = numpy.sum(buffer,2) | |
|
r1287 | dataOut.data = buffer | |
dataOut.heightList = dataOut.heightList[0] + numpy.arange( newheights )*newdelta | |||
dataOut.windowOfFilter = window | |||
return dataOut | |||
|
r568 | ||
|
r1287 | class setH0(Operation): | |
def run(self, dataOut, h0, deltaHeight = None): | |||
|
r897 | ||
|
r568 | if not deltaHeight: | |
|
r1287 | deltaHeight = dataOut.heightList[1] - dataOut.heightList[0] | |
|
r897 | ||
|
r1287 | nHeights = dataOut.nHeights | |
|
r897 | ||
|
r568 | newHeiRange = h0 + numpy.arange(nHeights)*deltaHeight | |
|
r897 | ||
|
r1287 | dataOut.heightList = newHeiRange | |
return dataOut | |||
|
r897 | ||
|
r1287 | class deFlip(Operation): | |
|
r897 | ||
|
r1287 | def run(self, dataOut, channelList = []): | |
|
r897 | ||
|
r1287 | data = dataOut.data.copy() | |
if dataOut.flagDataAsBlock: | |||
|
r530 | flip = self.flip | |
|
r1287 | profileList = list(range(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: | |||
|
r1287 | if thisChannel not in dataOut.channelList: | |
|
r586 | 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: | |||
|
r1287 | if thisChannel not in dataOut.channelList: | |
|
r586 | continue | |
|
r897 | ||
|
r530 | data[thisChannel,:] = data[thisChannel,:]*self.flip | |
|
r897 | ||
|
r530 | self.flip *= -1. | |
|
r897 | ||
|
r1287 | dataOut.data = data | |
|
r897 | ||
|
r1287 | return dataOut | |
|
r897 | ||
|
r1287 | class setAttribute(Operation): | |
''' | |||
|
r1288 | Set an arbitrary attribute(s) to dataOut | |
|
r1287 | ''' | |
def __init__(self): | |||
Operation.__init__(self) | |||
self._ready = False | |||
def run(self, dataOut, **kwargs): | |||
|
r897 | ||
|
r1287 | for key, value in kwargs.items(): | |
setattr(dataOut, key, value) | |||
return dataOut | |||
|
r1308 | @MPDecorator | |
class printAttribute(Operation): | |||
''' | |||
Print an arbitrary attribute of dataOut | |||
''' | |||
def __init__(self): | |||
Operation.__init__(self) | |||
def run(self, dataOut, attributes): | |||
r1338 | if isinstance(attributes, str): | ||
attributes = [attributes] | |||
|
r1308 | for attr in attributes: | |
if hasattr(dataOut, attr): | |||
log.log(getattr(dataOut, attr), attr) | |||
|
r1287 | class interpolateHeights(Operation): | |
def run(self, dataOut, topLim, botLim): | |||
|
r836 | #69 al 72 para julia | |
#82-84 para meteoros | |||
|
r1287 | if len(numpy.shape(dataOut.data))==2: | |
sampInterp = (dataOut.data[:,botLim-1] + dataOut.data[:,topLim+1])/2 | |||
|
r836 | sampInterp = numpy.transpose(numpy.tile(sampInterp,(topLim-botLim + 1,1))) | |
|
r1287 | #dataOut.data[:,botLim:limSup+1] = sampInterp | |
dataOut.data[:,botLim:topLim+1] = sampInterp | |||
|
r836 | else: | |
|
r1287 | nHeights = dataOut.data.shape[2] | |
|
r837 | x = numpy.hstack((numpy.arange(botLim),numpy.arange(topLim+1,nHeights))) | |
|
r1287 | y = 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) | |||
|
r1287 | dataOut.data[:,:,botLim:topLim+1] = ynew | |
|
r897 | ||
|
r1287 | return dataOut | |
|
r837 | ||
|
r1177 | ||
|
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 | ||
|
r1179 | def __init__(self, **kwargs): | |
|
r897 | ||
|
r1179 | Operation.__init__(self, **kwargs) | |
|
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] | |
r1296 | |||
|
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 | |
r1296 | |||
|
r1116 | 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 | |||
r1296 | |||
|
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 | |
|
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: | |
r1296 | if stride is None: | ||
|
r1116 | avgdata, avgdatatime = self.integrate(dataOut.data, dataOut.utctime) | |
else: | |||
avgdata, avgdatatime = self.integrateByStride(dataOut.data, dataOut.utctime) | |||
|
r897 | ||
r1296 | |||
|
r1116 | # dataOut.timeInterval *= n | |
|
r487 | dataOut.flagNoData = True | |
|
r897 | ||
|
r487 | if self.__dataReady: | |
dataOut.data = avgdata | |||
|
r1310 | if not dataOut.flagCohInt: | |
dataOut.nCohInt *= self.n | |||
dataOut.flagCohInt = True | |||
|
r487 | dataOut.utctime = avgdatatime | |
|
r1116 | # print avgdata, avgdatatime | |
# raise | |||
# dataOut.timeInterval = dataOut.ippSeconds * dataOut.nCohInt | |||
|
r487 | dataOut.flagNoData = False | |
|
r1173 | return dataOut | |
|
r1177 | ||
|
r487 | class Decoder(Operation): | |
|
r897 | ||
|
r487 | isConfig = False | |
__profIndex = 0 | |||
|
r897 | ||
|
r487 | code = None | |
|
r897 | ||
nCode = None | |||
|
r487 | nBaud = None | |
|
r897 | ||
|
r1179 | def __init__(self, **kwargs): | |
|
r897 | ||
|
r1179 | Operation.__init__(self, **kwargs) | |
|
r897 | ||
|
r495 | self.times = None | |
self.osamp = None | |||
|
r1004 | # self.__setValues = False | |
|
r1179 | self.isConfig = False | |
|
r1173 | 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 | ||
r1236 | repetitions = int(self.__nProfiles / self.nCode) | ||
|
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) | |
r1296 | |||
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 | ||
r1296 | |||
|
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 | |
r1296 | |||
|
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 | |
|
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 | |
|
r495 | ||
|
r1177 | ||
|
r495 | class ProfileConcat(Operation): | |
|
r897 | ||
|
r495 | isConfig = False | |
buffer = None | |||
|
r897 | ||
|
r1179 | def __init__(self, **kwargs): | |
|
r897 | ||
|
r1179 | 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): | |
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 | |
|
r1177 | ||
|
r495 | class ProfileSelector(Operation): | |
|
r897 | ||
|
r495 | profileIndex = None | |
# Tamanho total de los perfiles | |||
nProfiles = None | |||
|
r897 | ||
|
r1179 | def __init__(self, **kwargs): | |
|
r897 | ||
|
r1179 | 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 | ||
|
r1183 | return dataOut | |
|
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() | |
|
r1183 | return dataOut | |
|
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() | |
|
r1183 | return dataOut | |
|
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 | ||
|
r1183 | return dataOut | |
|
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 | ||
|
r1183 | return dataOut | |
|
r897 | ||
|
r1167 | raise ValueError("ProfileSelector needs profileList, profileRangeList or rangeList parameter") | |
|
r897 | ||
|
r1177 | ||
|
r495 | class Reshaper(Operation): | |
|
r897 | ||
|
r1179 | def __init__(self, **kwargs): | |
|
r897 | ||
|
r1179 | 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 | |
|
r1177 | ||
|
r823 | class SplitProfiles(Operation): | |
|
r897 | ||
|
r1179 | def __init__(self, **kwargs): | |
|
r897 | ||
|
r1179 | 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])) | |
r1296 | |||
|
r1168 | new_shape = shape[0], shape[1]*n, int(shape[2]/n) | |
r1296 | |||
|
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 | |
|
r1177 | ||
|
r823 | class CombineProfiles(Operation): | |
|
r1179 | def __init__(self, **kwargs): | |
|
r897 | ||
|
r1179 | 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 | |
r1296 | |||
class PulsePairVoltage(Operation): | |||
''' | |||
Function PulsePair(Signal Power, Velocity) | |||
The real component of Lag[0] provides Intensity Information | |||
The imag component of Lag[1] Phase provides Velocity Information | |||
Configuration Parameters: | |||
nPRF = Number of Several PRF | |||
theta = Degree Azimuth angel Boundaries | |||
Input: | |||
self.dataOut | |||
lag[N] | |||
Affected: | |||
self.dataOut.spc | |||
''' | |||
isConfig = False | |||
__profIndex = 0 | |||
__initime = None | |||
__lastdatatime = None | |||
__buffer = None | |||
r1303 | noise = None | ||
r1296 | __dataReady = False | ||
n = None | |||
__nch = 0 | |||
__nHeis = 0 | |||
removeDC = False | |||
ipp = None | |||
lambda_ = 0 | |||
def __init__(self,**kwargs): | |||
Operation.__init__(self,**kwargs) | |||
def setup(self, dataOut, n = None, removeDC=False): | |||
''' | |||
n= Numero de PRF's de entrada | |||
''' | |||
self.__initime = None | |||
self.__lastdatatime = 0 | |||
self.__dataReady = False | |||
self.__buffer = 0 | |||
self.__profIndex = 0 | |||
r1303 | self.noise = None | ||
r1296 | self.__nch = dataOut.nChannels | ||
self.__nHeis = dataOut.nHeights | |||
self.removeDC = removeDC | |||
self.lambda_ = 3.0e8/(9345.0e6) | |||
self.ippSec = dataOut.ippSeconds | |||
self.nCohInt = dataOut.nCohInt | |||
print("IPPseconds",dataOut.ippSeconds) | |||
print("ELVALOR DE n es:", n) | |||
if n == None: | |||
raise ValueError("n should be specified.") | |||
if n != None: | |||
if n<2: | |||
raise ValueError("n should be greater than 2") | |||
self.n = n | |||
self.__nProf = n | |||
self.__buffer = numpy.zeros((dataOut.nChannels, | |||
n, | |||
dataOut.nHeights), | |||
dtype='complex') | |||
def putData(self,data): | |||
''' | |||
Add a profile to he __buffer and increase in one the __profiel Index | |||
''' | |||
self.__buffer[:,self.__profIndex,:]= data | |||
self.__profIndex += 1 | |||
return | |||
r1307 | def pushData(self,dataOut): | ||
r1296 | ''' | ||
Return the PULSEPAIR and the profiles used in the operation | |||
Affected : self.__profileIndex | |||
''' | |||
r1338 | #----------------- Remove DC----------------------------------- | ||
r1296 | if self.removeDC==True: | ||
mean = numpy.mean(self.__buffer,1) | |||
tmp = mean.reshape(self.__nch,1,self.__nHeis) | |||
dc= numpy.tile(tmp,[1,self.__nProf,1]) | |||
self.__buffer = self.__buffer - dc | |||
r1338 | #------------------Calculo de Potencia ------------------------ | ||
r1314 | pair0 = self.__buffer*numpy.conj(self.__buffer) | ||
pair0 = pair0.real | |||
lag_0 = numpy.sum(pair0,1) | |||
r1338 | #------------------Calculo de Ruido x canal-------------------- | ||
r1314 | self.noise = numpy.zeros(self.__nch) | ||
for i in range(self.__nch): | |||
daux = numpy.sort(pair0[i,:,:],axis= None) | |||
self.noise[i]=hildebrand_sekhon( daux ,self.nCohInt) | |||
self.noise = self.noise.reshape(self.__nch,1) | |||
self.noise = numpy.tile(self.noise,[1,self.__nHeis]) | |||
noise_buffer = self.noise.reshape(self.__nch,1,self.__nHeis) | |||
noise_buffer = numpy.tile(noise_buffer,[1,self.__nProf,1]) | |||
r1338 | #------------------ Potencia recibida= P , Potencia senal = S , Ruido= N-- | ||
#------------------ P= S+N ,P=lag_0/N --------------------------------- | |||
#-------------------- Power -------------------------------------------------- | |||
r1314 | data_power = lag_0/(self.n*self.nCohInt) | ||
r1338 | #------------------ Senal --------------------------------------------------- | ||
r1314 | data_intensity = pair0 - noise_buffer | ||
data_intensity = numpy.sum(data_intensity,axis=1)*(self.n*self.nCohInt)#*self.nCohInt) | |||
#data_intensity = (lag_0-self.noise*self.n)*(self.n*self.nCohInt) | |||
for i in range(self.__nch): | |||
for j in range(self.__nHeis): | |||
if data_intensity[i][j] < 0: | |||
data_intensity[i][j] = numpy.min(numpy.absolute(data_intensity[i][j])) | |||
r1296 | |||
r1338 | #----------------- Calculo de Frecuencia y Velocidad doppler-------- | ||
r1303 | pair1 = self.__buffer[:,:-1,:]*numpy.conjugate(self.__buffer[:,1:,:]) | ||
lag_1 = numpy.sum(pair1,1) | |||
r1314 | data_freq = (-1/(2.0*math.pi*self.ippSec*self.nCohInt))*numpy.angle(lag_1) | ||
data_velocity = (self.lambda_/2.0)*data_freq | |||
r1303 | |||
r1338 | #---------------- Potencia promedio estimada de la Senal----------- | ||
r1314 | lag_0 = lag_0/self.n | ||
S = lag_0-self.noise | |||
r1307 | |||
r1338 | #---------------- Frecuencia Doppler promedio --------------------- | ||
r1303 | lag_1 = lag_1/(self.n-1) | ||
R1 = numpy.abs(lag_1) | |||
r1307 | |||
r1338 | #---------------- Calculo del SNR---------------------------------- | ||
r1307 | data_snrPP = S/self.noise | ||
r1314 | for i in range(self.__nch): | ||
for j in range(self.__nHeis): | |||
if data_snrPP[i][j] < 1.e-20: | |||
data_snrPP[i][j] = 1.e-20 | |||
r1307 | |||
r1338 | #----------------- Calculo del ancho espectral ---------------------- | ||
r1303 | L = S/R1 | ||
L = numpy.where(L<0,1,L) | |||
L = numpy.log(L) | |||
tmp = numpy.sqrt(numpy.absolute(L)) | |||
r1314 | data_specwidth = (self.lambda_/(2*math.sqrt(2)*math.pi*self.ippSec*self.nCohInt))*tmp*numpy.sign(L) | ||
r1296 | n = self.__profIndex | ||
self.__buffer = numpy.zeros((self.__nch, self.__nProf,self.__nHeis), dtype='complex') | |||
self.__profIndex = 0 | |||
r1314 | return data_power,data_intensity,data_velocity,data_snrPP,data_specwidth,n | ||
r1296 | |||
r1307 | def pulsePairbyProfiles(self,dataOut): | ||
r1296 | |||
self.__dataReady = False | |||
r1314 | data_power = None | ||
r1296 | data_intensity = None | ||
data_velocity = None | |||
r1303 | data_specwidth = None | ||
r1307 | data_snrPP = None | ||
self.putData(data=dataOut.data) | |||
r1296 | if self.__profIndex == self.n: | ||
r1314 | data_power,data_intensity, data_velocity,data_snrPP,data_specwidth, n = self.pushData(dataOut=dataOut) | ||
r1296 | self.__dataReady = True | ||
r1314 | return data_power, data_intensity, data_velocity, data_snrPP, data_specwidth | ||
r1296 | |||
r1307 | def pulsePairOp(self, dataOut, datatime= None): | ||
r1296 | |||
if self.__initime == None: | |||
self.__initime = datatime | |||
r1314 | data_power, data_intensity, data_velocity, data_snrPP, data_specwidth = self.pulsePairbyProfiles(dataOut) | ||
r1296 | self.__lastdatatime = datatime | ||
r1314 | if data_power is None: | ||
return None, None, None,None,None,None | |||
r1296 | |||
avgdatatime = self.__initime | |||
deltatime = datatime - self.__lastdatatime | |||
self.__initime = datatime | |||
r1314 | return data_power, data_intensity, data_velocity, data_snrPP, data_specwidth, avgdatatime | ||
r1296 | |||
def run(self, dataOut,n = None,removeDC= False, overlapping= False,**kwargs): | |||
if not self.isConfig: | |||
self.setup(dataOut = dataOut, n = n , removeDC=removeDC , **kwargs) | |||
self.isConfig = True | |||
r1314 | data_power, data_intensity, data_velocity,data_snrPP,data_specwidth, avgdatatime = self.pulsePairOp(dataOut, dataOut.utctime) | ||
r1296 | dataOut.flagNoData = True | ||
if self.__dataReady: | |||
dataOut.nCohInt *= self.n | |||
r1314 | dataOut.dataPP_POW = data_intensity # S | ||
dataOut.dataPP_POWER = data_power # P | |||
dataOut.dataPP_DOP = data_velocity | |||
dataOut.dataPP_SNR = data_snrPP | |||
dataOut.dataPP_WIDTH = data_specwidth | |||
r1296 | dataOut.PRFbyAngle = self.n #numero de PRF*cada angulo rotado que equivale a un tiempo. | ||
dataOut.utctime = avgdatatime | |||
dataOut.flagNoData = False | |||
return dataOut | |||
r1314 | |||
|
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 | # | |
|
r1675 | # return |