% First line of the merge data looks like:
% Time(s) Pressure(db) Temperature(C) Salinity(PSU) Sigma(kgm^-3) ...
% Vchl C401.1 C405.1 C409.3 C413.5 ...
% C417.8 C422.8 C427.4 C431.8 C436.3 ...
% ...........................
% beta412 beta440 beta488 ...
% beta510 beta532 beta595 ...
% beta650 beta715 beta880 ...
% Eco_bb_100 Eco_bb_125 Eco_bb_150
% Last 12 columns
cd /Users/javier/Desktop/Javier/PHD_RIT/DMCcourse/Lab4
%% Load data from merged data
% Cast 2
filename = '/Users/javier/Desktop/Javier/PHD_RIT/DMCcourse/CruiseDataMonday/Station_1/Data_Mergedmerfile_002.mer';
import_merged_1
indexST1C2 = 30:140;
DepthST1C2 = pres(indexST1C2);
BB9ST1C2 = betadata(indexST1C2,:);
Tc_ST1C2 = temp(indexST1C2,:);
S_ST1C2 = sal(indexST1C2,:);
% Cast 3
filename = '/Users/javier/Desktop/Javier/PHD_RIT/DMCcourse/CruiseDataMonday/Station_1/Data_Mergedmerfile_003.mer';
import_merged_1
indexST1C3 = 1:123;
DepthST1C3 = pres(indexST1C3);
BB9ST1C3 = betadata(indexST1C3,:);
Tc_ST1C3 = temp(indexST1C3,:);
S_ST1C3 = sal(indexST1C3,:);
% Cast 4
filename = '/Users/javier/Desktop/Javier/PHD_RIT/DMCcourse/CruiseDataMonday/Station_1/Data_Mergedmerfile_004.mer';
import_merged_1
indexST1C4 = 1:104;
DepthST1C4 = pres(indexST1C4);
BB9ST1C4 = betadata(indexST1C4,:);
Tc_ST1C4 = temp(indexST1C4,:);
S_ST1C4 = sal(indexST1C4,:);

% Cast 6
filename = '/Users/javier/Desktop/Javier/PHD_RIT/DMCcourse/CruiseDataMonday/Station_1/Data_Mergedmerfile_006.mer';
import_merged_1
indexST1C6 = 1:127;
DepthST1C6 = pres(indexST1C6);
BB9ST1C6 = betadata(indexST1C6,:);
Tc_ST1C6 = temp(indexST1C6,:);
S_ST1C6 = sal(indexST1C6,:);

wavelength = [407,439,485,507,527,594,651,715,878];

%% Dark Count from Cruise 1 - Station 1 - Cast 5
% From cast with black tape
filename = '/Users/javier/Desktop/Javier/PHD_RIT/DMCcourse/CruiseDataMonday/Station_1/Data_Mergedmerfile_005.mer';
import_merged_1
DepthST1C5 = pres;
BB9ST1C5 = betadata;
DarkCounts = median(BB9ST1C5,1);

