%% Fiugre S9 and S10 gengerator

close all
clear all

load Fig2_result_data.mat
%% colormap
cjet=jet;
cmap0=cjet([ 64 55 48 42 38 35 30 24 15 10 1]*4,:);
cmap_reg=cmap0;

cmap=[[1 0 0]; cmap0([4 4 4 4 6 8 8 8 8],:);[0 0 1]];%両端を赤と青に変更

%% sumD
load DataNomal
regionNo = 2;
yearNo =3;

Demandunit=NormalizedData(:,1,regionNo,yearNo);
PVunit=NormalizedData(:,2,regionNo,yearNo);
Windunit=NormalizedData(:,3,regionNo,yearNo);            

sumD=sum(Demandunit);      
sumP=sum(PVunit);   
sumW=sum(Windunit);   

nHour=24;
nDays=365;
nTime=nDays*nHour;

%% legend name
reg_name={'Hokkaido';'Tohoku';'Tokyo';'Chubu';'Hokuriku';'Kansai';'Chugoku';'Shikoku';'Kyushu';'Okinawa'};
leg_label={'PV:WT';'10: 0';' 9: 1';' 5: 5';' 1: 9';' 0:10'};
j_PVratio_set = [0 1 2 6 10 11];
%% cg-cs for each PV ratio
Lsys=10;%total LCOE required

figure(1)

for i_region=1:10
    subplot(2,5,i_region)
    
    for i_year=1
        i=(i_region-1)*3+i_year;
        
         for j=j_PVratio_set
            if j==0
                plot(NaN,NaN,'w-')
            else
                temp_data=output_xg_xs_ab_term_Ecase_full{i,j};
        
                xg=temp_data(:,1);
                xs=temp_data(:,2)/sumD;
                a0=temp_data(:,3)/sumD;%slope a=ΣG
                a0=a0(~isnan(a0));        
                cg = Lsys./[1; (xg(1:end-1) + xs(1:end-1)./a0)];
                cs =[0; cg(2:end)./a0];

                hold on
                plot(cg, cs,'o-','MarkerFaceColor',cmap(j,:),'MarkerEdgeColor',cmap(j,:),'Color',cmap(j,:),'MarkerSize',2)
            end
         end
    end
    xlim([0 10])
    ylim([0 3000])
    grid on
    box on
    set(gca,'FontSize',14)
    
    temp_ax = gca;
    temp_ax.XTick=0:2:10;
    switch i_region
        case 1       
            temp_ax.XTickLabel=[];
            ylabel('$$ c_s $$','fontsize',28,'Interpreter','latex');
        case {2 3 4 5}
            temp_ax.XTickLabel=[];
            temp_ax.YTickLabel=[];            
        case 6
            xlabel('$$ c_g $$','fontsize',28,'Interpreter','latex');
            ylabel('$$ c_s $$','fontsize',28,'Interpreter','latex');
        case {7 8 9 10}
            temp_ax.YTickLabel=[];
            xlabel('$$ c_g $$','fontsize',28,'Interpreter','latex');
    end
    title(reg_name{i_region},'FontSize',10)
end 

legend(leg_label)
set(gcf,'Color',[1 1 1]);


%% plot1 for each region (2018)
figure(2)
for i_region=1:10
    subplot(2,5,i_region)
    
    for i_year=1
        i=(i_region-1)*3+i_year;
        for j=j_PVratio_set
            if j==0
                plot(NaN,NaN,'w-')
            else
                temp_data=output_xg_xs_ab_term_Ecase_full{i,j};
                hold on
                plot(temp_data(:,1),temp_data(:,2)/sumD,'o-','MarkerFaceColor',cmap(j,:),'MarkerEdgeColor',cmap(j,:),'Color',cmap(j,:),'MarkerSize',2)                
            end
        end
    end
    grid on
    box on
    xlim([1 3])
    ylim([0 0.2])
    set(gca,'FontSize',14)
    
    temp_ax = gca;
    switch i_region
        case 1       
            temp_ax.XTickLabel=[];
            ylabel('$$ x_s $$','fontsize',28,'Interpreter','latex');
        case {2 3 4 5}
            temp_ax.XTickLabel=[];
            temp_ax.YTickLabel=[];            
        case 6
            xlabel('$$ x_g $$','fontsize',28,'Interpreter','latex');
            ylabel('$$ x_s $$','fontsize',28,'Interpreter','latex');
        case {7 8 9 10}
            temp_ax.YTickLabel=[];
            xlabel('$$ x_g $$','fontsize',28,'Interpreter','latex');
    end
   title(reg_name{i_region},'FontSize',10)
