pro mk_hk_map, infil, all_mdi=all_mdi, all_sc=all_sc
;
;HISTORY:
;	Written 23-May-94 by M.Morrison
;	26-May-94 (MDM) - Modified to read .mdihk and NOT to have
;			  a header at the beginning of that file
;       31-May-94 (MDM) - Modified to figure the number of records properly
;                         when the input file is .schk
;
swap_os=['vms','ultrix','OSF']                  ; list of swap machines
chk=where(!version.os eq swap_os,nscount)       ; current OS in noswap list?
qswap = nscount ne 0                            ; assign boolean
;
if (keyword_set(all_mdi) or keyword_set(all_sc)) then begin
    mdirs = str2arr( getenv('MDI_HK_DIRS') )
    dirs = file_list(mdirs, '', /cd)
    if (keyword_set(all_sc)) then infil = file_list(dirs, '*.schk') $
			else infil = file_list(dirs, '*.mdihk')
end
;
for ifil=0,n_elements(infil)-1 do begin
    infil0 = infil(ifil)
    print, 'Now processing ', infil0
    siz = file_stat(infil0, /size)
    nrec = siz / 356
    if (strpos(infil0, '.schk') ne -1) then nrec = siz / 32
    if (siz le 28) then begin
	file_delete, infil0
	file_delete, infil0 + 'x'
    end else begin
	head = rd_hk(/struct_head)	;get the structure
	head.start_time = 999999999
	head.progverno = 2.00 * 1000
	;
	outfil = infil0 + 'x'
	openw, lun, outfil, /get_lun
	writeu, lun, head
	;
	step = 100
	for i=0L,nrec-1,step do begin
	    rec = rd_hk(infil0, i, (i+step-1)<(nrec-1) )
	    out = rec.time
	    head.start_time = min([head.start_time, out])
	    head.end_time = max([head.start_time, out])
	    head.nrec = head.nrec + n_elements(out)
	    if (qswap) then dec2sun, out	& writeu, lun, out
	end

	point_lun, lun, 0
	if (qswap) then dec2sun, head	& writeu, lun, head
	free_lun, lun
    end
end
;
end
