pro go_toban, interactive=interactive, $
    nosrsjcl=nosrsjcl, nosrsget=nosrsget, noreformat=noreformat, noobs=noobs, noevn=noevn, nopnt=nopnt, nosfd=nosfd, $
    nosl=nosl, noccd_sunc=noccd_sunc, nosft=nosft, $
    journal=journal, mail=mail, error=error, $
    year=year, st_tim=st_tim0, en_tim=en_tim0, summary_line=summary_line, dsn=dsn, $
    flare2=flare2, srsget=srsget, copy_cmd=copy_cmd, daily_forecast=daily_forecast
;
;
summary_line = '????'
indir = [getenv('DIR_SITE_NEWDATA')]            ;the reformatted data directory
st_iindir = n_elements(indir)   ;start with the last one
repeat begin
    st_iindir = st_iindir - 1
    fff = file_list(indir(st_iindir), 'ada*')
end until ((st_iindir eq 0) or (fff(0) ne ''))
iindir = st_iindir
wid = ' '
error = 0
start_time1 = systime(1)
run_time1 = 0.
run_time2 = 0.
;
;;qsrsjcl   = 1 - keyword_set(nosrsjcl)     ;No = 0, Yes = 1
qsrsget   = 1 - keyword_set(nosrsget)
qreformat = 1 - keyword_set(noreformat)
qobs      = 1 - keyword_set(noobs)
qevn      = 1 - keyword_set(noevn)
qpnt      = 1 - keyword_set(nopnt)
qsfd      = 1 - keyword_set(nosfd)
qsl       = 1 - keyword_set(nosl)
qccd_sunc = 1 - keyword_set(noccd_sunc)
qsft      = 1 - keyword_set(nosft)
ans_arr = ['No ', 'Yes']
if (keyword_set(interactive)) then begin
    ;yesnox, 'Run SRSJCL?   ', qsrsjcl, ans_arr(abs(qsrsjcl))   ;No = 0, yes = 1
    yesnox, 'Run SRSGET?   ', qsrsget, ans_arr(abs(qsrsget))
    yesnox, 'Run REFORMAT? ', qreformat, ans_arr(abs(qreformat))
    yesnox, 'Run MK_OBS?   ', qobs, ans_arr(abs(qobs))
    yesnox, 'Run MK_EVN?   ', qevn, ans_arr(abs(qevn))
    yesnox, 'Run MK_PNT?   ', qpnt, ans_arr(abs(qpnt))
    yesnox, 'Run MK_SFD?   ', qsfd, ans_arr(abs(qsfd))
    yesnox, 'Run MK_SL?    ', qsl, ans_arr(abs(qsl))
    yesnox, 'Run CCD_SUNC? ', qccd_sunc, ans_arr(abs(qccd_sunc))
    yesnox, 'Run MK_SFT?   ', qsft, ans_arr(abs(qsft))
end
;
if (keyword_set(journal)) then begin        ;MDM added 30-Nov-92
    jou_fil = '~/GO_TOBAN' + ex2fid( anytim2ex(!stime) ,/sec)
    if (!journal ne 0) then begin
    print, 'GO_TOBAN: Journal option is used, but a journal file is already open'
    print, 'GO_TOBAN: Closing current journal file'
    tbeep
    journal     ;close journal file
    end
    journal, jou_fil
end
;
ans_arr = ['will be NOT run', 'will be run']        ;MDM added 30-Nov-92
;print, 'SRSJCL   ', ans_arr(abs(qsrsjcl))
print, 'SRSGET   ', ans_arr(abs(qsrsget))
print, 'REFORMAT ', ans_arr(abs(qreformat))
print, 'MK_OBS   ', ans_arr(abs(qobs))
print, 'MK_EVN   ', ans_arr(abs(qevn))
print, 'MK_PNT   ', ans_arr(abs(qpnt))
print, 'MK_SFD   ', ans_arr(abs(qsfd))
print, 'MK_SL    ', ans_arr(abs(qsl))
print, 'MK_SFT   ', ans_arr(abs(qsft))
;
; slf, add code to find machine dependent raw telemetry mount point
; slf, 12-May-1994 - dont spawn df commnnds, use fstab for raw mount
if (qsrsget) or (qreformat) then begin
   delim=':'                        ; fstab delimiter
   if (strmid(get_host(), 0, 5) eq 'flare') then delim=' '
   fstab=rd_tfile('/etc/fstab',5,delim=delim)       ; read into columns
   raw=where(strpos(fstab(0,*),'raw') ne -1,rcnt)   ; raw telem disk
   rawmount='/raw_tlm'                  ; default
   if rcnt gt 0 then begin              ; found, entry 
      rawrec=fstab(*,raw)
      mount=where(strpos(rawrec(1:*),'raw') ne -1, rcnt) ; now find mount
   endif
   if rcnt gt 0 then rawmount=(rawrec(mount+1))(0) else $
      message,/info,"Can't find raw reference in fstab, using default"
   message,/info,'Raw Telemetry Disk is: ' + rawmount
