   pro rdspc2_data, file, chan, spcnum, pointr, $
           a, time, npts, spectrum, velfit, primary, found 
;+
; IDL v.2
;
; For a required channel CHAN and a required spectrum number SPCNUM, 
; reads the roadmap, fit results and the fitted theory spectrum from
; SPC file by direct access method. 
; On each call SPC file is opened and closed
;
;input:
;     file = ' '         ; filename to be read
;     chan = long(0)     ; required channel number of the spectrum to be read
;     spcnum = long(0)   ; required number of the spectrum to be read
;     pointr = lonarr(4) ; pointers to records containing roadmaps for records
;                          for each channel. RDSPC_HEAD should be run 
;                          first to get it
;
;
;output:
;	time                  time of the spectrum (?)
;	npts                  number of bins in the spectrm (?)
;	spectrum              total fitted spectrum (?)
;	velfit		      velocity component if velfit > 0  (?)
;	primary		      primary component if velfit > 0    (?)
;	found 	              error flag ? 10 if sucessful -10 if not (?)
;
; Subroutines required:
; RDSPC_HEAD must be run once before calling RDSPC_DATA
;
; Written:
;     rdbsd2_data.pro atp.
;     rewritten as rdspc2_data AF, May-1991
;     nrecs shift corrected    AF, Nov 1991
;-


; open the file for direct access
;
   get_lun,lun
   openr,lun,file   ;       open lun
;;;   nrecs = spcnum/16 
   nrecs = (spcnum-1)/16 
   rec = pointr(chan-1) + nrecs    ; work out road map record (-1 idl
                                   ; starts at zero not one) 
   thisrec = assoc(lun,lonarr(16),rec-1)   ;   alloc record (-1 for idl)
   rdmap = thisrec(0)   ;         read record
   spcrec = rdmap(spcnum-1-nrecs*16)-1   ; (spcnum -1 for subs) -1 for
                                         ; assoc find record for our spectrum 

if (spcrec gt 0) then begin     ; theory spectrum for spcnum is in the 
                                ; file
   found = 10

;
;   read record one of the SPCNUM data (ref pg 10-49 idl manual)
;
   thisrec = assoc(lun,bytarr(64),spcrec)   ;   alloc record
   dumm = thisrec(0)

;;     >       CHAN, SPCNUM, NPTS, (TIM2(I), I=1,7), SUMDGI, WO, DW, 
;;     >       KMAX, KIT, NFREE

   parmax = 8   ; maximum number of fitted parameters

   time = lonarr(7)         ; time of the spectrum
   a = dblarr(parmax)       ; fitted parameters: Te, Td, Tdk, back, EM, EM2,
                            ; upflow velcity
   sa = dblarr(parmax)      ; uncertainties of parameters
   spectrum = fltarr(256)   ; total fitted spectrum
   primary = fltarr(256)    ; primary component if velfit > 0

   rchan = long(dumm,0)     ; channel number
   rspcnum = long(dumm,4)   ; spectrum number
   npts  = long(dumm,8)     ; number of bins in the spectrum
   time(0) = long(dumm,12)  ; time of the spectrum: hh,mm,ss,ms,d,m,y
   time(1) = long(dumm,16)   
   time(2) = long(dumm,20)   
   time(3) = long(dumm,24)   
   time(4) = long(dumm,28)   
   time(5) = long(dumm,32)   
   time(6) = long(dumm,36)   
   sumdgi = float(dumm,40)   ; integration time
   wo     = float(dumm,44)   ; wavelength offset
   dw     = float(dumm,48)   ; dispersion
   kmax   = long(dumm,52)    ; max number of BSDFIT iterations
   kit = long(dumm,56)       ; actual number o BSDFIT iterations
   nfree = long(dumm,60)     ; No. of degrees of freedom
;
;
   spcrec = spcrec + 1   ;          increment record pointer


   if ( chan ne rchan ) then begin 
      print, ' ERROR IN RDSPC_DATA: '
      print, ' CHANNEL REQUIRED = ',chan
      print, ' CHANNEL READ = ',rchan
      stop
   endif

   if ( spcnum ne rspcnum) then begin
      print, ' ERROR IN RDSPC_DATA: '
      print, ' SPECTRUM REQUIRED = ',spcnum
      print, ' SEPCTRUM READ = ',rspcnum
      stop
   endif

;
;   read record two of the SPCNUM data
;
   thisrec = assoc(lun,bytarr(64),spcrec)   ;   alloc record
   dumm = thisrec(0)

;;     >      (A(I),I=1,PARMAX)   double precision, parmax=8

   for i=0,parmax-1 do begin
      a(i) = double(dumm,i*8)   ; fitted parameters
   endfor

   spcrec = spcrec + 1   ;          increment record pointer
;
;   read record three of the SPCNUM data
;
   thisrec = assoc(lun,bytarr(64),spcrec)   ;   alloc record
   dumm = thisrec(0)

;;     >      RCHI, IER, VELFIT, BLANK(1:52)

   rchi = float(dumm,0)   ; reduced chi-square
   ier = long(dumm,4)     ; IER from Curfith
   velfit = long(dumm,8)  ; velocity component if velfit > 0


   spcrec = spcrec + 1   ;          increment record pointer
;
;   read record four of the SPCNUM data
;
   thisrec = assoc(lun,bytarr(64),spcrec)   ;   alloc record
   dumm = thisrec(0)

;;     >          (SA(I),I=1,PARMAX)

   for i=0,parmax-1 do begin
      sa(i) = double(dumm,i*8)   ; uncertainties
   endfor

;
;   read records with the total theory spectrum
;
;   calcuate the number of records required by the spectrum 
;
   nrecs = npts/16;
   nleft = npts - nrecs*16
;
;   read countrates values
;
   spcrec = spcrec + 1

   if (nleft gt 0) then begin
      thisrec = assoc(lun,fltarr(16,nrecs+1),spcrec)
   endif else begin
      thisrec = assoc(lun,fltarr(16,nrecs),spcrec)       
   endelse

   dumm = thisrec(0)
   for i = 0,nrecs-1 do begin
      spectrum(i*16) = dumm(*,i)   ;    read line from lun
   endfor

   spcrec = spcrec + nrecs

   if (nleft gt 0) then begin
      spectrum(nrecs*16) = dumm(0:nleft-1,nrecs)   ; read extra few

      spcrec = spcrec + 1
   endif

   spectrum = spectrum(0:npts-1)


;  Read records with the primary component if two-component fit

   if (velfit gt 0) then begin    

      if (nleft gt 0) then begin
         thisrec = assoc(lun,fltarr(16,nrecs+1),spcrec)
      endif else begin
         thisrec = assoc(lun,fltarr(16,nrecs),spcrec)         
      endelse

      dumm = thisrec(0)
      for i = 0,nrecs-1 do begin
         primary(i*16) = dumm(*,i)   ;    read line from lun
      endfor

      spcrec = spcrec + nrecs 

      if (nleft gt 0) then begin
         primary(nrecs*16) = dumm(0:nleft-1,nrecs)  ; read extra few

         spcrec = spcrec + 1
      endif

      primary = primary(0:npts-1)

   endif   ; velfit gt 0

 endif else begin   ; if (spcrec gt 0)

   found = -10 

 endelse


   close,lun
   free_lun,lun
   end      


