pro hist_count, list, default=default , outfil=outfil
;
;+ 
;NAME: 
;	hist_count
;PURPOSE: 
;	To search the telemetry command history for certain strings and make
;	a table of how often they are found for each month
;SAMPLE CALLING SEQUENCE:
;	hist_count
;	hist_count, ['mdipict', 'mdiltc', 'mdifocus'] 
;OPTIONAL INPUT: 
;	list - An array holding the list of words to search for 
;OPTIONAL KEYWORD INPUT:
;	default - If set, use a long list of routines something like:
;HISTORY:
;	Written Feb-94 by M.Morrison
;V1.1	25-Apr-94 (MDM) - Made it a procedure
;-
;
progver = 'HIST_COUNT  Ver 1.1

if (keyword_set(default)) then begin
    list = ['mdipict', 'mdiltc', 'mdifocus', 'mdiiss', 'mdidust', 'mdilaser', $
	'mdidtune', 'big', 'supersun', 'vfdlscan', 'mtmtune', 'maplaser', 'mapgrad', $
	'mdiprot', 'mdimrot', 'mdipdist', $
	'" mdion "', 'lfdclose', 'mbfdbc', 'lfdopen', 'mbfd1o']
    outfil = concat_dir(getenv('MDI_CAL_TXT'), 'cmd_hist_count.txt')
end
if (n_elements(list) eq 0) then begin
    print, 'Enter the list of items to search for separated by ","'
    lin = 'mdipict, mdiltc, mdifocus'
    input, 'Search list: ', list, lin
    list = str2arr(list, delim=',')
end
;
if (n_elements(outfil) eq 0) then outfil = 'hist_count.txt'
infil = file_list(getenv('MDI_CAL_TXT'), 'cmd_hist.9*')
n = n_elements(infil)
;
spawn, 'head -1000 ' + infil(0), result
i = 4		;skip header
while (strpos(result(i), '-9') eq -1) do i=i+1
sttim = strmid(result(i), strpos(result(i), '-9')-6, 18)
;
spawn, 'tail -1000 ' + infil(n-1), result
i = n_elements(result)-1
while (strpos(result(i), '-9') eq -1) do i=i-1
entim = strmid(result(i), strpos(result(i), '-9')-6, 18)
;
print, 'Saving results to: ', outfil
file_delete, outfil
openw, lun, outfil, /get_lun
printf, lun, progver + '    Program Run: ' + !stime
printf, lun, ' '
printf, lun, 'Input file spans: ' + sttim + ' to ' + entim
printf, lun, ' '
;
break_file, infil, dsk_log, dir, filnam, ext
fmt0 = '(1x, 15x, ' + strtrim(n,2) + 'a6, a8)'
fmt  = '(1x, a15, ' + strtrim(n,2) + 'i6, i8)'
tit = string(strmid(ext, 1, 4), '  Total', format=fmt0)
print, tit
printf, lun, tit
printf, lun, ' '
;
for i=0,n_elements(list)-1 do begin
    out = intarr(n)
    for ifil=0,n-1 do begin	;for each file
	cmd = 'grep ' + list(i) + ' ' + infil(ifil) + ' | wc'
	spawn, cmd, result
	arr = str2arr(strtrim(strcompress(result(0)),2), delim=' ')
	out(ifil) = arr(0)
    end
    str = string(list(i)+'                ', out, total(out), format=fmt)
    print, str
    printf, lun, str
end
;
free_lun, lun
end