end
%legend(leg_label)
set(gcf,'Color',[1 1 1]);

%% plot1 for each region (2018) GD plot
figure(3)
for i_region=1:10
    subplot(2,5,i_region)
    
    for i_year=1
        i=(i_region-1)*3+i_year;
        for j=j_PVratio_set
            if j==0
                plot(NaN,NaN,'w-')
            else
                temp_data=output_xg_xs_ab_term_Ecase_full{i,j};
                hold on
                plot([1; temp_data(:,3)/sumD],[1; temp_data(:,4)/sumD],'o-','MarkerFaceColor',cmap(j,:),'MarkerEdgeColor',cmap(j,:),'Color',cmap(j,:),'MarkerSize',2)
            end
        end
        plot([0 1],[0 1],'k-')
    end
    grid on
    box on
    xlim([0 0.8])
    ylim([0 0.8])
    set(gca,'FontSize',14)
    
    temp_ax = gca;
    switch i_region
        case 1       
            temp_ax.XTickLabel=[];
             ylabel('$$ D_{ij} $$','fontsize',28,'Interpreter','latex');
        case {2 3 4 5}
            temp_ax.XTickLabel=[];
            temp_ax.YTickLabel=[];            
        case 6
            xlabel('$$ G_{ij} $$','fontsize',28,'Interpreter','latex');
             ylabel('$$ D_{ij} $$','fontsize',28,'Interpreter','latex');
        case {7 8 9 10}
            temp_ax.YTickLabel=[];
            xlabel('$$ G_{ij} $$','fontsize',28,'Interpreter','latex');
    end
   title(reg_name{i_region},'FontSize',10)
end
%legend(leg_label)
set(gcf,'Color',[1 1 1]);

%% for each region (2016-18)
mks='o^v';

for i_region=1:10
    for i_year=1:3
        i=(i_region-1)*3+i_year;
        
        PVunit=NormalizedData(:,2,i_region,i_year);
        Windunit=NormalizedData(:,3,i_region,i_year);
        rec_GpGw=NaN(100,4);%PVtermでのGp,Gw、WTtermでのGp,Gw        
        for j=1:2
            switch j
                case 1%PVonly
                    temp_data=output_xg_xs_ab_term_Ecase_full{i,1};        
                case 2%WTonly
                    temp_data=output_xg_xs_ab_term_Ecase_full{i,11};
            end
            temp_term0=temp_data(:,[5 6]);
            temp_term=temp_term0(~isnan(temp_term0(:,1)),:);
            n_term=size(temp_term,1);%決定期間の種類数
            
            for i_term=1:n_term%PVonly, WTonlyでの決定期間
                if temp_term(i_term,1)<temp_term(i_term,2)
                    pick_t=temp_term(i_term,1):temp_term(i_term,2);
                else
                    pick_t=[1:temp_term(i_term,2) temp_term(i_term,1):nTime];
                end
                rec_GpGw(i_term,2*(j-1)+[1 2])=[sum(PVunit(pick_t)) sum(Windunit(pick_t))]/sumD;
            end
        end
        rec_GpGw = rec_GpGw(~isnan(sum(rec_GpGw,2)),:);
        
        figure(22)
        subplot(1,2,1)%PVonlyの決定期間でのGp-Gwプロット
        hold on
        plot(rec_GpGw(:,1),rec_GpGw(:,2),[mks(i_year) '-'],'MarkerFaceColor',cmap_reg(i_region,:),'MarkerEdgeColor',cmap_reg(i_region,:),'Color',cmap_reg(i_region,:),'MarkerSize',3);
        subplot(1,2,2)%WTonlyの決定期間でのGw-Gpプロット
        hold on
        plot(rec_GpGw(:,4),rec_GpGw(:,3),[mks(i_year) '-'],'MarkerFaceColor',cmap_reg(i_region,:),'MarkerEdgeColor',cmap_reg(i_region,:),'Color',cmap_reg(i_region,:),'MarkerSize',3);
        
        figure(23)
        %PVonlyの決定期間でのGp-Gwプロット 拡大用
        hold on
        plot(rec_GpGw(:,1),rec_GpGw(:,2),[mks(i_year) '-'],'MarkerFaceColor',cmap_reg(i_region,:),'MarkerEdgeColor',cmap_reg(i_region,:),'Color',cmap_reg(i_region,:),'MarkerSize',3);
    end
