pro conflx_binner,wave,wave_edges,bin_edges,dwave,bin_centers,$
	windex,wave4plot,edges=edges

;+
; NAME 
; 	conflx_binner 
; PURPOSE 
; 	To prepare wavelength and bin arrays for accurate use of CONFLX 
;	in MEWE_SPEC. 
;       If necessary, expand array of wavelength bins so no bin is larger
;       than 1 angstrom.  This is needed for accuracy with use of CONFLX 
;	in MEWE_SPEC because the continuum is only computed at the center 
;	of each bin and multiplied by the bin width.
; CALLING SEQUENCE
;	conflx_binner,wave,wave_edges,bin_edges,dwave,bin_centers,$
;	   windex,wave4plot,[/edges]
; INPUT
;	wave = 1-d array of wavelengths, either centers or edges.
; OUTPUT
;	wave_edges = wavelengths of the boundaries of the spectral
;	   bins used for output of flux values.  If wave is input as
;	   edges then wave_edges=wave.
;	bin_edges = the array of bin edges for computation of the continuum.
;	   If the input wave is edges and no bins are >1 then bin_edges=wave.
;	dwave =  the array of continuum bin widths for CONFLX calculation.
;	bin_centers = the array of wavelengths at which CONFLX computes
;	   the x-ray continuum.  If input wave is for bin centers and the
;	   spacing is not uniform then bin_centers will differ from wave.
;	windex, indices of elements of bin_edges which match elements of
;	   wave_edges.  This is needed for use in summing output of conflx 
;	   into dwave bins.
;	wave4plot, centers of bins defined by wave_edges, for use in plotting.
; OPTIONAL KEYWORD INPUT
;	/edges, specifies that the wave array are bin edges.
;	*** THIS IS RECOMMENDED FORMAT FOR WAVELENGTH BINS OF UNEQUAL WIDTH.
; ABSORPTION EDGES
;	wav_lim = [1.4,4.6,6.1,9.1,14.2,16.8,18.6,22.5,25.3,31.6,  $
;                  51.9,57.0,89.8,227.9,911.9] (Angstroms)
; HISTORY
;	12-Jun-96, LWA, written.
;	 5-Jul-99, LWA, modified to incorporate absorption edges.
;-

;Compute the wavelengths of the input bin edges and bin_widths.
dwave=wave(1:*)-wave        ;Initial difference array.

if NOT keyword_set(edges) then begin

   ;Case where _wave_ is for the bin centers.
   ;Outer edges of end bins only extend to first and last value of wave.
   ;Edges of interior bins are placed midway between the values of wave.
   ;For a wave vector with unequal spacing a new wave_centers vector
   ;will be calculated from these bin edges and will be different 
   ;from the input wave.

   wave_edges=wave(0:n_elements(wave)-2) + (dwave/2.)
   wave_edges=[wave(0),wave_edges(0:n_elements(wave)-2), $
	wave(n_elements(wave)-1)]

endif else begin
   wave_edges=wave
endelse

bin_edges=wave_edges

;Insert absorption edges into wavelength array.
wav_lim = [1.4,4.6,6.1,9.1,14.2,16.8,18.6,22.5,25.3,31.6,  $
	51.9,57.0,89.8,227.9,911.9]
bin_edges2=[bin_edges,wav_lim]
bin_edges2=bin_edges2(sort(bin_edges2))
ii=where(bin_edges2 ge min(bin_edges) and bin_edges2 le max(bin_edges))
abs_edges=where(bin_edges2(ii) ne bin_edges,bb)
bin_edges=bin_edges2(ii)
dwave=bin_edges(1:*)-bin_edges

;Insert additional sub-bins so that no wavelength bin is >1 A.
bigbin=where(dwave gt 1,nb)
if (nb gt 0) then begin
   for i=n_elements(bigbin)-1,0,-1 do begin
      nn=fix(dwave(bigbin(i)))
      if (dwave(bigbin(i))-nn eq 0) then nn=nn-1 else nn=nn
      insert=[bin_edges(bigbin(i))+findgen(nn+1)]
      if(bigbin(i)-1 ge 0) then begin
         bin_edges=[bin_edges(0:bigbin(i)-1),insert,bin_edges(bigbin(i)+1:*)]
      endif else begin
         bin_edges=[insert,bin_edges(bigbin(i)+1:*)]
      endelse
   endfor
endif
if (nb gt 0 or bb gt 0) then begin
   windex=get_indices(wave_edges,bin_edges) 
endif else begin
   windex=-1
endelse

dwave=bin_edges(1:*)-bin_edges
bin_centers=bin_edges(0:n_elements(bin_edges)-2) + (dwave/2.)
wave4plot=(wave_edges(1:*)+wave_edges)/2.0

end
