clear
close all
clc
format compact

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

% Color code for different policy stringency
color_code = [
    0.0235    0.1922    0.3882
    0.1922    0.5098    0.7412
    0.4196    0.6824    0.8392
    0.6196    0.7922    0.8824
    ];

marker_symbol = {'x', '^', 's', 'o'};
marker_size = [4 4 4 4]-1;


% Figure 2: CO2 & precursor emissions under different policy stringency
figure(1); clf;
set(gcf, 'unit', 'centimeter', 'pos', [0.35   8.00   18.8    5]);

w = 0.16;
h = 0.38;
x0 = 0.2150;
dx = 0.2225;
y1 = 0.59;
y2 = 0.09;

% ==============================
% CO2
ax1 = axes('pos', [x0    y1    w    h]); hold on; box on;

data = xlsread('plot.xlsx','Fig2','B2:E7');

for f = 1:4    
    plot(yr, data(:,f)/1000, 'color', color_code(f,:), 'markerf', color_code(f,:), 'markersize', marker_size(f), 'marker', char(marker_symbol(f)));
end
set(gca, 'fontsize', 6);
set(gca, 'xcolor', [1 1 1]*0.3, 'ycolor', [1 1 1]*0.3);
ylabel('CO_2 (billion ton)', 'color', 'k');
xlim([2010 2030]);
ylim([0 16]);
set(gca, 'ytick', 0:4:16);

[~, hL] = legend('No Policy', '3% Policy', '4% Policy', '5% Policy');
set(legend, 'pos', [0.235   0.635    0.3    0.15]);
x = 0.0513;
y = 0.15;
set(hL(5), 'xdata', [x y])
set(hL(6), 'xdata', 2*x)
set(hL(7), 'xdata', [x y])
set(hL(8), 'xdata', 2*x)
set(hL(9), 'xdata', [x y])
set(hL(10), 'xdata', 2*x)
set(hL(11), 'xdata', [x y])
set(hL(12), 'xdata', 2*x)
x = 0.17;
set(hL(1), 'pos', [x    0.8345    0])
set(hL(2), 'pos', [x    0.6115    0])
set(hL(3), 'pos', [x    0.3885    0])
set(hL(4), 'pos', [x    0.1655    0])
set(legend, 'edge', 'none');
set(legend, 'color', 'none');

my_gridline;

% ==============================
% Urban emissions
for i = 1:5
    switch i
        case 1
            [~, ~, raw] = xlsread('plot.xlsx','Fig2','B11:E16');
            data = reshape([raw{:}],size(raw));
            bau = data(:,1);
            p3 = data(:,2);
            p4 = data(:,3);
            p5 = data(:,4);
            clear raw data;
            
            ax2 = axes('pos', [x0+dx    y1    w    h]); hold on; box on;
            set(gca, 'xcolor', [1 1 1]*0.3, 'ycolor', [1 1 1]*0.3);
            ylabel('SO_2 (million ton)', 'color', 'k');
            ylim([0 60]);
            set(gca, 'ytick', 0:15:60);
        case 2
            [~, ~, raw] = xlsread('plot.xlsx','Fig2','B20:E25');
            data = reshape([raw{:}],size(raw));
            bau = data(:,1);
            p3 = data(:,2);
            p4 = data(:,3);
            p5 = data(:,4);
            clear raw data;
            
            ax3 = axes('pos', [x0+dx+dx    y1    w    h]); hold on; box on;
            set(gca, 'xcolor', [1 1 1]*0.3, 'ycolor', [1 1 1]*0.3);
            ylabel('NO_x (million ton)', 'color', 'k');
            ylim([0 60]);
            set(gca, 'ytick', 0:15:60);
        case 3
            [~, ~, raw] = xlsread('plot.xlsx','Fig2','B29:E34');
            data = reshape([raw{:}],size(raw));
            bau = data(:,1);
            p3 = data(:,2);
            p4 = data(:,3);
            p5 = data(:,4);
            clear raw data;
            
            ax4 = axes('pos', [x0    y2    w    h]); hold on; box on;
            set(gca, 'xcolor', [1 1 1]*0.3, 'ycolor', [1 1 1]*0.3);
            ylabel('NH_3 (million ton)', 'color', 'k');
            ylim([0 20]);
            set(gca, 'ytick', 0:5:20);
        case 4
            [~, ~, raw] = xlsread('plot.xlsx','Fig2','B38:E43');
            data = reshape([raw{:}],size(raw));
            bau = data(:,1);
            p3 = data(:,2);
            p4 = data(:,3);
            p5 = data(:,4);
            clear raw data;
            
            ax5 = axes('pos', [x0+dx    y2    w    h]); hold on; box on;
            set(gca, 'xcolor', [1 1 1]*0.3, 'ycolor', [1 1 1]*0.3);
            ylabel('BC (million ton)', 'color', 'k');
            ylim([0 2.4]);
            set(gca, 'ytick', 0:0.6:2.4);
        case 5
            [~, ~, raw] = xlsread('plot.xlsx','Fig2','B47:E52');
            data = reshape([raw{:}],size(raw));
            bau = data(:,1);
            p3 = data(:,2);
            p4 = data(:,3);
            p5 = data(:,4);
            clear raw data;
            
            ax6 = axes('pos', [x0+dx+dx    y2    w    h]); hold on; box on;
            set(gca, 'xcolor', [1 1 1]*0.3, 'ycolor', [1 1 1]*0.3);
            ylabel('OC (million ton)', 'color', 'k');
            ylim([0 4]);
            set(gca, 'ytick', 0:1:4);
    end
    plot(yr, bau, 'x-', 'color', color_code(1,:), 'markerf', color_code(1,:), 'markersize', marker_size(1));
    plot(yr, p3, '^-', 'color', color_code(2,:), 'markerf', color_code(2,:), 'markersize', marker_size(2));
    plot(yr, p4, 's-', 'color', color_code(3,:), 'markerf', color_code(3,:), 'markersize', marker_size(3));
    plot(yr, p5, 'o-', 'color', color_code(4,:), 'markerf', color_code(4,:), 'markersize', marker_size(4));
    set(gca, 'fontsize', 6);
    xlim([2010 2030]);
    my_gridline;
end

annotation('textbox', [x0-0.002 0.89 0.2 0.1], 'String', '(a)', 'linestyle', 'none', 'fontweight', 'bold');
annotation('textbox', [x0-0.002+dx 0.89 0.2 0.1], 'String', '(b)', 'linestyle', 'none', 'fontweight', 'bold');
annotation('textbox', [x0-0.002+dx+dx 0.89 0.2 0.1], 'String', '(c)', 'linestyle', 'none', 'fontweight', 'bold');
annotation('textbox', [x0-0.002 0.12 0.2 0.1], 'String', '(d)', 'linestyle', 'none', 'fontweight', 'bold');
annotation('textbox', [x0-0.002+dx 0.12 0.2 0.1], 'String', '(e)', 'linestyle', 'none', 'fontweight', 'bold');
annotation('textbox', [x0-0.002+dx+dx 0.12 0.2 0.1], 'String', '(f)', 'linestyle', 'none', 'fontweight', 'bold');

%% Re-plot legend
set(hL(1), 'fontsize', 7)
set(hL(2), 'fontsize', 7)
set(hL(3), 'fontsize', 7)
set(hL(4), 'fontsize', 7)