pro rd_bda_data, lun, ibyt, idset, nout, index, data, roadmap, sel_dsets
;+
;NAME:
;       RD_BDA_DATA
;PURPOSE:
;       Perform the actual data read for BDA files (called from RD_XDA)
;INPUT:
;       lun     - the logical unit number where the file is open
;       ibyt    - the starting byte value to read from
;       idset   - the dataset number for the output matrix
;       nout    - the total number of datasets to be extracted
;       index   - the index structure for all output datasets
;       roadmap - the roadmap
;       sel_dsets- the indicies of the datasets selected for extraction
;INPUT/OUTPUT:
;       data    - the data
;RESTRICTIONS:
;	There might be problems if using old data files (prior to Dec-91)
;	mixed with new data files.  The "length" field might be non-zero
;	only in a few files and the maximum sample length used might be
;	wrong (too small)...
;HISTORY:
;       written 21-Mar-92 by Mons Morrison
;-
;
common rd_bda_data_blk, ref_dtyp, quse_length
;
dtyp = index(idset).gen.data_word_type mod 16
;
if (idset eq 0) then begin
    maxsamps = 0
    quse_length = 1
    ii = tag_index(roadmap, 'length')
    if (ii ne -1) then maxsamps = max(roadmap(sel_dsets).length)
    if (maxsamps eq 0) then begin		;handles case where tag "index" is not defined at all (OR) case where length = 0
	rd_fheader, infil, file_header, ndset0
	maxsamps = max(file_header.maxSamps)
	quse_length = 0
    end

    case dtyp of
	1: data = bytarr(maxsamps, nout)
	2: data = intarr(maxsamps, nout)
	4: data = fltarr(maxsamps, nout)
    endcase
    ref_dtyp = dtyp
end
if (index(idset).gen.ndatabyte eq 0) then return
		
if (quse_length) then nbyte = index(idset).bcs.length $
		else nbyte = index(idset).gen.ndatabyte	
		;"ndatabyte" is the actual number of bytes on the disk, including padding
case dtyp of
    1: data0 = bytarr(nbyte)
    2: data0 = intarr(nbyte)
    4: data0 = fltarr(nbyte)
endcase

rdwrt, 'R', lun, ibyt, 0, data0, 1

if (dtyp gt ref_dtyp) then case dtyp of	;new data type which requires more space
    2: data = fix(data)		;convert to integer*2
    4: data = float(data)	;convert to real*4
endcase
;
data(0,idset) = data0

end
