PRO fullstack_xy, image, x_arr, y_arr, flux, img_stack=img_stack,$
             SIZE=size, SILENT=silent, $
             DCUBE=datacube, WCUBE=weightcube, $
             Nstacked=Nstacked, ID_stacked=id_stacked, $
             RESAMPLING=resampling, $
             MEDIAN=MEDIAN, ROTATE=ROTATE, $
             UNDEF=undef, USE_ALL=use_all, $
             WEIGHT=weight, WEI_STACK=wei_stack,$
                  aper_param=aper_param, band=band, psf=psf, unc_flux = unc_flux, Nboot=Nboot

;+
;  NAME: 
;     FULLSTACK_XY
;
;  PURPOSE:
;     Do the stacking of a catalog of sources
;
;  EXPLANATION:
;
;  CALLING SEQUENCE: 
;     fullstack_xy, image, x, y, imout, ...
;
;  INPUTS:     
;     IMAGE   : input 2D image 
;     X_ARR   : x coordinates of the sources to stack (in pixel, IDL convention), 
;     Y_ARR   : y coordinates of the sources to stack (in pixel, IDL convention), 
;
;  OPTIONAL INPUT KEYWORDS:
;     SIZE    : Output image will have a size of 2*size+1 x 2*size+1 in pixels.
;               Default value is 30 pix.
;     SILENT  : If it is not set, display warnings.
;     RESAMPLING : Interpolate (bilinearly) subimages in order to be
;                  precisely centered on the sources. No effect if HEALPIX=1
;     MEDIAN  : Compute median instead of mean. Default is to compute mean.
;     ROTATE  : If set, rotate each source by 90° * (k MOD 4).
;     UNDEF   : Only subimages without bad pixels are stacked. Values
;               of bad pixels are set with this keyword. Default value
;               is NaN.
;     HEALPIX : if set, the input image is considered as an Healpix
;               vector and coordinates are supposed to be galactic
;               coordinates (in degree).
;     WEIGHT: input map with uncertainties. Must have same size than
;                 the image map. Needed if ERR_STACK and/or WEIGHTCUBE
;                 output keyowrd are set. If set, the stacking will be
;                 weighted by this uncertainties.
;
;  OUTPUTS:  
;  FLUX     : Flux of the stacked artificial source in units of the map. 
;
;  OPTIONAL OUTPUT KEYWORDS:
;     DATACUBE : Contains the whole cube of data. 3D array which size
;                 is 2*size+1 x 2*size+1 x Nstacked 
;     IMG_STACK_OUT: 2D float array of size (2*size+1, 2*size+1)
;     UNC_FLUX : Unertainty on this flux computed by bootstrap.
;     IM_STACKED : 2D array of the result of the stacking. It
;                  corresponds, either to the mean or to the
;                  median. This array has 2*size+1 x 2*size+1 elements.
;      NSTACKED : Will contains the number of sources effectively
;                 stacked. It may differ of the number of elements of
;                 input arrays because sources that are close of the edges or out
;                 of the field of view or with bad pixels are
;                 rejected.
;      ID_STACKED : Return the ID of the stacked sources.
;      DCUBE      : Contains the whole cube of data. 3D array which size
;                 is 2*size+1 x 2*size+1 x Nstacked 
;      WCUBE      : Same as datacube but with the wheighting.
;      WEI_STACK : Stacking of weight map.
;
;  PROCEDURE: 
;      ZSCALE_RANGE
;
;  EXAMPLE:
;
;  MODIFICATION HISTORY:
;       9-Dec-2009: Written by Matthieu Bethermin
;
;     Copyright Insitut d'Astrophysique Spatiale
;
;     This program is free software: you can redistribute it and/or modify
;     it under the terms of the GNU General Public License as published by
;     the Free Software Foundation, either version 2 of the License, or
;     (at your option) any later version.
;
;     This program is distributed in the hope that it will be useful,
;     but WITHOUT ANY WARRANTY; without even the implied warranty of
;     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;     GNU General Public License for more details.
;
;     You should have received a copy of the GNU General Public License
;     along with this program.  If not, see <http://www.gnu.org/licenses/>.
;
;-  

if arg_present(unc_flux) then dcube = 1
if arg_present(unc_flux) and keyword_set(weight) then wcube = 1

dostack_xy, image, x_arr, y_arr, img_stack, $
            SIZE=size, SILENT=silent, $
            DCUBE=dcube, WCUBE=wcube, $
            Nstacked=Nstacked, ID_stacked=id_stacked, $
            RESAMPLING=resampling, $
            MEDIAN=MEDIAN, ROTATE=ROTATE, $
            UNDEF=undef, USE_ALL=use_all, $
            WEIGHT=weight, WEI_STACK=wei_stack

stacking_photometry, img_stack, flux, aper_param = aper_param, band = band, psf = psf

if arg_present(unc_flux) then stacking_bootstrap, dcube, unc_flux,$
   wcube = wcube, aper_param = aper_param, band = band, psf = psf, Nboot = Nboot

END
