% nbp1201_read_bottle_data_old.m

clear;

data_location='your_path';
%data_location='olgas_path';

%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
%@@@@@@@@@@@@@@@@@@  DATA 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='~/antarctic/nbp1201/nuts/nuts_CTD_merged/';
end
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
% FILE NAME.
fname='nbp1201_nuts_chl_iron_CTD';
fname_text=strrep(fname,'_','\_');
fname_ext='txt';
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
% Cruise ID, date.
cr_id='nbp1201';
crName='NBP1201';
cruise_title='R/V Nathaniel Palmer NBP1201';
date_str='December 25, 2011 - February 9, 2012';
% Fonts, markers, color.
fnt_axes=10;
fnt_label=10;
fnt_title=10;
plot_id='o';
plot_color_all=[0.6 0.6 0.6];
show_st_color=[1 0 0];
line_width=0.8;
marker_size=2;
%****************************
% Nuts/Chl/Fe/CTD merged columns - old version:
%****************************
colCast_N=1;      % CTD Cast (not Station #)
colBot_N=2;       % Bottle
colSamp_N=3;      % Nuts Sample
colPres_N=4;      % CTD Pressure
colDep_N=5;       % Nominal depth
% BTL data.
colMon_N=6;       % Month
colDay_N=7;       % Day
colYear_N=8;      % Year
colPotTemp_N=9;   % Potemp090C
colPotTemp_s_N=10;% Potemp190C
colSal_N=11;      % Sal00
colSal_s_N=12;    % Sal11
colSig_N=13;      % Sigma-é00
colTemp_N=14;     % T090C
colCnd_N=15;      % C0S/m
colTemp_s_N=16;   % T190C
colCnd_s_N=17;    % C1S/m
colOxy_N=18;      % Sbeox0ML/L
colOxy_s_N=19;    % Sbeox1ML/L
colFluor_N=20;    % FlECO-AFL
colTrans_N=21;    % Bat
colPar_N=22;      % Par
colLat_N=23;      % Latitude
colLon_N=24;      % Longitude
colTime_N=25;     % TimeJ
colSpar_N=26;     % Spar
% Nuts data.
colPO4_N=27;      % Phosphate (uM)
colNO2_N=28;      % Nitrite (uM)
colNO3NO2_N=29;   % N+N (uM)
colNH4_N=30;      % Ammonium (uM)
colSi_N=31;       % Silicate (uM)
% Chl data.
colFo_N=32;      % Fo
colFa_N=33;      % Fa
colFoFa_N=34;    % Fo/Fa
colChl_N=35;     % Chlorophyll a (ug/L)
colPheo_N=36;    % Pheo a (ug/L)
% Iron data.
colFe_N=37;      % Iron (nM)
%
% Groups first/last columns.
first_col_chl=32;
last_col_chl=36;
first_col_nuts=27;
last_col_nuts=31;
col_iron=37;
%---------------------------
% Merged data list - column # and parameter name:
%---------------------------
columns_list=strvcat('9 - Potemp090C',...
    '10 - Potemp190C',...
    '11 - Sal00',...
    '12 - Sal11',...
    '13 - Sigma-é00',...
    '14 - T090C',...
    '15 - C0S/m',...
    '16 - T190C',...
    '17 - C1S/m',...
    '18 - Sbeox0ML/L',...
    '19 - Sbeox1ML/L',...
    '20 - FlECO-AFL',...
    '21 - Xmiss',...
    '22 - Par',...
    '26 - Spar',...
    '27 - Phosphate (uM)',...
    '28 - Nitrite (uM)',...
    '29 - N+N (uM)',...
    '30 - Ammonium(uM)',...
    '31 - Silicate (uM)',...
    '32 - Fo',...
    '33 - Fa',...
    '34 - Fo/Fa',...
    '35 - Chl a (ug/L)',...
    '36 - Pheo a (ug/L)',...
    '37 - Fe (nM)');
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
%@@@@@@  MENU: Parameter  @@@@@@
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
column_name=f_menu_of_strings('Parameter',columns_list);
if isempty(column_name), return; end
% Parameter - column #.
colNum=str2num(column_name(1:2));
% Parameter - name.
par_name=column_name(6:end);
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
%@@@@@  MENU: Show Casts @@@@@@@
%@@@@ ALL and ONE or ONE  @@@@@@
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
show_casts_options=f_menu_of_strings('Show casts',strvcat('All and One','One'));
if isempty(show_casts_options), return; end
%**************************
% Change lower depth (less for chl data).
%**************************
if colNum>=first_col_chl & colNum<=last_col_chl
    lowerDepth=210;
else
    lowerDepth=950;
end
%***************************************************
%**************  LOAD Merged file  *****************
%***************************************************
p=['load ' path_name fname ';'];
eval(p);
p=['d=' fname ';'];
eval(p);
size(d)
%**************************
% Replace minus (below detection) values with zeros.
%**************************
%if strcmp(par_name,'Chl a (ug/L)') | strcmp(par_name,'Pheo a (ug/L)') | ...
%        strcmp(par_name,'FlECO-AFL') | ...
%        (colNum>=first_col_nuts & colNum<=last_col_nuts )
%    r_minus=find(d(:,colNum)<0);
%    d(r_minus,colNum)=0;
%end
% Casts and Stations lists.
r_nan=find(isnan(d(:,colNum)));
d_for_list=d;
d_for_list(r_nan,:)=[];
cast_list=(unique(d_for_list(:,colCast_N)))';
clear d_for_list;
st_list=[];
for castNum=cast_list
    if castNum<=42
        stNum=floor(castNum)-1; % floor - to consider cast 4.2 (4A)
    else
        stNum=floor(castNum)-2;
    end
    st_list=[st_list stNum];
end
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
%@@@@@@@  MENU: STATION # @@@@@@
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
st_num=f_menu_of_numbers_title('Station #',st_list);
if isempty(st_num), return; end
rr=find(st_list(:)==st_num);
cast_num=cast_list(rr);
%*******************
% Figure.
%*******************
figure;
set(gcf,'color',[1 1 1],'position',[385.00        368.00        413.00        457.00],...
    'paperPositionMode','auto');
%*****************************************************
%*****************   PLOT ALL  ***********************
%*****************************************************
if strcmp(show_casts_options,'All and One')
    hold on;
    for castNum=cast_list
        r_st=find(d(:,colCast_N)==castNum);
        parSt=d(r_st,colNum);
        presSt=d(r_st,colPres_N);
        plot(parSt,presSt,'o-','color',plot_color_all,'linewidth',line_width,...
            'markerFaceColor',plot_color_all,'markerEdgeColor',plot_color_all,...
            'markersize',marker_size);
    end
end
%*****************************************************
%************  SELECTED STATION DATA  ****************
%*****************************************************
r_st=find(d(:,colCast_N)==cast_num);
par_st=d(r_st,colNum);
pres_st=d(r_st,colPres_N);
bot_st=d(r_st,colBot_N);
samp_st=d(r_st,colSamp_N);
dep_st=d(r_st,colDep_N);
%**************************
% Selected station Date/Time.
%**************************
timeJ_st=d(r_st(end),colTime_N);
if timeJ_st>=360
    year_ref=2011;
else
    year_ref=2012;
end
dnum_n=datenum([year_ref 1 1 0 0 0]) + timeJ_st-1;
date_format='dd-mmm-yyyy HH:MM:SS';
date_str=datestr(dnum_n,date_format);
%**************************
% Plot Selected station.
%**************************
hold on;
plot(par_st,pres_st,'o-','color',show_st_color,'linewidth',line_width+0.5,...
    'markerFaceColor',show_st_color,...
    'markerEdgeColor',show_st_color,...
    'markersize',marker_size+3);
hold off;
%**************************
% Display selected station data.
%**************************
par_name
if colNum>=first_col_nuts & colNum<=last_col_nuts % Nutrients
    st_cast_bot_depth_pres_par_NutsSample=[st_num*ones(size(dep_st,1),1) cast_num*ones(size(dep_st,1),1) bot_st dep_st pres_st par_st samp_st]
else
    st_cast_bot_depth_pres_par=[st_num*ones(size(dep_st,1),1) cast_num*ones(size(dep_st,1),1) bot_st dep_st pres_st par_st]
end
%*****************************************************
%**************  SET AXES PROPERTIES  ****************
%*****************************************************
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,'fontsize',fnt_label);
ylabel('Pressure (db)','fontsize',fnt_label);
str_title(1)={[crName ' Station #' num2str(st_num) ' (CTD Cast #' num2str(cast_num) ')']};
str_title(2)={[date_str ' UTC (bottle #1 time)']};
title(str_title,'fontsize',fnt_title,'color','b');
% Text - file name.
text(x_lim(2),y_lim(2)+0.12*range(y_lim),[fname_text '.' fname_ext],...
    'horizontalAlignment','right','fontsize',fnt_axes-1,'color','b','fontAngle','italic');

