% nbp1201_ctd_profs_finalFromMatfile.m
% Plot one CTD parameter: all CTD stations and one selected.
%
% I N P U T: nbp1201_ctd.mat
% File location:
% ~/antarctic/nbp1201/CTD/process_final/matfile/
% File contents:
% ctd_data ctd_list data_variables list_variables.
% File is output from: nbp1201_read_ctd_data_final.m

clear;

% --- Plots color, ID.
plot_color_select=[1 0 0];
plot_color_all=[0.7 0.7 0.7];
plot_id_select='-';
plot_id_all='-';
% ---- Fonts.
fnt_axes=10;
fnt_label=10;
fnt_title=10;
title_color=[0 0 1];
line_width=0.8;
% Cruse name.
cr_name='NBP1201';
% --- "Bad" Primary Oxygen - cast 51 Warning text.
% (do not use this profile):
bad_oxy_text(1)={'!!! Cast #51 "bad" Oxygen:'};
bad_oxy_text(2)={'VPR sensor used for test.'};
bad_oxy_text(3)={'Do not use this profile'};
bad_oxy_color=[0 0.6 0];
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
%@@@@@@@@@@@@@@@@@@    CTD MATFILE PATH, NAME  @@@@@@@@@@@@@@@@@@@@@
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
path_name='~/antarctic/nbp1201/CTD/process_final/matfile/';
fname='nbp1201_ctd';
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
%@@@@@@@@@@@@@@    CTD MATFILE ctd_data variables  @@@@@@@@@@@@@@@@@
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
% Columns#.
colSt=1;
colCast=2;
colTime=3;
colPres=4;
colTemp=5;
colTemp_s=6;
colCnd=7;
colCnd_s=8;
colFluor=9;
colTrans=10;
colAltim=11;
colPar=12;
colCpar=13;
colSpar=14;
colOxyV=15;       %  Oxy V
colOxyV_s=16;     %  Oxy V
colPotTemp=17;
colPotTemp_s=18;
colOxy=19;       %  Oxy ml/L
colOxy_s=20;     %  Oxy ml/L
colSal=21;
colSal_s=22;
colSig=23;
colSig_s=24;
colFlag=25;
% Flag - 'bad data'.
bad_flag=-9.990e-29;
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
%@@@@@@@@@@@@@@@@    CTD MATFILE: ctd_list variables  @@@@@@@@@@@@@@
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
% Columns#:
col_cast=1;
col_year=2;
col_mon=3;
col_day=4;
col_h=5;
col_min=6;
col_sec=7;
col_lat=8;
col_lon=9;
%**************************************************************
%*********************  LOAD CTD MATFILE   ********************
%**************************************************************
p=['load ' path_name fname ';'];
eval(p);
% whos
%  Name                    Size               Bytes  Class     Attributes
%  ctd_data            67734x25            13546800  double
%  ctd_list              119x9                 8568  double
%  data_variables         25x14                 700  char
%  list_variables          9x19                 342  char
D=ctd_data;
c_list=ctd_list;
ctdList_all=(c_list(:,col_cast) )';
%**************************************************************
%*********************   MENU: CTD CAST  **********************
%**************************************************************
castNum_select=f_menu_of_numbers_title('CTD CAST',ctdList_all);
if isempty(castNum_select), return; end
% CTD Cast name.
if castNum_select==4.2
    cast_name=[num2str(floor(castNum_select)) 'A'];
else
    cast_name=num2str(castNum_select);
