;
on_error,2                      ;Return to caller if an error occurs
n = n_elements(y)       ;# of points.
;
qinit = 0
if (n_elements(a) ne 0) then if (total(a) ne 0) and (n_elements(a) eq 3) then qinit=1   ;initial value was passed in
;
if (not qinit) then begin
    ;;print, 'Calculating initial value'
    c = poly_fit(x,y,1,yf)      ;fit a straight line.
    yd = y-yf           ;difference.
    
    ymax=max(yd) & xmax=x(!c) & imax=!c ;x,y and subscript of extrema
    ymin=min(yd) & xmin=x(!c) & imin=!c
    a=fltarr(3)         ;coefficient vector
    if abs(ymax) gt abs(ymin) then i0=imax else i0=imin ;emiss or absorp?
    i0 = i0 > 1 < (n-2)     ;never take edges
    dy=yd(i0)           ;diff between extreme and mean
    del = dy/exp(1.)        ;1/e value
    i=0
    while ((i0+i+1) lt n) and $ ;guess at 1/2 width.
        ((i0-i) gt 0) and $
        (abs(yd(i0+i)) gt abs(del)) and $
        (abs(yd(i0-i)) gt abs(del)) do i=i+1
    a = [max(y), x(i0), abs(x(i0)-x(i0+i))] ;estimates
end

;;print,'Initial Guess: ', a
!c=0                ;reset cursor for plotting
return,curvefit(x,y,replicate(1.,n),a,sigmaa, $
        function_name = "GAUSS_FUNCT3") ;call curvefit
end