clc;
clear all;
close all;
% Read exle file
filename = 'C:\PhD in MCM\Dofasco project\MeOH\GAMS_MEOH\NPVwithtax3.xlsx';
Excel = actxserver('Excel.Application');
Workbooks = Excel.Workbooks;
Excel.Visible =1;

sheet = 9;
x1Range = 'D1:G6';  %NPV factors
x2Range = 'N1:R7';  %Location effects
x3Range = 'D1:H1';  %AF factor
x4Range = 'B1:K27';  %Methanol history price and PPP
x5Range = 'B1:K27';  %Electricity history price

[A D both] = xlsread(filename,sheet,x1Range);
[B C both] = xlsread(filename,sheet,x2Range);   % B read numbers; C read text. both means read both
network_station_coordinates = both;
E = xlsread (filename,10,x3Range);
F = xlsread(filename,11,x4Range);
G = xlsread(filename,12,x5Range);

ECOG = 214288224;   % status-quo electricity generated from COG. kwh
COM = 19.1;    % COM of CCPP million $ TPC plus TOC
FCI = 63.19;      % FCI of CCPP million $
TW = 281015128;    %CCPP additional electricity generated kWh.


x = -20:10:200;       % electricity price range $/MWH


for j = 1:1:1  % AF factor
    
    for i =  1:1:2     % case study
        figure (i+(j-1)*5)

    if i==1
        a = 5;      %eliminate error
        y = 1.4493*x+345.77;      
        y1 = 1.4638*x+293.25;     
        X = 92.41;                
        X1 = 90.95;                %Elec price with uBFG TCO2 @ 50$/ton
        ymc1 = 3.9209*x+125.24-a;      
        ymc2 = 3.5165*x+111.44-a;     
        fplot(@(x) 1.4493*x+345.77, [0 X],'r')      % y MeOH price with uBFG TCO2 @present
        hold on
        fplot(@(x) 3.9209*x+125.24-a,[X 160],'b')      %ycm1 Decision line for either MeOH or CCPP with uBFG TCO2 @present
        hold on
        plot([X X],[0 1.4493*X+345.77],'g')         %X Elec price with uBFG TCO2 @ present
        hold on
        fplot(@(x) 1.4638*x+293.25, [0 X1],'r-.')    % y1 MeOH price with uBFG TCO2 @ 50$/ton
        hold on
        fplot(@(x) 3.5165*x+111.44-a, [X1 160],'b-.')  %ycm2 Decision line for either MeOH or CCPP with uBFG TCO2 @50$/ton
        hold on
        plot([X1 X1],[0 1.4638*X1+293.25],'g-.')         %X1 elec price with uBFG TCO2 @ 50$/ton
        hold on

    elseif i==2
        a = 5;
        y = 1.4558*x+375.83;
        y1 = 1.4554*x+348.45;
        X = 96.57;
        X1 = 67.17;
        ymc1 = 3.6503*x+167.44-a;
        ymc2 = 3.7647*x+192.74-a;
        fplot(@(x) 1.4558*x+375.83, [0 X],'r')      % y MeOH price with uBFG TCO2 @present
        hold on
        fplot(@(x) 1.4554*x+348.45,[0  X1],'r-.')      %y1 Decision line for either MeOH or CCPP with uBFG TCO2 @present
        hold on
        plot([X X],[0 1.4558*X+375.83],'g')         %X Elec price with uBFG TCO2 @ present
        hold on
        plot([X1 X1],[0 1.4554*X1+348.45],'g-.')     %X1 elec price with uBFG TCO2 @ 50$/ton
        hold on
        fplot(@(x) 3.6503*x+167.44-a, [X 160],'b')    % ycm1 chose MeOH or CCPP TCO2 @ present 
        hold on
        fplot(@(x) 3.7647*x+192.74-a, [X1 160],'b-.')  % ycm2 Decision line for either MeOH or CCPP with uBFG TCO2 @50$/ton
        hold on

    elseif i == 3
        y = 1.4575*x+358.52;
        y1 = 1.459*x+338.5;
        X = 91.93;
        X1 = 86.03;
        ymc1 = 3.4773*x+175.48;
        ymc2 = 3.4499*x+169.73;
        
        fplot(@(x) 1.4575*x+358.52, [0 X],'r')      % y MeOH price with uBFG TCO2 @present
        hold on
        fplot(@(x) 1.459*x+338.5,[0 X1],'r-.')      %y1 Decision line for either MeOH or CCPP with uBFG TCO2 @ 50$/ton
        hold on
        plot([X X],[0 1.4575*X+358.52],'g')         %X Elec price with uBFG TCO2 @ present
        hold on
        plot([X1 X1],[0 1.459*X1+338.5],'g-.')     %X1 elec price with uBFG TCO2 @ 50$/ton
        hold on
        fplot(@(x) 3.4773*x+175.48, [X 160],'b')    % ycm1 chose MeOH or CCPP TCO2 @ present
        hold on
        fplot(@(x) 3.4499*x+169.73, [X1 160],'b-.')  % ycm2 Decision line for either MeOH or CCPP with uBFG TCO2 @50$/ton
        hold on

        
    elseif i==4
        y= 1.4559*x+170.11;
        y1 = 1.4564*x+164.74;
        X = 40.48;
        X1 = 0.85;
        a = 0;
        ymc1 = 4.0103*x+65.432 +a;
        ymc2 = 4.0103*x+160.31 +a;
        
        fplot(@(x) 1.4559*x+170.11, [-20 X],'r')      % y MeOH price with uBFG TCO2 @present
        hold on
        fplot(@(x) 1.4564*x+164.74,[-20 X1],'r-.')      %y1 Decision line for either MeOH or CCPP with uBFG TCO2 @ 50$/ton
        hold on
        plot([X X],[0 1.4559*X+170.11],'g')         %X Elec price with uBFG TCO2 @ present
        hold on
        plot([X1 X1],[0 1.4564*X1+164.74],'g-.')     %X1 elec price with uBFG TCO2 @ 50$/ton
        hold on
        fplot(@(x) 4.0103*x+65.432 +a, [X 180],'b')    % ycm1 chose MeOH or CCPP TCO2 @ present
        hold on
        fplot(@(x) 4.0103*x+160.31 +a, [X1 180],'b-.')  % ycm2 Decision line for either MeOH or CCPP with uBFG TCO2 @50$/ton
        hold on
    else
        y = 1.4567*x+199.26;
        y1 = 1.4591*x+210.9;
        X = 51.28;
        X1 = -1.92;
        b = 0;
        ymc1= 3.8406*x+75.819 +b;
        ymc2 = 3.8406*x+215.02+b;
        
        fplot(@(x) 1.4567*x+199.26, [-20 X],'r')      % y MeOH price with uBFG TCO2 @present
        hold on
        fplot(@(x) 1.4591*x+210.9,[-20 X1],'r-.')      %y1 Decision line for either MeOH or CCPP with uBFG TCO2 @ 50$/ton
        hold on
        plot([X X],[0 1.4567*X+199.26],'g')         %X Elec price with uBFG TCO2 @ present
        hold on
        plot([X1 X1],[0 1.4591*X1+210.9],'g-.')     %X1 elec price with uBFG TCO2 @ 50$/ton
        hold on
        fplot(@(x) 3.8406*x+75.819 +b, [X 160],'b')    % ycm1 chose MeOH or CCPP TCO2 @ present
        hold on
        fplot(@(x) 3.8406*x+215.02+b, [X1 160],'b-.')  % ycm2 Decision line for either MeOH or CCPP with uBFG TCO2 @50$/ton
        hold on
    end


    for k = 1:1:13
scatter(G(1+2*(k-1),i),F(1+2*(k-1),i),25, 'm');
% dotname = num2str(19-k);     %convert number to string  
% text(G(1+2*(k-1),i)+3,F(1+2*(k-1),i),dotname); 
title(C(1,i));
% 
xlabel('Electricity price ($/MWH)');
ylabel('Cumulative present value (million $)');
        

% region tex
% text (10,700,'Build MeOH plant','Color','g')
% text (10,200,'Status-quo','Color','b')
% text(90,200,'Build CCPP','Color','g')
% text(90,550,'Build MeOH plant or CCPP','Color','r')

% legend('Carbon tax in year 2018 MeOH','Carbon tax = 0 CAD/tonne CO_2e','Carbon tax = 50 CAD/tonne CO_2e','Electricity Price when NPV = 0')
% legend('NPV= 0 for MeOH, TCO2 = Present','NPV= 0 for MeOH, TCO_2 = 50 $/ton','NPV = 0 for CCPP, TCO2 = Present','NPV = 0 for CCPP, TCO_2 = 50 $/ton')




    end
    
    end
end
