% 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_files=1:pocet_files
       nazev = s(i_files).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;
        %% 
        
        %sem vložte skript který s daty uloženýmy v promìnných: Unix_time,
        %prijimac, time, Message provádí dlaší operace.
        
        %% Dekodovani Surface Position Message

        REF_LKPR_WGS84=[50.10083333 14.26000000 376];                 %referencni bod LKPR

%% Prevedeni zpravy z HEX do BIN
lt_message_bin=hexToBinaryVector(Message);

%% Filtrace zprav DF17
[m,n] = size(lt_message_bin);
%nn = 0;               % posunuti
if n > 112            % kontrola, zda nejaka zprava nema vice bitu
    nn = n - 112;           % posunuti o pocet bitu navic (u zpravy 112 na zacatku "0"
end
i = 1;
i_acrf_op_msg = 1;
i_op_data = 1;
i_df17 = 1;
i_surface_msg = 1;
i_surface_data = 1;

lt_df17 = zeros(m,114);

for i=1:m           % prochazeni dat a nasledne filtrovani
    clear ls_poloha;
    lv_df = lt_message_bin(i,1+nn:5+nn); % ziskani informace o typu DF
    lv_df_dec = polyval(lv_df,2);        % DF v dec.
    if lv_df_dec == 17   % filtrovani pouze DF17 zprav
       lt_df17(i_df17,1:112) = lt_message_bin(i,1+nn:112+nn); % odstraneni nul
       lt_df17(i_df17,113) = Unix_time(i,1);
       lt_df17(i_df17,114) = prijimac(i,1);
              
       lv_type_code = lt_df17(i_df17,33:37); % ziskani informace o typu zpravy
       lv_type_code_dec = polyval(lv_type_code,2); % typ zpravy v dec
       
       if lv_type_code_dec > 4 && lv_type_code_dec < 9 % filtrace surface position zprav
          lt_surface_position(i_surface_msg,:) = lt_df17(i_df17,:); % ulozeni         
       
          ls_filtr1 = [50.08220858 14.21797955]; %"levy spodni okraj LKPR"
          ls_filtr2 = [50.12192187 14.29604570]; %"pravy horni okraj LKPR"
       
          [ls_poloha(1,1),ls_poloha(1,2)] = DF17_poloha_surface(lt_surface_position(i_surface_msg,:),REF_LKPR_WGS84(1,1),REF_LKPR_WGS84(1,2)); %poloha
          lv_ICAO_adress    = lt_surface_position(i_surface_msg,9:32);  %ICAO adresa
          lv_heading_status = lt_surface_position(i_surface_msg,45);    %Heading status
          lv_heading        = lt_surface_position(i_surface_msg,46:52); %Heading
          
          if ls_poloha(1,1) > ls_filtr1(1,1) && ls_poloha(1,2) > ls_filtr1(1,2) && ls_poloha(1,1) < ls_filtr2(1,1) && ls_poloha(1,2) < ls_filtr2(1,2)
              lt_surface_data(i_surface_data,1)   = polyval(lv_ICAO_adress,2);      % ICAO adresa
              lt_surface_data(i_surface_data,2:3) = ls_poloha(1,1:2);               % Poloha
              lt_surface_data(i_surface_data,4)   = lv_heading_status;              % Heading status
              lt_surface_data(i_surface_data,5)   = polyval(lv_heading,2).*2.8125;  % Heading
              lt_surface_data(i_surface_data,6)   = lt_surface_position(i_surface_msg,113); % Unix Time
              lt_surface_data(i_surface_data,7)   = lt_surface_position(i_surface_msg,114); % Prijimac
              lt_surface_data(i_surface_data,8)   = lv_type_code_dec;       % Type Code
              
              i_surface_data = i_surface_data + 1;
          end
          i_surface_msg = i_surface_msg + 1;
       elseif lv_type_code_dec == 31;   % Filtrace Aircraft Operation Message
           lv_subtype_code = lt_df17(i_df17,38:40);
           lv_subtype_code_dec = polyval(lv_subtype_code,2);
           if lv_subtype_code_dec == 1;     % Filtrace Surface AO MSG
               lt_aircraft_operation_surface(i_acrf_op_msg,:) = lt_df17(i_df17,:);  % Ulozeni zpravy
               
               lv_ICAO_adress = lt_aircraft_operation_surface(i_acrf_op_msg,9:32); %ICAO adresa
               lv_ICAO_version = lt_aircraft_operation_surface(i_acrf_op_msg,73:75); %ICAO Version
               lv_NACP = lt_aircraft_operation_surface(i_acrf_op_msg,77:80); % NAC P
               lv_NICA = lt_aircraft_operation_surface(i_acrf_op_msg,76);    % NIC A
               lv_NICC = lt_aircraft_operation_surface(i_acrf_op_msg,52);    % NIC C
               lv_HRD = lt_aircraft_operation_surface(i_acrf_op_msg,86);     % HRD
               
               lt_operation_data(i_op_data,1) = polyval(lv_ICAO_adress,2);  % ICAO adresa
               lt_operation_data(i_op_data,2) = polyval(lv_ICAO_version,2); % ICAO version
               lt_operation_data(i_op_data,3) = lv_NICA;                    % NIC - A
               lt_operation_data(i_op_data,4) = lv_NICC;                    % NIC - C
               lt_operation_data(i_op_data,5) = polyval(lv_NACP,2);         % NAC Position
               lt_operation_data(i_op_data,6) = lt_aircraft_operation_surface(i_acrf_op_msg,113); % Unix Time
               lt_operation_data(i_op_data,7) = lt_aircraft_operation_surface(i_acrf_op_msg,114); % Prijimac
               lt_operation_data(i_op_data,8) = lv_HRD;                     % HRD
               
               i_acrf_op_msg = i_acrf_op_msg + 1;
               i_op_data = i_op_data + 1;
           end;
       end       
       i_df17 = i_df17 + 1;   
    end
end
end