def compare_bluefiles(file1=None, file2=None):
    """
    Compares 2 X-midas bluefiles.  It first checks to make sure the number of 
    elements are the same.  If they are not the same it returns false.  If they 
    are the same then it compares item by item.  If at least one of the items
    does not match it prints the index number and the values and returns False
    AFTER iterating through the whole data set otherwise it returns True.
    
    Inputs:
            <file1>    The first X-midas file to compare
            <file2>    The second X-midas file to compare
    Output:
        True if each element in the data in both files matches. 
    """
    msg = None
    # making sure the files exists
    if file1 == None:
        msg = '\nThe first file cannot be None\n'
    if file2 == None:
        msg = '\nThe second file cannot be None\n'
    if not os.path.isfile(file1):
        msg = '\nThe file %s is either invalid or it does not exists\n' % file1
    if not os.path.isfile(file2):
        msg = '\nThe file %s is either invalid or it does not exists\n' % file2

    if msg != None:
        print msg
        return False
    
    hdr1, data1 = bluefile.read(file1)
    hdr2, data2 = bluefile.read(file2)      

    if hdr1['format'].startswith('C'):
        data1 = data1.flatten()

    if hdr2['format'].startswith('C'):
        data2 = data2.flatten()

    sz1 = len(data1)
    sz2 = len(data2)
      
    # check the number of elements
    if sz1 != sz2:
        print "Files does not contain the same amount of items"
        return False
    
    are_the_same = True
    # check each element in the data
    for i, item1, item2 in zip(range(0, sz1), data1, data2):
        if item1 != item2:
            print ("Item[%d]:\t%s  <==>   %s are not the same" % 
                                            (i, str(item1), str(item2)))
            are_the_same = False
    
    return are_the_same
