% nbp1201_uw_plot_all_chris.m
%
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
%@@@@@@@@@@@@@@@@@   INPUT  @@@@@@@@@@@@@@@@@@@@
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
% Merged PCO2 files created during the cruise by Chris.
% (1) Days 7:15(part) data:
% rttsg_days_7_15.mat - output from 'nbp1201_read_PCO2_chris.m which input is
% data on daily basis;
% (2) Chris' merged data file for the remaining days 15-36 (includes part of day 15):
% rttsg.
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
% PLOT data locations and values in colormap,
% all days [8:36]. Day 7 is available but short - starts at 22:13:27
%
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
%@@@@@@@@@@@@@   DATA SOURCE  @@@@@@@@@@@@@@@@@@
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
% Merged pCO2
% (1)
% http://science.whoi.edu/users/mcgillic/nbp1201/science_drive/rttsg/
% File names:
% 20120107-combo-rttsg  % start: Jan-7-2012 22:13:27
% 20120108-combo-rttsg
% 20120109-combo-rttsg
% 20120110-combo-rttsg
% 20120111-combo-rttsg
% 20120112-combo-rttsg
% 20120113-combo-rttsg
% 20120114-combo-rttsg
% 20120115-combo-rttsg  % end:   Jan-15-2012 02:06:05
% (2)
% From Chris during the cruise time.
% File name:
% rttsg  % start: Jan-15-2012 02:07:51
%          end:   Feb-5-2012 20:31:33
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

clear;
t0 = clock;

saveOption='yes';
%saveOption='no';

plotTimeMarkes='yes';
%plotTimeMarkes='no';

% Data path.
path_name_uw='/data2/antarctic_data2/nbp1201/underway/process_ship/rttsg_dir/';
day_list=8:36;% day 7 is available but short - starts at 22:13:27
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
%@@@@@@@@@@   Data file columns:  @@@@@@@@@@@
%@@@@@@@@@@@ Date/Time/Location  @@@@@@@@@@@@
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
colDay_M=1;
colMonth_M=2;
colYear_M=3;
colHour_M=4;
colMin_M=5;
colSec_M=6;
colLat_M=7;    % NBPseap latitude, degrees
colLon_M=8;    % NBPseap longitude, degrees
day_format='day_of_date';
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
%******************
% C O N S T A N T S
%******************
% Font size.
fnt_axis=8;
fnt_lbl=8;
fnt_title=8;
fnt_text=8;
fnt_name='helvetica';
marker_size=1;
marker_size_time=8;
crName='NBP1201';
%#############################################
%############  COLORMAP data  ################
%#############################################
% Load Colormap.
load palet_13_ant;
cmap=palet_13_ant;
n_bands=size(cmap,1);
n_dec_chars=2;
cval=1:size(cmap,1)+1;
%#############################################
%############   Map limits  ##################
%#############################################
%axis([160 205 -80 -72]);
lonmin = 160;
lonmax = 195;
latmax = -72;
latmin =-79;
% --------  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 ##############
%#############################################
% Ticks interval.
lon_int=5;
lat_int=1;
% ---- 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;
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
%@@@@@@@@@@@@@  Quantities list   @@@@@@@@@@@
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
par_list=strvcat('09 NBPMTSGsal',...
    '10 NBPMTSGtemp',...
    '11 NBPpco2PCO2wetD',...
    '12 NBPhDASchlor',...
    '13 NBPoxygOxyg',...
    '14 NBPoxygSat',...
    '15 NBPRtmprtemp');
%------------------------------------------------
%-------------------- MENU (2): QUANTITY --------
%------------------------------------------------
colNum_name=f_menu_of_strings('QUANTITY Column: Name',par_list);
if isempty(colNum_name), return; end
%-----------------------------------------------
colNum=str2num(colNum_name(1:2));
%q_name=deblank(colNum_name(4:end));
source='rttsg';
switch  colNum
    case 10
        channel_id='mtsg';
        channel_name='Micro-TSG';
        q_name='Internal Temperature degC';
        %%%colNum=10; % !!!rttsg
        q_name_print='internal_temp';
    case 15
        channel_id='rtmp';
        channel_name='Remote Temperature';
        q_name='Temperature at seawater intake degC';
        %%%colNum=15; % !!!rttsg
        q_name_print='remote_temp';
    case 9
        channel_id='mtsg';
        channel_name='Micro-TSG';
        q_name='Salinity';
        %%%colNum=9; % !!!rttsg
        q_name_print='sal';
    case 11
        channel_id='pco2';
        channel_name='PCO2';
        q_name='pCO2 pressure microAtm';
        %%%colNum=11; % !!!rttsg
        q_name_print='PCO2';
    case 13
        channel_id='oxyg';
        channel_name='Oxygen data';
        q_name='Oxygen Reading MkM';
        %%%colNum=13; % !!!rttsg
        q_name_print='oxy';
    case 14
        channel_id='oxyg';
        channel_name='Oxygen data';
        q_name='Saturation Reading %';
        %%%colNum=14; % !!!rttsg
        q_name_print='oxy_sat';
    case 12
        channel_id='hdas';
        channel_name='Hydro-DAS';
        q_name='Fluorescence \mug/L'; %!!! calc
        %%%colNum=12; % !!!rttsg
        q_name_print='fluor';
