ClearAll["Global`*"];
SetDirectory[NotebookDirectory[]];
$HistoryLength=3;
Needs["PlotLegends`"];
ClearAll[exp];
SetAttributes[exp,{HoldAll,Listable}];
exp[co_]:=Module[{a,graf},a=ToString[Unevaluated[co]];graf:=Evaluate[co];Export[Evaluate[a<>".png"],graf,ImageResolution->300]];

pom=Import["dat1a.xls"][[1]];
startdat=pom[[1,1]];
vykony=(10^3)*(Last/@pom);
PmaxOdber=Max[vykony];
PminOdber=Min[vykony];
Print["Pmax= ",PmaxOdber," W,  Pmin= ",PminOdber," W"];
hodina=3600;
ctvrthodina=0.25*hodina;

nden=24*4;
nBase=8*4;
nPeak=nden-nBase;
nStartBase=72;(*index n=0 znamena zacatek prvni base v datech*)

datPpom=Drop[vykony,nStartBase];
datP={#,datPpom[[#+1]]}&/@Range[0,Length[datPpom]-1];

poradiVeDni[n_]:=Mod[n,nden];
base[n_]:=0≤poradiVeDni[n]<nBase;(*da True kdyz poradi n v datP je v Base*)
doKoncePeaku[n_]:=nden-poradiVeDni[n];
nDat=Length[datP];
(*poradiVeDni/@Range[nDat]*)(*poradi mezi 0 nden-1*)

myR:=0.1*Round[10#]&;
t1=14*nden;
t2=t1+30*nden;
datakus=Take[datP,{t1,t2}];

energie=ctvrthodina*Plus@@(datakus/.{a_,b_}:>b);
strednivykon=(energie/((t2-t1)*ctvrthodina));

Pmaxkus=Max[datakus];

pl2=ListPlot[datakus/.{i_,Pi_}:>{i,strednivykon},AxesLabel->{"poradi ctvrthodiny","P , Pstredni  [W]"},PlotLabel->"Pstredni= "<>ToString[myR[0.001strednivykon]]<>"kW,  Pmax= "<>ToString[myR[0.001Pmaxkus]]<>" kW",PlotStyle->{Red, PointSize[0.006]}];

Unom=400;
IminJistic=(Pmaxkus/((Sqrt[3])*Unom));
Print["minimalni proud jistice= ",IminJistic," A"]

bod[{x_,y_}]:=Graphics[{PointSize[0.006],If[base[x],Magenta,Black],Point[{x,y}]}];
body=bod/@datakus;
pla=Show[pl2,body,PlotRange->All]
exp[pla];

veVysTarifu=Select[datakus,!base[#[[1]]]&];
vNizTarifu=Select[datakus,base[#[[1]]]&];
energieVeVysTarifu=ctvrthodina*Plus@@(veVysTarifu/.{i_,Pi_}:>Pi);
energieVNizTarifu=ctvrthodina*Plus@@(vNizTarifu/.{i_,Pi_}:>Pi);

Print["v NT spotrebovano ",energieVNizTarifu," J"]
Print["v VT spotrebovano ",energieVeVysTarifu," J"]

energieveVTprumrovano=Length[veVysTarifu]*strednivykon*ctvrthodina;
energieVNTprumrovano=Length[vNizTarifu]*strednivykon*ctvrthodina;
Print["v NT spotrebovano pri Pstr ",energieVNTprumrovano," J"]
Print["v VT spotrebovano pri Pstr ",energieveVTprumrovano," J"]

IminJisticPrum=(strednivykon/((Sqrt[3])*Unom));
Print["minimalni proud jistice pri rovom. zateizeni= ",IminJisticPrum," A"]

(*vypocty cen, jistic 420A, C 27d*)
mesicniZaJistic=13.43*420;
Print["stala platba za mesic = ",mesicniZaJistic," Kc"];
cenaVT1MWhC27=2251.53;
cenaNT1MWhC27=86.56;
MWh=(10^6)*3600.;
cena1JVT27=(cenaVT1MWhC27/MWh);
cena1JNT27=(cenaNT1MWhC27/MWh);
cenaEnergieVT=energieVeVysTarifu*cena1JVT27;
Print["za energii ve VT platime mesicne: ",cenaEnergieVT," Kc"];
cenaEnergieNT=energieVNizTarifu*cena1JNT27;
Print["za energii v NT platime mesicne: ",cenaEnergieNT," Kc"];
cenaCelkem=mesicniZaJistic+cenaEnergieVT+cenaEnergieNT;
Print["celkem mesicne: ",cenaCelkem," Kc"]

(*vypocty cen, jistic 160A*)
mesicniZaJisticPrum=2149;
Print["stala platba za mesic = ",mesicniZaJisticPrum," Kc"];
cenaEnergieVTPrum=energieveVTprumrovano*cena1JVT27;
Print["za energii ve VT platime mesicne: ",cenaEnergieVTPrum," Kc"];
cenaEnergieNTPrum=energieVNTprumrovano*cena1JNT27;
Print["za energii v NT platime mesicne: ",cenaEnergieNTPrum," Kc"];
cenaCelkemPrum=mesicniZaJisticPrum+cenaEnergieVTPrum+cenaEnergieNTPrum;
Print["celkem mesicne: ",cenaCelkemPrum," Kc"]
Print["usetrili jsme mesicne: ",cenaCelkem-cenaCelkemPrum," Kc"]

(*vypocty cen, jistic 420A, C 25d*)

mesicniZaJistic=11.1*420;
Print["stala platba za mesic = ",mesicniZaJistic," Kc"];
cenaVT1MWhC25=1860.77;
cenaNT1MWhC25=71.54;
MWh=(10^6)*3600.;
cena1JVT25=(cenaVT1MWhC25/MWh);
cena1JNT25=(cenaNT1MWhC25/MWh);
cenaEnergieVT=energieVeVysTarifu*cena1JVT25;
Print["za energii ve VT platime mesicne: ",cenaEnergieVT," Kc"];
cenaEnergieNT=energieVNizTarifu*cena1JNT25;
Print["za energii v NT platime mesicne: ",cenaEnergieNT," Kc"];
cenaCelkem=mesicniZaJistic+cenaEnergieVT+cenaEnergieNT;
Print["celkem mesicne: ",cenaCelkem," Kc"]

(*vypocty cen, jistic 160A*)
mesicniZaJisticPrum=1776;
Print["stala platba za mesic = ",mesicniZaJisticPrum," Kc"];
cenaEnergieVTPrum=energieveVTprumrovano*cena1JVT25;
Print["za energii ve VT platime mesicne: ",cenaEnergieVTPrum," Kc"];
cenaEnergieNTPrum=energieVNTprumrovano*cena1JNT25;
Print["za energii v NT platime mesicne: ",cenaEnergieNTPrum," Kc"];
cenaCelkemPrum=mesicniZaJisticPrum+cenaEnergieVTPrum+cenaEnergieNTPrum;
Print["celkem mesicne: ",cenaCelkemPrum," Kc"]
Print["usetrili jsme mesicne: ",cenaCelkem-cenaCelkemPrum," Kc"]