Пример #1
0
def add_sicd_from_ortho_helper(kmz_document,
                               ortho_helper,
                               inc_image_corners=False,
                               inc_valid_data=False,
                               inc_scp=False,
                               inc_collection_wedge=False,
                               block_size=10,
                               dmin=30,
                               mmult=4):
    """
    Adds for a SICD to the provided open kmz from an ortho-rectification helper.

    Parameters
    ----------
    kmz_document : Document
    ortho_helper : OrthorectificationHelper
    inc_image_corners : bool
        Include the image corners, if possible?
    inc_valid_data : bool
        Include the valid image area, if possible?
    inc_scp : bool
        Include the scp?
    inc_collection_wedge : bool
        Include the aperture location and collection wedge?
    block_size : None|int|float
        The block size for the iterator
    dmin : int|float
        The remap parameters - the default is the high contrast remap value.
    mmult : int|float
        The remap parameters - the default is the high contrast remap value.

    Returns
    -------
    None
    """

    if not isinstance(ortho_helper, OrthorectificationHelper):
        raise TypeError(
            'ortho_helper must be an OrthorectificationHelper instance, got '
            'type {}'.format(type(ortho_helper)))
    if not isinstance(kmz_document, Document):
        raise TypeError(
            'kmz_document must be an sarpy.io.kml.Document instance, got '
            'type {}'.format(type(kmz_document)))

    # create a folder for these sicd details
    sicd = ortho_helper.sicd
    folder = kmz_document.add_container(
        the_type='Folder',
        name=_get_sicd_name(sicd),
        description=_get_sicd_description(sicd))
    # write the sicd details aside from the overlay
    add_sicd_geometry_elements(sicd,
                               kmz_document,
                               folder,
                               inc_image_corners=inc_image_corners,
                               inc_valid_data=inc_valid_data,
                               inc_scp=inc_scp,
                               inc_collection_wedge=inc_collection_wedge)
    # create the ortho-rectification iterator
    calculator = FullResolutionFetcher(ortho_helper.reader,
                                       index=ortho_helper.index,
                                       dimension=1,
                                       block_size=block_size)
    ortho_iterator = OrthorectificationIterator(
        ortho_helper, calculator=calculator, dmin=dmin,
        mmult=mmult)  # use the high contrast remap params
    # write the image overlay
    _write_sicd_overlay(ortho_iterator, kmz_document, folder)
Пример #2
0
def create_detected_image_sidd(ortho_helper,
                               output_directory,
                               output_file=None,
                               block_size=10,
                               dimension=0,
                               bounds=None,
                               version=2,
                               include_sicd=True,
                               remap_function=None):
    """
    Create a SIDD version of a basic detected image from a SICD type reader.

    Parameters
    ----------
    ortho_helper : OrthorectificationHelper
        The ortho-rectification helper object.
    output_directory : str
        The output directory for the given file.
    output_file : None|str
        The file name, this will default to a sensible value.
    block_size : int
        The approximate processing block size to fetch, given in MB. The
        minimum value for use here will be 1.
    dimension : int
        Which dimension to split over in block processing? Must be either 0 or 1.
    bounds : None|numpy.ndarray|list|tuple
        The sicd pixel bounds of the form `(min row, max row, min col, max col)`.
        This will default to the full image.
    version : int
        The SIDD version to use, must be one of 1 or 2.
    include_sicd : bool
        Include the SICD structure in the SIDD file?
    remap_function : None|MonochromaticRemap
        The applied remap function. If one is not provided, then a default is
        used. Required global parameters will be calculated if they are missing,
        so the internal state of this remap function may be modified.

    Returns
    -------
    None

    Examples
    --------
    .. code-block:: python

        import os

        from sarpy.io.complex.converter import open_complex
        from sarpy.processing.ortho_rectify import BivariateSplineMethod, NearestNeighborMethod, PGProjection
        from sarpy.io.product.sidd_product_creation import create_detected_image_sidd

        reader = open_complex('<sicd type object file name>')
        ortho_helper = NearestNeighborMethod(reader, index=0)

        # create a sidd version 2 file for the whole file
        create_detected_image_sidd(ortho_helper, '<output directory>', block_size=10, version=2)
    """

    if not os.path.isdir(output_directory):
        raise SarpyIOError(
            'output_directory {} does not exist or is not a directory'.format(
                output_directory))

    if not isinstance(ortho_helper, OrthorectificationHelper):
        raise TypeError(
            'ortho_helper is required to be an instance of OrthorectificationHelper, '
            'got type {}'.format(type(ortho_helper)))

    if remap_function is None:
        remap_function = DEFAULT_IMG_REMAP(override_name='IMG_DEFAULT')
    _validate_remap_function(remap_function)

    # construct the ortho-rectification iterator - for a basic data fetcher
    calculator = FullResolutionFetcher(ortho_helper.reader,
                                       dimension=dimension,
                                       index=ortho_helper.index,
                                       block_size=block_size)
    ortho_iterator = OrthorectificationIterator(ortho_helper,
                                                calculator=calculator,
                                                bounds=bounds,
                                                remap_function=remap_function,
                                                recalc_remap_globals=False)

    # create the sidd structure
    ortho_bounds = ortho_iterator.ortho_bounds
    sidd_structure = create_sidd_structure(ortho_helper,
                                           ortho_bounds,
                                           product_class='Detected Image',
                                           pixel_type='MONO{}I'.format(
                                               remap_function.bit_depth),
                                           version=version)
    # set suggested name
    sidd_structure.NITF[
        'SUGGESTED_NAME'] = ortho_helper.sicd.get_suggested_name(
            ortho_helper.index) + '_IMG'

    # create the sidd writer
    full_filename = _validate_filename(output_directory, output_file,
                                       sidd_structure)
    writer = SIDDWriter(full_filename, sidd_structure,
                        ortho_helper.sicd if include_sicd else None)

    # iterate and write
    for data, start_indices in ortho_iterator:
        writer(data, start_indices=start_indices, index=0)
