function hxrbs_deglitch, array, nsig, ratio, recur=recur, nw=nw, sig=sig, back=back, out1=out1

checkvar, ratio, 3
nel = n_elements(array)
back = [array(0),0.5*(array + array(2:*)),array(nel-1)]
back2= [array(0:1),0.5*(array + array(4:*)),array(nel-2:*)]
out = [array(0),array(1:*)] - back
out1= out
sp_ratio = f_div( array,  back>1.0)
sp_ratio2= f_div( array,  back2>1.0)
sig = sqrt( back > 0.0) > 1.
w = where( abs(out) gt nsig * sig and (abs(sp_ratio)>abs(sp_ratio2))  gt ratio , nw)
out = array
if nw ge 1 then out(w) = back(w)

if keyword_set(recur) then begin
    out = hxrbs_deglitch( out, nsig, nw=mw)
    while mw ge 1 do out = hxrbs_deglitch( out, nsig, nw=mw)

endif

return, out

end