; $Id: fitov.pro,v 1.3 2002/09/16 20:43:56 jmspark Exp $
pro fitov,d,xax,yax,xmin=xmin,xmax=xmax,ymin=ymin,ymax=ymax,hardcopy=hardcopy,$
font=fontnum, lasty=lasty, minuit=minuit, fit=fit
;+
; NAME:         FITOV
; PURPOSE:
;               Overlay a fit on  a TASCOM file plotted with TASPL
; CATEGORY:
; CALLING SEQUENCE:
;               FITOV,tasdata,'x-axis index','y-axis index'
; INPUTS:
;               tasdata = a tascom data structure read in with TASRD
;               x-axis index= string defining parameter field for x-axis
;               y-axis index= string defining parameter field for y-axis
;                      fitov,d,'h','i'
;                   (default y-axis is I) so taspl,d,'h' will also work
;                       or fitov,tasrd('filename'),'em'
; OPTIONAL INPUT PARAMTERS:
; KEYWORD PARAMETERS:
; OUTPUTS:
; OPTIONAL OUTPUT PARAMETERS:
; COMMON BLOCKS:
;       fitp            the fit parameters and stuff
;       uncertainty     the uncertainty in the fit parameters
;       plot            the plotting range
;
; SIDE EFFECTS:
; RESTRICTIONS:
; PROCEDURE:
; MODIFICATION HISTORY:
;               Thom Mason, Riso National Lab, 1993
;               Oct. 1993: Lots of modifications....
;                       Paul Schleger, Riso
;-
common fitp,ngau,nlor,a,vary,pnames,gw,ufunct,nufun
COMMON com_minchoice, minchoice, minpath
common plot, xrs, yrs           ;Plotting range
COMMON preferences, pref

IF NOT keyword_set(minuit) THEN minuit=0 ELSE minuit=minuit
IF keyword_set(fontnum) THEN fontnum=fontnum ELSE fontnum=5
IF !P.FONT EQ 0 THEN fontnum=11;Use courier font, if we are using hardware fonts. avoids prop. spac.
fntstr = STRCOMPRESS('!'+STRING(fontnum,format='(I)'),/REMOVE_ALL)
;There are two special symbols, which are used, but are different, depending
;on whether you are using the Vector drawn fonts or the Postscript hardware fonts
;Currently, this routine used the greek chi and the math plus-minus symbol
IF !P.FONT EQ 0 THEN BEGIN
        chisym = '!9c'
        pmsym = STRCOMPRESS(fntstr+'!9!S!B!E-!R!A!I+!N',/REMOVE_ALL)
END ELSE BEGIN
        chisym = '!7v'
        pmsym = '!M+'
END

xind=tasid(xax,d)
sind=-1
if (n_params() eq 2) then begin
  sind=tasid('sigi',d)
  yind=tasid('I',d)
  endif else begin
  if (strupcase(yax) eq 'I') then sind=tasid('sigi',d) else $
   if (strupcase(yax) eq 'MON') then sind=tasid('sigm',d)
  yind=tasid(yax,d)
  endelse
if (sind ge 0) then err=d.data(sind,*) else err=d.data(yind,*)/d.data(yind,*)
;Check where the error might be zero
index=WHERE(err EQ 0,count)
IF (count NE 0) THEN err(index)=1.0
sigma=fltarr(n_elements(a))
chi=gen_fit(d.data(xind,*),d.data(yind,*),1./err^2.,sigma=sigma)

dx=xrs(1)-xrs(0)
dy=yrs(1)-yrs(0)
mnx=xrs(0) & mxx=xrs(1)
mny=yrs(0) & mxy=yrs(1)

x=mnx+findgen(200)*1.2*dx/199.
f=fltarr(n_elements(x))
index=where(vary,count)
IF (minchoice EQ 'SFit') THEN BEGIN	;We did a simple IDL gcurfit
	IF count NE 0 THEN general, x,a(index),f ELSE general, x,a,f
END ELSE $
  junk=CALL_EXTERNAL(minpath,'eval_func',x,f,FIX(n_elements(x)))
;changed by Guangyong Xu



