;+
; NAME:
;       PLOTFIT
; PURPOSE:
;      Plot FIT files from SPCFIT 
; CATEGORY:
;      BCS
; CALLING SEQUENCE:
;      plotfit,x,y,e,w,z,yf,/log
; INPUTS:
;      prompts for fit filename
; OUTPUTS:            
;      t = time array (secs since UTBASE)
;      y = fitted parameter array 
;      e = error in y
;      w = wavelength array
;      z = observed spectrum array
;      yf= fitted spectrum
; KEYWORDS:
;      /log  = plot y axis on Log10 scale
; MODIFICATION HISTORY:     
;      DMZ (ARC) June'91
;-

pro plotfit,x,y,e,w,z,yf,log=log

common fit_stuff,fit_file,fit_data,length

params=['temp','emiss','vturb','flux','blue','ratio']
forever=0 & new=1 & wave=1

;-- parameter options

repeat begin
 theory=0 & hard=0
 repeat begin     
  print,'* enter:'
  print,'     1 : for electron temperature '
  print,'     2 : for primary component emission measure '
  print,'     3 : for primary component nonthermal broadening' 
  print,'     4 : for integrated line flux of primary component' 
  print,'     5 : for secondary component blueshift velocity'
  print,'     6 : for secondary/primary component emission measure ratio'
  opt='' & read,'----> ',opt & opt=fix(opt)
 endrep until ((opt gt 0) and (opt lt 7))
 
 chan='' &  read,'* enter channel number [def=1]: ',chan 
 if chan eq '' then  chan=1 else chan=fix(chan)

;-- read fit parameters

 rdfit,file,chan=chan,x,y,e,utbase,param=params(opt-1), $
       xtitle=xtitle,ytitle=ytitle,plabels=plabels
 if new then xrange=[min(x),max(x)] & setutbase,utbase

hardc:

;-- plot parameters

 if not hard then begin

  tstart=atime(utime(utbase)+xrange(0))
  comma=strpos(tstart,',')
  if comma ge 0 then ts=strmid(tstart,comma+1,80) else ts=tstart
  print,'* current plot start time = ',ts
  ts=''
  read,'* enter new value to change [hit return to keep]: ',ts
  if ts ne '' then xrange(0)=utime(ts)

  tend=atime(utime(utbase)+xrange(1))
  comma=strpos(tend,',')
  if comma ge 0 then te=strmid(tend,comma+1,80) else te=tend
  print,'* current plot end time = ',te
  te=''
  read,'* enter new value to change [hit return to keep]: ',te
  if te ne '' then xrange(1)=utime(te)

 endif

;-- plot light curve

 if hard then set_hard
 plotltc,x,y,utbase,ebar=e,xtitle=xtitle,ytitle=ytitle,log=log,$
          title=plabels(0),xrange=xrange
 if hard then set_hard,plabels,/send

 if not hard then begin
  theory='' 
  read,'* compare observed and fitted spectra [def=N]? ',theory
  theory=strupcase(strmid(theory,0,1))
  if theory eq 'Y' then theory=1 else theory=0
  if theory then begin
   mess='USE X-CURSOR TO SELECT TIME OF INTEREST'
   print,'* '+mess
   xyouts,.04,.98,mess,/normal
   cursor,p,q,/data & stime=p
   if keyword_set(log) then oplot,[p,p],10.^!y.crange,linestyle=2 else $
     oplot,[p,p],!y.crange,linestyle=2

;-- read the observed and fitted theory spectra

   sfile=strep(strupcase(fit_file),'FIT','SPC')
   find=findfile(sfile,count=sf)
   bfile=strep(strupcase(fit_file),'FIT','BRR')
   find=findfile(bfile,count=bf)
   if sf eq 0 then begin message,/contin,sfile+ 'NOT FOUND ' & goto,quit & endif
   if bf eq 0 then begin message,/contin,bfile+ 'NOT FOUND ' & goto,quit & endif
 
   getbrr,bfile
   getbrr_spec,chan,stime,w,z,ez,utbase,accum,wave=wave,$
         ytitle=ytitle,xtitle=xtitle,plabels=slabels
   rdspc,sfile,chan,stime,yf,ftime,pk
   diff=abs(stime(0)-ftime(0))
   if diff gt 2.*accum then begin
    message,'no fitted spectrum near requested time',/contin
    goto,quit
   endif
  endif
 endif

;-- plot spectra

 if theory then begin
  if hard then set_hard
  plotspec,w,z,xtitle=xtitle,ytitle=ytitle,title=slabels(0),psym=10
  oplot,w,yf
  if hard then set_hard,/send,slabels
 endif

 quit:

;-- hard copy?
 
 if not hard then begin
  hard='' & read,'* hard copy [def=N]? ',hard
  hard=strupcase(strmid(hard,0,1))
  if hard eq 'Y' then hard=1 else hard=0 
  if hard then goto,hardc
 endif else hard=0

 if not hard then begin
  again='' & read,'* another plot [def=N]? ',again
  again=strupcase(strmid(again,0,1))
  if again eq 'Y' then begin
   new='' & read,'* read new FIT file [def=N]? ',new
   new=strupcase(strmid(new,0,1))
   if new eq 'Y' then new=1 else new=0
   if new then fetch,file,'FIT'
   forever=0
  endif else forever=1
 endif

endrep until forever

return & end