pro fix_bsd_header,bsd_header,bsd_index,bsd_data
;
;+
; NAME: 		Fix_bsd_header v1.2
;
; PURPOSE:		rationalise bsd header structures
;
; CALLING SEQUENCE:     fix_bsd_header,bsd_header,bsd_index,bsd_data
;
; INPUTS:		bsd_header - header structure to be rationalised
;			bsd_index  - index structure
;			bsd_data   - data structure
;
; OUTPUTS:		rationalised bsd_header
;
; OPTIONAL OUTPUTS:	none
;
; RESTRICTIONS:		The Pointer arrays are **NOT** calculated. Use of
;			the header block with any program other than one
;			that explicitly calculates the roadmap ab initio,
;			(eg wr_bsd.pro) will cause unpleasantness.
;
; PROCEDURE:		scan index structures and data structure + use to 
;			regenerate header
;
; MODIFICATION HISTORY: written 4/7/92 atp mssl v1.0,1.1,1.2
;
;-
;

new_header = {bsd_h}; /* define new header.block */
;
; first find the number of spectra by looking at structure of bsd_index
; and do error checks
;
e_index_mismatch = 0
e_chans = 0
e_nostruct = 0

bsd_i_siz = size(bsd_index)
bsd_d_siz = size(bsd_data)
;
; check ndims bad code I know
;
nidims = bsd_i_siz(0)
nddims = bsd_d_siz(0)

if (nidims ne nddims) then e_index_mismatch = 1 
if (nidims eq 0) then e_chans = 1
if (nidims gt 2) then e_chans = 1


if (nidims eq 2) then begin
; we have more than one channel present.
 nchans = bsd_i_siz(1)
 if (nchans ne bsd_d_siz(1)) then begin
	print,'fbh:Mismatch between number of channels in index + data blocks'
	e_index_mismatch = 1
	endif

 nspec = bsd_i_siz(2)
 if (nspec ne bsd_d_siz(2)) then begin
	print,'fbh:Mismatch between number of spectra in index + data blocks'
	e_index_mismatch=1
	endif

 endif else begin 
; we have one channel
 nchans = 1
 nspec = bsd_i_siz(1)
 if (nspec ne bsd_d_siz(2)) then begin
	print,'fbh:Mismatch between number of spectra in index + data blocks'
	e_index_mismatch=1
	endif
 endelse

 if (bsd_i_siz(nidims+1) ne 8) then begin
  ; not a structure
  e_nostruct = 1
  endif

 if (bsd_d_siz(nddims+1) ne 8) then begin
  ; not a structure
  e_nostruct = 1
  endif
;
; do errors + return if necessary
; 
 if (e_index_mismatch) then  begin 
     print,'fbh:Index Data mismatch'
     return
     endif
 if (e_chans) then begin
     print,'fbh:Channel error'
     return
     endif
 if (e_nostruct) then begin
     print,'fbh:Not Structures'
     return
     endif
;
; now work out numchn and totspc
; 
 fred = reform(bsd_index)
 chanarr = lonarr(nchans)

 for ii=1,nchans do chanarr(ii-1) = fred(ii-1).chan
;
; initially assume all valid chans have same amount of data
; 
 new_header.numchn(*)  =  0
 new_header.numchn(chanarr-1) = nspec 
 new_header.totspc = total(new_header.numchn)
 new_header.ptrdmp = [0,0,0,0] ; this is reset by wr_bsd
 ;
 new_header.floval = 0 ; dont know what to do about this
 new_header.floset(*) = 0; "
 new_header.flcorr = 0; "

 if (nchans eq 1) then begin
    maxcrate = max(bsd_index(*).crate,max_subs)
    achan = chanarr(0)
    new_header.peak(*) = 0
    new_header.peak(achan-1) = maxcrate
    new_header.pknum(*) = 0
    new_header.pknum(achan-1) = max_subs + 1
    endif else begin
     new_header.peak(*) = 0
     new_header.pknum(*) = 0
    for ii = 1, nchans do begin 
     maxcrate = max(bsd_index(ii-1,*).crate,max_subs)
     achan = chanarr(ii-1)
     new_header.peak(achan-1) = maxcrate
     new_header.pknum(achan-1) = max_subs + 1
     endfor
    endelse
;
;  put vernum = 1 4/july/92
;
    new_header.vernum = 1

    tmparr = int2secarr(reform(bsd_index(0,*).time))
    
    fred =  max(tmparr,subs)

    new_header.tdl = bsd_index(0,subs).time

    fred = min(tmparr,subs)

    new_header.tdf = bsd_index(0,subs).time
;
;  make sure all the spare arrays hold spaces
;
    new_header.spare1(*) = 32
    new_header.spare2(*) = 32
    new_header.spare3(*) = 32
;
;  now stick new - > old
;

    bsd_header = new_header

    return
end