endif

;pgs files = file_list(indir, 'ada*')
;; GLS - 23-Mar-99 - added following line to ensure proper ordering and
;;   uniqueness of files:
;pgs files = uniq(files,sort(files))
;
;pgs: scrutinize
files_temp = file_list(indir, 'ada*')
ss_temp = uniq(files_temp,sort(files_temp))
files=files_temp(ss_temp)
;
;
nfil = n_elements(files)
;
if (qsrsget) then begin
;   --------------- Auto Purge ------------------------------------------
;   slf, 25-nov-1992 - add auto purge of raw telemetry disk
    message,/info,'Purging raw telemetry disk...'
;   purgefiles= rawmount + '/v1/9*/????????.????????'
    purgefiles= rawmount + '/v1/????/????????.????????'  ; y2k fix
    file_purge,purgefiles,keep=15,/bydate 
;   ----------------------------------------------------------------------

    if (n_elements(st_tim0) eq 0) then begin
    i = nfil-1
    qdone = 0
    while ((not qdone) or (i eq -1)) do begin
        infil = files(i)
        rd_fheader, infil, fheader
        last_data = anytim2ints([fheader.last_time, fheader.last_day])
        if (last_data.day ne 0) then qdone = 1  ;could have a file that is not complete
        i = i - 1
    end
    if (i eq -1) then begin
        message = ['There are no good ADA files in '+ arr2str(indir), $
            'so the date/time of the last processed data is not available']
        print, message
        if (keyword_set(mail) and (getenv('MAIL_TOBAN_ERROR') ne '')) then mail, message, user='$MAIL_TOBAN_ERROR'
        print, 'Returning...
        error = 100
        return
        end
    st_tim = last_data 
    end else begin
    st_tim = anytim2ints(st_tim0)               ;in UT
    end
    cur_tim = anytim2ints(!stime, off=-9*60L*60L)   ;JST is 9 hours ahead of UT
    ;
    ;if (n_elements(st_tim0) eq 0) then st_tim = last_data else st_tim = anytim2ints(st_tim0)               ;in UT
    if (n_elements(en_tim0) eq 0) then en_tim = cur_tim   else en_tim = anytim2ints(en_tim0)
    ;
    del_hrs = int2secarr(en_tim, st_tim)/60/60
    srsget_duration = ['Start time for requested transfer: ' + fmt_tim(st_tim), $
            'End time for requested transfer:   ' + fmt_tim(en_tim), $
            'SRSGET Time Spanned:               ' + string(del_hrs) + ' hours']
    if (del_hrs gt 96) then begin
    message = ['This is the improper use for GO_TOBAN', $
        'The duration should never be over 96 hours', $
        srsget_duration]
    print, message
    ;;if (keyword_set(mail) and (getenv('MAIL_TOBAN_ERROR') ne '')) then mail, message, user='$MAIL_TOBAN_ERROR'
    error = 101
    ;;print, 'Returning...'
    ;;return
    end
    qok = 1
    ;;yesnox, 'Is this ok? ', qok, 'Yes'    ;SLF removed 24-Oct-92
    ;
    if (not qok) then stop, 'Stopping...'
    ;
    ;---------------------------------------------------------------------------------
    ;
    message,/info,'Starting get_sirius phase...'
    dsn=keyword_set(dsn)
    nodsn=1-dsn    
    get_sirius, st_tim, en_tim, error, temp=nodsn, nodsn=nodsn, outfil=rawfil, flare2=flare2, srsget=srsget
    message = ''
    case error of
    0: ;do nothing - success
    9: begin
        message = 'GO_TOBAN: ERROR DURING SRSGET.  Mainframe is probably off'
        mail_to = 'MAIL_TOBAN_WARN'
        end
    10: begin
            message = [message, 'GO_TOBAN: Someone else is running SRSGET', $
                'GO_TOBAN: Please terminate other user of SRSGET.']
            mail_to = 'MAIL_TOBAN_ERROR'
        end
    else: begin
        message = 'GO_TOBAN: ERROR DURING SRSGET.  Error = '+string(error)
        mail_to = 'MAIL_TOBAN_WARN'
        end
    endcase
    if (message ne '') then begin
    print, message
    if (keyword_set(mail) and (getenv(mail_to) ne '')) then mail, message, user='$'+mail_to
    print, 'Returning...'
    summary_line = message(0)
    return
    end

    if (not file_exist(rawfil)) then begin      ;MDM added 3-Dec-92
    message = ['GO_TOBAN: ERROR DURING SRSGET - Output file was not created', $
        'and there was no associated error returned from SRSGET']
    print, message
    if (keyword_set(mail) and (getenv('MAIL_TOBAN_ERROR') ne '')) then mail, message, user='$MAIL_TOBAN_ERROR'
    print, 'GO_TOBAN: Returning...
    error = 110
    summary_line = message(0)
    return
    end
    nnn = file_info2(rawfil, finfo)
    nmf = finfo.size/9216       ;number of major frames copied
    if (nmf lt 35) then begin               ;MDM added 3-Dec-92
    message =  ['GO_TOBAN: No new data.  Only one minute or less of data copied', $
            'Deleting file: ' + rawfil(0) + ' and exiting GO_TOBAN']
    spawn, 'rm -f ' + rawfil(0)
    print, message
    if (keyword_set(mail) and (getenv('MAIL_TOBAN_WARN') ne '')) then mail, message, user='$MAIL_TOBAN_WARN'
    print, 'GO_TOBAN: Returning...
    error = 120
    if (keyword_set(copy_cmd)) then spawn, copy_cmd    ;Now copy SRSPAS.NEW to SRSPAS.OLD - copied here 6-Mar-95
    summary_line = message(0)
    return
    end
