pro rd_sda_data, lun, ibyt, idset, nout, index, data, roadmap, sel_dsets, nlin_out, nlin_read
;+
;NAME:
;	RD_SDA_DATA
;PURPOSE:
;	Perform the actual data read for SDA 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
;OPTIONAL INPUT:
;	nlin_out - the number of output lines (size of output array)
;	nlin_read - the number of lines to read for this image
;INPUT/OUTPUT:
;	data	- the data
;HISTORY:
;	written 21-Mar-92 by Mons Morrison
;	30-Nov-94 (MDM) - Modified to allow partial reads of images
;	 7-Mar-95 (MDM) - 30-Nov-94 mod was not put online yet
;			- Modified to read compressed data sections
;	15-Mar-95 (MDM) - Expanded to handle INTEGER*4 (because of
;			  ground based data sets.
;-
;
common rd_sda_data_blk, ref_dtyp
;
dtyp = index(idset).gen.data_word_type mod 16
qcomp = index(idset).gen.data_word_type / 128
;
if (idset eq 0) then begin
    ncol_max = max(roadmap(sel_dsets).shape_cmd(0))
    nlin_max = max(roadmap(sel_dsets).shape_cmd(1))

    temp = mask(roadmap.pfi_ffi, 0, 2)
    if (total(temp) eq 0) then nlin_max = 64	;only simple raw PFIs extracted
						;Multiple strip ORs will cause problems - make the buffer size too large

    if (keyword_set(nlin_out)) then nlin_max = nlin_out		;MDM added 30-Nov-94

    case dtyp of
	1: data = bytarr(ncol_max, nlin_max, nout)
	2: data = intarr(ncol_max, nlin_max, nout)
	3: data = lonarr(ncol_max, nlin_max, nout)
	4: data = fltarr(ncol_max, nlin_max, nout)
    endcase
    ref_dtyp = dtyp
end
if (index(idset).gen.ndatabyte eq 0) then return

ncol = index(idset).sxt.shape_sav(0)
nlin = index(idset).sxt.shape_sav(1)
if (keyword_set(nlin_read)) then nlin = nlin_read		;MDM added 30-Nov-94
case dtyp of
    1: data0 = bytarr(ncol,nlin)
    2: data0 = intarr(ncol,nlin)
    3: data0 = lonarr(ncol,nlin)
    4: data0 = fltarr(ncol,nlin)
endcase

if (qcomp) then begin
    data2 = bytarr(index(idset).gen.ndatabyte)
    rdwrt, 'R', lun, ibyt, 0, data2, 1
    data0 = data_compress(data2, decomp=data0)
end else begin
    rdwrt, 'R', lun, ibyt, 0, data0, 1
end
;
if (dtyp gt ref_dtyp) then case dtyp of	;new data type which requires more space
    2: data = fix(data)		;convert to integer*2
    3: data = long(data)	;convert to integer*4
    4: data = float(data)	;convert to real*4
endcase
;
data(0,0,idset) = data0
;
end
