pro pr_sxt_term, st_date, en_date, qstop=qstop, outfile=outfile, hc=hc, 	$
		cmd=cmd, all=all,dpe_lo=dpe_lo,dpe_hi=dpe_hi,			$
		tim2night_lo=tim2ni_lo,tim2night_hi=tim2ni_hi,			$
		by_pointing=by_point,						$
		rmap=rmap, index=index, dset=dset, infil=infil, coord=coord0
;+
; NAME:
;	pr_sxt_term
; PURPOSE:
;	To summarize times of data from the SXT terminator database (sft)
; CALLING SEQUENCE:
;	pr_sxt_term			; List terminators for today
;	pr_sxt_term, '1-jan
;	pr_sxt_term, '30-oct-91', '2-nov-91', /hc, /by_point
;	pr_sxt_term, '30-oct-91', '2-nov-91', outfile=outfil, /cmd
; INPUT:
;	st_date	- The UT date to search for.  If no date is entered,
;		  the current date is used.  The year is not
;		  required, but if entered should be of the
;		  form 1-Oct-91 style.  The date should be
;		  entered in string style with date first.
; OPTIONAL INPUT:
;	en_date	- The ending date to list data for.  If no date is passed,
;		  it only searches for "st_date"
; OPTIONAL INPUT KEYWORDS:
;  	all	- Normally summarize images which have:
;				dpe_lo <= dpe       <= dpe_hi and  (=5.3s)
;		  		10s    <= tim2night <= 25s (prior to D/N)
;		  /all returns all images in the sft database.
;	dpe_hi	- Default = 25 (5.3 sec): Sets max dpe value to include
;	dpe_ho  - Default = 25 (5.3 sec): Sets min dpe value to include
;       tim2night_lo- Default = 10 sec.  Sets min time prior to D/N to include.
;       tim2night_hi- Default = 25 sec.  Sets max time prior to D/N to include.
;	by_point- If set, will summarize results by unique pointings and
;		  by gt_filtb values (includes ND mesh and non-ND mesh cases).
;	cmd	- If set, will call gt_center with /cmd to get command pointings
;			(less accurate, but considerably faster)
;	outfile	- If a filename is passed, the results will also
;		  be written to that file
;	hc	- If set, write the results to a temporary file, print
;		  it and delete it.
; OPTIONAL OUTPUT KEYWORDS:
;	rmap	- Roadmaps of the terminator images
;	index	- Vector of indexes of the terminator images
;	dset	- Vector of image numbers
;	infil	- Vector of sft file names
;	coord	- (2,N) array of results from gt_center(index,/angle)
;
;HISTORY:
;	14-Oct-94, J. R. Lemen, Written.
;-
version = 'V1.0  14-Oct-94'			; Version label

qhc = 0
qout = 0
if (keyword_set(outfile)) then begin
    openw, lunout, outfile, /get_lun
    qout = 1
end
if (keyword_set(hc)) then begin
    openw, lunhc, 'pr_sxt_term.temporary', /get_lun
    qhc = 1
end
;
;--------------------------------
; Get the filenames and roadmaps
;--------------------------------

infil = sft_files(st_date,en_date,count=count)
if count eq 0 then return
rd_roadmap,infil,rmap					; Read the roadmaps

;--------------------------
; Get time to night values
;--------------------------

tim2orbit,rmap,tim2night=tim2night
if n_elements(dpe_lo) eq 0 then dpe_lo = 25		; 5.4 sec exposure
if n_elements(dpe_hi) eq 0 then dpe_hi = 25		; 5.4 sec exposure
if n_elements(tim2ni_lo) eq 0 then tim2ni_lo = 10.	; Must be at least 10s prior to D/N
if n_elements(tim2ni_hi) eq 0 then tim2ni_hi = 25.	; Must be within   25s prior to D/N

;-------------------------------------
; Make selections on dpe and tim2night
;-------------------------------------

if keyword_set(all) then begin
   nimg = n_elements(rmap)
   dset = indgen(nimg) 
endif else dset = where((tim2night*60. ge tim2ni_lo) and	$
			(tim2night*60. le tim2ni_hi) and	$
			(gt_dpe(rmap) ge dpe_lo)     and	$
			(gt_dpe(rmap) le dpe_hi),nimg)

if nimg eq 0 then begin
   message,'No terminator images found for selected period',/info
   return
endif 

if nimg ne n_elements(rmap) then 	$
	message,string('Selecting images with '+strtrim(dpe_lo,2)+' <= dpe <= ',strtrim(dpe_hi,2)),/info
rmap = rmap(dset) & tim2night = tim2night(dset)

;----------------------------------
; Read indexes and get coordinates
;----------------------------------

rd_sda, infil, dset, index, /nodata		; Read the indexes
coord0 = gt_center(index,/angle,cmd=cmd)	; Angle in arcsec

