%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                        %
%                               CCSDS_Kod                                %
%                                                                        %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%KODOVANIE DAT PRE PRVY A DRUHY KODER
%Vstupne premenne:
%data - data vstupujuce do kodera [bin]
%b - celkova dlzka bloku
%rate - rate kodera
%Vystupna premenna:
%out - kodovane data
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function out = CCSDS_Kod(data,b,punc)

dlz = b;                                                             
blok = b - 4;                                                        
it1 = ceil(length(data)/blok);                                            
it2 = floor(length(data)/blok);                                   
out = [];

for i = 1:it1
    %%ROZDELOVANIE DAT DO BLOKOV
    if(i<=it2)
       z = data(1,(i*blok-blok)+1:i*blok); 
    else
       sss = blok - length(data(1,(i*blok-blok)+1:end));
       z = [data(1,(i*blok-blok)+1:end) zeros(1,sss)];                     
    end
    
    %%KODER1
    k_d1 = CCSDS_Koder(z,1); 
    x(1,:) = k_d1(1:4:4*dlz);                                               %sys. bity 0a
    x(2,:) = k_d1(2:4:4*dlz);                                               %par. bity 1a
    x(3,:) = k_d1(3:4:4*dlz);                                               %par. bity 2a
    x(4,:) = k_d1(4:4:4*dlz);                                               %par. bity 3a

    int_data = CCSDS_Int(x(1,:),1);                                   

    %%KODER2
    k_d2 = CCSDS_Koder(int_data,2);                                
    x(5,:) = k_d2(2:3:3*dlz);                                               %par. bity 1b
    x(6,:) = k_d2(3:3:3*dlz);                                               %par. bity 3b
    
    %%RATE 1/6
    if(punc == 1/6)
        y(1:6:6*dlz) = x(1,1:dlz);                                          %multiplexovanie vystup 0a
        y(2:6:6*dlz) = x(2,1:dlz);                                          %multiplexovanie vystup 1a
        y(3:6:6*dlz) = x(3,1:dlz);                                          %multiplexovanie vystup 2a
        y(4:6:6*dlz) = x(4,1:dlz);                                          %multiplexovanie vystup 3a
        y(5:6:6*dlz) = x(5,1:dlz);                                          %multiplexovanie vystup 1b
        y(6:6:6*dlz) = x(6,1:dlz);                                          %multiplexovanie vystup 3b
        out = [out y];

    %%RATE 1/4    
    elseif(punc == 1/4)
        y(1:4:4*dlz) = x(1,1:dlz);                                          %multiplexovanie vystup 0a 
        y(2:4:4*dlz) = x(3,1:dlz);                                          %multiplexovanie vystup 2a
        y(3:4:4*dlz) = x(4,1:dlz);                                          %multiplexovanie vystup 3a
        y(4:4:4*dlz) = x(5,1:dlz);                                          %multiplexovanie vystup 1b
        out = [out y];
        
    %RATE 1/3
    elseif(punc == 1/3)
        y(1:3:3*dlz) = x(1,1:dlz);                                          %multiplexovanie vystup 0a
        y(2:3:3*dlz) = x(2,1:dlz);                                          %multiplexovanie vystup 1a
        y(3:3:3*dlz) = x(5,1:dlz);                                          %multiplexovanie vystup 1b
        out = [out y];
        
    %RATE 1/2
    elseif(punc == 1/2)                                            
        y(1,1:2:2*dlz) = x(1,1:dlz);                                        %multiplexovanie vystup 0a
        y(1,2:4:2*dlz) = x(2,1:2:dlz);                                      %multiplexovanie vystup 1a 1/2
        y(1,4:4:2*dlz) = x(5,2:2:dlz);                                      %multiplexovanie vystup 1b 1/2
        out = [out y];
    else
        error('CHYBA!!!');
    end
end
end