Commit a5cf81af authored by jay's avatar jay
Browse files

Fixed error where sys.getbytes differs from the protobuf ByteSize, latter is correct.

parent f1b81fbd
Loading
Loading
Loading
Loading
+73 −5
Original line number Diff line number Diff line
@@ -60,15 +60,22 @@ def to_isis(path, C, mode='w', version=VERSION,
        with IsisStore(path, mode) as store:
            point_messages, point_sizes = store.create_points(C)
            points_bytes = sum(point_sizes)
            #store.write()
            buffer_header, buffer_header_size = store.create_buffer_header(C, networkid,
                                                                           targetname,
                                                                           description,
                                                                           username,
                                                                           point_sizes)

            # Write the buffer header
            store.write(buffer_header,HEADERSTARTBYTE)


            # Then write the points, so we know where to start writing, + 1 to avoid overwrite
            point_start_offset = HEADERSTARTBYTE + buffer_header_size
            for i, point in enumerate(point_messages):
                store.write(point, point_start_offset)
                point_start_offset += point_sizes[i]

            header = store.create_pvl_header(C, version, headerstartbyte, networkid,
                                             targetname, description, username,
                                             buffer_header_size, points_bytes)
@@ -102,11 +109,30 @@ class IsisStore(object):
        ----------
        C : object
               A control network object

        offset : int
                 The byte offset into the output binary
        """
        self._handle.seek(offset)
        self._handle.write(data)

    def create_points(self, cnet):
        """
        Step through a C object and return protocol buffer point objects

        Parameters
        ----------
        cnet : object
               A control network object

        Returns
        -------
        point_messages : list
                         of serialized points buffers

        point_sizes : list
                      of integer point sizes
        """
        point_sizes = []
        point_messages = []

@@ -134,7 +160,7 @@ class IsisStore(object):
            point_spec.measures.extend(measure_iterable)

            point_message = point_spec.SerializeToString()
            point_sizes.append(sys.getsizeof(point_message))
            point_sizes.append(point_spec.ByteSize())
            point_messages.append(point_message)

        return point_messages, point_sizes
@@ -150,10 +176,28 @@ class IsisStore(object):
        cnet : object
               A control network object

        networkid : str
                    The user defined identifier of this control network

        targetname : str
                 The name of the target, e.g. Moon

        description : str
                  A description for the network.

        username : str
               The name of the user / application that created the control network

        point_sizes : list
                      of the point sizes for each point message

        Returns
        -------
        message : str
        header_message : str
                  The serialized message to write

        header_message_size : int
                              The size of the serialized header, in bytes
        """
        raw_header_message = cnf.ControlNetFileHeaderV0002()
        raw_header_message.created = cnet.creationdate
@@ -164,8 +208,9 @@ class IsisStore(object):
        raw_header_message.userName = username

        raw_header_message.pointMessageSizes.extend(point_sizes)
        header_message_size = raw_header_message.ByteSize()
        header_message = raw_header_message.SerializeToString()
        header_message_size = sys.getsizeof(header_message)
        #header_message_size = sys.getsizeof(header_message)

        return header_message, header_message_size

@@ -176,14 +221,37 @@ class IsisStore(object):
        Create the PVL header object
        Parameters
        ----------
        cnet : C
        cnet : object
               A control net object

        version : int
              The current ISIS version to write, defaults to 2

        headerstartbyte : int
                          The seek offset that the protocol buffer header starts at

        networkid : str
                    The name of the network

        targetname : str
                     The name of the target, e.g. Moon

        description : str
                      A description for the network.

        username : str
                   The name of the user / application that created the control network

        buffer_header_size : int
                             Total size of the header in bytes

        points_bytes : int
                       The total number of bytes all points require

        Returns
        -------
         : object
           An ISIS compliant PVL header object

        """

+4 −4
Original line number Diff line number Diff line
@@ -41,7 +41,7 @@ class TestWriteIsisControlNetwork(unittest.TestCase):


    def test_create_buffer_header(self):
        header_message_size = 116
        header_message_size = 83
        with open('test.net', 'rb') as f:
            f.seek(io_controlnetwork.HEADERSTARTBYTE)
            raw_header_message = f.read(header_message_size)
@@ -59,7 +59,7 @@ class TestWriteIsisControlNetwork(unittest.TestCase):
            self.assertEqual('Not modified', header_protocol.lastModified)

            #Repeating
            self.assertEqual([64, 56], header_protocol.pointMessageSizes)
            self.assertEqual([31, 23], header_protocol.pointMessageSizes)

    def test_create_point(self):
        with open('test.net', 'rb') as f:
@@ -76,10 +76,10 @@ class TestWriteIsisControlNetwork(unittest.TestCase):
        self.assertEqual(5, mpoints)

        points_bytes = find_in_dict(pvl_header, 'PointsBytes')
        self.assertEqual(120, points_bytes)
        self.assertEqual(54, points_bytes)

        points_start_byte = find_in_dict(pvl_header, 'PointsStartByte')
        self.assertEqual(65652, points_start_byte)
        self.assertEqual(65619, points_start_byte)