%% Pulled data from s0707fxt.txt (manually joined and cropped in xls cut and
%% paste into ultraedit
%% 
%% Saved s0707fxt
load s0707fxt

%% Check flow rate
figure; hold; plot(s0707fxt(:,5));
%% Pump operating at below 1 l/m, data culled.
[i,j] = find (s0707fxt(:,5) >= 1);
s0707fxt = s0707fxt(i,:);
%% Fixt
plot(s0707fxt(:,5),'o');

%% Check position data
figure; plot(s0707fxt(:,4),s0707fxt(:,3),'.')
%% Bad lat long
% % [i,j] = find (s0707fxt(:,3) <= 50);
% % s0707fxt = s0707fxt(i,:);
%% Fixt
% % figure; plot(s0707fxt(:,4),s0707fxt(:,3),'o')

%% Check time
figure;(plot(s0707fxt(:,2)));
clear i j
close all
save s0707fxt s0707fxt

%% Import CTD and Bottle salts
%% Joined all CTDd files and culled to SCIMS variables
%% Find the surface data - because the 'shallowest' can be 1, 2 or 3m we
%% have to cull them all then remove the duplicates.
%% Did it manually.  Final file m0707.mat
%% Saved two versions in CTDu_crop .xls a)has more samples, b)just bottle samples

%% Manually generated CTD and SCIMS comparison m0707
%% Temperature
%% Used crop_a
load m0707cut
ts = m0707(:,7);
tc = (m0707(:,13)+ m0707(:,14))./2;
figure; hold;
plot(ts,tc,'or');
%% Derived linear regression (R2 0.9963) using cftool
% Coefficients (with 95% confidence bounds):
p1 = 1.008  %(0.9932, 1.023)
p2 = -0.357  %(-0.616, -0.09805)
y = p1.*ts + p2;
plot(ts,y,'.k');
%% Load SCIMS data
load s0707fxt
TS = s0707fxt(:,6);
figure; hold;
plot(ts,tc,'or');
y = p1.*TS + p2;
plot(TS,y,'.k');
%% Compare original and corrected
figure;hold;
plot(TS,'or')
plot(y,'.k')
%% SCIMS Temperature vector
t0707 = y;
%% Clean up
clear ans TS fx sc ss tc ts ts2 x p1 p2 goodness1 fittedmodel1 
close all

%% Regresison for Salinity
%% Used crop_a
ss = m0707(:,8);
sc = (m0707(:,15) + m0707(:,16))./2;  %% Averages Salt0 and Salt1
figure; hold;
plot(ss,sc,'ob');
%% Derived linear regression (R2 .9987) using cftool
p1 = 0.9893  %(0.9806, 0.998)
p2 = 0.371  %(0.08009, 0.6618)
y = p1.*ss + p2;
plot(ss,sc,'.k');
%% Load SCIMS data
load s0707fxt
SS = s0707fxt(:,7);
figure; hold;
plot(ss,sc,'ob');
y = p1.*SS + p2;
plot(SS,y,'.k');
%% Compare original and corrected
figure;hold;
plot(SS,'ob')
plot(y,'.k')
%% SCIMS Salinity vector
s0707 = y;
clear ans SS fx sc ss tc ts ts2 x p1 p2 goodness1 fittedmodel1 
close all

%% Regression for Fluorometry vs ChlA
%% ChlA data from CTDu file
%% Used crop_b

fs = m0707(:,9);
fc = m0707(:,25);
figure; hold;
plot(fs,fc,'og');
%% Derived quadratic regression (R2 0.8182)using cftool
p1 = 0.003868  %(0.0003248, 0.007411)
p2 = 0.1605  %(0.09168, 0.2294)
p3 = 0.1257  %(-0.06473, 0.3161)
y = p1.*fs.^2 + p2.*fs + p3;
plot(fs,y,'.k');
%% Now apply correction to all SCIMS Fluor Voltage
load s0707fxt
FS = s0707fxt(:,8);
figure; hold;
plot(fs,fc,'og');
y = p1.*FS.^2 + p2.*FS + p3;
plot(FS,y,'.k');
figure;hold;
plot(FS,'og')
plot(y,'.k')

%% SCIMS Fluorometry
f0707 = y;
clear ans SS fx sc ss tc ts ts2 x y p1 p2 p3 fc fs FS
close all
scims0707 = [s0707fxt t0707 s0707 f0707];

