function [PRDnorm,PRDlognorm,PRDgev,PRDunif]=DistrFce(Tg,pocetvinuti,krithodnota,smerpstporuchy,rozdeleni1,rozdeleni2,rozdeleni3,rozdeleni4,k)
clc
% _________________________________________________________________________
% rozdeleni=typ rozdeleni, kterym se dany rok fituje, 1=normální,
% 2=lognormální, 3=extremni hodnoty, 4=rovnomerne
% _________________________________________________________________________
% t=Tg(:,1);
% Tg=Tg(:,2:end);
clc
[t1,X1,l]=vyhazet_radky4(Tg(:,1),Tg(:,2:end));
X=[t1,X1];
nn=size(X1);
n=nn(2);

if pocetvinuti==2
   i=0;
    for j=1:40
    i=i+2;
    tau(j)=i;
    end
   tau=[0,tau];
else
    for j=1:80
    tau(j)=j;
    end
    tau=[0,tau];
end


if k==1
b1=[];
j=1;
for i=1:n
    f1=fit(log(t1),log(X1(:,i)),'poly1');
    b1(:,j)=coeffvalues(f1);
j=j+1;
nnn=length(tau);
for jj=1:nnn
X11nove(jj,i)=exp(b1(1,i))*(tau(jj)^b1(2,i));
end    
end
for i=1:n
for jj=2:nnn
X1nove(jj-1,i)=abs(X11nove(jj,i)-X11nove(jj-1,i))/abs(X11nove(jj-1,i));
end    
end
end

if k==2;
b2=[];
j=1;
 for i=1:n
f2=fit(t1,X1(:,i),'poly1');
b2(:,j)=coeffvalues(f2)';
j=j+1;
%     f1(i)=fit(t1,X1(:,i),'poly1');
%     b5(i)=coeffvalues(f1(i));
% b5
% for i=1:n
% X_lin=b5(1,i)+b5(2,i).*tau;
% X1nove(:,i)=X_lin;
% end    
nnn=length(tau);
for jj=1:nnn
% X1nove(:,i)=b5(2,i)+b5(1,i).*tau;
X11nove(jj,i)=b2(2,i)+b2(1,i)*tau(jj);
end    
 end
for i=1:n
for jj=2:nnn
X1nove(jj-1,i)=abs(X11nove(jj,i)-X11nove(jj-1,i))/abs(X11nove(jj-1,i));
end    
end 
end


if k==3
b3=[];
j=1;
for i=1:n
    f3=fit(t1,X1(:,i),'exp1');
    b3(:,j)=coeffvalues(f3);
j=j+1;
nnn=length(tau);
for jj=1:nnn
% X1nove(:,i)=b5(2,i)+b5(1,i).*tau(jj);
X11nove(jj,i)=b3(1,i)*exp(b3(2,i)*tau(jj));  
end    
end
for i=1:n
for jj=2:nnn
X1nove(jj-1,i)=abs(X11nove(jj,i)-X11nove(jj-1,i))/abs(X11nove(jj-1,i));
end    
end 

end

