;----------------------------------------------------------------------------
;+
; NAME:
;        SXT_OBSRPT
; PURPOSE:
;        Provides a summary listing of the #s of FFIs and PFIs per
;	 Yohkoh orbit, between input times. Further divides PFIs
;	 by unique FOVs.
; CALLING SEQUENCE:
;	 sxt_obsrpt, sttime	(Default: entime = sttime + 24 hrs)
;	 sxt_obsrpt, sttime, entime
;	 sxt_obsrpt, /hc	(DPRINTs output to printer AOT screen)
;	 sxt_obsrpt, outfile=outfile    (Output to outfile AOT screen)
;	 sxt_obsrpt, /outfile	(Default: outfile = 'obs_rpt.out')
;	 sxt_obsrpt, /hc, /outfile
;        sxt_obsrpt, ... , /html
; INPUTS:
;	 sttime = Begin time for selected observational period.
;	 entime = End time for selected observational period. Default
;		  is sttime+24hrs.  May crash if interval is less than 2hrs.
; KEYWORD PARAMETERS:
;        Keywords:
;	 /HC -> Output goes to printer instead of screen. A temporary
;		file 'obs_rpt.tmp' is created, printed with DPRINT,
;		and then deleted.
;	 OUTFILE=OUTFILE -> Output goes to OUTFILE instead of screen.
;		Default value is 'obs_rpt.out'. If this keyword is
;		enabled WITH /HC, then OUTFILE is DPRINTed, but NOT
;		deleted.
;        /html -> Generates html output.
;	 /ffi_report -> Includes pointing and resolution of FFIs
;        /noaa_no -> Includes listing of NOAA active region nearest PFI
;
; OUTPUTS:
;	 Listing of Yohkoh orbits spanning timerange, from FEM files.
;	 Per orbit, the following: total FFIs and PFIs, unique FOVs
;	 with # PFIs for each (from RD_OBS and SXT_UNIQFOV).
; COMMON BLOCKS:
;	 Typically avoided.
; NOTES:
;	 Originally written for Schmieder-June96 campaign.
;	
; MODIFICATION HISTORY:
;	 M. A. Weber, 19-Jun-96 --- Written. V1.00 (alpha!)
;	 25/7/96  - (MAW) - Adjusted "feature" so now code can handle 
;			    time spans greater than 24 hours. (V1.01)
;	 97.6.10  - (MAW) - Fixed major bug with SXTP seconds array.
;			    (V1.10)
;        06/30/99 - (ARD) - Added option to generate html output.
;	 07/02/99 - (DMcK)- Patched PFI counting problems.
;	 07/16/99 - (DMcK)- Added FFI_REPORT option.
;        09/14/99 - (DMcK)- Added NOAA_NO option, and trapped out
;			    calibration FFIs. (V2.00)
;	 09/07/00 - (DMcK)- Improved trapping of calibration FFIs,
;			    and fixed bug whereby program crashed when
;			    PFIs exist but all FFIs are calibrations.
;			    Still crashes if interval is less than 2hrs.
;        09/22/00 - (DMcK)- Adjusted patch of 7-sep-00 to fix case where
;			    zero FFIs and zero PFIs were made during an
;			    orbit.
;-
;----------------------------------------------------------------------------

pro sxt_obsrpt, sttime, entime, hc=hc, outfile=outfile, qstop=qstop, $
	html=html, ffi_report=ffi_report, noaa_no=noaa_no

; ---------------------------
; Initial business.
; ---------------------------
	sttime_orig = anytim2ints(sttime)
	sttime_early = anytim2ints(sttime, off=-6000.)
	if n_params() eq 2 then entime = anytim2ints(entime) $
	   else begin
		entime = sttime_orig
		entime.day = sttime_orig.day+1
	   endelse

	qout = 0
	if keyword_set(outfile) then begin
	   if string(outfile) eq string(1) then $
		outfile = 'obs_rpt.out'
	   qout = 1
	endif

	qhc = 0
	if keyword_set(hc) then begin
	   qhc = 1
	   if not qout then outfile = 'obs_rpt.tmp'
	endif

	qhtml = 0
	if keyword_set(html) then begin
	   qhtml = 1
	endif

	qffi = 0
	if keyword_set(ffi_report) then begin
	   qffi = 1
	endif

	qnar = 0
	if keyword_set(noaa_no) then begin
	   qnar = 1
	endif

	if qout or qhc then openw, lunhc, outfile, /get_lun

        counts_buffer = ['  ', ' ', '']

