MaskPOPEI1970=nan(139,231);
MaskPOPEI1970(POPEI1970>0)=1;
POPEISUM=nansum(nansum(P1970.*MaskPOPEI1970));

MaskPOPEI2020=nan(139,231);
MaskPOPEI2020(POPEI2020>0)=1;
POPEISUM(2)=nansum(nansum(P2020.*MaskPOPEI2020));

MaskPOPEI1970_2=nan(139,231);
MaskPOPEI1970_2(POPEI1970>2)=1;
POPEISUM(3)=nansum(nansum(P1970.*MaskPOPEI1970_2));

MaskPOPEI2020_2=nan(139,231);
MaskPOPEI2020_2(POPEI2020>2)=1;
POPEISUM(4)=nansum(nansum(P2020.*MaskPOPEI2020_2));


for i=1:6
    MaskPOPEI1970=nan(139,231);
    MaskPOPEI2020=nan(139,231);
    MaskPOPEI1970(POPEI1970>(i*0.5-0.5))=1;
    MaskPOPEI2020(POPEI2020>(i*0.5-0.5))=1;
    POPEISUM(1,i)=nansum(nansum(P1970.*MaskPOPEI1970));
    POPEISUM(2,i)=nansum(nansum(P2020.*MaskPOPEI2020));
    APOPEISUM(1,i)=nansum(nansum(MaskPOPEI1970))*770.0625;
    APOPEISUM(2,i)=nansum(nansum(MaskPOPEI2020))*770.0625;
end

for i=1:6
    MaskPOPEI21970=nan(139,231);
    MaskPOPEI22020=nan(139,231);
    MaskPOPEI21970(POPEI21970>(i*0.2-0.2))=1;
    MaskPOPEI22020(POPEI22020>(i*0.2-0.2))=1;
    POPEI2SUM(1,i)=nansum(nansum(P1970.*MaskPOPEI21970));
    POPEI2SUM(2,i)=nansum(nansum(P2020.*MaskPOPEI22020));
    APOPEI2SUM(1,i)=nansum(nansum(MaskPOPEI21970))*770.0625;
    APOPEI2SUM(2,i)=nansum(nansum(MaskPOPEI22020))*770.0625;
end


 [ha, pos] = tight_subplot(2,4,[.1,.05],[.2 .2],[.02 .02])
% for ii = 1:8; axes(ha(ii)); plot(randn(10,ii)); end
% set(ha(1:2),'XTickLabel',''); set(ha,'YTickLabel','')
axes(ha(1))
m_proj('Lambert','lon',NEWLONLIMS,'lat',NEWLATLIMS)
m_pcolor(trmm_pos(:,:,1), trmm_pos(:,:,2),POPEI1970);shading flat;
m_grid('linewi',0.5,'tickdir','out')
% hold on
% m_plot(bndlon4(:)-0.125,bndlat4(:)+0.125,'x','color','k');
caxis([0 3])
for k=1:length(M.ncst)
m_line(M.ncst{k}(:,1),M.ncst{k}(:,2),'color','k','linewidth', 0.5);
end
title('a.>100 mm TCPEI, Early Period')
colormap(gca,'jet')
set(gca,'fontsize',12);
h1=colorbar;
colorTitleHandle = get(h1,'Title');
titleString = 'TCPEI';
set(colorTitleHandle ,'String',titleString);

axes(ha(2))
m_proj('Lambert','lon',NEWLONLIMS,'lat',NEWLATLIMS)
m_pcolor(trmm_pos(:,:,1), trmm_pos(:,:,2),POPEI2020);shading flat;
m_grid('linewi',0.5,'tickdir','out')
% hold on
% m_plot(bndlon4(:)-0.125,bndlat4(:)+0.125,'x','color','k');
 caxis([0 3])
for k=1:length(M.ncst)
m_line(M.ncst{k}(:,1),M.ncst{k}(:,2),'color','k','linewidth', 0.5);
end
title('b. >100 mm TCPEI, Late Period')
colormap(gca,'jet')
set(gca,'fontsize',12);
h2=colorbar;
colorTitleHandle = get(h2,'Title');
titleString = 'TCPEI';
set(colorTitleHandle ,'String',titleString);

