pro rd_bsd_rdmap, lun_filnam, header, roadmap
;+
;	NAME:
;		rd_bsd_rdmap v0.9
;
;	PURPOSE:
;		Read the roadmap section of a BSD file
;
;	CALLING SEQUENCE:
;		rd_bsd_rdmap, lun_filnam, header, roadmap
;
;	INPUT:
;		lun_filnam	input file specification or the
;				unit number of an opened BSD file
;
;               Header 		The BSD_header record for the current file.
;				(see rd_bsd_header.pro)
;
;	OUTPUT:
;		roadmap   	BSD_roadmap structure. see bsd_struct.pro for 
;				details.
;
;	HISTORY:
;		JTM: (24 Oct 91)
; 		atp: march 1992 - mods for new bsd structures/cleanup
;		atp: (7/7/92) v0.9 - minor fixes, header, allow different 
;				     roadmap sizes for each channel.
;-
;
; open the bsd file if not already open
;
bsd_struct
siz = size(lun_filnam)
vtyp = siz(siz(0) + 1)
;
if (vtyp eq 7) then begin	;passed file name
	get_lun, lun
	openr, lun, lun_filnam, /block
endif else begin
	lun = lun_filnam
endelse
;
; get pointing info from header
;
if (header.numchn(0) gt 0) then p0 = header.numchn(0) else p0 = 0
if (header.numchn(1) gt 0) then p1 = header.numchn(1) else p1 = 0
if (header.numchn(2) gt 0) then p2 = header.numchn(2) else p2 = 0
if (header.numchn(3) gt 0) then p3 = header.numchn(3) else p3 = 0

pall = [p0,p1,p2,p3]

n_rdmap = max(pall)

if (n_rdmap eq 0) then begin
    print,'bsd_rdmap: There appear to be no data records here'
    stop
    endif
;
; create roadmap array
;
roadmap = replicate({bsd_road},n_rdmap)
;
; read by channel - initial hack is to use a for loop 
; 
roadmap(*).offset(*) = 0
roadmap(*).exist(*) = 0

for ii = 0,3 do begin
  if pall(ii) gt 0 then begin
   pointer = header.ptrdmp(ii) -1
   point_lun, lun, pointer*64L
   temp = lonarr(pall(ii))
   readu, lun, temp
   roadmap(0:(header.numchn(ii)-1)).offset(ii) = temp
   endif
  roadmap(*).exist(ii) = (roadmap(*).offset(ii) ne 0)
  endfor

if (vtyp eq 7) then free_lun, lun

end