Пример #3
0
def create_csi_sidd(ortho_helper,
                    output_directory,
                    output_file=None,
                    dimension=0,
                    block_size=30,
                    bounds=None,
                    version=2,
                    include_sicd=True,
                    remap_function=None):
    """
    Create a SIDD version of a Color Sub-Aperture Image from a SICD type reader.

    Parameters
    ----------
    ortho_helper : OrthorectificationHelper
        The ortho-rectification helper object.
    output_directory : str
        The output directory for the given file.
    output_file : None|str
        The file name, this will default to a sensible value.
    dimension : int
        The dimension over which to split the sub-aperture.
    block_size : int
        The approximate processing block size to fetch, given in MB. The
        minimum value for use here will be 1.
    bounds : None|numpy.ndarray|list|tuple
        The sicd pixel bounds of the form `(min row, max row, min col, max col)`.
        This will default to the full image.
    version : int
        The SIDD version to use, must be one of 1 or 2.
    include_sicd : bool
        Include the SICD structure in the SIDD file?
    remap_function : None|MonochromaticRemap
        The applied remap function. For csi processing, this must explicitly be
        an 8-bit remap. If one is not provided, then a default is used. Required
        global parameters will be calculated if they are missing, so the internal
        state of this remap function may be modified.

    Returns
    -------
    None

    Examples
    --------
    .. code-block:: python

        import os
        from sarpy.io.complex.converter import open_complex
        from sarpy.io.product.sidd_product_creation import create_csi_sidd
        from sarpy.processing.csi import CSICalculator
        from sarpy.processing.ortho_rectify import NearestNeighborMethod

        reader = open_complex('<sicd type object file name>')
        ortho_helper = NearestNeighborMethod(reader, index=0)
        create_csi_sidd(ortho_helper, '<output directory>', dimension=0, version=2)

    """

    if not os.path.isdir(output_directory):
        raise SarpyIOError(
            'output_directory {} does not exist or is not a directory'.format(
                output_directory))

    if not isinstance(ortho_helper, OrthorectificationHelper):
        raise TypeError(
            'ortho_helper is required to be an instance of OrthorectificationHelper, '
            'got type {}'.format(type(ortho_helper)))

    # construct the CSI calculator class
    csi_calculator = CSICalculator(ortho_helper.reader,
                                   dimension=dimension,
                                   index=ortho_helper.index,
                                   block_size=block_size)

    if remap_function is None:
        remap_function = DEFAULT_CSI_REMAP(override_name='CSI_DEFAULT',
                                           bit_depth=8)
    _validate_remap_function(remap_function)
    if remap_function.bit_depth != 8:
        raise ValueError(
            'The CSI SIDD specifically requires an 8-bit remap function.')

    # construct the ortho-rectification iterator
    ortho_iterator = OrthorectificationIterator(ortho_helper,
                                                calculator=csi_calculator,
                                                bounds=bounds,
                                                remap_function=remap_function,
                                                recalc_remap_globals=False)

    # create the sidd structure
    ortho_bounds = ortho_iterator.ortho_bounds
    sidd_structure = create_sidd_structure(
        ortho_helper,
        ortho_bounds,
        product_class='Color Subaperture Image',
        pixel_type='RGB24I',
        version=version)
    # set suggested name
    sidd_structure.NITF[
        'SUGGESTED_NAME'] = csi_calculator.sicd.get_suggested_name(
            csi_calculator.index) + '_CSI'

    # create the sidd writer
    full_filename = _validate_filename(output_directory, output_file,
                                       sidd_structure)
    writer = SIDDWriter(full_filename, sidd_structure,
                        csi_calculator.sicd if include_sicd else None)

    # iterate and write
    for data, start_indices in ortho_iterator:
        writer(data, start_indices=start_indices, index=0)
