clear
close all
clc
format compact

set_list;
yr = [2007,2010,2015,2020,2025,2030];

% Sector grouping
group = {{'EIS', 'MAN', 'CON', 'COL', 'CRU', 'GAS', 'OIL'}, ...
         {'ELE'}, ...
         {'TRN'}, ...
         {'SER', 'OMN', 'WTR', 'AGR', 'g', 'i'}, ...
         {'c'}};
g_group = {' Energy-intensive and \newline Manufacturing Industries', 'Electricity', 'Transportation', 'Household', 'Other'};

% Color code for energy types
clr = [0.7412         0    0.1490
       0.9555    0.2379    0.1464
       0.9935    0.6013    0.2562
       0.9961    0.8510    0.4627];

%% Energy by sector
% BAU
egy_bau2030 = xlsread('plot.xlsx','Fig1','B9:F12');

% 4 percent policy
egy_policy2030 = xlsread('plot.xlsx','Fig1','B16:F19');

% Diff
d_egy = egy_bau2030-egy_policy2030;


%% CO2
% BAU
co2_bau2030 = xlsread('plot.xlsx','Fig1','B23:F23');
co2_policy2030 = xlsread('plot.xlsx','Fig1','B24:F24');


%% SO2
% BAU
so2_bau2030 = xlsread('plot.xlsx','Fig1','B27:F27');

% 4 percent policy
so2_policy2030 = xlsread('plot.xlsx','Fig1','B28:F28');


%% NOx
% BAU
nox_bau2030 = xlsread('plot.xlsx','Fig1','B31:F31');

% 4 percent policy
nox_policy2030 = xlsread('plot.xlsx','Fig1','B32:F32');


%% Figure 1: bar chart 
figure(1); clf;
set(gcf, 'unit', 'centimeter', 'pos', [2.5    10.0    17.5   4.5]);

ht = 0.78;
x_pos = 0.065;
y_pos = 0.19;
dy = 0.175;
dx1 = 0.315;
dx2 = 0.208;

% ==============================
% Energy barchart
ax1 = axes;
set(gca, 'pos', [x_pos   y_pos    dy    ht]);

data = xlsread('plot.xlsx','Fig1','B2:D5');
data = transpose(data);
A2010 = data(1,:);
A2030 = data(2,:);
B2030 = data(3,:);
hb = bar([1 2 3], data, 0.65, 'stacked', 'linestyle', 'none');
for i = 1:length(hb)
    set(hb(i), 'facec', clr(i,:));
end

text(1, A2010(1)/2, 'Coal', 'horizontalalignment', 'center', 'fontsize', 5)
text(1, A2010(1)+A2010(2)/2, 'Gas', 'horizontalalignment', 'center', 'fontsize', 5)
text(1, A2010(1)+A2010(2)+A2010(3)/2, 'Oil', 'horizontalalignment', 'center', 'fontsize', 5)
text(1, A2010(1)+A2010(2)+A2010(3)+A2010(4)/2, 'Non-Fossil', 'horizontalalignment', 'center', 'fontsize', 5)

set(gca, 'tickdir', 'out');
set(gca, 'fontsize', 6);
xlim([0.55 3.45]);
box off;
set(gca, 'xcolor', [1 1 1]*0.3, 'ycolor', [1 1 1]*0.3);
set(gca, 'layer', 'top');
ylabel('Energy Use (Mega ton coal equivalent)      ', 'color', 'k');
set(gca, 'xtick', 1:3, 'xticklabel', []);
text(1, -380, '2010', 'horizontalalignment', 'center', 'fontsize', 6)
text(2, -380, 'No Policy', 'horizontalalignment', 'center', 'fontsize', 6)
text(3.1, -380, '4% Policy', 'horizontalalignment', 'center', 'fontsize', 6)
text(2, -820, 'in 2030', 'horizontalalignment', 'center', 'fontsize', 6)
text(3.1, -820, 'in 2030', 'horizontalalignment', 'center', 'fontsize', 6)

text(2.95, 7500, '(a)', 'fontsize', 9, 'fontweight', 'bold');