if k==4
b4=[];
j=1;
for i=1:n
    X=X1(:,i);
    r=ksr(t1,X);
    f4=fit(t1,r.f','poly2');
    b4(:,j)=coeffvalues(f4);
j=j+1;
nnn=length(tau);
for jj=1:nnn
% X1nove(:,i)=b5(2,i)+b5(1,i).*tau(jj);
X11nove(jj,i)=b4(1,i)*tau(jj)^2+b4(2,i)*tau(jj)+b4(3,i);
end    
end
for i=1:n
for jj=2:nnn
X1nove(jj-1,i)=abs(X11nove(jj,i)-X11nove(jj-1,i))/abs(X11nove(jj-1,i));
end    
end 

end

if k==5
b5=[];
j=1;
for i=1:n
    X=X1(:,i);
    klp=smooth(X,3);
    f5=fit(t1,klp,'poly2');
    b5(:,j)=coeffvalues(f5);
j=j+1;
nnn=length(tau);
for jj=1:nnn
% X1nove(:,i)=b5(2,i)+b5(1,i).*tau(jj);
X11nove(jj,i)=b5(1,i)*tau(jj)^2+b5(2,i)*tau(jj)+b5(3,i);
end    
end
for i=1:n
for jj=2:nnn
X1nove(jj-1,i)=abs(X11nove(jj,i)-X11nove(jj-1,i))/abs(X11nove(jj-1,i));
end    
end 

end


% X_lin=b5(2)+b5(1).*t;
% y=b5(2)+b5(1)*tau;


if pocetvinuti==2
    for i=1:40
     if rozdeleni1==1   
        Hnorm(i)=chi2gof(X1nove(i,:),'cdf',{@normcdf,mean(X1nove(i,:)),std(X1nove(i,:))});
Hnorm=not(Hnorm);%normalni rozdeleni
%--------------------------------------------------------------------------
%H   ...vysledek testu dobre shody (analyza chyby), 1-normalni rozdeleni
%        nezamitnuto
%--------------------------------------------------------------------------

pstporuchynorm(i)=1-normcdf(krithodnota,mean(X1nove(i,:)),std(X1nove(i,:)));
     
     end
     if rozdeleni2==1
Hlognorm(i)=chi2gof(X1nove(i,:),'cdf',{@logncdf,mean(X1nove(i,:)),std(X1nove(i,:))});
Hlognorm=not(Hlognorm);%lognormalni rozdeleni
pstporuchylognorm(i)=1-logncdf(krithodnota,mean(X1nove(i,:)),std(X1nove(i,:)));
    
     end
%--------------------------------------------------------------------------
%[p] = wblfit(X1nove(i,:));
% Hwbl(i)=chi2gof(X1nove(i,:),'cdf',{@wblcdf,p(1),p(2)});
% Hwbl=not(Hwbl); ... neprojde, protože potřebuje v X1nove jen kladné
% hodnoty
%--------------------------------------------------------------------------
if rozdeleni3==1
[p] = gevfit(X1nove(i,:));
Hgev(i)=chi2gof(X1nove(i,:),'cdf',{@gevcdf,p(1),p(2),p(3)});
Hgev=not(Hgev);   %extremni hodnoty
pstporuchygev(i)=1-gevcdf(krithodnota,p(1),p(2),p(3));

end
%--------------------------------------------------------------------------
% [p] = betafit(X1nove(i,:));
% Hbeta(i)=chi2gof(X1nove(i,:),'cdf',{@betacdf,p(1),p(2)});
% Hbeta=not(Hbeta);...neprojde, potrebuje hodnoty mensi jak 1
%--------------------------------------------------------------------------   
%  p = geofit(X1nove(i,:));
% Hgeo(i)=chi2gof(X1nove(i,:),'cdf',{@geocdf,p});
% Hgeo=not(Hgeo);   %geometricke rozdeleni
if rozdeleni4==1
[p] = [min(X1nove(i,:)),max(X1nove(i,:))];
Hunif(i)=chi2gof(X1nove(i,:),'cdf',{@unifcdf,p(1),p(2)});
Hunif=not(Hunif); 
pstporuchyunif(i)=1-unifcdf(krithodnota,p(1),p(2));
end
    end
PRDnormv=[];
PRDlognormv=[];
PRDgevv=[];
PRDunifv=[];

 for i=1:40
if rozdeleni1==1
    if pstporuchynorm(i)>smerpstporuchy
PRDnormv(i)=i;
    end
    else PRDnorm=0;
end
if rozdeleni2==1
     if pstporuchylognorm(i)>smerpstporuchy
PRDlognormv(i)=i;
     end
     else PRDlognorm=0;
end
if rozdeleni3==1
    if pstporuchygev(i)>smerpstporuchy
PRDgevv(i)=i;
    end
    else PRDgev=0;
end
if rozdeleni4==1
    if pstporuchyunif(i)>smerpstporuchy
PRDunifv(i)=i;
    end
    else PRDunif=0;
end
 end
 
PRDnorm=2*find(PRDnormv,1);
PRDlognorm=2*find(PRDlognormv,1);
PRDgev=2*find(PRDgevv,1);
PRDunif=2*find(PRDunifv,1);


else
    for i=1:80
if rozdeleni1==1
Hnorm(i)=chi2gof(X1nove(i,:),'cdf',{@normcdf,mean(X1nove(i,:)),std(X1nove(i,:))});
Hnorm=not(Hnorm);
pstporuchynorm(i)=1-normcdf(krithodnota,mean(X1nove(i,:)),std(X1nove(i,:)));
end
if rozdeleni2==1
Hlognorm(i)=chi2gof(X1nove(i,:),'cdf',{@logncdf,mean(X1nove(i,:)),std(X1nove(i,:))});
Hlognorm=not(Hlognorm);
pstporuchylognorm(i)=1-logncdf(krithodnota,mean(X1nove(i,:)),std(X1nove(i,:)));
end
if rozdeleni3==1
[p] = gevfit(X1nove(i,:));
Hgev(i)=chi2gof(X1nove(i,:),'cdf',{@gevcdf,p(1),p(2),p(3)});
 Hgev=not(Hgev);
pstporuchygev(i)=1-gevcdf(krithodnota,p(1),p(2),p(3)); 
end
if rozdeleni4==1
[p] = [min(X1nove(i,:)),max(X1nove(i,:))];
Hunif(i)=chi2gof(X1nove(i,:),'cdf',{@unifcdf,p(1),p(2)});
Hunif=not(Hunif);  
pstporuchyunif(i)=1-unifcdf(krithodnota,p(1),p(2));
end    
    end

PRDnormv=[];
PRDlognormv=[];
PRDgevv=[];
PRDunifv=[];
     
 for i=1:80
if rozdeleni1==1
     if pstporuchynorm(i)>smerpstporuchy
PRDnorm=i+1;
     end
else PRDnorm=0;
end
if rozdeleni2==1
    if pstporuchylognorm(i)>smerpstporuchy
PRDlognorm=i+1;
    end
    else PRDlognorm=0;
end
if rozdeleni3==1
      if pstporuchygev(i)>smerpstporuchy
PRDgev=i+1;
      end
      else PRDgev=0;
end
if rozdeleni4==1
     if pstporuchyunif(i)>smerpstporuchy
PRDunif=i+1;
     end
     else PRDunif=0;
end
 end
 end
clc
% nn
% b5
% X1nove
if rozdeleni1==1
Hnorm
end
if rozdeleni2==1
Hlognorm
end
if rozdeleni3==1
Hgev
end
if rozdeleni4==1
Hunif
end
if rozdeleni1==1
pstporuchynorm
end
if rozdeleni2==1
pstporuchylognorm
end
if rozdeleni3==1
pstporuchygev
end
if rozdeleni4==1
pstporuchyunif
end
% PRDnorm
% PRDlognorm
% PRDgev
% PRDunif

