diff --git a/schainpy/model/data/jrodata.py b/schainpy/model/data/jrodata.py index 1b46264..a35d1bc 100644 --- a/schainpy/model/data/jrodata.py +++ b/schainpy/model/data/jrodata.py @@ -609,7 +609,9 @@ class Spectra(JROData): return range(self.nPairs) def getNormFactor(self): + pwcode = 1 + if self.flagDecodeData: pwcode = numpy.sum(self.code[0]**2) #normFactor = min(self.nFFTPoints,self.nProfiles)*self.nIncohInt*self.nCohInt*pwcode*self.windowOfFilter @@ -637,13 +639,19 @@ class Spectra(JROData): return timeInterval - nPairs = property(getNPairs, "I'm the 'nPairs' property.") - pairsIndexList = property(getPairsIndexList, "I'm the 'pairsIndexList' property.") - normFactor = property(getNormFactor, "I'm the 'getNormFactor' property.") - flag_cspc = property(getFlagCspc) - flag_dc = property(getFlagDc) - noise = property(getNoise, "I'm the 'nHeights' property.") - timeInterval = property(getTimeInterval, "I'm the 'timeInterval' property") + def setValue(self, value): + + print "This property should not be initialized" + + return + + nPairs = property(getNPairs, setValue, "I'm the 'nPairs' property.") + pairsIndexList = property(getPairsIndexList, setValue, "I'm the 'pairsIndexList' property.") + normFactor = property(getNormFactor, setValue, "I'm the 'getNormFactor' property.") + flag_cspc = property(getFlagCspc, setValue) + flag_dc = property(getFlagDc, setValue) + noise = property(getNoise, setValue, "I'm the 'nHeights' property.") + timeInterval = property(getTimeInterval, setValue, "I'm the 'timeInterval' property") class SpectraHeis(Spectra): diff --git a/schainpy/model/data/jroheaderIO.py b/schainpy/model/data/jroheaderIO.py index 68b7c46..4ccce96 100644 --- a/schainpy/model/data/jroheaderIO.py +++ b/schainpy/model/data/jroheaderIO.py @@ -182,8 +182,11 @@ class SystemHeader(Header): def read(self, fp): + startFp = fp.tell() + try: header = numpy.fromfile(fp,SYSTEM_STRUCTURE,1) + self.size = header['nSize'][0] self.nSamples = header['nNumSamples'][0] self.nProfiles = header['nNumProfiles'][0] @@ -195,6 +198,11 @@ class SystemHeader(Header): print "SystemHeader: " + e return 0 + endFp = self.size + startFp + + if fp.tell() != endFp: + raise IOError, "System Header is not consistent" + return 1 def write(self, fp): @@ -273,72 +281,63 @@ class RadarControllerHeader(Header): def read(self, fp): - try: - startFp = fp.tell() - header = numpy.fromfile(fp,RADAR_STRUCTURE,1) - - size = int(header['nSize'][0]) - self.expType = int(header['nExpType'][0]) - self.nTx = int(header['nNTx'][0]) - self.ipp = float(header['fIpp'][0]) - self.txA = float(header['fTxA'][0]) - self.txB = float(header['fTxB'][0]) - self.nWindows = int(header['nNumWindows'][0]) - self.numTaus = int(header['nNumTaus'][0]) - self.codeType = int(header['nCodeType'][0]) - self.line6Function = int(header['nLine6Function'][0]) - self.line5Function = int(header['nLine5Function'][0]) - self.fClock = float(header['fClock'][0]) - self.prePulseBefore = int(header['nPrePulseBefore'][0]) - self.prePulserAfter = int(header['nPrePulseAfter'][0]) - self.rangeIpp = header['sRangeIPP'][0] - self.rangeTxA = header['sRangeTxA'][0] - self.rangeTxB = header['sRangeTxB'][0] - # jump Dynamic Radar Controller Header -# jumpFp = size - 116 -# self.dynamic = numpy.fromfile(fp,numpy.dtype('byte'),jumpFp) - #pointer backward to dynamic header and read -# backFp = fp.tell() - jumpFp -# fp.seek(backFp) - - samplingWindow = numpy.fromfile(fp,SAMPLING_STRUCTURE,self.nWindows) - - self.nHeights = int(numpy.sum(samplingWindow['nsa'])) - self.firstHeight = samplingWindow['h0'] - self.deltaHeight = samplingWindow['dh'] - self.samplesWin = samplingWindow['nsa'] + + startFp = fp.tell() + header = numpy.fromfile(fp,RADAR_STRUCTURE,1) + + size = int(header['nSize'][0]) + self.expType = int(header['nExpType'][0]) + self.nTx = int(header['nNTx'][0]) + self.ipp = float(header['fIpp'][0]) + self.txA = float(header['fTxA'][0]) + self.txB = float(header['fTxB'][0]) + self.nWindows = int(header['nNumWindows'][0]) + self.numTaus = int(header['nNumTaus'][0]) + self.codeType = int(header['nCodeType'][0]) + self.line6Function = int(header['nLine6Function'][0]) + self.line5Function = int(header['nLine5Function'][0]) + self.fClock = float(header['fClock'][0]) + self.prePulseBefore = int(header['nPrePulseBefore'][0]) + self.prePulserAfter = int(header['nPrePulseAfter'][0]) + self.rangeIpp = header['sRangeIPP'][0] + self.rangeTxA = header['sRangeTxA'][0] + self.rangeTxB = header['sRangeTxB'][0] + + samplingWindow = numpy.fromfile(fp,SAMPLING_STRUCTURE,self.nWindows) + + self.nHeights = int(numpy.sum(samplingWindow['nsa'])) + self.firstHeight = samplingWindow['h0'] + self.deltaHeight = samplingWindow['dh'] + self.samplesWin = samplingWindow['nsa'] + + self.Taus = numpy.fromfile(fp,' 0: - fp.seek(jumpFp) - except Exception, e: - print "RadarControllerHeader: " + e - return 0 + endFp = size + startFp + if fp.tell() != endFp: + raise IOError, "Radar Controller Header is not consistent" + return 1 def write(self, fp): @@ -436,7 +435,7 @@ class RadarControllerHeader(Header): class ProcessingHeader(Header): - size = None +# size = None dtype = None blockSize = None profilesPerBlock = None @@ -452,7 +451,7 @@ class ProcessingHeader(Header): def __init__(self): - self.size = 0 +# self.size = 0 self.dtype = 0 self.blockSize = 0 self.profilesPerBlock = 0 @@ -468,17 +467,23 @@ class ProcessingHeader(Header): self.deltaHeight = 0 self.samplesWin = 0 self.spectraComb = 0 -# self.nCode = None -# self.code = None -# self.nBaud = None + self.nCode = None + self.code = None + self.nBaud = None + self.shif_fft = False self.flag_dc = False self.flag_cspc = False + self.flag_decode = False + self.flag_deflip = False def read(self, fp): -# try: + + startFp = fp.tell() + header = numpy.fromfile(fp,PROCESSING_STRUCTURE,1) - self.size = int(header['nSize'][0]) + + size = int(header['nSize'][0]) self.dtype = int(header['nDataType'][0]) self.blockSize = int(header['nSizeOfDataBlock'][0]) self.profilesPerBlock = int(header['nProfilesperBlock'][0]) @@ -495,21 +500,38 @@ class ProcessingHeader(Header): self.firstHeight = float(samplingWindow['h0'][0]) self.deltaHeight = float(samplingWindow['dh'][0]) self.samplesWin = samplingWindow['nsa'][0] + self.spectraComb = numpy.fromfile(fp,'u1',2*self.totalSpectra) -# if ((self.processFlags & PROCFLAG.DEFINE_PROCESS_CODE) == PROCFLAG.DEFINE_PROCESS_CODE): -# self.nCode = int(numpy.fromfile(fp,' 0: self.flag_cspc = True - -# except Exception, e: -# print "Error ProcessingHeader: " -# return 0 + + endFp = size + startFp + + if fp.tell() != endFp: + raise IOError, "Processing Header is not consistent" return 1 def write(self, fp): + #Clear DEFINE_PROCESS_CODE +# self.processFlags = self.processFlags & (~PROCFLAG.DEFINE_PROCESS_CODE) headerTuple = (self.size, self.dtype, @@ -551,26 +576,43 @@ class ProcessingHeader(Header): sampleWindowTuple = (self.firstHeight,self.deltaHeight,self.samplesWin) samplingWindow = numpy.array(sampleWindowTuple,SAMPLING_STRUCTURE) samplingWindow.tofile(fp) - if self.totalSpectra != 0: - spectraComb = numpy.array([],numpy.dtype('u1')) +# spectraComb = numpy.array([],numpy.dtype('u1')) spectraComb = self.spectraComb spectraComb.tofile(fp) -# if self.processFlags & PROCFLAG.DEFINE_PROCESS_CODE == PROCFLAG.DEFINE_PROCESS_CODE: -# nCode = numpy.array([self.nCode], numpy.dtype('u4')) #Probar con un dato que almacene codigo, hasta el momento no se hizo la prueba -# nCode.tofile(fp) -# -# nBaud = numpy.array([self.nBaud], numpy.dtype('u4')) -# nBaud.tofile(fp) -# -# code = self.code.reshape(self.nCode*self.nBaud) -# code = code.astype(numpy.dtype(' 1: self.dataOut.radarControllerHeaderObj.ippSeconds = self.radarControllerHeaderObj.ippSeconds/self.nTxs + #Time interval and code are propierties of dataOut. Its value depends of radarControllerHeaderObj. + # self.dataOut.timeInterval = self.radarControllerHeaderObj.ippSeconds * self.processingHeaderObj.nCohInt # # if self.radarControllerHeaderObj.code is not None: @@ -276,13 +278,11 @@ class VoltageReader(JRODataReader, ProcessingUnit): self.dataOut.nCohInt = self.processingHeaderObj.nCohInt - self.dataOut.flagShiftFFT = False - - self.dataOut.flagDecodeData = False #asumo q la data no esta decodificada + self.dataOut.flagDecodeData = self.processingHeaderObj.flag_decode #asumo q la data no esta decodificada - self.dataOut.flagDeflipData = False #asumo q la data no esta sin flip + self.dataOut.flagDeflipData = self.processingHeaderObj.flag_deflip #asumo q la data no esta sin flip - self.dataOut.flagShiftFFT = False + self.dataOut.flagShiftFFT = self.processingHeaderObj.shif_fft def getData(self): """ @@ -569,31 +569,26 @@ class VoltageWriter(JRODataWriter, Operation): self.systemHeaderObj.nChannels = self.dataOut.nChannels self.radarControllerHeaderObj = self.dataOut.radarControllerHeaderObj.copy() - self.setBasicHeader() - - processingHeaderSize = 40 # bytes self.processingHeaderObj.dtype = 0 # Voltage self.processingHeaderObj.blockSize = self.__getBlockSize() self.processingHeaderObj.profilesPerBlock = self.profilesPerBlock self.processingHeaderObj.dataBlocksPerFile = self.blocksPerFile self.processingHeaderObj.nWindows = 1 #podria ser 1 o self.dataOut.processingHeaderObj.nWindows - self.processingHeaderObj.processFlags = self.getProcessFlags() self.processingHeaderObj.nCohInt = self.dataOut.nCohInt self.processingHeaderObj.nIncohInt = 1 # Cuando la data de origen es de tipo Voltage self.processingHeaderObj.totalSpectra = 0 # Cuando la data de origen es de tipo Voltage -# if self.dataOut.code is not None: -# self.processingHeaderObj.code = self.dataOut.code -# self.processingHeaderObj.nCode = self.dataOut.nCode -# self.processingHeaderObj.nBaud = self.dataOut.nBaud -# codesize = int(8 + 4 * self.dataOut.nCode * self.dataOut.nBaud) -# processingHeaderSize += codesize + if self.dataOut.code is not None: + self.processingHeaderObj.code = self.dataOut.code + self.processingHeaderObj.nCode = self.dataOut.nCode + self.processingHeaderObj.nBaud = self.dataOut.nBaud if self.processingHeaderObj.nWindows != 0: self.processingHeaderObj.firstHeight = self.dataOut.heightList[0] self.processingHeaderObj.deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0] self.processingHeaderObj.nHeights = self.dataOut.nHeights self.processingHeaderObj.samplesWin = self.dataOut.nHeights - processingHeaderSize += 12 - self.processingHeaderObj.size = processingHeaderSize \ No newline at end of file + self.processingHeaderObj.processFlags = self.getProcessFlags() + + self.setBasicHeader() \ No newline at end of file diff --git a/schainpy/model/proc/jroproc_spectra.py b/schainpy/model/proc/jroproc_spectra.py index 3cb5600..721b7d7 100644 --- a/schainpy/model/proc/jroproc_spectra.py +++ b/schainpy/model/proc/jroproc_spectra.py @@ -606,6 +606,7 @@ class SpectraProc(ProcessingUnit): return 1 def setRadarFrequency(self, frequency=None): + if frequency != None: self.dataOut.frequency = frequency @@ -807,7 +808,10 @@ class IncohInt(Operation): def byProfiles(self, *args): self.__dataReady = False - + avgdata_spc = None + avgdata_cspc = None + avgdata_dc = None + self.putData(*args) if self.__profIndex == self.n: @@ -821,6 +825,9 @@ class IncohInt(Operation): def byTime(self, datatime, *args): self.__dataReady = False + avgdata_spc = None + avgdata_cspc = None + avgdata_dc = None self.putData(*args) diff --git a/schainpy/model/proc/jroproc_voltage.py b/schainpy/model/proc/jroproc_voltage.py index e2caecf..fe257d4 100644 --- a/schainpy/model/proc/jroproc_voltage.py +++ b/schainpy/model/proc/jroproc_voltage.py @@ -136,9 +136,12 @@ class VoltageProc(ProcessingUnit): if maxHei == None: maxHei = self.dataOut.heightList[-1] - if (minHei < self.dataOut.heightList[0]) or (minHei > maxHei): - raise ValueError, "some value in (%d,%d) is not valid" % (minHei, maxHei) - + if (minHei < self.dataOut.heightList[0]): + minHei = self.dataOut.heightList[0] +# raise ValueError, "height range [%d,%d] is not valid. Data height range is [%d, %d]" % (minHei, +# maxHei, +# self.dataOut.heightList[0], +# self.dataOut.heightList[-1]) if (maxHei > self.dataOut.heightList[-1]): maxHei = self.dataOut.heightList[-1] @@ -627,7 +630,10 @@ class Decoder(Operation): return self.datadecTime def run(self, dataOut, code=None, nCode=None, nBaud=None, mode = 0, osamp=None, times=None): - + + if dataOut.flagDecodeData: + print "This data is already decoded, recoding again ..." + if not self.isConfig: if code is None: @@ -638,7 +644,11 @@ class Decoder(Operation): self.setup(code, osamp, dataOut) self.isConfig = True - + + if self.code is None: + print "Fail decoding: Code is not defined." + return + if dataOut.flagDataAsBlock: """ Decoding when data have been read as block, @@ -917,139 +927,139 @@ class Reshaper(Operation): dataOut.nProfiles = dataOut.data.shape[1] dataOut.ippSeconds *= factor - -import collections -from scipy.stats import mode - -class Synchronize(Operation): - - isConfig = False - __profIndex = 0 - - def __init__(self): - - Operation.__init__(self) -# self.isConfig = False - self.__powBuffer = None - self.__startIndex = 0 - self.__pulseFound = False - - def __findTxPulse(self, dataOut, channel=0, pulse_with = None): - - #Read data - - powerdB = dataOut.getPower(channel = channel) - noisedB = dataOut.getNoise(channel = channel)[0] - - self.__powBuffer.extend(powerdB.flatten()) - - dataArray = numpy.array(self.__powBuffer) - - filteredPower = numpy.correlate(dataArray, dataArray[0:self.__nSamples], "same") - - maxValue = numpy.nanmax(filteredPower) - - if maxValue < noisedB + 10: - #No se encuentra ningun pulso de transmision - return None - - maxValuesIndex = numpy.where(filteredPower > maxValue - 0.1*abs(maxValue))[0] - - if len(maxValuesIndex) < 2: - #Solo se encontro un solo pulso de transmision de un baudio, esperando por el siguiente TX - return None - - phasedMaxValuesIndex = maxValuesIndex - self.__nSamples - - #Seleccionar solo valores con un espaciamiento de nSamples - pulseIndex = numpy.intersect1d(maxValuesIndex, phasedMaxValuesIndex) - - if len(pulseIndex) < 2: - #Solo se encontro un pulso de transmision con ancho mayor a 1 - return None - - spacing = pulseIndex[1:] - pulseIndex[:-1] - - #remover senales que se distancien menos de 10 unidades o muestras - #(No deberian existir IPP menor a 10 unidades) - - realIndex = numpy.where(spacing > 10 )[0] - - if len(realIndex) < 2: - #Solo se encontro un pulso de transmision con ancho mayor a 1 - return None - - #Eliminar pulsos anchos (deja solo la diferencia entre IPPs) - realPulseIndex = pulseIndex[realIndex] - - period = mode(realPulseIndex[1:] - realPulseIndex[:-1])[0][0] - - print "IPP = %d samples" %period - - self.__newNSamples = dataOut.nHeights #int(period) - self.__startIndex = int(realPulseIndex[0]) - - return 1 - - - def setup(self, nSamples, nChannels, buffer_size = 4): - - self.__powBuffer = collections.deque(numpy.zeros( buffer_size*nSamples,dtype=numpy.float), - maxlen = buffer_size*nSamples) - - bufferList = [] - - for i in range(nChannels): - bufferByChannel = collections.deque(numpy.zeros( buffer_size*nSamples, dtype=numpy.complex) + numpy.NAN, - maxlen = buffer_size*nSamples) - - bufferList.append(bufferByChannel) - - self.__nSamples = nSamples - self.__nChannels = nChannels - self.__bufferList = bufferList - - def run(self, dataOut, channel = 0): - - if not self.isConfig: - nSamples = dataOut.nHeights - nChannels = dataOut.nChannels - self.setup(nSamples, nChannels) - self.isConfig = True - - #Append new data to internal buffer - for thisChannel in range(self.__nChannels): - bufferByChannel = self.__bufferList[thisChannel] - bufferByChannel.extend(dataOut.data[thisChannel]) - - if self.__pulseFound: - self.__startIndex -= self.__nSamples - - #Finding Tx Pulse - if not self.__pulseFound: - indexFound = self.__findTxPulse(dataOut, channel) - - if indexFound == None: - dataOut.flagNoData = True - return - - self.__arrayBuffer = numpy.zeros((self.__nChannels, self.__newNSamples), dtype = numpy.complex) - self.__pulseFound = True - self.__startIndex = indexFound - - #If pulse was found ... - for thisChannel in range(self.__nChannels): - bufferByChannel = self.__bufferList[thisChannel] - #print self.__startIndex - x = numpy.array(bufferByChannel) - self.__arrayBuffer[thisChannel] = x[self.__startIndex:self.__startIndex+self.__newNSamples] - - deltaHeight = dataOut.heightList[1] - dataOut.heightList[0] - dataOut.heightList = numpy.arange(self.__newNSamples)*deltaHeight -# dataOut.ippSeconds = (self.__newNSamples / deltaHeight)/1e6 - - dataOut.data = self.__arrayBuffer - - self.__startIndex += self.__newNSamples - - return \ No newline at end of file +# +# import collections +# from scipy.stats import mode +# +# class Synchronize(Operation): +# +# isConfig = False +# __profIndex = 0 +# +# def __init__(self): +# +# Operation.__init__(self) +# # self.isConfig = False +# self.__powBuffer = None +# self.__startIndex = 0 +# self.__pulseFound = False +# +# def __findTxPulse(self, dataOut, channel=0, pulse_with = None): +# +# #Read data +# +# powerdB = dataOut.getPower(channel = channel) +# noisedB = dataOut.getNoise(channel = channel)[0] +# +# self.__powBuffer.extend(powerdB.flatten()) +# +# dataArray = numpy.array(self.__powBuffer) +# +# filteredPower = numpy.correlate(dataArray, dataArray[0:self.__nSamples], "same") +# +# maxValue = numpy.nanmax(filteredPower) +# +# if maxValue < noisedB + 10: +# #No se encuentra ningun pulso de transmision +# return None +# +# maxValuesIndex = numpy.where(filteredPower > maxValue - 0.1*abs(maxValue))[0] +# +# if len(maxValuesIndex) < 2: +# #Solo se encontro un solo pulso de transmision de un baudio, esperando por el siguiente TX +# return None +# +# phasedMaxValuesIndex = maxValuesIndex - self.__nSamples +# +# #Seleccionar solo valores con un espaciamiento de nSamples +# pulseIndex = numpy.intersect1d(maxValuesIndex, phasedMaxValuesIndex) +# +# if len(pulseIndex) < 2: +# #Solo se encontro un pulso de transmision con ancho mayor a 1 +# return None +# +# spacing = pulseIndex[1:] - pulseIndex[:-1] +# +# #remover senales que se distancien menos de 10 unidades o muestras +# #(No deberian existir IPP menor a 10 unidades) +# +# realIndex = numpy.where(spacing > 10 )[0] +# +# if len(realIndex) < 2: +# #Solo se encontro un pulso de transmision con ancho mayor a 1 +# return None +# +# #Eliminar pulsos anchos (deja solo la diferencia entre IPPs) +# realPulseIndex = pulseIndex[realIndex] +# +# period = mode(realPulseIndex[1:] - realPulseIndex[:-1])[0][0] +# +# print "IPP = %d samples" %period +# +# self.__newNSamples = dataOut.nHeights #int(period) +# self.__startIndex = int(realPulseIndex[0]) +# +# return 1 +# +# +# def setup(self, nSamples, nChannels, buffer_size = 4): +# +# self.__powBuffer = collections.deque(numpy.zeros( buffer_size*nSamples,dtype=numpy.float), +# maxlen = buffer_size*nSamples) +# +# bufferList = [] +# +# for i in range(nChannels): +# bufferByChannel = collections.deque(numpy.zeros( buffer_size*nSamples, dtype=numpy.complex) + numpy.NAN, +# maxlen = buffer_size*nSamples) +# +# bufferList.append(bufferByChannel) +# +# self.__nSamples = nSamples +# self.__nChannels = nChannels +# self.__bufferList = bufferList +# +# def run(self, dataOut, channel = 0): +# +# if not self.isConfig: +# nSamples = dataOut.nHeights +# nChannels = dataOut.nChannels +# self.setup(nSamples, nChannels) +# self.isConfig = True +# +# #Append new data to internal buffer +# for thisChannel in range(self.__nChannels): +# bufferByChannel = self.__bufferList[thisChannel] +# bufferByChannel.extend(dataOut.data[thisChannel]) +# +# if self.__pulseFound: +# self.__startIndex -= self.__nSamples +# +# #Finding Tx Pulse +# if not self.__pulseFound: +# indexFound = self.__findTxPulse(dataOut, channel) +# +# if indexFound == None: +# dataOut.flagNoData = True +# return +# +# self.__arrayBuffer = numpy.zeros((self.__nChannels, self.__newNSamples), dtype = numpy.complex) +# self.__pulseFound = True +# self.__startIndex = indexFound +# +# #If pulse was found ... +# for thisChannel in range(self.__nChannels): +# bufferByChannel = self.__bufferList[thisChannel] +# #print self.__startIndex +# x = numpy.array(bufferByChannel) +# self.__arrayBuffer[thisChannel] = x[self.__startIndex:self.__startIndex+self.__newNSamples] +# +# deltaHeight = dataOut.heightList[1] - dataOut.heightList[0] +# dataOut.heightList = numpy.arange(self.__newNSamples)*deltaHeight +# # dataOut.ippSeconds = (self.__newNSamples / deltaHeight)/1e6 +# +# dataOut.data = self.__arrayBuffer +# +# self.__startIndex += self.__newNSamples +# +# return \ No newline at end of file