photutils/aperture/core.py
def _prepare_photometry_input(data, unit, wcs, mask, error, pixelwise_error):
    """
    Parse photometry input.

    Photometry routines accept a wide range of inputs, e.g. ``data``
    could be (among others)  a numpy array, or a fits HDU.
    This requires some parsing and bookkeping to ensure that all inputs
    are complete and consistent.
    For example, the data could carry a unit and the wcs itself, so we need to
    check that it is consistent with the unit and wcs given as explicit
    parameters.

    Note that this function is meant to be used in addition to, not instead
    of, the `~astropy.nddata.support_nddata` decorator, i. e. ``data`` is
    never an `~astropy.nddata.NDData` object, because that will be split up
    in data, wcs, mask, ... keywords by the decorator already.

    See `~photutils.aperture_photometry` for a description of all
    possible input values.

    Returns
    -------
    data : `~astropy.units.Quantity` instance
    wcs_transformation : `~astropy.wcs.WCS` instance or None
    mask : np.array or None
    error : `~astropy.units.Quantity` instance or None
    """

    dataunit = None
    datamask = None
    wcs_transformation = wcs

    if isinstance(data, fits.HDUList):
        for i in range(len(data)):
            if data[i].data is not None:
                warnings.warn("Input data is a HDUList object, photometry is "
                              "run only for the {0} HDU."
                              .format(i), AstropyUserWarning)
                data = data[i]
                break

    if isinstance(data, (fits.PrimaryHDU, fits.ImageHDU)):
        header = data.header
        data = data.data

        if 'BUNIT' in header:
            dataunit = header['BUNIT']

    if wcs_transformation is None:
        try:
            wcs_transformation = WCS(header)
        except:
            # data was not fits so header is not defined or header is invalid
            # Let the calling application raise an error is it needs a WCS.
            pass

    if hasattr(data, 'unit'):
        dataunit = data.unit

    if unit is not None and dataunit is not None:
        dataunit = u.Unit(dataunit, parse_strict='warn')
        unit = u.Unit(unit, parse_strict='warn')

        if not isinstance(unit, u.UnrecognizedUnit):
            data = u.Quantity(data, unit=unit, copy=False)
            if not isinstance(dataunit, u.UnrecognizedUnit):
                if unit != dataunit:
                    warnings.warn('Unit of input data ({0}) and unit given '
                                  'by unit argument ({1}) are not identical.'
                                  .format(dataunit, unit))
        else:
            if not isinstance(dataunit, u.UnrecognizedUnit):
                data = u.Quantity(data, unit=dataunit, copy=False)
            else:
                warnings.warn('Neither the unit of the input data ({0}), nor '
                              'the unit given by the unit argument ({1}) is '
                              'parseable as a valid unit'
                              .format(dataunit, unit))

    elif unit is None:
        if dataunit is not None:
            dataunit = u.Unit(dataunit, parse_strict='warn')
            data = u.Quantity(data, unit=dataunit, copy=False)
        else:
            data = u.Quantity(data, copy=False)
    else:
        unit = u.Unit(unit, parse_strict='warn')
        data = u.Quantity(data, unit=unit, copy=False)

    # Check input array type and dimension.
    if np.iscomplexobj(data):
        raise TypeError('Complex type not supported')
    if data.ndim != 2:
        raise ValueError('{0}-d array not supported. '
                         'Only 2-d arrays supported.'.format(data.ndim))

    # Deal with the mask if it exists
    if mask is not None or datamask is not None:
        if mask is None:
            mask = datamask
        else:
            mask = np.asarray(mask)
            if np.iscomplexobj(mask):
                raise TypeError('Complex type not supported')
            if mask.shape != data.shape:
                raise ValueError('Shapes of mask array and data array '
                                 'must match')

            if datamask is not None:
                # combine the masks
                mask = np.logical_or(mask, datamask)

    # Check whether we really need to calculate pixelwise errors, even if
    # requested.  If error is not an array, then it's not needed.
    if (error is None) or (np.isscalar(error)):
        pixelwise_error = False

    # Check error shape.
    if error is not None:
        if isinstance(error, u.Quantity):
            if np.isscalar(error.value):
                error = u.Quantity(np.broadcast_arrays(error, data),
                                   unit=error.unit)[0]
        elif np.isscalar(error):
            error = u.Quantity(np.broadcast_arrays(error, data),
                               unit=data.unit)[0]
        else:
            error = u.Quantity(error, unit=data.unit, copy=False)

        if error.shape != data.shape:
            raise ValueError('shapes of error array and data array must'
                             ' match')

    return data, wcs_transformation, mask, error, pixelwise_error

    Parse photometry input.

    Photometry routines accept a wide range of inputs, e.g. ``data``
    could be (among others)  a numpy array, or a fits HDU.
    This requires some parsing and bookkeping to ensure that all inputs
    are complete and consistent.
    For example, the data could carry a unit and the wcs itself, so we need to
    check that it is consistent with the unit and wcs given as explicit
    parameters.

    Note that this function is meant to be used in addition to, not instead
    of, the `~astropy.nddata.support_nddata` decorator, i. e. ``data`` is
    never an `~astropy.nddata.NDData` object, because that will be split up
    in data, wcs, mask, ... keywords by the decorator already.

    See `~photutils.aperture_photometry` for a description of all
    possible input values.

    Returns
    -------
    data : `~astropy.units.Quantity` instance
    wcs_transformation : `~astropy.wcs.WCS` instance or None
    mask : np.array or None
    error : `~astropy.units.Quantity` instance or None
    
