Example #1
0
    def _create_image_segment_details(self):
        super(SICDWriter, self)._create_image_segment_details()

        pixel_size, dtype, transform_data, pv_type, isubcat, image_segment_limits = self._image_parameters()
        img_groups = tuple(range(len(image_segment_limits)))
        self._img_groups = (img_groups, )

        iid2 = self._get_iid2()
        idatim = ' '
        if self.sicd_meta.Timeline is not None and self.sicd_meta.Timeline.CollectStart is not None:
            idatim = re.sub(r'[^0-9]', '', str(self.sicd_meta.Timeline.CollectStart.astype('datetime64[s]')))

        isource = self._get_isorce()

        icp, rows, cols = None, None, None
        if self.sicd_meta.GeoData is not None and self.sicd_meta.GeoData.ImageCorners is not None:
            # noinspection PyTypeChecker
            icp = self.sicd_meta.GeoData.ImageCorners.get_array(dtype=numpy.float64)
            rows = self.sicd_meta.ImageData.NumRows
            cols = self.sicd_meta.ImageData.NumCols
        abpp = 4*pixel_size
        bands = [ImageBand(ISUBCAT=entry) for entry in isubcat]

        img_details = []

        for i, entry in enumerate(image_segment_limits):
            if i == 0:
                iloc = '0000000000'
            else:
                prev_lims = image_segment_limits[i-1]
                prev_rows = prev_lims[1] - prev_lims[0]
                iloc = '{0:05d}00000'.format(prev_rows)

            this_rows = entry[1]-entry[0]
            this_cols = entry[3]-entry[2]
            subhead = ImageSegmentHeader(
                IID1='SICD{0:03d}'.format(0 if len(image_segment_limits) == 1 else i+1),
                IDATIM=idatim,
                IID2=iid2,
                ISORCE=isource,
                IREP='NODISPLY',
                ICAT='SAR',
                NROWS=this_rows,
                NCOLS=this_cols,
                PVTYPE=pv_type,
                ABPP=abpp,
                IGEOLO=interpolate_corner_points_string(numpy.array(entry, dtype=numpy.int64), rows, cols, icp),
                NBPC=1,
                NPPBH=get_npp_block(this_cols),
                NBPR=1,
                NPPBV=get_npp_block(this_rows),
                NBPP=abpp,
                IDLVL=i+1,
                IALVL=i,
                ILOC=iloc,
                Bands=ImageBands(values=bands),
                Security=self._security_tags)
            img_details.append(ImageDetails(2, dtype, transform_data, entry, subhead))

        self._img_details = tuple(img_details)
Example #2
0
    def _create_image_segment(self, index, img_groups, img_details):
        cur_count = len(img_details)
        pixel_size, abpp, irep, raw_dtype, transform_data, pv_type, band_count, irepband, \
            image_segment_limits = self._image_parameters(index)
        new_count = len(image_segment_limits)
        img_groups.append(tuple(range(cur_count, cur_count + new_count)))
        security = self._get_security_tags(index)

        iid2 = self._get_iid2(index)
        sidd = self.sidd_meta[index]

        idatim = self._get_fdt()

        isorce = self._get_isorce(index)

        rows = sidd.Measurement.PixelFootprint.Row
        cols = sidd.Measurement.PixelFootprint.Col

        icp = self._get_icp(sidd)
        bands = [ImageBand(ISUBCAT='', IREPBAND=entry) for entry in irepband]

        for i, entry in enumerate(image_segment_limits):
            if i == 0:
                iloc = '0000000000'
            else:
                prev_lims = image_segment_limits[i - 1]
                prev_rows = prev_lims[1] - prev_lims[0]
                iloc = '{0:05d}00000'.format(prev_rows)

            this_rows = entry[1] - entry[0]
            this_cols = entry[3] - entry[2]
            subhead = ImageSegmentHeader(
                IID1='SIDD{0:03d}{1:03d}'.format(len(img_groups), i + 1),
                IDATIM=idatim,
                IID2=iid2,
                ISORCE=isorce,
                IREP=irep,
                ICAT='SAR',
                NROWS=this_rows,
                NCOLS=this_cols,
                PVTYPE=pv_type,
                ABPP=abpp,
                IGEOLO=interpolate_corner_points_string(
                    numpy.array(entry, dtype=numpy.int64), rows, cols, icp),
                IMODE='P',
                NPPBH=get_npp_block(this_cols),
                NPPBV=get_npp_block(this_rows),
                NBPP=abpp,
                IDLVL=i + 1 + len(img_details),
                IALVL=i + len(img_details),
                ILOC=iloc,
                Bands=ImageBands(values=bands),
                Security=security)
            img_details.append(
                ImageDetails(band_count, raw_dtype, transform_data, entry,
                             subhead))
