pro fhist_average,x,h,ri,number=w,mean=xm,sigma=xs,weight=weight $ ,range=mm,nbins=nbins,geth=geth if keyword_set(geth) or not keyword_set(h) then begin if n_elements(mm) ne 2 then mm = minmax_range(x) if not keyword_set(nbins) then nbins = 20 binsize = (mm[1]-mm[0])/nbins h = histogram( x,min=mm[0],max=mm[1],binsize=binsize,reverse=ri) ; note: histogram does not work properly with the nan keyword set ; the size of h will be nbins+1 print,h[nbins] h = h[0:nbins-1] ; help,names='*' endif ;nbins = n_elements(h) ;xm = replicate(!values.f_nan,nbins) w = make_array(value=0.,dimen=dimen(h)) wx = w wx2 = w if not keyword_set(weight) then weight= replicate(1.,n_elements(x)) weight_x = weight * x weight_x2= weight_x * x whn0 = where(h ne 0,count) for j=0,count-1 do begin i = whn0[j] ind = ri[ ri[i]: ri[i+1]-1 ] w[i] = total(weight[ind]) wx[i] = total(weight_x[ind]) wx2[i] = total(weight_x2[ind]) endfor xm = wx / w xs = sqrt( wx2/w - xm^2 ) end