end
%***************************************************************
%*******************   CTD CAST DATE/TIME   ********************
%***************************************************************
% Date.
r_st=find(c_list(:,col_cast)==castNum_select);
yyyy_st=c_list(r_st,col_year);
mm_st=c_list(r_st,col_mon);
dd_st=c_list(r_st,col_day);
% Time.
hh_st=c_list(r_st,col_h);
minmin_st=c_list(r_st,col_min);
secsec_st=c_list(r_st,col_sec);
% Date and time - string represetation.
str_date=datestr([yyyy_st mm_st dd_st hh_st minmin_st secsec_st],0);
%***************************************************************
%************************  MENU PARAMETER  *********************
%***************************************************************
parNum = menu('Quantity',...
    'Temperature prim (deg C)',...
    'Temperature sec (deg C)',...
    'Conductivity prim (S/m)',...
    'Conductivity sec (S/m)',...
    'Fluorescence (mkg/L)',...
    'Transmissometer (%)',...
    'Altimeter (m)',...
    'Irradiance PAR (mkE/m^2 sec)',...
    'Surface Irradiance SPAR (mkE/m^2 sec)',...
    'Cpar',...
    'Oxygen prim (ml/L)',... % (ml/L)
    'Oxygen sec (ml/L)',...  % (ml/L)
    'Salinity prim (psu)',...
    'Salinity sec (psu)',...
    'Potential Temp prim (deg C)',...
    'Potential Temp sec (deg C)',...
    'Sigmat prim (mg/m^3)',...
    'Sigmat sec (mg/m^3)',...
    'Oxygen raw prim (V)',...
    'Oxygen raw sec (V)',...
    'CANCEL');
if parNum>20, return; end
if parNum==1
    colNum = colTemp;
    j_label='Temperature (deg C)';
    jN='Temperature Prim';
elseif parNum==2
    colNum = colTemp_s;
    j_label='Temperature (deg C)';
    jN='Temperature Sec';
elseif parNum==3
    colNum = colCnd;
    j_label='Conductivity (S/m)';
    jN='Conductivity Prim';
elseif parNum==4
    colNum = colCnd_s;
    j_label='Conductivity (S/m)';
    jN='Conductivity Sec';
elseif parNum==5
    colNum = colFluor;
    j_label='Fluorescence (\mug/L)';
    jN='Fluorescence';
elseif parNum==6
    colNum = colTrans;
    j_label='Transmissometer ()';
    jN='Transmissometer';
elseif parNum==7
    colNum = colAltim;
    j_label='Altimeter (m)';
    jN='Altimeter';
elseif parNum==8
    colNum = colPar;
    j_label='PAR (mkE m^-^2 sec^-^1)';
    jN='Irradiance PAR';
elseif parNum==9
    colNum = colSpar;
    j_label='SPAR (mkE m^-^2 sec^-^1)';
    jN='SPAR';
elseif parNum==10
    colNum = colCpar;
    j_label='Cpar';
    jN='Cpar';
elseif parNum==11
    colNum = colOxy;
    j_label='Oxygen Prim (ml/L)';
    jN='Oxygen prim (ml/L)';
    % !!! Text location for cast #51 "bad" Oxygen:
    x_bad_oxy=8.83;
elseif parNum==12
    colNum = colOxy_s;
    j_label='Oxygen Sec (ml/L)';
    jN='Oxygen sec (ml/L)';
elseif parNum==13
    colNum = colSal;
    j_label='Salinity Prim (psu)';
    jN='Salinity prim';
elseif parNum==14
    colNum = colSal_s;
    j_label='Salinity Sec (psu)';
    jN='Salinity sec';
elseif parNum==15
    colNum = colPotTemp;
    j_label='Potential Temp prim (deg C)';
    jN='Potential Temp Prim';
elseif parNum==16
    colNum = colPotTemp_s;
    j_label='Potential Temp prim (deg C)';
    jN='Potential Temp Sec';
elseif parNum==17
    colNum = colSig;
    j_label='SigmaT Prim (kg/m^3)';
    jN='SigmaT prim';
elseif parNum==18
    colNum = colSig_s;
    j_label='SigmaT Sec (kg/m^3)';
    jN='SigmaT sec';
elseif parNum==19
    colNum = colOxyV;
    j_label='Oxygen Raw Prim (V)';
    jN='OxyV prim';
    % !!! Text location for cast #51 "bad" Oxygen:
    x_bad_oxy=2.42;
