pro go_dsn_final, week, outfil=outfil
;+
; NAME: GO_DSN_FINAL
; PURPOSE: to produce output text file showing weekly summary
;         of DSN station-by-station coverage (incl. KSC)
; CALLING SEQUENCE: go_dsn_final,indgen(9)+5
;         This will create a file ~/dsn_summary.txt, showing the
;       summary of DSN downlinks by station for weeks 5-13
; INPUT:  week (can be a vector)
; OPTIONAL INPUT: outfile
; METHOD: apply rd_tfile on solass* and visible files
; HISTORY: NVN written 25-may-00
;      11-jun-00, debugged (HSH) to trap errors resulting from no SOLASS
;       3-jul-00, replaced Santiago printout with NASA total (HSH)
;-

extpr=anytim2ex(!stime)
if extpr(6) lt 1900 then extpr(6)=extpr(6)+1900 ; for inconsistent ext time
yymmdd=string(extpr(6),format='(i4.4)')+  $
  string(extpr(5),format='(i2.2)')+string(extpr(4),format='(i2.2)')

if n_elements(outfil) ne 1 then outfil='~/dsn_summary_'+yymmdd+'.txt'
dum0='Week'
dum1='Wallops'
dum2='Goldstone'
dum3='Canberra'
dum4='Madrid'
dum5='NASA'
dum6='Japan'

nw=n_elements(week)
openw,1,outfil
printf,1,dum0,dum1,dum2,dum3,dum4,dum5,dum6, $
  format='(a,5x,a,2x,a,3x,a,5x,a,7x,a,6x,a)'
;dsn_final,16,wallops=wallops                    ; removed 10-Jun-00 (HSH)
for i=0,nw-1 do begin
   dsn_final,week(i),wallops=wallops,santiago=santiago,$
     goldstone=goldstone,canberra=canberra,madrid=madrid
   if wallops eq 0 then goto, bail                         ; patch (HSH)
   cmd="ls -t "+'$DIR_GEN_STATUS/v*_*'+string(week(i),format='(i2.2)')+'*'
   spawn,cmd,files & vfil=files(0) ; newest one
   rv=rd_tfile(vfil,11)
   ksc=where(strmid(rv(10,*),0,3) eq 'Vis'  $
       or strmid(rv(9,*),0,3) eq 'Vis', nksc)
   nasa = wallops+goldstone+madrid+canberra
   printf,1,week(i),wallops,goldstone,canberra,madrid,nasa,nksc, $
     format='(2x,i2.2,1x,6(8x,i3))' 
endfor

bail:
close,1

return
end
