;+
; NAME:
;       make_z_distrib
;
; PURPOSE:
;  Make z distribution from the "cube" created using create_counts
;  for sources with flux S>flux_cut_mJy
;
;
; CATEGORY:
;
;
; CALLING SEQUENCE:
;  make_z_distrib 
; 
; INPUTS:
;  wavele in meter
;  save file IDL
;  Cut in flux in mJy
;	
; OUTPUTS:
;  z_plot
;  z_distrib :     redshift distribution of the sources
;  z_distrib_fond: redshift distrbution of the sources contributing to the CIB
;  z_distrib_fluc: redshift distribution of the sources contributing to the fluctuations 
;
; PROCEDURE:
;  None
;
; EXAMPLE:
;  make_z_distrib, 170.e-6, 180., 1, zpl_170, zdis_170, zdis_170_fond, zdis_170_fluc, file='create_counts_0170_Omega_lambda.save'
;
;
; MODIFICATION HISTORY:
;      12 feb-2000: written G. lagache
;      18 May 2002: make up for the delivery, G. Lagache
;-
PRO make_z_distrib, wave, flux_cut_mJy, wnum, z_plot, z_distrib, z_distrib_fond, z_distrib_fluc, plot=plot, STOP=STOP, file=file, verb = verb

loadct_plot

; Restore arrays
;---------------
IF KEYWORD_SET(file) THEN BEGIN 
    restore, file
ENDIF ELSE BEGIN
    print, 'Please, enter your file name'
ENDELSE



; COMPUTE DNDS
;--------------
delta_lnlum = (-alog(lum_array) + shift(alog(lum_array),-1))(0)
dsdz_new = dsdz
For il =0, n_elements(lum_array)-1 DO dsdz_new(il, *) = smooth(abs(dsdz(il,*)), 15) ; smooth discontinuities
dndlnlds = dndlnldz / dsdz_new/ 1.d0
bad=where(dsdz EQ 0 OR finite(dndlnldz) EQ 0, count_bad)
IF count_bad GT 0 THEN  dndlnlds(bad)=0.d0

; Creation of flux table
;-----------------------
s_start = -7.                   ; start in log(Jy)
s_end = 3.                      ; end in log(Jy)
s_step = .05                    ; Prendre un pas petit pour "IF (Sjy(i) GE S1(il) and sJy(i) LE S2(il))"
                                ; mais pas en S doit etre plus grand que pas en L quand discontinuites 
s_loopend = FIX((s_end - s_start) / s_step)
sjy = FLTARR(s_loopend +1)
FOR is = 0, s_loopend DO BEGIN  
    sjy(is) = s_start + is * s_step
ENDFOR                          

delta_s = sJy *0.
for i=0, n_elements(sJy) -2 do delta_s(i) = - 10^sJy(i) + 10^sJy(i+1)

; FIND POINTS WHERE THE DS/DZ MAY SHOW DISCONTINUITIES
; (Case for large wavelegnths)
;-----------------------------------------------------
z1 = fltarr(n_elements(lum_array))
z2 = z1
S2 = z1
S1 = z1

FOR ilum = 0, n_elements(lum_array) -1 DO BEGIN
    toto=reform(dsdz(ilum, *))
    tutu=toto
    For i=0, n_elements(z)-1 DO tutu(i) = abs(toto(i)) -toto(i) 
    g=where(tutu NE 0, count)
    IF count GT 1 THEN  BEGIN   ; Si 1 seul point negatif: c'est ignore
        z1(ilum) = z(g(0)) 
        z2(ilum) = z(g(count-1))
        S1(ilum) = alog10(Slz(ilum, g(0)))
        S2(ilum) = alog10(Slz(ilum, g(count-1)))
    ENDIF
ENDFOR

; COMPUTE dN/dS's
;--------------------
dnds=sjy*0

