pro do_ads,    stfil, enfil, week=week, do_error=do_error, $
		sel_list=sel_list, nopnt=nopnt,		   $
		main2=main2, odisk=odisk, okftp=okftp, retry=retry, $
		outftp=outftp, noindex=noindex
;
;+
;
;   Name: do_ads
;
;   Purpose: ftp (get) specified ADS files from mainframe to workstation
;	     (optionally insert ADS info into PNT files)
;
;   Input Parameters:
;	stfil - starting file number
;       enfil - ending file number
;       week  - form yy_ww - gets all files for that week
;
;   Optional Keyword Parameters:
;      main2 - passed through to get_ads (uses facom2 telnet address if set)
;      odisk - passed throuth to get_ads (Optical disk naming convention)
;      okftp - character string of successful ftps (null if failure)
;      nopnt - if set, does not run ads_into_pnt (ftp only)
;
;   HISTORY:
;	(do_ads.pro)      Written Jul-92 by M.Morrison
;       27-Aug-92 (MDM) - Added keyword WEEK
;			- Also added RUN_TIME and FAIL
;	31-Aug-92 (MDM) - Made RD_ADS_DIR use the /COPY switch to get the
;			  newest directory listing from the mainframe
;	14-Sep-92 (SLF) - Split ftp and ads_into_pnt logic for early
;			  diagnosis of ftp transfer errors
;			  added keywords to get_ads (odisk/main2)
;			  added outftp for ads file output (ftp get target)
;		          added references to environmental variables
;	20-Oct-92 (slf) - changed get_ads2 references to get_ads
;			  renamed do_ads2 to do_ads
;			  (renamed get_ads2.pro to get_ads.pro)
;			  add noindex retry keywords
;	27-oct-92 (slf) - code to avoid copying existing files
;			  minor mods to assist cronjob execution
;	14-Nov-92 (slf) - mod to retry option/messages
;	17-Aug-93 (MDM) - Changed ADS_INTO_PNT call into ADS_INTO_ATT
;			  call.
;	 1-Sep-93 (MDM) - Modified so that it recognizes compressed ADS
;			  files and will not copy them over
;	 3-Jan-94 (MDM) - Changed mail distribution
;       21-Feb-94 (SLF) - Compress all new stuff 
;        1-Mar-94 (SLF) - Fix bug; 1 file in INDEX does not exist
;	14-Mar-94 (SLF) - file delimitors (embeded meta-character problem)
;			  ads files have parens, like: PSK239.SAADF.A9311(T1900001).Z
;       
;
;-
;
if not keyword_set(outftp) then outftp=getenv('DIR_GEN_ADS')
run_time = 0.
start_time = systime(1)
;
list = rd_ads_dir(copy=1-keyword_set(noindex))		;do not sort - do them by original entry number
;
if (n_elements(stfil) ne 0) then begin
    n = enfil-stfil+1
    ;sel_list = (n-1-indgen(n)) + stfil	;work backwards (?)
    sel_list = indgen(n) + stfil	
end
;
if (keyword_set(week)) then begin
    rd_fem, fix(strmid(week, 3, 2)), fix(strmid(week, 0, 2)), fem
    sttim = fem(0)
    entim = anytim2ints(fem(n_elements(fem)-1), off=100*60.)	;100 minutes past last s/c day

    n = n_elements(list)
    ads_st = anytim2ints(list)
    ads_en = anytim2ints(list, off=list.duration)
    qsel = bytarr(n)
    for i=0,n-1 do begin
	if ( (int2secarr(ads_st(i), sttim) ge 0) and (int2secarr(ads_st(i), entim) le 0)) then qsel(i) = 1	;start time within week
	if ( (int2secarr(ads_en(i), sttim) ge 0) and (int2secarr(ads_en(i), entim) le 0)) then qsel(i) = 1	;end time within the week
    end
    sel_list = where(qsel)	;maintain the order that they were created - have later files overwrite earlier files
    if (sel_list(0) eq -1) then begin
        message,/info,'No data files found for week: ' + week + ', returning...'
	return
    end
