Пример #1
0
    def __init__(self):
        """
        Inicializador de la clases SimulatorReader para
        generar datos de voltage simulados.
        Input:
            dataOut: Objeto de la clase Voltage.
            Este Objeto sera utilizado apra almacenar
            un perfil de datos  cada vez qe se haga
            un requerimiento (getData)
        """
        ProcessingUnit.__init__(self)
        print(" [ START ] init - Metodo Simulator Reader")

        self.isConfig                  = False
        self.basicHeaderObj            = BasicHeader(LOCALTIME)
        self.systemHeaderObj           = SystemHeader()
        self.radarControllerHeaderObj  = RadarControllerHeader()
        self.processingHeaderObj       = ProcessingHeader()
        self.profileIndex              = 2**32-1
        self.dataOut                   = Voltage()
        #code0                          = numpy.array([1,1,1,0,1,1,0,1,1,1,1,0,0,0,1,0,1,1,1,0,1,1,0,1,0,0,0,1,1,1,0,1])
        code0                          = numpy.array([1,1,1,-1,1,1,-1,1,1,1,1,-1,-1,-1,1,-1,1,1,1,-1,1,1,-1,1,-1,-1,-1,1,1,1,-1,1])
        #code1                          = numpy.array([1,1,1,0,1,1,0,1,1,1,1,0,0,0,1,0,0,0,0,1,0,0,1,0,1,1,1,0,0,0,1,0])
        code1                          = numpy.array([1,1,1,-1,1,1,-1,1,1,1,1,-1,-1,-1,1,-1,-1,-1,-1,1,-1,-1,1,-1,1,1,1,-1,-1,-1,1,-1])
        #self.Dyn_snCode                = numpy.array([code0,code1])
        self.Dyn_snCode                = None
Пример #2
0
    def __init__(self):
        """
        Inicializador de la clase VoltageReader para la lectura de datos de voltage.

        Input:
            dataOut    :    Objeto de la clase Voltage. Este objeto sera utilizado para
                              almacenar un perfil de datos cada vez que se haga un requerimiento
                              (getData). El perfil sera obtenido a partir del buffer de datos,
                              si el buffer esta vacio se hara un nuevo proceso de lectura de un
                              bloque de datos.
                              Si este parametro no es pasado se creara uno internamente.

        Variables afectadas:
            self.dataOut

        Return:
            None
        """

        ProcessingUnit.__init__(self)
        
        self.ext = ".r"
        self.optchar = "D"
        self.basicHeaderObj = BasicHeader(LOCALTIME)
        self.systemHeaderObj = SystemHeader()
        self.radarControllerHeaderObj = RadarControllerHeader()
        self.processingHeaderObj = ProcessingHeader()
        self.lastUTTime = 0
        self.profileIndex = 2**32 - 1        
        self.dataOut = Voltage()
        self.selBlocksize = None
        self.selBlocktime = None
Пример #3
0
    def __init__(self):
        """
        Inicializador de la clase SpectraWriter para la escritura de datos de espectros.

        Affected:
            self.dataOut
            self.basicHeaderObj
            self.systemHeaderObj
            self.radarControllerHeaderObj
            self.processingHeaderObj

        Return: None
        """

        Operation.__init__(self)

        self.ext = ".pdata"
        self.optchar = "P"
        self.shape_spc_Buffer = None
        self.shape_cspc_Buffer = None
        self.shape_dc_Buffer = None
        self.data_spc = None
        self.data_cspc = None
        self.data_dc = None
        self.setFile = None
        self.noMoreFiles = 0
        self.basicHeaderObj = BasicHeader(LOCALTIME)
        self.systemHeaderObj = SystemHeader()
        self.radarControllerHeaderObj = RadarControllerHeader()
        self.processingHeaderObj = ProcessingHeader()
Пример #4
0
    def verifyFile(self, filename):

        flag = True

        try:
            fp = open(filename, 'rb')
        except IOError:
            log.error("File {} can't be opened".format(filename), self.name)
            return False

        if self.online and self.waitDataBlock(0):
            pass

        basicHeaderObj = BasicHeader(LOCALTIME)
        systemHeaderObj = SystemHeader()
        radarControllerHeaderObj = RadarControllerHeader()
        processingHeaderObj = ProcessingHeader()

        if not (basicHeaderObj.read(fp)):
            flag = False
        if not (systemHeaderObj.read(fp)):
            flag = False
        if not (radarControllerHeaderObj.read(fp)):
            flag = False
        if not (processingHeaderObj.read(fp)):
            flag = False
        if not self.online:
            dt1 = basicHeaderObj.datatime
            pos = self.fileSize - processingHeaderObj.blockSize - 24
            if pos < 0:
                flag = False
                log.error('Invalid size for file: {}'.format(self.filename),
                          self.name)
            else:
                fp.seek(pos)
                if not (basicHeaderObj.read(fp)):
                    flag = False
            dt2 = basicHeaderObj.datatime
            if not self.isDateTimeInRange(dt1, self.startDate, self.endDate, self.startTime, self.endTime) and not \
                    self.isDateTimeInRange(dt2, self.startDate, self.endDate, self.startTime, self.endTime):
                flag = False

        fp.close()
        return flag
Пример #5
0
    def __init__(self):  #, **kwargs):
        """
        Inicializador de la clase SpectraReader para la lectura de datos de espectros.

        Inputs:
            dataOut    :    Objeto de la clase Spectra. Este objeto sera utilizado para
                              almacenar un perfil de datos cada vez que se haga un requerimiento
                              (getData). El perfil sera obtenido a partir del buffer de datos,
                              si el buffer esta vacio se hara un nuevo proceso de lectura de un
                              bloque de datos.
                              Si este parametro no es pasado se creara uno internamente.

        Affected:
            self.dataOut

        Return      : None
        """

        ProcessingUnit.__init__(self)

        self.pts2read_SelfSpectra = 0
        self.pts2read_CrossSpectra = 0
        self.pts2read_DCchannels = 0
        self.ext = ".pdata"
        self.optchar = "P"
        self.basicHeaderObj = BasicHeader(LOCALTIME)
        self.systemHeaderObj = SystemHeader()
        self.radarControllerHeaderObj = RadarControllerHeader()
        self.processingHeaderObj = ProcessingHeader()
        self.lastUTTime = 0
        self.maxTimeStep = 30
        self.dataOut = Spectra()
        self.profileIndex = 1
        self.nRdChannels = None
        self.nRdPairs = None
        self.rdPairList = []
Пример #6
0
 def __init__(self):
     """
     Inicializador de la clases SimulatorReader
     para generar datos de voltage simulados.
     Input:
         dataOut: Objeto de la clase Voltage. 
         Este Objeto sera utilizado apra almacenar un perfil de datos
         cada vez qe se haga un requerimiento (getData)
     """
     self.isConfig              = False
     self.bascicHeaderObj       = BasicHeader(LOCALTIME)
     self.systemHeaderObj       = SystemHeader()
     self.radarControlHeaderObj = RadarControlHeader()
     self.ProcessingHeaderObj   = ProcessingHeader()
     self.profileIndex          = 2**32-1
     self.dataOut               = Voltage()
Пример #7
0
    def __init__(self):#, **kwargs):
        """
        Inicializador de la clase VoltageWriter para la escritura de datos de espectros.

        Affected:
            self.dataOut

        Return: None
        """
        Operation.__init__(self)#, **kwargs)

        self.nTotalBlocks = 0

        self.profileIndex = 0

        self.isConfig = False

        self.fp = None

        self.flagIsNewFile = 1

        self.blockIndex = 0

        self.flagIsNewBlock = 0

        self.setFile = None

        self.dtype = None

        self.path = None

        self.filename = None

        self.basicHeaderObj = BasicHeader(LOCALTIME)

        self.systemHeaderObj = SystemHeader()

        self.radarControllerHeaderObj = RadarControllerHeader()

        self.processingHeaderObj = ProcessingHeader()
Пример #8
0
    def __setFileHeader(self):
        '''
        In this method will be initialized every parameter of dataOut object (header, no data)
        '''
        ippSeconds = 1.0*self.__nSamples/self.__sample_rate

        nProfiles = 1.0/ippSeconds  #Number of profiles in one second

        self.dataOut.radarControllerHeaderObj = RadarControllerHeader(ipp=self.__ippKm,
                                                                      txA=0,
                                                                      txB=0,
                                                                      nWindows=1,
                                                                      nHeights=self.__nSamples,
                                                                      firstHeight=self.__firstHeigth,
                                                                      deltaHeight=self.__deltaHeigth,
                                                                      codeType=self.__codeType,
                                                                      nCode=self.__nCode, nBaud=self.__nBaud,
                                                                      code = self.__code)

        self.dataOut.systemHeaderObj = SystemHeader(nSamples=self.__nSamples,
                                                    nProfiles=nProfiles,
                                                    nChannels=len(self.__channelList),
                                                    adcResolution=14)

        self.dataOut.type = "Voltage"

        self.dataOut.data = None

        self.dataOut.dtype = numpy.dtype([('real','<i8'),('imag','<i8')])

