% This script:
%       (1) loads data from the file 'Estimates.xlsx' specified below, 
%       (2) creates selected figures, and
%       (3) saves them in the subfolder \selected_figures using a pdf format
% This script is set up to create the same set of charts as reported in Section 4 of the paper in Figures 1-8.
% This script can be modified to create charts also for other countries.

% clear workspace
clc
clf
clear 
close all
 
%% load data
file = 'Estimates.xlsx';
disp(['Loading data from ', file,' ...']);

warning('off');
data_R=readtable(file,'Sheet','R_mf5'); [dtR1,dtR2,dtR3]=xlsread(file,'R_mf5');
data_dc=readtable(file,'Sheet','dc');   [dc1,dc2,dc3]=xlsread(file,'dc');
data_b=readtable(file,'Sheet','bg_mf5');


data_R25=readtable(file,'Sheet','R_mf3');
data_b25=readtable(file,'Sheet','bg_mf3');

[a1,a2]=size(data_R);

t = datetime(2020,1,22) + caldays(0:a2-2);

% define the set of countries and/or regions below
CL={'China'; 'World_excl_China';'Northern_Hemisphere_excl_China';...
    'Southern_Hemisphere'; 'East_Asia_and_Pacific_excl_China'; 'Eastern_Europe_and_Central_Asia';...
    'Western_Europe'; 'Latin_America_and_Caribbean'; 'Middle_East_and_North_Africa';...
    'North_America'; 'South_Asia'; 'SubSaharan_Africa'; 'US'; 'Brazil';...
    'India'; 'Russia'; 'South_Africa'; 'Iran'; 'Turkey'; 'Australia';...
    'Belgium'; 'France'; 'Germany'; 'Italy'; 'Netherlands'; 'Poland';...
    'Portugal'; 'Spain'; 'United_Kingdom'};

%% plot selected figures
disp('Creating figures ...');
for i=1:a1 % for all countries

   name1=string(data_R{i,1});
   name2=strrep(name1,'.','');
   name3=strrep(name2,':','');
   name4=strrep(name3,' ','_');
   name5=strrep(name4,'*','');
   country_name=name5;
   [sp, foundc]=find_go_starts(CL,country_name);
   if foundc
    
    raux=data_R25(i,2:end);    r=table2array(raux);
    daux=data_dc(i,2:end);   d=table2array(daux);
    baux=data_b25(i,2:end);    b=table2array(baux);
    xmin=find(r<2.99,1); 
    if strcmp('World_excl_China',country_name)
           xmin=69;
    end
    if strcmp('East_Asia_and_Pacific_excl_China',country_name)
       xmin=47;
    end
    if strcmp('Northern_Hemisphere_excl_China',country_name)
       xmin=69;
    end  
    
    r(1,1:xmin-2)=nan; b(1,1:xmin-2)=nan;
    
    xmin=xmin-14;
    xmax=find(~isnan(d),1,'last');
    
    t_min = datetime(2020,1,29) + caldays(xmin-1);
    t_max = datetime(2020,1,29) + caldays(xmax-1);

   % Plot
        h = figure;
       yyaxis left
        
        plot(t,b,':b','LineWidth',2.0)
        hold on
        plot(t,r,'-r','LineWidth',1.5)
        hold on
        yline(1,'-k','LineWidth',1.25);

        hold on
        xlim([t_min t_max]);
        xticks(datetime(t_min):calweeks(3):datetime(t_max));
        xtickangle(90);
        xtickformat('MMM dd')
        ax = gca;
        ax.YAxis(1).Color = 'k';
        ax.YAxis(2).Color = 'k';
        
        cloneYAxisFromLeftToRight
        
        set(h,'Units','Inches');
        pos = get(h,'Position');
        set(h,'PaperPositionMode','Auto','PaperUnits','Inches','PaperSize',[pos(3), pos(4)])
        print(h,strcat('R_',country_name),'-dpdf','-r0')
        filename = strcat('R_',country_name,'.pdf');
        movefile(filename,'.\figures');
% if jpegs are desired, then the commands below can be used.        
%         print(h,country_name,'-djpeg','-r0')
%         filename = strcat(country_name,'.jpg');
%         movefile(filename,'.\figures');    
    close all;
    
        h = figure;
        yyaxis left
        plot(t,d,'-k','LineWidth',1.5)
        hold on
        xlim([t_min t_max]);
        xticks(datetime(t_min):calweeks(3):datetime(t_max));
        xtickangle(90);
        xtickformat('MMM dd')
        ax = gca;
        ax.YAxis(1).Color = 'k';
        ax.YAxis(2).Color = 'k';
        cloneYAxisFromLeftToRight
        
        set(h,'Units','Inches');
        pos = get(h,'Position');
        set(h,'PaperPositionMode','Auto','PaperUnits','Inches','PaperSize',[pos(3), pos(4)])
        print(h, strcat('C_',country_name),'-dpdf','-r0')
        filename = strcat('C_',country_name,'.pdf');
        movefile(filename,'.\figures');
%         print(h,country_name,'-djpeg','-r0')
%         filename = strcat(country_name,'.jpg');
%         movefile(filename,'.\figures');    
    close all;
   end % if foundc
end
disp('Done. Selected figures are saved in the subfolder \figures.');

%% auxiliary functions

function cloneYAxisFromLeftToRight()
    fmt = ytickformat(gca);
    ax0 = get(gca);
    yyaxis right;
    ax1 = gca;
    p =  properties(ax0.YAxis).';
    for i=1:numel(p)  %copy all public properties
        try   %#ok<TRYNC> %may fail if property is read-only
            ax1.YAxis(2).(p{i}) = ax0.YAxis.(p{i});
        end
    end
    %extras
    ax1.YColor = ax0.YColor;
    ax1.YColorMode = ax0.YColorMode;
    ax1.YDir = ax0.YDir;
    ax1.YLimMode = ax0.YLimMode;
    ax1.YScale = ax0.YScale;
    ax1.YTickLabelMode = ax0.YTickLabelMode;
    ax1.YTickLabelRotation = ax0.YTickLabelRotation;
    ax1.YTickMode = ax0.YTickMode;
    
    %core
    ax1.YTickLabel = ax0.YTickLabel;
    ax1.YLim = ax0.YLim;
    ax1.YTick = ax0.YTick;
    
    %restore original format on the right Y-axis
    ytickformat(fmt);
end
