pro rd_egse_hk_txt, tlm, alg, dsc, cmd, infil=infil, qdebug=qdebug, txt=txt
;
;
;
;+
;NAME:
;	rd_egse_hk_txt
;PURPOSE:
;	Read the EGSE database definition files (ASCII) into structures
;HISTORY:
;	Written 1998 by M.Morrison
;     26-March-1998 - S.L.Freeland - look for SSW version of dbase file  
;     12-May-  1998 - (FZ)         - Changed loop variables to long
;	16-Jun-98 (MDM) - Corrected DSC hi subscripting (was getting lo)
;-
;  
if (keyword_set(qdebug)) then idl_speed, 0
;
if (n_elements(infil) eq 0) then begin
    if (getenv('GSE_CONFIG_TYPE') eq 'TRACE') then infil = concat_dir(getenv('db'), 'trace_test.db') $
				   		else infil = concat_dir(getenv('db'), 'mdi.db')
end
;
sswversion=concat_dir('TRACE_EGSE_DBASE','trace_test.db')

if (not file_exist(infil)) then begin
    if file_exist(sswversion) then infil=sswversion else begin 
       print, 'RD_EGSE_HK_TXT: Cannot find file: ' + infil
       return
    endelse
end
;
txt = rd_tfile(infil, 20, delim=',')
;
;------------------------------------
nmax = 10
dsc = {name: '', descr: '', $		;string table description lookup
	low: 0L, hi: 0L, str: ''}
ss = where( txt(0,*) eq 'DSC', nss)
if (nss ne 0) then begin
    dsc = replicate(dsc, nss)
    dsc.name = reform(txt(1,ss))
    dsc.descr = reform(txt(8,ss))
    dsc.low = long(reform(txt(4,ss)))
    dsc.hi = long(reform(txt(5,ss)))
    dsc.str = reform(txt(2,ss))
end
;
;------------------------------------
alg = {name: '', coeff: dblarr(8), descr: ''}	;analog coefficient conversion table
ss = where( txt(0,*) eq 'ALG', nss)
if (nss ne 0) then begin
    alg = replicate(alg, nss)
    alg.name = reform(txt(1,ss))
    alg.coeff = double(txt(3:10,ss))
    alg.descr = reform(txt(11,ss))
end
;
;------------------------------------
nmaps = 3
tlm = {name: '', group: '', type: '', descr: '', stbit: 0L, nbit: 0L, $
	conv: '', unit: '', conv_code: '.', $
	apid: strarr(nmaps), stbyte: lonarr(nmaps)}
ss = where( txt(0,*) eq 'PKT', nss)
if (nss ne 0) then begin
    tlm = replicate(tlm, nss)
    tlm.name 	= strlowcase(reform(txt(2,ss)))		;1 in TLM
    tlm.group 	= reform(txt(5,ss))			;4 in TLM
    tlm.type 	= reform(txt(6,ss))			;5 in TLM
    tlm.descr 	= reform(txt(12,ss))			;
    tlm.apid(0) = reform(txt(1,ss))
    tlm.stbyte(0) = long(reform(txt(7,ss)))		;6 in TLM	
    tlm.stbit 	= reform(txt(8,ss))
    tlm.nbit  	= reform(txt(9,ss))
    ;
    ss33 = where( is_number( reform(txt(1,ss)) ), nss33)			;Telemetry APID
    if (nss33 ne 0) then tlm(ss33).stbyte(0) = tlm(ss33).stbyte(0) - 12		;-12 because of CCSDS header?
end

ss = where( txt(0,*) eq 'TLM', nss)
for i=0L,nss-1 do begin
    ii = where(tlm.name eq strlowcase(txt(1,ss(i))), nss2)
    if (nss2 eq 1) then begin
	tlm(ii).unit 	= txt(7,ss(i))
	tlm(ii).conv	= txt(11,ss(i))
	dummy = where(alg.name eq txt(11,ss(i)), ndummy)  	& if (ndummy ne 0) then tlm(ii).conv_code = 'A'
	dummy = where(dsc.name eq txt(11,ss(i)), ndummy)  	& if (ndummy ne 0) then tlm(ii).conv_code = 'S'
	if (tlm(ii).descr eq '') then tlm(ii).descr = txt(14,ss(i))
    end
end
;
ss = where( (txt(0,*) eq 'MAP') and (txt(1,*) eq 'DPH'), nss)
for i=0L,nss-1 do begin
    lin = txt(*,ss(i))
    sou_apid	= lin(4)
    sou_st 	= long(lin(5))
    n 		= long(lin(6))
    ss2 = where( (tlm.apid(0) eq sou_apid) and (tlm.stbyte(0) ge sou_st) and (tlm.stbyte(0) lt sou_st+n), nss2)
    if (nss2 ne 0) then begin
	tlm(ss2).apid(1) 	= lin(1)
	tlm(ss2).stbyte(1) 	= tlm(ss2).stbyte(0) - sou_st + long(lin(2))
    end
end
;
if (keyword_set(qdebug)) then idl_speed, 1
end
;-------------------------------------------------------------------
pro go_rd_egse_hk_txt
;
rd_egse_hk_txt, tlm, alg, dsc, cmd, /qdebug
pause
;
n = n_elements(tlm)
ss = indgen(n)
if (keyword_set(qalph)) then ss = sort(tlm.name)
out = strarr(n)
for i=0L,n-1 do begin
    tlm0 = tlm(ss(i))
    fmt = '(1x, a12, 1x,a3, 1x,a50, 2(1x,a3,1x,i3), i3, i3, 1x,a1,1x,a8,1x,a5)'
    out0 = string(tlm0.name+'                  ', tlm0.type, tlm0.descr, $
		tlm0.apid(0), tlm0.stbyte(0), $
		tlm0.apid(1), tlm0.stbyte(1), $
		tlm0.stbit, tlm0.nbit, $
		tlm0.conv_code, tlm0.conv, tlm0.unit, format=fmt)
    print, out0
    out(i) = out0
end

end