#        self.dataOut.nChannels = 0

#        self.dataOut.nHeights = 0

        self.dataOut.nProfiles = nProfiles

        self.dataOut.heightList = self.__firstHeigth + numpy.arange(self.__nSamples, dtype = numpy.float)*self.__deltaHeigth

        self.dataOut.channelList = self.__channelList

        self.dataOut.blocksize = self.dataOut.nChannels * self.dataOut.nHeights

#        self.dataOut.channelIndexList = None

        self.dataOut.flagNoData = True

        #Set to TRUE if the data is discontinuous
        self.dataOut.flagDiscontinuousBlock = False

        self.dataOut.utctime = None

        self.dataOut.timeZone = self.__timezone/60  #timezone like jroheader, difference in minutes between UTC and localtime

        self.dataOut.dstFlag = 0

        self.dataOut.errorCount = 0

        self.dataOut.nCohInt = 1

        self.dataOut.flagDecodeData = False #asumo que la data esta decodificada

        self.dataOut.flagDeflipData = False #asumo que la data esta sin flip

        self.dataOut.flagShiftFFT = False

        self.dataOut.ippSeconds = ippSeconds

        #Time interval between profiles
        #self.dataOut.timeInterval = self.dataOut.ippSeconds * self.dataOut.nCohInt

        self.dataOut.frequency = self.__frequency

        self.dataOut.realtime = self.__online
Пример #9
0
    def fillJROHeader(self):

        #fill radar controller header
        self.dataOut.radarControllerHeaderObj = RadarControllerHeader(
            ippKm=self.__ippKm,
            txA=self.__txA,
            txB=0,
            nWindows=1,
            nHeights=self.__nSamples,
            firstHeight=self.__firstHeight,
            deltaHeight=self.__deltaHeight,
            codeType=self.__codeType,
            nCode=self.__nCode,
            nBaud=self.__nBaud,
            code=self.__code,
            fClock=1)

        #fill system header
        self.dataOut.systemHeaderObj = SystemHeader(nSamples=self.__nSamples,
                                                    nProfiles=self.newProfiles,
                                                    nChannels=len(
                                                        self.__channelList),
                                                    adcResolution=14,
                                                    pciDioBusWith=32)

        self.dataOut.type = "Voltage"

        self.dataOut.data = None

        self.dataOut.dtype = numpy.dtype([('real', '<i8'), ('imag', '<i8')])

        #        self.dataOut.nChannels = 0

        #        self.dataOut.nHeights = 0

        self.dataOut.nProfiles = self.newProfiles * self.nblocks

        #self.dataOut.heightList = self.__firstHeigth + numpy.arange(self.__nSamples, dtype = numpy.float)*self.__deltaHeigth
        ranges = numpy.reshape(self.rangeFromFile.value, (-1))
        self.dataOut.heightList = ranges / 1000.0  #km

        self.dataOut.channelList = self.__channelList

        self.dataOut.blocksize = self.dataOut.nChannels * self.dataOut.nHeights

        #        self.dataOut.channelIndexList = None

        self.dataOut.flagNoData = True

        #Set to TRUE if the data is discontinuous
        self.dataOut.flagDiscontinuousBlock = False

        self.dataOut.utctime = None

        #self.dataOut.timeZone = -5 #self.__timezone/60  #timezone like jroheader, difference in minutes between UTC and localtime
        if self.timezone == 'lt':
            self.dataOut.timeZone = time.timezone / 60.  #get the timezone in minutes
        else:
            self.dataOut.timeZone = 0  #by default time is UTC

        self.dataOut.dstFlag = 0

        self.dataOut.errorCount = 0

        self.dataOut.nCohInt = 1

        self.dataOut.flagDecodeData = False  #asumo que la data esta decodificada

        self.dataOut.flagDeflipData = False  #asumo que la data esta sin flip

        self.dataOut.flagShiftFFT = False

        self.dataOut.ippSeconds = self.ippSeconds

        #Time interval between profiles
        #self.dataOut.timeInterval = self.dataOut.ippSeconds * self.dataOut.nCohInt

        self.dataOut.frequency = self.__frequency

        self.dataOut.realtime = self.online
        pass
