pro rd_bpc,bpc_fil,data,spec_max
;+
;  procedure to read a BPC file and load the results into a structure
;  for easy access for plotting etc
;
;  The structure definition is no longer.......
;
; ARRAY(chan,spec).QUALITY(option,line)
; ARRAY(chan,spec).INTENSITY(option,line)
; ARRAY(chan,spec).IERR(option,line)
; ARRAY(chan,spec).WIDTH(option,line)
; ARRAY(chan,spec).WERR(option,line)
; ARRAY(chan,spec).POSITION(option,line)
; ARRAY(chan,spec).PERR(option,line)
; ARRAY(chan,spec).BACK1(option,line)
; ARRAY(chan,spec).BACK2(option,line)
; ARRAY(chan,spec).TIME(6)
; ARRAY(chan,spec).WO(3)
; ARRAY(chan,spec).DW
; ARRAY(chan,spec).NBACK
; ARRAY(chan,spec).NOPT_USED
; ARRAY(chan,spec).TOTAL
; ARRAY(chan,spec).SDGI
;
; Use:   First note that all counters start at zero!
;        Produce a BPC file by using MKBSD, BSDCAL then
;
;        idl>  rd_bpc,array,'bpc file name'
;
;        idl> plot,array(3,*).intensity(1,2)   would then plot all the
;                                              intensities derived for the
;                                              3rd line in the second fit
;                                              option of the SXV channel - 
;                                              if such a beast were to exist.
;
;
;     CDP  Nov 91
;
;-
;  Initialise arrays and data types
;
dum_i = intarr(6)
dum_r = fltarr(6)
nopt = 0
nback = 0
lines = 0
opt = 0
nopt = 0
c = 0
s = 0
spec_max = 0
spec_min = 10000
text = '                                         '
optnam = ' '
;
;  was file data array name given?
;
if n_params() lt 1 then begin
   print,' '
   print,' You must call rd_bpc with at least the data array name'
   print,' as the first parameter.'
   print,' '
   return
endif

;
;  was the file name given?
;
if n_params() lt 2 then begin
   bpc_fil = '       '
   read,'Give the BPC file name - prefix is optional: ',bpc_fil
endif
;
;  check for BPC prefix
;
;bpc_fil = strupcase(bpc_fil)
;if strpos(bpc_fil,'bpc') eq -1 then begin
;   bpc_fil = 'bpc' + bpc_fil
;endif
;
;  call structure definition
;
bpc_struct,bpc_param=data

;
;  get and open the file
;
get_lun,luny
openr,luny,bpc_fil
;
;  run through it to pick up size of array needed (from max no. of spectra)
;
on_ioerror, repeat1
while eof(luny) ne 1 do begin
   repeat1:
   readf,luny,text
   if strpos(text,'Spectrum') ne -1 then begin
      readf,luny,chan,spect
      if spect gt spec_max then spec_max = spect
      if spect lt spec_min then spec_min = spect
   endif
endwhile
close,luny

;
;  replicate basic structure into an array of structures
;
data = replicate(data,4,spec_max)


;
;  open file again and read
;
openr,luny,bpc_fil

text = '                                          '
while strpos(text,'Channel') eq -1 do readf,luny,text
text = '                                          '

;
;  read header line for each spectrum
;
while eof(luny) ne 1 do begin
  on_ioerror, repeat2
  readf,luny,c,s,dum_i,dum_r,nopt,nback
   c = c - 1
   s = s - 1
   data(c,s).time       = dum_i
   data(c,s).dw         = dum_r(0)
   data(c,s).wo         = dum_r(1:3)
   data(c,s).sdgi       = dum_r(4)
   data(c,s).total      = dum_r(5)
   data(c,s).nopt_used  = nopt
   data(c,s).nback 	= nback
;
; skip other header lines 
;
   readf,luny,text
   readf,luny,text   
   readf,luny,text

;
;  loop through the options used for this spectrum
;
   ocounter = 0
   for i=1,nopt do begin
      on_ioerror, skip1
      if nback eq 1 then begin
         readf,luny,opt,qual,lines,bst,bend,rchi,$
               inten,ierr,wid,widerr,pos,poserr,b1,be,optnam
      endif else begin
         readf,luny,opt,qual,lines,bst,bend,rchi,$
               inten,ierr,wid,widerr,pos,poserr,b1,be,b2,b2e,optnam
      endelse

      o = opt - 1
      l = lines - 1
      data(c,s).options(ocounter,0) = opt
      data(c,s).nlines(o,0) = lines
      data(c,s).binst(o,0) = bst
      data(c,s).binend(o,0) = bend
      data(c,s).rchisqr(o,0) = rchi
      data(c,s).optname(o) = optnam
      data(c,s).intensity(o,0) = inten
      data(c,s).ierr(o,0) = ierr
      data(c,s).width(o,0) = wid
      data(c,s).werr(o,0) =  widerr
      data(c,s).position(o,0) = pos
      data(c,s).perr(o,0) = poserr
      data(c,s).back1(o,0) = b1
      data(c,s).back1err(o,0) = be
      if nback gt 1 then begin
         data(c,s).back2(o,0) = b2
         data(c,s).back2err(o,0) = b2e
      endif else begin
         data(c,s).back2(o,0) = 0.0
	 data(c,s).back2err(o,0) = 0.0
      endelse
      data(c,s).quality(o,0) = qual
      skip1:
;
; are there other lines in this option?
;
      ocounter = ocounter + 1
      if lines gt 1 then begin
         for j = 1,lines-1 do begin
            on_ioerror, skip2
            readf,luny,dum_r
            data(c,s).intensity(o,j) = dum_r(0)
            data(c,s).ierr(o,j)      = dum_r(1)
            data(c,s).width(o,j)     = dum_r(2)
            data(c,s).werr(o,j)      = dum_r(3)
            data(c,s).position(o,j)  = dum_r(4)
            data(c,s).perr(o,j)      = dum_r(5)
            data(c,s).back1(o,j)     = 0.0
            data(c,s).back1err(o,j)     = 0.0
            data(c,s).back2(o,j)     = 0.0
            data(c,s).back2err(o,j)     = 0.0
            data(c,s).quality(o,j)   = qual
            skip2:
         endfor
      endif
   endfor

;
;  search for beginning of next spectrum
;
   repeat2:
   while strpos(text,'Channel') eq -1 and eof(luny) ne 1 do readf,luny,text
   text = '                                          '
endwhile

;
; tidy up file access
;
close,luny
free_lun,luny

;
;  print information (guarenteed to confuse with the unit offset!!!!!)
;
print,' '
print,' Data array has a total of ',spec_max,' spectral slots',$
        format='(a,i3,a)'
print,' The first non-empty slot is for spectrum #',spec_min,format='(a,i3,a)'
end
