pro fit_bsc_plot,Index,chan=chan_sel,page=page,				$
		line=lline,charsize=ccharsize,				$ ; Style controls
		xoff_anno=xoff_anno,font=font,				$ ; Style controls
		color=ccolor,						$ : Style controls
		notitle=notitle,title=ttitle,nodate=nodate,		$ ; Control parameters
		trange=trange,vrange=vrange,				$
		ps=ps,eps=eps,hc=hc,fileout=fileout,			$ ; PostScript specific
		xsize=xsize,ysize=ysize,				$
		xoffs=xoffs,yoffs=yoffs,xlen=xlen,ylen=ylen,qtest=qtest
;+
; NAME:
;   fit_bsc_plot
; PURPOSE:
;   Plot fit_bsc results of fitted synthetic spectra to BCS observed spectra 
; CALLING SEQUENCE:
;   fit_bsc_plot,bsc_index
;   fit_bsc_plot,bsc_index
; INPUTS:
;   INDEX  = BSC Index structure 
; OPTIONAL INPUT KEYWORDS:
;   chan      = Select one channel to display
;   xoff_anno = Number between 0 and 1 which specifies location of the plot annotation 
;		[def=.97; defines right edge]
;   font      = Specifies the font to use (make this a number -- not a string)
;               0=Use hardware fonts.  -1=The current default fonts
;   notitle   = If set, don't do any plot annotation.
;   title     = User specified title (default title will be channel number)
;   nodate    = By default, plot date is put on PS plots.  Use /nodate to suppress
;   trange    = yrange option for Te    plots
;   vrange    = yrange option for Vturb plots
;   color     = color keyword for PLOT
;   line      = Set to connect the points.
;   charsize  = Normal keywords for plot, xyouts
; PostScript Specific:
;    hc	      = If set, make a hardcopy version.
;    PS       = If set, send the output to an idl.ps file 
;		 (but don't close or send to printer)
;    eps      = Same as PostScript, but makes an encapsulated file
;    fileout  = Name of the output PostScript file
;    xlen     = Specifies length of X-axis (inches)
;    ylen     = Specifies length of y-axis (inches)
;    xsize    = Specifies the PostScript Page X-size (inches) [def= 6.5 in]
;    ysize    = Specifies the PostScript Page Y-size (inches) [def= 8.5 in]
;    xoffs    = X-offset [def= 1.0 in]
;    yoffs    = Y offset [def= 1.5 in]
;  
; SIDE EFFECTS:
;  
; MODIFICATION HISTORY:
; 21-jan-94, J. R. Lemen (LPARL), Written
; 14-oct-94, JRL, Changed plot to count rate (s-1); detect and eliminate gaps
;	          in the count rate curve; added error bars to non-thermal vel.;
;		  Set xstyle=2 to extend the x-axis slightly
; 30-may-95, JRL, Fudge calls to utplot to overcome inconsistency when xstyle=2 and
;		  timerange=timerange keywords are specified.
;-
;on_error,2			; Return to caller

if n_params() lt 1 then message,'Calling sequence ==> FIT_BSC_PLOT,BSC_INDEX'
if not bsc_check(Index) then message,'Index not a BSC index structure'


; -------- Does the index contain the .FIT field? --------

Tags = tag_names(Index)
jj = where(tags eq 'FIT',count)
if (count eq 0) then message,'Index does not contain .FIT field'

; -------- Is there more than one channel? -----------------
nindex = index
if n_elements(chan_sel) eq 0 then begin
  chan_uniq = gt_bsc_chan(nindex,/uniq)
  chan = chan_uniq(0)
  if n_elements(chan_uniq) gt 1 then begin
     message,' ***  Warning:  There are more than one channels in index',/cont
     message,'      Will use channel = '+strtrim(chan(0),2),/cont
  endif
endif else chan = chan_sel

if n_elements(chan) ne 0 then nindex = nindex(where(gt_bsc_chan(nindex) eq chan))

xstyle=2 & ystyle=0
if n_elements(ccharsize) eq 0 then charsize = 1. else charsize = ccharsize
nodata = 1-keyword_set(lline)

; -------------  Set up for plotting ---------------------

save_dname = !d.name

; Set up the plot page size for Portrait mode
  if n_elements(xsize) eq 0 then xsize = 6.5	; X-page size in inches
  if n_elements(ysize) eq 0 then ysize = 8.5	; Y-Page size in inches
  if n_elements(xoffs) eq 0 then xoffs = 1.	; Offset in inches
  if n_elements(yoffs) eq 0 then yoffs = 1.5	; Offset in inches

  if n_elements(xoff_anno) eq 0 then xoff_anno = .97	; Defines right edge

if keyword_set(ps) or keyword_set(hc) or keyword_set(eps) then begin
     if !d.name ne 'PS' then set_plot,'ps'
     if n_elements(fileout) ne 0 then device,file=fileout else device,file='idl.ps'
     device,/portrait,/inches,xsize=xsize,ysize=ysize,xoffs=xoffs,yoffs=yoffs
     device,encap = keyword_set(eps)
endif
    
; -------------------------------------------------------------------------------
; These parameters set the position of the plot panels (normalized coordinates

xpos = .16
ypos = reverse(.08 + indgen(4)*.23)

if n_elements(xlen) eq 0 then xlen = xsize  * (1-xpos*1.2)
if n_elements(ylen) eq 0 then ylen = (ysize * .71) / 4.


title  = 'BCS Channel '+strtrim(gt_bsc_chan(nindex(0)),2)+': '+(gt_bsc_chan(nindex(0),/str))(0)
if n_elements(ttitle)  gt 0 then title = ttitle 	; Yes? - Use supplied mtitle
if keyword_set(notitle) then title=' '			; Yes? - No mtitle

; ----------------------------------------------------------------------
;---  Set up the color for the plots ---
if !d.name eq 'PS' then begin
   color = 0 & ocolor = 0 & ffont = 0			; Default to Hardware font for PS
endif else begin
  if n_elements(ccolor) eq 0 then color = !d.n_colors-1 else color = ccolor
  ffont = -1						; Default to vector drawn fonts
endelse

if n_elements(font) ne 0 then 	$
  if font ge 3 then begin
     title = '!'+strtrim(font,2)+title 
     ffont = -1
  endif else if font ne -1 then ffont = 0		; Set the hardware font option

; ----------------------------------------------------------------------
; Create a new plot window if the current one has a y-size of 512 or less

if !d.name eq 'X' then if !d.y_size le 512 or !d.window eq -1 then wdef,xxx,512,700,/lr


; Set up the requested page
Page0 = 0
if max(nindex.fit.ncomp_fit) gt 1 then Page1 = 1 else Page1 = 0
if n_elements(page) gt 0 then begin
   if (page lt 0) or (page gt page1) then begin
	message,'No data for requested Page = '+strtrim(page,2),/cont
 	return
   endif else begin
      Page0 = Page		; Only plot the requested page number
      Page1 = Page
   endelse
endif

timerange=[nindex(0),nindex(n_elements(nindex)-1)]
gap_size = 2.			; Size of gaps in light curve interval


for i=Page0,Page1 do begin
  clear_utplot & clearplot
  for j=0,3 do begin
    !p.position = [xpos(0),ypos(j),xpos(0)+float(xlen)/xsize,ypos(j)+float(ylen)/ysize]
    if keyword_set(qtest) then print,'Plot '+strtrim(j,2)+': ',!p.position

    case j of 
    0: if i eq 0 then begin
	 count_rate = nindex.bsc.total_cnts/(nindex.bsc.actim/1000.)
	 utplot,nindex,count_rate,					$
		ytitle='Count Rate (s!U-1!N)',/nodata,xstyle=xstyle,	$
		charsize=charsize,xtitle='',font=ffont,color=color,title=title
	 t0 = nindex.bsc.actim/1000.			; Interval of coverage in sec
	 t1 = int2secarr(nindex) 
	 t1 = t1(1:*)-t1				; Assumes monotonic times
	 ii = where(t1 ge gap_size*t0,npts)		; Look for gaps
	 if npts eq 0 then ii = [-1,n_elements(nindex)-1] else ii = [-1,ii,n_elements(nindex)-1]
;;	 print,'Number of ii = ',n_elements(ii)-1		;***
;;stop	;***
	 for k=0,n_elements(ii)-2 do begin
	   aa = ii(k)+1 & bb = ii(k+1)
 	   if aa eq bb then ppsym = 5 else ppsym = 0
	   outplot,nindex(aa:bb),count_rate(aa:bb),psym=ppsym
         endfor
       endif
    1: if i eq 0 then begin
	  utplot,nindex,nindex.fit.te6,ytitle='Te6 (MK)',nodata=nodata,		$
		   timerange=timerange,charsize=charsize,xtitle='',		$
		   font=ffont,color=color,/noerase,/ynoz,yr=trange,xstyle=xstyle
	  eutplot,nindex,nindex.fit.te6,nindex.fit.u_te6,/unc,color=color
	  xyouts,/norm,!p.position(2)*xoff_anno,!p.position(3)*.97,'Stationary Comp.',$
		   charsize=charsize,align=1.,color=color
       endif else begin
	  utplot,nindex,nindex.fit.te6_s,ytitle='Te6 (MK)',nodata=nodata,	$
		   timerange=timerange,charsize=charsize,xtitle='',		$
		   font=ffont,color=color,/ynoz,xstyle=xstyle
	  eutplot,nindex,nindex.fit.te6_s,nindex.fit.u_te6_s,/unc,color=color
	  xyouts,/norm,!p.position(2)*xoff_anno,!p.position(3)*.97,'Secondary Comp.',$
		   charsize=charsize,align=1.,color=color
       endelse
    2: if i eq 0 then begin
	  utplot,nindex,nindex.fit.em50,ytitle='EM/10!U50!N (cm-3)',nodata=nodata,/noerase,	$
		   timerange=timerange,charsize=charsize,xtitle='',		$
	           font=ffont,color=color,xstyle=xstyle
	  eutplot,nindex,nindex.fit.em50,nindex.fit.u_em50,/unc,color=color
       endif else begin
	  utplot,nindex,nindex.fit.em50_s,ytitle='EM/10!U50!N (cm-3)',nodata=nodata,/noerase,	$
		   timerange=timerange,charsize=charsize,xtitle='',		$
		   font=ffont,color=color,xstyle=xstyle
	  eutplot,nindex,nindex.fit.em50_s,nindex.fit.u_em50_s,/unc,color=color
       endelse

    3: if i eq 0 then begin
	  vturb = bcs_broad(chan,Td6=(nindex.fit.Td6-nindex.fit.te6)>0,/vel)
	  npts = n_elements(nindex)
;          vturb = fltarr(npts) 
	  u_vturb = vturb
	  for k=0,n_elements(nindex)-1 do begin
;	    vturb(k)   = bsc_vturb(nindex(k))
	    u_vturb(k) = bsc_vturb(nindex(k),/uncer)
	  endfor
	  ss = where(vturb gt 0, npts)
	  if npts eq 0 then begin
	    ss = [0,n_elements(nindex)-1]
	    vturb(ss)=[1,1]
          endif
; The timerange=timerange does not work consistently with xstyle=2
          if n_elements(vrange) eq 0 then vvrange=[min(vturb(ss)),max(vturb(ss))] else vvrange=vrange
	  utplot,nindex,vturb,nodata=nodata,/noerase,				$
		   timerange=timerange,charsize=charsize,			$
		   font=ffont,color=color,ytitle='Turb Vel (km/s)',yr=vvrange,xstyle=xstyle
	  if not keyword_set(nodata) then outplot,nindex(ss),vturb(ss)
	  eutplot,nindex(ss),vturb(ss),u_vturb(ss),/unc,color=color
;	  outplot,nindex(ss),vturb(ss),psym=2,color=color
       endif else begin
	  utplot,nindex(ss),nindex(ss).fit.vel,nodata=nodata,/noerase,		$
		   timerange=timerange,charsize=charsize,			$
		   font=ffont,color=color,ytitle='Upflow Vel (km/s)',xstyle=xstyle
	  outplot,nindex(ss),nindex(ss).fit.vel,psym=2,color=color
       endelse
    endcase
    endfor				; j=0,3

; ----------------------------------------------------------------------
; Add the Time/date on PS Plot

      if (!d.name eq 'PS') and not keyword_set(nodate) then	$
	xyouts,/norm,xpos(0),.02,'Plot done: '+fmt_tim(!stime),charsize=.7*charsize
   if (!d.name ne 'PS') and (Page0 ne Page1) and (i eq Page0) then begin
	 ans = '' & read,'Pause: ',ans
   endif
endfor					; i=Page0,Page1

empty
if !d.name eq 'PS' then device,/close
if keyword_set(hc) then pprint,fileout	; Try to send to printer
clearplot				; Reset the defaults
clear_utplot				; Reset the defaults
set_plot,save_dname

if keyword_set(qtest) then stop
end