Пример #10
0
class SpectraReader(JRODataReader, ProcessingUnit):
    """
    Esta clase permite leer datos de espectros desde archivos procesados (.pdata). La lectura
    de los datos siempre se realiza por bloques. Los datos leidos (array de 3 dimensiones)
    son almacenados en tres buffer's para el Self Spectra, el Cross Spectra y el DC Channel.

                        paresCanalesIguales    * alturas * perfiles  (Self Spectra)
                        paresCanalesDiferentes * alturas * perfiles  (Cross Spectra)
                        canales * alturas                            (DC Channels)

    Esta clase contiene instancias (objetos) de las clases BasicHeader, SystemHeader,
    RadarControllerHeader y Spectra. Los tres primeros se usan para almacenar informacion de la
    cabecera de datos (metadata), y el cuarto (Spectra) para obtener y almacenar un bloque de
    datos desde el "buffer" cada vez que se ejecute el metodo "getData".

    Example:
        dpath = "/home/myuser/data"

        startTime = datetime.datetime(2010,1,20,0,0,0,0,0,0)

        endTime = datetime.datetime(2010,1,21,23,59,59,0,0,0)

        readerObj = SpectraReader()

        readerObj.setup(dpath, startTime, endTime)

        while(True):

            readerObj.getData()

            print readerObj.data_spc

            print readerObj.data_cspc

            print readerObj.data_dc

            if readerObj.flagNoMoreFiles:
                break

    """
    def __init__(self):  #, **kwargs):
        """
        Inicializador de la clase SpectraReader para la lectura de datos de espectros.

        Inputs:
            dataOut    :    Objeto de la clase Spectra. Este objeto sera utilizado para
                              almacenar un perfil de datos cada vez que se haga un requerimiento
                              (getData). El perfil sera obtenido a partir del buffer de datos,
                              si el buffer esta vacio se hara un nuevo proceso de lectura de un
                              bloque de datos.
                              Si este parametro no es pasado se creara uno internamente.

        Affected:
            self.dataOut

        Return      : None
        """

        ProcessingUnit.__init__(self)

        self.pts2read_SelfSpectra = 0
        self.pts2read_CrossSpectra = 0
        self.pts2read_DCchannels = 0
        self.ext = ".pdata"
        self.optchar = "P"
        self.basicHeaderObj = BasicHeader(LOCALTIME)
        self.systemHeaderObj = SystemHeader()
        self.radarControllerHeaderObj = RadarControllerHeader()
        self.processingHeaderObj = ProcessingHeader()
        self.lastUTTime = 0
        self.maxTimeStep = 30
        self.dataOut = Spectra()
        self.profileIndex = 1
        self.nRdChannels = None
        self.nRdPairs = None
        self.rdPairList = []

    def createObjByDefault(self):

        dataObj = Spectra()

        return dataObj

    def __hasNotDataInBuffer(self):
        return 1

    def getBlockDimension(self):
        """
        Obtiene la cantidad de puntos a leer por cada bloque de datos

        Affected:
            self.nRdChannels
            self.nRdPairs
            self.pts2read_SelfSpectra
            self.pts2read_CrossSpectra
            self.pts2read_DCchannels
            self.blocksize
            self.dataOut.nChannels
            self.dataOut.nPairs

        Return:
            None
        """
        self.nRdChannels = 0
        self.nRdPairs = 0
        self.rdPairList = []

        for i in range(0, self.processingHeaderObj.totalSpectra * 2, 2):
            if self.processingHeaderObj.spectraComb[
                    i] == self.processingHeaderObj.spectraComb[i + 1]:
                self.nRdChannels = self.nRdChannels + 1  #par de canales iguales
            else:
                self.nRdPairs = self.nRdPairs + 1  #par de canales diferentes
                self.rdPairList.append(
                    (self.processingHeaderObj.spectraComb[i],
                     self.processingHeaderObj.spectraComb[i + 1]))

        pts2read = self.processingHeaderObj.nHeights * self.processingHeaderObj.profilesPerBlock

        self.pts2read_SelfSpectra = int(self.nRdChannels * pts2read)
        self.blocksize = self.pts2read_SelfSpectra

        if self.processingHeaderObj.flag_cspc:
            self.pts2read_CrossSpectra = int(self.nRdPairs * pts2read)
            self.blocksize += self.pts2read_CrossSpectra

        if self.processingHeaderObj.flag_dc:
            self.pts2read_DCchannels = int(self.systemHeaderObj.nChannels *
                                           self.processingHeaderObj.nHeights)
            self.blocksize += self.pts2read_DCchannels

    def readBlock(self):
        """
        Lee el bloque de datos desde la posicion actual del puntero del archivo
        (self.fp) y actualiza todos los parametros relacionados al bloque de datos
        (metadata + data). La data leida es almacenada en el buffer y el contador del buffer
        es seteado a 0

        Return: None

        Variables afectadas:

            self.flagIsNewFile
            self.flagIsNewBlock
            self.nTotalBlocks
            self.data_spc
            self.data_cspc
            self.data_dc

        Exceptions:
            Si un bloque leido no es un bloque valido
        """

        fpointer = self.fp.tell()

        spc = numpy.fromfile(self.fp, self.dtype[0], self.pts2read_SelfSpectra)
        spc = spc.reshape((self.nRdChannels, self.processingHeaderObj.nHeights,
                           self.processingHeaderObj.profilesPerBlock
                           ))  #transforma a un arreglo 3D

        if self.processingHeaderObj.flag_cspc:
            cspc = numpy.fromfile(self.fp, self.dtype,
                                  self.pts2read_CrossSpectra)
            cspc = cspc.reshape(
                (self.nRdPairs, self.processingHeaderObj.nHeights,
                 self.processingHeaderObj.profilesPerBlock
                 ))  #transforma a un arreglo 3D

        if self.processingHeaderObj.flag_dc:
            dc = numpy.fromfile(
                self.fp, self.dtype, self.pts2read_DCchannels
            )  #int(self.processingHeaderObj.nHeights*self.systemHeaderObj.nChannels) )
            dc = dc.reshape((self.systemHeaderObj.nChannels,
                             self.processingHeaderObj.nHeights
                             ))  #transforma a un arreglo 2D

        if not self.processingHeaderObj.shif_fft:
            #desplaza a la derecha en el eje 2 determinadas posiciones
            shift = int(self.processingHeaderObj.profilesPerBlock / 2)
            spc = numpy.roll(spc, shift, axis=2)

            if self.processingHeaderObj.flag_cspc:
                #desplaza a la derecha en el eje 2 determinadas posiciones
                cspc = numpy.roll(cspc, shift, axis=2)

        #Dimensions : nChannels, nProfiles, nSamples
        spc = numpy.transpose(spc, (0, 2, 1))
        self.data_spc = spc

        if self.processingHeaderObj.flag_cspc:
            cspc = numpy.transpose(cspc, (0, 2, 1))
            self.data_cspc = cspc['real'] + cspc['imag'] * 1j
        else:
            self.data_cspc = None

        if self.processingHeaderObj.flag_dc:
            self.data_dc = dc['real'] + dc['imag'] * 1j
        else:
            self.data_dc = None

        self.flagIsNewFile = 0
        self.flagIsNewBlock = 1

        self.nTotalBlocks += 1
        self.nReadBlocks += 1

        return 1

    def getFirstHeader(self):

        self.getBasicHeader()
        self.dataOut.systemHeaderObj = self.systemHeaderObj.copy()
        self.dataOut.radarControllerHeaderObj = self.radarControllerHeaderObj.copy(
        )
        self.dataOut.dtype = self.dtype
        self.dataOut.pairsList = self.rdPairList
        self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock
        self.dataOut.nFFTPoints = self.processingHeaderObj.profilesPerBlock
        self.dataOut.nCohInt = self.processingHeaderObj.nCohInt
        self.dataOut.nIncohInt = self.processingHeaderObj.nIncohInt
        xf = self.processingHeaderObj.firstHeight + self.processingHeaderObj.nHeights * self.processingHeaderObj.deltaHeight
        self.dataOut.heightList = numpy.arange(
            self.processingHeaderObj.firstHeight, xf,
            self.processingHeaderObj.deltaHeight)
        self.dataOut.channelList = list(range(self.systemHeaderObj.nChannels))
        self.dataOut.flagShiftFFT = True  #Data is always shifted
        self.dataOut.flagDecodeData = self.processingHeaderObj.flag_decode  #asumo q la data no esta decodificada
        self.dataOut.flagDeflipData = self.processingHeaderObj.flag_deflip  #asumo q la data esta sin flip

    def getData(self):
        """
        First method to execute before "RUN" is called.

        Copia el buffer de lectura a la clase "Spectra",
        con todos los parametros asociados a este (metadata). cuando no hay datos en el buffer de
        lectura es necesario hacer una nueva lectura de los bloques de datos usando "readNextBlock"

        Return:
            0    :    Si no hay mas archivos disponibles
            1    :    Si hizo una buena copia del buffer

        Affected:
            self.dataOut
            self.flagDiscontinuousBlock
            self.flagIsNewBlock
        """

        if self.flagNoMoreFiles:
            self.dataOut.flagNoData = True
            return 0

        self.flagDiscontinuousBlock = 0
        self.flagIsNewBlock = 0

        if self.__hasNotDataInBuffer():

            if not (self.readNextBlock()):
                self.dataOut.flagNoData = True
                return 0

        #data es un numpy array de 3 dmensiones (perfiles, alturas y canales)

        if self.data_spc is None:
            self.dataOut.flagNoData = True
            return 0

        self.getBasicHeader()
        self.getFirstHeader()
        self.dataOut.data_spc = self.data_spc
        self.dataOut.data_cspc = self.data_cspc
        self.dataOut.data_dc = self.data_dc
        self.dataOut.flagNoData = False
        self.dataOut.realtime = self.online

        return self.dataOut.data_spc