; ---------------------------
; Retrieve FEM files.
; ---------------------------

	rd_fem, sttime_early, entime, fem

	rd_obs, sttime_early, entime, bcs, sxtf, sxtp, /nobcs, /quiet

; ---------------------------
; Get S/C orbit times.
; ---------------------------

	dawn_int = anytim2ints(fem)
	dusk_int = anytim2ints(fem, off=fem.night)
	ss1 = sel_timrange(dawn_int, sttime_orig, entime, /between)
	ss2 = sel_timrange(dusk_int, sttime_orig, entime, /between)
	if ss1(0) eq ss2(0) then ss = ss1 else ss = [ss2(0),ss1]
	dawn_int = dawn_int(ss)
	dusk_int = dusk_int(ss)
	norbits = n_elements(ss)

	day_str  = gt_day(dawn_int, /string)
	dawn_str = gt_time(dawn_int, /string)
	dusk_str = gt_time(dusk_int, /string)

	dawn_secarr = int2secarr(dawn_int, sttime_orig)
	dusk_secarr = int2secarr(dusk_int, sttime_orig)

; ---------------------------
; Retrieve OBS files.
; ---------------------------

	rd_obs,sttime_early,dusk_int(norbits-1),bcs,sxtf,sxtp,/nobcs,/quiet

; ----------------------------
; Prepare SecArray times for
; SXTFs and SXTPs.
; -----------------------------

	sxtf_secarr = int2secarr(sxtf, sttime_orig)
	sxtp_secarr = int2secarr(sxtp, sttime_orig)

