コード例 #1
0
ファイル: capella.py プロジェクト: khavernathy/sarpy
def is_a(file_name):
    """
    Tests whether a given file_name corresponds to a Capella SAR file.
    Returns a reader instance, if so.

    Parameters
    ----------
    file_name : str
        the file_name to check

    Returns
    -------
    CapellaReader|None
        `CapellaReader` instance if Capella file, `None` otherwise
    """

    if is_file_like(file_name):
        return None

    try:
        capella_details = CapellaDetails(file_name)
        logging.info(
            'File {} is determined to be a Capella file.'.format(file_name))
        return CapellaReader(capella_details)
    except SarpyIOError:
        return None
コード例 #2
0
def open_complex(file_name: Union[str, BinaryIO]) -> SICDTypeReader:
    """
    Given a file, try to find and return the appropriate reader object.

    Parameters
    ----------
    file_name : str|BinaryIO

    Returns
    -------
    SICDTypeReader

    Raises
    ------
    SarpyIOError
    """

    if (not is_file_like(file_name)) and (not os.path.exists(file_name)):
        raise SarpyIOError('File {} does not exist.'.format(file_name))
    # parse openers, if not already done
    parse_openers()
    # see if we can find a reader though trial and error
    for opener in _openers:
        reader = opener(file_name)
        if reader is not None:
            return reader
    # check the final attempt openers
    for opener in _define_final_attempt_openers():
        reader = opener(file_name)
        if reader is not None:
            return reader

    # If for loop completes, no matching file format was found.
    raise SarpyIOError('Unable to determine complex image format.')
コード例 #3
0
def is_a(file_name):
    """
    Tests whether a given file_name corresponds to a Cosmo Skymed file. Returns a reader instance, if so.

    Parameters
    ----------
    file_name : str|BinaryIO
        the file_name to check

    Returns
    -------
    CSKReader|None
        `CSKReader` instance if Cosmo Skymed file, `None` otherwise
    """

    if is_file_like(file_name):
        return None

    if not is_hdf5(file_name):
        return None

    if h5py is None:
        return None

    try:
        csk_details = CSKDetails(file_name)
        logging.info('File {} is determined to be a Cosmo Skymed file.'.format(
            file_name))
        return CSKReader(csk_details)
    except SarpyIOError:
        return None
コード例 #4
0
def is_a(file_name):
    """
    Tests whether a given file_name corresponds to a ICEYE file. Returns a reader instance, if so.

    Parameters
    ----------
    file_name : str|BinaryIO
        the file_name to check

    Returns
    -------
    CSKReader|None
        `CSKReader` instance if Cosmo Skymed file, `None` otherwise
    """

    if h5py is None:
        return None

    if is_file_like(file_name):
        return None

    try:
        iceye_details = ICEYEDetails(file_name)
        logging.info(
            'File {} is determined to be a ICEYE file.'.format(file_name))
        return ICEYEReader(iceye_details)
    except (ImportError, IOError):
        return None
コード例 #5
0
ファイル: nisar.py プロジェクト: kirkjens/sarpy
def is_a(file_name):
    """
    Tests whether a given file_name corresponds to a NISAR file. Returns a reader instance, if so.

    Parameters
    ----------
    file_name : str|BinaryIO
        the file_name to check

    Returns
    -------
    NISARReader|None
        `NISARReader` instance if NISAR file, `None` otherwise
    """

    if is_file_like(file_name):
        return None

    if not is_hdf5(file_name):
        return None

    if h5py is None:
        return None

    try:
        nisar_details = NISARDetails(file_name)
        logger.info('File {} is determined to be a NISAR file.'.format(file_name))
        return NISARReader(nisar_details)
    except (ImportError, SarpyIOError):
        return None
コード例 #6
0
def final_attempt(file_name: str) -> Optional[ComplexNITFReader]:
    """
    Contingency check to open for some other complex NITF type file.
    Returns a reader instance, if so.

    Parameters
    ----------
    file_name : str|BinaryIO
        the file_name to check

    Returns
    -------
    ComplexNITFReader|None
    """

    if is_file_like(file_name):
        return None

    try:
        nitf_details = ComplexNITFDetails(file_name)
        logger.info(
            'File {} is determined to be some other format complex NITF.')
        return ComplexNITFReader(nitf_details)
    except (SarpyIOError, ValueError):
        return None
