;+
; NAME: avg_nonzero.pro
;
;
;
; PURPOSE:
; compute a mean without having to use moment, and does not
; average in zeros
;
; INPUTS:
; data: an array that contains the data you with to average
;
;
;
;
; OPTIONAL INPUTS:
; dimension: tells which dimension to total over, see help for total
;
;
; KEYWORD PARAMETERS:
; NAN: pass nan to total()
; double: perform the avergaing in double, pased to total()
;
;
; OUTPUTS:
; the mean of the array
;
;
; EXAMPLE:
; a = [ [1.00000,1.00000,2.00000,3.00000], $
; [4.00000,0.000000,6.00000,7.00000], $
; [8.00000,9.00000,0.000000,11.0000], $
; [12.0000,13.0000,14.0000,0.000000] ]
; IDL> print, avg_nonzero(a)
; 7.00000
; IDL> print, avg_nonzero(a,1)
; 1.75000 5.66667 9.33333 13.0000
; IDL> print, avg_nonzero(a,2)
; 6.25000 7.66667 7.33333 7.00000
;
;
; MODIFICATION HISTORY:
; 3/28/2003 Brian Larsen
; - created
;
; 7/01/2007 Brian Larsen
; - fixed up
;
; 7/24/2007 Brian Larsen
; - rewritten much smarter from
; http://groups.google.com/group/comp.lang.idl-pvwave/browse_frm/thread/baf04086fc71d348/ee7ef51147d32797#
;
;
;
;-
FUNCTION avg_nonzero, data, dimension, NAN=nan, DOUBLE=double
if n_elements(data) eq 0 then $
message, /ioerror, 'avg_nonzero requires an input array'
if n_elements(dimension) eq 0 then dimension=0
mean = total(data,dimension, NAN=nan, DOUBLE=double)/ $
(total(data<1.,dimension, NAN=nan, DOUBLE=double) > 1.)
return, mean
END