pro wrtBCSMap, lun, fpoint, bytout, fhead, rsiz, bcs_dp_sync, nbcs_dp_sync, init_fhead=init_fhead, err_log=err_log, $
				sub_dp=sub_dp
;
;
;
ref_struct, ref, dpos1, dpos2, sxtos1, sxtos2, sxtos3, stat1
;
if (keyword_set(init_fhead)) then gen_struct, file_header=fhead
gen_struct, gen_index=gen_index
bcs_struct, bcs_index=bcs_index, bcs_roadmap=bcs_roadmap, bcs_dps_head=bcs_dps_head, bcs_version=bcs_version
;
qdebug = 0
print, 'Now Inside WRTBCSMAP.PRO
;
rsiz = ref.BCS_Fil_Rec_Siz
;
if ((nbcs_dp_sync ge 1) and (keyword_set(sub_dp))) then bcs_sub_dp, bcs_dp_sync, nbcs_dp_sync, /info, err_log=err_log
				;change from ongoing total counts accumulated
				;to counts for the last accumulation period
				;2 or 16 seconds
;
ibyt_stop = bytout
if (nbcs_dp_sync gt 0) then begin
    fpoint.opt_section = bytout
    bcs_dps_head.nEntries = nbcs_dp_sync
    rdwrt, 'W', lun, bytout, rsiz, bcs_dps_head, 1
    rdwrt, 'W', lun, bytout, rsiz, bcs_dp_sync(0:nbcs_dp_sync-1), 1
end
;
maxsamps = 0
nDataSets = 0
fpoint.map_section = bytout
;
first_day = 0
first_time = 0
last_day = 0
last_time = 0	;TODO - what if no spectra extracted
;
ibyt = fpoint.data_section
while (ibyt lt ibyt_stop) do begin
    ibyt0 = ibyt
    if (qdebug) then print, 'Dset = ', ndatasets+1, '  ibyt = ', ibyt0

    rdwrt, 'R', lun, ibyt, rsiz, Gen_Index, 1
    rdwrt, 'R', lun, ibyt, rsiz, BCS_Index, 1

    bcs_roadmap.byteSkip		= ibyt0
    bcs_roadmap.time			= gen_index.time
    bcs_roadmap.day			= gen_index.day
    ;
    BCS_roadmap.BlockID			= bcs_index.BlockID
    BCS_roadmap.ModeID			= bcs_index.ModeID
    BCS_roadmap.moderepnum		= bcs_index.moderepnum
    BCS_roadmap.dgi			= bcs_index.dgi
    BCS_roadmap.dp_Flags		= bcs_index.DP_Flags
    BCS_roadmap.BCS_Status		= bcs_index.BCS_Status

    BCS_roadmap.total_cnts		= bcs_index.total_cnts
    BCS_roadmap.length    		= bcs_index.length    
    BCS_roadmap.controltally		= bcs_index.controltally

    rdwrt, 'W', lun, bytout, rsiz, BCS_Roadmap, 1

    if (qdebug) then print, 'Time = ', gen_index.time
    if (qdebug) then print, 'GEN_Index.nIndexByte =', GEN_Index.nIndexByte
    if (qdebug) then print, 'GEN_Index.nDataByte  =', GEN_Index.nDataByte

    ibyt = ibyt0 + GEN_Index.nIndexByte + Gen_Index.nDataByte
    nDataSets = nDataSets + 1

    day  = bcs_roadmap.day
    time  = bcs_roadmap.time
    if (nDataSets eq 1) then begin
	first_day  = day
	first_time = time
	last_day   = day
	last_time  = time
    end else begin
        s_day = 86400
        v = (day-first_day)*s_day + (time-first_time)/1000
        if (v lt 0) then begin  ;found an earlier start time/day
            first_day  = day
            first_time = time
        end
        v = (day-last_day)*s_day + (time-last_time)/1000
        if (v gt 0) then begin  ;found a later end time/day
            last_day  = day
            last_time = time
        end
    end

    maxsamps = max([maxSamps, gen_index.nDataByte])
end

fpoint.TotBytes		= bytout
fpoint.roadmap_version  = bcs_version.roadmap

fhead.first_Time	= first_time
fhead.first_Day		= first_day
fhead.last_Time		= last_time
fhead.last_Day		= last_day

fhead.nDataSets		= nDataSets
fhead.maxSamps		= maxSamps
fhead.ntot_opt		= nbcs_dp_sync 

;; stop
end
