% get_etl_gps: read_gps from etl files, 2011, moanawave
infile='gps01109121_raw.txt';
%use gprmc recs
% original:
 %$GPRMC,...
  %001340,A,3216.0841,N,07951.8174,W,11.9,212.8,101007,7.1,W,D*22
            %hhmmss,A,laddmm,  N/S,loddmm,  E/W, spdKts,angle,DDMMYY, magvar
            % 20081012
            % $GPRMC,...
            %192146,A,0923.0104,N,07954.8289,W,0.0,238.1,121008,2.8,W,A*13
 % from my old gps logging code:
 % %gps=struct('mday',[],'lat',[],'lon',[],'spdm',[],'cog',[],'m agv',[]);
% 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
% 0000121 $GPRMC,210000.90,A,1903.30881408,S,07328.81452168,W,9.72,253.41,010411,1.3,W,D*23
%                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 and ddmmyy 
% do not need to fopen infile
% flist=fopen(infile);
headdata=importdata(infile);
temp=headdata(cellfun(@(x) ~isempty(strfind(x,'$GPRMC')),headdata));
ncols=11;
stx=ones(ncols,36000)*NaN;
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);
%         '%2f%2f%3f %*6c %*9c %*c %f %c %f %c %f %f %f %f %*c %*4c %*[^\n]',...
%         'delimiter', ', ',  'emptyvalue', NaN);
    % now get the composits???
    comps=textscan(temp{ixx,:},'%*s %s %*c %s %*c %s %*c %*s %*s %s%*[^\n]',...
            'delimiter', ',',  'emptyvalue', NaN);
    % vals: 'S'    'W'    [9.7200]    [253.4100]    [1.3000]
    %comps{1}{1}=210000.90; {2}{1}=1903.30881408; {3}{1}=07328.81452168; {4}{1}=010411
    dmy = char(comps{4}{1});
    hms = char(comps{1}{1});
    dv=[(str2num(dmy(5:6))+2000) str2num(dmy(3:4)) (str2num(dmy(1:2))) ...
        str2num(hms(1:2)) str2num(hms(3:4)) str2num(hms(5:6))];
     mday=datenum(dv);
    la=char(comps{2}{1});
    lo=char(comps{3}{1});
    ladeg=str2num(la{1:2)); lamin=str2num(la(3:end));
    lodeg=str2num(lo{1:3)); lomin=str2num(lo(4:end));
    lat=ladeg+lamin/60; lon=lodeg+lomin/60;
    try
%       stx(:,ixx)=[ vals{1};vals{2};vals{3};double(vals{4});double(vals{5});vals{6};double(vals{7});vals{8};vals{9};vals{10};vals{11}];
        stx(:,ixx)=[vals{1};vals{2};double(vals{3});vals{4};double(vals{5});vals{6};vals{7};vals{8};vals{9}];
    catch
       % stx(:,ixx)=ones(11,1)*NaN;
        stx(:,ixx)=ones(9,1)*NaN;
    end
end
stx(4:end,any(isnan(stx),1))=NaN;


%   dmy=sscanf(char(comps{4}{1}),'%02d%02d%02d'); dmy(3)=float(dmy(3)+2000);
%     hms=sscanf(char(comps{1}{1}),'%02d%02d%f'); 
%     mday=datenum(dmy(3),dym(2),dmy(1),hms(1),hms(2),hms(3));