Example #3
0
    def _create_image_segment(self, index, img_groups, img_details):
        cur_count = len(img_details)
        pixel_size, abpp, irep, dtype, complex_type, pv_type, band_count, irepband, \
            image_segment_limits = self._image_parameters(index)
        new_count = len(image_segment_limits)
        img_groups.append(tuple(range(cur_count, cur_count + new_count)))
        security = self._get_security_tags(index)

        iid2 = self._get_iid2(index)
        sidd = self.sidd_meta[index]

        idatim = self._get_fdt()

        isorce = ''
        if sidd.ExploitationFeatures.Collections[
                0].Information.SensorName is not None:
            isorce = sidd.ExploitationFeatures.Collections[
                0].Information.SensorName

        rows = sidd.Measurement.PixelFootprint.Row
        cols = sidd.Measurement.PixelFootprint.Col

        icp = self._get_icp(sidd)
        bands = [ImageBand(ISUBCAT='', IREPBAND=entry) for entry in irepband]

        for i, entry in enumerate(image_segment_limits):
            this_rows = entry[1] - entry[0]
            this_cols = entry[3] - entry[2]
            subhead = ImageSegmentHeader(
                IID1='SIDD{0:03d}{1:03d}'.format(len(img_groups), i + 1),
                IDATIM=idatim,
                IID2=iid2,
                ISORCE=isorce,
                IREP=irep,
                ICAT='SAR',
                NROWS=this_rows,
                NCOLS=this_cols,
                PVTYPE=pv_type,
                ABPP=abpp,
                IGEOLO=interpolate_corner_points_string(
                    numpy.array(entry, dtype=numpy.int64), rows, cols, icp),
                IMODE='P' if irep == 'RGB' else 'B',
                NPPBH=get_npp_block(this_cols),
                NPPBV=get_npp_block(this_rows),
                NBPP=abpp,
                IDLVL=i + 1 + len(img_details),
                IALVL=i + len(img_details),
                ILOC='{0:05d}{1:05d}'.format(entry[0], entry[2]),
                Bands=ImageBands(values=bands),
                Security=security)
            img_details.append(
                ImageDetails(band_count, dtype, complex_type, entry, subhead))
Example #4
0
    def _create_image_segments(self) -> Tuple[Tuple[ImageSubheaderManager, ...], Tuple[Tuple[int, ...], ...], Tuple[Tuple[Tuple[int, ...], ...]]]:
        image_managers = []
        basic_args = {
            'IREP': 'NODISPLY',
            'IC': 'NC',
            'ICAT': 'SAR',
            'IID2': self._get_iid2(),
            'IDATIM': self._get_idatim(),
            'ISORCE': self._get_isorce()
        }
        pixel_type = self.sicd_meta.ImageData.PixelType  # required to be defined
        # NB: SICDs are required to be stored as big-endian, so the endian-ness
        #   of the memmap must be explicit
        if pixel_type == 'RE32F_IM32F':
            basic_args['PVTYPE'] = 'R'
            basic_args['NBPP'] = 32
            basic_args['ABPP'] = 32
            isubcat = ('I', 'Q')
        elif pixel_type == 'RE16I_IM16I':
            basic_args['PVTYPE'] = 'SI'
            basic_args['NBPP'] = 16
            basic_args['ABPP'] = 16
            isubcat = ('I', 'Q')
        elif pixel_type == 'AMP8I_PHS8I':
            basic_args['PVTYPE'] = 'INT'
            basic_args['NBPP'] = 8
            basic_args['ABPP'] = 8
            isubcat = ('M', 'P')
        else:
            raise ValueError('Got unhandled pixel_type {}'.format(pixel_type))

        rows = self.sicd_meta.ImageData.NumRows
        cols = self.sicd_meta.ImageData.NumCols
        icp = None
        if self.sicd_meta.GeoData is not None and self.sicd_meta.GeoData.ImageCorners is not None:
            # noinspection PyTypeChecker
            icp = self.sicd_meta.GeoData.ImageCorners.get_array(dtype=numpy.float64)

        image_segment_limits = default_image_segmentation(rows, cols, self.row_limit)

        image_segment_collection = (tuple(range(len(image_segment_limits))), )
        image_segment_coordinates = (image_segment_limits, )

        for i, entry in enumerate(image_segment_limits):
            if i == 0:
                iloc = '0000000000'
            else:
                prev_lims = image_segment_limits[i-1]
                prev_rows = prev_lims[1] - prev_lims[0]
                iloc = '{0:05d}00000'.format(prev_rows)

            this_rows = entry[1]-entry[0]
            this_cols = entry[3]-entry[2]
            subhead = ImageSegmentHeader(
                IID1='SICD{0:03d}'.format(0 if len(image_segment_limits) == 1 else i+1),
                NROWS=this_rows,
                NCOLS=this_cols,
                IGEOLO=interpolate_corner_points_string(numpy.array(entry, dtype=numpy.int64), rows, cols, icp),
                NPPBH=0 if this_cols > 8192 else this_cols,
                NPPBV=0 if this_rows > 8192 else this_rows,
                NBPC=1,
                NBPR=1,
                IDLVL=i+1,
                IALVL=i,
                ILOC=iloc,
                Bands=ImageBands(values=[ImageBand(ISUBCAT=entry) for entry in isubcat]),
                Security=self._security_tags,
                **basic_args)
            image_managers.append(ImageSubheaderManager(subhead))
        return tuple(image_managers), image_segment_collection, image_segment_coordinates
