ClearAll["Global`*"];
SetDirectory[NotebookDirectory[]];
$HistoryLength=3;

pom=Import["dat1a.xls"][[1]];
vykony=(Last/@pom);
PmaxOdber=Max[vykony];
PminOdber=Min[vykony];
Print["Pmax= ",PmaxOdber," kW,  Pmin= ",PminOdber," kW"];
hodina=3600;
ctvrthodina=0.25*hodina;
myR:=0.01*Round[100#]&;

(*"Tesla podle http://rameznaam.com/2015/04/30/tesla-powerwall-battery-economics-almost-there/"*)

kurzDollar=24.75;
accdata=(({{"H20pumping", 2200, 175}, {"compressedAir", 4400, 120}, {"NaSbat", 5400, 270}, {"SMHbat", 6700, 600}, {"VanadRedoxbat", 7000, 510}, {"FeCrbat", 3200, 170}, {"ZnBrbat", 4200, 210}, {"Pbbat", 3900, 220}, {"flywheel", 4200, 375}, {"LiONbat", 7000, 1000}, {"Teslabat", 428, 120}}))/.{co_,sic_,lcoe_}:>{co,kurzDollar*sic,kurzDollar*lcoe};

platbaJistic[Paccu_]:=Module[{rezerva=0.05,Unom=400,iMin,cena1A=13.43},
iMin=Round[1000((PmaxOdber+Paccu)/((Sqrt[3])*Unom))*(1+rezerva)];
12*iMin*cena1A
];

rocniEnergie=((8760*4.)/(1000Length[vykony]))*Plus@@vykony;

denneNThodin=8;
denneVThodin=24-denneNThodin;
rocniEnergieveVT=(denneVThodin/24)*rocniEnergie;
rocniEnergievNT=(denneNThodin/24)*rocniEnergie;
Print["rocni spotreba= ",(10^3)myR[(rocniEnergie/(10^3))]," MWh"];
Print["rocni spotreba ve VT= ",(10^3)myR[(rocniEnergieveVT/(10^3))]," MWh"];
Print["rocni spotreba v NT= ",(10^3)myR[(rocniEnergievNT/(10^3))]," MWh"];

cenaVT1MWhC25=1860.77;
cenaNT1MWhC25=71.54;
platbaDistribuceBezAku=platbaJistic[0]+rocniEnergieveVT*cenaVT1MWhC25+rocniEnergievNT*cenaNT1MWhC25;
Print["platba za distribuci bez akumulace= ",platbaDistribuceBezAku," Kc"];

ClearAll[vyhodnot1];
vyhodnot1[{Paccu_,diskont_,typAccu:{co_,sic1_,lcoe1_},{kSIC_,kLCOE_,pocetRoku_}}]:=Module[{sic,lcoe,investice,WNT,WVT,Waccu,nhodin=365*24,kCenaDistribuce=0.01,rocniPlatba,rocniNaklady,r,rocniPlatbaBezAccu,PDN,rocniNediskontovanaUspora,},
sic=sic1*kSIC;
lcoe=lcoe1*kLCOE;
investice=Paccu*sic;
Waccu=(denneNThodin/24.)*nhodin*(Paccu/1000.);
WNT=rocniEnergievNT+Waccu;
WVT=rocniEnergieveVT-Waccu;
rocniPlatba[rok_]:=(platbaJistic[Paccu]+WVT*cenaVT1MWhC25+WNT*cenaNT1MWhC25)*(((1+kCenaDistribuce))^(rok-1));
rocniPlatbaBezAccu[rok_]:=platbaDistribuceBezAku*(((1+kCenaDistribuce))^(rok-1));
rocniNaklady[rok_]:=rocniPlatba[rok]+lcoe*Waccu;
r=(diskont/100);
rocniNediskontovanaUspora[rok_]:=rocniPlatbaBezAccu[rok]-rocniNaklady[rok];
PDN=(investice/(rocniNediskontovanaUspora[1]));


If[0≤PDN≤10,{typAccu,PDN},{typAccu,"nemozne"}]
];

ClearAll[vyhodnot3];
vyhodnot3[{Paccu_,diskont_,{co_,sic1_,lcoe1_},{kSIC_,kLCOE_,pocetRoku_}}]:=Module[{sic,lcoe,investice,WNT,WVT,Waccu,nhodin=365*24,kCenaDistribuce=0.01,rocniPlatba,rocniNaklady,r,rocniPlatbaBezAccu,PDN,rocniNediskontovanaUspora,NPV,NPVfce,nLet,discont,irrpom,irr,diskontovanaDobaNavratnosti},
sic=sic1*kSIC;
lcoe=lcoe1*kLCOE;
investice=Paccu*sic;
Waccu=(denneNThodin/24)*nhodin*(Paccu/1000);
WNT=rocniEnergievNT+Waccu;
WVT=rocniEnergieveVT-Waccu;
rocniPlatba[rok_]:=(platbaJistic[Paccu]+WVT*cenaVT1MWhC25+WNT*cenaNT1MWhC25)*(((1+kCenaDistribuce))^(rok-1));
rocniPlatbaBezAccu[rok_]:=platbaDistribuceBezAku*(((1+kCenaDistribuce))^(rok-1));
rocniNaklady[rok_]:=rocniPlatba[rok]+lcoe*Waccu;
r=(diskont/100);
rocniNediskontovanaUspora[rok_]:=rocniPlatbaBezAccu[rok]-rocniNaklady[rok];
PDN=(investice/(rocniNediskontovanaUspora[1]));
NPVfce[discont_,nLet_]:=-investice+(∑_(rok=1)^nLet)((rocniNediskontovanaUspora[rok])/(((1+discont))^(rok-1)));
NPV=NPVfce[r,pocetRoku];
diskontovanaDobaNavratnosti=Quiet@Interpolation[{NPVfce[r,#],#}&/@Range[30]][0];
irr=Quiet@Interpolation[{NPVfce[#,pocetRoku],#}&/@Range[0,5,0.02]][0];
If[0≤diskontovanaDobaNavratnosti≤pocetRoku,{PDN,diskontovanaDobaNavratnosti,NPV,100irr,{sic,lcoe}},{"nemozne","nemozne","nemozne","nemozne"}]

];

ClearAll[zpracuj];
zpracuj[{Paccu_,diskont_,{kSIC_,kLCOE_,pocetRoku_}}]:=Module[{vydano1,mozne,vys},
vydano1=vyhodnot1[{Paccu,diskont,#,{kSIC,kLCOE,pocetRoku}}]&/@accdata;
mozne=Select[vydano1,NumericQ[#[[2]]]&]/.{{co_,sic1_,lcoe1_},PDN_}:>{co,sic1,lcoe1};
vys={#[[1]],vyhodnot3[{Paccu,diskont,#,{kSIC,kLCOE,pocetRoku}}]}&/@mozne;
Sort[vys,#1[[2,3]]>#2[[2,3]]&]
];
zpracuj[{100,5.5,{0.2,0.1,15}}]