Пример #4
0
def create_csi_sidd(ortho_helper,
                    output_directory,
                    output_file=None,
                    dimension=0,
                    block_size=30,
                    bounds=None,
                    version=2):
    """
    Create a SIDD version of a Color Sub-Aperture Image from a SICD type reader.

    Parameters
    ----------
    ortho_helper : OrthorectificationHelper
        The ortho-rectification helper object.
    output_directory : str
        The output directory for the given file.
    output_file : None|str
        The file name, this will default to a sensible value.
    dimension : int
        The dimension over which to split the sub-aperture.
    block_size : int
        The approximate processing block size to fetch, given in MB. The
        minimum value for use here will be 1.
    bounds : None|numpy.ndarray|list|tuple
        The sicd pixel bounds of the form `(min row, max row, min col, max col)`.
        This will default to the full image.
    version : int
        The SIDD version to use, must be one of 1 or 2.

    Returns
    -------
    None

    Examples
    --------
    .. code-block:: python

        import os
        from sarpy.io.complex.converter import open_complex
        from sarpy.io.product.sidd_product_creation import create_csi_sidd
        from sarpy.processing.csi import CSICalculator
        from sarpy.processing.ortho_rectify import NearestNeighborMethod

        reader = open_complex('<sicd type object file name>')
        ortho_helper = NearestNeighborMethod(reader, index=0)
        create_csi_sidd(ortho_helper, '<output directory>', dimension=0, version=2)

    """

    if not os.path.isdir(output_directory):
        raise IOError(
            'output_directory {} does not exist or is not a directory'.format(
                output_directory))

    if not isinstance(ortho_helper, OrthorectificationHelper):
        raise TypeError(
            'ortho_helper is required to be an instance of OrthorectificationHelper, '
            'got type {}'.format(type(ortho_helper)))

    # construct the CSI calculator class
    csi_calculator = CSICalculator(ortho_helper.reader,
                                   dimension=dimension,
                                   index=ortho_helper.index,
                                   block_size=block_size)

    # construct the ortho-rectification iterator
    ortho_iterator = OrthorectificationIterator(ortho_helper,
                                                calculator=csi_calculator,
                                                bounds=bounds)

    # create the sidd structure
    ortho_bounds = ortho_iterator.ortho_bounds
    sidd_structure = create_sidd_structure(
        ortho_helper,
        ortho_bounds,
        product_class='Color Subaperture Image',
        pixel_type='RGB24I',
        version=version)
    # set suggested name
    sidd_structure._NITF = {
        'SUGGESTED_NAME':
        csi_calculator.sicd.get_suggested_name(csi_calculator.index) + '_CSI',
    }

    # create the sidd writer
    full_filename = _validate_filename(output_directory, output_file,
                                       sidd_structure)
    writer = SIDDWriter(full_filename, sidd_structure, csi_calculator.sicd)

    # iterate and write
    for data, start_indices in ortho_iterator:
        writer(data, start_indices=start_indices, index=0)
