pro dsn_final, week, year=year,goldstone=goldstone, canberra=canberra, $
    madrid=madrid, santiago=santiago, wallops=wallops, verbose=verbose

;+
; NAME:  DSN_FINAL
; PURPOSE: Count the number of downlinks at each NASA station,
;          using the most updated information (the highest version
;          of solass.wNN files)
; CALLING SEQUENCES: dsn_final, week, year=year,goldstone=goldstone, $
;    canberra=canberra, madrid=madrid, santiago=santiago, wallops=wallops
; INPUT:  week:  integer like 35 for week 35, not a vector
; OPTIONAL INPUT: year, default is the present year
; OPTIONAL OUTPUTS: goldstone, canberra, madrid, santiago, wallops  --
;         these return numbers of downlinks
; HISTORY: Written by NVN, 25-May-00
;          12-Jun-00, trapped errors resulting from lack of SOLASS (HSH)
;           3-Jul-00, added NASA total to print
;        
;-

;  rest=7  nothing done
;  rest=6  if Sun is done
;  rest=5  if Sun/Sat are done
;  rest=4  if Sun/Sat/Fri are done
;  rest=3  if Sun/Sat/Fri/Thu are done
;  rest=2  if Sun/Sat/Fri/Thu/Wed are done
;  rest=1  if Sun/Sat/Fri/Thu/Wed/Tue are done
;  rest=0  if everything is done

prt=anytim2ex(!stime)
if n_elements(year) ne 1 then year=prt(6)
spawn,'pwd',pwd0  & pwd0=pwd0(0); save the present working directory
spawn,'hostname',hn
if strmid(hn(0),0,5) eq 'flare' then cd,'/home/flare/com/yohkoh/wallops' $
    else cd,'/f17p/com/yohkoh/wallops'
; nt=n_elements(week)
; for i=0, nt-1 do begin
stt=week2ex(year,week)
stt=addtime(stt,delta=1440) ; DSN's week starts on Monday
dayst=stt(4)
monst=stt(5)
yeast=stt(6)
cmd='ls -t solass.w'+string(week,format='(i2.2)')+'*'   
spawn,cmd,files
;
; The following put in 12-Jun-00 to handle operator errors
;
if (size(files))(0) eq 0 then begin
  print, "The week requested doesn't have a SOLASS file on this directory"
  wallops = 0
  goto, bail 
endif

nf=n_elements(files)
ngold=0
ncan=0
nmad=0
nsant=0
nwall=0
nother=0

i_rest=7
for i=0,nf-1 do begin
   infil=files(i) 
   if keyword_set(verbose) then print,'i=',i,' infil=',infil,'  i_rest=',i_rest
   if i_rest ne 0 then begin 
      y=rd_tfile(infil, 13)
      sz=size(y)
      n0=sz(2)
      jmon=where(strmid(y(0,*),0,4) eq '*MON', nmon)
      jtue=where(strmid(y(0,*),0,4) eq '*TUE', ntue)
      jwed=where(strmid(y(0,*),0,4) eq '*WED', nwed)
      jthu=where(strmid(y(0,*),0,4) eq '*THU', nthu)
      jfri=where(strmid(y(0,*),0,4) eq '*FRI', nfri)
      jsat=where(strmid(y(0,*),0,4) eq '*SAT', nsat)
      jsun=where(strmid(y(0,*),0,4) eq '*SUN', nsun)
      if nmon eq 1 then begin
         rest=0
         if rest lt i_rest then goto, count else goto, dummy
      endif
      if ntue eq 1 then begin
         rest=1
         if rest lt i_rest then goto, count else goto, dummy
      endif
      if nwed eq 1 then begin
         rest=2
         if rest lt i_rest then goto, count else goto, dummy
      endif
      if nthu eq 1 then begin
         rest=3
         if rest lt i_rest then goto, count else goto, dummy
      endif
      if nfri eq 1 then begin
         rest=4
         if rest lt i_rest then goto, count else goto, dummy
      endif
      if nsat eq 1 then begin
         rest=5
         if rest lt i_rest then goto, count else goto, dummy
      endif
      if nsat eq 1 then begin
         rest=6
      endif

count: if keyword_set(verbose) then print,'Working on ',infil,'   rest=',rest
      if i_rest eq 1 then bottom=jtue(0)
      if i_rest eq 2 then bottom=jwed(0)
      if i_rest eq 3 then bottom=jthu(0)
      if i_rest eq 4 then bottom=jfri(0)
      if i_rest eq 5 then bottom=jsat(0)
      if i_rest eq 6 then bottom=jsun(0)
      if i_rest eq 7 then bottom=n0-1
      
      ii=where(strmid(y(0,0:bottom),0,1) ne '*', ndata)
      nsta=n_elements(ii)
      if keyword_set(verbose) then print,'nsta=',nsta
      for k=0,nsta-1 do begin
         station=strmid(y(5,ii(k)),4,2) 
         if station eq '16' or station eq '17' then ngold=ngold+1
         if station eq '46' then ncan=ncan+1
         if station eq '66' then nmad=nmad+1
         if station eq '74' then nsant=nsant+1
	 if station eq '76' or station eq '80' or $
             station eq '82' then nwall=nwall+1
         if station ne '16' and station ne '17' and station ne '46' and $
             station ne '66' and station ne '74' and station ne '76' and $
             station ne '80' and station ne '82' then nother=nother+1
      endfor
      i_rest=rest
      goto, done
   endif
dummy: if keyword_set(verbose) then print,'No addition:     file=',infil
done:

endfor

if keyword_set(verbose) then begin
   print,y(*,1)
   print,'                '
   print,'Goldstone =',ngold
   print,'Canberra =',ncan
   print,'Madrid =',nmad
   print,'Santiago =',nsant
   print,'Wallops =',nwall
   print,'Other =',nother
   print,'NASA total =',ngold+ncan+nmad+nsant+nwall+nother
endif

wallops=nwall
santiago=nsant
canberra=ncan
goldstone=ngold
madrid=nmad
others=nother

bail:
cd,pwd0
end
