pro do_ads, stfil, enfil, week=week, do_error=do_error, sel_list=sel_list, $
		run_time=run_time, fail=fail
;
;
;HISTORY:
;	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
;
fail = 1
run_time = 0.
start_time = systime(1)
;
list = rd_ads_dir(/copy)		;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
	print, 'DO_ADS: No data files found for week: ', week, ' covers ', fmt_tim(sttim), ' ', fmt_tim(entim)
	print, 'Returning...'
	return
    end
end
;
if (keyword_set(do_error)) then begin
    errfil = '/ydb/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
;
fail = 0
for j=0,n_elements(sel_list)-1 do begin
    i = sel_list(j)
    infil = list(i).file
    outfil = concat_dir('/ydb/ads', infil)
    print, 'Now processing ', infil

    get_ads, infil

    cmd = 'ls "' + outfil + '"'
    spawn, cmd, result
    ;if (file_exist(outfil)) then begin
    if (result(0) ne '') then begin
	ads_into_pnt, outfil, run_time=run_time0

	openu, lun, '/ydb/ads/do_ads.log', /append, /get_lun	;open/close log each time to make sure it gets written
	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 + '"'
    end else begin
	fail = 1
	openu, lun, '/ydb/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    

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