; per redshift slice 
;--------------------
dnds_025 = dnds
dnds_050 = dnds
dnds_075 = dnds
dnds_100 = dnds
dnds_125 = dnds
dnds_150 = dnds
dnds_175 = dnds
dnds_200 = dnds
dnds_225 = dnds
dnds_250 = dnds
dnds_300 = dnds
dnds_350 = dnds
dnds_400 = dnds
dnds_500 = dnds
dnds_800 = dnds

dn1 =0 & dn2=0 & dn3 =0

FOR i =0, n_elements(sJy) -2 do BEGIN ; LOOP on fluxes  

    bv =0*1.d0

    FOR il = 0, N_elements(lum_array) -1 do BEGIN ; LOOP on L
        IF z1(il) EQ 0 THEN BEGIN ; PAS DE CHANGEMENT DE PENTE DANS LE SLz(il, *)
            temp_s = alog10(slz(il, *))
            good = where(abs(sJy(i)-temp_s) Eq min(abs(sJy(i)-temp_s)) AND Sjy(i) GE min(temp_s) AND Sjy(i) LE max(temp_s), count) 
            IF count GT 0 THEN BEGIN 
                bv = bv + dndlnlds(il,good)* delta_lnlum * delta_s(i)
                zgood = (z(good))(0)
                dnz = (dndlnlds(il,good)* delta_lnlum)(0)
            ENDIF ELSE BEGIN
                dnz=0 & zgood=0
            ENDELSE
            
            IF zgood LT 0.25                   THEN dnds_025(i) = dnds_025(i) + dnz *delta_s(i)
            IF zgood GE 0.25 AND zgood LT 0.50 THEN dnds_050(i) = dnds_050(i) + dnz *delta_s(i)
            IF zgood GE 0.50 AND zgood LT 0.75 THEN dnds_075(i) = dnds_075(i) + dnz *delta_s(i)
            IF zgood GE 0.75 AND zgood LT 1.00 THEN dnds_100(i) = dnds_100(i) + dnz *delta_s(i)
            IF zgood GE 1.00 AND zgood LT 1.25 THEN dnds_125(i) = dnds_125(i) + dnz *delta_s(i)
            IF zgood GE 1.25 AND zgood LT 1.50 THEN dnds_150(i) = dnds_150(i) + dnz *delta_s(i)
            IF zgood GE 1.50 AND zgood LT 1.75 THEN dnds_175(i) = dnds_175(i) + dnz *delta_s(i)
            IF zgood GE 1.75 AND zgood LT 2.00 THEN dnds_200(i) = dnds_200(i) + dnz *delta_s(i)
            IF zgood GE 2.00 AND zgood LT 2.25 THEN dnds_225(i) = dnds_225(i) + dnz *delta_s(i)
            IF zgood GE 2.25 AND zgood LT 2.50 THEN dnds_250(i) = dnds_250(i) + dnz *delta_s(i)
            IF zgood GE 2.50 AND zgood LT 3.00 THEN dnds_300(i) = dnds_300(i) + dnz *delta_s(i)
            IF zgood GE 3.00 AND zgood LT 3.50 THEN dnds_350(i) = dnds_350(i) + dnz *delta_s(i)
            IF zgood GE 3.50 AND zgood LT 4.   THEN dnds_400(i) = dnds_400(i) + dnz *delta_s(i)
            IF zgood GE 4.00 AND zgood LT 5.   THEN dnds_500(i) = dnds_500(i) + dnz *delta_s(i)
            IF zgood GE 5.00                   THEN dnds_800(i) = dnds_800(i) + dnz *delta_s(i)
            

            ENDIF ELSE BEGIN 
                
                IF z1(il) LT z2(il) AND z2(il) LE max(z) THEN BEGIN ; 2 CHANGEMENTS DE PENTE
                    
                    IF (Sjy(i) GT S2(il) OR Sjy(i) LT S1(il)) THEN BEGIN
                        temp_s = alog10(slz(il, *))
                        good = where(abs(sJy(i)-temp_s) Eq min(abs(sJy(i)-temp_s)) AND Sjy(i) GE min(temp_s) AND Sjy(i) LE max(temp_s), count) 

                        IF count GT 0 THEN BEGIN 
                            bv = bv + dndlnlds(il,good)* delta_lnlum * delta_s(i)
                            zgood = (z(good))(0)
                            dnz = (dndlnlds(il,good)* delta_lnlum)(0)
                        ENDIF ELSE BEGIN
                            dnz= 0 & zgood=0
                        ENDELSE

                        IF zgood LT 0.25                   THEN dnds_025(i) = dnds_025(i) + dnz *delta_s(i)
                        IF zgood GE 0.25 AND zgood LT 0.50 THEN dnds_050(i) = dnds_050(i) + dnz *delta_s(i)
                        IF zgood GE 0.50 AND zgood LT 0.75 THEN dnds_075(i) = dnds_075(i) + dnz *delta_s(i)
                        IF zgood GE 0.75 AND zgood LT 1.00 THEN dnds_100(i) = dnds_100(i) + dnz *delta_s(i)
                        IF zgood GE 1.00 AND zgood LT 1.25 THEN dnds_125(i) = dnds_125(i) + dnz *delta_s(i)
                        IF zgood GE 1.25 AND zgood LT 1.50 THEN dnds_150(i) = dnds_150(i) + dnz *delta_s(i)
                        IF zgood GE 1.50 AND zgood LT 1.75 THEN dnds_175(i) = dnds_175(i) + dnz *delta_s(i)
                        IF zgood GE 1.75 AND zgood LT 2.00 THEN dnds_200(i) = dnds_200(i) + dnz *delta_s(i)
                        IF zgood GE 2.00 AND zgood LT 2.25 THEN dnds_225(i) = dnds_225(i) + dnz *delta_s(i)
                        IF zgood GE 2.25 AND zgood LT 2.50 THEN dnds_250(i) = dnds_250(i) + dnz *delta_s(i)
                        IF zgood GE 2.50 AND zgood LT 3.00 THEN dnds_300(i) = dnds_300(i) + dnz *delta_s(i)
                        IF zgood GE 3.00 AND zgood LT 3.50 THEN dnds_350(i) = dnds_350(i) + dnz *delta_s(i)
                        IF zgood GE 3.50 AND zgood LT 4.   THEN dnds_400(i) = dnds_400(i) + dnz *delta_s(i)
                        IF zgood GE 4.00 AND zgood LT 5.   THEN dnds_500(i) = dnds_500(i) + dnz *delta_s(i)
                        IF zgood GE 5.00                   THEN dnds_800(i) = dnds_800(i) + dnz *delta_s(i)
                    ENDIF

                    IF (Sjy(i) GE S1(il) and sJy(i) LE S2(il)) THEN BEGIN
                        ;print, 'I am here!'
                        good1 = where(z LT z1(il)) & zgood1= z(good1)
                        temp_s = alog10(slz(il, good1)) & dn1= dndlnlds(il,good1)
                        good1 = where(abs(sJy(i)-temp_s) Eq min(abs(sJy(i)-temp_s)) AND Sjy(i) GE min(temp_s) AND Sjy(i) LE max(temp_s), count1)
                        IF count1 GT 0 THEN BEGIN 
                            dn1= (dn1(good1))(0) & zgood1 = (zgood1(good1))(0)
                        ENDIF ELSE BEGIN
                            dn1 = 0  & zgood1=0
                        ENDELSE 
                        
                        IF zgood1 LT 0.25                    THEN dnds_025(i) = dnds_025(i) + dn1  * delta_lnlum(0) *delta_s(i)
                        IF zgood1 GE 0.25 AND zgood1 LT 0.50 THEN dnds_050(i) = dnds_050(i) + dn1  * delta_lnlum(0) *delta_s(i)
                        IF zgood1 GE 0.50 AND zgood1 LT 0.75 THEN dnds_075(i) = dnds_075(i) + dn1  * delta_lnlum(0) *delta_s(i)
                        IF zgood1 GE 0.75 AND zgood1 LT 1.00 THEN dnds_100(i) = dnds_100(i) + dn1  * delta_lnlum(0) *delta_s(i)
                        IF zgood1 GE 1.00 AND zgood1 LT 1.25 THEN dnds_125(i) = dnds_125(i) + dn1  * delta_lnlum(0) *delta_s(i)
                        IF zgood1 GE 1.25 AND zgood1 LT 1.50 THEN dnds_150(i) = dnds_150(i) + dn1  * delta_lnlum(0) *delta_s(i)
                        IF zgood1 GE 1.50 AND zgood1 LT 1.75 THEN dnds_175(i) = dnds_175(i) + dn1  * delta_lnlum(0) *delta_s(i)
                        IF zgood1 GE 1.75 AND zgood1 LT 2.00 THEN dnds_200(i) = dnds_200(i) + dn1  * delta_lnlum(0) *delta_s(i)
                        IF zgood1 GE 2.00 AND zgood1 LT 2.25 THEN dnds_225(i) = dnds_225(i) + dn1  * delta_lnlum(0) *delta_s(i)
                        IF zgood1 GE 2.25 AND zgood1 LT 2.50 THEN dnds_250(i) = dnds_250(i) + dn1  * delta_lnlum(0) *delta_s(i)
                        IF zgood1 GE 2.50 AND zgood1 LT 3.00 THEN dnds_300(i) = dnds_300(i) + dn1  * delta_lnlum(0) *delta_s(i)
                        IF zgood1 GE 3.00 AND zgood1 LT 3.50 THEN dnds_350(i) = dnds_350(i) + dn1  * delta_lnlum(0) *delta_s(i)
                        IF zgood1 GE 3.50 AND zgood1 LT 4.   THEN dnds_400(i) = dnds_400(i) + dn1  * delta_lnlum(0) *delta_s(i)
                        IF zgood1 GE 4.00 AND zgood1 LT 5.   THEN dnds_500(i) = dnds_500(i) + dn1  * delta_lnlum(0) *delta_s(i)
                        IF zgood1 GE 5.00                    THEN dnds_800(i) = dnds_800(i) + dn1  * delta_lnlum(0) *delta_s(i)
                        
                        good2 =  where(z GE z1(il) and z LT z2(il)) & zgood2= z(good2)
                        temp_s = alog10(slz(il, good2)) & dn2= dndlnlds(il,good2)
                        good2 = where(abs(sJy(i)-temp_s) Eq min(abs(sJy(i)-temp_s)) AND Sjy(i) GE min(temp_s) AND Sjy(i) LE max(temp_s), count2)
                        IF count2 GT 0 THEN BEGIN 
                            dn2= (dn2(good2))(0) & zgood2 = (zgood2(good2))(0)
                        ENDIF ELSE BEGIN
                            dn2 = 0 & zgood2 =0
                        ENDELSE 
                        
                        IF zgood2 LT 0.25                    THEN dnds_025(i) = dnds_025(i) + dn2  * delta_lnlum(0) *delta_s(i)
                        IF zgood2 GE 0.25 AND zgood2 LT 0.50 THEN dnds_050(i) = dnds_050(i) + dn2  * delta_lnlum(0) *delta_s(i)
                        IF zgood2 GE 0.50 AND zgood2 LT 0.75 THEN dnds_075(i) = dnds_075(i) + dn2  * delta_lnlum(0) *delta_s(i)
                        IF zgood2 GE 0.75 AND zgood2 LT 1.00 THEN dnds_100(i) = dnds_100(i) + dn2  * delta_lnlum(0) *delta_s(i)
                        IF zgood2 GE 1.00 AND zgood2 LT 1.25 THEN dnds_125(i) = dnds_125(i) + dn2  * delta_lnlum(0) *delta_s(i)
                        IF zgood2 GE 1.25 AND zgood2 LT 1.50 THEN dnds_150(i) = dnds_150(i) + dn2  * delta_lnlum(0) *delta_s(i)
                        IF zgood2 GE 1.50 AND zgood2 LT 1.75 THEN dnds_175(i) = dnds_175(i) + dn2  * delta_lnlum(0) *delta_s(i)
                        IF zgood2 GE 1.75 AND zgood2 LT 2.00 THEN dnds_200(i) = dnds_200(i) + dn2  * delta_lnlum(0) *delta_s(i)
                        IF zgood2 GE 2.00 AND zgood2 LT 2.25 THEN dnds_225(i) = dnds_225(i) + dn2  * delta_lnlum(0) *delta_s(i)
                        IF zgood2 GE 2.25 AND zgood2 LT 2.50 THEN dnds_250(i) = dnds_250(i) + dn2  * delta_lnlum(0) *delta_s(i)
                        IF zgood2 GE 2.50 AND zgood2 LT 3.00 THEN dnds_300(i) = dnds_300(i) + dn2  * delta_lnlum(0) *delta_s(i)
                        IF zgood2 GE 3.00 AND zgood2 LT 3.50 THEN dnds_350(i) = dnds_350(i) + dn2  * delta_lnlum(0) *delta_s(i)
                        IF zgood2 GE 3.50 AND zgood2 LT 4.   THEN dnds_400(i) = dnds_400(i) + dn2  * delta_lnlum(0) *delta_s(i)
                        IF zgood2 GE 4.00 AND zgood2 LT 5.   THEN dnds_500(i) = dnds_500(i) + dn2  * delta_lnlum(0) *delta_s(i)
                        IF zgood2 GE 5.00                    THEN dnds_800(i) = dnds_800(i) + dn2  * delta_lnlum(0) *delta_s(i)
                        
                        good3 =  where(z GE z2(il)) & zgood3= z(good3)
                        temp_s = alog10(slz(il, good3)) & dn3= dndlnlds(il,good3)
                        good3 = where(abs(sJy(i)-temp_s) Eq min(abs(sJy(i)-temp_s)) AND Sjy(i) GE min(temp_s) AND Sjy(i) LE max(temp_s), count3)
                        IF count3 GT 0 THEN BEGIN 
                            dn3= (dn3(good3))(0) & zgood3 = (zgood3(good3))(0)
                        ENDIF ELSE BEGIN
                            dn3 = 0 & zgood3=0
                        ENDELSE 
                        
                        IF zgood3 LT 0.25                    THEN dnds_025(i) = dnds_025(i) + dn3  * delta_lnlum(0) *delta_s(i)
                        IF zgood3 GE 0.25 AND zgood3 LT 0.50 THEN dnds_050(i) = dnds_050(i) + dn3  * delta_lnlum(0) *delta_s(i)
                        IF zgood3 GE 0.50 AND zgood3 LT 0.75 THEN dnds_075(i) = dnds_075(i) + dn3  * delta_lnlum(0) *delta_s(i)
                        IF zgood3 GE 0.75 AND zgood3 LT 1.00 THEN dnds_100(i) = dnds_100(i) + dn3  * delta_lnlum(0) *delta_s(i)
                        IF zgood3 GE 1.00 AND zgood3 LT 1.25 THEN dnds_125(i) = dnds_125(i) + dn3  * delta_lnlum(0) *delta_s(i)
                        IF zgood3 GE 1.25 AND zgood3 LT 1.50 THEN dnds_150(i) = dnds_150(i) + dn3  * delta_lnlum(0) *delta_s(i)
                        IF zgood3 GE 1.50 AND zgood3 LT 1.75 THEN dnds_175(i) = dnds_175(i) + dn3  * delta_lnlum(0) *delta_s(i)
                        IF zgood3 GE 1.75 AND zgood3 LT 2.00 THEN dnds_200(i) = dnds_200(i) + dn3  * delta_lnlum(0) *delta_s(i)
                        IF zgood3 GE 2.00 AND zgood3 LT 2.25 THEN dnds_225(i) = dnds_225(i) + dn3  * delta_lnlum(0) *delta_s(i)
                        IF zgood3 GE 2.25 AND zgood3 LT 2.50 THEN dnds_250(i) = dnds_250(i) + dn3  * delta_lnlum(0) *delta_s(i)
                        IF zgood3 GE 2.50 AND zgood3 LT 3.00 THEN dnds_300(i) = dnds_300(i) + dn3  * delta_lnlum(0) *delta_s(i)
                        IF zgood3 GE 3.00 AND zgood3 LT 3.50 THEN dnds_350(i) = dnds_350(i) + dn3  * delta_lnlum(0) *delta_s(i)
                        IF zgood3 GE 3.50 AND zgood3 LT 4.   THEN dnds_400(i) = dnds_400(i) + dn3  * delta_lnlum(0) *delta_s(i)
                        IF zgood3 GE 4.00 AND zgood3 LT 5.   THEN dnds_500(i) = dnds_500(i) + dn3  * delta_lnlum(0) *delta_s(i)
                        IF zgood3 GE 5.00                    THEN dnds_800(i) = dnds_800(i) + dn3  * delta_lnlum(0) *delta_s(i)
                        
                        bv = bv + (dn1 + dn2 + dn3)* delta_lnlum * delta_s(i)
                    ENDIF
                    
                ENDIF ELSE BEGIN
                    print, 'Erreur'
                    STOP
                ENDELSE      
                
            ENDELSE
            
        
    ENDFOR  

    dnds(i) = bv   ; = dn

