pro reslot, infil, nn, toff0, outfilnam, qwrite=qwrite, toff2=toff2, day=day, qstop=qstop, strec=strec, qcheck=qcheck
;
;
if (n_elements(qcheck) eq 0) then stop, 'qcheck is undefined'
;
break_file, infil, dsk_log, dir00, filnam, ext
if (strpos(infil, 'TRACE_G0') ne -1) then begin
    case strmid(filnam, 6, 4) of
	'G010': nn = 154
	'G011': nn = 138
	'G077': nn = 334
	'G078': nn =  64
    endcase
end
;
;infil = 'bad_pkts_970307.hk4e'	& nn=392	& outfil = '970307.hk4e'	& toff = 0
;infil = 'bad_pkts_970307.hk4e'	& nn=334	& outfil = '970307.hk4e'	& toff = 0
;
print, infil
toff = toff0	;save copy of it
if (n_elements(toff2) eq 0) then toff2 = toff
qstop2 = 1
qstop3 = 1
out0 = {ccsds: bytarr(6), time: long(0), ccsds2: bytarr(2), $
        hk: bytarr(nn)}
;
iday = int2secarr(day, '24-May-68')
;
break_file, infil, dsk_log, dir00, filnam, ext
;outfil = '/td02/log/schk/' + strmid(ext, 6, 6) + strmid(ext, 0, 5)
outfil = concat_dir(getenv('MDI_GENTM_LOG_DIR'), strmid(ext, 6, 6) + strmid(ext, 0, 5))
if (keyword_set(outfilnam)) then outfil = concat_dir(getenv('MDI_GENTM_LOG_DIR'), outfilnam)
if (strpos(infil, 'TRACE_G0') ne -1) then begin
    fid = strmid( ex2fid(anytim2ex(day)), 0, 6)
    case strmid(filnam, 6, 4) of
	'G010': outfil = concat_dir(getenv('MDI_GENTM_LOG_DIR'), fid + '.sc0a')
	'G011': outfil = concat_dir(getenv('MDI_GENTM_LOG_DIR'), fid + '.sc0b')
	'G077': outfil = concat_dir(getenv('MDI_GENTM_LOG_DIR'), fid + '.hk4d')
	'G078': outfil = concat_dir(getenv('MDI_GENTM_LOG_DIR'), fid + '.hk4e')
    endcase
end
if (not file_exist(outfil)) then stop, 'Output File: ' + outfil, ' does not exist
;
closeall
openr, lun1, infil, /get_lun
openu, lun2, outfil, /get_lun
rd_pkt_head, lun2, head, off_head, info
;
offset = 0
if (strpos(infil, 'TRACE_G0') ne -1) then offset = 48
point_lun, lun1, offset
if (keyword_set(strec)) then point_lun, lun1, strec*get_nbytes(out0) + offset
;
filsiz = fstat(lun1)
filsiz = filsiz.size
;
last_recn = 0
ii = 0L
if (keyword_set(strec)) then ii = long(strec)
;while (not eof(lun1)) do begin
inpos = long(offset)
while (inpos+nn+12 le filsiz) do begin	;while 
    readu, lun1, out0

    ;orig_time = out0.time
    orig_time = double(out0.time) + unsign(fix(out0.ccsds2, 0),16)/65536D	;MDM added fractional sec 27-May-97
    new_time = orig_time + toff
    ;;out0.time = out0.time + toff
    recn = round(((new_time) mod 86400) / info.time_step)
    ;
    if (new_time lt iday) or (new_time gt iday+86400) then begin
        new_time = orig_time + toff2
        recn = round(((new_time) mod 86400) / info.time_step)
    end
    ;
    qwrite2 = 1
    if (new_time lt iday) or (new_time gt iday+86400) then begin
	print, 'Slot outside of file.  Slot # = ', recn
	;;qstop2 = 0	;only stop the first time?
	qwrite2 = 0
	if (keyword_set(qstop2)) then stop
    end

    if (qwrite2) then begin
	skip = recn * get_nbytes(out0) + off_head
	out0.time = new_time
	print, fmt_tim(anytim2ints('24-May-68', off=double(orig_time))), '  ', $
			fmt_tim(anytim2ints('24-May-68', off=double(new_time))), recn, skip, qwrite

	if (recn lt last_recn) then begin
	    print, 'Telemetry moving backwards in the file (use qstop3=0 )'
	    if (keyword_set(qstop3)) then stop
	end
	if (recn-last_recn gt 2) and (last_recn ne 0) then begin
	    print, 'Moved more than two slots'
	    if (keyword_set(qstop)) then stop
	end
	last_recn = recn

	qwrite4 = 1
	if (keyword_set(qcheck)) then begin
	    point_lun, lun2, skip
	    chk = out0
	    readu, lun2, chk
	    if (chk.time ne 0) then begin
		if (str_diff(out0,chk) ne 0) then begin
		    print, 'Data already in the slot and it doesnt match'
		    ;if (keyword_set(qstop)) then stop
		    stop
		    qwrite4 = 0
		end else begin
		    print, 'Data already in the slot, but matches
		end
	    end
	end

	point_lun, lun2, skip
	if (keyword_set(qwrite) and qwrite4) then writeu, lun2, out0
    end
    ii = ii + 1
    point_lun, -1*lun1, inpos
