;+
; Function to return the polynomial using the most recently fitted
; baseline coefficients and !g.nfit or the nfit keyword using the
; data in the primary data container.  This function is used in
; all of the GUIDE baseline related functions but may also be useful
; in other uses.
;
; @keyword nfit {in}{optional}{type=integer} Only use at most nfit
; parameters.  If !g.nfit is < nfit, then only !g.nfit parameters will
; be used and a warning will be issued.
;
; @keyword ok {out}{optional}{type=boolean} 1 on success, 0 on failure.
;
; @returns Array corresponding to the polynomial at !g,polyfit through nfit
; evaluated at all of the channels in the primary data container.  Returns
; -1 on failure (ok will be 0).
;
; @uses ortho_poly
; @uses data_valid
;
; @version $Id: getbasemodel.pro,v 1.1 2005/04/26 19:12:27 bgarwood Exp $
;-
function getbasemodel, nfit=nfit, ok=ok
    compile_opt idl2
    ok = 0
    if !g.nfit < 0 then begin
        message, 'There is no fit to use', /info
	return,-1
    endif
    npts = !g.line ? data_valid(!g.s[0]) : data_valid(!g.c[0])
    if (npts le 0) then begin
        message, 'There is no data in the primary data container',/info
        return,-1
    endif
    allChans = dindgen(npts)
    if keyword_set(nfit) then begin
	nfit_used = nfit < !g.nfit
	if nfit_used ne nfit then message,'Actual nfit used = ' + string(nfit_used), /info
    endif else begin
        nfit_used = !g.nfit
    endelse
    ok = 1
    return, ortho_poly(allChans, !g.polyfit[*,0:nfit_used])
end