end

figure(22)
for i_subplt=1:2
    subplot(1,2,i_subplt)
    plot([0 1],[0 1],'k-')
    grid on
    box on
    xlim([0 0.8])
    ylim([0 0.8])    
    set(gca,'FontSize',14)

    if i_subplt==1
        xlabel('$$ G_{PV} $$','fontsize',28,'Interpreter','latex');
        ylabel('$$ G_{WT} $$','fontsize',28,'Interpreter','latex');
    else
        xlabel('$$ G_{WT} $$','fontsize',28,'Interpreter','latex');
        ylabel('$$ G_{PV} $$','fontsize',28,'Interpreter','latex');
    end
end
set(gcf,'Color',[1 1 1]);

%拡大用
figure(23)
plot([0 1],[0 1],'k-')
grid on
box on
xlim([0 0.05])
ylim([0 0.05]) 
set(gcf,'Color',[1 1 1]);

%% 拡大用1(chubu)
figure(5)
i_region=4;
i_year=1;
i=(i_region-1)*3+i_year;

%1）cg-cs
subplot(1,3,1)
 for j=j_PVratio_set
    if j==0
        plot(NaN,NaN,'w-')
    else
        temp_data=output_xg_xs_ab_term_Ecase_full{i,j};

        xg=temp_data(:,1);
        xs=temp_data(:,2)/sumD;
        a0=temp_data(:,3)/sumD;%傾きa=ΣG
        a0=a0(~isnan(a0));        
        cg = Lsys./[1; (xg(1:end-1) + xs(1:end-1)./a0)];
        cs =[0; cg(2:end)./a0];

        hold on
        plot(cg, cs,'o-','MarkerFaceColor',cmap(j,:),'MarkerEdgeColor',cmap(j,:),'Color',cmap(j,:),'MarkerSize',2)
    end
 end
xlim([3.8 6])
ylim([100 350])
grid on
box on

%2）xg-xs
subplot(1,3,2)
 for j=j_PVratio_set
    if j==0
        plot(NaN,NaN,'w-')
    else
        temp_data=output_xg_xs_ab_term_Ecase_full{i,j};
        hold on
        plot(temp_data(:,1),temp_data(:,2)/sumD,'o-','MarkerFaceColor',cmap(j,:),'MarkerEdgeColor',cmap(j,:),'Color',cmap(j,:),'MarkerSize',2)                
    end
 end
xlim([1.24 1.55])
ylim([0.012 0.025])
grid on
box on


%3）G-D
subplot(1,3,3)
 for j=j_PVratio_set
    if j==0
        plot(NaN,NaN,'w-')
    else
        temp_data=output_xg_xs_ab_term_Ecase_full{i,j};
        hold on
        plot([1; temp_data(:,3)/sumD],[1; temp_data(:,4)/sumD],'o-','MarkerFaceColor',cmap(j,:),'MarkerEdgeColor',cmap(j,:),'Color',cmap(j,:),'MarkerSize',2) 
    end
 end
plot([0 1],[0 1],'k-')
xlim([0.032 0.038])
ylim([0.063 0.07])
grid on
box on
set(gcf,'Color',[1 1 1]);