def compare_bluefiles(file1=None, file2=None):
    """
    Compares 2 X-midas bluefiles.  It first checks to make sure the number of 
    elements are the same.  If they are not the same it returns false.  If they 
    are the same then it compares item by item.  If at least one of the items
    does not match it prints the index number and the values and returns False
    AFTER iterating through the whole data set otherwise it returns True.
    
    Inputs:
            <file1>    The first X-midas file to compare
            <file2>    The second X-midas file to compare
    Output:
        True if each element in the data in both files matches. 
    """
    msg = None
    # making sure the files exists
    if file1 == None:
        msg = '\nThe first file cannot be None\n'
    if file2 == None:
        msg = '\nThe second file cannot be None\n'
    if not os.path.isfile(file1):
        msg = '\nThe file %s is either invalid or it does not exists\n' % file1
    if not os.path.isfile(file2):
        msg = '\nThe file %s is either invalid or it does not exists\n' % file2

    if msg != None:
        print msg
        return False

    hdr1, data1 = bluefile.read(file1)
    hdr2, data2 = bluefile.read(file2)

    if hdr1['format'].startswith('C'):
        data1 = data1.flatten()

    if hdr2['format'].startswith('C'):
        data2 = data2.flatten()

    sz1 = len(data1)
    sz2 = len(data2)

    # check the number of elements
    if sz1 != sz2:
        print "Files does not contain the same amount of items"
        return False

    are_the_same = True
    # check each element in the data
    for i, item1, item2 in zip(range(0, sz1), data1, data2):
        if item1 != item2:
            print("Item[%d]:\t%s  <==>   %s are not the same" %
                  (i, str(item1), str(item2)))
            are_the_same = False

    return are_the_same
    def run(self, infile, pktsize=1024, streamID=None):
        """
        Pushes the data through the connected port.  Each packet of data 
        contains no more than pktsize elements.  Once all the elements have 
        been sent, the method sends an empty list with the EOS set to True to 
        indicate the end of the stream.
        
        Inputs:
            <infile>     The name of the X-Midas file containing the data 
                         to push
            <pktsize>    The maximum number of elements to send on each push
            <streamID>   The stream ID to be used, if None, then it defaults to filename 
        """        
        hdr, data = bluefile.read(infile, list)
        # generates a new SRI based on the header of the file
        path, stream_id = os.path.split(infile)
        if streamID == None:
            sri = hdr_to_sri(hdr, stream_id)
        else:
            sri = hdr_to_sri(hdr, streamID)
        self.pushSRI(sri)
        
        start = 0           # stores the start of the packet
        end = start         # stores the end of the packet

        if hdr['format'].startswith('C'):
            data = data.flatten()
            if hdr['format'].endswith('F'):
                data = data.view(float32)
            elif hdr['format'].endswith('D'):
                data = data.view(float64)

        sz = len(data)      
        self.done = False
        
        # Use midas header timecode to set time of first sample
        # NOTE: midas time is seconds since Jan. 1 1950
        #       Redhawk time is seconds since Jan. 1 1970
        currentSampleTime = 0.0
        if hdr.has_key('timecode'):
            # Set sample time to seconds since Jan. 1 1970 
            currentSampleTime = hdr['timecode'] - long(631152000)
            if currentSampleTime < 0:
                currentSampleTime = 0.0
      
        while not self.done:
            chunk = start + pktsize
            # if the next chunk is greater than the file, then grab remaining
            # only, otherwise grab a whole packet size
            if chunk > sz:
                end = sz
                self.done = True
            else:
                end = chunk
            
            dataset = data[start:end]
            
            # X-Midas returns an array, so we need to generate a list
            if hdr['format'].endswith('B'):
                d = dataset.tostring()
            else:
                d = dataset.tolist()
            start = end
            
            T = BULKIO.PrecisionUTCTime(BULKIO.TCM_CPU, BULKIO.TCS_VALID, 0.0, int(currentSampleTime), currentSampleTime - int(currentSampleTime))
            self.pushPacket(d, T, False, sri.streamID)
            dataSize = len(d)
            sampleRate = 1.0/sri.xdelta
            currentSampleTime = currentSampleTime + dataSize/sampleRate
        T = BULKIO.PrecisionUTCTime(BULKIO.TCM_CPU, BULKIO.TCS_VALID, 0.0, int(currentSampleTime), currentSampleTime - int(currentSampleTime))
        if hdr['format'].endswith('B'):
            self.pushPacket('', T, True, sri.streamID)
        else: 
            self.pushPacket([], T, True, sri.streamID)
    def run(self, infile, pktsize=1024, streamID=None):
        """
        Pushes the data through the connected port.  Each packet of data 
        contains no more than pktsize elements.  Once all the elements have 
        been sent, the method sends an empty list with the EOS set to True to 
        indicate the end of the stream.
        
        Inputs:
            <infile>     The name of the X-Midas file containing the data 
                         to push
            <pktsize>    The maximum number of elements to send on each push
            <streamID>   The stream ID to be used, if None, then it defaults to filename 
        """
        hdr, data = bluefile.read(infile, list)
        # generates a new SRI based on the header of the file
        path, stream_id = os.path.split(infile)
        if streamID == None:
            sri = hdr_to_sri(hdr, stream_id)
        else:
            sri = hdr_to_sri(hdr, streamID)
        self.pushSRI(sri)

        start = 0  # stores the start of the packet
        end = start  # stores the end of the packet

        if hdr['format'].startswith('C'):
            data = data.flatten()
            if hdr['format'].endswith('F'):
                data = data.view(float32)
            elif hdr['format'].endswith('D'):
                data = data.view(float64)

        sz = len(data)
        self.done = False

        # Use midas header timecode to set time of first sample
        # NOTE: midas time is seconds since Jan. 1 1950
        #       Redhawk time is seconds since Jan. 1 1970
        currentSampleTime = 0.0
        if hdr.has_key('timecode'):
            # Set sample time to seconds since Jan. 1 1970
            currentSampleTime = hdr['timecode'] - long(631152000)
            if currentSampleTime < 0:
                currentSampleTime = 0.0

        while not self.done:
            chunk = start + pktsize
            # if the next chunk is greater than the file, then grab remaining
            # only, otherwise grab a whole packet size
            if chunk > sz:
                end = sz
                self.done = True
            else:
                end = chunk

            dataset = data[start:end]

            # X-Midas returns an array, so we need to generate a list
            if hdr['format'].endswith('B'):
                d = dataset.tostring()
            else:
                d = dataset.tolist()
            start = end

            T = BULKIO.PrecisionUTCTime(
                BULKIO.TCM_CPU, BULKIO.TCS_VALID, 0.0, int(currentSampleTime),
                currentSampleTime - int(currentSampleTime))
            self.pushPacket(d, T, False, sri.streamID)
            dataSize = len(d)
            sampleRate = 1.0 / sri.xdelta
            currentSampleTime = currentSampleTime + dataSize / sampleRate
        T = BULKIO.PrecisionUTCTime(BULKIO.TCM_CPU, BULKIO.TCS_VALID, 0.0,
                                    int(currentSampleTime),
                                    currentSampleTime - int(currentSampleTime))
        if hdr['format'].endswith('B'):
            self.pushPacket('', T, True, sri.streamID)
        else:
            self.pushPacket([], T, True, sri.streamID)
