% gets surface bb (between 1 and 9m) for use in inversion plots
% loads float0005.mat which contains:
  % float0005, containing:
    % profile, date, lat, lon, P, theta, S, sigma, B_LSS_b_corr, Chl_b_corr, O2sat, and O2
  % tt

close all
clear all
  
load float0005.mat

I=find(float0005(:,5)>1 & float0005(:,5)<9);
bb_surf=float0005(I,9); % data points: 199
T_surf=float0005(I,6); % data points: 199
S_surf=float0005(I,7); % data points: 199
Sigma_surf=float0005(I,8); % data points: 199
chl_surf=float0005(I,10); % data points: 199
time_surf=tt(I);

% h=figure('Units','inches','Resize','off'); % figure 5
% figure(h);
% set(gcf,'paperposition',[1.5 3.5 3.5 2.5]); % sets location and size on print out
% set(gcf,'position',[0 0 3.5 2.5]); % sets window location and size on figure
% set(gca,'FontName','Times New Roman');
% set(gca,'FontSize',10);
% set(gca,'LineWidth',0.75);
% set(gca,'XTick',[732099 732464 732829 733194 733621]);
% datetick(gca,'x','mmmyy','keepticks');
% title({'Backscatter 1-9m','Float 0005, North Atlantic'});
% ylabel('LSS_Sig');
% xlabel('time [month]');

clear I  float0005;

A=ones(5,221);
time=unique(tt);
j=1;
for i=1:221
    B=find(time_surf==time(i));
    if isfinite(B)==1
        A(:,i)=[T_surf(B), S_surf(B), Sigma_surf(B), bb_surf(B), chl_surf(B)]';
        j=B;
    else
        A(:,i)=0.5*([T_surf(j-1), S_surf(j-1), Sigma_surf(j-1), bb_surf(j-1), chl_surf(j-1)]'+[T_surf(j+1), S_surf(j+1), Sigma_surf(j+1), bb_surf(j+1), chl_surf(j+1)]');
    end
end
    
for i=1:5
    A(i,:)=A(i,:)-mean(A(i,:));
end

B=ones(5,441);
for i=1:5
    A(i,:)=A(i,:)-mean(A(i,:));
    B(i,:)=xcorr(A(i,:),'coeff');
end

h=figure('Units','inches','Resize','off'); % figure 5
set(0,'DefaultAxesLineStyleOrder','-o|:|--+|-*|-.') % temp, sal, sigma, bb, chl
set(gcf,'DefaultAxesColorOrder',[0 0 0;0 0 0;0 0 0;0 0 0;0 0 0])
figure(h);
set(gcf,'paperposition',[1.5 3.5 3.5 2.5]); % sets location and size on print out
set(gcf,'position',[0 0 3.5 2.5]); % sets window location and size on figure
plot([-221+1:221-1]*5,B,'LineWidth',2)
axis([0 100 0 1])
set(gca,'FontName','Times New Roman');
set(gca,'FontSize',10);
set(gca,'LineWidth',0.75);
xlabel('time lag (day)');
ylabel('lag correlation coefficient');
% legend('Temperature','Salinity','Density','Backscattering','chlorophyll','Location','NorthEast');
% draw legend
annotation('textbox',[0.77 0.84 0.1 0.1],'String','temp','EdgeColor','none','FontName','Times New Roman','FontSize',10);
annotation('textbox',[0.77 0.78 0.1 0.1],'String','sal','EdgeColor','none','FontName','Times New Roman','FontSize',10);
annotation('textbox',[0.77 0.72 0.1 0.1],'String','sigma','EdgeColor','none','FontName','Times New Roman','FontSize',10);
annotation('textbox',[0.77 0.66 0.1 0.1],'String','chl','EdgeColor','none','FontName','Times New Roman','FontSize',10);
annotation('textbox',[0.77 0.60 0.1 0.1],'String','b_{bp}','EdgeColor','none','FontName','Times New Roman','FontSize',10);

annotation('textbox',[0.63 0.835 0.1 0.1],'String','o','EdgeColor','none','FontName','Times New Roman','FontSize',10);
annotation('line',[0.68 0.77],[0.880 0.880],'lineWidth',2,'LineStyle','-'); % temp

annotation('line',[0.66 0.77],[0.820 0.820],'lineWidth',2,'LineStyle',':'); % sal

annotation('textbox',[0.63 0.72 0.1 0.1],'String','+','EdgeColor','none','FontName','Times New Roman','FontSize',10); % sigma +
annotation('line',[0.68 0.77],[0.760 0.760],'lineWidth',2,'LineStyle','--'); % sigma

annotation('line',[0.66 0.77],[0.700 0.700],'lineWidth',2,'LineStyle','-.'); % chl

annotation('textbox',[0.63 0.59 0.1 0.1],'String','*','EdgeColor','none','FontName','Times New Roman','FontSize',10); % bb *
annotation('line',[0.68 0.77],[0.640 0.640],'lineWidth',2,'LineStyle','-'); % bb

save bb_surf time_surf bb_surf