end
;
if ((not qsrsget) and (qreformat)) then begin           ;MDM added 2-Nov-92
    ;;dirs = get_subdirs('/flare1_raw_tlm_mount/v1')        ;only true for ISASSx machines
    ;;;dirs = get_subdirs('/raw_tlm/v1')                ;true for flare1 and flare11 machines
    dirs = get_subdirs(rawmount + '/v1')        ; slf, 24-nov-92
    ff = file_list(dirs, '*')
    
    nfile = file_info(ff, finfo)
    for i=0,nfile-1 do print, i, ' -- ', finfo(i).filename, '     ', finfo(i).date, ' ', finfo(i).time, ' ', finfo(i).size
    print, -1, ' == File name to be entered by hand'
    input, 'Enter file number to reformat ', ifil, nfile-1

    rawfil = ' '
    if (ifil eq -1) then begin
    read, 'Enter the full file name (including the path) ', rawfil
    if (not file_exist(rawfil)) then begin
        print, 'GO_TOBAN: Sorry, this file does not exist ', rawfil
        print, 'Returning...', string(7b)
        error = 110
        return
    end
    nfile = file_info(rawfil, finfo)
    end else begin
    rawfil = ff(ifil)
    finfo = finfo(ifil)
    end
end
;
;---------------------------------------------------------------------------------
;
if (qreformat) then begin
    message,/info,'Checking that diskspace is available'        ;added 3-Dec-92
    qok = 0
    while (not qok) do begin
    free = diskfree(indir(iindir))      ;megabytes
    need = finfo(0).size/1e+6       ;converted to megabytes

    if ((iindir eq st_iindir) and (free lt 100)) then begin
        message = ['         WARNING   WARNING   WARNING ', $
            'Primary reformating disk ' + indir(iindir) + ' down to less than 100 megabytes']
        print, message
        if (keyword_set(mail) and (getenv('MAIL_TOBAN_WARN') ne '')) then mail, message, user='$MAIL_TOBAN_WARN'
    end

    if (need*1.25 gt free) then begin   ;allow 25% slop     ;since SFD files created
        iindir = iindir + 1
        if (iindir ge n_elements(indir)) then begin
        message = ['            URGENT URGENT URGENT         ', $
            'GO_TOBAN: None of the disks listed have space', $
            'Checked the following disks: ' + arr2str(indir), $
            '**** Clean up disk space ***']
        print, message
        if (keyword_set(mail) and (getenv('MAIL_TOBAN_ERROR') ne '')) then mail, message, user='$MAIL_TOBAN_ERROR'
        error = 150
        return
        end else begin
        message = ['GO_TOBAN: Not enough free space on ' + indir(iindir-1), $
                'Need:        ' + string(need) + ' megabytes', $
                'Amount Free: ' + string(free) + ' megabytes', $
                'Looking to use backup disk: ' + indir(iindir)]
        print, message
        if (keyword_set(mail) and (getenv('MAIL_TOBAN_ERROR') ne '')) then mail, message, user='$MAIL_TOBAN_ERROR'
        end
    end else begin
        qok = 1
    end
    end

    message,/info,'Starting reformatting phase...'
    reformat, rawfil, /instr, /att, dir=indir(iindir), fileid=fileid, year=year, /autodel
    prstr,replicate(' ',5)
    tbeep,10
    message,/info,'Reformatting complete, you may access the sfr and spr files.
    prstr,replicate(' ',5)

    rd_roadmap, concat_dir(indir(iindir), 'ada'+fileid), rmap
    nn = n_elements(rmap)
    delt = int2secarr(rmap(nn-1), rmap(0))

    message = ['GO_TOBAN: Reformatting finished', $
        'Input Raw Telemetry File: ' + rawfil, $
        'Output FileID: ' + fileid, $
        'Reformatted data covers from: ' + fmt_tim(rmap(0)) + ' to ' + fmt_tim(rmap(nn-1)), $
        '                               which is ' + string(delt/60/60) + ' hours' ]
    if (n_elements(srsget_duration) ne 0) then message = [message, '  ', srsget_duration]
    end_time = systime(1)
    run_time1 = (end_time-start_time1)/60.
    message = [message, '  ', 'SRSGET and REFORMAT stages took: ' + string(run_time1) + ' minutes to run']
    print, message

    if (keyword_set(mail) and (getenv('MAIL_TOBAN_STATUS') ne '')) then mail, message, user='$MAIL_TOBAN_STATUS'