Пример #11
0
class VoltageReader(JRODataReader, ProcessingUnit):
    """
    Esta clase permite leer datos de voltage desde archivos en formato rawdata (.r). La lectura
    de los datos siempre se realiza por bloques. Los datos leidos (array de 3 dimensiones:
    perfiles*alturas*canales) son almacenados en la variable "buffer".

                             perfiles * alturas * canales

    Esta clase contiene instancias (objetos) de las clases BasicHeader, SystemHeader,
    RadarControllerHeader y Voltage. Los tres primeros se usan para almacenar informacion de la
    cabecera de datos (metadata), y el cuarto (Voltage) para obtener y almacenar un perfil de
    datos desde el "buffer" cada vez que se ejecute el metodo "getData".

    Example:

        dpath = "/home/myuser/data"

        startTime = datetime.datetime(2010,1,20,0,0,0,0,0,0)

        endTime = datetime.datetime(2010,1,21,23,59,59,0,0,0)

        readerObj = VoltageReader()

        readerObj.setup(dpath, startTime, endTime)

        while(True):

            #to get one profile
            profile =  readerObj.getData()

            #print the profile
            print profile

            #If you want to see all datablock
            print readerObj.datablock

            if readerObj.flagNoMoreFiles:
                break

    """

    def __init__(self):
        """
        Inicializador de la clase VoltageReader para la lectura de datos de voltage.

        Input:
            dataOut    :    Objeto de la clase Voltage. Este objeto sera utilizado para
                              almacenar un perfil de datos cada vez que se haga un requerimiento
                              (getData). El perfil sera obtenido a partir del buffer de datos,
                              si el buffer esta vacio se hara un nuevo proceso de lectura de un
                              bloque de datos.
                              Si este parametro no es pasado se creara uno internamente.

        Variables afectadas:
            self.dataOut

        Return:
            None
        """

        ProcessingUnit.__init__(self)
        
        self.ext = ".r"
        self.optchar = "D"
        self.basicHeaderObj = BasicHeader(LOCALTIME)
        self.systemHeaderObj = SystemHeader()
        self.radarControllerHeaderObj = RadarControllerHeader()
        self.processingHeaderObj = ProcessingHeader()
        self.lastUTTime = 0
        self.profileIndex = 2**32 - 1        
        self.dataOut = Voltage()
        self.selBlocksize = None
        self.selBlocktime = None

    def createObjByDefault(self):

        dataObj = Voltage()

        return dataObj

    def __hasNotDataInBuffer(self):

        if self.profileIndex >= self.processingHeaderObj.profilesPerBlock * self.nTxs:
            return 1

        return 0

    def getBlockDimension(self):
        """
            Obtiene la cantidad de puntos a leer por cada bloque de datos

            Affected:
                self.blocksize

            Return:
                None
        """
        pts2read = self.processingHeaderObj.profilesPerBlock * \
            self.processingHeaderObj.nHeights * self.systemHeaderObj.nChannels
        self.blocksize = pts2read

    def readBlock(self):
        """
            readBlock lee el bloque de datos desde la posicion actual del puntero del archivo
            (self.fp) y actualiza todos los parametros relacionados al bloque de datos
            (metadata + data). La data leida es almacenada en el buffer y el contador del buffer
            es seteado a 0

            Inputs:
                None

            Return:
                None

            Affected:
                self.profileIndex
                self.datablock
                self.flagIsNewFile
                self.flagIsNewBlock
                self.nTotalBlocks

            Exceptions: 
                Si un bloque leido no es un bloque valido
        """

        # if self.server is not None:
        #     self.zBlock = self.receiver.recv()
        #     self.zHeader = self.zBlock[:24]
        #     self.zDataBlock = self.zBlock[24:]
        #     junk = numpy.fromstring(self.zDataBlock, numpy.dtype([('real','<i4'),('imag','<i4')]))
        #     self.processingHeaderObj.profilesPerBlock = 240
        #     self.processingHeaderObj.nHeights = 248
        #     self.systemHeaderObj.nChannels
        # else:
        current_pointer_location = self.fp.tell()
        junk = numpy.fromfile(self.fp, self.dtype, self.blocksize)

        try:
            junk = junk.reshape((self.processingHeaderObj.profilesPerBlock,
                                 self.processingHeaderObj.nHeights, self.systemHeaderObj.nChannels))
        except:
            # print "The read block (%3d) has not enough data" %self.nReadBlocks

            if self.waitDataBlock(pointer_location=current_pointer_location):
                junk = numpy.fromfile(self.fp, self.dtype, self.blocksize)
                junk = junk.reshape((self.processingHeaderObj.profilesPerBlock,
                                     self.processingHeaderObj.nHeights, self.systemHeaderObj.nChannels))
        #             return 0

        # Dimensions : nChannels, nProfiles, nSamples

        junk = numpy.transpose(junk, (2, 0, 1))
        self.datablock = junk['real'] + junk['imag'] * 1j

        self.profileIndex = 0

        self.flagIsNewFile = 0
        self.flagIsNewBlock = 1

        self.nTotalBlocks += 1
        self.nReadBlocks += 1

        return 1

    def getFirstHeader(self):

        self.getBasicHeader()

        self.dataOut.processingHeaderObj = self.processingHeaderObj.copy()

        self.dataOut.systemHeaderObj = self.systemHeaderObj.copy()

        self.dataOut.radarControllerHeaderObj = self.radarControllerHeaderObj.copy()

        if self.nTxs > 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:
        #
        #             self.dataOut.nCode = self.radarControllerHeaderObj.nCode
        #
        #             self.dataOut.nBaud = self.radarControllerHeaderObj.nBaud
        #
        #             self.dataOut.code = self.radarControllerHeaderObj.code

        self.dataOut.dtype = self.dtype

        self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock

        self.dataOut.heightList = numpy.arange(
            self.processingHeaderObj.nHeights) * self.processingHeaderObj.deltaHeight + self.processingHeaderObj.firstHeight

        self.dataOut.channelList = list(range(self.systemHeaderObj.nChannels))

        self.dataOut.nCohInt = self.processingHeaderObj.nCohInt

        # asumo q la data no esta decodificada
        self.dataOut.flagDecodeData = self.processingHeaderObj.flag_decode

        # asumo q la data no esta sin flip
        self.dataOut.flagDeflipData = self.processingHeaderObj.flag_deflip

        self.dataOut.flagShiftFFT = self.processingHeaderObj.shif_fft

    def reshapeData(self):

        if self.nTxs < 0:
            return

        if self.nTxs == 1:
            return

        if self.nTxs < 1 and self.processingHeaderObj.profilesPerBlock % (1. / self.nTxs) != 0:
            raise ValueError("1./nTxs (=%f), should be a multiple of nProfiles (=%d)" % (
                1. / self.nTxs, self.processingHeaderObj.profilesPerBlock))

        if self.nTxs > 1 and self.processingHeaderObj.nHeights % self.nTxs != 0:
            raise ValueError("nTxs (=%d), should be a multiple of nHeights (=%d)" % (
                self.nTxs, self.processingHeaderObj.nHeights))

        self.datablock = self.datablock.reshape(
            (self.systemHeaderObj.nChannels, self.processingHeaderObj.profilesPerBlock * self.nTxs, int(self.processingHeaderObj.nHeights / self.nTxs)))

        self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock * self.nTxs
        self.dataOut.heightList = numpy.arange(self.processingHeaderObj.nHeights / self.nTxs) * \
            self.processingHeaderObj.deltaHeight + self.processingHeaderObj.firstHeight
        self.dataOut.radarControllerHeaderObj.ippSeconds = self.radarControllerHeaderObj.ippSeconds / self.nTxs

        return

    def readFirstHeaderFromServer(self):

        self.getFirstHeader()

        self.firstHeaderSize = self.basicHeaderObj.size

        datatype = int(numpy.log2((self.processingHeaderObj.processFlags &
                                   PROCFLAG.DATATYPE_MASK)) - numpy.log2(PROCFLAG.DATATYPE_CHAR))
        if datatype == 0:
            datatype_str = numpy.dtype([('real', '<i1'), ('imag', '<i1')])
        elif datatype == 1:
            datatype_str = numpy.dtype([('real', '<i2'), ('imag', '<i2')])
        elif datatype == 2:
            datatype_str = numpy.dtype([('real', '<i4'), ('imag', '<i4')])
        elif datatype == 3:
            datatype_str = numpy.dtype([('real', '<i8'), ('imag', '<i8')])
        elif datatype == 4:
            datatype_str = numpy.dtype([('real', '<f4'), ('imag', '<f4')])
        elif datatype == 5:
            datatype_str = numpy.dtype([('real', '<f8'), ('imag', '<f8')])
        else:
            raise ValueError('Data type was not defined')

        self.dtype = datatype_str
        #self.ippSeconds = 2 * 1000 * self.radarControllerHeaderObj.ipp / self.c
        self.fileSizeByHeader = self.processingHeaderObj.dataBlocksPerFile * self.processingHeaderObj.blockSize + \
            self.firstHeaderSize + self.basicHeaderSize * \
            (self.processingHeaderObj.dataBlocksPerFile - 1)
        #        self.dataOut.channelList = numpy.arange(self.systemHeaderObj.numChannels)
        #        self.dataOut.channelIndexList = numpy.arange(self.systemHeaderObj.numChannels)
        self.getBlockDimension()

    def getFromServer(self):
        self.flagDiscontinuousBlock = 0
        self.profileIndex = 0
        self.flagIsNewBlock = 1
        self.dataOut.flagNoData = False
        self.nTotalBlocks += 1
        self.nReadBlocks += 1
        self.blockPointer = 0

        block = self.receiver.recv()

        self.basicHeaderObj.read(block[self.blockPointer:])
        self.blockPointer += self.basicHeaderObj.length
        self.systemHeaderObj.read(block[self.blockPointer:])
        self.blockPointer += self.systemHeaderObj.length
        self.radarControllerHeaderObj.read(block[self.blockPointer:])
        self.blockPointer += self.radarControllerHeaderObj.length
        self.processingHeaderObj.read(block[self.blockPointer:])
        self.blockPointer += self.processingHeaderObj.length
        self.readFirstHeaderFromServer()

        timestamp = self.basicHeaderObj.get_datatime()
        print('[Reading] - Block {} - {}'.format(self.nTotalBlocks, timestamp))
        current_pointer_location = self.blockPointer
        junk = numpy.fromstring(
            block[self.blockPointer:], self.dtype, self.blocksize)

        try:
            junk = junk.reshape((self.processingHeaderObj.profilesPerBlock,
                                 self.processingHeaderObj.nHeights, self.systemHeaderObj.nChannels))
        except:
            # print "The read block (%3d) has not enough data" %self.nReadBlocks
            if self.waitDataBlock(pointer_location=current_pointer_location):
                junk = numpy.fromstring(
                    block[self.blockPointer:], self.dtype, self.blocksize)
                junk = junk.reshape((self.processingHeaderObj.profilesPerBlock,
                                     self.processingHeaderObj.nHeights, self.systemHeaderObj.nChannels))
        #               return 0

        # Dimensions : nChannels, nProfiles, nSamples

        junk = numpy.transpose(junk, (2, 0, 1))
        self.datablock = junk['real'] + junk['imag'] * 1j
        self.profileIndex = 0
        if self.selBlocksize == None:
            self.selBlocksize = self.dataOut.nProfiles
        if self.selBlocktime != None:
            if self.dataOut.nCohInt is not None:
                nCohInt = self.dataOut.nCohInt
            else:
                nCohInt = 1
            self.selBlocksize = int(self.dataOut.nProfiles * round(self.selBlocktime / (
                nCohInt * self.dataOut.ippSeconds * self.dataOut.nProfiles)))
        self.dataOut.data = self.datablock[:,
                                           self.profileIndex:self.profileIndex + self.selBlocksize, :]
        datasize = self.dataOut.data.shape[1]
        if datasize < self.selBlocksize:
            buffer = numpy.zeros(
                (self.dataOut.data.shape[0], self.selBlocksize, self.dataOut.data.shape[2]), dtype='complex')
            buffer[:, :datasize, :] = self.dataOut.data
            self.dataOut.data = buffer
            self.profileIndex = blockIndex

        self.dataOut.flagDataAsBlock = True
        self.flagIsNewBlock = 1
        self.dataOut.realtime = self.online

        return self.dataOut.data

    def getData(self):
        """
            getData obtiene una unidad de datos del buffer de lectura, un perfil,  y la copia al objeto self.dataOut
            del tipo "Voltage" con todos los parametros asociados a este (metadata). cuando no hay datos
            en el buffer de lectura es necesario hacer una nueva lectura de los bloques de datos usando
            "readNextBlock"

            Ademas incrementa el contador del buffer "self.profileIndex" en 1.

            Return:

                Si el flag self.getByBlock ha sido seteado el bloque completo es copiado a self.dataOut y el self.profileIndex
                es igual al total de perfiles leidos desde el archivo.

                Si self.getByBlock == False:

                    self.dataOut.data = buffer[:, thisProfile, :]

                    shape = [nChannels, nHeis]

                Si self.getByBlock == True:

                    self.dataOut.data = buffer[:, :, :]

                    shape = [nChannels, nProfiles, nHeis]

            Variables afectadas:
                self.dataOut
                self.profileIndex

            Affected:
                self.dataOut
                self.profileIndex
                self.flagDiscontinuousBlock
                self.flagIsNewBlock
        """
        if self.flagNoMoreFiles:
            self.dataOut.flagNoData = True
            return 0
        self.flagDiscontinuousBlock = 0
        self.flagIsNewBlock = 0
        if self.__hasNotDataInBuffer():
            if not(self.readNextBlock()):
                return 0

            self.getFirstHeader()

            self.reshapeData()
        if self.datablock is None:
            self.dataOut.flagNoData = True
            return 0

        if not self.getByBlock:

            """
                Return profile by profile

                If nTxs > 1 then one profile is divided by nTxs and number of total
                blocks is increased by nTxs (nProfiles *= nTxs)
            """
            self.dataOut.flagDataAsBlock = False
            self.dataOut.data = self.datablock[:, self.profileIndex, :]
            self.dataOut.profileIndex = self.profileIndex

            self.profileIndex += 1

        else:
            """
                Return a block
            """
            if self.selBlocksize == None:
                self.selBlocksize = self.dataOut.nProfiles
            if self.selBlocktime != None:
                if self.dataOut.nCohInt is not None:
                    nCohInt = self.dataOut.nCohInt
                else:
                    nCohInt = 1
                self.selBlocksize = int(self.dataOut.nProfiles * round(self.selBlocktime / (
                    nCohInt * self.dataOut.ippSeconds * self.dataOut.nProfiles)))

            self.dataOut.data = self.datablock[:,
                                               self.profileIndex:self.profileIndex + self.selBlocksize, :]
            self.profileIndex += self.selBlocksize
            datasize = self.dataOut.data.shape[1]

            if datasize < self.selBlocksize:
                buffer = numpy.zeros(
                    (self.dataOut.data.shape[0], self.selBlocksize, self.dataOut.data.shape[2]), dtype='complex')
                buffer[:, :datasize, :] = self.dataOut.data

                while datasize < self.selBlocksize:  # Not enough profiles to fill the block
                    if not(self.readNextBlock()):
                        return 0
                    self.getFirstHeader()
                    self.reshapeData()
                    if self.datablock is None:
                        self.dataOut.flagNoData = True
                        return 0
                    # stack data
                    blockIndex = self.selBlocksize - datasize
                    datablock1 = self.datablock[:, :blockIndex, :]

                    buffer[:, datasize:datasize +
                           datablock1.shape[1], :] = datablock1
                    datasize += datablock1.shape[1]

                self.dataOut.data = buffer
                self.profileIndex = blockIndex

            self.dataOut.flagDataAsBlock = True
            self.dataOut.nProfiles = self.dataOut.data.shape[1]

        self.dataOut.flagNoData = False

        self.getBasicHeader()

        self.dataOut.realtime = self.online

        return self.dataOut.data
