pro chk_pointing, sprfiles, hc=hc, mispoints=mispoints, nimages=nimages, $
	min_sep=min_sep, sfd=sfd
;+
;   Name: chk_pointing
;
;   Purpose: check SXT OR pointings and generate statistics
;
;   Input Parameters:
;      sprfiles: The following options are allowed for sprfiles:
;		 1. not supplied - user selects from menu (recent spr files)
;		 2. string/string arrary - supplied spr file names
;		 3. Numeric - most recent N spr files are checked
;
;   Calling Sequence:
;      chk_pointing [sprfiles, min_sep=min_sep, /sfd , /hc]
;
;   Calling Examples:
;      chk_pointing [,/sfd]		  ; select 1 or more spr from menu
;      chk_pointing,newfiles(/spr,last=3) ; statistics for last 3 spr files
;      chk_pointing,3			  ; same as above
;      chk_pointing,3, /sfd       	  ; statistics PLUS graphical 
;
;   Optional Keyword Parameters:
;	hc      - (in) sitch, if set, hardcopy of statistics page
;	min_sep - (in/out) minimum seperation for uniq fov (pass to sxt_uniqfov)
;	nimages - (out) - total number of images per file
;	mispoint- (out) - number of bad pointings / file (guess!!)
;
;   Calls: newfiles, sxt_uniqfov, saa_in, chk_orfov, and dbase access stuff
;
;   Restrictions: if spr filenames not supplied, must have spr data in
;		  $DIR_SITE_NEWDATA - sfd option assumes corresponding sfd
;		  data in same directory
;
;   History:
;   11-Sep-1993 (SLF) Written
;   14-sep-1993 (SLF) Handle multiple xshapes in single FOV
;
;  NOTE: as with lastsfd, crash may occur if reformatting is in progress
;        you can always read the most recent COMPLETE spr/sfd files with:
;           chk_pointing,newfiles(/spr,old=1,/last),/sfd
;-			
if not keyword_set(min_sep) then min_sep=1.0
;
; check positional paramter option
sfiles=size(sprfiles)
case sfiles(sfiles(0)+1) of 
   7: spfiles=sprfiles
   0: spfiles=newfiles(/spr,/select)
   else: begin
      message,/info,'Checking last ' + strtrim(sprfiles,2) + ' spr files'
      spfiles=newfiles(/spr,last=sprfiles)
   endcase
endcase

; define general header 
nightmins=.5						; 30 seconds till lms
header=['CHK_POINTING run at: ' + fmt_tim(!stime) ,'']
header=[header,'Minimum FOV Separation: ' + strtrim(min_sep,2)]
header=[header,'"Night" cutoff: ' + strtrim(nightmins,2) + ' mins. befor LMS']

; by fov or by file?
byfov=keyword_set(byfov)
byfile=1-keyword_set(byfov)	; default

mispt=['YES','NO']		; program mis-point guesses
out_arr=header
mispoints=0.			; vector of mispoint percentages (guess!)
nimages=0.			; vector of total images

for i=0,n_elements(spfiles)-1 do begin
;  read the roadmap for one file
   rd_roadmap,spfiles(i),rmap

;  ---------------- sfd display ---------------------
;  get corresponding sfd file, if requested
   if keyword_set(sfd) then begin
      if n_elements(sfdw) eq 0 then wdef,sfdw,512,512, /ur
      usesfd=str_replace(spfiles(i),'spr','sfd')
      if not file_exist(usesfd) then begin
         message,/info,"Can't find corresonding sfd file..."
         usesfd=newfiles(/sfd,/last)
      endif
      message,/info,'Reading corresponding sfd file...'
      lastsfd,index,data,rebin=512,/notv, usefile=usesfd
      plot_fov,rmap,img=data,/nocmd,/skipfirst
      stepper_title,get_info(index,/non)
   endif
;  -----------------------------------------------------

;  call sxt_uniqfov to determine uniq pointings within min_sep
   ufov=sxt_uniqfov(rmap, min_sep=min_sep, counts=counts, avgpnt=avgpnt)
   nfov=n_tags(ufov)
   message,/info,'Number  of uniq pointings: ' + strtrim(nfov,2)
;
;  get ephemeris information for statistics
   tim2orbit,rmap,tim2night=tim2night

;  heder per file...
   fheader=['','File:' + spfiles(i)]
   fheader=[fheader,'Number  of uniq pointings: ' + strtrim(nfov,2)]
   fheader=[fheader,'', $
	'   FOV# NImag	Helio  #SAA #Night  ActRatio Mispoint','']
   onefile=fheader

   totbad=0.
   totimg=total(counts)
;  -------------- loop for each uniq FOV in one file ----------------------
   for fov=0,n_tags(ufov)-1 do begin			; for each FOV
;     get the data
      rd_sda,spfiles(i), ufov.(fov), index, data

;     --------- determine the 'Activity Ratio' via chk_orfov.pro
      xcmd=gt_shape_cmd(index,/x)
      sdata=size(data)
      small=where(xcmd ne sdata(1),scount)			; smaller?
      if sdata(0) eq 3 and scount gt 0 then begin
	message,/info,'Mixed shapes in OR, shifting image...'
	for img=0,scount-1 do begin
	   shift=sdata(2) - (xcmd(small(img))/2)
	   temp=data(0:shift*2-1,*,small(img))
           data(*,*,small(img))=1
           data(shift,0,small(img))=temp
;	   data(shift,0,small(img))=data(0:shift*2-1,*,small(img))
        endfor
      endif
      act_ratio=chk_orfov(data,/average)			; 'activity ratio'
      if act_ratio lt 1.1 then totbad=totbad+counts(fov)	; EMPERICAL!!!

;     -------- generate epemeris statistics
;     in SAA (via saa_in.pro)
      nsaa=saa_in(index)
      tsaa=fix(total(nsaa))
      nnight=where(tim2night(ufov.(fov)) lt nightmins,tnight)
;
;     --------- write fov summary statistics 
      onefov=string(fov, counts(fov), conv_a2h(avgpnt(*,fov),/string,/arcmin), $
	  tsaa, tnight,act_ratio, mispt(act_ratio gt 1.1),    $
	  format="(i6, i6, '   ', a6, i6, i6, f10.5, a6)")
      onefile=[onefile,onefov]   
   endfor

   onefile=[onefile,'-----------------------------------------------------']
;  Calculate mispoint statistic
   misone=totbad/totimg*100
   mispoints=[mispoints, totbad]
   nimages=[nimages,totimg]
   onefile=[onefile,'Mispoint Rate: ' + $
	string(misone,'%',format="(f5.1,a)") ]
;  -------------
   onefile=onefile(1:*)
   prstr,onefile
   out_arr=[out_arr,onefile]
endfor
; trim initial values
mispoints=mispoints(1:*)
nimages=nimages(1:*)

if keyword_set(hc) then prstr,out_arr, hc=hc
end
      

         