elseif parNum==20
    colNum = colOxyV_s;
    j_label='Oxygen Raw Sec (V)';
    jN='OxyV sec';
end
str_disp='"BAD" DATA INFO:';
count_cast=0;
for castNum=ctdList_all
    count_cast=count_cast+1;
    % Cast data.
    rc=find(D(:,colCast)==castNum);
    data=D(rc,:);
    % C O R R E C T I O N (1) - remove "bad" data.
    r_bad=find(data(:,colNum)==bad_flag);
    if ~isempty(r_bad)
        parCast_bad=data(r_bad,colNum);
        presCast_bad=data(r_bad,colPres);
        % Display "bad" data.
        str_disp=strvcat(str_disp,['Cast No ' num2str(castNum) ', ' jN ': ' num2str(length(r_bad)) ' line(s) with "bad" data (flag=-9.990e-29)'],...
            'Pressure and Parameter:',num2str([presCast_bad parCast_bad]));
        data(r_bad,:)=[];
    end
    % C O R R E C T I O N (2) - replace  Fluorescence negatives with zeros.
    if parNum==5
        r_minus=find(data(:,colNum)<0);
        data(r_minus,colNum)=0;
    end
    % Cast parameter and pressure.
    parCast=data(:,colNum);
    presCast=data(:,colPres);
    %***************************************************************
    %***********************  PLOT - ALL CASTS *********************
    %***************************************************************
    if count_cast==1
        % Set figure.
        figure;
        set(gcf,'color',[1 1 1],'position',[ 126   464   503   457],...
            'paperPositionMode','auto');
        % Plot fist profile.
        hh(1)=plot(parCast,presCast,plot_id_all,'color',plot_color_all,'linewidth',line_width);
    else
        hold on;
        plot(parCast,presCast,plot_id_all,'color',plot_color_all,'linewidth',line_width);
        hold off;
    end
    % C O R R E C T I O N (3) - needed for the Oxygen Primary from Cast 51.
    % !!! Do not use "Bad" Primary Oxygen from cast #51. This cast was the test
    % for the VPR  Oxygen Sensor.
    % Plot warning text:
    if (parNum==11 | parNum==19) & castNum==51
        hold on;
        plot(parCast,presCast,plot_id_all,'color',bad_oxy_color,'linewidth',line_width);
        hold off;
        text(x_bad_oxy,605,bad_oxy_text,...
            'horizontalAlignment','left',...
            'color',bad_oxy_color);
    end
    %***************************************************************
    %*******************   DATA - SELECTED CAST  *******************
    %***************************************************************
    if castNum==castNum_select
        parCast_select=parCast;
        presCast_select=presCast;
        st_name=num2str(data(1,colSt));
    end
    clear parCast presCast;
    clear data;
end  % For  for castNum=ctdList_all
%***************************************************************
%*******************  PLOT - SELECTED CAST *********************
%***************************************************************
hold on;
hh(2)=plot(parCast_select,presCast_select,plot_id_select,'color',plot_color_select,'linewidth',line_width+0.5);
hold off;
%***************************************************************
%*****************    AXES PROPERTIES   ************************
%***************************************************************
grid on;
set(gca,'ydir','reverse','fontsize',fnt_axes,'box','on');
ylabel('Pressure (db)','fontsize',fnt_label);
xlabel(j_label,'fontsize',fnt_label);
str_title(1)={['\color{blue}' cr_name ' CTD Cast ' cast_name  ' (Station ' st_name ') ' jN '. \color{red}FILENAME: nbp1201\_ctd.mat']};
str_title(2)={['\color{blue}' str_date ' UTC' ]};
title(str_title,'fontsize',fnt_title);
%******************
% Legend.
%******************
str_leg=strvcat('All casts','Selected cast');
hl=legend(hh(1:2),str_leg,4);
%******************
% Display "bad" data.
%******************
%if size(str_disp,1)>1
%    disp(str_disp);
%end

