load('../data/OMG_data_Nares')
profi_num = size(output,1);
depth = cell(profi_num,1);
% NOTE: a small percentage of probes do not have salinity or density data!
% for these who have salinity or density data
% Quality control (step1): remove -99 data values
flag = find(T_vec==-99|S_vec==-99|rho_vec==-99);
% Quality control (step2): binning
% binning is performed by fun_binning (see the appendix)
[T_vec,S_vec,rho_vec,depth_vec] = fun_binning(T_vec,S_vec,rho_vec,depth_vec);
depth{n} = depth_vec; % m
rho{n} = rho_vec; % kg/m3
% for these who do not have salinity or density data
% Quality control (step1): remove -99 data values
% Quality control (step2): binning
% binning is performed by fun_binning (see the appendix)
[T_vec,S_vec,rho_vec,depth_vec] = fun_binning(T_vec,S_vec,rho_vec,depth_vec);
% select one cast and find SST
MLD_index = find(T(cast,:)>=SST+0.1,1,'first');
MLD = depth(cast,MLD_index)
% calculate absolute salinity and potential temperature
P = gsw_p_from_z(-depth(cast,:),lat); %pressure
SA = gsw_SA_from_SP(S(cast,:),P,lat,lon); %absolute salinity
pT = gsw_pt_from_t(SA,T(cast,:),P,0); %potential temperature
% calculate the mean and sd of SA and pT within the SML
mean_pT_SML = mean(pT(1:MLD_index),'omitnan')
sd_pT_SML = std(pT(1:MLD_index),'omitnan')
mean_SA_SML = mean(SA(1:MLD_index),'omitnan')
sd_SA_SML = std(SA(1:MLD_index),'omitnan')
% plotting surface values: Potential Temperature
set(f,'units','centimeters','position',[1,1,9*1.2,12*1.2],'color','w')
plot(pT(1:MLD_index+105),-depth(cast,1:MLD_index+105),'linewidth',1.5)
plot([pT(MLD_index),pT(MLD_index)],[-depth(cast,1),-depth(cast,MLD_index+105)],'--k','linewidth',1.5)
plot([-2 -0.3],[-MLD,-MLD],'--k','linewidth',1.5)
plot(pT(MLD_index),-depth(cast,MLD_index),'r.','MarkerSize',20)
xlabel('Potential Temperature (^{o}C)','FontWeight','bold')
ylabel('Depth (m)','FontWeight','bold')
set(gca,'FontSize',FontSize,'tickdir','out','xaxislocation','top')
% plotting surface values: Absolute Salinity
set(f,'units','centimeters','position',[1,1,9*1.2,12*1.2],'color','w')
plot(SA(1:MLD_index+105),-depth(cast,1:MLD_index+105),'linewidth',1.5)
plot([SA(MLD_index),SA(MLD_index)],[-depth(cast,1),-depth(cast,MLD_index+105)],'--k','linewidth',1.5)
plot([28 35],[-MLD,-MLD],'--k','linewidth',1.5)
plot(SA(MLD_index),-depth(cast,MLD_index),'r.','MarkerSize',20)
xlabel('Absolute Salinity (ppt)','FontWeight','bold')
ylabel('Depth (m)','FontWeight','bold')
set(gca,'FontSize',FontSize,'tickdir','out','xaxislocation','top')
% plotting surface values: Density
set(f,'units','centimeters','position',[1,1,9*1.2,12*1.2],'color','w')
plot(rho(cast,1:MLD_index+105),-depth(cast,1:MLD_index+105),'linewidth',1.5)
plot([rho(cast,MLD_index),rho(cast,MLD_index)],[-depth(cast,1),-depth(cast,MLD_index+105)],'--k','linewidth',1.5)
plot([1023 1028.6],[-MLD,-MLD],'--k','linewidth',1.5)
plot(rho(cast,MLD_index),-depth(cast,MLD_index),'r.','MarkerSize',20)
xlabel('Density (kg/m^{3})','FontWeight','bold')
ylabel('Depth (m)','FontWeight','bold')
set(gca,'FontSize',FontSize,'tickdir','out','xaxislocation','top')
% calculate and plot the stability frequency
CT = gsw_CT_from_t(SA,T(cast,:),P); %conservative temperature
[N2, p_mid] = gsw_Nsquared(SA,CT,P,lat);
set(f,'units','centimeters','position',[1,1,9*1.2,12*1.2],'color','w')
plot(N2(1:MLD_index+105),-depth(cast,1:MLD_index+105),'linewidth',1.5)
plot(10^-3*[0 2],[-MLD,-MLD],'--k','linewidth',1.5)
xlabel('N^{2} (s^{-2})','FontWeight','bold')
ylabel('Depth (m)','FontWeight','bold')
set(gca,'FontSize',FontSize,'tickdir','out','xaxislocation','top')
mean_SA_SML = ones(profi_num,1);
mean_pT_SML = ones(profi_num,1);
mean_rho = ones(profi_num,1);
MLD_index = find(T(cast,:)>=SST+0.1,1,'first');
if isempty(MLD_index) || MLD_index == 999
MLD(cast) = depth(cast,MLD_index);
% calculate absolute salinity and potential temperature
P = gsw_p_from_z(-depth(cast,:),lat); %pressure
SA = gsw_SA_from_SP(S(cast,:),P,lat,lon); %absolute salinity
pT = gsw_pt_from_t(SA,T(cast,:),P,0); %potential temperature
mean_SA_SML(cast) = mean(SA(1:MLD_index),'omitnan');
mean_pT_SML(cast) = mean(pT(1:MLD_index),'omitnan');
mean_rho(cast) = mean(rho(1:MLD_index),'omitnan');
mean_pT_SML(isnan(MLD)) = [];
mean_SA_SML(isnan(MLD)) = [];
mean_rho(isnan(MLD)) = [];
set(f,'units','centimeters','position',[1,1,15.5*1.5,10*1.5],'color','w')
plot(mean_SA_SML(n),mean_pT_SML(n),'.','color',color(MLD(n),:),'MarkerSize',15)
xlabel('Mean Absolute Salinity (ppt)','FontWeight','bold')
ylabel('Mean Potential Temperature (^{o}C)','FontWeight','bold')
title('MLD (m) and Averaged Water Properties','FontWeight','bold')
set(gca,'FontSize',FontSize,'tickdir','out')
% make scatterplots: rho vs MLD
set(f,'units','centimeters','position',[1,1,15.5*1.5,10*1.5],'color','w')
plot(MLD,mean_rho,'.','MarkerSize',15)
xlabel('Mixed Layer Depth (m)','FontWeight','bold')
ylabel('Mean Density (kg/m^{3})','FontWeight','bold')
set(gca,'FontSize',FontSize,'tickdir','out')