Пример #12
0
    def __setHeaderDO(self):

        self.dataOut.radarControllerHeaderObj = RadarControllerHeader()

        self.dataOut.systemHeaderObj = SystemHeader()

        #---------------------------------------------------------
        self.dataOut.systemHeaderObj.nProfiles = 100
        self.dataOut.systemHeaderObj.nSamples = 1000

        SAMPLING_STRUCTURE = [('h0', '<f4'), ('dh', '<f4'), ('nsa', '<u4')]
        self.dataOut.radarControllerHeaderObj.samplingWindow = numpy.zeros(
            (1, ), SAMPLING_STRUCTURE)
        self.dataOut.radarControllerHeaderObj.samplingWindow['h0'] = 0
        self.dataOut.radarControllerHeaderObj.samplingWindow['dh'] = 1.5
        self.dataOut.radarControllerHeaderObj.samplingWindow['nsa'] = 1000
        self.dataOut.radarControllerHeaderObj.nHeights = int(
            self.dataOut.radarControllerHeaderObj.samplingWindow['nsa'])
        self.dataOut.radarControllerHeaderObj.firstHeight = self.dataOut.radarControllerHeaderObj.samplingWindow[
            'h0']
        self.dataOut.radarControllerHeaderObj.deltaHeight = self.dataOut.radarControllerHeaderObj.samplingWindow[
            'dh']
        self.dataOut.radarControllerHeaderObj.samplesWin = self.dataOut.radarControllerHeaderObj.samplingWindow[
            'nsa']

        self.dataOut.radarControllerHeaderObj.nWindows = 1
        self.dataOut.radarControllerHeaderObj.codetype = 0
        self.dataOut.radarControllerHeaderObj.numTaus = 0
        #self.dataOut.radarControllerHeaderObj.Taus = numpy.zeros((1,),'<f4')

        #self.dataOut.radarControllerHeaderObj.nCode=numpy.zeros((1,), '<u4')
        #self.dataOut.radarControllerHeaderObj.nBaud=numpy.zeros((1,), '<u4')
        #self.dataOut.radarControllerHeaderObj.code=numpy.zeros(0)

        self.dataOut.radarControllerHeaderObj.code_size = 0
        self.dataOut.nBaud = 0
        self.dataOut.nCode = 0
        self.dataOut.nPairs = 0

        #---------------------------------------------------------

        self.dataOut.type = "Voltage"

        self.dataOut.data = None

        self.dataOut.dtype = numpy.dtype([('real', '<f4'), ('imag', '<f4')])

        self.dataOut.nProfiles = 1

        self.dataOut.heightList = self.__firstHeigth + numpy.arange(
            self.__nSamples, dtype=numpy.float) * self.__deltaHeigth

        self.dataOut.channelList = list(range(self.nChannels))

        #self.dataOut.channelIndexList = None

        self.dataOut.flagNoData = True

        #Set to TRUE if the data is discontinuous
        self.dataOut.flagDiscontinuousBlock = False

        self.dataOut.utctime = None

        self.dataOut.timeZone = self.timezone

        self.dataOut.dstFlag = 0

        self.dataOut.errorCount = 0

        self.dataOut.nCohInt = 1

        self.dataOut.blocksize = self.dataOut.nChannels * self.dataOut.nHeights

        self.dataOut.flagDecodeData = False  #asumo que la data esta decodificada

        self.dataOut.flagDeflipData = False  #asumo que la data esta sin flip

        self.dataOut.flagShiftFFT = False

        self.dataOut.ippSeconds = 1.0 * self.__nSamples / self.__sample_rate

        #Time interval between profiles
        #self.dataOut.timeInterval =self.dataOut.ippSeconds * self.dataOut.nCohInt

        self.dataOut.frequency = self.__frequency

        self.dataOut.realtime = self.__online