end
%$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
%$$$$$$$$$$$$$$$$  LOAD UNDERWAY merged data  $$$$$$$$$$$$
%$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
% Day 15 data cosists from two parts: from Chris' merged data on daily basis and from
% Chris' merged data remaining days 15-36 (both ship process).
% Part 1.
fname_uw_1='rttsg_days_7_15';
p=['load ' path_name_uw fname_uw_1 '.mat;'];
eval(p);
p=['d_uw_1=' fname_uw_1 ';'];
eval(p);
% Part 2.
fname_uw_2='rttsg';
p=['load ' path_name_uw fname_uw_2 ';'];
eval(p);
p=['d_uw_2=' fname_uw_2 ';'];
eval(p);
d_uw_all=[d_uw_1;d_uw_2];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%  Collect ALL DAYS DATA    %%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
date_format='mm/dd/yyyy';
DATA=[];
days=[];
q_note='';
for day=day_list
    days=[days day];
    if day>=361
        year=2011;
    else
        year=2012;
    end
    % Date.
    date_num=datenum([year-1 12 31 24 0 0])+(day-1);
    date_vec=datevec(date_num);
    month=date_vec(1,2);
    date_day=date_vec(1,3);
    %day_date=datestr(date_vec,date_format);
    clear date_num date_vec;
    %$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
    %$$$$$$$$$$$$$$$$     Selected day data       $$$$$$$$$$$$
    %$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
    % Selected day data.
    r_day=find(d_uw_all(:,colYear_M)==year-2000 & d_uw_all(:,colMonth_M)==month & d_uw_all(:,colDay_M)==date_day);
    d_uw=d_uw_all(r_day,:);
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %%%%%%%%  Make West Lon positive %%%%%%%%%%
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Make W Lon positive - all E lon
    r_w=find(d_uw(:,colLon_M)<0);
    d_uw(r_w,colLon_M)=d_uw(r_w,colLon_M)+360;
    clear r_w;
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %%%%%%%  E X C L U D E  NaN data  %%%%%%%%%
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    r_nan=find(isnan(d_uw(:,colNum)));
    d_uw(r_nan,:)=[];
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %%%%%%%%   C O R R E C T I O N   %%%%%%%%%%
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % ------ C O R R E C T I O N  - pco2 NEGATIVES ---------
    if strcmp(q_name(1:4),'pCO2')
        r_minus=find(d_uw(:,colNum)<0);
        if ~isempty(r_minus)
            d_uw(r_minus,:)=[];
            q_note=strvcat('NOTE:',q_name,[num2str(length(r_minus)) ' NEGATIVES have been removed']);
        end
    elseif strcmp(q_name(1:4),'Fluo')
        r_minus=find(d_uw(:,colNum)<0);
        if ~isempty(r_minus)
            d_uw(r_minus,:)=0;
            q_note=strvcat('NOTE:',q_name,[num2str(length(r_minus)) ' NEGATIVES have been replaced with zero']);
        end
    end
    % ------ C O R R E C T I O N  -  svp1 (Sound velocity), pco2 NEGATIVES END ---
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %%%%%%%%%  DATA %%%%%%%%%%%%
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%
    if isempty(d_uw)
        disp(['Day #' num2str(day) ': ' channel_id  ' ' q_name ' ' subname_str ': no data'])
        return;
    else % For if isempty(d_uw)
        DATA=[DATA;d_uw];
    end % For if isempty(d_uw)
    clear d_uw;
