%% imported data from s0804raw.txt
%% 
%% Saved s0804raw
load s0804raw

%% Flow rates
figure; plot(s0804raw(:,3))
%% Are all good

%% Check position data
%% There is NO position data
%% Pulled lat long from the event log (e0804raw)which has date and time also.
%% How to merge the two?
%%

%% Merging event and scs 

load e0804raw %% Serial date/time, lat, long
load s0804serraw %% raw2 has the serial date/time in first column

e = e0804raw; clear e0804raw %% SerialDateTime Lat Long
s = s0804serraw; clear s0804serraw %% Serialdatetime temp sal
%% need to trim each in order to coorelate
[i,j] = find(e(:,1) >= s(1,1));
e2 = e(i,:);
e = e2;
[i,j] = find(s(:,1) <= e(end,1));
s2 = s(i,:);
s = s2;
clear i j e2 s2 ans

%% In order to interpolate, one must remove duplicate times.
t = e(:,1);
[a,b,c] = unique(t);
t = t(b);
lat = e(b,2);
long = e(b,3); clear a b c
x  = s(1,1):0.000143:e(end,1);  %% changed increment to get closest to number of scs samples
nlat = (interp1(t,lat,x,'linear'))';
nlong = (interp1(t,long,x,'linear'))';
figure; plot(t,lat,'o',x,nlat,'.')
figure; plot(t,long,'o',x,nlong,'.')
slat = nlat(1:length(s));
slong = nlong(1:length(s));
s = [s(:,1) slat slong s(:,2) s(:,3)]; 
s0804fxt = s;
save s0804fxt s0804fxt

clear t x slat slong nlat nlong e s lat long

close all

%% 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 s0804ALLcrop3.xls,
%% cut and paste into ultraedit saved as m0804.txt
%% saved as m0804.mat

%% Temperature
load m0804
ts = m0804(:,1);
tc = m0804(:,7);
figure; hold;
plot(ts,tc,'or');
%% Derived linear regression (R2 0.9824) using cftool
% Coefficients (with 95% confidence bounds):
p1 =      0.9738;  %(0.9552, 0.9924)
p2 =       0.279;  %(0.03015, 0.5278)
y = p1.*ts + p2;
plot(ts,y,'.k');
%% Load SCIMS data
load s0804fxt
TS = s0804fxt(:,4);
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
t0804 = y;
%% Clean up
clear ans TS fx sc ss tc ts ts2 x p1 p2 y goodness1 fittedmodel1 
close all

%% Regresison for Salinity
ss = m0804(:,2);
sc = (m0804(:,8));
figure; hold;
plot(ss,sc,'ob');
%% Derived linear regression (R2 .9942) using cftool
p1 =      0.9933;  %%(0.9638, 1.023)
p2 =      0.2474;  %%(-0.7375, 1.232)
y = p1.*ss + p2;
plot(ss,sc,'.k');
%% Load SCIMS data
load s0804fxt
SS = s0804fxt(:,5);
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
s0804 = y;
clear ans SS fx sc ss tc ts ts2 x p1 p2 y goodness1 fittedmodel1 
close all

%% No flurometry data

%% Regression for Fluorometry vs ChlA
%% ChlA data from CTDu file
% fs = m0804(:,11);
% fc = m0804(:,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 s0804fxt
% FS = s0804fxt(:,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
% f0804 = y;
% clear ans SS fx sc ss tc ts ts2 x y p1 p2 p3 fc fs FS
% close all
scims0804 = [s0804fxt t0804 s0804];

%% Scatter Plots
load scims0804;
load coast; load stacoor; % load coastline and station position
%% Subset the data
z = 1:10:length(scims0804);
data = ones(length(z),7);
for k = 1:length(z);
    data(k,:) = scims0804(z(k),:);  
end
%% Temperature
figure; grid on; box on; ylim([29 37]); xlim([-125 -117]); hold;
scatter(data(:,3),data(:,2),50,data(:,6),'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 t0804path 
figure; grid on; box on; ylim([29 37]); xlim([-125 -117]); hold;
[x y] = meshgrid (min(data(:,3)):.1:max(data(:,3)), min(data(:,2)):.1:max(data(:,2)) ); %minimum value:increment:maximum value of x and y axis grid
[xi,yi,zi] = griddata(data(:,3),data(:,2),data(:,6),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 t0804con

%% Salinity
figure; grid on; box on; ylim([29 37]); xlim([-125 -117]); hold;
scatter(data(:,3),data(:,2),50,data(:,7),'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 s0804path 
figure; grid on; box on; ylim([29 37]); xlim([-125 -117]); hold;
[x y] = meshgrid (min(data(:,3)):.1:max(data(:,3)), min(data(:,2)):.1:max(data(:,2)) ); %minimum value:increment:maximum value of x and y axis grid
[xi,yi,zi] = griddata(data(:,3),data(:,2),data(:,7),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 s0804con


%% Fluorescence
%% No substantial variablility
[a,b] = find (scims0804(:,10));
chl = (scims0804(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 c0804path

[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 c0804con




