%% imported data from s0704raw.TXT
%% 
%% Saved s0704raw
load s0704raw

%% Can't check flow rate....  there is none

%% Check position data
figure; plot(s0704raw(:,3),s0704raw(:,2),'.')
%% Looks good

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

%% Saved as s0704fxt


%% 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.  

%% Manually generated CTD and SCIMS comparison from s0704ALLcrop3.xls,
%% cut and paste into ultraedit saved as m0704.txt
%% saved as m0704.mat

%% Temperature
load m0704
ts = m0704(:,9);
tc = (m0704(:,13)+ m0704(:,14))./2;
figure; hold;
plot(ts,tc,'or');
%% Derived linear regression (R2 0.9824) using cftool
% Coefficients (with 95% confidence bounds):
p1 =       1.002;  %(0.9654, 1.038)
p2 =     -0.0714;  %(-0.593, 0.4502)
y = p1.*ts + p2;
plot(ts,y,'.k');
%% Load SCIMS data
load s0704fxt
TS = s0704fxt(:,5);
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
t0704 = y;
%% Clean up
clear ans TS fx sc ss tc ts ts2 x p1 p2 goodness1 fittedmodel1 
close all

%% Regresison for Salinity
ss = m0704(:,10);
sc = (m0704(:,15) + m0704(:,16))./2;  %% Averages Salt0 and Salt1
figure; hold;
plot(ss,sc,'ob');
%% Derived linear regression (R2 .9942) using cftool
p1 =      0.9927;  %(0.9721, 1.013)
p2 =      0.2601;  %(-0.429, 0.9492)
y = p1.*ss + p2;
plot(ss,sc,'.k');
%% Load SCIMS data
load s0704fxt
SS = s0704fxt(:,6);
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
s0704 = 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
fs = m0704(:,11);
fc = m0704(:,26);
figure; hold;
plot(fs,fc,'og');
%% Derived quadratic regression (R2 0.4203)using cftool
p1 = 3.078;  %(2.072, 4.084)
p2 = -0.1511;  %(-0.4867, 0.1844)
y = p1.*fs + p2;
plot(fs,y,'.k');
%% Now apply correction to all SCIMS Fluor Voltage
load s0704fxt
FS = s0704fxt(:,7);
figure; hold;
plot(fs,fc,'og');
y = p1.*FS + p2;
plot(FS,y,'.k');
figure;hold;
plot(FS,'og')
plot(y,'.k')

%% SCIMS Fluorometry
f0704 = y;
clear ans SS fx sc ss tc ts ts2 x y p1 p2 p3 fc fs FS
close all
scims0704 = [s0704fxt t0704 s0704 f0704];

%% Scatter Plots
load scims0704;
load coast; load stacoor; % load coastline and station position
%% Subset the data
z = 1:10:length(scims0704);
data = ones(length(z),10);
for k = 1:length(z);
    data(k,:) = scims0704(z(k),:);  
end
%% Temperature
figure; grid on; box on; ylim([29 37]); xlim([-125 -117]); hold;
scatter(data(:,4),data(:,3),50,data(:,8),'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 t0704path 
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(:,8),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 t0704con

%% Salinity
figure; grid on; box on; ylim([29 37]); xlim([-125 -117]); hold;
scatter(data(:,4),data(:,3),50,data(:,9),'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 s0704path 
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 s0704con


%% Fluorescence
%% No substantial variablility
[a,b] = find (scims0704(:,10));
chl = (scims0704(a,:));
z = 1:10:length(chl);
sub = ones(length(z),10);
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(:,10),'filled'); colorbar('eastoutside')
colormapeditor % to adjust the colors all red
%print -depsc -r300 c0704path

[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(:,10),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]);
%print -depsc -r300 c0704con