clc
clf
clear 
clear all
close all
 
file = 'Estimates.xlsx';

data_R=readtable(file,'Sheet','R_mf8to25'); [dtR1,dtR2,dtR3]=xlsread(file,'R_mf8to25');
data_dc=readtable(file,'Sheet','dc');   [dc1,dc2,dc3]=xlsread(file,'dc');
data_b=readtable(file,'Sheet','bg_mf8to25');


data_R25=readtable(file,'Sheet','R_mf5to2');
data_b25=readtable(file,'Sheet','bg_mf5to2');

[a1,a2]=size(data_R);

t = datetime(2020,1,22) + caldays(0:a2-2);
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 comparisons of R
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_R(i,2:end);    r=table2array(raux);
    raux25=data_R25(i,2:end);    r25=table2array(raux25);
     
    daux=data_dc(i,2:end);   d=table2array(daux);
%     baux=data_b(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; r25(1,1:xmin-2)=nan;
    
    %xmin=find(~isnan(d),1);
    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,r25,':k','LineWidth',2.0)
        hold on
        plot(t,r,'-r','LineWidth',1.5)
        hold on
        yline(1,'-k','LineWidth',1.25);
        hold on
%         yyaxis right
%         plot(t,d,'--','Color',[0.5 0.5 0.5],'LineWidth',1.5)
       
       cloneYAxisFromLeftToRight 
       
        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';
        set(h,'Units','Inches');
        pos = get(h,'Position');
        set(h,'PaperPositionMode','Auto','PaperUnits','Inches','PaperSize',[pos(3), pos(4)])
        print(h,country_name,'-dpdf','-r0')
        filename = strcat(country_name,'.pdf');
        movefile(filename,'.\figures\comp_r');
%         print(h,country_name,'-djpeg','-r0')
%         filename = strcat(country_name,'.jpg');
%         movefile(filename,'.\figures');    
 
        
    
    close all;
   end % if foundc
end

%% plot comparisons of beta
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_R(i,2:end);    r=table2array(raux);
   % raux25=data_R25(i,2:end);    r25=table2array(raux25);
     
    daux=data_dc(i,2:end);   d=table2array(daux);
    baux=data_b(i,2:end);    b=table2array(baux);
    baux25=data_b25(i,2:end);    b25=table2array(baux25);
    
    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  
    
    b(1,1:xmin-2)=nan; b25(1,1:xmin-2)=nan;
    
    %xmin=find(~isnan(d),1);
    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,b25,':k','LineWidth',2.0)
        hold on
        plot(t,b,'-b','LineWidth',1.5)
        hold on
        yline(1,'-k','LineWidth',1.25);
        hold on
%         yyaxis right
%         plot(t,d,'--','Color',[0.5 0.5 0.5],'LineWidth',1.5)
       cloneYAxisFromLeftToRight 
        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';
        set(h,'Units','Inches');
        pos = get(h,'Position');
        set(h,'PaperPositionMode','Auto','PaperUnits','Inches','PaperSize',[pos(3), pos(4)])
        print(h,country_name,'-dpdf','-r0')
        filename = strcat(country_name,'.pdf');
        movefile(filename,'.\figures\comp_beta');
%         print(h,country_name,'-djpeg','-r0')
%         filename = strcat(country_name,'.jpg');
%         movefile(filename,'.\figures');    
    
    close all;
   end % if foundc
end

%% save selected series to csv file for Mfit

CLM={'Iran'; 'Turkey'; 'Iraq'; 'Saudi_Arabia'; 'Afghanistan'; 'Pakistan';...
    'US'; 'India';'Brazil';'Russia';...
    'Belgium'; 'France'; 'Germany'; 'Italy'; 'Netherlands'; 'Poland';...
    'Portugal'; 'Spain'; 'United_Kingdom'};
CLM_mfit={'Iran'; 'Turkey'; 'Iraq'; 'SauAra'; 'Afga'; 'Pak';...
    'US'; 'India';'Bra';'Rus';...
    'Belg'; 'Fra'; 'Ger'; 'Ita'; 'Neth'; 'Pol';...
    'Por'; 'Spain'; 'UK'};

j=size(CLM,1);

T=size(data_R,2)-1;
A=cell(T+1,j*2+1);

A(1,1)={'date'};
A(2:end,1)=dtR3(1,2:end)';
j=1;
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(CLM,country_name);
   if foundc
    A(1,2*j)={['R_',CLM_mfit{sp}]};
    A(2:end,2*j)=dtR3(i+1,2:end);
    A(1,2*j+1)={['dc_',CLM_mfit{sp}]};
    A(2:end,2*j+1)=dc3(i+1,2:end);
    j=j+1;
   end % if foundc
end

% save A to csvcsvwrite
filename='data_for_hashem.csv';
A(2:8,:)=[];
writecell(A,filename);


function copyAxis(a, b)
    p =  properties(a).';
    for i=1:numel(p)  %copy all public properties
        try   %may fail if property is read-only
            b.(p{i}) = a.(p{i});
        catch
            warning('failed to copy property: %s', p{i});
        end
    end
end

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