; -----------------------------
; Print column titles. (This part's ugly)
; -----------------------------

	if qhtml then begin

	 if qnar then begin  ;(for versions WITH the NOAA no. included)
	 if qffi then begin  ;(the version with FFI details)

	  header1 = '<html><body><table align=center border=1 cellspacing=4>'
	  header2 = '<tr><th colspan=11><font size=+2>Yohkoh orbits spanning the timerange<br>'
	  header3 = '('+gt_day(sttime,/str)+' '+gt_time(sttime,/str) + $
	            ') to ('+gt_day(entime,/str)+' '+gt_time(entime,/str)+')</th></tr>'
	  header4 = ''

	  ctit1 = '<tr><th rowspan=2><font size=+1>Yohkoh<br>orbit<th><font size=+1>Dawn' + $
	          '<th><font size=+1>Dusk<th colspan=3><font size=+1>Summary of FFIs' + $
		  '<th colspan=5><font size=+1>      Summary of PFIs         </tr>'
 	  ctit2 = '<tr><th colspan=2><font size=+1>(UT)' + $
	          '<th><font size=+1>#FFIs<th><font size=+1>Center<th><font size=+1>Resolution<th><font size=+1>#PFIs<th><font size=+1>Center' + $
		  '<th><font size=+1>FOV<th><font size=+1>Resolution<th><font size=+1>NOAA no.</tr>'
	  ctit3 = ''
	 endif else begin   ;(no FFI details, yes NOAA no.)
	  header1 = '<html><body><table align=center border=1 cellspacing=4>'
	  header2 = '<tr><th colspan=9><font size=+2>Yohkoh orbits spanning the timerange<br>'
	  header3 = '('+gt_day(sttime,/str)+' '+gt_time(sttime,/str) + $
	            ') to ('+gt_day(entime,/str)+' '+gt_time(entime,/str)+')</th></tr>'
	  header4 = ''

	  ctit1 = '<tr><th rowspan=2><font size=+1>Yohkoh<br>orbit<th><font size=+1>Dawn' + $
	          '<th><font size=+1>Dusk<th><font size=+1>#FFIs' + $
		  '<th colspan=5><font size=+1>      Unique FOVs for PFIs         </tr>'
 	  ctit2 = '<tr><th colspan=2><font size=+1>(UT)' + $
	          '<th><th><font size=+1>#PFIs<th><font size=+1>Location' + $
		  '<th><font size=+1>FOV<th><font size=+1>Resolution<th><font size=+1>NOAA no.</tr>'
	  ctit3 = ''
	 endelse

	 endif else begin   ;(now the versions WITHOUT the NOAA no.)
	 if qffi then begin
	  header1 = '<html><body><table align=center border=1 cellspacing=4>'
	  header2 = '<tr><th colspan=10><font size=+2>Yohkoh orbits spanning the timerange<br>'
	  header3 = '('+gt_day(sttime,/str)+' '+gt_time(sttime,/str) + $
	            ') to ('+gt_day(entime,/str)+' '+gt_time(entime,/str)+')</th></tr>'
	  header4 = ''

	  ctit1 = '<tr><th rowspan=2><font size=+1>Yohkoh<br>orbit<th><font size=+1>Dawn' + $
	          '<th><font size=+1>Dusk<th colspan=3><font size=+1>Summary of FFIs' + $
		  '<th colspan=4><font size=+1>      Summary of PFIs         </tr>'
 	  ctit2 = '<tr><th colspan=2><font size=+1>(UT)' + $
	          '<th><font size=+1>#FFIs<th><font size=+1>Center<th><font size=+1>Resolution<th><font size=+1>#PFIs<th><font size=+1>Center' + $
		  '<th><font size=+1>FOV<th><font size=+1>Resolution</tr>'
	  ctit3 = ''
	 endif else begin
	  header1 = '<html><body><table align=center border=1 cellspacing=4>'
	  header2 = '<tr><th colspan=8><font size=+2>Yohkoh orbits spanning the timerange<br>'
	  header3 = '('+gt_day(sttime,/str)+' '+gt_time(sttime,/str) + $
	            ') to ('+gt_day(entime,/str)+' '+gt_time(entime,/str)+')</th></tr>'
	  header4 = ''

	  ctit1 = '<tr><th rowspan=2><font size=+1>Yohkoh<br>orbit<th><font size=+1>Dawn' + $
	          '<th><font size=+1>Dusk<th><font size=+1>#FFIs' + $
		  '<th colspan=4><font size=+1>      Unique FOVs for PFIs         </tr>'
 	  ctit2 = '<tr><th colspan=2><font size=+1>(UT)' + $
	          '<th><th><font size=+1>#PFIs<th><font size=+1>Location' + $
		  '<th><font size=+1>FOV<th><font size=+1>Resolution</tr>'
	  ctit3 = ''
	 endelse
	endelse

	endif else begin    ;(Now do the non-HTML outputs)

	  header1 = '============================================'
	  header2 = 'Yohkoh orbits spanning the timerange'
	  header3 = '('+gt_day(sttime,/str)+' '+gt_time(sttime,/str) + $
	            ') to ('+gt_day(entime,/str)+' '+gt_time(entime,/str)+')'
	  header4 = '============================================'

	if qnar then begin    ;(versions with NOAA no.)
	if qffi then begin    ;(version with FFI details)
	  ctit1 = 'Yohkoh      Dawn     Dusk   |     FFIs  ' + $
	          '      |     Unique FOVs for PFIs                 |'
 	  ctit2 = ' orbit          (UT)        | #  Center ' + $
	          ' Res. |  #  Center       FOV       Res.     NOAA |'

	endif else begin      ;(version without FFI details)
	  ctit1 = 'Yohkoh      Dawn     Dusk   |  Number of FFIs '+$
		        '|     Unique FOVs for PFIs                 |'
	  ctit2 = ' orbit          (UT)        |           ' + $
		  '      |  #  Center       FOV       Res.     NOAA |'
	endelse
	  ctit3 = '____________________________|___________' + $
	          '______|__________________________________________|'

	
	endif else begin      ;(now versions without NOAA no.)
	if qffi then begin    ;(version with FFI details)
	  ctit1 = 'Yohkoh      Dawn     Dusk   |     FFIs  ' + $
	          '      |     Unique FOVs for PFIs        |'
 	  ctit2 = ' orbit          (UT)        | #  Center ' + $
	          ' Res. |  #  Center       FOV       Res. |'

	endif else begin      ;(version without FFI details)
	  ctit1 = 'Yohkoh      Dawn     Dusk   |  Number of FFIs '+$
		        '|     Unique FOVs for PFIs        |'
	  ctit2 = ' orbit          (UT)        |           ' + $
		  '      |  #  Center       FOV       Res. |'
	endelse
	  ctit3 = '____________________________|___________' + $
	          '______|_________________________________|'
	endelse
	endelse

	if qhc or qout then begin
	   printf, lunhc, header1
	   printf, lunhc, header2
           printf, lunhc, header3
           printf, lunhc, header4
	   printf, lunhc, ''
	   printf, lunhc, ctit1
           printf, lunhc, ctit2
           printf, lunhc, ctit3
	endif else begin
	   print, header1
	   print, header2
           print, header3
           print, header4
	   print, ''
	   print, ctit1
           print, ctit2
           print, ctit3
	endelse

; -----------------------------
; Main loop to extract relevant
; info from the OBS data, orbit by orbit.
; -----------------------------

	for i = 0, (norbits-1) do begin
	   ssf = where((sxtf_secarr ge dawn_secarr(i)) and $
		       (sxtf_secarr le dusk_secarr(i)), countf)
	   ssp = where((sxtp_secarr ge dawn_secarr(i)) and $
		       (sxtp_secarr le dusk_secarr(i)), countp)

spaces='        '

; -----------------------------
; Get details for the FFIs
; -----------------------------

; Don't report the calibration FFIs (dark, diffusers, straylight monitors)
;   (Patched 22-sep-00)
  gooduns=ssf
if (gooduns(0) ne -1) then begin
  baduns=where(gt_expmode(sxtf(ssf)) eq 1 or gt_filta(sxtf(ssf)) eq 4 or gt_filtb(sxtf(ssf)) eq 1)
  if (baduns(0) gt -1) then begin
   gooduns=kill_index(ssf,ssf(baduns))
   ssf=gooduns
   if (gooduns(0) eq -1) then countf=long(0) else countf=n_elements(gooduns)
  endif
endif

;
; End of "calibration" trap


if qffi and (countf gt 1) then begin

  ufov=sxt_uniqfov(sxtf(ssf),fov=nfov)

  for fj=0,nfov-1 do begin
   dummy_ufov=sxt_uniqfov(sxtf(ssf(ufov.(fj))),uniqss=uniqss)
 if (uniqss(0) ge 0) then begin
   n_uniqss=n_elements(uniqss)
   counts_str=strarr(n_uniqss)     
   fii=0
   for fii=0,n_uniqss-1 do begin
        cen=gt_center(sxtf(ssf(ufov.(fj)(uniqss(fii)))),/helio,/string)
        rez=gt_res(sxtf(ssf(ufov.(fj)(uniqss(fii)))))
        dummy_ss=where(gt_res(sxtf(ssf(ufov.(fj)))) eq rez,n_dummy)
        rez_str=strmid(strcompress(2.455*(2^rez),/rem),0,4)+'"'
        counts_str(fii)=strcompress(n_dummy,/rem)

           if qhtml then begin
            if (fii gt 0) then begin
              line1 = '<tr><td align=center>'+'"'+'</td>' + $       
                      '<td align=center>'+'"'+'</td>' + $        
                      '<td align=center>'+'"'+'</td>' + $        
                      '<td align=center>'+counts_str(fii)+'</td>' + $
                      '<td align=center>'+cen+'</td>' + $
                      '<td align=center>'+rez_str+'</td></tr>'
            endif else begin
	      if qnar then begin
              line1 = '<tr><td colspan=11><center>' + $
		      '&nbsp;</center>' + $
		      '</td></tr><tr><td align=center>'+day_str(i)+'</td>' + $
                      '<td align=center>'+dawn_str(i)+'</td>' + $
                      '<td align=center>'+dusk_str(i)+'</td>' + $
                      '<td align=center>'+counts_str(fii)+'</td>' + $
                      '<td align=center>'+cen+'</td>' + $
                      '<td align=center>'+rez_str+'</td></tr>'
	      endif else begin
              line1 = '<tr><td colspan=10><center>' + $
		      '&nbsp;</center>' + $
		      '</td></tr><tr><td align=center>'+day_str(i)+'</td>' + $
                      '<td align=center>'+dawn_str(i)+'</td>' + $
                      '<td align=center>'+dusk_str(i)+'</td>' + $
                      '<td align=center>'+counts_str(fii)+'</td>' + $
                      '<td align=center>'+cen+'</td>' + $
                      '<td align=center>'+rez_str+'</td></tr>'
	      endelse
            endelse
           endif else begin
            if fii eq 0 then begin
             line1 = day_str(i)+' '+dawn_str(i)+'-'+dusk_str(i)
     	     line1=line1+strmid(spaces,0,3-strlen(counts_str(fii)))
            endif else begin
             line1='    "         "        "     '
            endelse
            line1=line1+' '+counts_str(fii)+'  '+cen+'  '+rez_str
	   endelse
       if (qhc or qout) and (rez gt 0) then printf, lunhc, line1 else print, line1
   endfor  ; fii-loop
  endif
  endfor   ; fj-loop 
endif      ; qffi loop for more than one FFI per orbit

if qffi and (countf eq 1) then begin
	cen=gt_center(sxtf(ssf),/helio,/string,/cmd)
        rez=gt_res(sxtf(ssf))
        n_dummy=1
        rez_str=strmid(strcompress(2.455*(2^rez),/rem),0,4)+'"'
        counts_str=strcompress(n_dummy,/rem)

           if qhtml then begin
              line1 = '<tr><td align=center>'+day_str(i)+'</td>' + $
                      '<td align=center>'+dawn_str(i)+'</td>' + $   
                      '<td align=center>'+dusk_str(i)+'</td>' + $   
                      '<td align=center>'+counts_str+'</td>' + $     
                      '<td align=center>'+cen+'</td>' + $
                      '<td align=center>'+rez_str+'</td></tr>'
           endif else begin
              line1 = day_str(i)+' '+dawn_str(i)+'-'+dusk_str(i)
              line1=line1+strmid(spaces,0,3-strlen(counts_str))
              line1=line1+' '+counts_str+'  '+cen+'  '+rez_str
           endelse

        if (qhc or qout) and (rez gt 0) then printf, lunhc, line1 else print, line1
endif      ; qffi loop for exactly one FFI per orbit

if qffi and (countf eq 0) then begin
	cen =' N/A '
	rez_str=' N/A'
	counts_str='0'
	if qhtml then begin
              line1 = '<tr><td align=center>'+day_str(i)+'</td>' + $
                      '<td align=center>'+dawn_str(i)+'</td>' + $
                      '<td align=center>'+dusk_str(i)+'</td>' + $
                      '<td align=center>'+counts_str+'</td>' + $
                      '<td align=center>'+cen+'</td>' + $
                      '<td align=center>'+rez_str+'</td></tr>'   
           endif else begin
              line1 = day_str(i)+' '+dawn_str(i)+'-'+dusk_str(i)
              line1=line1+strmid(spaces,0,3-strlen(counts_str))
              line1=line1+' '+counts_str+'  '+cen+'  '+rez_str
           endelse
           
        if qhc or qout then printf, lunhc, line1 else print, line1
endif      ; qffi loop for zero FFIs this orbit (e.g. Flare mode)

if not qffi then begin
           if qhtml then begin
	      line1 = '<tr><td align=center>'+day_str(i)+'</td>' + $
		      '<td align=center>'+dawn_str(i)+'</td>' + $
		      '<td align=center>'+dusk_str(i)+'</td>' + $
		      '<td align=center>'+strmid(countf,7,5)+'</td></tr>'
	   endif else begin
	      line1 = day_str(i)+' '+dawn_str(i)+'-'+dusk_str(i)+ $
		  strmid(countf,7,5)
	   endelse
	   if qhc or qout then printf, lunhc, line1 else print, line1
endif

; -----------------------------
; Get details for the PFIs
; -----------------------------

	   if countp gt 1 then begin
	   	ufov = sxt_uniqfov(sxtp(ssp),fov=nfov,min_img=5)
				; Find unique pointings

        no_pfis=0
   for j=0,nfov-1 do begin      ; For each pointing...
        dummy_ufov=sxt_uniqfov(sxtp(ssp(ufov.(j))),uniqss=uniqss)  
                        ; Get unique (resolution+shape) sets for this ROI

        n_uniqss=n_elements(uniqss)
        counts_str=strarr(n_uniqss)
        for ii=0,n_uniqss-1 do begin
	 if (n_elements(ufov.(j)) gt 1) then begin
           cen = gt_center(sxtp(ssp(ufov.(j)(uniqss(ii)))),/helio,/string)
           rez = gt_res(sxtp(ssp(ufov.(j)(uniqss(ii)))))
           dummy_ss=where(gt_res(sxtp(ssp(ufov.(j)))) eq rez,n_dummy)
                        ; N_DUMMY counts how many have same resolution
                        ; in this ROI
           
           rez_str = strmid(strcompress(2.455*(2^rez),/rem),0,4)+'"'
           rez = transpose([[rez], [rez]])
           shp = fix(gt_shape(sxtp(ssp(uniqss(ii))))*2.455*(2^rez))
           shp_str = strcompress(shp / 60, /rem)+ '''' + $
                          strcompress(shp mod 60, /rem) + '"'
           shp_str = shp_str(0,*) + 'x' + shp_str(1,*)
           space_str = '    "         "        "                       '
           if qnar then narn=fov2nar(sxtp(ssp(ufov.(j)(uniqss(ii)))))
        
        ; Now count the PFIs
        ;

      n_dummy=fix(float(n_dummy)*64./float(sxtp(ssp(uniqss(ii))).shape_cmd(1)))
                        ; Accounts for Nx2, Nx3, etc. PFIs

           no_pfis=no_pfis+n_dummy      ; Total # PFIs this orbit
                                        ; (Not used, though)
           counts_str(ii)=strcompress(n_dummy,/rem)
                        ; No. of PFIs of this ROI, this shape, this res.
        
           xpos = strpos(shp_str,'x')
           shplen = strlen(shp_str)
           shp_str = counts_buffer(xpos-4) + shp_str +         $
                    counts_buffer(strlen(strmid(shp_str,7,6))-3)

; ---------------------------
; Output section.  (This part's pretty ugly too.)
; ---------------------------
	if qnar then begin         ;(for versions with NOAA no.)
	   if qhtml then begin     ;(HTML versions)
	      if qffi then begin   ;(version with FFI details)
	       line2 = '<tr><td align=center>"</td><td align=center>"</td><td align=center>"</td>'+$
			'<td></td><td></td><td></td><td align=center>'+counts_str(ii)+'</td>' + $
		     '<td align=center>'+cen+'</td><td align=center>' + $
		     shp_str+'</td><td align=center>'+rez_str+'</td><td align=center>'+narn+'</td></tr>'

	      endif else begin     ;(no FFI details)
	       line2 = '<tr><td align=center>"</td><td align=center>"</td><td align=center>"</td>'+$
			'<td></td><td align=center>'+counts_str(ii)+'</td>' + $
		     '<td align=center>'+cen+'</td><td align=center>' + $
		     shp_str+'</td><td align=center>'+rez_str+'</td><td align=center>'+narn+'</td></tr>'

              endelse  		     

	   endif else begin        ;(non-HTML version)
	     line2 =space_str+counts_buffer(strlen(counts_str(ii))-1)+ $
	           counts_str(ii)+'  '+cen+'  '+shp_str+ $
		   '  '+rez_str+'   '+narn
	   endelse

          endif else begin         ;(output for version without NOAA no.)
	     if qhtml then begin   ;(HTML versions)
	      if qffi then begin   ;(version with FFI details)
	       line2 = '<tr><td align=center>"</td><td align=center>"</td><td align=center>"</td>'+$
			'<td></td><td></td><td></td><td align=center>'+counts_str(ii)+'</td>' + $
		     '<td align=center>'+cen+'</td><td align=center>' + $
		     shp_str+'</td><td align=center>'+rez_str+'</td></tr>'
	      endif else begin     ;(no FFI details)
	       line2 = '<tr><td align=center>"</td><td align=center>"</td><td align=center>"</td>'+$
			'<td></td><td align=center>'+counts_str(ii)+'</td>' + $
		     '<td align=center>'+cen+'</td><td align=center>' + $
		     shp_str+'</td><td align=center>'+rez_str+'</td></tr>'

              endelse  		     
	   endif else begin        ;(non-HTML version)
	     line2 =space_str+counts_buffer(strlen(counts_str(ii))-1)+ $
	           counts_str(ii)+'  '+cen+'  '+shp_str+ $
		   '  '+rez_str
	   endelse
	endelse

	   if qhc or qout then printf, lunhc, line2 else print, line2
     endif  ; End trap for fewer than one PFI at this pointing
   endfor  ; ii-loop
  endfor  ; j-loop

endif
endfor ; i-loop

; ---------------------------
; Finish up.
; ---------------------------

	if qhtml then begin
	   if qhc or qout then printf, lunhc, '</table></body></html>' else print, '</table></body></html>'
	endif
	
	if qhc or qout then begin
	   close, lunhc
	   free_lun, lunhc
	   if qhc then $
		if not qout then dprint, outfile, /delete $
		else dprint, outfile
	endif

if keyword_set(qstop) then stop
end
