pro mk_obs_bcs, bcs_roadmap, dp_sync, obs_bcs_arr, qdebug=qdebug
;
;
;HISTORY:
;	Written Fall '91 by M.Morrison
;	 3-Mar-92 (MDM) - Made ALL_CNTS and ACC_CNTS the SQRT of
;			  the DP_SYNC record value (to fit into one byte)
;	15-Oct-92 (MDM) - Added /QDEBUG and had print statements only
;			  occur when QDEBUG is set
;	15-Apr-93 (MDM) - Changed {obs_bcs_rec} to {obs_bcs_obs_rec}
;
qdebug = keyword_set(qdebug)
;
ms_day = 86400000            ; 1 day = 86400000 msec
;
qdp_sync = 1	;there is dp_sync data
if (get_nbytes(dp_sync) le 2) then qdp_sync = 0
;
nbuff = 100
obs_bcs_arr = replicate({obs_bcs_obs_rec}, nbuff)
nobs = 0
;
time = bcs_roadmap.time
day  = bcs_roadmap.day
if (n_elements(bcs_roadmap) ne 1) then begin
    ss = indgen(n_elements(bcs_roadmap)-1)
    diff = (day(ss+1)-day(ss))*ms_day + (time(ss+1)-time(ss))
end
;
msod_spec = (day-day(0))*ms_day + time		;can only handle 10 day span
msod_dp = -1000000	;make it never find any data
if (qdp_sync) then msod_dp   = (dp_sync.day-dp_sync(0).day)*ms_day + dp_sync.time
;
tim_sin_last = 0
total_cnts = lonarr(4)		;zero out array
ntotal_cnts = long(0)
nDPavg = 0
;
n = n_elements(bcs_roadmap)
for i=0,n-1 do begin
    total_cnts = total_cnts + bcs_roadmap(i).total_cnts
    ntotal_cnts = ntotal_cnts + 1

    if (tim_sin_last eq 0) then begin	;starting a new entry
	obs_bcs_arr(nobs).entry_type	= 18
	obs_bcs_arr(nobs).time		= bcs_roadmap(i).time
	obs_bcs_arr(nobs).day		= bcs_roadmap(i).day
	obs_bcs_arr(nobs).blockID	= bcs_roadmap(i).blockID
	obs_bcs_arr(nobs).seqID		= bcs_roadmap(i).seqID
	obs_bcs_arr(nobs).ModeID	= bcs_roadmap(i).ModeID
	obs_bcs_arr(nobs).dgi		= bcs_roadmap(i).dgi
	obs_bcs_arr(nobs).dp_flags	= bcs_roadmap(i).dp_flags
	obs_bcs_arr(nobs).bcs_status	= bcs_roadmap(i).bcs_status
	ist = i
    end

    qforce = 0
    if (i eq n-1) then begin
	qforce = 1		;write out last record for sure
    end else begin
	tim_sin_last = tim_sin_last + diff(i)
	if (tim_sin_last gt 4000) then qforce = 1

	if (bcs_roadmap(i).modeID ne bcs_roadmap(i+1).modeID) then qforce = 1
	;TODO - any other cases to check for?
    end

    if (qforce) then begin
	obs_bcs_arr(nobs).total_cnts	= total_cnts / ntotal_cnts
	obs_bcs_arr(nobs).nAveraged	= ntotal_cnts*4

	st_time = msod_spec(ist)
	en_time = msod_spec(i)
	ss1 = where( (msod_dp ge st_time) and (msod_dp le en_time))
	if (ss1(0) ne -1) then begin	;found some match
	    acc_interval = dp_sync(ss1).acc_interval(0)		;TODO - currently only look at first element
	    ss2 = where(acc_interval ne 0)
	    if (ss2(0) ne -1) then begin
		ss = ss1(ss2)
		acc_interval = dp_sync(ss).acc_interval(0)	;
		temp = acc_interval - acc_interval(0)
		if (total(temp) ne 0) then begin
		    if (qdebug) then print, 'WRN/BCS: DP_SYNC.acc_interval not all the same for the entry', string(7b)
		    ;TODO - how to treat?
		    ;stop
		end else begin
		    nn = n_elements(ss)
		    if (nn gt 3) then begin
			if (qdebug) then print, 'WRN/BCS: # of DP_SYNC combined is > 3
			;Only log 3, so when the ObsLog says 3 DP entries, it is 3 or more
		    end
		    for ichan=0,3 do begin
			obs_bcs_arr(nobs).all_cnts(ichan)	= sqrt(total(dp_sync(ss).all_cnts(ichan,*)) / (nn*2))
			obs_bcs_arr(nobs).acc_cnts(ichan)	= sqrt(total(dp_sync(ss).acc_cnts(ichan,*)) / (nn*2))
			obs_bcs_arr(nobs).acc_interval		= dp_sync(ss(0)).acc_interval(0)
			obs_bcs_arr(nobs).nAveraged		= obs_bcs_arr(nobs).nAveraged + nn<3	;truncate
		    end
		end
	    end else begin
		if (qdebug) then print, 'WRN/BCS: No DP Sync where acc_interval ne 0'
	    end
	end else begin
	    if (qdebug) then print, 'WRN/BCS: No DP Sync for this observing log entry'
	end

	tim_sin_last = 0
	total_cnts = lonarr(4)		;zero out array
	ntotal_cnts = long(0)
	nDPavg = 0

	nobs = nobs + 1
	if (nobs ge n_elements(obs_bcs_arr)) then obs_bcs_arr = [obs_bcs_arr, replicate({obs_bcs_obs_rec}, nbuff)]
    end
end
;
obs_bcs_arr = obs_bcs_arr(0:nobs-1)
;
end
