% nbp1201_plot_navigation.m
%
% INPUT: RVDAS Navigation data (mat files)
% PLOT data locations, one day from [361:365 1:36].

clear;
t0 = clock;

% Data path - part 1.
path_part_1_nav='/data2/antarctic_data2/nbp1201/underway/rvdas/nav/';
%******************
% C O N S T A N T S
%******************
% Option: Save data as mat file:
%optionSave='yes';
optionSave='no';
% Font size.
fnt_axis=10;
fnt_lbl=10;
fnt_title=10;
fnt_text=8;
%fnt_name='times';
fnt_name='helvetica';
marker_size=2;
marker_size_time=9;
fig_pos=[360.00        460.00        599.00        461.00];
crName='NBP1201';
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
%@@@@@@@@@@@@@  DATA  NAMES   @@@@@@@@@@@@@@@
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
PCOD_channels=strvcat('GLL','GGA');
channel_name_list_nav=strvcat('seap GGA: seapath GPS - GGA',...
    'PCOD GLL: Trimble (PCOD) GPS',...
    'PCOD GGA:  Trimble (PCOD) GPS');
day_list_string=strvcat('361: Dec 27,2011',...
    '362: Dec 28, 2011',...
    '363: Dec 29, 2011',...
    '364: Dec 30, 2011',...
    '365: Dec 31, 2011',...
    '001: Jan 1, 2012',...
    '002: Jan 2, 2012',...
    '003: Jan 3, 2012',...
    '004: Jan 4, 2012',...
    '005: Jan 5, 2012',...
    '006: Jan 6, 2012',...
    '007: Jan 7, 2012',...
    '008: Jan 8, 2012',...
    '009: Jan 9, 2012',...
    '010: Jan 10, 2012',...
    '011: Jan 11, 2012',...
    '012: Jan 12, 2012',...
    '013: Jan 13, 2012',...
    '014: Jan 14, 2012',...
    '015: Jan 15, 2012',...
    '016: Jan 16, 2012',...
    '017: Jan 17, 2012',...
    '018: Jan 18, 2012',...
    '019: Jan 19, 2012',...
    '020: Jan 20, 2012',...
    '021: Jan 21, 2012',...
    '022: Jan 22, 2012',...
    '023: Jan 23, 2012',...
    '024: Jan 24, 2012',...
    '025: Jan 25, 2012',...
    '026: Jan 26, 2012',...
    '027: Jan 27, 2012',...
    '028: Jan 28, 2012',...
    '029: Jan 29, 2012',...
    '030: Jan 30, 2012',...
    '031: Jan 31, 2012',...
    '032: Feb 1, 2012',...
    '033: Feb 2, 2012',...
    '034: Feb 3, 2012',...
    '035: Feb 4, 2012',...
    '036: Feb 5, 2012');
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
%@@@@@@@@@@@@@@@  MENUS   @@@@@@@@@@@@@@@@@@@
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
%------------------------------------------------
%------ MENU (1): Day of the year: Date  --------
%------------------------------------------------
day_name=f_menu_of_strings('The day of year: Date',day_list_string);
if isempty(day_name), return; end
%-----------------------------------------------
day=str2num(day_name(1:3));
day_date=deblank(day_name(6:end));
if day<10
    day_str=['d00' num2str(day)];
elseif day>=10 & day<100
    day_str=['d0' num2str(day)];
else
    day_str=['d' num2str(day)];
end
if day>=361
    year=2011;
else
    year=2012;
