% serial gps input, create ascii text version for matlab
% input:on com1 for serial, com4 for usb
% output:
% year month day hour minu sec lat lon spd_kts cog magvar
% magvar is part of the gps output string
% previously we converted sog to m/s
%
cd ([matlabroot '/work/gps'])
addpath([matlabroot '/work'])
dv=datevec(now); year=dv(1); yearstr=num2str(year);

ofname=['gps_' yearstr '.txt' ];
rfname=['gps_' yearstr '_raw.txt' ] ;
% need to see if serial port is open
% DO NOT CLEAR ALL
disp ('Please DO NOT clear all!')
try
    if iscell(infos),
        if strncmp(infos{1} ,'serial',6),
            disp('serial port open');
        end
    end
catch
    % use com4 if usb, com1 if serial
    s = serial('COM4');
    set(s,'BaudRate',4800);
    fopen(s);
    infos=get(s,{'Type','Name','Port'});
    % info ==  'serial'    'Serial-COM4'    'COM4'
end
o1cmd=sprintf('ofid=fopen(''%s'',''at'');',ofname);
eval(o1cmd);
o2cmd=sprintf('rfid=fopen(''%s'',''at'');',rfname);
eval(o2cmd);
%  like:  rfid=fopen('gps_2008_raw.txt','at');