% ylimit = [0 70];
% xlimit = [0 100];
% 
% figure
% subplot(3,3,1)
% fs = 15;
% set(gcf,'color','white')
% plot(BB9ST1C5(:,1),DepthST1C5,'.')
% set(gca,'fontsize',fs)
% ylabel('Depth [m]')
% title('407','fontsize',fs)
% set(gca,'YDir','reverse');
% xlim(xlimit)
% ylim(ylimit)
% % legend('407','439','485','507','527','594','651','715','878');
% 
% subplot(3,3,2)
% fs = 15;
% set(gcf,'color','white')
% plot(BB9ST1C5(:,2),DepthST1C5,'.')
% set(gca,'fontsize',fs)
% title('439','fontsize',fs)
% set(gca,'YDir','reverse');
% xlim(xlimit)
% ylim(ylimit)
% 
% subplot(3,3,3)
% fs = 15;
% set(gcf,'color','white')
% plot(BB9ST1C5(:,3),DepthST1C5,'.')
% set(gca,'fontsize',fs)
% title('485','fontsize',fs)
% set(gca,'YDir','reverse');
% xlim(xlimit)
% ylim(ylimit)
% 
% subplot(3,3,4)
% fs = 15;
% set(gcf,'color','white')
% plot(BB9ST1C5(:,4),DepthST1C5,'.')
% set(gca,'fontsize',fs)
% ylabel('Depth [m]')
% title('507','fontsize',fs)
% set(gca,'YDir','reverse');
% xlim(xlimit)
% ylim(ylimit)
% 
% subplot(3,3,5)
% fs = 15;
% set(gcf,'color','white')
% plot(BB9ST1C5(:,5),DepthST1C5,'.')
% set(gca,'fontsize',fs)
% title('527','fontsize',fs)
% set(gca,'YDir','reverse');
% xlim(xlimit)
% ylim(ylimit)
% 
% subplot(3,3,6)
% fs = 15;
% set(gcf,'color','white')
% plot(BB9ST1C5(:,6),DepthST1C5,'.')
% set(gca,'fontsize',fs)
% title('594','fontsize',fs)
% set(gca,'YDir','reverse');
% xlim(xlimit)
% ylim(ylimit)
% 
% subplot(3,3,7)
% fs = 15;
% set(gcf,'color','white')
% plot(BB9ST1C5(:,7),DepthST1C5,'.')
% set(gca,'fontsize',fs)
% ylabel('Depth [m]')
% xlabel('BB9 DN')
% title('651','fontsize',fs)
% set(gca,'YDir','reverse');
% xlim(xlimit)
% ylim(ylimit)
% 
% subplot(3,3,8)
% fs = 15;
% set(gcf,'color','white')
% plot(BB9ST1C5(:,8),DepthST1C5,'.')
% set(gca,'fontsize',fs)
% xlabel('BB9 DN')
% title('715','fontsize',fs)
% set(gca,'YDir','reverse');
% xlim(xlimit)
% ylim(ylimit)
% 
% subplot(3,3,9)
% fs = 15;
% set(gcf,'color','white')
% plot(BB9ST1C5(:,9),DepthST1C5,'.')
% set(gca,'fontsize',fs)
% xlabel('BB9 DN')
% title('878','fontsize',fs)
% set(gca,'YDir','reverse');
% xlim(xlimit)
% ylim(ylimit)
%% Correct VSF for absorption along the path
% aAC = [   0.01 0.01 0.01 ...
%             0.01 0.01 0.01 ...
%             0.01 0.01 0.01]; % aborption coefficient from AC-S
%% Cast 2
% aACmatrix = repmat(aAC,size(BB9ST1C2,1),1);

filename = '/Users/javier/Desktop/Javier/PHD_RIT/DMCcourse/CruiseDataMonday/Mon_S1_Cast2_unfiltered_AllData.mat';
data = load(filename);
aACmatrixC2 = data.a_sample;
aACmatrixC2 = aACmatrixC2(indexST1C2,:);
wvlAC = data.wvl_c;

% filename = '/Users/javier/Desktop/Javier/PHD_RIT/DMCcourse/CruiseDataMonday/Mon_S1_Cast4_filtered.mat';
% data = load(filename);
% aACmatrixC4 = data.a_sample;

% Everything corrected 
[bpST1C2all,bbpST1C2all] = BB9cal(BB9ST1C2,DarkCounts,aACmatrixC2,wvlAC,wavelength,...
    Tc_ST1C2,S_ST1C2);

% No sw removal
[bpST1C2plcorr,bbpST1C2plcorr] = BB9cal(BB9ST1C2,DarkCounts,aACmatrixC2,wvlAC,wavelength);

% % No path length correction
[bpST1C2nocorr,bbpST1C2nocorr] = BB9cal(BB9ST1C2,DarkCounts);