ENDFOR

; Plot dN/dS and Z contributions
;-------------------------------
IF KEYWORD_SET(plot) THEN BEGIN

  window, wnum, xs=550, ys=550
  SJ = 10.^sjy
  plot, sjy, alog10(dnds*SJ^2.5), title='dn/ds and Redshift contribs' ;, yr=[-6, 4], /ysty, xr=[-4, 2], /xsty
  oplot, sjy, alog10(dnds_025*SJ^2.5), color=1
  oplot, sjy, alog10(dnds_050*SJ^2.5), color=2
  oplot, sjy, alog10(dnds_100*SJ^2.5), color=3
  oplot, sjy, alog10(dnds_200*SJ^2.5), color=4
ENDIF

; Z DISTRIB
;----------
good=where(10.^sJy GT flux_cut_mJy*1.e-3 and 10.^sJy LT 3.)

z_plot=[0., 0.25, 0.5, 0.75, 1., 1.25, 1.5, 1.75, 2., 2.25, 2.5, 3., 3.5, 4., 5., 8.]
z_distrib = fltarr(n_elements(z_plot)-1)
z_distrib_fond = z_distrib
z_distrib_fluc = z_distrib

z_distrib(0) = total(dnds_025(good)) 
z_distrib(1) = total(dnds_050(good)) 
z_distrib(2) = total(dnds_075(good)) 
z_distrib(3) = total(dnds_100(good)) 
z_distrib(4) = total(dnds_125(good)) 
z_distrib(5) = total(dnds_150(good)) 
z_distrib(6) = total(dnds_175(good)) 
z_distrib(7) = total(dnds_200(good)) 
z_distrib(8) = total(dnds_225(good)) 
z_distrib(9) = total(dnds_250(good)) 
z_distrib(10) = total(dnds_300(good))
z_distrib(11) = total(dnds_350(good))
z_distrib(12) = total(dnds_400(good))
z_distrib(13) = total(dnds_500(good))
z_distrib(14) = total(dnds_800(good))

