%% Imported 0711SCIMSSRAW.xls
%% Saved as 0711SCIMSRAW.mat
load s0711raw

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

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

%% Check time
figure;(plot(s0711raw(:,2)));
%% Someone set the clock sback for DST... FUCKER!

clear i j
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.  Final file m0711.mat

load m0711  %% Manually generated CTD and SCIMS comparison
%% Temperature
ts = m0711(:,8);
tc = (m0711(:,14)+m0711(:,15))./2;
figure; hold;
plot(ts,tc,'or');
%% Derived linear regression (R2 .9953) using cftool
% Coefficients (with 95% confidence bounds):
p1 = 1.036;  %(1.017, 1.055)
p2 = -0.7485;  %(-1.054, -0.4425)
y = p1.*ts + p2;
plot(ts,y,'.k');
%% Load SCIMS data
load s0711fxt
TS = s0711fxt(:,6);
figure; hold;
plot(ts,tc,'or');
%% Applying correction
p1 = 1.036;  %(1.017, 1.055)
p2 = -0.7485;  %(-1.054, -0.4425)
y = p1.*TS + p2;
plot(TS,y,'.k');
%% Compare original and corrected
figure;hold;
plot(TS,'or')
plot(y,'.k')
%% SCIMS Temperature vector
t0711 = y;
%% Clean up
clear ans TS fx sc ss tc ts ts2 x
close all

%% Regresison for Salinity
ss = m0711(:,9);
sc = (m0711(:,16)+m0711(:,17))./2;  %% Averages Salt0 and Salt1
figure; hold;
plot(ss,sc,'ob');
%% Derived linear regression (R2 .996) using cftool
p1 = 0.9885;  %%(0.9718, 1.005)
p2 = 0.4486;  %%(-0.1073, 1.005)
y = p1.*ss + p2;
plot(ss,sc,'.k');
%% Load SCIMS data
load s0711fxt
SS = s0711fxt(:,7);
figure; hold;
plot(ss,sc,'ob');
%% Applying correction
p1 = 0.9885;  %%(0.9718, 1.005)
p2 = 0.4486;  %%(-0.1073, 1.005)
y = p1.*SS + p2;
plot(SS,y,'.k');
%% Compare original and corrected
figure;hold;
plot(SS,'ob')
plot(y,'.k')
%% SCIMS Salinity vector
s0711 = y;
clear ans SS fx sc ss tc ts ts2 x
close all

%% Regression for Fluorometry
%% Manually entered ChlA data from \Summary\0711Chl001-067.out

fs = m0711(:,10);
fc = m0711(:,19);
figure; hold;
plot(fs,fc,'og');
%% Derived linear regression (R2 ) using cftool
% p1 = 0.3166;  %%(0.2651, 0.368)
% p2 = -0.1497;  %%(-0.3663, 0.06691)
% y = p1.*fs + p2;

p1 =      0.0221;  %%(0.01066, 0.03354)
p2 =     0.07722;  %%(-0.05503, 0.2095)
p3 =      0.2182;  %%(-0.05423, 0.4906)
y = p1.*fs.^2 + p2.*fs + p3;

plot(fs,y,'.k');
%% Now apply correction to all SCIMS Fluor Voltage
load s0711fxt
FS = s0711fxt(:,8);
figure; hold;
plot(fs,fc,'og');

% p1 = 0.3166;  %%(0.2651, 0.368)
% p2 = -0.1497;  %%(-0.3663, 0.06691)
% y = p1.*FS + p2;

p1 =      0.0221;  %%(0.01066, 0.03354)
p2 =     0.07722;  %%(-0.05503, 0.2095)
p3 =      0.2182;  %%(-0.05423, 0.4906)
y = p1.*FS.^2 + p2.*FS + p3;

plot(FS,y,'.k');

figure;hold;
plot(FS,'og')
plot(y,'.k')

%% SCIMS Fluorometry
f0711 = y;
clear ans SS fx sc ss tc ts ts2 x y p1 p2 p3 fc fs FS
close all
scims0711 = [s0711fxt t0711 s0711 f0711];


