pro check_ql_after_lz, infil, out, status, init_out=init_out
;+
;NAME:
;	check_ql_after_lz
;PURPOSE:
;	To check to see if an LZ file exists when an QL file comes
;	in. If there is an LZ file, is there new data in the QL file.
;INPUT:
;	a single DPS file name
;OUTPUT:
;	output	- A string array summary
;	status	>= 0 means run the reslotter, <0 means LZ is online
;		=    0 if there is no LZ file yet
;		=   -1 if there is an LZ file, but no new data
;		=  -10 if there is an LZ file, but only a little new data
;		= -100 if there is an LZ file and enough new data to warrent a new LZ file
;		=    2 if there is no file online at all (no slot or LZ file)
;		=    3 if it's APID 2 or 4 (can't be handled by RD_HK right now)
;		=    4 if the input file does not exist
;HISTORY:
;	Written 10-Jun-98 by M.Morrison
;	11-Jun-98 (MDM) - Added check for input file not existing
;	11-Jun-98 (MDM) - Removed bypass for apid 2 and 4
;_
;
common check_ql_after_lz_blk1, last_remake
;
if (n_elements(last_remake) eq 0) then last_remake = 'xx'
;
if (keyword_set(init_out)) then begin
    out = ['CHECK_LZ_AFTER_LZ  Ver 1.0  Program Run: ' + !stime,' ', $
	'           QL File                          #QL   #LZ  #New  #Min', $
	'                                           Pkts  Pkts  Pkts']
	;TRACE_G010_QL_1998-05-04T09-56-09Z_V03      404  3642     0  0.00
    prstr, out
end
;
if (n_elements(out) eq 0) then out = ''
break_file, infil, dsk_log, dir, filnam, ext
;
;
ss = where( (strpos(infil, 'G003') eq -1) and (strpos(infil, 'G073') eq -1) $
	and (strpos(infil, 'G014') eq -1) and (strpos(infil, 'G029') eq -1) )
if (ss(0) eq -1) then begin
    status = 3
    out0 = string(filnam, 'APID cannot be handled by CHECK_QL_AFTER_LZ right now', format='(a, 3x, a, 1x, a)')
    out = [out, out0]
    return
end
;
if (not file_exist(infil)) then begin
    status = 4
    out0 = string(filnam, 'Input file does not exist', format='(a, 3x, a, 1x, a)')
    out = [out, out0]
    return
end
;
;;print, 'Now processing: ' + infil
;
;
tarr = dps_fname2time(infil, xx_outfilnam, fid, xx_dps, apid)
ext = ['.hk', '.sc'] + string(apid and 'ff'x, format='(z2.2)')
fid2 = strmid(fid, 2, 6)
ff = file_list('$GSE_GENTM_LOG_DIR', fid2+ext)
hkfil = ff(0)
if (hkfil eq '') then begin
    out0 = string(filnam, 'Could not find input file: ', fid2+ext, format='(a, 3x, a, 1x, a)')
    out = [out, out0]
    status = 2
    return
end
break_file, hkfil, dsk_log2, dir2, filnam2, ext2
;
qremake_lz = 0
mapfil = hkfil+'x'
if (file_exist(mapfil)) then begin
    status = -1
    lzmap = rd_hk(hkfil, /map)
    pkt_rate = min(deriv_arr(lzmap))
    tmpfil = concat_dir( getenv('HOME'), 'temp' + ext2)
    spawn, ['ln', '-s', infil, tmpfil], /noshell
    rec = rd_hk(tmpfil, 0, 9999999)
    spawn, ['rm', '-f', tmpfil], /noshell
    ;
    nlz = n_elements(lzmap)
    nql = n_elements(rec)
    ss = where_arr(lzmap, rec.time, nss)	;how many ql times are in lz file
    nnew = nql - nss
    nnew_min = nnew * pkt_rate/60.
    extra = ''
    if (nnew gt 0) then status = -10
    ;;if (nnew gt 1000) or (abs(pkt_rate) gt 10) then begin
    if (nnew/float(nlz) gt 0.10) or (abs(pkt_rate) gt 10) then begin
	status = -100
	if (fid2 ne last_remake) then begin
	    extra = ' Remake:' + fid2
	    last_remake = fid2
	end
    end
    out0 = string(filnam, nql, nlz, nnew, nnew_min, extra, $
				format='(a, 3x, 3i6, f6.1, a)')
end else begin
    out0 = string(filnam, 'No LZ file online for: ', fid2+ext, format='(a, 3x, a, 1x, a)')
    status = 0
end
out = [out, out0]
print, out0
;
end
;-------------------------------------------------------
pro go_check_ql_after_lz
;
;
;ff00 = file_list('/disk6/people/dps', '*.DAT1')
;ff00 = file_list('/disk6/people/dps', 'TRACE_G078*.DAT1')
dirs = '/disk6/people/dps'
dirs = ['/disk6/people/dps', '/disk6/people/dps/to_be_deleted2']
ff00 = file_list(dirs, '*.DAT1')
;ss = where( (strpos(ff00, 'G002') eq -1) and (strpos(ff00, 'G004') eq -1) )
ss = where( (strpos(ff00, 'G002') eq -1) and (strpos(ff00, 'G004') eq -1) and (strpos(ff00, 'G011') eq -1) )
ss = where( (strpos(ff00, 'G002') eq -1) and (strpos(ff00, 'G004') eq -1) $
	and (strpos(ff00, 'G011') eq -1) and (strpos(ff00, 'G003') eq -1) $
	and (strpos(ff00, 'G073') eq -1)  $
	and (strpos(ff00, 'G014') eq -1) and (strpos(ff00, 'G029') eq -1) )
ff0 = ff00(ss)
break_file, ff0, dsk_log, dir, filnam, ext
p = strpos(filnam(0), '_QL_')
ff0 = ff0( sort(strmid(filnam, p, 999)))

for i=0,n_elements(ff0)-1 do check_ql_after_lz, ff0(i), out, init_out=(i eq 0)
end