pro rd_roadmap, lun_filnam, roadmap, ndset, recsize, filidx=filidx
;	---------------------------------------------------------
;+
;NAME:
;	rd_roadmap
;PURPOSE:
;	Read and extract the entire roadmap from any of 
;	the reformatted data base files.  If the variable "roadmap"
;	is defined, and the requested file is the same as the last
;	call to rd_roadmap, then the data is not read a second time.
;CALLING SEQUENCE:
;	rd_roadmap, lun_filnam, roadmap, ndset, recsize
;INPUT:
;	lun_filnam -	input file name(s).  It can be a scalar or a vector.  
;			NOTE: They must all be for the same instrument!!
;				OR 
;			the logical unit number of an openned
;			data base file. scalar or vector
;Output:
;	roadmap		summary of data-index logical records 
;	ndset		number of roadmap records/image blocks
;			If "lun_filnam" is an array, ndset is an
;			array
;	recsize		VMS record size			
;OPTIONAL KEYWORD OUTPUT:
;	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,...)
;History:
;	written by Mons Morrison, Fall 90.
;	 7-Dec-91 (MDM) - Added option of passing multiple file names
;	18-Mar-92 (MDM) - Added capability to not re-read the roadmap from the
;			  files if the last read was using the same file.  The
;			  roadmap variable has to be passed back into the
;			  routine for this to work.
;	11-Jun-92 (MDM) - Fixed bug where there is trouble when there are
;			  zero datasets in the first file
;	 7-Jul-92 (MDM) - Added check to see if file exists - only checks the
;			  first file if the input filename list is an array.
;			  Also, only works if the input is a string type
;	20-Jul-92 (MDM) - Updated the header
;	23-Jul-92 (MDM) - Added HXI_ROADMAP structure
;	13-Aug-92 (MDM) - Added SXL_ROADMAP structure
;	13-Nov-92 (MDM) - Added BSC_ROADMAP structure
;	19-Mar-93 (MDM) - Correction for SXL structure name since structure
;			  name was changed to include the version number
;	 9-Jun-93 (MDM) - Modified the way that the individual roadmaps
;			  from each file are appended into the output array
;	28-Jul-94 (MDM) - Modified documentation header
;	15-Nov-94 (MDM) - Modified to allocate memory for all of the roadmaps
;			  in advance and insert the roadmap for each file
;			  into the large array (previously it was doing an
;			  append for each file)
;        5-oct-96 (SLF) - changed fildix references from BYTE to INTEGER
;	18-Feb-01 (LWA) - Changed "for" loops to longword type
;-
;
common rd_roadmap_blk, last_fheader
;
siz = size(lun_filnam)
vtyp = siz(siz(0)+1)
if (vtyp eq 7) then begin
    if (not file_exist(lun_filnam(0))) then begin
	print, 'First filename in list does not exist.  Input: ', lun_filnam
	roadmap=0b
	return
    end
end
;
rd_fheader, lun_filnam, fheader, ndset
ndset_tot = total(ndset)
if (total(ndset) eq 0) then begin
    print, 'No datasets in all files'
    roadmap=0b
    return
end
if ((n_elements(roadmap) ne 0) and (n_elements(last_fheader) ne 0)) then begin
    qdiff = str_diff(fheader, last_fheader)
    if (total(ndset) ne n_elements(roadmap)) then qdiff = 1	;check that the existing array is the proper length
    filidx = -99
    for i=0L,n_elements(fheader)-1 do if (ndset(i) ne 0) then filidx = [filidx, intarr(ndset(i)) + i]
    filidx = filidx(1:*)
    if (not qdiff) then return			;the routine is being called twice with same files requested
end
;
hxt_struct
sxt_struct
gen_struct
bcs_struct
wbs_struct
att_struct
cba_struct
bsc_struct
;
nfiles = 1
if (siz(0) ne 0) then nfiles = siz(1)	;not a scalar
;
qfirst = 1
for ifil=0L,nfiles-1 do begin
    if (vtyp eq 7) then begin		;passed file name
	get_lun, lun
	openr, lun, lun_filnam(ifil), /block
    end else begin
	lun = lun_filnam(ifil)
    end
    ;
    rd_pointer, lun, pointer, recsize
    ;
    ndset0 = ndset(ifil)
    if (ndset0 eq 0) then goto, skip		;afterthought
    ;
    case strupcase(string(fheader(ifil).st$instrument)) of
	'ATT': roadmap0 = replicate({att_roadmap_rec}, ndset0)
	'CBA': roadmap0 = replicate({cba_roadmap_rec}, ndset0)

	'BCS': roadmap0 = replicate({bcs_roadmap_rec}, ndset0)
	'HXT': roadmap0 = replicate({hxt_roadmap_rec}, ndset0)
	'SXT': roadmap0 = replicate({sxt_roadmap_rec}, ndset0)
	'WBS': roadmap0 = replicate({wbs_roadmap_rec}, ndset0)

	else: begin
	    print, 'RD_ROADMAP: '
	    print, 'Invalid Instrument Code: ', strupcase(string(fheader(ifil).st$instrument))
	    stop
	end
    endcase
    if (strupcase(string(fheader(ifil).st$file_type)) eq 'HXI') then roadmap0 = replicate({hxi_roadmap_rec}, ndset0)
    if (strupcase(string(fheader(ifil).st$file_type)) eq 'SXL') then roadmap0 = replicate({sxl_39f1_roadmap_rec}, ndset0)
    if (strupcase(string(fheader(ifil).st$file_type)) eq 'BSC') then roadmap0 = replicate({bsc_roadmap_rec}, ndset0)
    ;
    ibyt = pointer.map_section
    rdwrt, 'R', lun, ibyt, 0, roadmap0
    ;
;    if (qfirst) then begin
;        qfirst = 0
;        roadmap = roadmap0
;        filidx = intarr(ndset0) + ifil          ;only one file being read
;    end else begin
;        ;roadmap = str_concat(roadmap, roadmap0)
;        roadmap = [roadmap, temporary(roadmap0)]        ;MDM changed 9-Jun-93 to speed things up
;        filidx = [filidx, intarr(ndset0) + ifil]
;    end

    ;---- MDM changed 15-Nov-94
    if (qfirst) then begin
	qfirst = 0
	iout = 0
	roadmap = replicate(roadmap0(0), ndset_tot)
	filidx =  intarr(ndset_tot)
    end
    ;
    roadmap(iout) = temporary(roadmap0)
    filidx(iout)  = intarr(ndset0) + ifil
    iout = iout + ndset0
    ;
    skip:
    if (vtyp eq 7) then free_lun, lun
end
;
if (nfiles eq 1) then ndset = ndset(0)	; turn it into a scalar
;
last_fheader = fheader
end