axes(ha(3))
Xlab = categorical({'>0','>0.5','>1','>1.5','>2','>2.5'});
Xlab = reordercats(Xlab,{'>0','>0.5','>1','>1.5','>2','>2.5'});
b1=bar(Xlab,POPEISUM');
xtips1 =b1(1).XEndPoints;
ytips1 = b1(1).YEndPoints;
labels1 = ({'55m','24m','10m','3m','846k','15k'});
text(xtips1,ytips1,labels1,'HorizontalAlignment','center',...
    'VerticalAlignment','bottom','FontSize',8)
xtips2 =b1(2).XEndPoints;
ytips2 = b1(2).YEndPoints;
labels2 = ({'107m','86m','54m','26m','16m','6.6m'});
text(xtips2,ytips2,labels2,'HorizontalAlignment','center',...
    'VerticalAlignment','bottom','FontSize',8)
title('c. >100 mm TCPEI and Population')
xlabel('TCPEI')
ylabel('Population')
legend('Early Period','Late Period')
set(gca,'fontsize',12);

axes(ha(4))
Xlab = categorical({'>0','>0.5','>1','>1.5','>2','>2.5'});
Xlab = reordercats(Xlab,{'>0','>0.5','>1','>1.5','>2','>2.5'});
b2=bar(Xlab,APOPEISUM');
xtips3 =b2(1).XEndPoints;
ytips3 = b2(1).YEndPoints;
labels3 = ({'968k','444k','135k','35k','6161','770'});
text(xtips3,ytips3,labels3,'HorizontalAlignment','center',...
    'VerticalAlignment','bottom','FontSize',8)
xtips4 =b2(2).XEndPoints;
ytips4 = b2(2).YEndPoints;
labels4 = ({'1m','871k','400k','171k','81k','25k'});
text(xtips4,ytips4,labels4,'HorizontalAlignment','center',...
    'VerticalAlignment','bottom','FontSize',8)
title('d. >100 mm TCPEI and Area')
xlabel('TCPEI')
ylabel('Area (km^{2})')
legend('Early Period','Late Period')
set(gca,'fontsize',12);


axes(ha(5))
m_proj('Lambert','lon',NEWLONLIMS,'lat',NEWLATLIMS)
m_pcolor(trmm_pos(:,:,1), trmm_pos(:,:,2),POPEI21970);shading flat;
m_grid('linewi',0.5,'tickdir','out')
% hold on
% m_plot(bndlon4(:)-0.125,bndlat4(:)+0.125,'x','color','k');
caxis([0 1.5])
for k=1:length(M.ncst)
m_line(M.ncst{k}(:,1),M.ncst{k}(:,2),'color','k','linewidth', 0.5);
end
title('e. >200 mm TCPEI, Early Period')
colormap(gca,'jet')
set(gca,'fontsize',12);
h1=colorbar;
colorTitleHandle = get(h1,'Title');
titleString = 'TCPEI';
set(colorTitleHandle ,'String',titleString);

axes(ha(6))
m_proj('Lambert','lon',NEWLONLIMS,'lat',NEWLATLIMS)
m_pcolor(trmm_pos(:,:,1), trmm_pos(:,:,2),POPEI22020);shading flat;
m_grid('linewi',0.5,'tickdir','out')
% hold on
% m_plot(bndlon4(:)-0.125,bndlat4(:)+0.125,'x','color','k');
 caxis([0 1.5])
for k=1:length(M.ncst)
m_line(M.ncst{k}(:,1),M.ncst{k}(:,2),'color','k','linewidth', 0.5);
end
title('f. >200 mm TCPEI, Late Period')
colormap(gca,'jet')
set(gca,'fontsize',12);
h2=colorbar;
colorTitleHandle = get(h2,'Title');
titleString = 'TCPEI';
set(colorTitleHandle ,'String',titleString);

axes(ha(7))
Xlab2 = categorical({'>0','>0.2','>0.4','>0.6','>0.8','>1'});
Xlab2 = reordercats(Xlab2,{'>0','>0.2','>0.4','>0.6','>0.8','>1'});
b3=bar(Xlab2,POPEI2SUM');
xtips5 =b3(1).XEndPoints;
ytips5 = b3(1).YEndPoints;
labels5 = ({'4.9m','4.5m','2.3m','715k','15k','0'});
text(xtips5,ytips5,labels5,'HorizontalAlignment','center',...
    'VerticalAlignment','bottom','FontSize',8)
xtips6 =b3(2).XEndPoints;
ytips6 = b3(2).YEndPoints;
labels6 = ({'29m','28m','23m','14m','8m','4m'});
text(xtips6,ytips6,labels6,'HorizontalAlignment','center',...
    'VerticalAlignment','bottom','FontSize',8)
title('g. >200 mm TCPEI and Population')
xlabel('TCPEI')
ylabel('Population')
legend('Early Period','Late Period')
ylim([0 35000000])
set(gca,'fontsize',12);


axes(ha(8))
Xlab2 = categorical({'>0','>0.2','>0.4','>0.6','>0.8','>1'});
Xlab2 = reordercats(Xlab2,{'>0','>0.2','>0.4','>0.6','>0.8','>1'});
b4=bar(Xlab2,APOPEI2SUM');
xtips7 =b4(1).XEndPoints;
ytips7 = b4(1).YEndPoints;
labels7 = ({'109k','52k','15k','5390','770','0'});
text(xtips7,ytips7,labels7,'HorizontalAlignment','center',...
    'VerticalAlignment','bottom','FontSize',8)
xtips8 =b4(2).XEndPoints;
ytips8 = b4(2).YEndPoints;
labels8 = ({'286k','226k','152k','78k','36k','11k'});
text(xtips8,ytips8,labels8,'HorizontalAlignment','center',...
    'VerticalAlignment','bottom','FontSize',8)
title('h. >200 mm TCPEI and Area')
xlabel('TCPEI')
ylabel('Area (km^{2})')
legend('Early Period','Late Period')
set(gca,'fontsize',12);
ylim([0 310000])