z_distrib_fond(0) = total(dnds_025(good)*10.^sjy(good)) 
z_distrib_fond(1) = total(dnds_050(good)*10.^sjy(good)) 
z_distrib_fond(2) = total(dnds_075(good)*10.^sjy(good)) 
z_distrib_fond(3) = total(dnds_100(good)*10.^sjy(good)) 
z_distrib_fond(4) = total(dnds_125(good)*10.^sjy(good)) 
z_distrib_fond(5) = total(dnds_150(good)*10.^sjy(good)) 
z_distrib_fond(6) = total(dnds_175(good)*10.^sjy(good)) 
z_distrib_fond(7) = total(dnds_200(good)*10.^sjy(good)) 
z_distrib_fond(8) = total(dnds_225(good)*10.^sjy(good)) 
z_distrib_fond(9) = total(dnds_250(good)*10.^sjy(good)) 
z_distrib_fond(10) = total(dnds_300(good)*10.^sjy(good))
z_distrib_fond(11) = total(dnds_350(good)*10.^sjy(good))
z_distrib_fond(12) = total(dnds_400(good)*10.^sjy(good))
z_distrib_fond(13) = total(dnds_500(good)*10.^sjy(good))
z_distrib_fond(14) = total(dnds_800(good)*10.^sjy(good))

