clear all

load('DataLatiGradPartialr0.mat','chngpars','partialr0','parnames','par0values','popthrhd','OutData'); 


level=1;
popthrhdnmbr=length(popthrhd);
columns=[1:5,5+(level:popthrhdnmbr:6*popthrhdnmbr),(5+6*popthrhdnmbr+1):length(OutData(1,:))];


pta0=min(OutData(:,2));
pta1=max(OutData(:,2));
ptb=min(OutData(:,3));
ptc=max(OutData(:,4));
indx=(OutData(:,1)<=90 & OutData(:,2)>pta0-1e-4 & OutData(:,2)<pta1+1e-4 ...
    & OutData(:,3)<ptb+1e-4 & OutData(:,3)>ptb-1e-4...
    & OutData(:,4)<ptc+1e-4 & OutData(:,4)>ptc-1e-4); 

plotOutData=OutData(indx,columns);

nmbrofruns=1;
r0length=sum((plotOutData(:,1)==plotOutData(1,1) & plotOutData(:,3)==plotOutData(1,3) & plotOutData(:,4)==plotOutData(1,4)));
rn0=rand(1)
rn0=0.2499;%0.6111;0.9312;0.7489;0.1884;0.2432;0.3649;0.7284;0.2499;0.4051;0.8835
idinit=max(floor(plotOutData(end,1)*rn0)-nmbrofruns,0)*r0length;
indx=idinit+1:idinit+min(plotOutData(end,1),nmbrofruns)*r0length;
rn=floor(rand(1)*plotOutData(end,1));
rn=rn-1;
rn=37;
indx1=(OutData(:,1)==rn & plotOutData(:,3)==plotOutData(1,3) & plotOutData(:,4)==plotOutData(1,4));

stdon=0; % binary.

bmscale=1;
Binsize=(5-(-2))/60;

xaxis=2;
%%

figure(1)
hold off

xaxispts=log(plotOutData(indx,xaxis))/log(10);
plot([-8],[0],'g-',[-8],[-1],'b-',[-8],[-2],'r-',[-8],[-3],'m-','linewidth',3)
legend('Plant','Herbivore','Carnivore','Biomass','location','nw')
xlabel('log(r_0)','fontsize',16)
ylabel('Numer of Species','fontsize',16)


hold on

if stdon==0

plot(xaxispts,plotOutData(indx,9),'g.', ...
    xaxispts,plotOutData(indx,10),'b.', ...
    xaxispts,plotOutData(indx,11),'r.','markersize',20)
hold on

end


Bms=log(plotOutData(:,8)+plotOutData(:,6)+plotOutData(:,7))/log(10);
yBms=log(plotOutData(:,xaxis))/log(10);
Bms=bmscale*Bms;

XX=yBms(indx1)';
YY1=plotOutData(indx1,9)';
YY2=plotOutData(indx1,10)';
YY3=plotOutData(indx1,11)';
YY0=log((plotOutData(indx1,8)+plotOutData(indx1,6)+plotOutData(indx1,7))')/log(10);

[aa,bb]=sort(XX);

if stdon==0

    plot(aa,YY1(bb),'gs',aa,YY1(bb),'k-',...
        aa,YY2(bb),'bs',aa,YY2(bb),'k-',...
        aa,YY3(bb),'rs',aa,YY3(bb),'k-','markersize',8,'linewidth',1)
    hold on

end

hold on

axis([-2 5 0 180])

box off

set(gca,'XTick',-2:1:5)

ax1 = gca;
set(ax1,'XColor','k','YColor','k')

ax2 = axes('Position',get(ax1,'Position'),... 
    'XAxisLocation','top',...
           'YAxisLocation','right',...
           'Color','none');%,'XColor','k','YColor','k');
hold on


plot(aa,YY0(bb),'ms',aa,YY0(bb),'k-','markersize',8,'linewidth', 1,'Parent',ax2)
hold on
plot(xaxispts,log(plotOutData(indx,8)+plotOutData(indx,6)+plotOutData(indx,7))/log(10),'m.','markersize',20,'Parent',ax2)
ylabel('log(Biomass)','fontsize',16)

axis([-2 5 -3 1.1*max(max(YY0))])

yend=5;
%xlimits = get(ax1,'XLim');
ylimits = get(ax1,'YLim');
%xinc = (xlimits(2)-xlimits(1))/yend;
yinc = (ylimits(2)-ylimits(1))/yend;

%set(ax1,'XTick',[xlimits(1):xinc:xlimits(2)],...
%        'YTick',[ylimits(1):yinc:ylimits(2)])

set(ax1,'YTick',[ylimits(1):yinc:ylimits(2)])        

%%