end
% Month of the day.
date_num=datenum([year-1 12 31 24 0 0])+(day-1);
date_vec=datevec(date_num);
month=date_vec(1,2);
clear date_num date_vec;
%------------------------------------------------
% -------- MENU (2): Navigation channel ---------
%------------------------------------------------
channel_name_nav=f_menu_of_strings('NAVIGATION channel',channel_name_list_nav);
if isempty(channel_name_nav), return; end
%--------------------------------------------
channel_id_nav=channel_name_nav(1:4);
sub_channel_id_nav=channel_name_nav(6:8);
%$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
%$$$$$$$$$$$$$  N A V I G A T I O N   DATA  $$$$$$$$$$$$$$
%$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
%@@@@@@@  INPUT Navigation columns   @@@@@@@@
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
if strcmp(channel_id_nav,'PCOD') & strcmp(sub_channel_id_nav,'GLL')
    % (1) PCOD GLL (day #1 size: 86400x9):
    colYear_N=1;
    colDay_N=2;
    colHour_N=3;
    colMin_N=4;
    colSec_N=5;
    colLat_N=6;  % ddmm.mmmmmm
    colLon_N=7;  % ddmm.mmmmmm
elseif  strcmp(channel_id_nav,'PCOD') & strcmp(sub_channel_id_nav,'GGA')
    % (2) PCOD GGA (day #1 size: 86400x16).
    colYear_N=1;
    colDay_N=2;
    colHour_N=3;
    colMin_N=4;
    colSec_N=5;
    colTime_N=6; % hhmmss.ss
    colLat_N=7;  % ddmm.mmmmmm
    colLon_N=8;  % ddmm.mmmmmm
elseif strcmp(channel_id_nav,'seap') & strcmp(sub_channel_id_nav,'GGA')
    %@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    %@@@@@@@  INPUT Navigation columns   @@@@@@@@
    %@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    % (3) seap GGA (day #1 size: 86411x15).
    % NOTES:
    % 1. variable "Empty field" excluded;
    % 2. the name of the last variable is not clear - the guide shows 2 variables after
    % the last "M" (replaced with "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_N=1;
    colDay_N=2;
    colHour_N=3;
    colMin_N=4;
    colSec_N=5;
    colTime_N=6; % hhmmss.ss
    colLat_N=7;  % ddmm.mmmmmm
    colLon_N=8;  % ddmm.mmmmmm
else
    return;
end
sub_channel_id_nav_str=['_' sub_channel_id_nav];
sub_channel_id_nav_text=['\_' sub_channel_id_nav];
path_name_nav=[path_part_1_nav channel_id_nav '/process/'];
fname_nav=['NBP1201_nav_' channel_id_nav '_' sub_channel_id_nav '_'  day_str];
fname_nav_text=['NBP1201\_nav\_' channel_id_nav sub_channel_id_nav_text '\_' day_str];
fname_ext_nav='mat';
%**********************************************************
%************   LOAD NAVIGATION mat file  *****************
%**********************************************************
p=['load ' path_name_nav fname_nav '.' fname_ext_nav ';'];
eval(p);
p=['d_nav=' fname_nav ';'];
eval(p);
if  ( strcmp(channel_id_nav,'PCOD') & strcmp(sub_channel_id_nav,'GGA') ) | ...
        ( strcmp(channel_id_nav,'PCOD') & strcmp(sub_channel_id_nav,'GLL') ) | ...
        ( strcmp(channel_id_nav,'seap') & strcmp(sub_channel_id_nav,'GGA') )
    % Lat, Lon - Get decimal degree from ddmm.mmmmmm.
    lat=abs(d_nav(:,colLat_N));
    lon=abs(d_nav(:,colLon_N));
    lat_dec=floor(lat/100) + ( (floor(lat)/100-floor(lat/100))*100 + ( lat-floor(lat) ) )/60;
    lon_dec=floor(lon/100) + ( (floor(lon)/100-floor(lon/100))*100 + ( lon-floor(lon) ) )/60;
    r_minus_lat=find(d_nav(:,colLat_N)<0);
    r_minus_lon=find(d_nav(:,colLon_N)<0);
    lat_dec(r_minus_lat,1)=lat_dec(r_minus_lat,1)*(-1);
    lon_dec(r_minus_lon,1)=lon_dec(r_minus_lon,1)*(-1);
    d_nav(:,colLat_N)=lat_dec;
    d_nav(:,colLon_N)=lon_dec;
    %--------- E.g. --------------------
    %lat=6421.630563;
    %lon=09907.079075;
    %lat_dec=floor(lat/100) + ( (floor(lat)/100-floor(lat/100))*100 + ( lat-floor(lat) ) )/60;
    %--------- E.g. END --------------------
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%  Make W Lon positive %%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Make W Lon positive.
r_w=find(d_nav(:,colLon_N)<0);
d_nav(r_w,colLon_N)=d_nav(r_w,colLon_N)+360;
clear r_w;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%  DATA %%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Decimal hour from Navigation.
h_dec_N=d_nav(:,colHour_N) + ( d_nav(:,colMin_N) + d_nav(:,colSec_N)/60 )/60;
% Location.
lat_uw=d_nav(:,colLat_N);
lon_uw=d_nav(:,colLon_N);
%#############################################
%############   Map limits  ##################
%#############################################
lonmin_d = min(lon_uw);
lonmax_d = max(lon_uw);
latmin_d = min(lat_uw);
latmax_d = max(lat_uw);
klat=0.05;
klon=0.05;
if day==361 | day==362 | day==3 | day==4 | day==6
    klat=0.15;
elseif day==19  | day==25  | day==35
    klat=1.3;
elseif day==33
    klon=1.3;
end
d_lon=range([lonmin_d lonmax_d])*klon;
d_lat=range([latmin_d latmax_d])*klat;
lonmin=lonmin_d-d_lon;
lonmax=lonmax_d+d_lon;
latmin=latmin_d-d_lat;
latmax=latmax_d+d_lat;
lon_int=1;
lat_int=0.5;
% --------  Calculate relation relyx = axisy/axisx (for PlotBoxAspectRatio)
weg = 111*cos(pi*(latmin+(latmax-latmin)/2)/180);% Weight of one degree longitude
xc = (lonmax-lonmin)*weg;                        % Longitude, km
yc = (latmax-latmin)*111;                        % Latitude, km
relyx = yc/xc;
%#############################################
%######## X,Y ticks, tickLabels ##############
%#############################################
% ---- X-ticks.
x_tick=floor(lonmin):lon_int:lonmax;
x_tick_label='';
for ii=1:length(x_tick)
    if x_tick(ii)<180
        x_tick_str=[num2str(x_tick(ii)) 'E'];
    else
        x_tick_str=[num2str(360-x_tick(ii)) 'W'];
    end

    if ii==length(x_tick)
        delim='';
    else
        delim='|';
    end
    x_tick_label=[x_tick_label x_tick_str delim];
end
clear ii;
% ---- Y-ticks.
y_tick=floor(latmin):lat_int:latmax;
y_tick_label='';
for ii=1:length(y_tick)
    if ii==length(y_tick)
        delim='';
    else
        delim='|';
    end
    y_tick_label=[y_tick_label num2str(-y_tick(ii)) 'S' delim];
end
clear ii;
%***********************************************************
%************  Set figure, axes properties  ****************
%***********************************************************
figure;
set(gcf,'position',fig_pos,'color',[1 1 1],...
    'paperPositionMode','auto');
%***********************************************************
%*****************    Label Date/time    *******************
%***********************************************************
d_hour=[0 4 8 12 16 20 23];
%date_format='yyyy-mm-dd HH:MM:SS';
%date_format='mm/dd/yyyy HH:MM:SS';
%date_format='mm/dd';
%date_format='yyyy/mm/dd HH:MM';
date_format='HH:MM';
time_CHECK=[];
hold on;
for nn=1:length(d_hour)
    dnum_n=datenum([year month day d_hour(nn) 0 0]);
    r_h=find(d_nav(:,colHour_N)==d_hour(nn) & d_nav(:,colMin_N)==0);
    if ~isempty(r_h)
        % use Navigation point with min HOUR time.
        [mi,rmi]=min(h_dec_N(r_h,1));
        lat=lat_uw(r_h(rmi),1);
        lon=lon_uw(r_h(rmi),1);
        text(lon,lat-range([latmin latmax])*0.06,['  ' datestr(dnum_n,date_format)],...
            'fontsize',fnt_text,'fontname',fnt_name,'horizontalAlignment','left','Color','r');
        plot(lon,lat,'k+','markersize',marker_size_time,'markerEdgeColor','r');
        time_CHECK=[ time_CHECK;d_nav(r_h(rmi),[colHour_N colMin_N colSec_N]) ];
    end
end
hold off;
%***********************************************************
%*******************   PLOT locations   ********************
%***********************************************************
hold on;
plot(lon_uw,lat_uw,'o','color',[0 0 1],'markersize',marker_size);
hold off;
%***********************************************************
%*****************  Set Axes Properties  *******************
%***********************************************************
axPos=get(gca,'Position');
kk=1.0;
set(gca,'position',[axPos(1)+axPos(3)*(-0.04) axPos(2)+axPos(4)*0.03 axPos(3)*kk axPos(4)*kk]);
axis([lonmin lonmax latmin latmax]);
set(gca,'PlotBoxAspectRatio',[1 relyx*1 1],...
    'tickdir','out',...
    'ticklength',(get(gca,'ticklength')*0.6),...
    'xtick',x_tick,'xticklabel',x_tick_label,...
    'ytick',y_tick,'yticklabel',y_tick_label,...
    'fontname',fnt_name,'Fontsize',fnt_axis,...
    'box','on');
xlabel('Longitude','fontname',fnt_name,...
    'Fontsize',fnt_lbl);
ylabel('Latitude','fontname',fnt_name,...
    'Fontsize',fnt_lbl);
title([crName ' Navigation ' day_date ' (day #' num2str(day) '):  ' channel_id_nav ' (' sub_channel_id_nav ')'],'fontname',fnt_name,...
    'Fontsize',fnt_title,'color',[0 0 0]);
%***********************************************************
%*************  Find measurements interval *****************
%***********************************************************
% --- take the hour which have max data points to compute measurements interval.
h_n=[];
for h_i=[0:23]
    rr=find(d_nav(:,colHour_N)==h_i);
    if ~isempty(rr), h_n=[h_n;h_i length(rr)]; end
end
[ma,ma_r]=max(h_n(:,2));
hour_use_for_interval=h_n(ma_r,1);
rrr=find(d_nav(:,colHour_N)==hour_use_for_interval);
% --- time interval in seconds.
time_dif=diff(h_dec_N(rrr,1));
% --- exclude max to cosider missing data.
[mad,madr]=max(time_dif);
time_dif(madr)=[];
% --- average time interval.
time_int_sec=mean(time_dif)*3600;
if time_int_sec>60
    % --- time interval string - in minutes.
    time_int_str=[ ' Time interval =~ ' num2str(time_int_sec/60,'%2.2f') ' minutes'];
else
    % --- time interval string - in seconds. 
    time_int_str=[ ' Time interval =~ ' num2str(time_int_sec,'%2.2f') ' seconds'];
end
%***********************************************************
%*************   PLOT Text - file info   *******************
%***********************************************************
    % --- control text position depending on plots aspect ratio.
    kx_text=0.05;
    if relyx>=2.4
        ky_text=0.14;
        kx_text=0.7;
    elseif relyx>=1.69
        ky_text=0.14;
        kx_text=0.4;
    elseif relyx>=0.87
        ky_text=0.14;
        kx_text=0.1;
    elseif relyx>=0.82
        ky_text=0.14;
    elseif relyx>=0.67
        ky_text=0.16;
    elseif relyx<=0.195
        ky_text=0.6;
    elseif relyx<=0.32
        ky_text=0.4;
        %kx_text=1;
    else
        ky_text=0.3;
    end
str_t(1)={['File name: ' fname_nav_text '.' fname_ext_nav ',' time_int_str]};
text(lonmin-range([lonmin lonmax])*kx_text,latmin-range([latmin latmax])*ky_text,str_t,...
    'fontsize',fnt_text,'fontname',fnt_name,...
    'horizontalAlignment','left','fontAngle','it','color',[0 0 1]);
estimated_time_minutes=etime(clock,t0)/60

