PRO stacking_bootstrap, datacube, eflux, wcube = wcube, aper_param = aper_param, band = band, psf = psf, Nboot = Nboot, median = median

;+
; NAME: 
;  stacking_bootstrap
;
; PURPOSE:
;  perform photometry of the stacked image.
;
; CALLING SEQUENCE:
;  stacking_bootstrap, datacube, eflux, aper_param = aper_param
;  ,band= band, psf = psf, Nboot = Nboot, median = median
;
; INPUTS:
;  DATACUBE: (2*size+1,2*size+1,Nsources) datacube coming from a
;  dostack routine
;  
; KEYWORD PARAMETERS:
;  WCUBE: (2*size+1,2*size+1,Nsources) weight cube coming from a
;  dostack routine
;  NBOOT: Number of bootstrap (initial:100)
;  MEDIAN: Perform median stacking instead of mean stacking. (Warning!
;  Median is not compatible with WCUBE)
;
;  WARNING=> You must choose one (and only one) of the three next keyword!!!
;  APER_PARAM: 4-elements vector containing the aperture radius in pixel, the
;  minimum and maximum radii 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. Performs PSF fitting photometry.
;
; OUTPUTS:
;  EFLUX: Unertainty on the flux of the stacked source in the
;  center of the map
;
; MODIFICATION HISTORY:
;  06/10/2009: written M. Bethermin (IAS)
;  09/12/2009: modified by M. Bethermin (add weighted stacking)
;
;     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(Nboot) ne 1 then Nboot = 100

  Nx = (size(datacube))(1) 
  Ny = (size(datacube))(2) 
  Nsources = (size(datacube))(3) 

  flux_vec = fltarr(Nboot)
  
  for k = 0, Nboot-1 do begin
     sel = floor(Nsources*randomu(seed,Nsources))
     bad = where(sel eq Nsources)
     if bad(0) ne -1 then sel(bad) = Nsources-1 

     if keyword_set(wcube) then begin
        if keyword_set(median) then begin
           message, print, 'Warning! Weighted stacking is meaningless with median stacking!', /cont  
           imstack = median(datacube(*,*,sel), dimension = 3)
        endif else imstack = total(datacube(*,*,sel)*wcube(*,*,sel),3)*1./total(wcube(*,*,sel),3)       
     endif else begin
        if keyword_set(median) then imstack = median(datacube(*,*,sel), dimension = 3) else imstack = total(datacube(*,*,sel),3)*1./Nsources
     endelse

     stacking_photometry, imstack, flux_temp, aper_param = aper_param, band = band, psf = psf

     flux_vec(k) = flux_temp

  endfor

  eflux = stdev(flux_vec)

END
