function returntemplate=iterative_curve_fit_singledecay(event,seed_vals,fit_latency_window,return_pareddown_fitinfo,plot_fits,plot_fit_residuals)
%description: used to make single exponent rise, single exponent decay fits of extracted synaptic current waveforms
%called by: cluster_asynch_based_on_phasic, phasic_and_asynchronous_event_extractor_core
%calls: monoexprisedecay_template_generator
%notes: also called from the command line with manually selected events to get the best values for a template to be used in detection
    %'old' template seeds are as follows:
    %%[14.16371820548567  11.31185487577630  19.35826590564763... amp_scaler, tau_rise and tau_decay averaged from one 'good' event from each of patch20070319T125639 and patch20070319T131951
    %%[10.431819085631   12.507638858663   159.893180756647... amp_scaler, tau_rise and tau_decay from the slow event after the second AP, trial 5, patch20070319T125639
%code confirmed date: feb 4, 2013

trial=event(1);whichAP=event(2);releventtime=event(3);timerelfirstAP=event(4);critval=event(5);abseventtime=event(6);eventwasslopecorrected=event(7);event=event(8:end);datalen=length(event);
lower_bounds=[2 3 6 -30 fit_latency_window(1)];
latency_limit=fit_latency_window(end);    %a for loop is used with latency values, rather than just finding the latency by fitting, as experience shows this greatly improves fit quality
upper_bounds=[70 100 500 30 latency_limit];
optionz=optimset('display','off');
[best_fitted_vals,best_resnorm,best_residual]=lsqcurvefit(@monoexprisedecay_template_generator,seed_vals,[1:datalen],event,lower_bounds,upper_bounds,optionz);

for latency_value=1:1:latency_limit
    [fitted_vals,resnorm,residual]=lsqcurvefit(@monoexprisedecay_template_generator,[seed_vals(1:4) latency_value],[1:datalen],event,[lower_bounds(1:4) latency_value-1],[upper_bounds(1:4) latency_value+1],optionz);
    if resnorm<best_resnorm,best_fitted_vals=fitted_vals;best_residual=residual;best_resnorm=resnorm;end
end

if return_pareddown_fitinfo
    puretemplate=monoexprisedecay_template_generator([best_fitted_vals(1:3) 0 0],[1:datalen]);
    [minval,minind]=min(puretemplate);
    fitted_PSC=minval;
    rise_time=minind;
    returntemplate=[0 puretemplate rise_time fitted_PSC];
else
    puretemplate=monoexprisedecay_template_generator(best_fitted_vals,[1:datalen]);
    [minval,minind]=min(puretemplate);
    fitted_PSC=minval-best_fitted_vals(4);
    rise_time=minind-best_fitted_vals(5);
    fitted_charge=sum(puretemplate-best_fitted_vals(4));
    fitted_values=[best_fitted_vals rise_time fitted_PSC fitted_charge best_resnorm];
    returntemplate=[puretemplate fitted_values];
end
if plot_fits,figure,plot(event,'k','linewidth',1),hold on, plot(puretemplate,'r','linewidth',1)
    if plot_fit_residuals,plot(abs(best_residual),'o','markersize',4,'markerfacecolor','k','markeredgecolor','k'),end
    ylabel(strcat('rise time',num2str(rise_time)))
    xlabel(strcat('trial',num2str(trial),'whichAP',num2str(whichAP),'releventtime',num2str(releventtime),'timerelfirstAP',num2str(timerelfirstAP),'critval',num2str(critval),...
        'abseventtime',num2str(abseventtime),'slopecrct',num2str(eventwasslopecorrected)))
end
