%% import data from 1011NH_UnderwayCrop.txt
%% this will give you a matrix called data xrows by 6, xrows should equal
%% total rows as can be seen by opening file in Ultra edit.  In this case
%% there are 101941 rows and all imported.
%% colheaders = {LA','LO','ST','SA','FL','FI';} 
%% Rename and save this data
% uhead = colheaders;
data=importdata('1510OC_Underway_Crop_LAT_LONG_FIXED.txt')
udata = data.data;
save u1510OC udata %uhead

clear

%% Load the data
load u1510OC

%% Flow rates
%% Check the numbers, if a few are bad ignore, if there's alot remove as
%% necessary as resave
figure; plot(udata(:,8),'.')
%% Position data
%% if gps drops out/innaccurate remove bad data
figure; plot(udata(:,4),udata(:,3),'.')
%% Both check out, if bad check out 0901NH.m file for some more info

%% Check time
%% Should be uniform, could show an error, just good to check

%% Check temperature
figure; plot(udata(:,5),'.r') 

%% Check Salt
figure; plot(udata(:,6),'.b') 
%% keep them as they are legit

%% Import ...CTDvUnderway.txt only 10 columns date/time did not import
data=importdata('1510OC_CTDvsUnderway.txt')

cdata = data;

save u1510OC udata cdata
clear
load u1510OC

%% TEMPERATURE
%% Derived linear regression using Excel y = 0.9999x  + 0.4447 R² = = 0.9949
%% Swap values with the values in yymmxx_CTDvsUnderway.xlsx file
%% Enter appropriate values 
p1 = 0.9999;
p2 = 0.4447;
%% cdata : T_AVG = col3, ST = col7
plot(cdata.data(:,3),cdata.data(:,7),'or');hold
%% we apply the regression to small ST sample
y = p1.*cdata.data(:,7) + p2;
plot(cdata.data(:,7),y,'ob')
%% udata : now apply to all ST
Y = p1.*udata(:,5) + p2;
plot(udata(:,5),Y,'.k')
%% Compare original and corrected
figure;hold;
plot(udata(:,5),'or')
plot(Y,'.k')
%% Corrected Underway Temperature vector
t1510OC = Y;
%% Clean up
clear ans p1 p2 y Y i j 
close all

%% SALINITY
%% Derived linear regression using Excel y = 1.0014x - 0.1671 R² = 0.9447
%% Enter appropriate values
p1 = 1.0014;
p2 = -0.1671;
%% cdata : BTL_SALT = col5, SA = col8
plot(cdata.data(:,5),cdata.data(:,8),'or');hold
%% we apply the regression to small SA sample
y = p1.*cdata.data(:,8) + p2;
plot(cdata.data(:,8),y,'ob')
%% udata : now apply to all SA
Y = p1.*udata(:,6) + p2;
plot(udata(:,6),Y,'.k')
%% Compare original and corrected
figure;hold;
plot(udata(:,6),'or')
plot(Y,'.k')
%% Corrected Underway Salinity vector
s1510OC = Y;
%% Clean up
clear ans p1 p2 y Y i j 
close all


%% Fluorometer / ChlA
%% Data y = 0.1602x + 0.1056 R² = 0.9156
%% Enter appropriate values
p1 = 0.1602;
p2 = 0.1056;
%% cdata : BTL_CHL = col8, FL = col11
plot(cdata.data(:,6),cdata.data(:,9),'or');hold
%% we apply the regression to small FL sample
y = p1.*cdata.data(:,6) + p2;
plot(cdata.data(:,6),y,'ob')
%% udata : now apply to all FL = col7
Y = p1.*udata(:,7) + p2;
plot(udata(:,7),Y,'.k')
%% Compare original and corrected
figure;hold;
plot(udata(:,7),'or')
plot(Y,'.k')
%% Corrected Underway ChlA vector
c1510OC = Y;
%% Clean up
clear ans p1 p2 y Y i j 
close all

udata = [udata t1510OC s1510OC c1510OC];
clear t1510OC s1510OC c1510OC % dont need them anymore
save u1510OC

dlmwrite('1510OC_UnderwayFinal.txt', udata, 'delimiter', '\t','precision', '%.6f');
%%You will need to manually add the headers
%% DATE_UTC	TIME_UTC	LA	LO	ST	SA	FL	FI	CAL_T	CAL_S	CAL_C

%% Scatter Plots
load u1510OC;
load coast; load stacoor; % load coastline and station position
%% Subset the data ~ every 2.5 minutes
z = 1:10:length(udata);
data = ones(length(z),11); %% number here is the # of columns in udata it should always be 11 if you get an error here your matrix is too wide
for k = 1:length(z);
    data(k,:) = udata(z(k),:);  
end
%% Temperature
%% Path
figure; grid on; box on; ylim([29 37]); xlim([-125 -117]); hold; %% these limits may change if you include data north or south of the standard calcofi lines
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) : this is a custom function 
print -depsc -r300 t1510OCpath 
%% Contour
% 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 t0901NHcon

%% Salinity
%% Path
%% 
% [a,b] = find (udata(:,10)>32.8); %take out majority >32.8
% sal = (udata(a,:));
% z = 1:10:length(sal);
% sub = ones(length(z),11);
% for k = 1:length(z);
%     sub(k,:) = sal(z(k),:);  
% end
z = 1:10:length(udata);
sub = ones(length(z),11);
for k = 1:length(z);
    data(k,:) = udata(z(k),:);  
end
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 s1510OCpath 

% [a,b] = find (udata(:,10)<32.8); %add rainy spots
% sal = (udata(a,:));
% z = 1:10:length(sal);
% sub = ones(length(z),11);
% for k = 1:length(z);
%     sub(k,:) = sal(z(k),:);  
% end
% 
% figure; grid on; box on; ylim([29 37]); xlim([-125 -117]); hold;
% scatter(sub(:,4),sub(:,3),50,sub(:,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 s1008NHpath 


% %% Contour
% 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 s0901NHcon


%% Fluorescence
%% 
% [a,b] = find (udata(:,11)>11); need to subset
% chl = (udata(a,:));
% z = 1:10:length(chl);
% sub = ones(length(z),11);
% for k = 1:length(z);
%     sub(k,:) = chl(z(k),:);  
% end
%% didnt need to subset
z = 1:10:length(udata);
data = ones(length(z),11);
for k = 1:length(z);
    data(k,:) = udata(z(k),:);  
end
figure; grid on; box on; ylim([29 37]); xlim([-125 -117]); hold;
scatter(data(:,4),data(:,3),75,data(:,11),'filled'); colorbar('eastoutside')
caxis([0.013 0.021]);% set the color bar limit
colormapeditor % to adjust the colors 
print -depsc -r300 c1510OCpath
% 
% [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]);
% %print -depsc -r300 c0901con