;
function spectral_ratio, cflux, ecflux=ecflux, epar=epar, drm=drm,  apar_in=apar_in, model=model, $
photon_edges=eph, wuse=wuse, nbin=nbin, mratio=mratio, par_out=par_out, $
stopit=stopit


checkvar, model, 'f_bpow'
apar_def =  [[1.,1.],[1.,7.],[max(eph)+[0.,0.]],[1.,1.],[min(eph)+[0.,0.]],[1,1]]
checkvar, apar_in, apar_def
checkvar, wuse, indgen(n_elements(cflux(*,0)))
checkvar, nbin, 50

;
;   Which is the non-linear parameter to vary?
;
ipar=(where(apar_in(0,*) ne apar_in(1,*), npar))(0)

if npar ne 1 then begin
    Print,'Error in Apar_in.  Choose 1 element to vary.'
    print,'Apar_in:', apar_in
    chkarg,'spectral_ratio',proc
    more,proc
    endif   
; It's range is:
par_range = apar_in(*,ipar) 

edge_products, eph, gmean=ephm, wid=weph 

drange = par_range(1)-par_range(0)
par_out = (findgen(nbin)+0.5)*(drange/nbin) + par_range(0)

;
; Check whether MRATIO needs recalculating
;

common spectral_ratio_com, spec_rat_str

test = datatype(spec_rat_str) eq 'STC'
if test then $
test = match_struct(exclude=[0], spec_rat_str, $
{mratio: spec_rat_str.mratio, par_out: par_out, drm:drm(wuse,*), eph:eph,  wuse: wuse, nbin:nbin, apar_in:apar_in})


if not test then begin
    
    apar_use = (apar_in(0,*))(*)#(fltarr(1,nbin)+1.)
    apar_use(ipar,*) = par_out
    apar_use(ipar-1>0,*) = 1.0
    out = fcount_rate( dummy_val, apar_use, eph, drm, wuse=wuse, model=model)
    mratio = f_div( out(0:n_elements(wuse)-2,*),out(1:*,*) )
    
    
    spec_rat_str = {mratio: mratio, par_out: par_out, drm:drm(wuse,*), eph:eph, wuse: wuse, nbin:nbin, apar_in:apar_in}
    
    endif else begin
    mratio = spec_rat_str.mratio
    apar_use = (apar_in(0,*))(*)#(fltarr(1,nbin)+1.)
    apar_use(ipar-1>0,*) = 1.0
    endelse
;
;array of results, norm and param, each ratio, each time
do_errors =  n_elements(ecflux) eq n_elements(cflux) 

out_vals = reform( fltarr( 2, n_elements(cflux(wuse,0))-1, n_elements(cflux(0,*)) ), $
2, n_elements(cflux(wuse,0))-1, n_elements(cflux(0,*)))
if do_errors then epar     = out_vals

for i=1, n_elements(wuse) -1 do begin
    oratio = f_div( cflux(wuse(i-1),*), cflux(wuse(i),*))
    if do_errors then begin
        sratio = oratio * sqrt( f_div(ecflux(wuse(i-1),*)^2,cflux(wuse(i-1),*)^2 + $
        f_div(ecflux(wuse(i),*)^2,cflux(wuse(i),*)^2 )))
        
        endif
    wpos = where( oratio gt 0.0, npos)

    if npos gt 0 then begin
        out_vals(1,i-1,wpos) = interpol( par_out, (mratio(i-1,*))(*), oratio(wpos) )
        
        if do_errors then $
        epar(1,i-1,wpos) = ((interpol( par_out, (mratio(i-1,*))(*), 1.01*oratio(wpos) ) - $
        out_vals(1,i-1,wpos)) * 100.)(*) * sratio(wpos) 
        
        apar_use(ipar,wpos) = out_vals(1,i-1,wpos)
        out_vals(0,i-1,wpos) = f_div( cflux( wuse(i-1), wpos), $
        fcount_rate( dummy_val, apar_use(*,wpos), eph, drm, wuse=wuse(i-1)) )
        endif
    if do_errors then epar(0,i-1,*) = out_vals(0,i-1,*) * f_div(sratio,oratio)

    endfor

if keyword_set(stopit) then stop
if exist(epar) then epar = reform( epar )

return, reform(out_vals)
end