pro rd_sda, infil, dset_arr, index, data, roadmap, nodata=nodata, filidx=filidx
;	---------------------------------------------------------------
;+
;NAME:
;	RD_SDA
;PURPOSE:
;	Read and extract SXT images specified in the 'dset_arr'
;	parameter from the SXT Solar-A reformatted data base.
;CALLING SEQUENCE:
;	Rd_SDA, infil, dset_arr, index, data, roadmap
;INPUT:
;	infil   : input file specification (SPR... or SFR...)
;		  "infil" can be a vector of filenames (WARNING - 
;		  do not mix and match FFI and PFI)
;	dset_arr: image indices vector.  The order that the images are
;		  placed in the output variable "data" is the same
;		  order that they appear in "dset_arr"
;		  If "-1" is passed, the whole file is read
;		  and the dset_arr returned is the "indgen(ndset)"
;		  If infil is a scalar, then dset_arr should be
;		  a scaler or 1-D vector.  i
;		  If infil is a vector of files, then dset_arr should 
;		  be a structure with fields ".dset" and ".ifil".
;		  ".dset" is the dataset number WITHIN THE FILE, and
;		  ".ifil" is the index of the filename within the
;		  infil array.
;		  The structure is "N" elements long
;		  where "N" is the total number of datasets to
;		  extract.  
;OPTIONAL INPUT:
;	nodata  : If present, only the index is read
;OUTPUT:
;	index	: data-index logical record (one for each 
;		  image requested)
;	data	: 2D or 3D image array
;	roadmap	: summary of data-index logical record, note
;		  all roadmaps records in the file are returned.
;		  In the case where "infil" is an array, roadmap
;		  is the concatenated roadmap for all files.
;       filidx  : a vector the same length as the roadmap
;                 with the index of the file associated with that entry
;                 (ie: 0,0,0,0,1,1,1,1,2,2,2,2,...)
;		  This is output from RD_ROADMAP
;Examples:
;	rd_sda, filename, indgen(10), index, data, roadmap
;	returns 1st 10 images in file=filename
;HISTORY:
;	written by Mons Morrison, Fall 90.
;	11-Nov-91 MDM - Added option to not read the data
;	12-Nov-91 MDM - added the option to read the whole
;			file by passing a -1
;	 7-Dec-91 MDM - added option to pass a vector of filenames
;			as well as the "filidx" parameter
;-
;	---------------------------------------------------------------
;
sxt_struct
sxt_old_struct
;
get_lun, lun
nfiles = n_elements(infil)
;
rd_roadmap, infil, roadmap, ndset, recsize, filidx=filidx		;get roadmap for ALL files
if (total(ndset) eq 0) then begin
    print, 'RD_SDA: # of datasets in the file is zero'
    print, 'Returning
    return
end
;
siz = size(dset_arr)
typ = siz (siz(0)+1 )
qstruct = (typ eq 8)
if (not qstruct) then begin
    dset_arr0 = make_str("{dummy, dset: fix(0), ifil: fix(0)}")
    dset_arr0 = replicate(dset_arr0, n_elements(dset_arr))
    dset_arr0.dset = dset_arr
end else begin
    dset_arr0 = dset_arr
end
;
if ((nfiles gt 1) and (not qstruct)) then begin
    print, 'RD_SDA: Sorry - since you passed an array of file names'
    print, 'your "dset_arr" input must be a structure.  Please review the
    print, 'documentation of this routine by typing "doc_library,''rd_sda''
    stop
    return
end
if (qstruct) then begin
    imax_fil = max( dset_arr0.ifil )
    if (imax_fil gt nfiles-1) then begin
	print, 'RD_SDA: Your "dset_arr" specifies', imax_fil+1, ' files'
	print, 'but your "infil" array only has ', nfiles, ' files'
	return
    end
end
;
ss = where(dset_arr0.dset eq -1)		;check for cases where asking for whole data file
if (ss(0) ne -1) then begin
    tmp_arr = -1
    fil_arr = -1
    for i=0, n_elements(dset_arr0)-1 do if (dset_arr0(i).dset eq -1) then begin
	if (qstruct) then ifil = dset_arr0(i).ifil  $
		else ifil = 0
	filnam = infil( ifil )
	rd_fheader, filnam, dummy, ndset0
	tmp_arr = [tmp_arr, indgen(ndset0)]
	fil_arr = [fil_arr, intarr(ndset0)+ifil]
    end
    dset_arr0 = replicate(dset_arr0(0), n_elements(tmp_arr)-1 )
    dset_arr0.dset = tmp_arr(1:*)
    dset_arr0.ifil = fil_arr(1:*)
