function [gps] = get_etl_gps(infile)
% get_etl_gps: read_gps from etl files, 2011, moanawave
% some of this code was lifted from Ludo (ETL)
% infile='etl/gps01109121_raw.txt';
% use gprmc recs via etl logger
%                1           2            3   4             5  6   7      8       9
% %*14c          %f      %*c %f           %c %f            %c %f   %f     %d     %f       
% 123456789 123456789
% 0000012 $GPRMC,210000.80,A,1903.30873961,S,07328.81424386,W,9.73,253.39,010411,1.3,W,D*20
%                hhmmss      la lamin         lo lomin        spdK hdg    ddmmyy
% 1              2         3 4             5 6              7 8    9      10
% %*s            %s        %*c  %s         %*c %s           %*c %*s %*s %s
% time is hhmmss, date is ddmmyy 
%
dbg=-1;
% great new way to get these long files (thank you ETL)
headdata=importdata(infile);
temp=headdata(cellfun(@(x) ~isempty(strfind(x,'$GPRMC')),headdata));

ncols=9;
datar=ones(ncols,3600)*NaN;
oxx=1;   % index for output array
for ixx=1:length(temp)
    vals = textscan(temp{ixx,:},...
        '%*14c %f  %*c %f  %c %f %c %f %f %f %f %*c %*4c %*[^\n]',...
        'delimiter', ', ',  'emptyvalue', NaN);
    % keep only even seconds
    if cell2mat(vals(1)) == floor(cell2mat(vals(1)))
        try
            datar(:,oxx)=[vals{1};vals{2};double(vals{3});...
                vals{4};double(vals{5});vals{6};vals{7};vals{8};vals{9}];
            oxx=oxx+1;
        catch
            datar(:,ixx)=ones(9,1)*NaN;
        end
    end
end
datar(4:end,any(isnan(datar),1))=NaN;

% get date, time
 % eg date 10411.00, time 210000.80
dmyfld=datar(8,:);
day=floor(dmyfld/10000.);
myfld=dmyfld - (day*10000.);
mon=floor(myfld/100);
yr=2000 + (myfld - (mon*100));

hmsfld=datar(1,:);
hr=floor(hmsfld/10000.);
msfld=hmsfld-(hr * 10000.);
mins= floor(msfld/100);
sec=msfld - (mins*100);
% keep only even minutes:
gv = find(sec == 0);
gps.mday=datenum(yr(gv),mon(gv),day(gv),hr(gv),mins(gv),sec(gv));

% now get lat/long, their signs
ldm=datar(2,gv);
ldeg=floor(ldm/100);
lmin=(ldm-ldeg*100);
gps.lat=ldeg + lmin/60;
sw=find(datar(3,gv)==83);
gps.lat(sw) = -gps.lat(sw); 

ldm=datar(4,gv);
ldeg=floor(ldm/100);
lmin=(ldm-ldeg*100);
gps.lon=ldeg + lmin/60;
sw=find(datar(5,gv)==87);
gps.lon(sw) = -gps.lon(sw); 

if dbg > 0
    fprintf('%d points,', length(gps.mday));
    try
        fprintf(' %s ', datestr(gps.mday(1)))
        fprintf(' %s\n',datestr(gps.mday(end)))
    catch
        disp('bad mday')
    end
end
%keyboard
% speed, heading
gps.speed=datar(6,gv).*.514;
gps.heading=datar(7,gv);
gps.magv=datar(9,gv);


