
PRO ERRORS, I2, O2, I1, O1, ratio, error, silent=silent, sum=sum

;+
; NAME
;
;     ERRORS
;
; EXPLANATION
;
;     Given two line intensities/fluxes and their 1-sigma errors, 
;     calculates the ratio and the 1-sigma error on the ratio.
;
; INPUTS
;
;     I2    Intensity/flux of numerator line. Can be an array.
;
;     O2    1-sigma error of numerator line. Can be an array.
;
;     I1    Intensity/flux of numerator line. Can be an array.
;
;     O1    1-sigma error of numerator line. Can be an array.
;
; KEYWORDS
;
;     SILENT  If set, then the output parameters are not printed to the
;             screen.
;
;     SUM     By default, if the input parameters I1, O1, etc., are arrays
;             then ERRORS treats each element separately and returns RATIO
;             and ERROR as arrays. By specifying the /SUM keyword, ERRORS
;             will add together the individual elements of I1 and I2, and
;             compute a sum of squares for the sigmas. RATIO and ERROR are
;             then returned as scalars.
;
; OUTPUTS
;
;     RATIO  The line line ratio, I2/I1.
;
;     ERROR  1-sigma error on ratio
;
; HISTORY
;
;     Ver.1, 5-May-1999, Peter Young
;     Ver.2, 5-Aug-2003, Peter Young
;          added /SUM keyword
;-

if n_params() lt 4 then begin
   print,'Use:  IDL> errors,i2,o2,i1,o1 [,ratio,error,/silent,/sum]'
   return
endif

i1=double(i1) & i2=double(i2)
o1=double(o1) & o2=double(o2)

IF keyword_set(sum) THEN BEGIN
  num_i=TOTAL(i2)
  num_o=SQRT(TOTAL(o2^2))
 ;
  den_i=TOTAL(i1)
  den_o=SQRT(TOTAL(o1^2))
ENDIF ELSE BEGIN
  num_i=i2
  num_o=o2
 ;
  den_i=i1
  den_o=o1
ENDELSE

ratio=num_i/den_i
error=sqrt( (den_i*num_o)^2 + (num_i*den_o)^2 ) / den_i^2

IF NOT keyword_set(silent) AND n_elements(ratio) EQ 1 THEN BEGIN
  IF (num_i/den_i LT 0.1) THEN BEGIN
    print,format='("Ratio: ",e10.2)',num_i/den_i
    print,format='("Sigma: ",e10.2)',sqrt( (den_i*num_o)^2 + $
                                           (num_i*den_o)^2 ) / den_i^2
  ENDIF ELSE BEGIN
    print,format='("Ratio: ",f10.3)',num_i/den_i
    print,format='("Sigma: ",f10.3)',sqrt( (den_i*num_o)^2 + $
                                           (num_i*den_o)^2 ) / den_i^2
  ENDELSE
ENDIF

end