Пример #5
0
def add_sicd_from_ortho_helper(kmz_document,
                               ortho_helper,
                               inc_image_corners=False,
                               inc_valid_data=False,
                               inc_scp=False,
                               inc_collection_wedge=False,
                               block_size=10,
                               remap_function=None):
    """
    Adds for a SICD to the provided open kmz from an ortho-rectification helper.

    Parameters
    ----------
    kmz_document : Document
    ortho_helper : OrthorectificationHelper
    inc_image_corners : bool
        Include the image corners, if possible?
    inc_valid_data : bool
        Include the valid image area, if possible?
    inc_scp : bool
        Include the scp?
    inc_collection_wedge : bool
        Include the aperture location and collection wedge?
    block_size : None|int|float
        The block size for the iterator
    remap_function : None|RemapFunction
        The remap function to apply, or a suitable default will be chosen.
    """

    if not isinstance(ortho_helper, OrthorectificationHelper):
        raise TypeError(
            'ortho_helper must be an OrthorectificationHelper instance, got '
            'type {}'.format(type(ortho_helper)))
    if not isinstance(kmz_document, Document):
        raise TypeError(
            'kmz_document must be an sarpy.io.kml.Document instance, got '
            'type {}'.format(type(kmz_document)))

    # create a folder for these sicd details
    sicd = ortho_helper.sicd
    folder = kmz_document.add_container(
        the_type='Folder',
        name=_get_sicd_name(sicd),
        description=_get_sicd_description(sicd))

    # write the sicd details aside from the overlay
    add_sicd_geometry_elements(sicd,
                               kmz_document,
                               folder,
                               inc_image_corners=inc_image_corners,
                               inc_valid_data=inc_valid_data,
                               inc_scp=inc_scp,
                               inc_collection_wedge=inc_collection_wedge)

    # create the ortho-rectification iterator
    if remap_function is None:
        remap_function = NRL()
    calculator = FullResolutionFetcher(ortho_helper.reader,
                                       index=ortho_helper.index,
                                       dimension=1,
                                       block_size=block_size)
    ortho_iterator = OrthorectificationIterator(ortho_helper,
                                                calculator=calculator,
                                                remap_function=remap_function,
                                                recalc_remap_globals=True)

    # write the image overlay
    _write_sicd_overlay(ortho_iterator, kmz_document, folder)
Пример #6
0
def create_detected_image_sidd(ortho_helper,
                               output_directory,
                               output_file=None,
                               block_size=10,
                               dimension=0,
                               bounds=None,
                               version=2):
    """
    Create a SIDD version of a basic detected image from a SICD type reader.

    Parameters
    ----------
    ortho_helper : OrthorectificationHelper
        The ortho-rectification helper object.
    output_directory : str
        The output directory for the given file.
    output_file : None|str
        The file name, this will default to a sensible value.
    block_size : int
        The approximate processing block size to fetch, given in MB. The
        minimum value for use here will be 1.
    dimension : int
        Which dimension to split over in block processing? Must be either 0 or 1.
    bounds : None|numpy.ndarray|list|tuple
        The sicd pixel bounds of the form `(min row, max row, min col, max col)`.
        This will default to the full image.
    version : int
        The SIDD version to use, must be one of 1 or 2.

    Returns
    -------
    None

    Examples
    --------
    .. code-block:: python
        import logging
        logging.basicConfig(level='INFO')
        import os

        from sarpy.io.complex.converter import open_complex
        from sarpy.processing.ortho_rectify import BivariateSplineMethod, NearestNeighborMethod, PGProjection
        from sarpy.io.product.sidd_product_creation import create_detected_image_sidd

        reader = open_complex('<sicd type object file name>')
        ortho_helper = NearestNeighborMethod(reader, index=0)

        # create a sidd version 2 file for the whole file
        create_detected_image_sidd(ortho_helper, '<output directory>', block_size=10, version=2)
    """

    if not os.path.isdir(output_directory):
        raise IOError(
            'output_directory {} does not exist or is not a directory'.format(
                output_directory))

    if not isinstance(ortho_helper, OrthorectificationHelper):
        raise TypeError(
            'ortho_helper is required to be an instance of OrthorectificationHelper, '
            'got type {}'.format(type(ortho_helper)))

    # construct the ortho-rectification iterator - for a basic data fetcher
    calculator = FullResolutionFetcher(ortho_helper.reader,
                                       dimension=dimension,
                                       index=ortho_helper.index,
                                       block_size=block_size)
    ortho_iterator = OrthorectificationIterator(ortho_helper,
                                                calculator=calculator,
                                                bounds=bounds)

    # create the sidd structure
    ortho_bounds = ortho_iterator.ortho_bounds
    sidd_structure = create_sidd_structure(ortho_helper,
                                           ortho_bounds,
                                           product_class='Detected Image',
                                           pixel_type='MONO8I',
                                           version=version)
    # set suggested name
    sidd_structure._NITF = {
        'SUGGESTED_NAME':
        ortho_helper.sicd.get_suggested_name(ortho_helper.index) + '_IMG',
    }

    # create the sidd writer
    full_filename = _validate_filename(output_directory, output_file,
                                       sidd_structure)
    writer = SIDDWriter(full_filename, sidd_structure, ortho_helper.sicd)

    # iterate and write
    for data, start_indices in ortho_iterator:
        writer(data, start_indices=start_indices, index=0)