Parse photometry input.
Photometry routines accept a wide range of inputs, e.g. ``data``
could be (among others)  a numpy array, or a fits HDU.
This requires some parsing and bookkeping to ensure that all inputs
are complete and consistent.
For example, the data could carry a unit and the wcs itself, so we need to
check that it is consistent with the unit and wcs given as explicit
parameters.

Note that this function is meant to be used in addition to, not instead
of, the `~astropy.nddata.support_nddata` decorator, i. e.

See `~photutils.aperture_photometry` for a description of all
possible input values.

Returns


Parse photometry input . Photometry routines accept a wide range of inputs e . g . ` ` data ` ` could be ( among others ) a numpy array or a fits HDU . This requires some parsing and bookkeping to ensure that all inputs are complete and consistent . For example the data could carry a unit and the wcs itself so we need to check that it is consistent with the unit and wcs given as explicit parameters . Note that this function is meant to be used in addition to not instead of the ` ~astropy . nddata . support_nddata ` decorator i . e . See ` ~photutils . aperture_photometry ` for a description of all possible input values . Returns
def _prepare_photometry_input(data, unit, wcs, mask, error, pixelwise_error):
    dataunit = None
    datamask = None
    wcs_transformation = wcs
    if isinstance(data, fits.HDUList):
        for i in range(len(data)):
            if data[i].data is not None:
                warnings.warn("Input data is a HDUList object, photometry is "
                              "run only for the {0} HDU."
                              .format(i), AstropyUserWarning)
                data = data[i]
                break
    if isinstance(data, (fits.PrimaryHDU, fits.ImageHDU)):
        header = data.header
        data = data.data
        if 'BUNIT' in header:
            dataunit = header['BUNIT']
    if wcs_transformation is None:
        try:
            wcs_transformation = WCS(header)
        except:
            pass
    if hasattr(data, 'unit'):
        dataunit = data.unit
    if unit is not None and dataunit is not None:
        dataunit = u.Unit(dataunit, parse_strict='warn')
        unit = u.Unit(unit, parse_strict='warn')
        if not isinstance(unit, u.UnrecognizedUnit):
            data = u.Quantity(data, unit=unit, copy=False)
            if not isinstance(dataunit, u.UnrecognizedUnit):
                if unit != dataunit:
                    warnings.warn('Unit of input data ({0}) and unit given '
                                  'by unit argument ({1}) are not identical.'
                                  .format(dataunit, unit))
        else:
            if not isinstance(dataunit, u.UnrecognizedUnit):
                data = u.Quantity(data, unit=dataunit, copy=False)
            else:
                warnings.warn('Neither the unit of the input data ({0}), nor '
                              'the unit given by the unit argument ({1}) is '
                              'parseable as a valid unit'
                              .format(dataunit, unit))
    elif unit is None:
        if dataunit is not None:
            dataunit = u.Unit(dataunit, parse_strict='warn')
            data = u.Quantity(data, unit=dataunit, copy=False)
        else:
            data = u.Quantity(data, copy=False)
    else:
        unit = u.Unit(unit, parse_strict='warn')
        data = u.Quantity(data, unit=unit, copy=False)
    if np.iscomplexobj(data):
        raise TypeError('Complex type not supported')
    if data.ndim != 2:
        raise ValueError('{0}-d array not supported. '
                         'Only 2-d arrays supported.'.format(data.ndim))
    if mask is not None or datamask is not None:
        if mask is None:
            mask = datamask
        else:
            mask = np.asarray(mask)
            if np.iscomplexobj(mask):
                raise TypeError('Complex type not supported')
            if mask.shape != data.shape:
                raise ValueError('Shapes of mask array and data array '
                                 'must match')
            if datamask is not None:
                mask = np.logical_or(mask, datamask)
    if (error is None) or (np.isscalar(error)):
        pixelwise_error = False
    if error is not None:
        if isinstance(error, u.Quantity):
            if np.isscalar(error.value):
                error = u.Quantity(np.broadcast_arrays(error, data),
                                   unit=error.unit)[0]
        elif np.isscalar(error):
            error = u.Quantity(np.broadcast_arrays(error, data),
                               unit=data.unit)[0]
        else:
            error = u.Quantity(error, unit=data.unit, copy=False)
        if error.shape != data.shape:
            raise ValueError('shapes of error array and data array must'
                             ' match')
    return data, wcs_transformation, mask, error, pixelwise_error