z_distrib_fluc(0) = total(dnds_025(good)*(10.^sjy(good))^2.)
z_distrib_fluc(1) = total(dnds_050(good)*(10.^sjy(good))^2.)   
z_distrib_fluc(2) = total(dnds_075(good)*(10.^sjy(good))^2.)   
z_distrib_fluc(3) = total(dnds_100(good)*(10.^sjy(good))^2.)  
z_distrib_fluc(4) = total(dnds_125(good)*(10.^sjy(good))^2.)   
z_distrib_fluc(5) = total(dnds_150(good)*(10.^sjy(good))^2.)   
z_distrib_fluc(6) = total(dnds_175(good)*(10.^sjy(good))^2.)   
z_distrib_fluc(7) = total(dnds_200(good)*(10.^sjy(good))^2.)   
z_distrib_fluc(8) = total(dnds_225(good)*(10.^sjy(good))^2.)   
z_distrib_fluc(9) = total(dnds_250(good)*(10.^sjy(good))^2.)  
z_distrib_fluc(10) = total(dnds_300(good)*(10.^sjy(good))^2.)  
z_distrib_fluc(11) = total(dnds_350(good)*(10.^sjy(good))^2.)  
z_distrib_fluc(12) = total(dnds_400(good)*(10.^sjy(good))^2.)  
z_distrib_fluc(13) = total(dnds_500(good)*(10.^sjy(good))^2.)  
z_distrib_fluc(14) = total(dnds_800(good)*(10.^sjy(good))^2.)  

