pro bcs_qs, lun, irec0, rec1, gen_index, ihalf, qs_struct
;
;input
;	ihalf = 0 or 1 depending on the half of the major frame to check
;
;
qfirst = 0
if (qs_struct.instr(0).st_day eq 0) then qfirst = 1             ;has not been initialized yet
;
ss = indgen(8)*2 + ihalf*32
scaval = rec1(16+49, ss)
hv_control = rec1(16+49, 16+ihalf*32)
stim_control = rec1(16+49, 26+ihalf*32)
;
qgotsf2 = 0
qgotsf3 = 0
rec2 = rec1		;copy the major frame to a temporary variable
irec = irec0
qrepeat = 1
while (qrepeat) do begin
    fi = fix(rec2(16+3,0))/64                  ;0,1,2, or 3
    if (fi eq 2) then begin
	fe26_thresh	= rec2(16+66, 28)
	qgotsf2 = 1
    end
    if (fi eq 3) then begin
	chan_mfd	= rec2(16+66, 28)
	saa_thresh	= rec2(16+66, 29)
	flr_riseThresh	= rec2(16+66, 30)
	flr_decayThresh	= rec2(16+66, 31)
	flr_optionID	= rec2(16+66, 63)
	qgotsf3 = 1
    end
    ;
    qrepeat = 0
    if (qfirst) then begin
	if ((not qgotsf2) or (not qgotsf3)) then begin		;check to see if have gotten both sets of words
	    irec = irec + 1		;try the next one
            rd_telem, lun, irec, rec2
	    qrepeat = 1
	end
    end
end
;
half_mf = dprate2sec(gen_index.dp_rate) / 2.*1000		;convert to msec
st_time = gen_index.time + half_mf*ihalf
;
n = n_elements(qs_struct.instr)
qnew_entry = chk_change(qs_struct.instr(n-1).scaval, scaval) or $
		chk_change(qs_struct.instr(n-1).hv_control, hv_control) or $
		chk_change(qs_struct.instr(n-1).stim_control, stim_control)
if (qgotsf2) then qnew_entry = qnew_entry or $
		chk_change(qs_struct.instr(n-1).fe26_thresh, fe26_thresh)
if (qgotsf3) then qnew_entry = qnew_entry or $
                chk_change(qs_struct.instr(n-1).chan_mfd, chan_mfd) or $
                chk_change(qs_struct.instr(n-1).saa_thresh, saa_thresh) or $
                chk_change(qs_struct.instr(n-1).flr_risethresh, flr_risethresh) or $
                chk_change(qs_struct.instr(n-1).flr_decaythresh, flr_decaythresh) or $
                chk_change(qs_struct.instr(n-1).flr_optionid, flr_optionid)
;
if (qnew_entry or qfirst) then begin
    if (qnew_entry and (not qfirst)) then begin
	err_log, 'STR', 'INF/BCS: Had to expand the QS_Instr Structure'

	qs_struct0 = qs_struct	;save current qs variable
	str = "{bcs_qs_rec2, "+$
                                "general1: {qs_general1_rec}, "+$
                                "instr: replicate({bcs_qs_instr_rec}," + strtrim(n+1,2) + "), "+$
                                "conv: {bcs_qs_conv_rec} }"  
	qs_struct = make_str(str)
	;
	;----- Copy previous data into current
	qs_struct.general1 = qs_struct0.general1
	qs_struct.conv     = qs_struct0.conv
	for i=0,n-1 do qs_struct.instr(i) = qs_struct0.instr(i)

	n = n + 1
    end
    ;
    qs_struct.instr(n-1).entry_type = '0212'x
    qs_struct.instr(n-1).st_day  = gen_index.day
    qs_struct.instr(n-1).st_time = st_time
    qs_struct.instr(n-1).scaval = scaval
    qs_struct.instr(n-1).hv_control = hv_control
    qs_struct.instr(n-1).stim_control = stim_control

    if (qfirst or qgotsf2) then begin
	qs_struct.instr(n-1).fe26_thresh	= fe26_thresh				;only use current if valid
    end else begin
	qs_struct.instr(n-1).fe26_thresh	= qs_struct.instr(n-2).fe26_thresh	;not updated, copy old value
    end

    if (qfirst or qgotsf3) then begin
        qs_struct.instr(n-1).chan_mfd		= chan_mfd
        qs_struct.instr(n-1).saa_thresh		= saa_thresh
        qs_struct.instr(n-1).flr_risethresh	= flr_risethresh
        qs_struct.instr(n-1).flr_decaythresh	= flr_decaythresh
        qs_struct.instr(n-1).flr_optionID	= flr_optionID
    end else begin
        qs_struct.instr(n-1).chan_mfd		= qs_struct.instr(n-2).chan_mfd		;not updated, copy old value
        qs_struct.instr(n-1).saa_thresh		= qs_struct.instr(n-2).saa_thresh	;not updated, copy old value
        qs_struct.instr(n-1).flr_risethresh	= qs_struct.instr(n-2).flr_risethresh	;not updated, copy old value
        qs_struct.instr(n-1).flr_decaythresh	= qs_struct.instr(n-2).flr_decaythresh	;not updated, copy old value
        qs_struct.instr(n-1).flr_optionID	= qs_struct.instr(n-2).flr_optionID	;not updated, copy old value
    end
end
;
qs_struct.instr(n-1).en_day  = gen_index.day
qs_struct.instr(n-1).en_time = st_time + half_mf
;
end