end % for day=day_list
% Quantity.
q_uw=DATA(:,colNum);
% Min, max.
data_min_max=[min(q_uw) max(q_uw)];
% Quantity min, max.
q_min_d = data_min_max(1,1);
q_max_d = data_min_max(1,2);
% Location.
lat_uw=DATA(:,colLat_M);
lon_uw=DATA(:,colLon_M);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%     PLOT       %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Set figure properties
%fig_pos=[118.00        269.00        737.00        558.00];
%figure;
%set(gcf,'color',[1 1 1],'position',fig_pos,...
%    'paperPositionMode','auto');
%colormap(cmap);
% Set figure properties
fig_pos=[118.00        475.00        490.00        354.00];
%fig_pos=[118.00        471.00        490.00        358.00];
figure;
set(gcf,'color',[1 1 1],'position',fig_pos,...
    'paperPositionMode','auto');
colormap(cmap);
kk_x=1.045;
kk_y=1.045;
%*********************
% Set axes position.
%*********************
gca;
aPos=get(gca,'position');
clf;
hhh=axes('position',[aPos(1)-aPos(3)*0.06 aPos(2)-aPos(4)*0.022 aPos(3)*kk_x aPos(4)*kk_y]);
if q_min_d==q_max_d
    %***********************************************************
    %*******************       MIN=MAX      ********************
    %***************   PLOT locations only  ********************
    %***********************************************************
    hold on;
    plot(lon_uw,lat_uw,'o','color',[0 0 1],'markersize',marker_size);
    hold off;
else % For if q_min_d==q_max_d
    % Calculate colormap vector.
    if strcmp(channel_id,'hdas') & strcmp(q_name(1:5),'Fluor')
        vv=f_colormap_vector(0,12,n_bands,n_dec_chars);
    else
        vv=f_colormap_vector(q_min_d,q_max_d,n_bands,n_dec_chars);
    end
    if isempty(vv)
        disp(strvcat('Data range is too small to calculate the colormap vector',...
            'Try to increase the number of decimal points (function f_colormap_vector input variable n_dec_chars'));
        return;
    end
    colormap(cmap);
    %@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    %@@@@@@  PLOT markers in COLORMAP color @@@@@@@@@@@@
    %@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    hold on;
    D=[];
    for jj=1:size(q_uw,1)
        q_j=q_uw(jj,1);
        lat=lat_uw(jj,1);
        lon=lon_uw(jj,1);
        countSearch=0;
        for ii=1:length(vv)-1
            countSearch=countSearch+1;
            if q_j<=vv(1)
                color_j=cmap(1,:);
            elseif q_j>=vv(end)
                color_j=cmap(end,:);
            elseif q_j>=vv(ii) & q_j<vv(ii+1)
                color_j=cmap(ii,:);
            else
                color_j=[];
            end
            if ~isempty(color_j), break; end
        end
        if ~isempty(color_j)
            % Check.
            D=[D;q_j ii cmap(ii,:)];
            % Plot marker in colormap color.
            plot(lon,lat,'o','markersize',marker_size,...
                'markeredgecolor',color_j,'markerfacecolor',color_j,...
                'linewidth',0.3);
        else % For if ~isempty(color_j)
            disp(['Color not found for the point: #' num2str(jj) ', value=' num2str(q_j)]);
            return;
        end % For  if ~isempty(color_j)
    end
    hold off;
    %***********************************************************
    %***********************   COLORBAR   **********************
    %***********************************************************
    cb_ticklabel=vv;
    caxis([min(cval) max(cval)]);
    h1 = colorbar;
    aPos = get(h1,'Position');
    %set(h1,'Position',[aPos(1,1)+aPos(1,3)*1.6 aPos(1,2)+aPos(1,4)*0.1 aPos(1,3)*0.7 aPos(1,4)*0.8]);
    set(h1,'Position',[aPos(1,1)+aPos(1,3)*1.7 aPos(1,2)+aPos(1,4)*0.1 aPos(1,3)*0.45 aPos(1,4)*0.8]);
    set(h1,'ytick',cval,...
        'yLim',[min(cval) max(cval)],...
        'ytick',cval,'yticklabel',cb_ticklabel,...
        'Fontsize',fnt_axis,'ticklength',get(h1,'ticklength')*3.5,...
        'xcolor',[0 0 0],'ycolor',[0 0 0],'fontname',fnt_name);
    %if n_bands>10
    %    tlb=f_ticklabel(cb_ticklabel,2);
    %    set(h1,'ytickLabel',tlb);
    %else
    %   set(h1,'yticklabel',cb_ticklabel);
    %end
