pro delete_week, week, indir=indir, noconfirm=noconfirm, move=move, $
	qstop=qstop, checksize=checksize
;
;+
;NAME:
;	delete_week
;PURPOSE:
;	Delete all files from a given week using the File ID as the
;	input to determine which week it falls in.
;CALLING SEQUENCE:
;	delete_week
;	delete_week, '92_52'
;       delete_week, '94_28', move='/yd10'	  ; creates sub: /yd10/94_28t
;	delete_week, '92_51', indir='/yd3/reformat'
;	delete_week, '93_03', indir='/yd3/reformat', move='/yd10/93_03t/.'
;       delete_week, '94_28', /checksize ; print how much space is required
;OPTIONAL INPUT:
;	week	- The week ID to delete in string form YY_WW
;OPTIONAL KEYWORD INPUT:
;	indir	- The input directory to use
;	noconfirm - If set, do not ask for a confirmation before
;		    deleting.
;HISTORY:
;	Written 5-Jan-93 by M.Morrison
;	27-Jan-93 (MDM) - Added the MOVE option
;	 4-Mar-93 (MDM) - Added /QSTOP option
;	30-Aug-93 (SLF) - Use file filter template (avoid garbage)
;        7-Jul-94 (SLF) - For moves, check sizes and abort if overflow predicted
;			  Add CHECKSIZE keyword - Create subdir YY_WWt
;-
;
if (n_elements(indir) eq 0) then indir = ''
if (n_elements(week) eq 0) then input, 'Enter the week number', week, '92_15'
;
ff = findfile(concat_dir(indir, '?????????.????'))
if (ff(0) eq '') then begin
    print, 'DELETE_WEEK: No files found in ' + indir
    return
end
;
break_file, ff, dsk_log, dir, filnam, ext
fid = strmid(filnam, 3, 6) + strmid(ext, 0, 5)
ufid = fid(uniq(fid, sort(fid)))
dattim = anytim2ints(fid2ex(ufid))
;
tim2orbit, dattim, wid=wid
;
ss = where(wid eq week, count)
if (keyword_set(qstop)) then stop
if (count eq 0) then begin
    print, 'DELETE_WEEK: No files found with week: '+ week
end else begin
    print, 'The following File IDs are included in your request:'
    prstr,[string(ufid(ss),format='(5a13)'),'']
    if keyword_set(move) or keyword_set(checksize) then begin
       message,/info,"Checking file sizes...."
       total_size= $
	 total(float(file_stat(file_list(indir,'*'+ufid(ss)),/size))) / 1.e6
         print
         message,/info, $
	   "Required Space: " + strtrim(total_size,2) + " Mbytes"
         if keyword_set(checksize) then begin
	    print
            return
         endif
       if total_size ge diskfree(move) then begin
          tbeep
          message,/info, "This will overflow disk " + move
          message,/info, "Returning..."
          return
       endif 
;      
;      if only the toplevel was passed, append YY_WWt
       if strpos(move,week) eq -1 then $ 	
	 dmove=concat_dir(move, week + 't') else dmove= move
       message,/info,"These files will fit on " + dmove
;      verify the subdirectory exists, else ,create it and set protection
       if not file_exist(dmove) then begin
          message,/info,"Creating subdirectory: " + dmove
          spawn,"mkdir -p " + dmove
          spawn,"chmod 777 " + dmove
       endif
    endif
    print
    if (not keyword_set(noconfirm)) then begin
	word = 'delete?'
	if (keyword_set(move)) then word = 'move?'
	yesnox, 'Is this ok to '+word, in, 'Yes'
	if (in eq 0) then return
    end
    for i=0,count-1 do begin
	fff = concat_dir(indir, '*' + ufid(ss(i))+'*')
	cmd = 'rm -f ' + fff
	if (keyword_set(move)) then cmd = 'mv ' + fff + ' ' + dmove
    	print, cmd
	spawn, cmd
    end
end
;
end
