function [H,p,h,pP,bod]=dga_duval(t,X_CH4,X_C2H2,X_C2H4,X_H2,X_C2H6,X_CO,X_CO2,tau,out,k,mezeC1,mezeC2,mezeC3,mezeC4)

% pP ... postupne pravdepodobnosti PD, T1, T2, T3, D1, D2, DT

%function [p pD1 pD2 pDT pT3 pT2 pT1 pPD H h y]=duval(t,X_CH4,X_C2H2,X_C2H4,X_H2,X_C2H6,X_CO,X_CO2,tau,out,k)
%function [p]=duval(X(:,1),X(:,2),X(:,3),X(:,4),X(:,5),X(:,6),X(:,7),X(:,8),tau,out)
%function [p]=duval(X)
% tname=str2mat('Oblast D1',...
%               'Test based on runs up and down: ',...
%               'Test based on Kendall coefficient: ',...
%               'Test based on Spearman coefficient: ',...
%               'Median test: '...
%         );
%plot3([4 10],[1 1],[0 0]);
% out=1;
% tau=100;
% 
% t=X(:,1);
% X_CH4=X(:,2);
% X_C2H2=X(:,3);
% X_C2H4=X(:,4);
% X_H2=X(:,5);
% X_C2H6=X(:,6);
% X_CO=X(:,7);
% X_CO2=X(:,8);

clc
[t_CH4,CH4,l]=vyhazet_radky(t,X_CH4);
[t_C2H2,C2H2,l]=vyhazet_radky(t,X_C2H2);
[t_C2H4,C2H4,l]=vyhazet_radky(t,X_C2H4);
[t_H2,H2,l]=vyhazet_radky(t,X_H2);
[t_C2H6,C2H6,l]=vyhazet_radky(t,X_C2H6);
[t_CO,CO,l]=vyhazet_radky(t,X_CO);
[t_CO2,CO2,l]=vyhazet_radky(t,X_CO2);

[tspol,CH4spol,C2H2spol,C2H4spol,l]=vyhazet_radky3(t,X_CH4,X_C2H2,X_C2H4);


[tspol1,CH4spol1,C2H2spol1,C2H4spol1,H2spol1,C2H6spol1,COspol1,l]=vyhazet_radky6(t,X_CH4,X_C2H2,X_C2H4,X_H2,X_C2H6,X_CO);
n=length(tspol1);
for i=1:n
    soucet(i)=CH4spol1(i)+C2H2spol1(i)+C2H4spol1(i)+H2spol1(i)+C2H6spol1(i)+COspol1(i);
end
t_soucet=tspol1;

[H,p]=pravd_prekroceni_nove(mezeC1,mezeC2,mezeC3,mezeC4,t_CH4,t_C2H2,t_C2H4,t_H2,t_C2H6,t_CO,t_CO2,t_soucet,CH4,C2H2,C2H4,H2,C2H6,CO,CO2,soucet,tau,out,k);

% n_CH4=length(CH4);
% n_C2H4=length(C2H4);
% n_C2H2=length(C2H2);
% n=min(min(n_CH4,n_C2H2),n_C2H4);
% l=1;
% % vytvoření řad, kde je společný čas
% for i=1:n_CH4 
%     I=t_CH4(i);
%     for j=1:n_C2H4
%         if t_C2H4(j)==I;
%             for k=1:n_C2H2
%                 if t_C2H2(k)==I;
%                     tspol(l)=I;
% %                     I=0;
%                      l=l+1;
%                 end;
%                
%             end;
%         end;    
%     end;
% end;
% 
% ntspol=length(tspol)-1;
% for i=1:ntspol
%     ttspol(i)=tspol(i);
% end;
% k=1;
% for i=1:length(t)
%     if t(i)==ttspol(k)-1
%        [t(i) X_CH4(i) X_C2H2(i) X_C2H4(i)];
%        k=k+1;
%     end;
% end;   
%    
% [t X_CH4 X_C2H2 X_C2H4]
% 
clc
[tspol,CH4spol,C2H2spol,C2H4spol];
plyny=[CH4spol,C2H2spol,C2H4spol];
s=size([CH4spol,C2H2spol,C2H4spol]);
s=s(1);
for i=1:s(1)
    sumaplyny(i)=sum(plyny(i,:)); 
end;
sumaplyny=sumaplyny';
for i=1:s(1)
    pomery(i,:)=plyny(i,:)/sumaplyny(i);
end;

clc
%Oblast D1
[hD1(2),pp2,yD1(2)]=pred_int2(tspol,pomery(:,3),tau,0,0.23,out,k);
[hD1(3),pp3,yD1(3)]=pred_int2(tspol,pomery(:,2),tau,0.13,1,out,k);
[hD1(1),pp1,yD1(1)]=pred_int2(tspol,pomery(:,1),tau,0,0.87,out,k);
pD1=pp2*pp3*pp1;


%Oblast D2
[hD2A(2),pp2A,yD2A(2)]=pred_int2(tspol,pomery(:,3),tau,0.23,0.4,out,k);
[hD2A(3),pp3A,yD2A(3)]=pred_int2(tspol,pomery(:,2),tau,0.13,0.77,out,k);
[hD2A(1),pp1A,yD2A(1)]=pred_int2(tspol,pomery(:,1),tau,0,0.64,out,k);
pD2A=pp2A*pp3A*pp1A;


[hD2B(2),pp2B,yD2B(2)]=pred_int2(tspol,pomery(:,3),tau,0.4,0.71,out,k);
[hD2B(3),pp3B,yD2B(3)]=pred_int2(tspol,pomery(:,2),tau,0.29,0.6,out,k);
[hD2B(1),pp1B,yD2B(1)]=pred_int2(tspol,pomery(:,1),tau,0,0.31,out,k);
pD2B=pp2B*pp3B*pp1B;