end % For if q_min_d==q_max_d
%***********************************************************
%********************   PLOT Min/Max   *********************
%***********************************************************
str_mi_ma(1)={['Min = ' num2str(data_min_max(1,1))]};
str_mi_ma(2)={['Max = ' num2str(data_min_max(1,2))]};
text(lonmin+range([lonmin lonmax])*0.1,latmin+range([latmin latmax])*0.85,str_mi_ma,...
    'fontsize',fnt_text,'fontname',fnt_name,'horizontalAlignment','left');
%***********************************************************
%*****************  Set Axes Properties  *******************
%***********************************************************
%axPos = get(gca,'Position');
%kk=1.05;
%set(gca,'position',[axPos(1)-axPos(3)*0.08 axPos(2)-axPos(4)*0.05 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),...
    'fontname',fnt_name,'Fontsize',fnt_axis,...
    'box','on');
ylabel('Latitude','fontname',fnt_name,...
    'Fontsize',fnt_lbl);
set(gca,'ytick',y_tick,'yticklabel',y_tick_label);
xlabel('Longitude','fontname',fnt_name,...
    'Fontsize',fnt_lbl);
set(gca,'xtick',x_tick,'xticklabel',x_tick_label);
title([crName ' Underway ' source ': ' channel_id ' ' q_name ' (time interval: 1 min)'],...
    'fontname',fnt_name,...
    'Fontsize',fnt_title,'color',[0 0 0]);
if strcmp(plotTimeMarkes,'yes')
    %***********************************************************
    %*****************  PLOT TIME Markers    *******************
    %***********************************************************
    % Decimal hour.
    h_dec_N=DATA(:,colHour_M) + ( DATA(:,colMin_M) + DATA(:,colSec_M)/60 )/60;
    hour_n=0;
    %date_format='yyyy-mm-dd HH:MM:SS';
    date_format='mm/dd/yy HH:MM';
    %date_format='mm/dd/yyyy HH:MM:SS';
    %date_format='mm/dd';
    %date_format='yyyy/mm/dd HH:MM';
    %date_format='HH:MM';
    dd_year=2012;%%%%%%
    hold on;
    for dday=[8]%days
        % Date.
        date_num=datenum([dd_year-1 12 31 24 0 0])+(dday-1);%%%%%%%%
        date_vec=datevec(date_num);
        year_n=date_vec(1,1);
        month_n=date_vec(1,2);
        date_day_n=date_vec(1,3);
        %%%%%%%%%%%%%%%%%%%%%%%%%
        % Find selected date/time from all the data.
        if strcmp(day_format,'day_of_date')
            r_h=find(DATA(:,colYear_M)==year_n-2000 & DATA(:,colMonth_M)==month_n & DATA(:,colDay_M)==date_day_n & ...
                DATA(:,colHour_M)==hour_n &  DATA(:,colMin_M)==0 & DATA(:,colMin_M)<=6);
        elseif strcmp(day_format,'day_of_year')
            r_h=find(DATA(:,colYear_M)==year_n-2000 & DATA(:,colDay_M)==dday & ...
                DATA(:,colHour_M)==hour_n & DATA(:,colMin_M)>=0  & DATA(:,colMin_M)<=6);
        else
            disp('Not considered date format');
            return;
        end
        %%%%%%%%%%%%%%%%%%%%%%%%%
        if ~isempty(r_h)
            % use the point with min HOUR time.
            [mi,rmi]=min(h_dec_N(r_h,1));
            lat=DATA(r_h(rmi),colLat_M);
            lon=DATA(r_h(rmi),colLon_M);
            dnum_n=datenum([year_n month_n date_day_n hour_n DATA(r_h(rmi),colMin_M) 0]);
            text(lon,lat+range([latmin latmax])*0.015,['  ' datestr(dnum_n,date_format)],...
                'fontsize',fnt_text,'fontname',fnt_name,'horizontalAlignment','right','color',[0 0 0]);
            plot(lon,lat,'k+','markersize',marker_size_time,'markerEdgeColor',[0 0 0]);
        end
    end % For  for dday=days
    hold off;
end % For if strcmp(plotTimeMarkes,'yes')
if ~isempty(q_note), ...
        disp(q_note); end
if strcmp(saveOption,'yes')
    fig=get(0,'currentFigure');
    pause(0.1);
    fname_out=['nbp1201_uw_' source '_' channel_id '_' q_name_print '.png'];
    p=['print ' fname_out ' -dpng -f' num2str(fig)];
    eval(p);
end
estimated_time_minutes=etime(clock,t0)/60