Пример #13
0
class SimulatorReader(JRODataReader, ProcessingUnit):
    incIntFactor                   = 1
    nFFTPoints                     = 0
    FixPP_IncInt                   = 1
    FixRCP_IPP                     = 1000
    FixPP_CohInt                   = 1
    Tau_0                          = 250
    AcqH0_0                        = 70
    H0                             = AcqH0_0
    AcqDH_0                        = 1.25
    DH0                            = AcqDH_0
    Bauds                          = 32
    BaudWidth                      = None
    FixRCP_TXA                     = 40
    FixRCP_TXB                     = 70
    fAngle                         = 2.0*math.pi*(1/16)
    DC_level                       = 500
    stdev                          = 8
    Num_Codes                      = 2
    #code0                          = numpy.array([1,1,1,0,1,1,0,1,1,1,1,0,0,0,1,0,1,1,1,0,1,1,0,1,0,0,0,1,1,1,0,1])
    #code1                          = numpy.array([1,1,1,0,1,1,0,1,1,1,1,0,0,0,1,0,0,0,0,1,0,0,1,0,1,1,1,0,0,0,1,0])
    #Dyn_snCode                     = numpy.array([Num_Codes,Bauds])
    Dyn_snCode                     = None
    Samples                        = 200
    channels                       = 2
    pulses                         = None
    Reference                      = None
    pulse_size                     = None
    prof_gen                       = None
    Fdoppler                       = 100
    Hdoppler                       = 36
    Adoppler                       = 300
    frequency                      = 9345
    nTotalReadFiles                = 1000

    def __init__(self):
        """
        Inicializador de la clases SimulatorReader para
        generar datos de voltage simulados.
        Input:
            dataOut: Objeto de la clase Voltage.
            Este Objeto sera utilizado apra almacenar
            un perfil de datos  cada vez qe se haga
            un requerimiento (getData)
        """
        ProcessingUnit.__init__(self)
        print(" [ START ] init - Metodo Simulator Reader")

        self.isConfig                  = False
        self.basicHeaderObj            = BasicHeader(LOCALTIME)
        self.systemHeaderObj           = SystemHeader()
        self.radarControllerHeaderObj  = RadarControllerHeader()
        self.processingHeaderObj       = ProcessingHeader()
        self.profileIndex              = 2**32-1
        self.dataOut                   = Voltage()
        #code0                          = numpy.array([1,1,1,0,1,1,0,1,1,1,1,0,0,0,1,0,1,1,1,0,1,1,0,1,0,0,0,1,1,1,0,1])
        code0                          = numpy.array([1,1,1,-1,1,1,-1,1,1,1,1,-1,-1,-1,1,-1,1,1,1,-1,1,1,-1,1,-1,-1,-1,1,1,1,-1,1])
        #code1                          = numpy.array([1,1,1,0,1,1,0,1,1,1,1,0,0,0,1,0,0,0,0,1,0,0,1,0,1,1,1,0,0,0,1,0])
        code1                          = numpy.array([1,1,1,-1,1,1,-1,1,1,1,1,-1,-1,-1,1,-1,-1,-1,-1,1,-1,-1,1,-1,1,1,1,-1,-1,-1,1,-1])
        #self.Dyn_snCode                = numpy.array([code0,code1])
        self.Dyn_snCode                = None

    def set_kwargs(self, **kwargs):
        for key, value in kwargs.items():
            setattr(self, key, value)

    def __hasNotDataInBuffer(self):

        if self.profileIndex >= self.processingHeaderObj.profilesPerBlock* self.nTxs:
            if self.nReadBlocks>0:
                tmp                           = self.dataOut.utctime
                tmp_utc                       = int(self.dataOut.utctime)
                tmp_milisecond                = int((tmp-tmp_utc)*1000)
                self.basicHeaderObj.utc       = tmp_utc
                self.basicHeaderObj.miliSecond= tmp_milisecond
            return 1
        return 0

    def setNextFile(self):
        """Set the next file to be readed open it and parse de file header"""

        if (self.nReadBlocks >= self.processingHeaderObj.dataBlocksPerFile):
            self.nReadFiles=self.nReadFiles+1
            if self.nReadFiles > self.nTotalReadFiles:
                self.flagNoMoreFiles=1
                raise schainpy.admin.SchainWarning('No more files to read')

            print('------------------- [Opening file] ------------------------------',self.nReadFiles)
            self.nReadBlocks  = 0
        #if self.nReadBlocks==0:
        #    self.readFirstHeader()

    def __setNewBlock(self):
        self.setNextFile()
        if self.flagIsNewFile:
            return 1

    def readNextBlock(self):
        while True:
            self.__setNewBlock()
            if not(self.readBlock()):
                return 0
            self.getBasicHeader()
            break
        if self.verbose:
            print("[Reading] Block No. %d/%d -> %s" %(self.nReadBlocks,
                                                      self.processingHeaderObj.dataBlocksPerFile,
                                                      self.dataOut.datatime.ctime()) )
        return 1

    def getFirstHeader(self):
        self.getBasicHeader()
        self.dataOut.processingHeaderObj      = self.processingHeaderObj.copy()
        self.dataOut.systemHeaderObj          = self.systemHeaderObj.copy()
        self.dataOut.radarControllerHeaderObj = self.radarControllerHeaderObj.copy()
        self.dataOut.dtype       = self.dtype

        self.dataOut.nProfiles   = self.processingHeaderObj.profilesPerBlock
        self.dataOut.heightList  = numpy.arange(self.processingHeaderObj.nHeights) * self.processingHeaderObj.deltaHeight + self.processingHeaderObj.firstHeight
        self.dataOut.channelList = list(range(self.systemHeaderObj.nChannels))
        self.dataOut.nCohInt     = self.processingHeaderObj.nCohInt
        # asumo q la data no esta decodificada
        self.dataOut.flagDecodeData  = self.processingHeaderObj.flag_decode
        # asumo q la data no esta sin flip
        self.dataOut.flagDeflipData  = self.processingHeaderObj.flag_deflip
        self.dataOut.flagShiftFFT    = self.processingHeaderObj.shif_fft
        self.dataOut.frequency       = self.frequency

    def getBasicHeader(self):
        self.dataOut.utctime = self.basicHeaderObj.utc + self.basicHeaderObj.miliSecond / \
            1000. + self.profileIndex * self.radarControllerHeaderObj.ippSeconds

        self.dataOut.flagDiscontinuousBlock = self.flagDiscontinuousBlock
        self.dataOut.timeZone               = self.basicHeaderObj.timeZone
        self.dataOut.dstFlag                = self.basicHeaderObj.dstFlag
        self.dataOut.errorCount             = self.basicHeaderObj.errorCount
        self.dataOut.useLocalTime           = self.basicHeaderObj.useLocalTime
        self.dataOut.ippSeconds             = self.radarControllerHeaderObj.ippSeconds / self.nTxs

    def readFirstHeader(self):

        datatype = int(numpy.log2((self.processingHeaderObj.processFlags &
                                   PROCFLAG.DATATYPE_MASK)) - numpy.log2(PROCFLAG.DATATYPE_CHAR))
        if datatype == 0:
            datatype_str = numpy.dtype([('real', '<i1'), ('imag', '<i1')])
        elif datatype == 1:
            datatype_str = numpy.dtype([('real', '<i2'), ('imag', '<i2')])
        elif datatype == 2:
            datatype_str = numpy.dtype([('real', '<i4'), ('imag', '<i4')])
        elif datatype == 3:
            datatype_str = numpy.dtype([('real', '<i8'), ('imag', '<i8')])
        elif datatype == 4:
            datatype_str = numpy.dtype([('real', '<f4'), ('imag', '<f4')])
        elif datatype == 5:
            datatype_str = numpy.dtype([('real', '<f8'), ('imag', '<f8')])
        else:
            raise ValueError('Data type was not defined')

        self.dtype = datatype_str


    def set_RCH(self, expType=2, nTx=1,ipp=None, txA=0, txB=0,
                 nWindows=None, nHeights=None, firstHeight=None, deltaHeight=None,
                 numTaus=0, line6Function=0, line5Function=0, fClock=None,
                 prePulseBefore=0, prePulseAfter=0,
                 codeType=0, nCode=0, nBaud=0, code=None,
                 flip1=0, flip2=0,Taus=0):
        self.radarControllerHeaderObj.expType       = expType
        self.radarControllerHeaderObj.nTx           = nTx
        self.radarControllerHeaderObj.ipp           = float(ipp)
        self.radarControllerHeaderObj.txA           = float(txA)
        self.radarControllerHeaderObj.txB           = float(txB)
        self.radarControllerHeaderObj.rangeIpp      = b'A\n'#ipp
        self.radarControllerHeaderObj.rangeTxA      = b''
        self.radarControllerHeaderObj.rangeTxB      = b''

        self.radarControllerHeaderObj.nHeights      = int(nHeights)
        self.radarControllerHeaderObj.firstHeight   = numpy.array([firstHeight])
        self.radarControllerHeaderObj.deltaHeight   = numpy.array([deltaHeight])
        self.radarControllerHeaderObj.samplesWin    = numpy.array([nHeights])


        self.radarControllerHeaderObj.nWindows      = nWindows
        self.radarControllerHeaderObj.numTaus       = numTaus
        self.radarControllerHeaderObj.codeType      = codeType
        self.radarControllerHeaderObj.line6Function = line6Function
        self.radarControllerHeaderObj.line5Function = line5Function
        #self.radarControllerHeaderObj.fClock        = fClock
        self.radarControllerHeaderObj.prePulseBefore= prePulseBefore
        self.radarControllerHeaderObj.prePulseAfter = prePulseAfter

        self.radarControllerHeaderObj.flip1         = flip1
        self.radarControllerHeaderObj.flip2         = flip2

        self.radarControllerHeaderObj.code_size     = 0
        if self.radarControllerHeaderObj.codeType  != 0:
            self.radarControllerHeaderObj.nCode         = nCode
            self.radarControllerHeaderObj.nBaud         = nBaud
            self.radarControllerHeaderObj.code          = code
            self.radarControllerHeaderObj.code_size     = int(numpy.ceil(nBaud / 32.)) * nCode * 4

        if fClock is None and deltaHeight is not None:
            self.fClock = 0.15 / (deltaHeight * 1e-6)
            self.radarControllerHeaderObj.fClock     = self.fClock
        if numTaus==0:
            self.radarControllerHeaderObj.Taus       =  numpy.array(0,'<f4')
        else:
            self.radarControllerHeaderObj.Taus       = numpy.array(Taus,'<f4')

    def set_PH(self, dtype=0, blockSize=0, profilesPerBlock=0,
                  dataBlocksPerFile=0, nWindows=0, processFlags=0, nCohInt=0,
                  nIncohInt=0, totalSpectra=0, nHeights=0, firstHeight=0,
                  deltaHeight=0, samplesWin=0, spectraComb=0, nCode=0,
                  code=0, nBaud=None, shif_fft=False, flag_dc=False,
                  flag_cspc=False, flag_decode=False, flag_deflip=False):

        self.processingHeaderObj.dtype             = dtype
        self.processingHeaderObj.profilesPerBlock  = profilesPerBlock
        self.processingHeaderObj.dataBlocksPerFile = dataBlocksPerFile
        self.processingHeaderObj.nWindows          = nWindows
        self.processingHeaderObj.processFlags      = processFlags
        self.processingHeaderObj.nCohInt           = nCohInt
        self.processingHeaderObj.nIncohInt         = nIncohInt
        self.processingHeaderObj.totalSpectra      = totalSpectra

        self.processingHeaderObj.nHeights          = int(nHeights)
        self.processingHeaderObj.firstHeight       = firstHeight#numpy.array([firstHeight])#firstHeight
        self.processingHeaderObj.deltaHeight       = deltaHeight#numpy.array([deltaHeight])#deltaHeight
        self.processingHeaderObj.samplesWin        = nHeights#numpy.array([nHeights])#nHeights

    def set_BH(self, utc = 0, miliSecond = 0, timeZone = 0):
        self.basicHeaderObj.utc                    = utc
        self.basicHeaderObj.miliSecond             = miliSecond
        self.basicHeaderObj.timeZone               = timeZone

    def set_SH(self, nSamples=0, nProfiles=0, nChannels=0, adcResolution=14, pciDioBusWidth=32):
        #self.systemHeaderObj.size           = size
        self.systemHeaderObj.nSamples       = nSamples
        self.systemHeaderObj.nProfiles      = nProfiles
        self.systemHeaderObj.nChannels      = nChannels
        self.systemHeaderObj.adcResolution  = adcResolution
        self.systemHeaderObj.pciDioBusWidth = pciDioBusWidth

    def init_acquisition(self):

        if self.nFFTPoints != 0:
            self.incIntFactor = m_nProfilesperBlock/self.nFFTPoints
            if (self.FixPP_IncInt > self.incIntFactor):
                self.incIntFactor = self.FixPP_IncInt/ self.incIntFactor
            elif(self.FixPP_IncInt< self.incIntFactor):
                print("False alert...")

        ProfilesperBlock  = self.processingHeaderObj.profilesPerBlock

        self.timeperblock =int(((self.FixRCP_IPP
                        *ProfilesperBlock
                        *self.FixPP_CohInt
                        *self.incIntFactor)
                       /150.0)
                      *0.9
                      +0.5)
        # para cada canal
        self.profiles     =  ProfilesperBlock*self.FixPP_CohInt
        self.profiles     =  ProfilesperBlock
        self.Reference    =  int((self.Tau_0-self.AcqH0_0)/(self.AcqDH_0)+0.5)
        self.BaudWidth    =  int((self.FixRCP_TXA/self.AcqDH_0)/self.Bauds + 0.5 )

        if (self.BaudWidth==0):
            self.BaudWidth=1

    def init_pulse(self,Num_Codes=Num_Codes,Bauds=Bauds,BaudWidth=BaudWidth,Dyn_snCode=Dyn_snCode):

        Num_Codes   = Num_Codes
        Bauds       = Bauds
        BaudWidth   = BaudWidth
        Dyn_snCode  = Dyn_snCode

        if Dyn_snCode:
            print("EXISTE")
        else:
            print("No existe")

        if Dyn_snCode: # if Bauds:
            pulses         = list(range(0,Num_Codes))
            num_codes      = Num_Codes
            for i in range(num_codes):
                pulse_size = Bauds*BaudWidth
                pulses[i]  = numpy.zeros(pulse_size)
                for j in range(Bauds):
                    for k in range(BaudWidth):
                        pulses[i][j*BaudWidth+k] = int(Dyn_snCode[i][j]*600)
        else:
            print("sin code")
            pulses     = list(range(1))
            if self.AcqDH_0>0.149:
                pulse_size = int(self.FixRCP_TXB/0.15+0.5)
            else:
                pulse_size = int((self.FixRCP_TXB/self.AcqDH_0)+0.5) #0.0375
            pulses[0]  = numpy.ones(pulse_size)
            pulses     = 600*pulses[0]

        return pulses,pulse_size

    def jro_GenerateBlockOfData(self,Samples=Samples,DC_level= DC_level,stdev=stdev,
                                Reference= Reference,pulses= pulses,
                                Num_Codes= Num_Codes,pulse_size=pulse_size,
                                prof_gen= prof_gen,H0 = H0,DH0=DH0,
                                Adoppler=Adoppler,Fdoppler= Fdoppler,Hdoppler=Hdoppler):
        Samples    = Samples
        DC_level   = DC_level
        stdev      = stdev
        m_nR       = Reference
        pulses     = pulses
        num_codes  = Num_Codes
        ps         = pulse_size
        prof_gen   = prof_gen
        channels   = self.channels
        H0         = H0
        DH0        = DH0
        ippSec     = self.radarControllerHeaderObj.ippSeconds
        Fdoppler   = self.Fdoppler
        Hdoppler   = self.Hdoppler
        Adoppler   = self.Adoppler

        self.datablock = numpy.zeros([channels,prof_gen,Samples],dtype= numpy.complex64)
        for i in range(channels):
            for k in range(prof_gen):
                #-----------------------NOISE---------------
                Noise_r    = numpy.random.normal(DC_level,stdev,Samples)
                Noise_i    = numpy.random.normal(DC_level,stdev,Samples)
                Noise      = numpy.zeros(Samples,dtype=complex)
                Noise.real = Noise_r
                Noise.imag = Noise_i
                #-----------------------PULSOS--------------
                Pulso      = numpy.zeros(pulse_size,dtype=complex)
                Pulso.real =  pulses[k%num_codes]
                Pulso.imag =  pulses[k%num_codes]
                #--------------------- PULSES+NOISE----------
                InBuffer                    = numpy.zeros(Samples,dtype=complex)
                InBuffer[m_nR:m_nR+ps]      = Pulso
                InBuffer                    =  InBuffer+Noise
                #--------------------- ANGLE -------------------------------
                InBuffer.real[m_nR:m_nR+ps] = InBuffer.real[m_nR:m_nR+ps]*(math.cos( self.fAngle)*5)
                InBuffer.imag[m_nR:m_nR+ps] = InBuffer.imag[m_nR:m_nR+ps]*(math.sin( self.fAngle)*5)
                InBuffer=InBuffer
                self.datablock[i][k]= InBuffer

        #----------------DOPPLER SIGNAL...............................................
        time_vec   = numpy.linspace(0,(prof_gen-1)*ippSec,int(prof_gen))+self.nReadBlocks*ippSec*prof_gen+(self.nReadFiles-1)*ippSec*prof_gen
        fd         = Fdoppler #+(600.0/120)*self.nReadBlocks
        d_signal   = Adoppler*numpy.array(numpy.exp(1.0j*2.0*math.pi*fd*time_vec),dtype=numpy.complex64)
        #-------------Senal con ancho espectral--------------------
        if prof_gen%2==0:
            min = int(prof_gen/2.0-1.0)
            max = int(prof_gen/2.0)
        else:
            min = int(prof_gen/2.0)
            max = int(prof_gen/2.0)
        specw_sig  = numpy.linspace(-min,max,prof_gen)
        w          = 4
        A          = 20
        specw_sig   = specw_sig/w
        specw_sig   = numpy.sinc(specw_sig)
        specw_sig   =  A*numpy.array(specw_sig,dtype=numpy.complex64)
        #------------------ DATABLOCK + DOPPLER--------------------
        HD=int(Hdoppler/self.AcqDH_0)
        for  i in range(12):
            self.datablock[0,:,HD+i]=self.datablock[0,:,HD+i]+ d_signal# RESULT
        #------------------ DATABLOCK + DOPPLER*Sinc(x)--------------------
        HD=int(Hdoppler/self.AcqDH_0)
        HD=int(HD/2)
        for  i in range(12):
            self.datablock[0,:,HD+i]=self.datablock[0,:,HD+i]+ specw_sig*d_signal# RESULT

    def readBlock(self):

        self.jro_GenerateBlockOfData(Samples= self.samples,DC_level=self.DC_level,
                                     stdev=self.stdev,Reference= self.Reference,
                                     pulses = self.pulses,Num_Codes=self.Num_Codes,
                                     pulse_size=self.pulse_size,prof_gen=self.profiles,
                                     H0=self.H0,DH0=self.DH0)

        self.profileIndex   = 0
        self.flagIsNewFile  = 0
        self.flagIsNewBlock = 1
        self.nTotalBlocks  += 1
        self.nReadBlocks   += 1

        return 1


    def getData(self):
        if self.flagNoMoreFiles:
            self.dataOut.flagNodata = True
            return 0
        self.flagDiscontinuousBlock = 0
        self.flagIsNewBlock         = 0
        if self.__hasNotDataInBuffer():   # aqui es verdad
            if not(self.readNextBlock()): # return 1 y por eso el if not salta  a getBasic Header
                return 0
            self.getFirstHeader()         # atributo

        if not self.getByBlock:
            self.dataOut.flagDataAsBlock = False
            self.dataOut.data = self.datablock[:, self.profileIndex, :]
            self.dataOut.profileIndex = self.profileIndex
            self.profileIndex += 1
        else:
            pass
        self.dataOut.flagNoData = False
        self.getBasicHeader()
        self.dataOut.realtime = self.online
        return self.dataOut.data


    def setup(self,frequency=49.92e6,incIntFactor= 1, nFFTPoints = 0, FixPP_IncInt=1,FixRCP_IPP=1000,
                   FixPP_CohInt= 1,Tau_0= 250,AcqH0_0 = 70 ,AcqDH_0=1.25, Bauds= 32,
                   FixRCP_TXA = 40, FixRCP_TXB = 50, fAngle = 2.0*math.pi*(1/16),DC_level= 50,
                   stdev= 8,Num_Codes = 1 , Dyn_snCode = None, samples=200,
                   channels=2,Fdoppler=20,Hdoppler=36,Adoppler=500,
                   profilesPerBlock=300,dataBlocksPerFile=120,nTotalReadFiles=10000,
                   **kwargs):

        self.set_kwargs(**kwargs)
        self.nReadBlocks = 0
        self.nReadFiles  = 1
        print('------------------- [Opening file: ] ------------------------------',self.nReadFiles)

        tmp              = time.time()
        tmp_utc          = int(tmp)
        tmp_milisecond   = int((tmp-tmp_utc)*1000)
        print(" SETUP -basicHeaderObj.utc",datetime.datetime.utcfromtimestamp(tmp))
        if Dyn_snCode is None:
            Num_Codes=1
            Bauds    =1



        self.set_BH(utc= tmp_utc,miliSecond= tmp_milisecond,timeZone=300 )
        self.set_RCH( expType=0, nTx=150,ipp=FixRCP_IPP, txA=FixRCP_TXA, txB= FixRCP_TXB,
                 nWindows=1 , nHeights=samples, firstHeight=AcqH0_0, deltaHeight=AcqDH_0,
                 numTaus=1, line6Function=0, line5Function=0, fClock=None,
                 prePulseBefore=0, prePulseAfter=0,
                 codeType=0, nCode=Num_Codes, nBaud=32, code=Dyn_snCode,
                 flip1=0, flip2=0,Taus=Tau_0)

        self.set_PH(dtype=0, blockSize=0, profilesPerBlock=profilesPerBlock,
                      dataBlocksPerFile=dataBlocksPerFile, nWindows=1, processFlags=numpy.array([1024]), nCohInt=1,
                      nIncohInt=1, totalSpectra=0, nHeights=samples, firstHeight=AcqH0_0,
                      deltaHeight=AcqDH_0, samplesWin=samples, spectraComb=0, nCode=0,
                      code=0, nBaud=None, shif_fft=False, flag_dc=False,
                      flag_cspc=False, flag_decode=False, flag_deflip=False)

        self.set_SH(nSamples=samples, nProfiles=profilesPerBlock, nChannels=channels)

        self.readFirstHeader()

        self.frequency                      = frequency
        self.incIntFactor                   = incIntFactor
        self.nFFTPoints                     = nFFTPoints
        self.FixPP_IncInt                   = FixPP_IncInt
        self.FixRCP_IPP                     = FixRCP_IPP
        self.FixPP_CohInt                   = FixPP_CohInt
        self.Tau_0                          = Tau_0
        self.AcqH0_0                        = AcqH0_0
        self.H0                             = AcqH0_0
        self.AcqDH_0                        = AcqDH_0
        self.DH0                            = AcqDH_0
        self.Bauds                          = Bauds
        self.FixRCP_TXA                     = FixRCP_TXA
        self.FixRCP_TXB                     = FixRCP_TXB
        self.fAngle                         = fAngle
        self.DC_level                       = DC_level
        self.stdev                          = stdev
        self.Num_Codes                      = Num_Codes
        self.Dyn_snCode                     = Dyn_snCode
        self.samples                        = samples
        self.channels                       = channels
        self.profiles                       = None
        self.m_nReference                   = None
        self.Baudwidth                      = None
        self.Fdoppler                       = Fdoppler
        self.Hdoppler                       = Hdoppler
        self.Adoppler                       = Adoppler
        self.nTotalReadFiles                = int(nTotalReadFiles)

        print("IPP    ", self.FixRCP_IPP)
        print("Tau_0  ",self.Tau_0)
        print("AcqH0_0",self.AcqH0_0)
        print("samples,window ",self.samples)
        print("AcqDH_0",AcqDH_0)
        print("FixRCP_TXA",self.FixRCP_TXA)
        print("FixRCP_TXB",self.FixRCP_TXB)
        print("Dyn_snCode",Dyn_snCode)
        print("Fdoppler", Fdoppler)
        print("Hdoppler",Hdoppler)
        print("Vdopplermax",Fdoppler*(3.0e8/self.frequency)/2.0)
        print("nTotalReadFiles", nTotalReadFiles)

        self.init_acquisition()
        self.pulses,self.pulse_size=self.init_pulse(Num_Codes=self.Num_Codes,Bauds=self.Bauds,BaudWidth=self.BaudWidth,Dyn_snCode=Dyn_snCode)
        print(" [ END ] - SETUP metodo")
        return

    def run(self,**kwargs): # metodo propio
        if not(self.isConfig):
            self.setup(**kwargs)
            self.isConfig = True
        self.getData()
