@mon_sci5k
pro redo_mon_sci5k, sttim, entim, qdebug=qdebug, infil=infil
;+
;NAME:
;	redo_mon_sci5k
;PURPOSE:
;	To give a summary of the SCI5K contents
;SAMPLE CALLING SEQUENCE:
;	redo_mon_sci5k, '24-nov-95', '24-nov-95 12:00'
;	redo_mon_sci5k, '14-Jun-95 12:00', '14-Jun-95 23:00'
;	redo_mon_sci5k, infil='/md83/ecs/done_ecs/MDISC_960113_235115.QKL'
;RESTRICTION:
;	Time cannot span over a day boundary
;HISTORY:
;	Written 22-Nov-95 by M.Morrison
;	19-Dec-95 (MDM) - Corrected fundamental error which caussed the
;			  data to be offset from what it should be
;	16-Jan-96 (MDM) - Added INFIL option
;			- Changed output file to be the time of the first
;			  packet, not the time that the routine was run.
;-
;
;
if (keyword_set(infil)) then begin
    break_file, infil, dsk_log, dir, filnam, ext
    fid = filnam + ext
    strec = 0
    enrec = 200000
    sttim = filnam+ext
    entim = filnam+ext
end else begin
    if (n_elements(sttim) eq 0) or (n_elements(entim) eq 0) then begin
        print, 'Start and end time must be specified'
        return
    end
    ;
    if (gt_day(sttim) ne gt_day(entim)) then begin
        print, 'Temporary restriction is that the start and end days must
        print, 'be the same
        return
    end
    ;
    sttim = fmt_tim(sttim)
    entim = fmt_tim(entim)
    tarr = anytim2ex(sttim)
    fid = ex2fid(tarr)
    strec = gt_time(sttim)/1000.  *24/15.           ;24 packets per major frame (15sec)
    enrec = gt_time(entim)/1000.  *24/15.           ;24 packets per major frame (15sec)
    ;
    strec = strec > 0 < 138239
    enrec = enrec > 0 < 138239
    infil = concat_dir(getenv('MDI_SCI5K_DIR'), strmid(fid, 0, 6) + '.sci')
end
;
if (keyword_set(qdebug)) then print, 'Reading ', infil, ' records ', strec, enrec
;
out = rd_hk(infil, strec, enrec, /new)
if (data_type(out) ne 8) then begin
    message, 'SCI5K read resulted in no data at all.', /info
    return
end
out.time = out.time + int2secarr('1-jan-70', '1-jan-58')	;re-adjust back to TAI time
;
print, 'Found ', n_elements(out), ' records for the time span'
print, 'from ', sttim, ' to ', entim
;
;--------------------------------------
;
if (n_elements(fill_cnt) eq 0) then fill_cnt = 100
;
none_cnt = 0L
cnt_minutes = 0L
;tmbuf = timstr2ex(!stime)
tmbuf = anytim2ints('1-Jan-58', off=out(0).time*1D)
tmbuf = anytim2ex(tmbuf)
tmstr = string(format='(3i2.2,"_",3i2.2)',tmbuf(6),tmbuf(5),tmbuf(4),tmbuf(0),tmbuf(1),tmbuf(2))

mon_fname = string(format='((A),"/", (A), ".redo_mon")', getenv("MDI_MON5K_DIR"), tmstr)
roll_fname= string(format='((A),"/", (A), ".redo_roll")', getenv("MDI_MON5K_DIR"), tmstr)

openw, lun, mon_fname, /get_lun
printf, lun, 'Input file: ' + infil
printf, lun, 'Records: ', strec, enrec

openw,rlun,roll_fname,/get_lun
printf,rlun,"Input file: '+infil
printf,rlun,'Records: ',strec,enrec

for i=0L,n_elements(out)-1 do begin
    out0 = out(i)
    ;b1 = [0, 0, 0, fix(out0.ccsds,0,3), fix(out0.time,0,2), fix(out0.ccsds2,0), fix(out0.hk,0,386/2)]
    b1 = [fix(out0.ccsds,0,3), fix(out0.time,0,2), fix(out0.ccsds2,0), fix(out0.hk,0,386/2)]
    decode_sci5k, b1, lun, i, none_cnt, cnt_minutes, simple=simple, fill_cnt=fill_cnt, roll_lun = roll_lun
    none_cnt = none_cnt + 1
    if ((none_cnt mod fill_cnt+1) eq fill_cnt) then print, 'No data segments for last ', none_cnt+1, ' packets'
    if ((none_cnt mod fill_cnt+1) eq fill_cnt) then printf, lun, 'No data segments for last ', none_cnt+1, 'packets'
end
;
close, lun
end