pD2=pD2A+pD2B;

%Oblast DT
[hDTA(2),pp2A,yDTA(2)]=pred_int2(tspol,pomery(:,3),tau,0.4,0.85,out,k);
[hDTA(3),pp3A,yDTA(3)]=pred_int2(tspol,pomery(:,2),tau,0.15,0.29,out,k);
[hDTA(1),pp1A,yDTA(1)]=pred_int2(tspol,pomery(:,1),tau,0,0.45,out,k);
pDTA=pp2A*pp3A*pp1A;


[hDTB(2),pp2B,yDTB(2)]=pred_int2(tspol,pomery(:,3),tau,0.4,0.5,out,k);
[hDTB(3),pp3B,yDTB(3)]=pred_int2(tspol,pomery(:,2),tau,0.13,0.15,out,k);
[hDTB(1),pp1B,yDTB(1)]=pred_int2(tspol,pomery(:,1),tau,0.35,0.47,out,k);
pDTB=pp2B*pp3B;

[hDTC(2),pp2C,yDTC(2)]=pred_int2(tspol,pomery(:,3),tau,0,0.5,out,k);
[hDTC(3),pp3C,yDTC(3)]=pred_int2(tspol,pomery(:,2),tau,0.04,0.13,out,k);
[hDTC(1),pp1C,yDTC(1)]=pred_int2(tspol,pomery(:,1),tau,0.98,1,out,k);
pDTC=pp2C*pp3C;

pDT=pDTA+pDTB+pDTC;

%Oblast T3
[hT3(2),pp2,yT3(2)]=pred_int2(tspol,pomery(:,3),tau,0.5,1,out,k);
[hT3(3),pp3,yT3(3)]=pred_int2(tspol,pomery(:,2),tau,0,0.15,out,k);
[hT3(1),pp1,yT3(1)]=pred_int2(tspol,pomery(:,1),tau,0,0.5,out,k);
pT3=pp2*pp3*pp1;

%Oblast T2
[hT2(2),pp2,yT2(2)]=pred_int2(tspol,pomery(:,3),tau,0.2,0.5,out,k);
[hT2(3),pp3,yT2(3)]=pred_int2(tspol,pomery(:,2),tau,0,0.04,out,k);
[hT2(1),pp1,yT2(1)]=pred_int2(tspol,pomery(:,1),tau,0.46,0.80,out,k);
pT2=pp2*pp3;

%Oblast T1
[hT1A(2),pp2A,yT1A(2)]=pred_int2(tspol,pomery(:,3),tau,0.02,0.2,out,k);
[hT1A(3),pp3A,yT1A(3)]=pred_int2(tspol,pomery(:,2),tau,0,0.04,out,k);
[hT1A(1),pp1A,yT1A(1)]=pred_int2(tspol,pomery(:,1),tau,0.78,0.98,out,k);
pT1A=pp2A*pp3A;


[hT1B(2),pp2B,yT1B(2)]=pred_int2(tspol,pomery(:,3),tau,0,0.02,out,k);
[hT1B(3),pp3B,yT1B(3)]=pred_int2(tspol,pomery(:,2),tau,0,0.04,out,k);
[hT1B(1),pp1B,yT1B(1)]=pred_int2(tspol,pomery(:,1),tau,0.94,0.98,out,k);
pT1B=pp2B*pp3B*pp1B;

pT1=pT1A+pT1B;

%Oblast PD
[hPD(2),pp2,yPD(2)]=pred_int2(tspol,pomery(:,3),tau,0,0.02,out,k);
[hPD(3),pp3,yPD(3)]=pred_int2(tspol,pomery(:,2),tau,0,0.02,out,k);
[hPD(1),pp1,yPD(1)]=pred_int2(tspol,pomery(:,1),tau,0.98,1,out,k);
pPD=pp1*pp2*pp3;
clc
% disp('Pravděpodobnost pro aplikaci metody')
% p
% disp('Oblast D1')
for i=1:4
ppD1(i)=pD1*p(i);
end
% %  hD1
% %  yD1
% disp('Oblast D2')
% % pD2A
% % pD2B
for i=1:4
ppD2(i)=pD2*p(i);
end
% % hD2A
% % yD2A
% % hD2B
% % yD2B
% disp('Oblast DT')
% % pDTA
% % pDTB
% % pDTC
for i=1:4
ppDT(i)=pDT*p(i);
end
% % hDTA
% % yDTA
% % hDTB
% % yDTB
% % hDTC
% % yDTC
% disp('Oblast T3')
for i=1:4
ppT3(i)=pT3*p(i);
end
% % hT3
% % yT3
% disp('Oblast T2')
for i=1:4
ppT2(i)=pT2*p(i);
end
% % hT2
% % yT2
% disp('Oblast T1')
% % pT1A
% % pT1B
for i=1:4
ppT1(i)=pT1*p(i);
end
% % hT1A
% % yT1A
% % hT1B
% % yT1B
% disp('Oblast PD')
for i=1:4
ppPD(i)=pPD*p(i);
end
% % hPD
% % yPD
% disp('Test, zda mají všechny plyny normální rozdělení')
% H
% disp('Test, zda mají poměry plynů CH4,C2H2 a C2H4 normální rozdělení')
h=hD1;
% disp('Souřadnice nejpravděpodobnějších poměrů plynů v Duvalově trojúhelníku')
bod=yD1;
pP=[ppPD;ppT1;ppT2;ppT3;ppD1;ppD2;ppDT]';

[bod1]=dga_duval_rozdily( t,X_CH4,X_C2H2,X_C2H4,tau,out,k);

duval_obrazek( bod, bod1 );

clc

