pro rd_spc_data,lun_filename,dset_arr,header,rdmap,index,data,channel,data_only=data_only
;+
; NAME:			rd_spc_data v0.9
;
; PURPOSE:		read spc data block + return in structure
;
; CALLING SEQUENCE:     rd_spc_header
;			rd_spc_rdmap
;			rd_spc_data
;
; INPUTS:		lun_filename - Logical unit number or filename.
;			dset_arr - dataset array for corresponding bsd file. 
;			header - header structure - see rd_spc_header
;			rdmap  - roadmap structure - see rd_spc_rdmap
;			/data_only - return only those data elements present
;			 in the SPC file. Id est don't return a data structure
;			 the same as the corresponding BSD file.
;
; OPTIONAL INPUTS:	channels - array of channel numbers (*) default.
;
; OUTPUTS:		data   - data structure. 
;			index  - data index structure.
;			 For structure definitions see file spc_struct.pro
;
; OPTIONAL OUTPUTS:  	none
;
; RESTRICTIONS:		Filename must match that in calls to rd_spc_header
;			 and rd_spc_rdmap
;			Dataset numbers correspond to equivalents in parent
$			 BSD file unless /data_only flag is used.
;			 Datasets without fits are blank as well as being
;			 indicated by roadmap.exist = 0
;
; PROCEDURE:		read record at a time, insert into structure, create
;			wavelength array.
;
; MODIFICATION HISTORY: written atp 5/92 from jtm's rd_bsd.pro
;			modified to account for uncertainties/internal time 
;			errors atp may92
;			modified atp july 92 for spc files
;			minor mods debug atp (7/7/92) v0.9
;-
spc_struct
;
siz = size(lun_filename)
vtyp = siz(siz(0) + 1)
;
if (vtyp eq 7) then begin	;passed file name
	get_lun, lun
	openr, lun, lun_filename, /block
endif else begin
	lun = lun_filename
endelse
;
if (n_elements(channel) eq 0) then channel = [1,2,3,4];

nchan = n_elements(channel);

nset  = n_elements(dset_arr);
npts  = header.numbsd(channel-1);
npts  = max(npts);
numdat = min([npts,nset]);
index = replicate({spc_r_h},nchan,numdat);
data  = replicate({spc_data},nchan,numdat);
;/* agreed order was - chan,idset */
temph = {spc_r_h};

for j=0,nchan-1 do begin
 ichan = channel(j) -1; 
 for i=0, numdat-1 do begin
  idset = dset_arr(i) - 1;
  if (rdmap(idset).exist(ichan) eq 1) then begin 
   point_lun,lun, (rdmap(idset).offset(ichan) - 1) *64L;
   readu,lun,temph;
   index(j,i) = temph;
   nrec = ((index(j,i).nbinssp/16)+((index(j,i).nbinssp mod 16) gt 0))
   tdat = fltarr((nrec)*16);
   readu, lun, tdat;
   data(j,i).counts = tdat(0:(index(j,i).nbinssp-1));
   ;
   ; if index.velfit is 1 then read the primary component
   ;
   if (index(j,i).velfit ge 1) then begin
      readu, lun, tdat;
      data(j,i).primary = tdat(0:(index(j,i).nbinssp-1));
      endif
   endif
  endfor
endfor
;
; subtract 1900 from time arrays if in that format.
;
if (index(0,0).time(6) gt 1000) then index(*,*).time(6) = index(*,*).time(6)-1900
;
; check to see if we need to collapse the data structures.
;

if (keyword_set(data_only)) then begin
    maxdat = max(header.numthe(*))
    new_index = replicate({spc_r_h},nchan,maxdat)
    new_data  = replicate({spc_data},nchan,maxdat)
    for ii=0,nchan-1 do begin
	ichan = channel(ii) - 1
	fred = where(rdmap(*).exist(ichan) eq 1)
	ntodo = n_elements(fred)
	if (total(fred) ne -1) then begin
		new_index(ichan,0:ntodo-1) = index(ichan,fred)
		new_data(ichan,0:ntodo-1) = data(ichan,fred)
		endif ;/* valid amount of data to compact */
	endfor; /* channel loop */
    index = new_index
    data  = new_data
    new_index = 0
    new_data = 0
    endif; /* need to compact arrays */
;
; finish
;	
close,lun
free_lun,lun

end
