% gga2arcview.m
% Drew Gashler
% 06 February 2001
% 
% Reads archived gga NMEA string from gps and loads lat,lon, into matlab variables.
% Writes comma delimited output file of latitude and longitude for arcview event theme.
%
% usage: function [lat, lon] = gga2arcview(inputFilename,outputFilename);

function [lat, lon] = gga2arcview(inputFilename,outputFilename);
% open the input file:
fidInput = fopen(inputFilename,'rt');

% open the output file and write header to it.
fidOutput = fopen(outputFilename,'w');
headerOutput = 'lat,lon';
fprintf(fidOutput,'%s\n',headerOutput);
line = 'junk';
% read and skip incomplete NMEA sentences:
%while ((line(1) ~= '$')|(isempty(line) == 1))
%  line = fgetl(fid);
%end	



%initialize the row number
i = 1;
string_buffer = 'initial value';
% define the delimiter:
delimiter = ',';
numWords = 15;
% remove the while loop temporarily
while(1)
  string_buffer = fgets(fidInput);
  if ~isstr(string_buffer),break,end
  latPrime(i)    = sscanf(string_buffer(15:24),'%f');
  lonPrime(i)        = sscanf(string_buffer(27:37),'%f');
  
  % latitude in the form 3648.1234, need to convert
  % integer part of solution:
  latInt(i) = floor(latPrime(i)./100);
  lonInt(i) = floor(lonPrime(i)./100);

  % decimal minutes part of solution
  latDec(i) = 100*(latPrime(i)./100-latInt(i))./60;
  lonDec(i) = 100*(lonPrime(i)./100-lonInt(i))./60;

  % sum to get the total
  lat(i) = latInt(i) + latDec(i);
  lon(i) = lonInt(i) + lonDec(i);
  
  % write to output file:
  fprintf(fidOutput,'%12.8f %s %12.8f\n',lat(i),',',-1.*lon(i));
  
  i           = i + 1;
end


%global lat lon latInt latDec lonInt lonDec latPrime lonPrime

fclose(fidInput);
fclose(fidOutput);