% save('backscat.mat','bpST1C2cal','bbpST1C2','DepthST1C2')
% 
figure
fs = 15;
set(gcf,'color','white')
plot(bpST1C2all,DepthST1C2)
set(gca,'fontsize',fs)
ylabel('Depth [m]')
xlabel('VSF [1/sr/m]')
title('Station 1 - Cast 2','fontsize',fs)
set(gca,'YDir','reverse');
% xlim(xlimit)
% ylim(ylimit)
legend('407','439','485','507','527','594','651','715','878');
xlim([-2e-3 14e-3])
% Backscattering


figure
fs = 15;
set(gcf,'color','white')
plot(bbpST1C2all,DepthST1C2)
set(gca,'fontsize',fs)
ylabel('Depth [m]')
xlabel('b_{bp} [1/m]')
title('Station 1 - Cast 2','fontsize',fs)
set(gca,'YDir','reverse');
legend('407','439','485','507','527','594','651','715','878');
xlim([-0.01 0.1])
ylim([0 80])
%

figure
fs = 15;
set(gcf,'color','white')
plot(wavelength,bpST1C2all)
set(gca,'fontsize',fs)
ylabel('VSF(\theta) [1/sr/m]')
xlabel('wavelength [nm]')

% title(str,'fontsize',fs)
% legend('All corr','Path length corr','No corr');
% xlim([-0.01 0.1])
% ylim([0 80])
%%
figure
d=50;
fs = 15;
set(gcf,'color','white')
plot(wavelength,bpST1C2all(d,:))
hold on
plot(wavelength,bpST1C2plcorr(d,:),'r')
plot(wavelength,bpST1C2nocorr(d,:),'.g')
set(gca,'fontsize',fs)
ylabel('Depth [m]')
xlabel('b_{bp} [1/m]')
str = sprintf('Station 1 - Cast 2 - %2.1f m',DepthST1C2(d));
title(str,'fontsize',fs)
legend('All corr','Path length corr','No corr');


%% Plot bb for different depth with \lambda = 650 nm for comparing with ECO-VSF
[~,z10] = min(abs(DepthST1C2-10));
[~,z30] = min(abs(DepthST1C2-30));
[~,z60] = min(abs(DepthST1C2-60));
bpST1C2cal([z10 z30 z60],7)


%% Cast 3
[bpST1C3cal,bbpST1C3] = BB9cal(BB9ST1C3,DarkCounts,Tc_ST1C3,S_ST1C3);
% ylimit = [0 70];
% xlimit = [0 100];

figure
fs = 15;
set(gcf,'color','white')
plot(bpST1C3cal,DepthST1C3)
set(gca,'fontsize',fs)
ylabel('Depth [m]')
xlabel('VSF [1/sr/m]')
title('Station 1 - Cast 3','fontsize',fs)
set(gca,'YDir','reverse');
% xlim(xlimit)
% ylim(ylimit)
legend('407','439','485','507','527','594','651','715','878');
% xlim([-2e-3 14e-3])
% Backscattering


figure
fs = 15;
set(gcf,'color','white')
plot(bbpST1C3,DepthST1C3)
set(gca,'fontsize',fs)
ylabel('Depth [m]')
xlabel('b_{bp} [1/m]')
title('Station 1 - Cast 3','fontsize',fs)
set(gca,'YDir','reverse');
legend('407','439','485','507','527','594','651','715','878');
% xlim([-2e-3 14e-3])
%% Cast 4
[bpST1C4cal,bbpST1C4] = BB9cal(BB9ST1C4,DarkCounts,Tc_ST1C4,S_ST1C4);
% ylimit = [0 70];
% xlimit = [0 100];

