% Skript naèítající data uložená na lokálním disku ve složce "Archiv".
% Naèítá data ve formátu .dat která jsou ke stažení z FTP serveru.

%% Initialize variables.
s = dir ('./Archiv/*.dat');%adresáø "Archiv" je musí být pøítomen ve složce ze kterého je program spouštìn nebo použít celou cestu
pocet_files=size(s,1);%zjistí poøet souborù v daném adresáøi

for i=1:pocet_files
       nazev = s(i).name;
       nazev=nazev(1,1:(end-4));
       filename = ['./Archiv/',nazev,'.dat'];

        delimiter = ';';
        %% Format string for each line of text:
        %   column1: double (%f)
        %	column2: double (%f)
        %   column3: text (%s)
        %	column4: text (%s)
        formatSpec = '%f%f%s%s%[^\n\r]';

        %% Open the text file.
        fileID = fopen(filename,'r');
        dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'EmptyValue' ,NaN, 'ReturnOnError', false);
        fclose(fileID);

        %% Allocate imported array to column variable names
        Unix_time = dataArray{:, 1};
        prijimac = dataArray{:, 2};
        time = dataArray{:, 3};
        Message = dataArray{:, 4};

        %% Clear temporary variables
        clearvars filename delimiter formatSpec fileID dataArray ans;
        %% 
a=cellfun(@hex2bin2,Message,'UniformOutput', false);% êàæäîå ñîîáùåíèå èç 4 ñòîëáöà ïåðåâîäèì 16->2;
b=cellfun(@length,a,'UniformOutput', false); %äëèíà âåêòîðà;
c=cell2mat(b);
d = cell(length(a), 1);
s = cell(length(a), 1);
aaa=(prijimac==1); %vybrany pro sledovani prijimac 
for i=1:length(d)
      if b{i}==112  
      d{i}=a{i};
      end
      if b{i}~=112
      d{i}= zeros(1,112); 
      end
end
e=cell2mat(d);
f=e(:,1:5);
g = zeros(length(f),1); 
for ii=1:length(f)
       j = num2str(f(ii, :));
       h=bin2dec(j); 
       g(ii,:) = h; 
end
%pro DF 17
k=e(:,9:32);
jj=num2cell(e,2);
jj1=num2cell(k,2);
l=cellfun(@crc_division4,jj,'UniformOutput', false); 
ll=cellfun(@bin2hex,jj1,'UniformOutput', false); 
dd=strcmp(ll,''); % vybrane letadlo pro sledovani
df17=(g==17);

 kk= e(:,33:37);
     n = zeros(length(kk),1);

         for iii=1:length(kk)
             oo = num2str(kk(iii, :));
             lll=bin2dec(oo);
             n(iii,:) = lll; 
         end
kkk=zeros(length(n),1);
for ooo=1:length(n)
    if n(ooo)>=1 && n(ooo)<=4
        kkk(ooo)=n(ooo);
    end
    if n(ooo)>=9 && n(ooo)<=18
        kkk(ooo)=n(ooo);
    end
    if n(ooo)==19 
        kkk(ooo)=n(ooo);
    end
end

select17=df17&dd&aaa&kkk;
sum17=sum(select17); %pocet zprav formatu DF17

%pro DF20
m=e(:,89:112);
jjj=num2cell(e,2);
n=cellfun(@crc_division4,jjj,'UniformOutput', false);
nn=cellfun(@bin2hex,n,'UniformOutput', false);
ddd=strcmp(nn,''); % vybrane letadlo pro sledovani
df20=(g==20);
select20=ddd&df20&aaa;
sum20=sum(select20); %pocet zprav formatu DF20

%pro DF21
o=e(:,89:112);
jjjj=num2cell(e,2);
p=cellfun(@crc_division4,jjjj,'UniformOutput', false);
pp=cellfun(@bin2hex,p,'UniformOutput', false); 
dddd=strcmp(pp,''); % vybrane letadlo pro sledovani
df21=(g==21);
select21=dddd&df21&aaa;
sum21=sum(select21); %pocet zprav formatu DF21

%pro DF4 a DF5
for iii=1:length(s)
      if b{iii}~=56  
      s{iii}=zeros(1,56);
      end
      if b{iii}==56
      s{iii}= a{iii}; 
      end
end
r=cell2mat(s);
t=r(:,1:5);
q = zeros(length(t),1); 
for iiii=1:length(t)
       w = num2str(t(iiii, :));
       v=bin2dec(w); 
       q(iiii,:) = v; 
end
x=r(:,33:56);
ww=num2cell(r,2);
y=cellfun(@crc_division4,ww,'UniformOutput', false);
yy=cellfun(@bin2hex,y,'UniformOutput', false);
ddddd=strcmp(yy,''); % vybrane letadlo pro sledovani
df4=(q==4);
select4=df4&ddddd&aaa;
sum4=sum(select4); %pocet zprav formatu DF4

df5=(q==5)
select5=df5&ddddd&aaa;
sum5=sum(select5); %pocet zprav formatu DF5
end