;----------------------------------
; Set up header titles
;----------------------------------

tit = strarr(9)
tit(0) = ' pr_sxt_term                       '+version
tit(1) = ' Terminator Information for SXT.   Program Run: '+ !stime
tit(3) = '    Start time: '+fmt_tim(st_date)
if n_elements(en_date) ne 0 then tit(3) = tit(3) + '        Stop time: '+fmt_tim(en_date)
if keyword_set(all) then tit(4) = 'All images chosen' else tit(4) = $
	string('    Images selected with '+strtrim(dpe_lo,2)+' <= dpe <= '+strtrim(dpe_hi,2), $
		tim2ni_lo,' <= Tim2night <=',tim2ni_hi,' s',format='(a,2x,2(f5.1,a))')
tit(5) = (['    Using ATT database pointings','    Using commanded pointings'])(keyword_set(cmd))
tit(6) = '      Time                                                     Tim2night Angle'
tit(7) = '      (UT)                                                       (sec)  (arcmin)'
if keyword_set(by_point) then titp = tit([0,1,2,3,4,8])

;----------------------------------
; Format the time listing
;----------------------------------

info_array = strarr(n_elements(rmap))
coord1     = strarr(n_elements(rmap))	; Pointing as a string
ew  = (['e','w'])(coord0(0,*) gt 0)	; E-W Angle: Sun-center relative to CCD center
ns  = (['n','s'])(coord0(1,*) lt 0)	; N-S Angle: Sun-center relative to CCD center
for i=0,n_elements(rmap)-1 do begin
   coord1(i) = string(NS(i),abs(round(coord0(1,i)/60.)),EW(i),abs(round(coord0(0,i)/60.)), $
				format='(a,i2.2,a,i2.2)')
   info_array(i) = ' ' + fmt_tim(rmap(i)) + 	$
	                gt_dp_mode(rmap(i), space=2, /short) + '/' + gt_dp_rate(rmap(i), /string, /short) + $
	                gt_filta(rmap(i), space=2) + '/' + gt_filtb(rmap(i), /string) + $
	                gt_res(rmap(i), /space, /orig) + $
	                gt_dpe(rmap(i), /space) + gt_dpe(rmap(i), /str, /conv) + $
	                gt_shape_cmd(rmap(i), /space) + $
			string(tim2night(i)*60.,format='(f5.1)') + $
			'   '+coord1(i)
endfor			


;----------------------------------
; Format the by pointing listing
;----------------------------------

if keyword_set(by_point) then begin
   uniq_coord = coord1(uniq(coord1, sort(coord1)))	; Get uniq pointings
   map_arr = strarr(n_elements(uniq_coord))		; Set up output array
   titp = [titp , string(' Pointing ',strtrim(gt_filtb(indgen(5)+2,/str),2),format='(a,2x,5(a6))')]
   for j=0,n_elements(uniq_coord)-1 do begin
	knum = intarr(5)				; don't include filtb = open
        for i=2,6 do begin				; Loop on Al01, AlMg, Be119, Al12, Mg3
	    ii = where((coord1 eq uniq_coord(j)) and (gt_filtb(rmap) eq i),ncount)
            knum(i-2) = ncount
        endfor
	map_arr(j) = string(uniq_coord(j),knum,format='(2x,a,2x,5i6)')
   endfor
endif
  
; ------------------------------
; Print the header information
; ------------------------------
prstr, [tit, info_array]
for i=0,n_elements(tit)-1 do begin
    if (qhc)  then printf, lunhc, tit(i)
    if (qout) then printf, lunout, tit(i)
end

  
; ------------------------------
; Print the information by time
; ------------------------------
for i=0,n_elements(info_array)-1 do begin
    if (qhc)  then printf, lunhc, info_array(i)
    if (qout) then printf, lunout, info_array(i)
end


; --------------------------------------
;  Include the by pointing information 
; --------------------------------------
if keyword_set(by_point) then begin
   prstr, [titp, map_arr]		; Send it tty
   FF = string(12B)                	; Form-feed character
   if (qhc)  then printf, lunhc,  FF
   if (qout) then printf, lunout, FF
   for i=0,n_elements(titp)-1 do begin
     if (qhc)  then printf, lunhc, titp(i)
     if (qout) then printf, lunout, titp(i)
   endfor
   for i=0,n_elements(map_arr)-1 do begin
    if (qhc)  then printf, lunhc, map_arr(i)
    if (qout) then printf, lunout, map_arr(i)
   endfor
endif


; ----------------------------------------
; Hard copy was requested
; ----------------------------------------
if (qhc) then begin
    free_lun, lunhc
    dprint, 'pr_sxt_term.temporary', /delete
end
if (qout) then free_lun, lunout
if (keyword_set(qstop)) then stop
end