end
nout = n_elements(dset_arr0)
;
dset_arr3 = dset_arr0.dset	;data set # relative to roadmap - figures how many dataset in total in each file.
if (qstruct) then begin		;need to change the dataset index to be relative to roadmap (ongoing dataset #)
     offset = 0
     for i=0,nfiles-2 do begin		
	offset = offset + ndset(i)
	ss = where(dset_arr.ifil eq i+1)
	if (ss(0) ne -1) then dset_arr3(ss).dset = dset_arr3(ss).dset + offset
    end
end
;;if (fheader.maxsamps ne 0) then begin	;only check for buffer size if there is any data
    ncol_max = max(roadmap(dset_arr3).shape_cmd(0))
    nlin_max = max(roadmap(dset_arr3).shape_cmd(1))
;;end
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
;
rd_index, infil(0), dummy, index0
index_version = [index0.gen.index_version, index0.sxt.index_version]
index = replicate(index0, nout)
data = 0	;free up previously defined memory
;
idset = 0	;dset in the output matrix
for ifil=0,nfiles-1 do begin
    ss = where(dset_arr0.ifil eq ifil)
    ndset_arr2 = 0
    if (ss(0) ne -1) then begin
	dset_arr2 = dset_arr0(ss).dset			;dataset vector for a single file
	ndset_arr2 = n_elements(dset_arr2)
    end

    if (ndset_arr2 ne 0) then begin
	openr, lun, infil(ifil), /block
	;
	rd_roadmap, lun, roadmap2
	rd_fheader, lun, fheader, ndset
	if (dset_arr2(0) eq -1) then dset_arr2 = indgen(ndset)		;should have already been converted to
									;an actual list of dataset #s above
	;
	for idset2=0,ndset_arr2-1 do begin
	    dset = dset_arr2(idset2)
	    ;
	    if ((dset gt ndset-1) or (dset lt 0)) then begin
		print, 'Data set number must be greater than 0 and less than ', ndset-1
		print, 'Action: Returning 
		return
	    end
	    ;
	    ibyt = roadmap2(dset).byteSkip
	    rdwrt, 'R', lun, ibyt, 0, index0, 1
	    index(idset) = index0
	    ;
	    chk_index_version = [index0.gen.index_version, index0.sxt.index_version]
	    if (total(chk_index_version-index_version) ne 0) then begin
		rd_index, lun, roadmap2(dset).byteSkip, index0
		index_tmp = replicate(index0, nout)		;make output structure the data type of the latest structure
		for i=0,idset-1 do index_tmp(i) = str_copy(index0, index(i))
		index = index_tmp
		index_version = [index0.gen.index_version, index0.sxt.index_version]
	    end
		
	    ;TODO - also, what if later datasets have expanded structures?  - have to make sure that the
	    ;       first dataset has all possible structure expansions defined in it.
	    ;
	    qread_data = 1
	    if (keyword_set(nodata)) then qread_data = 0
	    if (index(idset).gen.ndatabyte eq 0) then qread_data = 0
	    if (qread_data) then begin
		dtyp = index(idset).gen.data_word_type mod 16
		if (idset eq 0) then begin
		    case dtyp of
			1: data = bytarr(ncol_max, nlin_max, nout)
			2: data = intarr(ncol_max, nlin_max, nout)
			4: data = fltarr(ncol_max, nlin_max, nout)
		    endcase
		    ref_dtyp = dtyp
		end

		ncol = index(idset).sxt.shape_sav(0)
		nlin = index(idset).sxt.shape_sav(1)
		ibyt = roadmap2(dset).byteSkip + index(idset).gen.nIndexByte
		case dtyp of
		    1: data0 = bytarr(ncol,nlin)
		    2: data0 = intarr(ncol,nlin)
		    4: data0 = fltarr(ncol,nlin)
		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,0,idset) = data0

		idset = idset + 1
	    end		;if read data

	end		;loop for each dataset in one file
    close, lun
    end			;finished one file
end
;
free_lun, lun
end
