function analyze_hxw_data(baseline_filename)

tic
close all
induction_filename=strcat(baseline_filename,'s')
postinduction_filename=strcat(baseline_filename,'r')
% basic_pathname='C:\Documents and Settings\david nauen\Desktop\raw_data_hxw\';
basic_pathname='\\bilab\Archive\NCC1701cBKUP\Data\';
baseline_data=load(strcat(basic_pathname,baseline_filename));
baseline_data=baseline_data(:,4:end); %removing the first four columns since they only have sample number and time base and something else

postinduction_data=load(strcat(basic_pathname,postinduction_filename));
postinduction_data=postinduction_data(:,4:end);

realtime_data=load(strcat(basic_pathname,induction_filename));
realtime_data=realtime_data(:,4:end);

figure(1)
subplot(2,2,1:2),plot(baseline_data(:,1:2))
subplot(2,2,3:4),plot(postinduction_data(:,1:2))

presynaptic_neuron=input('Which cell is presynaptic?  (Blue is channel one, green is channel two.)')
postsynaptic_neuron=2*inv(presynaptic_neuron);
if presynaptic_neuron==1
    window_1=[410:500];
    window_2=[610:700];
elseif presynaptic_neuron==2
    window_1=[1420:1510];
    window_2=[1620:1710];
else error('y d f u')
end
plotting=input('Plot each trace? (1 for yes, 0 for no)')
ylimm_bottom=input('What is the lower vertical window setting for traces?')
ylimm_top=input('What is the upper vertical window setting for traces?')
ylimm=[ylimm_bottom ylimm_top];

figure(3)
subplot(4,3,1),plot(baseline_data(:,1:2)),ylim(ylimm), title('baseline')
subplot(4,3,4),plot(postinduction_data(:,1:2)),ylim(ylimm), title('postinduction')
%____________________________
minvalone_baseline=[];
minvaltwo_baseline=[];
number_of_trials_baseline=size(baseline_data,2);
live_trials_baseline=(2:2:number_of_trials_baseline);
for trial_baseline=2:2:number_of_trials_baseline
%     if isempty(find(trial_baseline==[3 4]))       %used for skipping trials if needed, but the resultant all-zero columns in the ascii matrices should be deleted before further analysis
    ddata=baseline_data(:,(trial_baseline-1):trial_baseline);
	ddata(:,1)=ddata(:,1)-mean(ddata(200:350,1));   %removing leak
	ddata(:,2)=ddata(:,2)-mean(ddata(200:350,2));

    [minvalone_baseline(trial_baseline),minindone]=min(ddata(window_1,postsynaptic_neuron));
    [minvaltwo_baseline(trial_baseline),minindtwo]=min(ddata(window_2,postsynaptic_neuron)); 
    if plotting
        figure(2),clf
        plot(ddata),hold on
        plot(minindone+window_1(1),minvalone_baseline(trial_baseline),'ro','markersize',5,'linewidth',3)
        plot(minindtwo+window_2(1),minvaltwo_baseline(trial_baseline),'ko','markersize',5,'linewidth',3)
        ylim([ylimm])
        xlim([window_1(1)-100 window_2(2)+100])
        title(num2str(trial_baseline/2))
        pause(.1)
    end
%     end
end

figure(3),
subplot(2,3,2)
minvalone_baseline=minvalone_baseline(live_trials_baseline);
minvaltwo_baseline=minvaltwo_baseline(live_trials_baseline);

plot(minvalone_baseline,'o','markersize',5,'markerfacecolor','r','markeredgecolor','r'),hold on
%plot(minvaltwo_baseline,'o','markersize',5,'markerfacecolor','k','markeredgecolor','k')
ylabel('PSC')
ylim([min([minvalone_baseline,minvaltwo_baseline]) max([minvalone_baseline,minvaltwo_baseline])])

preind_extent=[1 length(minvalone_baseline)]
begin_baseline=input('What trial begins the baseline?')
end_baseline=input('What trial ends the baseline?')
chosen_baseline_PSCo=minvalone_baseline(begin_baseline:end_baseline);
chosen_baseline_PSCa=minvaltwo_baseline(begin_baseline:end_baseline);

baseline_PSCo=mean(chosen_baseline_PSCo)
    plot([begin_baseline:end_baseline],repmat(baseline_PSCo,1,(end_baseline-(begin_baseline-1))),'k-','linewidth',4)
    plot([length(minvalone_baseline)+.5 length(minvalone_baseline)+.5],[2*baseline_PSCo 0],'b-','linewidth',4)
mean_PPR_baseline=mean(chosen_baseline_PSCa./chosen_baseline_PSCo)
std_PPR_baseline=std(chosen_baseline_PSCa./chosen_baseline_PSCo)

%____________________________
minvalone_postinduction=[];
minvaltwo_postinduction=[];
number_of_trials_postinduction=size(postinduction_data,2);
live_trials_postinduction=(2:2:number_of_trials_postinduction);
for trial_postinduction=2:2:number_of_trials_postinduction
%     if isempty(find(trial_postinduction==[99 100 101 102 103 104]))       %used for skipping trials if needed, but the resultant all-zero columns in the ascii matrices should be deleted before further analysis
    ddata=postinduction_data(:,(trial_postinduction-1):trial_postinduction);
	ddata(:,1)=ddata(:,1)-mean(ddata(200:350,1));   %removing leak
	ddata(:,2)=ddata(:,2)-mean(ddata(200:350,2));

    [minvalone_postinduction(trial_postinduction),minindone]=min(ddata(window_1,postsynaptic_neuron));
    [minvaltwo_postinduction(trial_postinduction),minindtwo]=min(ddata(window_2,postsynaptic_neuron)); 
    if plotting
        figure(2),clf
        plot(ddata),hold on
        plot(minindone+window_1(1),minvalone_postinduction(trial_postinduction),'ro','markersize',5,'linewidth',3)
        plot(minindtwo+window_2(1),minvaltwo_postinduction(trial_postinduction),'ko','markersize',5,'linewidth',3)
        ylim([ylimm])
        xlim([window_1(1)-100 window_2(2)+100])
        title(num2str(trial_postinduction/2))
        pause(.1)
    end
