pro rd_h_dbase1, lun, nout, ref, out, names, map2pack, mapv, test=test, fixit=fixit
;
;
if (nout eq 0) then return
;

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(test)) then begin
    out = bytarr(1000)
    readu, lun, out
    stop
end
;
out = replicate(ref, nout)
readu, lun, out
if (qswap) then dec2sun, out
if (keyword_set(fixit)) then begin
    out.offby1 = out.offby1 + fixit
    out.offby2 = out.offby2 + fixit
    out.offby3 = out.offby3 + fixit
    out.offby4 = out.offby4 + fixit
end
;
if (keyword_set(map2pack)) then begin
    names0 = string(out.st$name)
    ss = where_arr(names, names0)
    if (ss(0) ne -1) then map2pack(ss) = mapv
end
;
end
;------------------------------------------------------------------------------
pro rd_hist_dbase, ootc, blktc, lvt_param, lvt_tx, lvt_txs, ca, cas, $
		hk1, hk2, schk, pdde, pdco, pca, pcdi, $
		schk1, schk2, schk3, schk4, map2pack, infil=infil, trace=trace
;+
;NAME:
;	rd_hist_dbase
;PURPOSE:
;	To read the history database definition file READ_HIST.DBS
;SAMPLE CALLING SEQUENCE:
;	rd_hist_dbase, ootc, blktc, lvt_param, lvt_tx, lvt_txs, ca, cas, $
;		hk1, hk2, schk, pdde, pdco, pca, pcdi, infil=infil
;	aa=convert_hk(/read,/force_rd)                  
;OPTIONAL KEYWORD INPUT:
;	infil	- THe input file to read.
;		  NOTE: When reading a .DBS file created on IRIX 4 systems,
;		  the file name must have INSIM somewhere in the file name.
;OUTPUT:
;	ootc, blktc, lvt_param, lvt_tx, lvt_txs, ca, cas, $
;		hk1, hk2, schk, pdde, pdco, pca, pcdi
;
;	map2pack - Same length as LVT_PARAM and tells which packet the
;		   mnemonic is present in.
;HISTORY:
;	Written Apr-94 by M.Morrison
;	16-Jan-95 (MDM) - Added SCHK1, SCHK2, and SCHK4
;			- Added MAP2PACK
;	25-Jan-95 (MDM) - Added allowing exit when MSG_TYPE = 0
;			- Print the number of bytes read
;	14-Nov-95 (MDM) - Modified to handle the fact that under IRIX 5 the
;			  data values that used to be written as FLOAT are
;			  now written as DOUBLE.
;			- Changed the default file name to READ_HIST.EOF
;			  (used to be READ_HIST.DBS)
;	16-Nov-95 (MDM) - Further patch to be able to read the new database
;			  format (the offset values are 6 off)
;	22-Feb-96 (MDM) - Patched to read the INSIM file properly (problem
;			  with "pc_di" structure
;	28-Feb-97 (MDM) - Modified to work with a slightly modified PCDI
;			  structure for EGSE 4.2 READ_HIST.DBS file
;	25-Apr-97 (SDW) - Added "sc3" 
;	 7-May-97 (MDM) - Changed how to recognize the different PCDI
;			  structure for EGSE 4.2 reads
;-
;
if (n_elements(infil) eq 0) then infil = '$MDI_CAL_INFO/READ_HIST.EOF'
;
HIST_DBS_NULL    ='00000000'x     ;/* no more structures */
HIST_DBS_OOTC    ='00000001'x     ;/* ootc structures */
HIST_DBS_BLKTC   ='00000002'x     ;/* block tc structures */
HIST_DBS_PARM    ='00000004'x     ;/* parm structures */
HIST_DBS_TX      ='00000008'x     ;/* status text structures */
HIST_DBS_TXS     ='00000010'x     ;/* status text sub structures */
HIST_DBS_CA      ='00000020'x     ;/* cal table structures */
HIST_DBS_CAS     ='00000040'x     ;/* cal table sub structures */
HIST_DBS_HK1     ='00000080'x     ;/* HK pkt 1 structures */
HIST_DBS_HK2     ='00000100'x     ;/* HK pkt 2 structures */
HIST_DBS_SCHK    ='00000200'x     ;/* spacecraft HK structures */
HIST_DBS_PDDE    ='00000400'x     ;/* derived parm structures */
HIST_DBS_PDCO    ='00000800'x     ;/* conditional parm structures */
HIST_DBS_PCA     ='00001000'x     ;/* analog parm structures */
HIST_DBS_PCDI    ='00002000'x     ;/* digital structures */
HIST_DBS_DCT     ='00004000'x     ;/* formula evaluation structures */
HIST_DBS_MASK    ='00007fff'x     ;/* mask for all HIST_DBS structures */
HIST_DBS_SCHK1   ='00008000'x     ;/* spacecraft HK # 1 structures */
HIST_DBS_SCHK2   ='00010000'x     ;/* spacecraft HK # 2 structures */
HIST_DBS_SCHK3   ='00020000'x     ;/* spacecraft HK # 3 structures */
HIST_DBS_SCHK4   ='00040000'x     ;/* spacecraft HK # 4 structures */
HIST_DBS_DUNNO   ='00080000'x     ;

;
egse_hdr_msg = {msg_type: 0L, $    ;/* Message type, required for queue */
		msg_len: 0L, $     ;/* length of message packet */
		sort_key: 0L, $    ;/* history sort key */
		sys_gmt: 0L}
dbs_full_size = long(0)
if (keyword_set(trace)) then dbs_full_size = lonarr(234)
dbs_msg = {pkt_type: long(0), n_entries: long(0)}
ootc0 = {st$name: bytarr(8+1), st$danger: 0b, st$desc: bytarr(32+1), st$ver_name: bytarr(8+1), $
		st$dat: 0b, pad1: bytarr(3), $
		cont: 0L, nbpack: 0L}
blktc0 = {st$name: bytarr(8+1), st$danger: 0b, st$desc: bytarr(32+1), st$ver_name: bytarr(8+1), $
		st$dat: 0b, pad1: bytarr(3), $
		cont: 0L, nbpack: 0L, dest: 0L, ident: 0L, length: 0L, data: lonarr(30), $
		variable: 0L, f_args: 0L, v_args: 0L, use_cksum: 0L, cksum: 0L}
lvt_param0 = {st$name: bytarr(9+1+2), type: 0L, st$shrt_desc: bytarr(32+1+3), report: 0L, $
                user_count: 0L, limit_set: 0L, state: 0L, last_state: 0L, $
		st$datyp: bytarr(1+1), st$daseq: bytarr(12+1), st$datc: bytarr(8+1), $
		st$damess: bytarr(30+1+1), max_val: 0L, pc_idx: 0L}
lvt_tx0 = {st$name: bytarr(9+1+2), n_points: 0L, first_idx: 0L, last_idx: 0L}
lvt_txs0 = {str_low: 0L, str_high: 0L, st$str_txs: bytarr(12+1+3)}
dbs_ca0 = {st$name: bytarr(9+1), st$unit: bytarr(5+1), n_points: 0L, $
		first_idx: 0L, last_idx: 0L, junk: 0L, slope: dblarr(19)}
dbs_cas0 = {x_val: 0L, junk:0L, y_val: 0D}
pd_tm0 = {st$name: bytarr(9+1+2), offby1: 0L, mask1: 0L, offby2: 0L, mask2: 0L, $		;dbs_hk0, dbs_hk1, dbs_schk
		offby3: 0L, mask3: 0L, offby4: 0L, mask4: 0L, $
		nbocc: 0L, lgocc: 0L, lvt_idx: 0L}
pd_de0 = {st$name: bytarr(9+1), st$formula: bytarr(128+1+3), $
		first_idx: 0L, last_idx: 0L, lvt_idx: 0L}
pd_co0 = {st$name: bytarr(9+1), st$formula: bytarr(128+1+3), $
		first_idx: 0L, last_idx: 0L, value: 0L}
pca0 = {st$name: bytarr(9+1), st$unit: bytarr(5+1), st$curve: bytarr(8+1+3), $
		curve_idx: 0L, dliset: 0L, st$licon: bytarr(8+1+3), licon_idx: 0L, $
		junk1: 0L, $
		act_max: 0D, rpt_max: 0D, act_min: 0D, rpt_min: 0D, $
		act_del: 0L, rpt_del: 0L, lvt_idx: 0L, junk2: 0L}
pc_di0 = {st$name: bytarr(9+1), st$pcd_text: bytarr(9+1), tx_idx: 0L, cur_txs_idx: lonarr(6), $
		st$expect_str: bytarr(12+1), st$prev_bad_st: bytarr(12+1+2), lvt_idx: 0L, junk: bytarr(16)}

;;if (strpos(infil, '.EOF_NEW') ne -1) then begin		;slight modification
junk = file_info2(infil, finfo)
if (int2secarr(finfo, '1-Jan-97') gt 0) then begin	;egse 4.2 different format
    pc_di0 = {st$name: bytarr(9+1), st$pcd_text: bytarr(9+1), tx_idx: 0L, cur_txs_idx: lonarr(50), $
		st$expect_str: bytarr(12+1), st$prev_bad_st: bytarr(12+1+2), lvt_idx: 0L}
end

fixit = -6
if (strpos(infil, '.INSIM') ne -1) then begin		;old IRIX 4 format
    message, 'Assuming the file is in the old IRIX 4 format', /info
    dbs_ca0 = {st$name: bytarr(9+1), st$unit: bytarr(5+1), n_points: 0L, $
		first_idx: 0L, last_idx: 0L, slope: fltarr(19)}
    dbs_cas0 = {x_val: 0L, y_val: 0.0}
    pca0 = {st$name: bytarr(9+1), st$unit: bytarr(5+1), st$curve: bytarr(8+1+3), $
		curve_idx: 0L, dliset: 0L, st$licon: bytarr(8+1+3), licon_idx: 0L, $
		act_max: 0.0, rpt_max: 0.0, act_min: 0.0, rpt_min: 0.0, $
		act_del: 0L, rpt_del: 0L, lvt_idx: 0L}
    pc_di0 = {st$name: bytarr(9+1), st$pcd_text: bytarr(9+1), tx_idx: 0L, cur_txs_idx: lonarr(6), $
                st$expect_str: bytarr(12+1), st$prev_bad_st: bytarr(12+1+2), lvt_idx: 0L}
    fixit = 0
end

;
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
;
openr, lun, infil, /get_lun
readu, lun, egse_hdr_msg	& if (qswap) then dec2sun, egse_hdr_msg
readu, lun, dbs_full_size	& if (qswap) then dec2sun, dbs_full_size
;
qdone = 0
iloop = 0L
i80000 = 0
while (not qdone) do begin
    readu, lun, dbs_msg	& if (qswap) then dec2sun, dbs_msg
    point_lun, -lun, byte_pos
    print, dbs_msg.(0), dbs_msg.(1), byte_pos
    ;print, dbs_msg.(0), 2L^iloop
    ;dbs_msg.(0) = 2L^iloop
    ;if (dbs_msg.pkt_type eq 8192) then pause
    ;pause
    ;
    nout = dbs_msg.n_entries
    case dbs_msg.pkt_type of
	HIST_DBS_NULL:			;    ='00000000'x     ;/* no more structures */
	HIST_DBS_OOTC:  rd_h_dbase1, lun, nout, ootc0, ootc		;/* ootc structures */			1
	HIST_DBS_BLKTC: rd_h_dbase1, lun, nout, blktc0, blktc		;/* block tc structures */		2
	HIST_DBS_PARM:  rd_h_dbase1, lun, nout, lvt_param0, lvt_param	;/* parm structures */			4
	HIST_DBS_TX:    rd_h_dbase1, lun, nout, lvt_tx0, lvt_tx		;/* status text structures */		8
	HIST_DBS_TXS:   rd_h_dbase1, lun, nout, lvt_txs0, lvt_txs	;/* status text sub structures */	16
	HIST_DBS_CA:   	rd_h_dbase1, lun, nout, dbs_ca0, ca		;/* cal table structures */		32
	HIST_DBS_CAS:   rd_h_dbase1, lun, nout, dbs_cas0, cas		;/* cal table sub structures */		64
	'80000'x:   begin
		  case i80000 of
		        0: rd_h_dbase1, lun, nout, pd_tm0, hk1, names, map2pack, 1, fixit=fixit	 ;/* HK pkt 1 structures */		128
			1: rd_h_dbase1, lun, nout, pd_tm0, hk2, fixit=fixit
			2: ;sci5k ?? from /mdisw/egse_v3.2/dbs/TM.EOF file 
			3: rd_h_dbase1, lun, nout, pd_tm0, schk1, names, map2pack, 4, fixit=fixit
			4: rd_h_dbase1, lun, nout, pd_tm0, schk2, names, map2pack, 8, fixit=fixit
			5: rd_h_dbase1, lun, nout, pd_tm0, schk3, names, map2pack, 16, fixit=fixit
			6: rd_h_dbase1, lun, nout, pd_tm0, schk4, names, map2pack, 32, fixit=fixit
			else:
		  endcase
		  i80000 = i80000 + 1
		end
	HIST_DBS_HK1:   rd_h_dbase1, lun, nout, pd_tm0, hk1, names, map2pack, 1	 ;/* HK pkt 1 structures */		128
	HIST_DBS_HK2:   rd_h_dbase1, lun, nout, pd_tm0, hk2		;/* HK pkt 2 structures */		256
	HIST_DBS_SCHK:  rd_h_dbase1, lun, nout, pd_tm0, schk, names, map2pack, 2 ;/* spacecraft HK structures */		512
	HIST_DBS_PDDE:  rd_h_dbase1, lun, nout, pd_de0, pdde 		;/* derived parm structures */		1024
	HIST_DBS_PDCO:  rd_h_dbase1, lun, nout, pd_co0, pdco 		;/* conditional parm structures */	2048
	HIST_DBS_PCA:   rd_h_dbase1, lun, nout, pca0, pca		;/* analog parm structures */		4096
	HIST_DBS_PCDI:  rd_h_dbase1, lun, nout, pc_di0, pcdi 		;/* digital structures */		8192
	HIST_DBS_DCT:   		;    ='00004000'x     ;/* formula evaluation structures */			16384
	HIST_DBS_MASK:   		;    ='00007fff'x     ;/* mask for all HIST_DBS structures */
	HIST_DBS_SCHK1: rd_h_dbase1, lun, nout, pd_tm0, schk1, names, map2pack, 4  ;/* SCHK pkt 1 structures */		32768
	HIST_DBS_SCHK2: rd_h_dbase1, lun, nout, pd_tm0, schk2, names, map2pack, 8  ;/* SCHK pkt 2 structures */		65536
	HIST_DBS_SCHK3: rd_h_dbase1, lun, nout, pd_tm0, schk3, names, map2pack, 16  ;/* SCHK pkt 3 structures */	131072
	HIST_DBS_SCHK4: rd_h_dbase1, lun, nout, pd_tm0, schk4, names, map2pack, 32 ;/* SCHK pkt 4 structures */		262144
	'00080000'x:  							;dunno
	else: print, "Don't recognize the code " + strtrim(dbs_msg.pkt_type,2) + "..... Continuing..."
    endcase

    if (dbs_msg.pkt_type eq HIST_DBS_PARM) then begin
	names = string(lvt_param.st$name)
	n = n_elements(names)
	map2pack = intarr(n)		;initialize the array - to be passed to RD_H_DBASE1 and written into
    end

    ;;qdone = (dbs_msg.pkt_type eq HIST_DBS_DCT)
    qdone = eof(lun) or (dbs_msg.pkt_type eq 0)
    iloop = iloop + 1
end
;
point_lun, -lun, ibyte
print, 'RD_HIST_DBASE read ', ibyte, ' bytes'
free_lun, lun
end
