Ejemplo n.º 1
0
    def __init__(self,
                 file_name,
                 sicd_meta,
                 user_data=None,
                 check_older_version=False):
        """

        Parameters
        ----------
        file_name : str
        sicd_meta : SICDType
        user_data : None|Dict[str, str]
        check_older_version : bool
            Try to use an older version (1.1) of the SICD standard, for possible
            application compliance issues?
        """

        # choose magic number (with user data) and corresponding endian-ness
        magic_number = 0xFD7F02FF
        endian = SIODetails.ENDIAN[magic_number]

        # define basic image details
        image_size = (sicd_meta.ImageData.NumRows, sicd_meta.ImageData.NumCols)
        pixel_type = sicd_meta.ImageData.PixelType
        if pixel_type == 'RE32F_IM32F':
            data_type = numpy.dtype('{}f4'.format(endian))
            element_type = 13
            element_size = 8
            transform_data = 'COMPLEX'
        elif pixel_type == 'RE16I_IM16I':
            data_type = numpy.dtype('{}i2'.format(endian))
            element_type = 12
            element_size = 4
            transform_data = complex_to_int
        else:
            data_type = numpy.dtype('{}u1'.format(endian))
            element_type = 11
            element_size = 2
            transform_data = complex_to_amp_phase(sicd_meta.ImageData.AmpTable)
        # construct the sio header
        header = numpy.array([
            magic_number, image_size[0], image_size[1], element_type,
            element_size
        ],
                             dtype='>u4')
        # construct the user data - must be {str : str}
        if user_data is None:
            user_data = {}
        uh_args = sicd_meta.get_des_details(check_older_version)
        user_data['SICDMETA'] = sicd_meta.to_xml_string(tag='SICD',
                                                        urn=uh_args['DESSHTN'])
        data_offset = 20
        with open(file_name, 'wb') as fi:
            fi.write(struct.pack('{}5I'.format(endian), *header))
            # write the user data - name size, name, value size, value
            for name in user_data:
                name_bytes = name.encode('utf-8')
                fi.write(struct.pack('{}I'.format(endian), len(name_bytes)))
                fi.write(
                    struct.pack('{}{}s'.format(endian, len(name_bytes),
                                               name_bytes)))
                val_bytes = user_data[name].encode('utf-8')
                fi.write(struct.pack('{}I'.format(endian), len(val_bytes)))
                fi.write(
                    struct.pack('{}{}s'.format(endian, len(val_bytes),
                                               val_bytes)))
                data_offset += 4 + len(name_bytes) + 4 + len(val_bytes)
        # initialize the bip writer - we're ready to go
        output_bands = 2
        super(SIOWriter, self).__init__(file_name,
                                        image_size,
                                        data_type,
                                        output_bands,
                                        transform_data=transform_data,
                                        data_offset=data_offset)
Ejemplo n.º 2
0
    def __init__(self, file_name, sicd_meta, user_data=None):
        """

        Parameters
        ----------
        file_name : str
        sicd_meta : SICDType
        user_data : None|Dict[str, str]
        """

        # choose magic number (with user data) and corresponding endian-ness
        magic_number = 0xFD7F02FF
        endian = SIODetails.ENDIAN[magic_number]

        # define basic image details
        image_size = (sicd_meta.ImageData.NumRows, sicd_meta.ImageData.NumCols)
        pixel_type = sicd_meta.ImageData.PixelType
        if pixel_type == 'RE32F_IM32F':
            data_type = numpy.dtype('{}f4'.format(endian))
            element_type = 13
            element_size = 8
            complex_type = True
        elif pixel_type == 'RE16I_IM16I':
            data_type = numpy.dtype('{}i2'.format(endian))
            element_type = 12
            element_size = 4
            complex_type = complex_to_int
        else:
            data_type = numpy.dtype('{}u1'.format(endian))
            element_type = 11
            element_size = 2
            complex_type = complex_to_amp_phase(sicd_meta.ImageData.AmpTable)
        # construct the sio header
        header = numpy.array([
            magic_number, image_size[0], image_size[1], element_type,
            element_size
        ],
                             dtype='>u4')
        # construct the user data - must be {str : str}
        if user_data is None:
            user_data = {}
        user_data['SICDMETA'] = sicd_meta.to_xml_string(tag='SICD')
        data_offset = 20
        with open(file_name, 'wb') as fi:
            fi.write(struct.pack('{}5I'.format(endian), *header))
            # write the user data - name size, name, value size, value
            for name in user_data:
                name_bytes = name.encode('utf-8')
                fi.write(struct.pack('{}I'.format(endian), len(name_bytes)))
                fi.write(
                    struct.pack('{}{}s'.format(endian, len(name_bytes),
                                               name_bytes)))
                val_bytes = user_data[name].encode('utf-8')
                fi.write(struct.pack('{}I'.format(endian), len(val_bytes)))
                fi.write(
                    struct.pack('{}{}s'.format(endian, len(val_bytes),
                                               val_bytes)))
                data_offset += 4 + len(name_bytes) + 4 + len(val_bytes)
        # initialize the bip writer - we're ready to go
        super(SIOWriter, self).__init__(file_name,
                                        image_size,
                                        data_type,
                                        complex_type=complex_type,
                                        data_offset=data_offset)