%% Scatter Plots
load scims0707;
load coast; load stacoor; % load coastline and station position
%% Subset the data
z = 1:10:length(scims0707);
data = ones(length(z),11);
for k = 1:length(z);
    data(k,:) = scims0707(z(k),:);  
end
%% Temperature
figure; grid on; box on; ylim([29 37]); xlim([-125 -117]); hold;
scatter(data(:,4),data(:,3),50,data(:,9),'filled') % plot temperature data
plot(coast(:,1),coast(:,2),'k','LineWidth', 2) % plot coastline
plot(stacoor(:,4),stacoor(:,3),'ok','MarkerSize',10) % plot station position
% set(gca,'clim', [10 26]); % set the color bar limit
colorbar('eastoutside'); % position the bar
colormapeditor; % edit the bar (1-blue(5,-4) : 32-white : 64-red(1,-2)
% print -depsc -r300 t0707path 
figure; grid on; box on; ylim([29 37]); xlim([-125 -117]); hold;
[x y] = meshgrid (min(data(:,4)):.1:max(data(:,4)), min(data(:,3)):.1:max(data(:,3)) ); %minimum value:increment:maximum value of x and y axis grid
[xi,yi,zi] = griddata(data(:,4),data(:,3),data(:,9),x,y,'linear'); % grid the data
contourf(xi,yi,zi,'LineWidth', 2); % plotting the contour
plot(coast(:,1),coast(:,2),'k','LineWidth', 2) % plot coastline
plot(stacoor(:,4),stacoor(:,3),'ok','MarkerSize',10) % plot station position
grid on; box on;
% print -depsc -r300 t0707con

%% Salinity
figure; grid on; box on; ylim([29 37]); xlim([-125 -117]); hold;
scatter(data(:,4),data(:,3),50,data(:,10),'filled') % plot salinity data
% caxis([32.4 34]); % set the color bar limit
colorbar('eastoutside');
% colorbar('YTickLabel',{'32.4','32.8','33.2','33.6','34.0'}); % position the bar
plot(coast(:,1),coast(:,2),'k','LineWidth', 2) % plot coastline
plot(stacoor(:,4),stacoor(:,3),'ok','MarkerSize',10) % plot station position
set(gca,'clim', [32.4 34]); % set the color bar limit
colormapeditor; % edit the bar (1-blue(5,-4) : 32-white : 64-purple(8,-4)
% print -depsc -r300 s0707path 
figure; grid on; box on; ylim([29 37]); xlim([-125 -117]); hold;
[x y] = meshgrid (min(data(:,4)):.1:max(data(:,4)), min(data(:,3)):.1:max(data(:,3)) ); %minimum value:increment:maximum value of x and y axis grid
[xi,yi,zi] = griddata(data(:,4),data(:,3),data(:,10),x,y,'linear'); % grid the data
contourf(xi,yi,zi,'LineWidth', 2); % plotting the contour
plot(coast(:,1),coast(:,2),'k','LineWidth', 2) % plot coastline
plot(stacoor(:,4),stacoor(:,3),'ok','MarkerSize',10) % plot station position
grid on; box on;
% print -depsc -r300 s0707con


%% Fluorescence
%% Need to take into account huge variability.  So we plot < 5 >.
[a,b] = find (scims0707(:,11) > 5);
chl = (scims0707(a,:));
z = 1:10:length(chl);
sub = ones(length(z),11);
for k = 1:length(z);
    sub(k,:) = chl(z(k),:);  
end
figure; grid on; box on; ylim([29 37]); xlim([-125 -117]); hold;
scatter(sub(:,4),sub(:,3),75,sub(:,11),'filled'); colorbar('eastoutside')
colormapeditor % to adjust the colors all red
print -depsc -r300 c0707path

[x y] = meshgrid (min(sub(:,4)):.1:max(sub(:,4)), min(sub(:,3)):.1:max(sub(:,3)) ); %minimum value:increment:maximum value of x and y axis grid
[xi,yi,zi] = griddata(sub(:,4),sub(:,3),sub(:,11),x,y,'linear');
%gridding the temperature data to the above grid.
%‘cubic’ or any other interpolation methods can be used
figure; hold;
contourf(xi,yi,zi,'LineWidth', 2); %plotting the contour
colormapeditor % to adjust the colros
plot(coast(:,1),coast(:,2),'k','LineWidth', 2); plot(stacoor(:,4),stacoor(:,3),'k*');
ylim([29 37]); xlim([-125 -117]);