title = 'Redshift distribution '+STRING(flag_wave, FORMAT='(I4)')+' microns'
IF KEYWORD_SET(notitle) THEN title = ''

IF KEYWORD_SET(plot) THEN BEGIN

    window, wnum+1, xs=400, ys=400

    !P.charsize=1.2
    plot, z_plot, z_plot*0+1., yr=[0, max(z_distrib+0.1)], xr=[0., 8.], /xsty, /ysty, title=title, $
      xtitle='Redshift'  
    for i =0, n_elements(z_plot) -2 do oplot, [z_plot(i), z_plot(i+1)], [z_distrib(i), z_distrib(i)]
    for i=0, n_elements(z_plot) -3 do oplot, [z_plot(i+1), z_plot(i+1)], [z_distrib(i), z_distrib(i+1)]

    for i =0, n_elements(z_plot) -2 DO print, z_plot(i), '<z<', z_plot(i+1), '    : ', z_distrib(i), total(z_distrib(0:i))

ENDIF

IF KEYWORD_SET(verb) THEN BEGIN
    
    print, 'N(>S)', alog10(total(dnds(good))) 
    
    CFIRB=total(dnds*10.^sjy) * 3.e8/wave * 1.e-26 ; en W/m2/sr
    print, 'CFIRB', CFIRB, ' W/m2/sr'
    print, 'CFIRB', total(dnds*10.^sjy)/1.e6, ' MJy/sr'  

    print, 'For S>', flux_cut_mJy, '  mJy: '

    fluc=total(z_distrib_fluc)      ; en Jy^2/sr
    print, 'Fluctuations', fluc, '  Jy^2/sr'

    fond=total(z_distrib_fond) * 3.e8/wave * 1.e-26 ; en W/m2/sr
    print, 'Resolved CIB', total(z_distrib_fond)*1.e-6,'  MJy/sr' 

ENDIF


IF KEYWORD_SET(STOP) THEN STOP

END