%     end
end
minvalone_postinduction=minvalone_postinduction(live_trials_postinduction);
minvaltwo_postinduction=minvaltwo_postinduction(live_trials_postinduction);

figure(3),subplot(2,3,2), hold on
npres=length(minvalone_baseline);   %number of preinduction trials
plot([(npres+1):(npres+length(minvalone_postinduction))],minvalone_postinduction,'o','markersize',5,'markerfacecolor','r','markeredgecolor','r')
%plot([(npres+1):(npres+length(minvaltwo_postinduction))],minvaltwo_postinduction,'o','markersize',5,'markerfacecolor','k','markeredgecolor','k')
ylim([min([minvalone_baseline,minvaltwo_baseline,minvalone_postinduction,minvaltwo_postinduction]) max([minvalone_baseline,minvaltwo_baseline,minvalone_postinduction,minvaltwo_postinduction])])
xlim([0 length(minvalone_baseline)+length(minvalone_postinduction)+1])

postind_extent=[1 length(minvalone_postinduction)]+npres
begin_postinduction=input('What trial begins the postinduction?'),begin_postinduction=begin_postinduction-npres;
end_postinduction=input('What trial ends the postinduction?'),end_postinduction=end_postinduction-npres;
chosen_postinduction_PSCo=minvalone_postinduction(begin_postinduction:end_postinduction);
chosen_postinduction_PSCa=minvaltwo_postinduction(begin_postinduction:end_postinduction);
postinduction_PSCo=mean(chosen_postinduction_PSCo)
    plot([npres+begin_postinduction:npres+end_postinduction],repmat(postinduction_PSCo,1,(end_postinduction-(begin_postinduction-1))),'k-','linewidth',4)
mean_PPR_postinduction=mean(chosen_postinduction_PSCa./chosen_postinduction_PSCo)
std_PPR_postinduction=std(chosen_postinduction_PSCa./chosen_postinduction_PSCo)

pprs_pre=minvaltwo_baseline./minvalone_baseline;
pprs_post=minvaltwo_postinduction./minvalone_postinduction;
pprs=[pprs_pre pprs_post];
h1 = gca;
h2 = axes('Position',get(h1,'Position'));
plot(pprs,'g','linewidth',1)
set(h2,'YAxisLocation','right','Color','none','XTickLabel',[])
set(h2,'XLim',[0 length(minvalone_baseline)+length(minvalone_postinduction)+1],'Layer','top')
ylabel('PPR')

subplot(2,3,5)  %postinduction relative PSCs and PPRs
plot(chosen_postinduction_PSCo/baseline_PSCo, 's','markerfacecolor','b','markeredgecolor','b','markersize',5)
ylabel('NLized postinduction PSC')
xlim([0 end_postinduction-begin_postinduction+1])

h1 = gca;
h2 = axes('Position',get(h1,'Position'));
plot(pprs_post(begin_postinduction:end_postinduction)/mean_PPR_baseline,'m','LineWidth',1)
set(h2,'YAxisLocation','right','Color','none','XTickLabel',[])
set(h2,'XLim',([0 end_postinduction-begin_postinduction+1]),'Layer','top')
ylabel('NLized postinduction PPR')

subplot(2,3,4),plot(realtime_data),hold on
plot([270:295],realtime_data(270:295,postsynaptic_neuron),'r-','linewidth',4),
realtime_data=realtime_data(:,[postsynaptic_neuron:2:end]);
realcols=size(realtime_data,2);
legend(strcat(num2str(realcols),' pairings'))
hi_slope=mean(realtime_data(290:295,1));lo_slope=mean(realtime_data(270:275,1));first_slope=hi_slope-lo_slope
subplot(2,3,3) %the real time response to induction
nl_slopes=[];
for k=1:realcols
    hi=mean(realtime_data(290:295,k));
    lo=mean(realtime_data(270:275,k));
    nl_slope(k)=(hi-lo)/first_slope;
end
plot(nl_slope,'ks','markersize',10)
legend('NLized slopes during induction')

subplot(2,3,6)
%rectangle('position',[1,0,.2,abs(mean_PPR_baseline)]), hold on,rectangle('position',[1,abs(mean_PPR_baseline),.1,std_PPR_baseline],'facecolor','k')
plot(1,mean_PPR_baseline,'o','markeredgecolor','k','markersize',6),hold on
plot(2,mean_PPR_postinduction,'o','markeredgecolor','k','markersize',6)
errorbar(1,mean_PPR_baseline,std_PPR_baseline)
errorbar(2,mean_PPR_postinduction,std_PPR_postinduction)
xlim([0 3]), ylabel('Mean and SD PPR pre and post')
title(strcat(baseline_filename,'--',induction_filename,'--',postinduction_filename),'fontsize',20)
%____________________________
set(3,'position',[1 30 1600 1101])
set(3,'PaperOrientation','landscape')
set(3,'PaperPosition',[0.25 0.25 10.5 8])
print
toc