PRO fullstack_healpix, skyinput, linput, binput, reso_arcmin, flux, img_stack_out=img_stack_out, WEIGHT=weight,DISPLAY=display, DATACUBE=datacube, Nstacked=Nstacked, SIZE=size, Nest=Nest, MASKCUBE=maskcube, masknstacked=masknstacked, mask_stack_output=mask_stack_output, ROTATE=ROTATE, $
                       aper_param=aper_param, band=band, psf=psf, unc_flux = unc_flux, Nboot=Nboot
  
;+
; NAME:
;   fullstack_healpix
;
; PURPOSE:
;   perform stacking on data in the Healpix format (sky vector)
;   uses Healpix2.12a and the photometry
;
; CATEGORY:
;
; CALLING SEQUENCE:
;
; INPUTS:
;    SKYINPUT: 1D healpix vector of the sky data (must conform to the Healpix standard)
;    LINPUT: 1D array of longitude positions in decimal degrees to stack
;    BINPUT: 1D array of latitude positions in decimal degrees to stack
;    RESO_ARCMIN: FLOAT asked output resolution of the stacked map in
;    arcmin
;
;
; OPTIONAL INPUTS:
;     WEIGHT  : input weight map (MUST be in the exact same format as
;               SKYINPUT), with zeros when no data are present, and
;               count hit for coverage elsewhere (== coverage map)
;     ROTATE  : If set, rotate each image (and weight) bu 90 degrees
;                prior to stacking; usefull when dealing with
;                structured background
;
;     SIZE    : Output image will have a size of 2*size+1 x 2*size+1 in pixels.
;               Default value is 30 pix (i.e. output map will be 61*61pixels ).
;               Has to be an even number.
;     DISPLAY : If set, display (on window 9) a "movie" of the
;               stacking. It is not recommended to used this keyword
;               if the number of sources is important because it takes
;               a lot of time.
;     SILENT  : If it is not set, display warnings.
;     NBOOT   : Number of bootstrap (initial:100) if you compute unc_flux.
;
;    WARNING=> You must choose one (and only one) of the three keyword!!!
;    APER_PARAM: 4-elements vector containing the aperture in pixel, the
;    minimum and maximum radius of the sky annulus in pixel, and the
;    aperture correction.
;    BAND: Integer corresponding to the selected band. Use aperture
;    photometry with standard parameters corresponding to a Spitzer
;    band (1 to 4:IRAC bands, 5 to 7: MIPS band). New bands can be
;    added in load_std_aper_param_stacking.pro
;    PSF: (2*size+1,2*size+1) array representing a centered PSF for a
;    unit flux source. Perform PSF fitting photometry.
;
; OUTPUTS:
;  FLUX     : Flux of the stacked artificial source in units of the map.
;
; OPTIONAL OUTPUTS:
;      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. 
;      IMG_STACK_OUT: 2D float array of size (2*size+1, 2*size+1) of
;      final stacked image (without weight management in all cases)
;
; COMMENTS:
;     - Uses Healpix library 2.12a or later
;
; EXAMPLE:
;
;
;
; MODIFICATION HISTORY:
;     16-Sep-2009: written M. Bethermin (IAS)
;     9-Dec-2009: M. Bethermin->add weighting keywords
;
;     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 keyword_set(unc_flux) then datacube = 1
  if keyword_set(unc_flux) and keyword_set(weight) then maskcube = 1

  dostack_healpix, skyinput, linput, binput, reso_arcmin, img_stack_output, WEIGHT=weight,DISPLAY=display, DATACUBE=datacube, Nstacked=Nstacked, SIZE=size, Nest=Nest,MASKCUBE=maskcube, masknstacked=masknstacked, mask_stack_output=mask_stack_output,ROTATE=ROTATE

  stacking_photometry, imstack, flux, aper_param = aper_param, band = band, psf = psf

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

END