Пример #14
0
def isFileInTimeRange(filename, startDate, endDate, startTime, endTime):
    """
    Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.

    Inputs:
        filename            :    nombre completo del archivo de datos en formato Jicamarca (.r)

        startDate          :    fecha inicial del rango seleccionado en formato datetime.date

        endDate            :    fecha final del rango seleccionado en formato datetime.date

        startTime          :    tiempo inicial del rango seleccionado en formato datetime.time

        endTime            :    tiempo final del rango seleccionado en formato datetime.time

    Return:
        Boolean    :    Retorna True si el archivo de datos contiene datos en el rango de
                        fecha especificado, de lo contrario retorna False.

    Excepciones:
        Si el archivo no existe o no puede ser abierto
        Si la cabecera no puede ser leida.

    """

    try:
        fp = open(filename, 'rb')
    except IOError:
        print("The file %s can't be opened" % (filename))
        return None

    firstBasicHeaderObj = BasicHeader(LOCALTIME)
    systemHeaderObj = SystemHeader()
    radarControllerHeaderObj = RadarControllerHeader()
    processingHeaderObj = ProcessingHeader()

    lastBasicHeaderObj = BasicHeader(LOCALTIME)

    sts = firstBasicHeaderObj.read(fp)

    if not (sts):
        print(
            "[Reading] Skipping the file %s because it has not a valid header"
            % (filename))
        return None

    if not systemHeaderObj.read(fp):
        return None

    if not radarControllerHeaderObj.read(fp):
        return None

    if not processingHeaderObj.read(fp):
        return None

    filesize = os.path.getsize(filename)

    offset = processingHeaderObj.blockSize + 24  # header size

    if filesize <= offset:
        print("[Reading] %s: This file has not enough data" % filename)
        return None

    fp.seek(-offset, 2)

    sts = lastBasicHeaderObj.read(fp)

    fp.close()

    thisDatetime = lastBasicHeaderObj.datatime
    thisTime_last_block = thisDatetime.time()

    thisDatetime = firstBasicHeaderObj.datatime
    thisDate = thisDatetime.date()
    thisTime_first_block = thisDatetime.time()

    # General case
    #           o>>>>>>>>>>>>>><<<<<<<<<<<<<<o
    #-----------o----------------------------o-----------
    #       startTime                     endTime

    if endTime >= startTime:
        if (thisTime_last_block < startTime) or (thisTime_first_block >
                                                 endTime):
            return None

        return thisDatetime

    # If endTime < startTime then endTime belongs to the next day

    #<<<<<<<<<<<o                            o>>>>>>>>>>>
    #-----------o----------------------------o-----------
    #        endTime                    startTime

    if (thisDate == startDate) and (thisTime_last_block < startTime):
        return None

    if (thisDate == endDate) and (thisTime_first_block > endTime):
        return None

    if (thisTime_last_block < startTime) and (thisTime_first_block > endTime):
        return None

    return thisDatetime