end else begin
    break_file, files(nfil-1), dsk_log, dir, filnam, ext
    fileid = strmid(filnam, 3, 6) + ext
end
;
;---------------------------------------------------------------------------------
;
if (keyword_set(copy_cmd)) then spawn, copy_cmd     ;Now copy SRSPAS.NEW to SRSPAS.OLD
;
;---------------------------------------------------------------------------------
;
start_time2 = systime(1)
;
if (qreformat) then begin
    tim2orbit, fid2ex(fileid), wid=wid
    wid = wid(0)
end else begin
    tim2orbit, !stime, wid=wid0
    input, 'Enter the WeekID ', wid, wid0(0)
end
;
;pgs 990401files = file_list(indir, 'ada??????.????')
; GLS - 23-Mar-99 - added following line to ensure proper ordering and
;   uniqueness of files:
;pgs 990401 files = uniq(files,sort(files))
;
;pgs: scrutinize!
files_temp2 = file_list(indir, 'ada??????.????')
ss_temp2 = uniq(files_temp2,sort(files_temp2))
files=files_temp2(ss_temp2)
;
;

break_file, files, dsk_log, dir, filnam, ext
fids = strmid(filnam, 3, 6) + ext
tim2orbit, fid2ex(fids), wid=wids
ss = where(wids eq wid)
infil = files(ss)           ;has to be at least one
;
if (qobs) then begin
    message,/info,'Observing Log Creation...'
    mk_obs, infil, /append, /usefid
end
;
;---------------------------------------------------------------------------------
;
if (qevn) then begin
    message,/info,'Event File Creation...'
    mk_evn, week=wid
end
;
;---------------------------------------------------------------------------------
;
if (qpnt) then begin
    message,/info,'Pointing File Creation...'
    ;;mk_pnt, infil, /append                ;MDM removed 14-Dec-93
    mk_week_file, 'atr', infil
    mk_att, wid, infil_ada=infil                        ;MDM added 10-Oct-93
end
;
;---------------------------------------------------------------------------------
;
; ----------- define a list of remote hosts for background jobs ---------
nodelist='flare' + ['13','14','15']
; ------------------------------------------------------------------------

if (qsfd) then begin
    message,/info,'SFD File Creation...'
