pro flare2disk, outdisk=outdisk, prefix=prefix, select=select, $
   monode=monode, drive=drive
;+
;   Name: flares2disk
;
;   Purpose: copy files from MO to disk (mag or mo)
;
;   History:
;      1-Decc-1994 (SLF) 
;-

if not keyword_set(outdisk) then begin
   outdisk=''
   defdir=curdir()
   tbeep
   read,"Enter directory for output files (def=" + defdir + "): ",outdisk
   if outdisk eq '' then outdisk=defdir
endif

drive=keyword_set(drive)
if not keyword_set(monode) then monode=get_host(/short)
moname='/mo' + strtrim(drive,2)+'.' + monode
subdir='/yohkoh/*/'

if not keyword_set(prefix) then prefix='spr'
data=rd_tfile('/2p/freeland/dev/as/flare.dbase')   ; hardcoded

; split columns (0:date,1:time,2:weekid,3:fid,4:modisk)
coldata=str2cols(data)

times=reform(coldata(0,*)) + reform(coldata(1,*))
weeks=reform(coldata(2,*))
fids=strtrim(reform(coldata(3,*)),2)
mos=strtrim(reform(coldata(4,*)),2)

; loop by uniq mo side
order=sort(weeks)
umos=mos(uniq(mos(order)))

; ---------------- allow menu selection of mo subset -------------
if keyword_set(select) then begin
   moss=wmenu_sel(umos)
   if moss(0) eq -1 then begin
      message,/info,"Nothing selected, quitting..."
      return
   endif else umos=umos(moss)
endif 
umos=strtrim(umos,2)
; ---------------------------------------------------------------

resp=''
message,/info,"You will need the following MOs to accomplish this task..."
print,string(umos,format='(10(a5,2x))')
print
read,"Enter <CR> to continue, anything else to quit: ",resp
if resp(0) ne '' then stop,"Aborting run on request..."

badlog='mobad.log'
skiplog='moskip.log'
; ------------- loop through mos ---------------------
for i=0,n_elements(umos)-1 do begin
   tbeep
   print
   read,"Load MO# " + umos(i) + " on " + monode + ", drive# " + strtrim(drive,2) + ", <CR> to continue or 's' to skip MO: ",resp
   if strupcase(resp(0)) eq 'S' then begin
      mess='Skipping MO# ' + umos(i)
      message,/info,mess
      file_append,skiplog,mess,/uniq
   endif else begin
      momount,monode,drive=drive
      ss=where(mos eq umos(i),cnt)
      files=reform(str_perm(prefix,fids(ss)))
      prstr,["Looking for files...",'   ' + files]
      filesize=(total(get_afile_size(files)))(0)
      freespace=diskfree(outdisk)
      print,"Verifying free space..."
      if freespace(0) lt (float(filesize(0))/1e6)(0) then begin
         tbeep
         message,/info,"requested copy will overflow disk " + outdisk
         message,/info,"Clear some space and restart..."
         return
      endif
      list=file_list(concat_dir(moname,subdir),files)
      if list(0) eq '' then begin
         message,/info,"None of the expected files were found - check MO#
         file_append,badlog,["Expected files on " + umos(i) + " not found", files]
      endif else begin
         cpcmds='/bin/cp ' + list + ' ' + outdisk       
         prstr,"Spawning following copy commands..."
         for j=0,n_elements(cpcmds)-1 do begin
            print,'>>> ' + cpcmds(j)
            spawn,str2arr(cpcmds(j),' '),/noshell
         endfor
      endelse
      message,/info,"Dismounting disk..."
      momount,monode,drive=drive,/umount
   endelse
endfor

return
end


