; NAME: ; SDEV ; ; VERSION: 1 ; ; PURPOSE: ; Return the mean and standard deviation of a sample of numbers ; ; CALLING SEQUENCE: ; answer = sdev(numbers, [prnt=prnt, help=help]) ; ; INPUTS: ; numbers- An array of numbers ; ; OPTIONAL KEYWORD INPUTS: ; prnt- Set to print result to screen ; help- Set to nonzero scalar to have SDEV print syntax ; ; OUTPUT: ; answer- two element floating point vector with mean and s.d. ; ; EXTERNAL CALLS: ; NONE ; ; COMMON BLOCKS: ; NONE ; ; MODIFICATION HISTORY: ; - Written by Eric Volquardsen, NASA IRTF, Feb. 2003 ; ; ======================================================================= FUNCTION sdev, numbers, answer, prnt=prnt, help=help ; CHECK INPUTS IF n_elements(help) EQ 1 THEN BEGIN IF help NE 0 THEN help = 1 ENDIF ELSE help = 0 IF n_elements(numbers) LT 2 THEN help = 1 IF help EQ 1 THEN BEGIN print, '*********************************************' print, '*** SDEV SYNTAX ***' print, '*** > answer = sdev(numbers, print=print) ***' print, '*** ***' print, '*** numbers- array of numbers ***' print, '*** print- set to print results ***' print, '*********************************************' RETURN, 0 ENDIF numbers = float(numbers) IF n_elements(prnt) NE 1 THEN prnt = 0 IF prnt NE 0 THEN prnt = 1 ; CALCULATE MEAN n = float(n_elements(numbers)) mn = total(numbers)/n ; CALCULATE STANDARD DEVIATION stp1 = (numbers - mn)^2 stp2 = (total(stp1))/(n-1) sd = sqrt(stp2) ; GIVE ANSWER answer = [mn, sd] mn = strtrim(string(mn), 2) sd = strtrim(string(sd), 2) IF prnt EQ 1 THEN BEGIN print, 'mean = ', mn print, 'standard deviation = ', sd ENDIF RETURN, answer END