oplot,x,f,psym=0,linestyle=0
oplot,x,a(0)+a(1)*x+a(2)*x^2,psym=0,linestyle=1
noprob = WHERE(a NE 0,okcount)
nsigma=fltarr(n_elements(sigma))
IF (okcount NE 0) THEN nsigma(noprob) = 100.*ABS(sigma(noprob)/a(noprob))
text=strarr(3)
text(2)=fntstr
text(1)=fntstr
text(0)=fntstr+$
 'B0='+string(a(0),format='(g10.4)')+pmsym+fntstr+string(nsigma(0),format='(g6.2)')+'%'+$
 '!c'+'B1='+string(a(1),format='(g10.4)')+pmsym+fntstr+string(nsigma(1),format='(g6.2)')+'%'+$
 '!c'+'B2='+string(a(2),format='(g10.4)')+pmsym+fntstr+string(nsigma(2),format='(g6.2)')+'%'
for i=0,ngau-1 do for j=3,5 do $
  text(1)=text(1)+pnames(j)+string(i,format='(i1)')+'='+$
  string(a(j+3*i),format='(g10.4)')+pmsym+fntstr+string(nsigma(j+3*i),format='(g6.2)')+'%!c'
for i=0,nlor-1 do for j=6,8 do $
  text(2)=text(2)+pnames(j)+string(i,format='(i1)')+'='+$
  string(a(j+3*i+3*(ngau-1)),format='(g10.4)')+pmsym+fntstr+$
  string(nsigma(j+3*i+3*(ngau-1)),format='(g6.2)')+'%!c'
for i=9,9+nufun-1 do begin
  text(2)=text(2)+pnames(i)+'='+string(a(i+3*(ngau+nlor-2)),format='(g10.4)')+$
  pmsym+fntstr+string(nsigma(i+3*(ngau+nlor-2)),format='(g6.2)')+'%!c'
  endfor
chitx=fntstr+'!c'+chisym+fntstr+'!e2!N='+string(chi,format='(f7.3)')
if (gw ne 0.0) then chitx=chitx+'!c!cRW='+string(gw,format='(f8.4)')
IF keyword_set(hardcopy) THEN BEGIN
  IF (gw NE 0.) THEN BEGIN              ;gw is the resolution width (FWHM)
        jim=x(WHERE(f EQ max(f)))       ;x position centered over max peak
        jimmy= 0.5*(a(0)+a(1)*jim+a(2)*jim^2+max(f)); y position at half maximum
        oplot,[jim-0.5*gw,jim+0.5*gw],[jimmy,jimmy]
        xyouts,jim,jimmy+0.02*dy,'RW',charsize=1.0,alignment=0.5
  END
  xyouts,0.1,lasty-0.04,'Fit Parameters',/NORMAL, alignment=0,charsize=0.7
  xyouts,0.1,lasty-0.07,text(0)+chitx,charsize=0.7,/NORMAL, alignment=0
  xyouts,0.5,lasty-0.07,text(1),charsize=0.7,/NORMAL, alignment=0.5
  xyouts,0.9,lasty-0.07,text(2),charsize=0.7,/NORMAL, alignment=1

  IF (pref.multi_gauss NE 0) THEN BEGIN
        ind1 = indgen(ngau)*3+3 & ind2 = indgen(ngau)*3+5
        area = 1.06447*TOTAL(a(ind1)*a(ind2))
        print, 'Area:',area
        xyouts,0.1,lasty-0.20,'Area:'+STRING(area,format='(g10.4)'),charsize=0.8,/NORMAL, alignment=0
        lasty = lasty-0.03
  END

  lasty = lasty-0.1-0.06*max([ngau,nlor])
  IF gw NE 0 THEN lasty=lasty-0.05
END ELSE BEGIN
  IF (gw NE 0.) THEN oplot,[mxx-0.95*dx,mxx-0.95*dx+gw],[ymax-0.23*dy,ymax-0.23*dy]
  xyouts,mxx-0.95*dx,ymax-0.05*dy,text(0)+chitx,charsize=0.9
  xyouts,mxx-0.6*dx,ymax-0.05*dy,text(1),charsize=0.9
  xyouts,mxx-0.25*dx,ymax-0.05*dy,text(2),charsize=0.9
  lasty = -0.1  ;Let's assume that the available space is this value in /NORMAL coords
END

return
end