figure
fs = 15;
set(gcf,'color','white')
plot(bpST1C4cal,DepthST1C4)
set(gca,'fontsize',fs)
ylabel('Depth [m]')
xlabel('VSF [1/sr/m]')
title('Station 1 - Cast 4','fontsize',fs)
set(gca,'YDir','reverse');
% xlim(xlimit)
% ylim(ylimit)
legend('407','439','485','507','527','594','651','715','878');
% xlim([-2e-3 14e-3])
% Backscattering


figure
fs = 15;
set(gcf,'color','white')
plot(bbpST1C4,DepthST1C4)
set(gca,'fontsize',fs)
ylabel('Depth [m]')
xlabel('b_{bp} [1/m]')
title('Station 1 - Cast 4','fontsize',fs)
set(gca,'YDir','reverse');
legend('407','439','485','507','527','594','651','715','878');
% xlim([-2e-3 14e-3])
%% Cast 6
[bpST1C6cal,bbpST1C6] = BB9cal(BB9ST1C6,DarkCounts,Tc_ST1C6,S_ST1C6);
% ylimit = [0 70];
% xlimit = [0 100];

figure
fs = 15;
set(gcf,'color','white')
plot(bpST1C6cal,DepthST1C6)
set(gca,'fontsize',fs)
ylabel('Depth [m]')
xlabel('VSF [1/sr/m]')
title('Station 1 - Cast 6','fontsize',fs)
set(gca,'YDir','reverse');
% xlim(xlimit)
% ylim(ylimit)
legend('407','439','485','507','527','594','651','715','878');
xlim([-2e-3 14e-3])
% Backscattering


figure
fs = 15;
set(gcf,'color','white')
plot(bbpST1C6,DepthST1C6)
set(gca,'fontsize',fs)
ylabel('Depth [m]')
xlabel('b_{bp} [1/m]')
title('Station 1 - Cast 6','fontsize',fs)
set(gca,'YDir','reverse');
legend('407','439','485','507','527','594','651','715','878');
% xlim([-2e-3 14e-3])
%% b_{bp} all cast
figure
fs = 15;
set(gcf,'color','white')
plot(bbpST1C2(:,7),DepthST1C2,'b')
hold on
plot(bbpST1C3(:,7),DepthST1C3,'r')
plot(bbpST1C4(:,7),DepthST1C4,'g')
plot(bbpST1C6(:,7),DepthST1C6,'m')

set(gca,'fontsize',fs)
ylabel('Depth [m]')
xlabel('b_{bp} [1/m]')
title('Station 1','fontsize',fs)
set(gca,'YDir','reverse');
legend('Cast 2','Cast 3','Cast 4','Cast 6');
xlim([-0.04 0.08])
%% Comparison BB9 and ECO-VSF, Cruise 2
% Cast 2
dataC2 = importdata('/Users/javier/Desktop/Javier/PHD_RIT/DMCcourse/CruiseDataMonday/Station_1/ECO_002_station1_072213.mat');
depth = dataC2.data(30:140,1);
ECOST2C2 = dataC2.data(30:140,5);

figure
% subplot(1,4,1)
fs = 18;
lw = 1.2;
set(gcf,'color','white')
plot(bbpST1C2(:,7),DepthST1C2,'b','linewidth',lw)
hold on
plot(ECOST2C2,depth,'r','linewidth',lw)
set(gca,'fontsize',fs)
ylabel('Depth [m]')
xlabel('b_{bp} [1/m]')
title('Cast 2','fontsize',fs)
set(gca,'YDir','reverse');
legend('BB9','ECO-VSF');
grid on
xlim([-0.01 0.1])
ylim([0 80])
%%
% Cast 3
dataC3 = importdata('/Users/javier/Desktop/Javier/PHD_RIT/DMCcourse/CruiseDataMonday/Station_1/ECO_003_station1_072213.mat');
depth = dataC3.data(1:123,1);
ECOST2C3 = dataC3.data(1:123,5);

