def _save(self, file_to_write, overwrite):
        """Save PDS3Image object as PDS3 file.

        Parameters
        ----------
        filename: Set filename for the pds image to be saved.
        Overwrite: Use this keyword to save image with same filename.

        Usage: image.save('temp.IMG', overwrite=True)

        """
        if overwrite:
            file_to_write = self.filename
        elif os.path.isfile(file_to_write):
            msg = 'File ' + file_to_write + ' already exists !\n' + \
                  'Call save() with "overwrite = True" to overwrite the file.'
            raise IOError(msg)

        encoder = pvl.encoder.PDSLabelEncoder
        serial_label = pvl.dumps(self.label, cls=encoder)
        label_sz = len(serial_label)
        image_pointer = int(label_sz / self.label['RECORD_BYTES']) + 1
        self.label['^IMAGE'] = image_pointer + 1

        if self._sample_bytes != self.label['IMAGE']['SAMPLE_BITS'] * 8:
            self.label['IMAGE']['SAMPLE_BITS'] = self.data.itemsize * 8

        sample_type_to_save = self.DTYPES[self._sample_type[0] + self.dtype.kind]
        self.label['IMAGE']['SAMPLE_TYPE'] = sample_type_to_save

        if len(self.data.shape) == 3:
            self.label['IMAGE']['BANDS'] = self.data.shape[0]
            self.label['IMAGE']['LINES'] = self.data.shape[1]
            self.label['IMAGE']['LINE_SAMPLES'] = self.data.shape[2]
        else:
            self.label['IMAGE']['BANDS'] = 1
            self.label['IMAGE']['LINES'] = self.data.shape[0]
            self.label['IMAGE']['LINE_SAMPLES'] = self.data.shape[1]

        diff = 0
        if len(pvl.dumps(self.label, cls=encoder)) != label_sz:
            diff = abs(label_sz - len(pvl.dumps(self.label, cls=encoder)))
        pvl.dump(self.label, file_to_write, cls=encoder)
        offset = image_pointer * self.label['RECORD_BYTES'] - label_sz
        stream = open(file_to_write, 'a')
        for i in range(0, offset+diff):
            stream.write(" ")

        if (self._bands > 1 and self._format != 'BAND_SEQUENTIAL'):
            raise NotImplementedError
        else:
            self.data.tofile(stream, format='%' + self.dtype.kind)
        stream.close()
Save PDS3Image object as PDS3 file.

        Parameters
        ----------
        filename: Set filename for the pds image to be saved.
        Overwrite: Use this keyword to save image with same filename.

        Usage: image.save('temp.IMG', overwrite=True)