def _prepare_photometry_input ( data , unit , wcs , mask , error , pixelwise_error ) : dataunit = None datamask = None wcs_transformation = wcs if isinstance ( data , fits . HDUList ) : for i in range ( len ( data ) ) : if data [ i ] . data is not None : warnings . warn ( "Input data is a HDUList object, photometry is " "run only for the {0} HDU." . format ( i ) , AstropyUserWarning ) data = data [ i ] break if isinstance ( data , ( fits . PrimaryHDU , fits . ImageHDU ) ) : header = data . header data = data . data if 'BUNIT' in header : dataunit = header [ 'BUNIT' ] if wcs_transformation is None : try : wcs_transformation = WCS ( header ) except : pass if hasattr ( data , 'unit' ) : dataunit = data . unit if unit is not None and dataunit is not None : dataunit = u . Unit ( dataunit , parse_strict = 'warn' ) unit = u . Unit ( unit , parse_strict = 'warn' ) if not isinstance ( unit , u . UnrecognizedUnit ) : data = u . Quantity ( data , unit = unit , copy = False ) if not isinstance ( dataunit , u . UnrecognizedUnit ) : if unit != dataunit : warnings . warn ( 'Unit of input data ({0}) and unit given ' 'by unit argument ({1}) are not identical.' . format ( dataunit , unit ) ) else : if not isinstance ( dataunit , u . UnrecognizedUnit ) : data = u . Quantity ( data , unit = dataunit , copy = False ) else : warnings . warn ( 'Neither the unit of the input data ({0}), nor ' 'the unit given by the unit argument ({1}) is ' 'parseable as a valid unit' . format ( dataunit , unit ) ) elif unit is None : if dataunit is not None : dataunit = u . Unit ( dataunit , parse_strict = 'warn' ) data = u . Quantity ( data , unit = dataunit , copy = False ) else : data = u . Quantity ( data , copy = False ) else : unit = u . Unit ( unit , parse_strict = 'warn' ) data = u . Quantity ( data , unit = unit , copy = False ) if np . iscomplexobj ( data ) : raise TypeError ( 'Complex type not supported' ) if data . ndim != 2 : raise ValueError ( '{0}-d array not supported. ' 'Only 2-d arrays supported.' . format ( data . ndim ) ) if mask is not None or datamask is not None : if mask is None : mask = datamask else : mask = np . asarray ( mask ) if np . iscomplexobj ( mask ) : raise TypeError ( 'Complex type not supported' ) if mask . shape != data . shape : raise ValueError ( 'Shapes of mask array and data array ' 'must match' ) if datamask is not None : mask = np . logical_or ( mask , datamask ) if ( error is None ) or ( np . isscalar ( error ) ) : pixelwise_error = False if error is not None : if isinstance ( error , u . Quantity ) : if np . isscalar ( error . value ) : error = u . Quantity ( np . broadcast_arrays ( error , data ) , unit = error . unit ) [ 0 ] elif np . isscalar ( error ) : error = u . Quantity ( np . broadcast_arrays ( error , data ) , unit = data . unit ) [ 0 ] else : error = u . Quantity ( error , unit = data . unit , copy = False ) if error . shape != data . shape : raise ValueError ( 'shapes of error array and data array must' ' match' ) return data , wcs_transformation , mask , error , pixelwise_error
Parse photometry input.
Parse photometry input .
"""
    Parse photometry input.

    Photometry routines accept a wide range of inputs, e.g. ``data``
    could be (among others)  a numpy array, or a fits HDU.
    This requires some parsing and bookkeping to ensure that all inputs
    are complete and consistent.
    For example, the data could carry a unit and the wcs itself, so we need to
    check that it is consistent with the unit and wcs given as explicit
    parameters.

    Note that this function is meant to be used in addition to, not instead
    of, the `~astropy.nddata.support_nddata` decorator, i. e. ``data`` is
    never an `~astropy.nddata.NDData` object, because that will be split up
    in data, wcs, mask, ... keywords by the decorator already.

    See `~photutils.aperture_photometry` for a description of all
    possible input values.

    Returns
    -------
    data : `~astropy.units.Quantity` instance
    wcs_transformation : `~astropy.wcs.WCS` instance or None
    mask : np.array or None
    error : `~astropy.units.Quantity` instance or None
    """ # data was not fits so header is not defined or header is invalid # Let the calling application raise an error is it needs a WCS. # Check input array type and dimension. # Deal with the mask if it exists # combine the masks # Check whether we really need to calculate pixelwise errors, even if # requested.  If error is not an array, then it's not needed. # Check error shape.
data
unit
wcs
mask
error
pixelwise_error