pro mk_spd, sprs, outfile=outfile, conf_file=conf_file, mostfov=mostfov, $
	debug=debug
;
;+
;   Name: mk_spd
;
;   Purpose: make a processed version of an spr file (spd)
;
;   Input Parameters:
;      sprs -    file or file list of SPRs to process
;      outfile - output file name (default replaces SPR with SPD)
;      conf_file - name of configuration file (default $ys/sxt/doc/spd.config
;      mostfov - switch , if set, only do most frequen FOV
;
;   Calling Sequence:
;      mk_spd,sprs [,conf_file=configuration_file , /mostfov, outfile=outfile]
;
;   Method:
;      call sxt_uniqfov to identify uniq SXT pointings within spr files
;      call sxt_prep2 to prepare the data
;
;   Read selection/filter criteria from configuration file
;
;   History:
;      7-Sep-1993 (SLF) Proto version written
;      9-Dec-1993 (SLF) compress flag, sav_sda , sxt_prep call changes
;     13-Dec-1993 (SLF) change re-compressfrom from sxt_comp to sfd_comp
;      3-Feb-1995 (SLF) tweaked documentation
;     17-may-1995 (SLF) add despike_thresh and width to sxt_prep call
;-
debug=keyword_set(debug)

case n_elements(sprs) of
   0: message,'No input file name!...'
   1: 
   else: begin
      sprs=sprs(sort(sprs))
   endcase
endcase      

keywords=['min_sep',		$	; minimum X/Y in acrmintes per FOV
	  'min_img',		$	; minimum # of images/FOV to include
	  'recompress',		$
	  'tim2night',		$	; minutes before occultation
	  'tim2fms',		$	; minutes after fms
	  'rbm_status',		$
	  'despike_thresh',	$
	  'width',		$
          'mem_img',		$	; memory management (#images/loop)
	  'helio',		$
	  'register',		$
	  'normalize'		]	; [0=nosaa, 1=saa, *=dont care]

if n_elements(outfile) eq 0 then outfile=str_replace(sprs(0),'spr','spd')

; read configuration file
if n_elements(conf_file) eq 0 then $
	conf_file=concat_dir('$DIR_SXT_DOC','spd.config')

if string(conf_file) eq '1' then $
        conf_file=concat_dir('$DIR_SXT_DOC','spd.config')
if not file_exist(conf_file) then begin
   message,/info,'Configuration file: ' + conf_file + ' not found, returning...'
   return
endif

input=rd_tfile(conf_file,/compress,nocomment=';')

; process general keyword parameters 
for i=0,n_elements(keywords)-1 do begin
   find=wc_where(input,keywords(i)+'*',mcount,/case_ignore)
   if mcount gt 0 then begin
      for j=0,mcount-1 do begin
         if strpos(input(find(j)),'=*') eq -1 then stat=execute(input(find(j))) 
         remain=rem_elem(input,input(find(j)))
         input=input(remain)
      endfor
    endif
endfor

rd_roadmap,sprs,roadmap
rd_sda,sprs,-1,index,/nodata

uniqfov=sxt_uniqfov(index,min_sep=min_sep, counts=counts)
if keyword_set(mostfov) then begin
   message,/info,'Only including images from most common FOV...'
   min_img=max(counts)
endif

enough=where(counts ge min_img,ecount)

if ecount eq 0 then begin
   message,/info,'Not enough images for any FOV, returning...'
   return
endif else begin
   append=0				; new spd file on first sxt_prep call
   for fov=0,ecount-1 do begin		; for each uniq pointing
      message,/info,strtrim(counts(enough(fov)),2) + ' images for FOV# ' + strtrim(fov,2)
;     apply general filters
      ss=uniqfov.(enough(fov))
;
;     apply general filters
      fmess='No valid images for FOV# ' 
      tim2orbit,index(ss),tim2night=t2n,tim2fms=t2f

      if n_elements(tim2night) ne 0 then begin
	 message,/info,'Applying tim2night filter...'
         day=where(t2n gt tim2night, dcount)
         if dcount eq 0 then begin
            message,/info,'DAY FILTER ' + fmess + strtrim(fov,2)
         endif else ss=ss(day)
      endif
      if n_elements(tim2fms) ne 0 then begin
	 message,/info,'Applying tim2fms filter...'
         day=where(t2f  gt tim2fms ,dcount)
         if dcount eq 0 then begin
            message,/info,'DAY FILTER ' + fmess + strtrim(fov,2)
         endif else ss=ss(day)
      endif
      if n_elements(rbm_status) ne 0 then begin
	 message,/info,'Applying rbm filter...'
         rbms=where(index(ss).gen.rbm_status eq rbm_status, rcount)
         if rcount eq 0 then begin
            message,/info,'RBM FILTER ' + fmess + strtrim(fov,2)
         endif else ss=ss(rbms)
      endif
;     process composite images
      comps=wc_where(input,'composite',ccount)
      if ccount gt 0 then begin
         message,/info,'Processing composite image paramters'
         for i=0,ccount-1 do begin
            ecomps=wc_where(input,'endcomposite',eccount)
            prstr,input(comps(i):ecomps(i))
         endfor
      endif
      sss=(wc_where(input,'ss',sscount))(0)
      sse=(wc_where(input,'endss',ssecount))(0)
      if sse gt (sss+1) then begin
         message,/info,'Applying ss-filters'
         ssn=sss+1         
         repeat begin
	    sscond=input(ssn)
            sscond=str_replace(sscond,'index','index(ss)')
            sscond=str_replace(sscond,'roadmap','roadmap(ss)')
	    sscond=gt2exe(sscond,/addind)
	    extstr='ssnew=where(' + sscond + ',sscnt)'
            message,/info,extstr
            exestat=execute(extstr)
	    if sscnt gt 0 then ss=ss(ssnew)
	    help,ss
            ssn=ssn+1
	 endrep until sscnt eq 0 or ssn eq sse
      endif

      if ss(0) eq -1 then begin
	 message,/info,'No images left after applying filters!
      endif else begin
;        now call sxt_prep to do the work
;        10-mar-94 (SLF) modified to manage memory 
;        use common reference image
         rd_sda,sprs,ss(0),refindex,/nodata
;        mem_img = number of images per loop for memory management
;        may be set in mk_spd config file
         if n_elements(mem_img) eq 0 then mem_img = 50
         imgfirst=0
         remain=n_elements(ss)
         repeat begin
            imgcnt=min([mem_img,remain])
	    if imgcnt ne remain then begin
               message,/info,'Large Array: Memory Management Warning'
               message,/info,'Processing images: ' + $
                  strtrim(imgfirst,2) + ':'  + strtrim(imgfirst+imgcnt-1,2)
            endif
            sxt_prep, sprs, ss(imgfirst:imgfirst+imgcnt-1), $
		outindex,outdata , register=register , $
	        normalize=normalize,ref=refindex, helio=helio, $
                despike_thresh=despike_thresh, width=width
            if recompress then outdata=temporary(sfd_comp(outdata))
            sav_sda,outfile,outindex,outdata,append=append
            delvarx,outindex,outdata
            append=1	; set after first call
            imgfirst=imgfirst + imgcnt
            remain=remain-imgcnt
         endrep until remain le 0
      endelse
    endfor
endelse

return
end