;---------------------------------------------------------------------------
;   slf, 27-oct-1992 - check for non existant subset
;   Note - need quotes on files since parentheses in ADS filenames cause
;   trouble using findfile under unix    
;   slf 14-mar remove quotes, file_exist updated
    chkfiles=concat_dir(outftp,list(sel_list).file)
    chkfilesZ=chkfiles + '.Z'    
    nonexist= 1 - (file_exist(chkfiles) or file_exist(chkfilesZ))	;MDM modified 1-Sep-93
    anychk=where(nonexist,anycnt)
    if anycnt eq 0 then begin
       message,/info,'All existing ADS files for week:' + week + $
          ' already copied,  returning...'
       return
    endif else begin
       message,/info,'Number of ADS files to copy for week:' + $
	  week + ': ' + strtrim(anycnt,2)
       sel_list=sel_list(anychk)			; reduced set 
       prstr,list(sel_list).file
    endelse
;-----------------------------------------------------------------------------
end
;

if (keyword_set(do_error)) then begin
    errfil = '$DIR_GEN_ADS/do_ads.error'
    sel_list = fix(rdfile(errfil, 1))
    spawn, 'mv ' + errfil + ' ' + errfil + '_old'
end
;
if (n_elements(stfil) eq 0) then stfil = 0
if (n_elements(enfil) eq 0) then enfil = n_elements(list)-1
;
ftpok=['']					; initilize succes vector

for j=0,n_elements(sel_list)-1 do begin
    i = sel_list(j)
    infil = list(i).file
    outfil = concat_dir(outftp, infil)
    print, 'Now processing ', infil
    main2=keyword_set(main2)
    odisk=keyword_set(odisk)
    get_ads, infil, main2=main2, odisk=odisk, outdir=outftp
    cmd = 'ls "' + outfil + '"'
    spawn, cmd, result
;   slf, 19-oct-92 - add retry option on failure
;   invert options (odisk and node) 
    if result(0) eq '' and keyword_set(retry) then begin
	   message,/info, 'First FTP attempt for: ' +  infil + ' failed'
	   message,/info, 'Retrying using MTL device'
           get_ads, infil, main2=1-main2, odisk=1-odisk, outdir=outftp
           spawn, cmd, result
    endif
    ftpok=[ftpok,result(0)]			; append to success vector
    if (result(0) eq '') then begin
	openu, lun, '$DIR_GEN_ADS/do_ads.error', /append, /get_lun	;open/close log each time to make sure it gets written
	printf, lun, i, infil, gt_day(list(i),/str), 'ERROR IN COPY', !stime, format='(1x, i5, a30, 1x, a, 3x, a, 3x, a)'
	free_lun, lun
    end
end    

ftpsuccess=where(ftpok ne '',count)
if count gt 0 then ftpok=ftpok(ftpsuccess)

if not keyword_set(nopnt) then begin		
   for i=0,count-1 do begin
      message,/info,'ads_into_pnt:' + ftpok(i)
      ;ads_into_pnt,ftpok(i), run_time=run_time0
      ads_into_att,ftpok(i), run_time=run_time0
      mail, 'ADS_INTO_ATT run for '+ftpok(i), user='$MAIL_CRONJOBS'
      openu, lun, '$DIR_GEN_ADS/do_ads.log', /append, /get_lun    
      printf, lun, i, infil, gt_day(list(i),/str), run_time0, !stime, $
	  format='(1x, i5, a30, 1x, a, 3x, f7.2, 3x, a)'
      free_lun,lun
      ;; spawn, 'rm -f "' + outfil + '"'	;MDM removed 17-Aug-93
   endfor
endif

; slf   - compress new stuff
if count gt 0 then file_compress, ftpok 

end_time = systime(1)
run_time = (end_time-start_time)/60.
end
