% gets surface bb (between 1 and 9m) for use in inversion plots
% plots lag correlation coefficient vs. time lag (temp, sal, density, chl, and bbp)

% loads:
  % float0005.mat (generated by applyCalProfile.m)  

% saves bb_surf, time_surf, bb_surf

% written by E. Boss, UMaine, 2007
% email: emmanuel.boss@maine.edu
  
close all
clear all
  
load float0005.mat
% float0005 contains:
  % profile number, date, lat, long, P, theta, S, sigma,
  % B_LSS_b_corr, Chl_b_corr, O2sat, O2, POC_b, C_phy, and profile (in
  % days from start NOTE: oxygen sensors did not operate properly

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','position',[0 0 3.5 2.5],'paperposition',[1.5 3.5 3.5 2.5]); % figure 5
figure(h); 
% set(gcf,'DefaultAxesColorOrder',[1 0 0;1 1 0;0 1 0;0 1 1;0 0 1])
% set(gcf,'DefaultAxesColorOrder',[0 0 0; 0 0 0; 0 0 0; 0 0 0; 0 0 0]);
% set (gcf,'DefaultAxesLineStyleOrder',':|-|--|-.|+'); 
% plot([-221+1:221-1]*5,B(1,:),'-','LineWidth',2,'Color',[1 0 0]); % temperature
plot([-221+1:221-1]*5,B(1,:),'-','LineWidth',2,'Color',[1 0 0]); % temperature red
hold on
% plot([-221+1:221-1]*5,B(2,:),':','LineWidth',2,'Color',[0 0 0]); % salinity
plot([-221+1:221-1]*5,B(2,:),'-','LineWidth',2,'Color',[0 0 0]); % salinity black
% plot([-221+1:221-1]*5,B(3,:),'--','LineWidth',2,'Color',[0 1 0]); % density
plot([-221+1:221-1]*5,B(3,:),'-','LineWidth',2,'Color',[0 1 0]); % density green
% plot([-221+1:221-1]*5,B(4,:),'+','LineWidth',1,'Color',[0 1 1]); % bb
plot([-221+1:221-1]*5,B(4,:),'-','LineWidth',2,'Color',[0 1 1]); % bb cyan
% plot([-221+1:221-1]*5,B(5,:),'-.','LineWidth',2,'Color',[0 0 1]); % chl
plot([-221+1:221-1]*5,B(5,:),'-','LineWidth',2,'Color',[0 0 1]); % chl dark blue
hold off
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.78 0.84 0.1 0.1],'String','temp','EdgeColor','none','FontName','Times New Roman','FontSize',10);
annotation('textbox',[0.78 0.78 0.1 0.1],'String','sal','EdgeColor','none','FontName','Times New Roman','FontSize',10);
annotation('textbox',[0.78 0.72 0.1 0.1],'String','\sigma','EdgeColor','none','FontName','Times New Roman','FontSize',10);
annotation('textbox',[0.78 0.66 0.1 0.1],'String','chl','EdgeColor','none','FontName','Times New Roman','FontSize',10);
annotation('textbox',[0.78 0.60 0.1 0.1],'String','b_{bp}','EdgeColor','none','FontName','Times New Roman','FontSize',10);

annotation('line',[0.69 0.78],[0.880 0.880],'lineWidth',2,'LineStyle','-','Color',[1 0 0]); % temp
annotation('line',[0.69 0.78],[0.820 0.820],'lineWidth',2,'LineStyle','-','Color',[0 0 0]); % salinity
annotation('line',[0.69 0.78],[0.760 0.760],'lineWidth',2,'LineStyle','-','Color',[0 1 0]); % density
annotation('line',[0.69 0.78],[0.700 0.700],'lineWidth',2,'LineStyle','-','Color',[0 0 1]); % bb
annotation('line',[0.69 0.78],[0.640 0.640],'lineWidth',2,'LineStyle','-','Color',[0 1 1]); % chl
% annotation('textbox',[0.67 0.60 0.1 0.1],'String','+ + +','EdgeColor','none','FontName','Times New Roman','FontSize',10); % chl

save bb_surf time_surf bb_surf


