% nbp1201_read_navigation.m
%
% Read original data files under format control, save as mat files.
%
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
%@@@@@@@@@@@@@   DATA SOURCE  @@@@@@@@@@@@@@@@@@
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
% RVDAS NAVIGATION  data
% http://science.whoi.edu/users/mcgillic/nbp1201/rvdas/nav/
% 1201PCOD.tar (GGA and GLL)
% 1201seap.tar (GGA)
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
% NOTE:
% Estimated time for seap/GGA days [361:365 4:36] was 265.5509 minutes (4.4258 hours).

clear;

t0 = clock;
%operation
%etime(clock,t0)

%******************
% C O N S T A N T S
%******************
% Option: Save data as mat file:
%optionSave='yes';
optionSave='no';

path_part_1='/data2/antarctic_data2/nbp1201/underway/rvdas/';
path_part_2='nav/';
time_tag_chars=1:19;  %12+001:00:00:00.086
day_list=[361:365 3 4 7:36];
%
% ----- MENU: Navigation channel -----
channel_list=strvcat('PCOD','seap');
channel_id=f_menu_of_strings('Channel',channel_list);
if isempty(channel_id), return; end
pause(0.01);
%
% Path, names.
path_name=[path_part_1 path_part_2 channel_id '/'];
fname=['NBP1201' channel_id];
%%%fname=['NBP1201' channel_id '_test']; % For TEST - day 1 short
path_name_out=[path_name 'process/'];
fname_out_part=['NBP1201_nav_' channel_id];
fname_out_ext='mat';
comma_delimeter='yes';
PCOD_channels=strvcat('GLL','GGA');
%
% LOOP - for each day.
data=ones(100000,7);
%----------------------
M_not_found=[];
bad_lines_skipped='';
%----------------------
for day=1%[9 10 11 13:36]%day_list
%for day=12%day_list
    if day<10
        fname_ext=['d00' num2str(day)];
    elseif day>=10 &  day<100
        fname_ext=['d0' num2str(day)];
    else
        fname_ext=['d' num2str(day)];
    end
    %*******************************************************************
    %******************    Open file for reading   *********************
    %*******************************************************************
    pfid = fopen([path_name fname '.' fname_ext]);
    if pfid<0
        %fclose(pfid);
        disp(['File not found: ' fname '.' fname_ext ]);
        return;
    end
    count_line=0;
    data=[];
    data_PCOD_GGA=[];
    data_PCOD_GLL=[];
    while 1
        count_line=count_line+1;
        line = fgetl(pfid);
        if ~isstr(line), break, end
                     rr_6c=strfind(line,',,,,,,');
        %if ~isempty(rr_6c) | (strcmp(channel_id,'seap')  & day==5 & (count_line==101412 | count_line==101418) )
        if ~isempty(rr_6c) | (strcmp(channel_id,'seap')  & strfind(line,',,,0,,,,M,,M,,') )
        %if strcmp(channel_id,'PCOD') & day==362 & (count_line==211432 | count_line==211433)
            % Bad lines - skip.
            bad_lines_skipped=strvcat(bad_lines_skipped,[channel_id ' ' num2str(count_line) ' ' line(time_tag_chars)]);
        else
            %if ~ischar(line), break, end
            %%%ss=strfind(line,sub_channel_id);
            sub_channel_id=line(21:26);
            if ( strcmp(channel_id,'PCOD') & strcmp(sub_channel_id,'$GPGLL') ) | ...
                    ( strcmp(channel_id,'PCOD') & strcmp(sub_channel_id,'$GPGGA') ) | ...
                    ( strcmp(channel_id,'seap') & strcmp(sub_channel_id,'$GPGGA') )
                if  strcmp(channel_id,'PCOD') & strcmp(sub_channel_id,'$GPGLL')
                    npar=11;
                elseif strcmp(channel_id,'PCOD') & strcmp(sub_channel_id,'$GPGGA')
                    npar=18;
                elseif strcmp(channel_id,'seap') & strcmp(sub_channel_id,'$GPGGA')
                    npar=18;
                end
                %if ~isempty(ss)
                % C O R R E C T I O N  - DATE,TIME.
                line_cor_date=line(time_tag_chars);
                line_cor_date = strrep(line_cor_date,'+',char(9));
                line_cor_date = strrep(line_cor_date,':',char(9));
                % Concatenate corrected date/time with the string remaining
                % characters; do not use line ID characters.
                line_cor1=[line_cor_date line(length(time_tag_chars)+1:end)];
                % R E M O V E   SUB CHANNEL NAME.
                line_cor1=strrep(line_cor1,[sub_channel_id ','],'');
                %  R E P L A C E M E N T   E/W, N/S characters with 1/-1.
                line_cor1=strrep(line_cor1,',E',',1');
                line_cor1=strrep(line_cor1,',W',',-1');
                line_cor1=strrep(line_cor1,',N',',1');
                line_cor1=strrep(line_cor1,',S',',-1');
                if strcmp(comma_delimeter,'yes')
                    % C O R R E C T I O N  - comma delimeter: NOT available data.
                    line_cor2=line_cor1;
                    %rr_6c=strfind(line_cor2,',,,,,,');
                    %if ~isempty(rr_6c)
                    %    line_cor2=strrep(line_cor2,',,,,,,',',nan,nan,nan,nan,nan,');
                    %end
                    rr_5c=strfind(line_cor2,',,,,,');
                    if ~isempty(rr_5c)
                        line_cor2=strrep(line_cor2,',,,,,',',nan,nan,nan,nan,');
                    end
                    rr_4c=strfind(line_cor2,',,,,');
                    if ~isempty(rr_4c)
                        line_cor2=strrep(line_cor2,',,,,',',nan,nan,nan,');
                    end
                    rr_3c=strfind(line_cor2,',,,');
                    if ~isempty(rr_3c)
                        line_cor2=strrep(line_cor2,',,,',',nan,nan,');
                    end
                    rr_2c=strfind(line_cor2,',,');
                    if ~isempty(rr_2c)
                        line_cor2=strrep(line_cor2,',,',',nan,');
                    end
                    % C O R R E C T I O N  - remove Check Sum; in case of PCOD
                    % leave and equate to "1"  the Check Sum first character "A".
                    ss_comma=strfind(line_cor2,',');
                    if strcmp(channel_id,'PCOD') &  strcmp(sub_channel_id,'$GPGLL')
                        if strcmp(line_cor2(ss_comma(end)+1),'A'),...
                                line_cor2=strrep(line_cor2,line_cor2(ss_comma(end)+1:end),'1'); end
                    else
                        line_cor2(ss_comma(end):end)='';
                    end
                    if ( strcmp(channel_id,'seap') &  strcmp(sub_channel_id,'$GPGGA') ) | ...
                            ( strcmp(channel_id,'PCOD') & strcmp(sub_channel_id,'$GPGGA') )
                        % C O R R E C T I O N  - replace units characters "M" with "1".
                        su=strfind(line_cor2,'M');% units
                        if ~isempty(su)
                            line_cor2(su)='1';
                        else
                            M_not_found=[M_not_found;day count_line];
                            disp(['Line ' num2str(count_line) ': character M not found']);
                            %return;
                        end
                    end
                    % C O R R E C T I O N  - ',' to TAB.
                    line_cor2=strrep(line_cor2,',',char(9));
                    line_cor1=line_cor2;
                end % For if strcmp(comma_delimeter,'yes')
                % Read line under format control.
                line_data = (sscanf(line_cor1,'%f'))';
                % CHECK for vectors length. If 'good' - MAKE MATRIX, if 'bad' display a message:
                if length(line_data)==npar
                    if strcmp(channel_id,'PCOD')
                        p=['data_PCOD_' sub_channel_id(4:6) '=[data_PCOD_' sub_channel_id(4:6) ';line_data];'];
                        eval(p);
                    else
                        data = [data;line_data];
                    end
                else
                    disp(['Character (not number) is found in a line ' num2str(count_line) ':']);
                    disp(line);
                    disp('This part of line is read sucessfully:');
                    disp(line_data);
                    bad_line=count_line
                    return;
                end
                clear line_data line_cor1 line_cor2;
                %end % For if ~isempty(ss)
            end
        %end % For if strcmp(channel_id,'PCOD') & day==362 & count_line==211432
        end % For if ~isempty(rr_6c)
    end  % For while 1
    % Close file.
    fclose(pfid);
    if strcmp(channel_id,'seap')
        % Make South lat and West lon negative.
        data(:,7)=data(:,7) .* data(:,8);
        data(:,9)=data(:,9) .* data(:,10);
        % Remove unnecessary columns - (1,2)lat/lon signs and also (3) one column described as empty.
        data(:,[8 10 16])=[];
    elseif strcmp(channel_id,'PCOD')
        % GGA:
        % Make South lat and West lon negative.
        data_PCOD_GGA(:,7)=data_PCOD_GGA(:,7) .* data_PCOD_GGA(:,8);
        data_PCOD_GGA(:,9)=data_PCOD_GGA(:,9) .* data_PCOD_GGA(:,10);
        % Remove unnecessary columns - (1,2)lat/lon signs .
        data_PCOD_GGA(:,[8 10])=[];
        % GLL:
        % Make South lat and West lon negative.
        data_PCOD_GLL(:,6)=data_PCOD_GLL(:,6) .* data_PCOD_GLL(:,7);
        data_PCOD_GLL(:,8)=data_PCOD_GLL(:,8) .* data_PCOD_GLL(:,9);
        % Remove unnecessary columns.
        data_PCOD_GLL(:,[7 9])=[];
    end
    %*******************
    % Save data as a matfile.
    %*******************
    if strcmp(optionSave,'yes')
        if strcmp(channel_id,'PCOD')
            for nn=1:2
                fname_out_part_2=PCOD_channels(nn,:);
                fname_out=[fname_out_part '_' fname_out_part_2 '_' fname_ext];
                p=[fname_out '= data_PCOD_' fname_out_part_2 ';'];
                eval(p);
                p=['f_size=size(data_PCOD_' fname_out_part_2 ');'];
                eval(p);
                p=['save ' fname_out ' ' fname_out ';'];
                eval(p);
                p=['! mv ' fname_out '.' fname_out_ext ' ' path_name_out ';'];
                eval(p);
                %disp( strvcat(['File has been SAVED, filename: ' fname_out '.mat, size ' num2str(size(fname_out))],...
                disp( strvcat(['File has been SAVED, filename: ' fname_out '.mat, size ' num2str(f_size)],...
                    ['Location: ' path_name_out]) );
            end
        else
            fname_out_part_2='GGA';
            fname_out=[fname_out_part '_' fname_out_part_2 '_' fname_ext];
            p=[fname_out '= data;'];
            eval(p);
            p=['save ' fname_out ' ' fname_out ';'];
            eval(p);
            p=['! mv ' fname_out '.' fname_out_ext ' ' path_name_out ';'];
            eval(p);
            disp( strvcat(['File has been SAVED, filename: ' fname_out '.mat, size ' num2str(size(data))],...
                ['Location: ' path_name_out]) );
        end
        clear data data_PCOD_GLL data_PCOD_GGA;
    else
        if strcmp(channel_id,'PCOD')
            for nn=1:2
                fname_out_part_2=PCOD_channels(nn,:);
                p=['f_size=size(data_' channel_id '_' fname_out_part_2 ');'];
                eval(p);
                disp( ['OUTPUT File NOT SAVED, OUTPUT matrix: data_' channel_id '_' fname_out_part_2 ', size ' num2str(f_size)] );
            end
        else
            %   disp([monName ' ' num2str(dayNum) ', filename: ' fname]);
            disp( ['OUTPUT File NOT SAVED, OUTPUT matrix: data, size ' num2str(size(data))] );
        end
    end
end % for day=day_list
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
%@@@@@@@@@  OUTPUT matrix columns   @@@@@@@@@
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
% (1) PCOD GLL (day #1 size: 86400x9):
colYear_M=1;
colDay_M=2;
colHour_M=3;
colMin_M=4;
colSec_M=5;
colLat_M=6;  % degrees
colLon_M=7;  % degrees
%
% (2) seap GGA (day #1 size: 86411x15) and (3)  PCOD GGA (day #1 size: 86400x16).
% NOTE: the variable "Empty field" excluded;
%the name of the last variable is not clear - the guide shows 2 variables after
% the last "M" (put to"1" in my mat files):
% "age of DGPS corrections in seconds (s.s)" and "DGPS reference station ID
% (0000-1023)" and only one of them is available in the data.
colYear_M=1;
colDay_M=2;
colHour_M=3;
colMin_M=4;
colSec_M=5;
colTime_M=6; % hhmmss.ss
colLat_M=7;  % ddmm.mmmmmm
colLon_M=8;  % ddmm.mmmmmm
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
estimated_time_minutes=etime(clock,t0)/60
if ~isempty(M_not_found),...
    disp('!!! Check the variable: M_not_found - it is not empty'); end
if ~isempty(bad_lines_skipped),...
    disp('!!! Check the variable: bad_lines_skipped - it is not empty'); end