;       ****** use sfc temporary area for input sfc if defined *****
        chktemp = get_logenv('DIR_SXT_SFCTEMP')
        if chktemp ne '' then set_logenv,'DIR_SXT_SFC',chktemp
        sfr=newfiles(/sfr,last=5)          ; only consider last 5 SFRs
        sfd=str_replace(sfr,'sfr','sfd')
        need=where(1-file_exist(sfd),ncnt)
        if ncnt gt 0 then begin
           box_message,["Need to make SFDs: ",sfd(need)]
           for ifil=0,ncnt -1 do begin 
               mk_sfd,sfr(need(ifil)),outfil=sfd(need(ifil))                 
               mk_ssc, ifiles=sfr(need(ifil)), outdir=get_logenv('DIR_SITE_NEWDATA')
           endfor
           sxt_ssc2sss, newfiles(/ssc,last=ncnt), outdir=get_logenv('DIR_SITE_NEWDATA')
           go_sxt_sss2secondary
        endif else begin
           box_message,["all SFDs created"]
        endelse

    cmd = 'nohup ' + getenv('ys') + '/gen/script/idl_batch  mk_newdata ' + getenv('ys') + '/site/logs/mk_newdata.log &'
    
    node=get_host()

    rsh,node,cmd
    cmd = 'nohup ' + getenv('ys') + '/gen/script/idl_batch  go_mk_spd ' + getenv('ys') + '/site/logs/go_mk_spd.log DATE &'

    rsh,node,cmd

end
;
;---------------------------------------------------------------------------------

if (keyword_set(daily_forecast) and (n_elements(rmap) gt 1)) then begin
    cmd = 'nohup ' + getenv('ys') + '/gen/script/idl_batch /noftp  go_forecast ' + getenv('ys') + '/site/logs/forecast.log DATE &'
    rsh,node,cmd,/echo,user='software'
end


;---------------------------------------------------------------------------------
if (qsl) then begin
    infil3 = str_replace(infil, 'ada', 'sfr')
    sss = where(file_exist(infil3))
    if (sss(0) ne -1) then begin
    infil3 = infil3(sss)
    message,/info,'SLD File Creation...'
    mk_sl, infil3, prefix='sld'

    message,/info,'SLS File Creation...'
    mk_sl, infil3, prefix='sls'
    end
end
    
;---------------------------------------------------------------------------------
;
if (qccd_sunc and (n_elements(st_tim) gt 0)) then begin
   rd_week_file, anytim2ints(st_tim, off=-2*60.*60), !stime, 'ATT',att
   if data_chk(att,/struct) then begin
        cen = round(sxt_cen(att(n_elements(att)-1))/4.)
    cen(0) = 255-cen(0)
    dummy = ccd_sunc(update=cen)
   endif
end

;---------------------------------------------------------------------------------
if (qsft) then begin
    infil3 = str_replace(infil, 'ada', 'sfr')
    sss = where(file_exist(infil3))
    if (sss(0) ne -1) then begin
    infil3 = infil3(sss)
    message,/info,'SFT File Creation...'
    mk_sft, infil3
        mk_sfc,wid,/online,/noconfirm       ;slf added 9-nov-95        
    end
end

    
;---------------------------------------------------------------------------------
;
message = 'GO_TOBAN: Finished with all stages'
if (n_elements(srsget_duration) ne 0) then message = [message, '  ', srsget_duration]
end_time = systime(1)
run_time2 = (end_time-start_time2)/60.
message = [message, '  ', 'SRSGET and REFORMAT stages took: ' + string(run_time1) + ' minutes to run', $
            'MK_OBS, MK_EVN, MK_PNT and MK_SFD stages took: ' + string(run_time2) + ' minutes to run', $
            'for a total of ' + string(run_time1+run_time2) + ' minutes']
print, message
if (keyword_set(mail) and (getenv('MAIL_TOBAN_DEBUG') ne '')) then mail, message, user='$MAIL_TOBAN_DEBUG'
;
summary_line = 'SRSGET/REF took: ' + string(run_time1, format='(f7.2)') + ' min, and ' + $
        'OBS, EVN, PNT SFD, and SL stages took: ' + string(run_time2, format='(f7.2)') + ' min'

message,/info,'All stages completed succesfully...
if (keyword_set(journal)) then journal      ;close journal file
tbeep,2
end