indata='';
% from old gps code:
%gps=struct('mday',[],'lat',[],'lon',[],'spdm',[],'cog',[],'magv',[]);
indx=1;
one=1;
while (one == 1),
    indata=fscanf(s);
    %    try
    if length(indata) > 7,
    mtype=indata(2:6);
    switch(mtype)
    case 'GPRMC'
        %        if rem(minu,10) == 0,
        %         end
        if length(findstr(',',indata)) > 7,
            fprintf(rfid,'%s',indata);
            %$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


            [j,rema] = strtok(indata,',');
            [tstr,rema] = strtok(rema,',');
            % A for active: process only active ones
            [j,rema] = strtok(rema,',');
            if strcmp(j,'A'),
                lat=NaN;lon=NaN;spd=NaN;cog=NaN;magvar=NaN;
                ctim=tstr(1:2); hour=str2num(ctim);
                ctim=tstr(3:4); minu=str2num(ctim);
                ctim=tstr(5:6); sec=str2num(ctim);
                [cpos,rema] = strtok(rema,',');
                [ns,rema] = strtok(rema,',');
                if (length(cpos) >= 8 && ...
                        (strcmp(ns,'S') || strcmp(ns,'N'))),
                    cdeg=cpos(1:2); cmin=cpos(3:8);
                    lat=str2num(cdeg)+ str2double(cmin) / 60;
                    if strcmp(ns,'S'), lat = -lat; end
                    [cpos,rema]=strtok(rema,',');
                    [ew,rema]=strtok(rema,',');
                    if length(cpos) > 9,
                        cdeg=cpos(1:3); cmin=cpos(4:9);
                        lon=str2num(cdeg)+ str2double(cmin) / 60;
                        if strcmp(ew,'W'), lon = -lon; end
                        %spdKt,angle,DDMMYY, magvar
                        [cval,rema] = strtok(rema,',');
                        spd=str2num(cval);
                        [cval,rema] = strtok(rema,',');
                        cog=str2num(cval) ;
                        [tstr,rema] = strtok(rema,',');
                        if length(tstr) > 5,
                            day=str2num(tstr(1:2)); month=str2num(tstr(3:4));
                            year=2000+ str2num(tstr(5:6));
                            [cmvar,rema] = strtok(rema,','); magvar=str2num(cmvar);
                            [ew,rema]=strtok(rema,',');
                            if strcmp(ew,'W'), magvar= -magvar; end

                            % gps.mday(indx)=datenum(year,month,day,hour,minu,sec);
                            % gps.lat(indx)=lat; gps.lon(indx)=lon;
                            % gps.spdm(indx)=spd; gps.cog(indx)=cog; gps.magv(indx)=magvar;
                            %indx=indx+1;

                            fprintf('  %04d %02d %02d %02d %02d %02d %.4f %.4f %.4f %.4f %.3f\n',...
                                year,month,day,hour,minu,sec,lat,lon,spd,cog,magvar);
                            fprintf(ofid,'%04d %02d %02d %02d %02d %02d %.4f %.4f %.4f %.4f %.3f\n',...
                                year,month,day,hour,minu,sec,lat,lon,spd,cog,magvar);

                            %if rem(minu,5)==0
                            %    makegpsmat;
                            %    pause (1);
                            %end
                            pause(59);
                            %disp('Reopening Files');
                            try
                                fclose(ofid);
                                %ofid=fopen('gps_2008.txt','at');
                                eval(o1cmd);
                            catch
                                disp('Couldn''t close the file, re-opening');
                                rethrow(lasterror);
                                % ofid=fopen('gps_2008.txt','at');
                                eval(o1cmd);
                            end

                            try
                                fclose(rfid);
                                eval(o2cmd);
                                %rfid=fopen('gps_2008_raw.txt','at');
                            catch
                                disp('Couldn''t close the file, re-opening');
                                rethrow(lasterror);
                                eval(o2cmd);

                                %rfid=fopen('gps_2008_raw.txt','at');
                            end

                        end   %end length(tstr) > 5
                    end       % end length(cpos) > 9
                end           % end if (length(cpos) >= 8
            end               % end if strcmp(j,'A')
        end                   % end if length(findstr(',',indata)) > 7,
    end                           % end switch(mtype)
    end                               % end if length(indata) > 7,
    %   catch
    %      fprintf('e ')
    %  beep
    % end                               % end try/catch
end
%     ofname=['gps' num2str(datenum(gps.mday(end))-datenum(year,1,0)) '.mat' ];
%     eval(['save ' ofname ' gps'])
%end

%         case 'GPGGA'
%             %$gpgga,123519,4807.038,N,01131.000,E,1,08,0.9
%             %       hhmmss latdeglatmin N/S londeg lonmin E/W, Q
%             %Q; 0=invalid,1=
%         case 'GPZDA'
%             % date and time hhmmss.ss,dd,mm,yyyy
%             ;
%         case '$GPGLL',
%             [j,rem] = strtok(indata,',');
%             [clat,rem] = strtok(rem,',');
%             [ns,rem] = strtok(rem,',');
%
%             [clon,rem]=strtok(rem,',');
%             [ew,rem]=strtok(rem,',');
%               end

%
% 1: $GPGGA,000706,3217.1772,N,07950.9979,W,2,09,1.9,10.5,M,-32.5,M,,*4F
% 2: $GPGLL,3217.1772,N,07950.9979,W,000706,A,D*50
% 3: $GPBOD,,T,,M,,*47
% 4: $GPBWC,000706,,,,,,T,,M,,N,,D*7F
% 5: $GPVTG,211.5,T,218.6,M,11.8,N,21.8,K*47
% 6: $GPXTE,A,A,,,N,D*54
% 7: $PGRME,3.5,M,3.6,M,5.0,M*28
% 8: $PGRMZ,34,f,3*2C
%
% 9: $PGRMM,WGS 84*06
%
% 10: $GPRMC,000708,A,3217.1717,N,07951.0019,W,11.7,212.6,101007,7.1,W,D*25
%
% 11: $GPRMB,A,,,,,,,,,,,,A,D*0E
%
% for iii=1:100, indata=fscanf(s); fprintf(' %s ', indata(1:6)); end
%   $GPBWC  $GPVTG  $GPXTE  $PGRME  $PGRMZ  $PGRMM  $GPRMC  $GPRMB
%   $GPGGA  $GPGLL  $GPBOD  $GPBWC  $GPVTG  $GPXTE  $PGRME  $GPRMB
%   $GPGGA  $GPGLL  $GPBOD  $GPBWC  $GPVTG  $GPXTE  $PGRME  $PGRMZ
%   $PGRMM  $GPRMC  $GPRMB  $GPGGA  $GPGLL  $GPBOD  $GPBWC  $GPVTG
%   $GPXTE  $PGRME  $PGRMZ  $PGRMM  $GPRMC  $GPRMB  $GPGGA  $GPGLL
%   $GPBOD  $GPBWC  $GPVTG  $GPXTE  $PGRME  $PGRMZ  $PGRMM  $GPRMC
%   $GPRMB  $GPGGA  $GPGLL  $GPBOD  $GPBWC  $GPVTG  $GPXTE  $PGRME
%   $PGRMZ  $PGRMM  $GPRMC  $GPRMB  $GPGGA  $GPGLL  $GPBOD  $GPBWC
%   $GPVTG  $GPXTE  $PGRME  $PGRMZ  $PGRMM  $GPRMC  $GPRMB  $GPGGA
%   $GPGLL  $GPBOD  $GPBWC  $GPVTG  $GPXTE  $PGRME  $PGRMZ  $PGRMM
%   $GPRMC  $GPRMB  $GPGGA  $GPGLL  $GPBOD  $GPBWC  $GPVTG  $GPXTE
%   $PGRME  $PGRMZ  $PGRMM  $GPRMC  $GPRMB  $GPGGA  $GPGLL  $GPBOD
%   $GPBWC  $GPVTG  $GPXTE  $PGRME >>