コード例 #7
0
ファイル: sio.py プロジェクト: nyulacska/sarpy
def is_a(file_name):
    """
    Tests whether a given file_name corresponds to a SIO file. Returns a reader instance, if so.

    Parameters
    ----------
    file_name : str
        the file_name to check

    Returns
    -------
    SIOReader|None
        `SIOReader` instance if SIO file, `None` otherwise
    """

    if is_file_like(file_name):
        return None

    try:
        sio_details = SIODetails(file_name)
        logging.info(
            'File {} is determined to be a SIO file.'.format(file_name))
        return SIOReader(sio_details)
    except IOError:
        return None
コード例 #8
0
def is_a(file_name: str) -> Union[None, ICEYEReader]:
    """
    Tests whether a given file_name corresponds to a ICEYE file. Returns a reader instance, if so.

    Parameters
    ----------
    file_name : str|BinaryIO
        the file_name to check

    Returns
    -------
    None|ICEYEReader
        `ICEYEReader` instance if ICEYE file, `None` otherwise
    """

    if is_file_like(file_name):
        return None

    if not is_hdf5(file_name):
        return None

    if h5py is None:
        return None

    try:
        iceye_details = ICEYEDetails(file_name)
        logger.info(
            'File {} is determined to be a ICEYE file.'.format(file_name))
        return ICEYEReader(iceye_details)
    except SarpyIOError:
        return None
コード例 #9
0
    def __init__(self, nitf_details):
        """

        Parameters
        ----------
        nitf_details : str|BinaryIO|SIDDDetails
            filename, file-like object, or SIDDDetails object
        """

        if isinstance(nitf_details, str) or is_file_like(nitf_details):
            nitf_details = SIDDDetails(nitf_details)
        if not isinstance(nitf_details, SIDDDetails):
            raise TypeError(
                'The input argument for SIDDReader must be a filename or '
                'SIDDDetails object.')

        if not nitf_details.is_sidd:
            raise ValueError(
                'The input file passed in appears to be a NITF 2.1 file that does not contain '
                'valid sidd metadata.')

        self._nitf_details = nitf_details
        SIDDTypeReader.__init__(self, self.nitf_details.sidd_meta,
                                self.nitf_details.sicd_meta)
        NITFReader.__init__(self, nitf_details, reader_type="SIDD")
コード例 #10
0
ファイル: crsd.py プロジェクト: ngageoint/sarpy
    def __new__(cls, *args, **kwargs):
        if len(args) == 0:
            raise ValueError(
                'The first argument of the constructor is required to be a file_path '
                'or CRSDDetails instance.')
        if is_file_like(args[0]):
            raise ValueError('File like object input not supported for CRSD reading at this time.')
        crsd_details = _validate_crsd_details(args[0])

        if crsd_details.crsd_version.startswith('1.0'):
            return object.__new__(CRSDReader1_0)
        else:
            raise ValueError('Got unhandled CRSD version {}'.format(crsd_details.crsd_version))
コード例 #11
0
    def __init__(self, file_object):
        """

        Parameters
        ----------
        file_object : str|BinaryIO
            The path to or file like object referencing the CRSD file.
        """

        self._crsd_version = None
        self._crsd_header = None
        self._crsd_meta = None
        self._close_after = False

        if isinstance(file_object, string_types):
            if not os.path.exists(file_object) or not os.path.isfile(
                    file_object):
                raise SarpyIOError(
                    'path {} does not exist or is not a file'.format(
                        file_object))
            self._file_name = file_object
            self._file_object = open(file_object, 'rb')
            self._close_after = True
        elif is_file_like(file_object):
            self._file_object = file_object
            if hasattr(file_object, 'name') and isinstance(
                    file_object.name, string_types):
                self._file_name = file_object.name
            else:
                self._file_name = '<file like object>'
            self._close_after = False
        else:
            raise TypeError('Got unsupported input type {}'.format(
                type(file_object)))

        self._file_object.seek(0, os.SEEK_SET)
        head_bytes = self._file_object.read(10)
        if not isinstance(head_bytes, bytes):
            raise ValueError('Input file like object not open in bytes mode.')
        if not head_bytes.startswith(b'CRSD'):
            raise SarpyIOError(
                'File {} does not appear to be a CRSD file.'.format(
                    self.file_name))

        self._extract_version()
        self._extract_header()
        self._extract_crsd()
コード例 #12
0
ファイル: sicd.py プロジェクト: nyulacska/sarpy
    def __init__(self, nitf_details):
        """

        Parameters
        ----------
        nitf_details :  : str|BinaryIO|SICDDetails
            filename, file-like object, or SICDDetails object
        """

        if isinstance(nitf_details,
                      string_types) or is_file_like(nitf_details):
            nitf_details = SICDDetails(nitf_details)
        if not isinstance(nitf_details, SICDDetails):
            raise TypeError(
                'The input argument for SICDReader must be a filename, file-like object, '
                'or SICDDetails object.')
        super(SICDReader, self).__init__(nitf_details, reader_type="SICD")