end

;
free_lun, lun1
free_lun, lun2
end
;--------------------------------------------------
pro rezero_file
;
;
;
outfil = '/td02/log/schk/970418.hk4d'	& nn=64
if (not file_exist(outfil)) then stop
;
out0 = {ccsds: bytarr(6), time: long(0), ccsds2: bytarr(2), $
        hk: bytarr(nn)}
;
;
closeall
openu, lun2, outfil, /get_lun
rd_pkt_head, lun2, head, off_head, info
;
for recn=52000L,64000 do begin
    skip = recn * get_nbytes(out0) + off_head
    point_lun, lun2, skip
    writeu, lun2, out0
end

;
free_lun, lun2
end
;--------------------------------------------------
;
;+
;NAME:
;	reslot
;PURPOSE:
;	Reslot the bad telemetry file
;HISTORY:
;	Written Apr-97 by M.Morrison
;	21-Apr-97 (MDM) - Modified to not overwrite the packet time in the packet
;			  (just derive the slot number) -- that mod was then removed
;			- Modified to have two offsets possible
;			- Added DAY check (that the derived time is within the day'
;			- Put in checks for moving backwards in time 
;			  (funny telemetry stream 18-Apr)
;			  (two blocks of time where it jumps backwards and reruns a time span)
;	12-May-97 (MDM) - Added MINREC for ignoring everything before MINREC
;	19-May-97 (MDM) - DPS level zero slotting
;			- Changed format of "day" variable
;	30-May-97 (MDM) - 29-May reslot
;	 6-Jun-97 (MDM) - Reslot the remade junk file directly from ChrisH
;-
;
if (n_elements(qwrite) eq 0) then qwrite = 0	;  qwrite = 1  & qstop = 0
if (n_elements(qstop) eq 0) then qstop = 1
if (n_elements(qcheck) eq 0) then qcheck = 1
;
;Mike, do something like "grep BAD /td01/log/history/970529.hst"
;I run it, see some diagnostics, and if everything looks like what I expect,
;then I do	qwrite = 1  & qstop = 0
;and re-run it to actually write to the slotted file.  The problem is
;that sometimes the time changes several times within on bad packets file.
;The line below gives the packet time in date/time format. "RealTime" is
;from the history file log time.
;	
;IDL> print,fmt_tim(anytim2ints('24-May-68', off=double('36b1560c'x)))	;packet time
;
;---------------------------------------------
;
;.		    RealTime             PacketTime
toff  = int2secarr('1-jan-70', '1-Jan-58')
toff = 0
day = '21-May-97'
dir = '/hosts/michelson/data0/morrison/junk/'
;reslot, dir+'trace052197n.hk4e', 334, toff, '970521.hk4e', qwrite=qwrite, day=day, toff2=toff2, qstop=qstop, qcheck=qcheck
;reslot, dir+'trace052197n.hk4d',  64, toff, '970521.hk4d', qwrite=qwrite, day=day, toff2=toff2, qstop=qstop, qcheck=qcheck
;reslot, dir+'trace052197n.sc0a', 154, toff, '970521.sc0a', strec=1451, qwrite=qwrite, day=day, toff2=toff2, qstop=qstop, qcheck=qcheck
 reslot, dir+'trace052197n.sc0b', 138, toff, '970521.sc0b', qwrite=qwrite, day=day, toff2=toff2, qstop=qstop, qcheck=qcheck

stop, 'DONE.....' ;---------------------------------------------------
;
;.		    RealTime             PacketTime
toff  = int2secarr('29-May-97 20:43:42', '15-DEC-97  03:13:32')		;0x379a80dc
day = '29-May-97'
 reslot, '/td01/tmp/bad_pkts/bad_pkts.sc0b.970529', 138, toff, qwrite=qwrite, day=day, toff2=toff2, qstop=qstop, strec=961*5
 reslot, '/td01/tmp/bad_pkts/bad_pkts.sc0a.970529', 154, toff, qwrite=qwrite, day=day, toff2=toff2, qstop=qstop, strec=960
 reslot, '/td01/tmp/bad_pkts/bad_pkts.hk4e.970529', 334, toff, qwrite=qwrite, day=day, toff2=toff2, qstop=qstop
 reslot, '/td01/tmp/bad_pkts/bad_pkts.hk4d.970529',  64, toff, qwrite=qwrite, day=day, toff2=toff2, qstop=qstop

stop ;---------------------------------------------------
;.		    RealTime             PacketTime
toff  = 0
day = '21-May-97'
dir = '/usr3/ftp/pub/trace_IandT/dps/ACOUSTIC
ff = file_list(dir, 'TRACE_G0*1997-05-21*.DAT1')
for i=0,n_elements(ff)-1 do reslot, ff(i), xxx, toff, qwrite=qwrite, day=day,  qstop=qstop, qcheck=qcheck
;
stop ;---------------------------------------------------
;
;.		    RealTime             PacketTime
toff  = int2secarr('14-May-97 20:08:49', '15-Dec-97 08:36:23') 
day = '14-May-97'
dir = '/usr3/ftp/pub/trace_IandT/dps/SPACECRAFT_051497_TEST/
;reslot, dir+'TRACE_G077_LZ_1997-12-15T03-04-49Z_V01.DAT1',  64, toff, qwrite=qwrite, day=day,  qstop=qstop
;reslot, dir+'TRACE_G078_LZ_1997-12-15T03-04-49Z_V01.DAT1', 334, toff, qwrite=qwrite, day=day,  qstop=qstop
;reslot, dir+'TRACE_G010_LZ_1997-12-14T21-45-10Z_V01.DAT1', 154, toff, qwrite=qwrite, day=day,  qstop=qstop
 reslot, dir+'TRACE_G011_LZ_1997-12-14T21-45-07Z_V01.DAT1', 138, toff, qwrite=qwrite, day=day,  qstop=qstop

stop ;---------------------------------------------------
;
;.		    RealTime             PacketTime
toff  = int2secarr('8-May-97 13:07:28', '21-JUN-97  00:02:13') 
day = int2secarr('8-May-97', '24-May-68')
 reslot, '/td01/tmp/bad_pkts/bad_pkts.sc0b.970508', 138, toff, qwrite=qwrite, day=day, toff2=toff2, qstop=qstop, strec=961*5
 reslot, '/td01/tmp/bad_pkts/bad_pkts.sc0a.970508', 154, toff, qwrite=qwrite, day=day, toff2=toff2, qstop=qstop, strec=960
 reslot, '/td01/tmp/bad_pkts/bad_pkts.hk4e.970508', 334, toff, qwrite=qwrite, day=day, toff2=toff2, qstop=qstop
 reslot, '/td01/tmp/bad_pkts/bad_pkts.hk4d.970508',  64, toff, qwrite=qwrite, day=day, toff2=toff2, qstop=qstop


stop ;---------------------------------------------------
;.		    RealTime             PacketTime
toff  = int2secarr('7-May-97 20:20:36', '21-JUN-97  06:32:44') ;from end of history file
toff2 = int2secarr('7-May-97 18:34:50', '5-MAY-97  18:34:57')	;from start of history file
day = int2secarr('7-May-97', '24-May-68')
 reslot, '/td01/tmp/bad_pkts/bad_pkts.sc0a.970507', 154, toff, qwrite=qwrite, day=day, toff2=toff2, qstop=qstop
 reslot, '/td01/tmp/bad_pkts/bad_pkts.sc0b.970507', 138, toff, qwrite=qwrite, day=day, toff2=toff2, qstop=qstop
 reslot, '/td01/tmp/bad_pkts/bad_pkts.hk4e.970507', 334, toff, qwrite=qwrite, day=day, toff2=toff2, qstop=qstop
 reslot, '/td01/tmp/bad_pkts/bad_pkts.hk4d.970507',  64, toff, qwrite=qwrite, day=day, toff2=toff2, qstop=qstop


stop ;---------------------------------------------------
;qwrite = 1
toff  = int2secarr('18-apr-97 14:35:09', '17-APR-97  15:22:24') ;from beginning of history file
toff2 = int2secarr('18-apr-97 21:41:16', '15-DEC-97  01:56:52')	;from end of history file
;	Data from 14:40:28 to 16:05:05   
;             and 16:14    to 17:44    with a gap
;             and 19:45:49 to 21:39    with a gap
;
toff3 = int2secarr('18-Apr-97 16:08:59', '17-APR-97  15:21:58')	;middle of log 365c2096
toff4 = int2secarr('18-Apr-97 17:10:58', '17-APR-97  16:23:57')	;middle of log 365c2f1d
;
;17-APR-97  16:53:09  18-APR-97  16:05:54       11591     1924263
;17-APR-97  15:22:02  18-APR-97  14:34:47       10497     1742659
;
;17-APR-97  16:57:17  18-APR-97  16:10:02       11640     1932397
;17-APR-97  15:22:23  18-APR-97  14:35:08       10502     1743489
;
;17-APR-97  15:29:53  18-APR-97  14:42:38       10592     1758429
;14-DEC-97  23:52:22  18-APR-97  19:36:46       14121     2344243
;
day = int2secarr('18-Apr-97', '24-May-68')
 reslot, '/td01/tmp/bad_pkts/bad_pkts.sc0a.970418', 154, toff, qwrite=qwrite, day=day, toff2=toff2
 reslot, '/td01/tmp/bad_pkts/bad_pkts.sc0b.970418', 138, toff, qwrite=qwrite, day=day, toff2=toff2
 reslot, '/td01/tmp/bad_pkts/bad_pkts.hk4e.970418', 334, toff, qwrite=qwrite, day=day, toff2=toff2
 reslot, '/td01/tmp/bad_pkts/bad_pkts.hk4d.970418',  64, toff, qwrite=qwrite, day=day, toff2=toff2

;---------------------------------
stop
toff = int2secarr('10-Apr-97 17:21:47', '15-DEC-97  02:53:47')	;from history file
;	Data from 14:21 to 17:18
;

 reslot, '/td01/tmp/bad_pkts/bad_pkts.hk4e.970410', 334, toff, qwrite=qwrite
 reslot, '/td01/tmp/bad_pkts/bad_pkts.hk4d.970410',  64, toff, qwrite=qwrite
 reslot, '/td01/tmp/bad_pkts/bad_pkts.sc0a.970410', 154, toff, qwrite=qwrite
 reslot, '/td01/tmp/bad_pkts/bad_pkts.sc0b.970410', 138, toff, qwrite=qwrite

stop
qwrite = 1
toff = 0
toff = int2secarr('2-apr-97 23:59:59', '15-DEC-97  03:39:22')	;last packet time in the 2-Apr file
;
 reslot, '/td01/tmp/bad_pkts/bad_pkts.hk4e.970402', 334, toff, qwrite=qwrite
 reslot, '/td01/tmp/bad_pkts/bad_pkts.hk4d.970402',  64, toff, qwrite=qwrite
 reslot, '/td01/tmp/bad_pkts/bad_pkts.sc0a.970402', 154, toff, qwrite=qwrite
 reslot, '/td01/tmp/bad_pkts/bad_pkts.sc0b.970402', 138, toff, qwrite=qwrite

 reslot, '/td01/tmp/bad_pkts/bad_pkts.hk4e.970403', 334, toff, qwrite=qwrite
 reslot, '/td01/tmp/bad_pkts/bad_pkts.hk4d.970403',  64, toff, qwrite=qwrite
 reslot, '/td01/tmp/bad_pkts/bad_pkts.sc0a.970403', 154, toff, qwrite=qwrite
 reslot, '/td01/tmp/bad_pkts/bad_pkts.sc0b.970403', 138, toff, qwrite=qwrite
;

end