B
    |d%                 @   sL   d dl ZddlmZmZmZ ddlmZmZ ddl	m
Z
 dgZdd
dZdS )    N   )parse_input_dataparse_input_weightsparse_output_projection   )determine_offset_matrixsolve_corrections_sgd)ReprojectedArraySubsetreproject_and_coaddmeanFc
       '   	   K   sx  |dkrt d|dkr t dt||d\}}g }x>tt| D ],}t| | |d\}}|dkrld}n*t|| |d}tt|rt	|}|j
\}}td|d	 |d	 dg}tdd|d	 |d	 g}||||\}}tt|stt|r(d
}|d }d
}|d
 }nxtd
tt| d	 }t|d tt| d	 }td
tt| d	 }t|d
 tt| d	 }||k sD||k rqD|||||f }|| || f}|||ff|||d|
\}}|dk	r"|||ff|||d|
\}} t|}!d||!< d||!< |dk	rVd||!< ||9 }t||||||}|| qDW |rt|}"t|"}#|	r|#|#|	 8 }#x&t||#D ]\}}$| j|$8  _qW t|}%t|}&|dkr^xL|D ]D}d
|j|jd
k< |%|j  |j|j 7  < |&|j  |j7  < qW |dkrptjdd |%|& }%W dQ R X n|dkrptd|%|&fS )a
  
    Given a set of input images, reproject and co-add these to a single
    final image.

    This currently only works with 2-d images with celestial WCS.

    Parameters
    ----------
    input_data : iterable
        One or more input datasets to reproject and co-add. This should be an
        iterable containing one entry for each dataset, where a single dataset
        is one of:

            * The name of a FITS file
            * An `~astropy.io.fits.HDUList` object
            * An image HDU object such as a `~astropy.io.fits.PrimaryHDU`,
              `~astropy.io.fits.ImageHDU`, or `~astropy.io.fits.CompImageHDU`
              instance
            * A tuple where the first element is an `~numpy.ndarray` and the
              second element is either a `~astropy.wcs.WCS` or a
              `~astropy.io.fits.Header` object
            * An `~astropy.nddata.NDData` object from which the ``.data`` and
              ``.wcs`` attributes will be used as the input data.

    output_projection : `~astropy.wcs.WCS` or `~astropy.io.fits.Header`
        The output projection, which can be either a `~astropy.wcs.WCS`
        or a `~astropy.io.fits.Header` instance.
    shape_out : tuple, optional
        If ``output_projection`` is a `~astropy.wcs.WCS` instance, the
        shape of the output data should be specified separately.
    input_weights : iterable
        If specified, this should be an iterable with the same length as
        ``input_data``, where each item is one of:

            * The name of a FITS file
            * An `~astropy.io.fits.HDUList` object
            * An image HDU object such as a `~astropy.io.fits.PrimaryHDU`,
              `~astropy.io.fits.ImageHDU`, or `~astropy.io.fits.CompImageHDU`
              instance
            * An `~numpy.ndarray` array

    hdu_in : int or str, optional
        If one or more items in ``input_data`` is a FITS file or an
        `~astropy.io.fits.HDUList` instance, specifies the HDU to use.
    hdu_weights : int or str, optional
        If one or more items in ``input_weights`` is a FITS file or an
        `~astropy.io.fits.HDUList` instance, specifies the HDU to use.
    reproject_function : callable
        The function to use for the reprojection
    combine_function : { 'mean', 'sum', 'median' }
        The type of function to use for combining the values into the final
        image.
    match_background : bool
        Whether to match the backgrounds of the images.
    background_reference : `None` or `int`
        If `None`, the background matching will make it so that the average of
        the corrections for all images is zero. If an integer, this specifies
        the index of the image to use as a reference.
    kwargs
        Keyword arguments to be passed to the reprojection function.
    )r   summedianz1combine_function should be one of mean/sum/medianNzNreprojection function should be specified with the reproject_function argument)	shape_out)hdu_in)hdu_weightsg      g      ?r   r   )output_projectionr   r   g        )r   r   r   ignore)invalidr   z0combine_function='median' is not yet implemented)
ValueErrorr   rangelenr   r   npanyisnanZ
nan_to_numshapearrayZworld_to_pixelZpixel_to_worldmaxintfloorminceilr	   appendr   r   zipZzeros	footprintZview_in_original_arrayZerrstateNotImplementedError)'Z
input_datar   r   Zinput_weightsr   Zreproject_functionr   Zcombine_functionZmatch_backgroundZbackground_referencekwargsZwcs_outZarraysZidataZarray_inZwcs_inZ
weights_innynxZxcZycZxc_outZyc_outZiminZimaxZjminZjmaxZwcs_out_indivZshape_out_indivr   r#   weights_resetZoffset_matrixZcorrectionsZ
correctionZfinal_arrayZfinal_footprint r+   g/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/reproject/mosaicking/coadd.pyr
      s    J


$
  










)NNNNNr   FN)numpyr   utilsr   r   r   
backgroundr   r   Zsubset_arrayr	   __all__r
   r+   r+   r+   r,   <module>   s      