% pioneer_tn368_read_ctd_matfile.m
% From: pioneer_rb1904_read_ctd_matfile.m

clear;

% Cruise ID.
cr_id='tn368';
crName='TN368';
lowerDepth=520;
%
% Bad surface (at least Cast #5 sal00 at 2m looks suspicious).
bad_surface=2;
%
remove_bad_surface='yes';
%remove_bad_surface='no';
% 
%check_data='yes';
check_data='no';
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
%@@@@@@@@@@@@@@@@@@  DATA PATHS   @@@@@@@@@@@@@@@@@@@@
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
data_location='olgas_path';
%data_location='your_path';
if strcmp(data_location,'your_path')
    % If all data files are located in the same directory as the Matlab code,
    % use this:
    path_name='';
elseif strcmp(data_location,'olgas_path')
    path_name='~/OOI_pioneer/tn368/ctd/process/ctd_matfile/';
end
path_name_txt=strrep(path_name,'_','\_');
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
%@@@@@@@@@@@@@@@@@@  FILE NAME  @@@@@@@@@@@@@@@@@@@@@@
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
fname='tn368_ctd_data';
fname_txt=strrep(fname,'_','\_');
fname_ext='mat';
%*****************************************************
%************* LOAD CTD MATFILE **********************
%*****************************************************
p=['load ' path_name fname ';'];
eval(p);
% MATFILE variables:
% --- Three  numeric matricies:
% (1) CTD data, variable name "data", size: 37310x26;
% (2) CTD casts location and date, variable name "info_ctd_casts", size: 141x9;
% (3) CTD casts ## and stations name, variable name
% "info_ctd_casts_stations", size: 141x3;
% --- Three string variables with numeric matricies columns and quantities name:
% (1) data_columns;
% (2) info_ctd_casts_columns;
% (3) info_ctd_casts_stations_columns.
% --- Numeric variable - CTD data "bad" flag:
% (1) "bad_flag" = -9.990e-29;
% --- Numeric matricies data and column:
% (1) data:
d=data;
clear data;
% Cast and Pressure columns.
colCast_m=1;
colPres_m=2;
% (2) info_ctd_casts:
c_list=info_ctd_casts;
% Columns#:
col_st=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;
cast_list=(unique(c_list(:,col_st)))';
% (3) info_ctd_casts_stations:
d_cs=info_ctd_casts_stations;
% Columns:
cs_colCast=1;  % CTD cast
cs_colSt=2;    % station #
cs_colStId=3;  % Station ID: 1-A, 2-B, 3-AUV, 4-AL-CTD, 5-P, 6-NS, 7-EW, 8-NS6A, 9-A10z, 10-SLP, 11-SSF, 12-ALF, 13-AC, 14-AL, 15-HS, 16-S, 17-L
%*******************************
% Font, color.
%*******************************
fnt_axes=8;
fnt_label=9;
fnt_title=10;
plot_color_all=[0.6 0.6 0.6];
show_st_color=[1 0 0];
line_width=0.8;
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
% MENU (1) Show stations options: "ALL and ONE" or "ONE"
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
% Options:
% show all casts with selected one on the top
% or
% show selected cast.
show_stations_options=f_menu_of_strings('Show stations',strvcat('All and One','One'));
if isempty(show_stations_options), return; end
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
%@@@@@  MENU (2): CAST # @@@@@@@
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
% Select the cast.
cast_num=f_menu_of_numbers_title('Show Station',cast_list);
if isempty(cast_num), return; end
cast_str=num2str(cast_num);
%************************************************
%***** SELECTED STATION NAME and DATE/TIME ******
%************************************************
% --- STATION NAME.
r_s=find(d_cs(:,cs_colCast)==cast_num);
st_num=d_cs(r_s,cs_colSt);
st_id=d_cs(r_s,cs_colStId);
    % Station ID: 1-A, 2-B, 3-AUV, 4-AL-CTD, 5-P, 6-NS, 7-EW, 8-NS6A, 9-A10z, 10-SLP, 11-SSF, 12-ALF, 13-AC, 14-AL, 15-HS, 16-S, 17-L;
    switch st_id
        case 1
            st_name_part='A';
        case 2
            st_name_part='B';
        case 3
            st_name_part='AUV';
        case 4
            st_name_part='AL-CTD';
        case 5
            st_name_part='P';
        case 6
            st_name_part='NS';
        case 7
            st_name_part='EW';
        case 8
            st_name_part='NS6A'; % !!! (cast 57)
        case 9
            st_name_part='A10z'; % !!! (cast 26)
            % ------ TN368 stations ------------
        case 10 %### TN368
            st_name_part='SLP';
        case 11 %### TN368
            st_name_part='SSF';
        case 12 %### TN368
            st_name_part='ALF';
        case 13 %### TN368
            st_name_part='AC';
        case 14 %### TN368
            st_name_part='AL';
        case 15 %### TN368
            st_name_part='HS';
        case 16 %### TN368
            st_name_part='S';
        case 17 %### TN368
            st_name_part='L';
    end
    if st_id==8 | st_id==9 | st_id==10
        st_name=st_name_part;
    else
        st_name=deblank([st_name_part num2str(st_num)]);
    end
% --- DATE.
r_st=find(c_list(:,col_st)==cast_num);
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 (3): QUANTITY @@@@@
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
par_name_list=strvcat(data_columns,'CANCEL');
% Convert vector of strings to cell array.
par_name_list_cell=cellstr(par_name_list);
parNum = menu('Quantity',par_name_list_cell);
if parNum>length(par_name_list), return; end
column_name=deblank(par_name_list(parNum,:));
%*******************************
% Quantity name and column#.
%*******************************
[T,R]=strtok(column_name,' - ');
% Parameter column #.
colNum=str2num(T);
% Parameter name.
R(1:3)='';
par_name=R;
jN=par_name(1:5);
%##############################
%########  FIGURE #############
%##############################
figure;
fig_pos=[657.00        307.00        431.00        518.00];
set(gcf,'color',[1 1 1],'position',fig_pos,...
    'paperPositionMode','auto');
leg_text='';
hh=[];
%*****************************************************
%************    PLOT All stations   *****************
%*****************************************************
str_disp='Bad points info:';
total_bad=[];
cast_all_bad=[];
bad_surface_sal=[];
if strcmp(show_stations_options,'All and One')
    hold on;
    for castNum=cast_list
        r_st=find(d(:,colCast_m)==castNum);
        parSt=d(r_st,colNum);
        presSt=d(r_st,colPres_m);
        cast_total_points=length(parSt);
        %--------------------------
        % Remove "bad" - data indicated with flag.
        %--------------------------
        r_bad=find(parSt(:,1)==bad_flag);
        if ~isempty(r_bad)
            total_bad=[total_bad;castNum length(r_bad)];
            str_disp=strvcat(str_disp,['Cast No ' num2str(castNum) ', ' jN ': ' num2str(length(r_bad)) ' line(s) with "bad" data (flag=-9.990e-29)']);
            parSt(r_bad,:)=[];
            presSt(r_bad,:)=[];
        end
        if ~isempty(parSt)
        %--------------------------
        % Remove "bad" - bad surface data.
        %--------------------------
            if strcmp(remove_bad_surface,'yes')
                r_bad_surf=find(presSt(:,1)<=bad_surface);
                if ~isempty(r_bad_surf)
                    parSt(r_bad_surf,:)=[];
                    presSt(r_bad_surf,:)=[];
                end
            end
            % --- Plot station data.
            hh(1)=plot(parSt,presSt,'-',...
                'color',plot_color_all,'linewidth',line_width);
            %--------------------------------
            %--------- C H E C K  ???BAD ----
            %--------------------------------
            if strcmp(check_data,'yes')
            % Too low Salinity at surface?
            if strcmp(jN,'sal00')
                rrr_bad=find(parSt(:,1)<25);
                if ~isempty(rrr_bad)
                    bad_surface_sal=[bad_surface_sal;castNum*ones(1,length(rrr_bad)) presSt(rrr_bad) parSt(rrr_bad)];
                end
                % ANS:
                % Cast, pres, sal
                % bad_surface_sal =
                %  5.00          2.00         16.72
            end
            %--------- C H E C K  END ---------
            end % for if strcmp(check_option,'yes')
        else
            cast_all_bad=[cast_all_bad;castNum cast_total_points];
        end
        if castNum==cast_list(1), leg_text=strvcat(leg_text,'All CTD casts'); end
        clear r_st parSt presSt;
    end
    hold off;
end
%*****************************************************
%************  PLOT SELECTED station  ****************
%*****************************************************
% --- Select station data:
r_st=find(d(:,colCast_m)==cast_num);
parSt=d(r_st,colNum);
presSt=d(r_st,colPres_m);
        %--------------------------
        % Remove "bad" - bad surface data.
        %--------------------------
if strcmp(remove_bad_surface,'yes')
    r_bad_surf=find(presSt(:,1)<=bad_surface);
    if ~isempty(r_bad_surf)
        parSt(r_bad_surf,:)=[];
        presSt(r_bad_surf,:)=[];
    end
end
%--------------------------
% Remove "bad" data indicated with flag.
%--------------------------
r_bad=find(parSt(:,1)==bad_flag);
if ~isempty(r_bad)
    parSt(r_bad,:)=[];
    presSt(r_bad,:)=[];
end
% --- Plot Selected station.
if ~isempty(parSt)
    hold on;
    hh(length(hh)+1)=plot(parSt,presSt,'-',...
        'color',show_st_color,'linewidth',line_width+0.5);
    leg_text=strvcat(leg_text,['CTD cast ' cast_str ' st ' st_name]);
    hold off;
end
%*****************************************************
%**************  SET AXES PROPERTIES  ****************
%*****************************************************
ah=get(gca,'position');
set(gca,'position',[ah(1)-ah(3)*0.03 ah(2)+ah(4)*0.13 ah(3)*1.1 ah(4)*0.80]);
x_lim=get(gca,'xlim');
y_lim=[0 lowerDepth];
axis([x_lim y_lim]);
grid on;
set(gca,'ydir','reverse','fontsize',fnt_axes,'box','on');
xlabel([par_name ' (column #' num2str(colNum) ')'],'fontsize',fnt_label);
ylabel('Pressure (db)','fontsize',fnt_label);
str_title(1)={[crName]};
str_title(2)={['CTD cast #' cast_str ' Station ' st_name ': ' str_date ' UTC']};
title(str_title,'fontsize',fnt_title,'color','b');
%**************************
% Legend.
%**************************
if size(leg_text,1)>1, legend(hh,leg_text,'location','SouthEast'); end
%*********************
% Text - file name.
%*********************
str_text(1)={['CTD matfile: ' path_name_txt fname_txt '.' fname_ext]};
x_t=x_lim(1)-0.1*range(x_lim);
text(x_t,y_lim(2)+0.2*range(y_lim),str_text,'color',[0 0 0],'horizontalAlignment','left','fontsize',8);
if size(str_disp,1)>1
    disp(str_disp);
end
if ~isempty(cast_all_bad), cast_all_bad, end