%% ==============================
ax2 = axes;
set(gca, 'pos', [x_pos+dx1   y_pos    dy    ht]);
data = [sum(d_egy,2), d_egy];
hb = barh(data', 0.6, 'stacked');
set(hb, 'edgecolor', 'none');
for i = 1:length(hb)
    set(hb(i), 'facec', clr(i,:));
end

ylim([-0.5 5.5]+1);
set(gca, 'fontsize', 6);
set(gca, 'ydir', 'reverse');
set(gca, 'tickdir', 'out');
set(gca, 'layer', 'top');
set(gca, 'xcolor', [1 1 1]*0.3, 'ycolor', [1 1 1]*0);
xlabel('Reduction in Energy Use (Mtce)', 'color', 'k');
set(gca, 'ytick', 1:length(data), 'yticklabel', ['National Total',(g_group)], 'fontsize', 5.95);
box off;

den = [sum(egy_bau2030,2), egy_bau2030];
pctg = sum(data)./sum(den);
for i = 1:6
text(sum(data(:,i)), i, [' -', num2str(pctg(i)*100, '%2.0f'), '%'], 'horizontalalignment', 'left', 'fontsize', 6)
end

annotation('textbox', [x_pos+dx1+0.14, y_pos+0.12, 0.3, 0.025], 'string', '(b)', 'fontsize', 8, 'edgecolor', 'none', 'fontweight', 'bold');

% ==============================
ax3 = axes;
set(gca, 'pos', [x_pos+dx1+dx2   y_pos    dy    ht]);
d_co2 = (co2_policy2030 - co2_bau2030);
d_co2_ntl = (sum(co2_policy2030) - sum(co2_bau2030));
data = [d_co2_ntl, d_co2]/1e3;
barh(-data, 0.6, 'facec', [0 0.7 0], 'edgecolor', 'none');
ylim([0.5 6.5]);
set(gca, 'fontsize', 6);
set(gca, 'ydir', 'reverse');
set(gca, 'tickdir', 'out');
set(gca, 'layer', 'top');
box off;
set(gca, 'xcolor', [1 1 1]*0.3, 'ycolor', [1 1 1]*0);
xlabel('Reduction in CO_2 (Bt)', 'fontsize', 7, 'color', 'k');
set(gca, 'ytick', 1:length(pctg), 'yticklabel', []);

pctg = [d_co2_ntl, d_co2]./[sum(co2_bau2030), co2_bau2030];
for i = 1:length(pctg)
    text(-data(i), i, [' ', num2str(pctg(i)*100, '%2.0f'), '%'], 'fontsize', 6);
end

annotation('textbox', [x_pos+dx1+dx2+0.14, y_pos+0.12, 0.3, 0.025], 'string', '(c)', 'fontsize', 8, 'edgecolor', 'none', 'fontweight', 'bold');

% ==============================
ax4 = axes;
set(gca, 'pos', [x_pos+dx1+dx2+dx2   y_pos    dy    ht]);

d_so2 = (so2_policy2030 - so2_bau2030);
d_nox = (nox_policy2030 - nox_bau2030);

d_so2_ntl = (sum(so2_policy2030) - sum(so2_bau2030));
d_nox_ntl = (sum(nox_policy2030) - sum(nox_bau2030));

data = [d_so2; d_nox]';
data = [d_so2_ntl, d_nox_ntl;
        data];
hb = barh(-data, 1, 'hist');
set(hb, 'edgecolor', 'none');
set(hb(1), 'facec', [0 0.5 0]);
set(hb(2), 'facec', [0 0.85 0]);

set(gca, 'ydir', 'reverse');
set(gca, 'tickdir', 'out');
set(gca, 'layer', 'top');
ylim([-0.5 5.5]+1);
xlim([0 12]);
set(gca, 'xtick', 0:3:12);
set(gca, 'fontsize', 6);
set(gca, 'ytick', 1:length(data), 'yticklabel', []);
set(gca, 'xcolor', [1 1 1]*0.3, 'ycolor', [1 1 1]*0);
xlabel('Reduction in Emissions (Mt)', 'color', 'k');
box off;

legend('SO_2', 'NO_x');
set(legend, 'pos', [0.86    0.38    0.1058    0.1167]);

den = [sum(so2_bau2030), sum(nox_bau2030)
       so2_bau2030', nox_bau2030'];
pctg = data./den*100;
for i = 1:length(pctg)
    text(-data(i,1), i-0.15, [' ', num2str(pctg(i,1), '%2.0f'), '% '], 'fontsize', 5);
    text(-data(i,2), i+0.15, [' ', num2str(pctg(i,2), '%2.0f'), '% '], 'fontsize', 5);
end

annotation('textbox', [x_pos+dx1+dx2+dx2+0.14, y_pos+0.12, 0.3, 0.025], 'string', '(d)', 'fontsize', 8, 'edgecolor', 'none', 'fontweight', 'bold');