subplot(1,4,2)
set(gcf,'color','white')
plot(bbpST1C3(:,7),DepthST1C3,'b','linewidth',lw)
hold on
plot(ECOST2C3,depth,'r','linewidth',lw)
set(gca,'fontsize',fs)
% ylabel('Depth [m]')
xlabel('b_{bp} [1/m]')
title('Cast 3','fontsize',fs)
set(gca,'YDir','reverse');
grid on
xlim([-0.01 0.1])
ylim([0 80])

% Cast 4
dataC4 = importdata('/Users/javier/Desktop/Javier/PHD_RIT/DMCcourse/CruiseDataMonday/Station_1/ECO_004_station1_072213.mat');
depth = dataC4.data(1:104,1);
ECOST2C4 = dataC4.data(1:104,5);

subplot(1,4,3)
set(gcf,'color','white')
plot(bbpST1C4(:,7),DepthST1C4,'b','linewidth',lw)
hold on
plot(ECOST2C4,depth,'r','linewidth',lw)
set(gca,'fontsize',fs)
% ylabel('Depth [m]')
xlabel('b_{bp} [1/m]')
title('Cast 4','fontsize',fs)
set(gca,'YDir','reverse');
grid on
xlim([-0.01 0.1])
ylim([0 80])

% Cast 6
dataC6 = importdata('/Users/javier/Desktop/Javier/PHD_RIT/DMCcourse/CruiseDataMonday/Station_1/ECO_006_station1_072213.mat');
depth = dataC6.data(1:127,1);
ECOST2C6 = dataC6.data(1:127,5);

subplot(1,4,4)
set(gcf,'color','white')
plot(bbpST1C6(:,7),DepthST1C6,'b','linewidth',lw)
hold on
plot(ECOST2C6,depth,'r','linewidth',lw)
set(gca,'fontsize',fs)
% ylabel('Depth [m]')
xlabel('b_{bp} [1/m]')
title('Cast 6','fontsize',fs)
set(gca,'YDir','reverse');
grid on
xlim([-0.01 0.1])
ylim([0 80])

%% Comparison BB9 and ECO-VSF, Cruise 2
% cast 68

dataC68 = importdata('/Users/javier/Desktop/Javier/PHD_RIT/DMCcourse/CruiseDataMonday/Cruise2ECO/ECO_068_station1_072413.mat');
BB9depthC68 = dataC68.values(:,1);
gd = 1:68;
BB9ST2C68 = dataC68.values(:,5);

dataC68 = importdata('/Users/javier/Desktop/Javier/PHD_RIT/DMCcourse/CruiseDataMonday/Cruise2BB9/bbp_068.mat');
ECOdepthC68 = dataC68.data(:,1);
ECOST2C68 = dataC68.data(:,8);

figure
subplot(1,3,1)
fs = 18;
lw = 1.2;
set(gcf,'color','white')
plot(ECOST2C68(gd,:),BB9depthC68(gd),'b','linewidth',lw)
hold on
plot(BB9ST2C68(gd,:),ECOdepthC68(gd),'r','linewidth',lw)
set(gca,'fontsize',fs)
ylabel('Depth [m]')
xlabel('b_{bp} [1/m]')
title('Cast 68 - Station 1','fontsize',fs)
set(gca,'YDir','reverse');
legend('BB9','ECO-VSF');
grid on
% xlim([-0.01 0.1])
% ylim([0 80])

% cast 76
dataC76 = importdata('/Users/javier/Desktop/Javier/PHD_RIT/DMCcourse/CruiseDataMonday/Cruise2ECO/ECO_076_station1_072413.mat');
BB9depthC76 = dataC76.values(:,1);
gd = 1:63;
BB9ST2C76 = dataC76.values(:,5);

dataC76 = importdata('/Users/javier/Desktop/Javier/PHD_RIT/DMCcourse/CruiseDataMonday/Cruise2BB9/bbp_076.mat');
ECOdepthC76 = dataC76.data(:,1);
ECOST2C76 = dataC76.data(:,8);