%% Scatter Plots
data = scims0711;
z = 1:10:length(data);
cdata = ones(length(z),11);
for k = 1:length(z);
    cdata(k,:) = data(z(k),:);  
end

clear k z

load coast
load stacoor

figure; hold;
scatter(cdata(:,4),cdata(:,3),50,cdata(:,9),'filled')
plot(coast(:,1),coast(:,2),'k','LineWidth', 2)
colormap('jet')
colorbar('eastoutside')
ylim([29 37]);
xlim([-125 -117]);
title('0711NH - Surface Temperature (\circC)','fontsize',16,'fontname','kosMin-Bold')
xlabel('longitude','fontsize',14,'fontname','kosMin-Bold')
set(gca,'fontsize',12,'fontname','kosMin-Bold')
ylabel('latitude','fontsize',14,'fontname','kosMin-Bold')
plot(stacoor(:,4),stacoor(:,3),'ok','MarkerSize',10)
grid on
box on

figure; hold;
scatter(cdata(:,4),cdata(:,3),50,cdata(:,10),'filled')
plot(coast(:,1),coast(:,2),'k','LineWidth', 2)
colormap('winter')
colorbar('eastoutside')
ylim([29 37]);
xlim([-125 -117]);
title('0711NH - Surface Salinity (PSU)','fontsize',16,'fontname','kosMin-Bold')
xlabel('longitude','fontsize',14,'fontname','kosMin-Bold')
set(gca,'fontsize',12,'fontname','kosMin-Bold')
ylabel('latitude','fontsize',14,'fontname','kosMin-Bold')
plot(stacoor(:,4),stacoor(:,3),'ok','MarkerSize',10)
grid on
box on

figure; hold;
cdata = sortrows(cdata,-11);
% scatter(cdata(:,4),cdata(:,3),50,cdata(:,11),'filled')
[i,j] = find (cdata(:,11) >= 5);
scatter(cdata(i,4),cdata(i,3),50,cdata(i,11),'filled')
plot(coast(:,1),coast(:,2),'k','LineWidth', 2)
colormap('jet')
colorbar('eastoutside')
ylim([29 37]);
xlim([-125 -117]);
title('0711NH - Surface Fluorescence (Volts)','fontsize',16,'fontname','kosMin-Bold')
xlabel('longitude','fontsize',14,'fontname','kosMin-Bold')
set(gca,'fontsize',12,'fontname','kosMin-Bold')
ylabel('latitude','fontsize',14,'fontname','kosMin-Bold')
plot(stacoor(:,4),stacoor(:,3),'ok','MarkerSize',10)
grid on
box on
%% colormapsNc %Custom Colorbar
%% clear tx tit1 special shp sc prompt nx ninc nin nfin nc n lines j index i def cns alfain S C1 B A


%% Contours
load scims0711
load coast
load stacoor
z = 1:10:length(scims0711);
sub = ones(length(z),11);
for k = 1:length(z);
    sub(k,:) = scims0711(z(k),:);  
end
clear k z
[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(:,9),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
scatter(sub(:,4),sub(:,3),75,sub(:,9),'filled'); colorbar('eastoutside')
plot(coast(:,1),coast(:,2),'k','LineWidth', 2); plot(stacoor(:,4),stacoor(:,3),'k*');
ylim([29 37]); xlim([-125 -117]);


%% Fluorescence
[a,b] = find (scims0711(:,11) > 5);
chl = (scims0711(a,:));
z = 1:10:length(chl);
sub = ones(length(z),11);
for k = 1:length(z);
    sub(k,:) = chl(z(k),:);  
end
clear k z
[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
scatter(sub(:,4),sub(:,3),75,sub(:,11),'filled'); colorbar('eastoutside')
plot(coast(:,1),coast(:,2),'k','LineWidth', 2); plot(stacoor(:,4),stacoor(:,3),'k*');
ylim([29 37]); xlim([-125 -117]);