%$PORZE,175437.00,A,5456703.7,,03614670.9,,00.00,170.1,060513,CSM23*5D

fid = fopen('d:\Škola\M4_semestr\Dioplomka\data z mereni\kolejni_strecha\3.mereni_6.5.2013\NVS08_SBAS+RTMC_COM9_130506_180046.ubx','r');


PORZE = struct('utc',      [], ... %string
              'time',      [], ... %double <0,1> day part
              'status',    [], ... %double A - Vlid, V Invalid
              'x',         [], ... %double easting [m]
              'y',         [], ... %double northing [m]
              't_zone',    [], ... %double 
              'sog',       [], ... %double speed over the ground
              'cog',       [], ... %double course over the ground
              'date',      [], ... %string (day/month/year)
              'id',        [], ... %double
              'checksum',  []);    %%uint8
          
tline=fgetl(fid);
index = 1;
radek = 1;
wrong_checksum = 0;
disintegrated_lines = 0;

%výběr vzorku pro 24 hodin <1.vzorek, 1.vzorek +24h -1>
%dokud se nerovná čas prvního vzorku s dalším

%% Parse all PORZE in file
while (~feof(fid)) %until text lines in file or feof(obj1)
  % parse GGA (Global Positioning System Fix Data)
  if ((~isempty(strmatch('$PORZE',tline)))&&(~isempty(tline)))
  
  comma = findstr(tline,','); 
  asterisk = findstr(tline,'*');
 
  if ((length(comma) == 10)&&(length(asterisk) == 1)&&((length(tline)-asterisk)==2)) %line integrity control
  checksum_nmea  = tline(asterisk+1:asterisk+2);
  checksum_count = nmeachecksum(tline);
  if (checksum_nmea == checksum_count)             %checksum control
      
%time  
    t_time = tline(comma(1)+1:comma(2)-4);
    if(isempty(t_time))
     t_time  =  NaN;
    else
     PORZE.utc(index,:) = char(t_time);
     PORZE.time(index,:) =  datenum(t_time,'HHMMSS') - ...
                          floor(datenum(t_time,'HHMMSS'));   
     clear t_time;   
    end
%status
    if(isempty(tline(comma(2)+1:comma(3)-1)))
      PORZE.status(index,:)  =  NaN;
    else
      PORZE.status(index,:)  = tline(comma(2)+1:comma(3)-1);
    end
%x   
    if(isempty(tline(comma(3)+1:comma(4)-1)))
     PORZE.x(index,:) =  NaN;
    else
     PORZE.x(index,:) = str2double(tline(comma(3)+1:comma(4)-1)); % tline(comma(4)+1:comma(5)-1)
    end     
%y, t_zone   
    temp_y = str2double(tline(comma(5)+1:comma(6)-1));
    if(isempty(tline(comma(5)+1:comma(6)-1)))
     PORZE.y(index,:) =  NaN;
    else
     PORZE.t_zone(index,:) = uint8(floor(temp_y/1000000));   
     PORZE.y(index,:) = temp_y - double(PORZE.t_zone(index,:))*1000000; %- 500000 tline(comma(4)+1:comma(5)-1) 
    end
%sog   
  PORZE.sog(index,:) = str2double(tline(comma(7)+1:comma(8)-1));

%cog  
  PORZE.cog(index,:) = str2double(tline(comma(8)+1:comma(9)-1));
%date     
  PORZE.date(index,:)    = tline(comma(9)+1:comma(10)-1);
%id 
  PORZE.id(index,:)      = tline(comma(10)+1:asterisk-1);
%checksum
  PORZE.checksum(index,:) = tline(asterisk+1:asterisk+2);
  clear comma;
  clear asterisk;
   
    index = index + 1;
    disp(['PORZE veta: ', num2str(index),' -> Data OK. ', 'Radek: ', num2str(radek)]);
  else
    wrong_checksum = wrong_checksum + 1;
    disp(['PORZE veta: ', num2str(index), ' -> Warning: Data corupted, checksum not valid! ', 'Radek: ', num2str(radek)])
  end
  else %poskozeny radek
    disintegrated_lines =  disintegrated_lines + 1; 
  end  %rozpadly radek
  end  %neni PORZE
  
  tline = fgetl(fid); 
  radek = radek + 1;

end %while
disp(['Chybnych GGA vet (checksum): ', num2str(wrong_checksum)])
disp(['Chybnych GGA vet (spatny pocet,*, chybi checksum ): ', num2str(disintegrated_lines)])

 clear tline;
 fclose(fid);
 
 
 %% print
%  B = zeros(70000,3);
%  for ii=1:1:70000
%  B(ii,:) = [PORZE.x(ii) PORZE.y(ii) 340];
%  end
% 
%  B_mean = mean(B);
%  
%  for ii=1:1:length(B)
% B(ii,1) = B(ii,1) -  B_mean(1);
% B(ii,2) = B(ii,2) -  B_mean(2);
%  end
% 
% figure;
% scatter(B(:,1), B(:,2),'+','y') %Print all samples
% 
% %                   Easting     NOrthing
% %$PORZE,175437.00,A,5456703.7,,03614670.9,,00.00,170.1,060513,CSM23*5D
% temp_y =                        6417534.2;
% %temp_y =                       3614670.9;
% 
% t_zone = uint8(floor(temp_y/1000000))
% yy = temp_y - double(t_zone)*1000000 - 500000 % tline(comma(4)+1:comma(5)-1) 
% 
% %y 114670.8
% %x 5456703.7
% 
% 
% 
% %x 6144975.027665539e+05  prepoctene UTM souradnice
% %y 5454376.538146103e+06
% 
% %x 614434.729775	%ref bod 6
% %y 5454298.521450
% 
% x 5456703.7
% y 03614670.9
% 
% 114670.8
% 
% 614434.72
% 114670.8+333.95
% 
% %614434.72 - 114670.8 = 499760