subplot(1,3,2)
fs = 18;
lw = 1.2;
set(gcf,'color','white')
plot(ECOST2C76(gd,:),BB9depthC76(gd),'b','linewidth',lw)
hold on
plot(BB9ST2C76(gd,:),ECOdepthC76(gd),'r','linewidth',lw)
set(gca,'fontsize',fs)
ylabel('Depth [m]')
xlabel('b_{bp} [1/m]')
title('Cast 76 - Station 1','fontsize',fs)
set(gca,'YDir','reverse');
% legend('BB9','ECO-VSF');
grid on
% xlim([-0.01 0.1])
% ylim([0 80])

% cast 79
dataC79 = importdata('/Users/javier/Desktop/Javier/PHD_RIT/DMCcourse/CruiseDataMonday/Cruise2ECO/ECO_079_station2_072413.mat');
BB9depthC79 = dataC79.values(:,1);
gd = 1:97;
BB9ST2C79 = dataC79.values(:,5);

dataC79 = importdata('/Users/javier/Desktop/Javier/PHD_RIT/DMCcourse/CruiseDataMonday/Cruise2BB9/bbp_079.mat');
ECOdepthC79 = dataC79.data(:,1);
ECOST2C79 = dataC79.data(:,8);


subplot(1,3,3)
fs = 18;
lw = 1.2;
set(gcf,'color','white')
plot(ECOST2C79(gd,:),BB9depthC79(gd),'b','linewidth',lw)
hold on
plot(BB9ST2C79(gd,:),ECOdepthC79(gd),'r','linewidth',lw)
set(gca,'fontsize',fs)
ylabel('Depth [m]')
xlabel('b_{bp} [1/m]')
title('Cast 79 - Station 2','fontsize',fs)
set(gca,'YDir','reverse');
% legend('BB9','ECO-VSF');
grid on
% xlim([-0.01 0.1])
% ylim([0 80])

% %%
% figure
% fs = 15;
% set(gcf,'color','white')
% plot(smooth(bbpST1C2(:,7)),DepthST1C2,'b')
% hold on
% plot(smooth(bbpST1C3(:,7)),DepthST1C3,'r')
% plot(smooth(bbpST1C4(:,7)),DepthST1C4,'g')
% plot(smooth(bbpST1C6(:,7)),DepthST1C6,'m')
% 
% set(gca,'fontsize',fs)
% ylabel('Depth [m]')
% xlabel('b_{bp} [1/m]')
% title('Station 1','fontsize',fs)
% set(gca,'YDir','reverse');
% legend('Cast 2','Cast 3','Cast 4','Cast 6');
% xlim([-0.04 0.08])
% %%
% figure
% fs = 15;
% set(gcf,'color','white')
% h=fdesign.lowpass('N,F3dB',12,0.15);
% d1 = design(h,'butter');
% bbpST1C2sm = filtfilt(d1.sosMatrix,d1.ScaleValues,bbpST1C2(:,7));
% bbpST1C3sm = filtfilt(d1.sosMatrix,d1.ScaleValues,bbpST1C3(:,7));
% bbpST1C4sm = filtfilt(d1.sosMatrix,d1.ScaleValues,bbpST1C4(:,7));
% bbpST1C6sm = filtfilt(d1.sosMatrix,d1.ScaleValues,bbpST1C6(:,7));
% 
% plot(bbpST1C2sm,DepthST1C2,'b')
% hold on
% plot(bbpST1C3sm,DepthST1C3,'r')
% plot(bbpST1C4sm,DepthST1C4,'g')
% plot(bbpST1C6sm,DepthST1C6,'m')
% 
% set(gca,'fontsize',fs)
% ylabel('Depth [m]')
% xlabel('b_{bp} [1/m]')
% title('Station 1','fontsize',fs)
% set(gca,'YDir','reverse');
% legend('Cast 2','Cast 3','Cast 4','Cast 6');
% xlim([-0.04 0.08])
% 
% 