コード例 #13
0
ファイル: sicd.py プロジェクト: ngageoint/sarpy
    def __init__(self, nitf_details):
        """

        Parameters
        ----------
        nitf_details :  : str|BinaryIO|SICDDetails
            filename, file-like object, or SICDDetails object
        """

        if isinstance(nitf_details, str) or is_file_like(nitf_details):
            nitf_details = SICDDetails(nitf_details)
        if not isinstance(nitf_details, SICDDetails):
            raise TypeError(
                'The input argument for SICDReader must be a filename, file-like object, '
                'or SICDDetails object.')

        SICDTypeReader.__init__(self, None, nitf_details.sicd_meta)
        NITFReader.__init__(self, nitf_details, reader_type='SICD')
        self._check_sizes()
コード例 #14
0
    def __init__(self,
                 file_object: Union[str, BinaryIO],
                 sicd_meta: SICDType,
                 user_data: Optional[Dict[str, str]] = None,
                 check_older_version: bool = False,
                 check_existence: bool = True):
        """

        Parameters
        ----------
        file_object : str|BinaryIO
        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?
        check_existence : bool
            Should we check if the given file already exists, and raises an exception if so?
        """

        self._data_written = True
        if isinstance(file_object, str):
            if check_existence and os.path.exists(file_object):
                raise SarpyIOError(
                    'Given file {} already exists, and a new SIO file cannot be created here.'
                    .format(file_object))
            file_object = open(file_object, 'wb')

        if not is_file_like(file_object):
            raise ValueError(
                'file_object requires a file path or BinaryIO object')

        self._file_object = file_object
        if is_real_file(file_object):
            self._file_name = file_object.name
            self._in_memory = False
        else:
            self._file_name = None
            self._in_memory = True

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

        # define basic image details
        raw_shape = (sicd_meta.ImageData.NumRows, sicd_meta.ImageData.NumCols,
                     2)
        pixel_type = sicd_meta.ImageData.PixelType
        if pixel_type == 'RE32F_IM32F':
            raw_dtype = numpy.dtype('{}f4'.format(endian))
            element_type = 13
            element_size = 8
            format_function = ComplexFormatFunction(raw_dtype,
                                                    order='MP',
                                                    band_dimension=2)
        elif pixel_type == 'RE16I_IM16I':
            raw_dtype = numpy.dtype('{}i2'.format(endian))
            element_type = 12
            element_size = 4
            format_function = ComplexFormatFunction(raw_dtype,
                                                    order='MP',
                                                    band_dimension=2)
        else:
            raw_dtype = numpy.dtype('{}u1'.format(endian))
            element_type = 11
            element_size = 2
            format_function = ComplexFormatFunction(
                raw_dtype,
                order='MP',
                band_dimension=2,
                magnitude_lookup_table=sicd_meta.ImageData.AmpTable)

        # construct the sio header
        header = numpy.array([
            magic_number, raw_shape[0], raw_shape[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'])

        # write the initial things to the buffer
        self._file_object.seek(0, os.SEEK_SET)
        self._file_object.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')
            self._file_object.write(
                struct.pack('{}I'.format(endian), len(name_bytes)))
            self._file_object.write(
                struct.pack('{}{}s'.format(endian, len(name_bytes)),
                            name_bytes))
            val_bytes = user_data[name].encode('utf-8')
            self._file_object.write(
                struct.pack('{}I'.format(endian), len(val_bytes)))
            self._file_object.write(
                struct.pack('{}{}s'.format(endian, len(val_bytes)), val_bytes))
        self._data_offset = self._file_object.tell()
        # initialize the single data segment
        if self._in_memory:
            underlying_array = numpy.full(raw_shape,
                                          fill_value=0,
                                          dtype=raw_dtype)
            data_segment = NumpyArraySegment(underlying_array,
                                             'complex64',
                                             raw_shape[:2],
                                             format_function=format_function,
                                             mode='w')
            self._data_written = False
        else:
            data_segment = NumpyMemmapSegment(self._file_object,
                                              self._data_offset,
                                              raw_dtype,
                                              raw_shape,
                                              'complex64',
                                              raw_shape[:2],
                                              format_function=format_function,
                                              mode='w',
                                              close_file=False)
            self._data_written = True
        BaseWriter.__init__(self, data_segment)