pro bcs_fixbpc,filename,novelocity=novelocity,channels=channels,bpc_data=bpc_data, $ 
 		out=out,reson=reson,spectra=spectra 
;+
; NAME:			BCS_FIXBPC
;
; PURPOSE:		Fix bpc files for 1) missing spectra
;					  2) missing 2 components
;						
; CALLING SEQUENCE:  	bcs_fixbpc[,filename],/novel,channels='2-3', 
;			 bpc_data=bpc_data,out='output_file.name',reson=103.98
;
; INPUTS:			 -
;
; OPTIONAL INPUTS:	filename - bpc filename to fix. Prompted if not given.
;			novel	 - Don't force a second comp velocity 
;			channels - channels to do, either string, '1-3','*'
;				   or array [1,2,3,4], default is '2,3'
;				   See RESTRICTIONS.
;			bpc_data - A structure of type bpc_struct is returned.
;				   This contains the bpc_data.
;			out	 - An alternative output filename instead of
;				   'filename'+'_fixed'
;			reson	 - A scalar value, in bins, for the resonance
;				   line position.
;			spectra  - Only fix following spectra 
;				   spectra = [1,2,3, .... ,34]
;				   A large number of spectra is advised,
;				   or else the program will have difficulty 
;				   matching missing spectra.
;			
; OUTPUTS:		A file is written with the fixed bpc data in it.
;
; OPTIONAL OUTPUTS:	bpc_data can be returned
;
; RESTRICTIONS:		This only really works for CaXIX and FeXXV channels,
;			bpc_struct is homebrew not part of mons *_STRUCT
;			hierarchy. 
;			Written on a VAX. - ie. not tested under your
;			  operating system.
;
; PROCEDURE:		1)  Use nearest neighbour search to fill in 
;			    missing spectra. 
;			2)  if wanted, force second components to resonance 
;			    lines. 
;			3)  check all spectra for sensible numbers and/ratios
;
; MODIFICATION HISTORY: Chopped up ed_bpc  ATP aug 1993
;
;-
;
;	if !have read file name
;
if (n_params() eq 0) then begin
	bcs_getfile,filename,defstr="bpc*.*"
	if (filename eq '-1') then return
	endif
;
;	break it up + read file
;
break_file,filename,dsk,dir,file,ext,ver
filename = dsk+dir+file+ext
rd_bpc,filename,bpc_data,nspec
;
; 	do other keywords	
;
	if (keyword_Set(channels)) then begin
		fred_S = size(channels)
		if (fred_s(fred_s(0)+1) eq 7) then begin ; string
		   range,fred_S,4,chans,retval
		   if (retval gt 0) then begin 
			print,'Channel out of bounds'
			return
			endif
		   endif else begin 
		   if ((max(channels) gt 4) or (min(channels) lt 1)) then begin 
			print,'Channel out of Bounds'
			return
			endif
		   chans = channels
		   endelse
		endif else chans = [2,3]
;
	if (keyword_set(out)) then outfilename=out else begin
		outfilename = dsk+dir+file+ext+'_fixed'
		endelse
;
	if (keyword_set(reson)) then begin
		restype = size(reson)
		if (restype(restype(0)+1) le 5) then resvalue=reson else begin
		    resvalue = float(reson)
		    endelse
		endif
	if (keyword_set(spectra)) then begin
		selspec = spectra-1
		if n_elements(selspec) le 1 then begin
		  print,'Spectra needs to be specified as an array of more than one element.'
		  print,'  minimum is 1, maximum is ',nspec
		  return
		  endif
		ff = where(selspec ge nspec or selspec lt 0,kkount)		
		if (kkount gt 0) then begin
		  print,'Some of the spectra specified are out of bounds'
		  print,'Spectra in this file range from 1 to ',nspec
		  return
		  endif
		endif else begin
		  selspec = indgen(nspec)
		endelse
;
;  check out data
;
flag = bytarr(4)
flag(*) = 0
realch = total(reform(bpc_data(*,*).options(*,0)),1)
realch = total(realch,2)
got_data = where(realch gt 0,kount)
;
if (kount le 0) then begin
   print,'Cannot correct this file, apparently no data in it'
   return
   endif
;
flag(got_data) = 1
;
;  call routines in loop
;

  nchans = n_elements(chans)
  for ii = 0,nchans-1 do begin
	cchan = chans(ii)
	if (flag(cchan-1) eq 1) then begin
;
;	 	fill 
;
	bcs_bpcfs,bpc_data,cchan,selspec
;	
;		if needed do velocity
; 
	if (not keyword_set(novelocity)) then begin
	      bcs_bpc2c,bpc_data,cchan,selspec
	      endif
;
;		if reson flag then set resonance line pos
;
	if (keyword_set(reson)) then begin
	   bpc_data(cchan-1,selspec).position(0,0) = res_value
	   bpc_data(cchan-1,selspec).position(1,0) = res_value; acc for 2cmpt 
	   endif
;
;		check 
;	
	bcs_bpcckspec,bpc_data,cchan,selspec
;
;		end loop
;
	 endif ; /* no data in channel (flag)*/
	endfor
;
;		write out result
;
     wr_bpc,outfilename,bpc_data,nspec
;
;		end
;
end