Exemple #5
0
    def run(self, infile, pktsize=1024, streamID=None):
        """
        Pushes the data through the connected port.  Each packet of data 
        contains no more than pktsize elements.  Once all the elements have 
        been sent, the method sends an empty list with the EOS set to True to 
        indicate the end of the stream.
        
        Inputs:
            <infile>     The name of the X-Midas file containing the data 
                         to push
            <pktsize>    The maximum number of elements to send on each push
            <streamID>   The stream ID to be used, if None, then it defaults to filename 
        """
        hdr, data = bluefile.read(infile, list)
        # generates a new SRI based on the header of the file
        path, stream_id = os.path.split(infile)
        if streamID == None:
            sri = hdr_to_sri(hdr, stream_id)
        else:
            sri = hdr_to_sri(hdr, streamID)
        self.pushSRI(sri)

        start = 0  # stores the start of the packet
        end = start  # stores the end of the packet

        # Flatten framed (type 2000) and/or complex data (where each element
        # may be a 2-tuple)
        if hdr['format'].startswith('C') or hdr['class'] == 2:
            data = numpy.reshape(data, (-1, ))

        # For complex float/double, get a view of the data as the scalar type
        # instead of the complex type
        if hdr['format'] == 'CF':
            data = data.view(numpy.float32)
        elif hdr['format'] == 'CD':
            data = data.view(numpy.float64)

        sz = len(data)
        self.done = False

        # Use midas header timecode to set time of first sample
        # NOTE: midas time is seconds since Jan. 1 1950
        #       Redhawk time is seconds since Jan. 1 1970
        currentSampleTime = 0.0
        if hdr.has_key('timecode'):
            # Set sample time to seconds since Jan. 1 1970
            currentSampleTime = j1950_to_unix(hdr['timecode'])
            if currentSampleTime < 0:
                currentSampleTime = 0.0

        # Quantize packet size to match complex ("spa" is scalars per atom) and
        # framing ("ape" is atoms per element), with a minimum of one frame per
        # packet
        spe = hdr['spa'] * hdr['ape']
        pktsize = max(spe, int(pktsize / spe) * spe)

        # Create a normalized timestamp from the initial sample time
        T = bulkio.timestamp.create(currentSampleTime, 0.0)
        bulkio.timestamp.normalize(T)

        while not self.done:
            chunk = start + pktsize
            # if the next chunk is greater than the file, then grab remaining
            # only, otherwise grab a whole packet size
            if chunk >= sz:
                end = sz
                self.done = True
            else:
                end = chunk

            dataset = data[start:end]

            # X-Midas returns an array, so we need to generate a list
            if hdr['format'].endswith('B'):
                d = dataset.tostring()
            else:
                d = dataset.tolist()
            start = end

            self.pushPacket(d, T, False, sri.streamID)

            # Update the sample time to account for the packet; the data size
            # should always be an integral number of elements (taking subsize
            # and complex into account), but use a float just in case there's
            # a partial packet at the end
            dataSize = len(d) / float(spe)
            # Make a best guess at the time axis, based on the whether it's
            # framed data (and the Y-axis is in units of time), defaulting to
            # the X-axis
            if sri.subsize > 0 and sri.yunits == BULKIO.UNITS_TIME:
                delta = sri.ydelta
            else:
                delta = sri.xdelta
            T += dataSize * delta

        if hdr['format'].endswith('B'):
            self.pushPacket('', T, True, sri.streamID)
        else:
            self.pushPacket([], T, True, sri.streamID)