Example #5
0
def generic_nitf_header_test(instance, test_file):
    assert isinstance(instance, unittest.TestCase)

    # can we parse it at all? how long does it take?
    with instance.subTest(msg="header parsing"):
        start = time.time()
        details = NITFDetails(test_file)
        # how long does it take?
        logging.info('unpacked nitf details in {}'.format(time.time() - start))
        # how does it look?
        logging.debug(details.nitf_header)

    # is the output as long as it should be?
    with instance.subTest(msg="header length match"):
        header_string = details.nitf_header.to_bytes()
        equality = (len(header_string) == details.nitf_header.HL)
        if not equality:
            logging.error(
                'len(produced header) = {}, nitf_header.HL = {}'.format(
                    len(header_string), details.nitf_header.HL))
        instance.assertTrue(equality)

    # is the output what it should be?
    with instance.subTest(msg="header content match"):
        with open(test_file, 'rb') as fi:
            file_header = fi.read(details.nitf_header.HL)

        equality = (file_header == header_string)
        if not equality:
            chunk_size = 80
            start_chunk = 0
            while start_chunk < len(header_string):
                end_chunk = min(start_chunk + chunk_size, len(header_string))
                logging.error('real[{}:{}] = {}'.format(
                    start_chunk, end_chunk,
                    file_header[start_chunk:end_chunk]))
                logging.error('prod[{}:{}] = {}'.format(
                    start_chunk, end_chunk,
                    header_string[start_chunk:end_chunk]))
                start_chunk = end_chunk
        instance.assertTrue(equality)

    # is each image subheader working?
    if details.img_segment_offsets is not None:
        for i in range(details.img_segment_offsets.size):
            with instance.subTest('image subheader {} match'.format(i)):
                img_bytes = details.get_image_subheader_bytes(i)
                img_sub = ImageSegmentHeader.from_bytes(img_bytes, start=0)
                instance.assertEqual(len(img_bytes),
                                     img_sub.get_bytes_length(),
                                     msg='image subheader as long as expected')
                instance.assertEqual(
                    img_bytes,
                    img_sub.to_bytes(),
                    msg=
                    'image subheader serializes and deserializes as expected')

    # is each text segment working?
    if details.text_segment_offsets is not None:
        for i in range(details.text_segment_offsets.size):
            with instance.subTest('text subheader {} match'.format(i)):
                txt_bytes = details.get_text_subheader_bytes(i)
                txt_sub = TextSegmentHeader.from_bytes(txt_bytes, start=0)
                instance.assertEqual(len(txt_bytes),
                                     txt_sub.get_bytes_length(),
                                     msg='text subheader as long as expected')
                instance.assertEqual(
                    txt_bytes,
                    txt_sub.to_bytes(),
                    msg='text subheader serializes and deserializes as expected'
                )

    # is each graphics segment working?
    if details.graphics_segment_offsets is not None:
        for i in range(details.graphics_segment_offsets.size):
            with instance.subTest('graphics subheader {} match'.format(i)):
                graphics_bytes = details.get_graphics_subheader_bytes(i)
                graphics_sub = GraphicsSegmentHeader.from_bytes(graphics_bytes,
                                                                start=0)
                instance.assertEqual(
                    len(graphics_bytes),
                    graphics_sub.get_bytes_length(),
                    msg='graphics subheader as long as expected')
                instance.assertEqual(
                    graphics_bytes,
                    graphics_sub.to_bytes(),
                    msg=
                    'graphics subheader serializes and deserializes as expected'
                )

    # is each data extenson subheader working?
    if details.des_segment_offsets is not None:
        for i in range(details.des_segment_offsets.size):
            with instance.subTest('des subheader {} match'.format(i)):
                des_bytes = details.get_des_subheader_bytes(i)
                des_sub = DataExtensionHeader.from_bytes(des_bytes, start=0)
                instance.assertEqual(len(des_bytes),
                                     des_sub.get_bytes_length(),
                                     msg='des subheader as long as expected')
                instance.assertEqual(
                    des_bytes,
                    des_sub.to_bytes(),
                    msg='des subheader serializes and deserializes as expected'
                )