function D = siltprofiler_read_gps(fname)
%SILTPROFILER_READ_GOS   read GPS file from Deltares SiltProfiler
%
%   D = siltprofiler_read_gps(gpsname)
%
%See also: SILTPROFILER_PLOT; SILTPROFILER_READ_ASC

%% Copyright notice
%   --------------------------------------------------------------------
%   Copyright (C) 2004 Delft University of Technology
%       Gerben J. de Boer
%
%       g.j.deboer@tudelft.nl	
%
%       Fluid Mechanics Section
%       Faculty of Civil Engineering and Geosciences
%       PO Box 5048
%       2600 GA Delft
%       The Netherlands
%
%   This library is free software: you can redistribute it and/or
%   modify it under the terms of the GNU Lesser General Public
%   License as published by the Free Software Foundation, either
%   version 2.1 of the License, or (at your option) any later version.
%
%   This library is distributed in the hope that it will be useful,
%   but WITHOUT ANY WARRANTY; without even the implied warranty of
%   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
%   Lesser General Public License for more details.
%
%   You should have received a copy of the GNU Lesser General Public
%   License along with this library. If not, see <http://www.gnu.org/licenses/>.
%   --------------------------------------------------------------------

D         = dir(fname);
   
if isempty(D)   

clear D

D.name    = [];
D.date    = [];
D.bytes   = [];
D.isdir   = [];
D.datenum = [];
D.gps     = '';
D.datenum = [];
D.lat     = [];
D.lon     = [];
D.message = 'file not found';

else

fid       = fopen(fname,'rt');

D.gps     = '';
D.datenum = [];
D.lat     = [];
D.lon     = [];
D.message = 'file OK';

try

   rec             = fgets(fid); 
   
   while ~(rec==-1)
   
      if any(strfind(lower(rec),'$gpzda'))
      
      ind = strfind(rec,',');
         time = rec(ind(1)+1:ind(2)-1);
         H    = str2num(time(1:2));
         MI   = str2num(time(3:4));
         S    = str2num(time(5:6));
         Day  = str2num(rec(ind(2)+1:ind(3)-1));
         MO   = str2num(rec(ind(3)+1:ind(4)-1));
         Y    = str2num(rec(ind(4)+1:ind(5)-1));
         D.datenum(end+1) = datenum(Y,MO,Day,H,MI,S);

      end
      
      if any(strfind(lower(rec),'$gpgga'))
      
         ind          = strfind(rec,',');
         
         lat          = rec(ind(2)+1:ind(3)-1);
         GR           = str2num(lat(1:2));
         MN           = str2num(lat(3:end))./60;
         D.lat(end+1) = GR + MN;

         lon          = rec(ind(4)+1:ind(5)-1);
         GRA           = str2num(lon(1:3));
         MNT           = str2num(lon(4:end))./60;
         D.lon(end+1) = GRA + MNT;
        
      end 
      
      rec             = fgets(fid);
      
   end
   
catch

   warning(['error interpreting file:',fname])
   D.message = 'file corrupt';
   
end

   fclose(fid);
   
end   
