% (once) run C:\Documents and Settings\Lisa Taylor\My Documents\Data\COASOSU\chl_night.m
  % to get chl_night.mat (Strutton Night Chl)
% (once) run C:\Documents and Settings\Lisa Taylor\My Documents\Data\COASOSU\compile-noref.m
  % to get ps-compile.mat (Strutton data in addition to chl)
% (daily) run plot_argos_data.m to get min_argos.mat (Float 0005 surface data)

close all
clear all

%% ARGOS DATA, FLOAT 0005
load 'C:\Documents and Settings\Lisa Taylor\My Documents\Data\ARGOS\0005\min_argos'
% min_argos.mat contains:
  % dat(689x10)
  % P (pressure)
  % FRef (F_Ref)
  % Chl (F_Sig or blue sig)
  % LSSRef (LSS_Ref)
  % LSSSig (LSS_Sig, or blue sig) = backscattering
  % stat
  % lat
  % lon
  % date
  % date2
  % B_LSS_b, which is (LSSSig-51.8)*0.00004637
  % cp_b, which is B_LSS_b*63
  % Chl_b, which is (Chl-25)*0.0107 was 29, 25 is mean of counts at >950m
  % min_argos_chl (:,1), chl at 1st day's reading (not calibrated)
  % min_argos_backscat (:,1) backscat at 1st day's reading (not calibrated) 
  % min_argos_date (:,1) date/time of 1st day's reading
junkChl=min_argos_chl(:,:);
junkB=min_argos_backscat(:,:);
firstArgosChl=(junkChl-25)*0.01079772;
firstArgosBack=(junkB-51.8)*0.00004637;
firstArgosDate=min_argos_date(:,:);

%% STRUTTON CHL NIGHT ONLY
load 'C:\Documents and Settings\Lisa Taylor\My Documents\Data\COASOSU\chl_night'
% chl_night.mat contains:
  % date_night at chlNight(:,1) (needs correction, see below)
  % chl_2m_night at chlNight(:,2) 
  % chl_5m_night at chlNight(:,3)
  % chl_10m_night at chlNight(:,4)
  % chl_15m_night at chlNight(:,5)
  % chl_20m_night at chlNight(:,6)
time_strut=datenum(2004,1,1)+chlNight(:,1)-1; % Strutton date changed to 2004

%% STRUTTON DATA (other than chl night)
load 'C:\Documents and Settings\Lisa Taylor\My Documents\Data\COASOSU\ps-compile'
% ps-compile.mat contains:
  % PsCompile (6977x38), containing:
  % 1 date_pscompile (all arrays 6977x1)
  % 2 exceldate_pscompile
  % 3 year_day_pscompile
  % 4 lon_pscompile
  % 5 lat_pscompile
  % 6 flr_2m_pscompile
  % 7 chl_2m_pscompile
  % 8 flr_10m_pscompile
  % 9 chl_10m_pscompile
  % 10 flr_15m_pscompile
  % 11 chl_15m_pscompile
  % 12 b100_2m_pscompile
  % 13 b125_2m_pscompile
  % 14 b150_2m_pscompile
  % 15 b_back_2m_pscompile
  % 16 b100_10m_pscompile
  % 17 b125_10m_pscompile
  % 18 b150_10m_pscompile
  % 19 b_back_10m_pscompile
  % 20 b100_15m_pscompile
  % 21 b125_15m_pscompile
  % 22 b150_15m_pscompile
  % 23 b_back_15m_pscompile
  % 24 par_1m_pscompile
  % 25 unknown_1m_pscompile
  % 26 par_5m_pscompile
  % 27 flr_5m_pscompile
  % 28 par_10m_pscompile
  % 29 c_10m_pscompile
  % 30 par_20m_pscompile
  % 31 flr_20m_pscompile
  % 32 par_35m_pscompile
  % 33 c_35m_pscompile
  % 34 poc_bb_2m_pscompile
  % 35 poc_bb_10m_pscompile
  % 36 poc_beamc_10m_pscompile
  % 37 poc_bb_15m_pscompile
  % 38 poc_beamc_35m_pscompile
  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% CHLOROPHYLL PROGRAMS  
% match Argos data to nearest Strutton data
firstArgosChl(isnan(firstArgosChl)) = []; % initialize argos first chl array
firstArgosDate(isnan(firstArgosDate)) = []; % initialize argos first date array
chl_2m_night(isnan(chl_2m_night)) = []; % initialize strutton 2m array
chl_10m_night(isnan(chl_10m_night)) = []; % initialize strutton 10m array
chl_15m_night(isnan(chl_15m_night)) = []; % initialize strutton 15m array
time_strut(isnan(time_strut)) = []; % initialize strutton date array
strut_chl_2m_night_AR = interp1(time_strut,chl_2m_night,firstArgosDate, 'nearest');
strut_chl_10m_night_AR = interp1(time_strut,chl_10m_night,firstArgosDate, 'nearest');
strut_chl_15m_night_AR = interp1(time_strut,chl_15m_night,firstArgosDate, 'nearest');
  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% BACKSCATTER PROGRAMS  
% match Strutton data to within 24 hours of Argos
strutbin_mean = nan(size(dat,1),size(PsCompile,2)); % initialize meaned array (entire data set) to size you want
strutbin_std = nan(size(dat,1),size(PsCompile,2)); % initialize std deviation arrray
hours12=datenum(0,0,0,12,0,0); % set range of hours to include in meaned values
for ka=1:size(dat,1)
    thisargotime=dat(ka,9);
    idx=find((date_pscompile>=(thisargotime-hours12)) & (date_pscompile<=thisargotime+hours12));
    strutbin_mean(ka,:)=mean(PsCompile(idx,:));
    strutbin_std(ka,:)=std(PsCompile(idx,:));
end
strutbin_mean_bback_2m=strutbin_mean(:,15);
strutbin_mean_bback_10m=strutbin_mean(:,19);
strutbin_mean_bback_15m=strutbin_mean(:,23);

% put all data into one array for regression analysis
% MUST ADD ADDITIONAL DATA BEYOND 67 WHEN RUNNING NEW DATA!
strutbin_2m=[strutbin_mean_bback_2m(1:67)];
strutbin_2m(:,2)=2;
strutbin_10m=[strutbin_mean_bback_10m(1:67)];
strutbin_10m(:,2)=10;
strutbin_15m=[strutbin_mean_bback_15m(1:67)];
strutbin_15m(:,2)=15;
blssb=[B_LSS_b(1:67)];
blssb(:,2)=0;
x=dat(:,9);
y=[x(1:67)];
z1=[strutbin_2m,y];
z2=[strutbin_10m,y];
z3=[strutbin_15m,y];
z4=[blssb,y];
allBBackDepths24=[z1;z2;z3;z4];

% match Argos data to nearest Strutton data
b_back_2m_pscompile(isnan(b_back_2m_pscompile)) = []; % initialize strutton b2m array
b_back_10m_pscompile(isnan(b_back_10m_pscompile)) = []; % initialize strutton b10m array
b_back_15m_pscompile(isnan(b_back_15m_pscompile)) = []; % initialize strutton b15m array
date_pscompile(isnan(date_pscompile)) = []; % initialize strutton date array
date(isnan(date)) = []; % initialize argos b array

strut_back2m_AR = interp1(date_pscompile,b_back_2m_pscompile,date([1:131,133:end]), 'nearest');
strut_back10m_AR = interp1(date_pscompile,b_back_10m_pscompile,date([1:131,133:end]), 'nearest');
strut_back15m_AR = interp1(date_pscompile,b_back_15m_pscompile,date([1:131,133:end]), 'nearest');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% CHLOROPHYLL PLOTS
%%%%%%%%%%%%%%%%% All Strutton chl 2m night and first Argos measurement vs date
figure 
plot(firstArgosDate,firstArgosChl,'.') % firstArgosChl is 1x142
hold on
plot(time_strut,chl_2m_night,'mx')
set(gca,'xlim',[datenum('12 jun 2004') datenum('06 sep 2004')])
datetick('x', 'mmmyy','keeplimits')
xlabel('Date')
ylabel('Chlorophyll [mg/m3]')
title({'COASOSU and ARGOS Chlorophyll','Night Data Only'})
legend('Float 0005','Chl 2m from Strutton')
hold off

%%%%%%%%%%%%%%%%% AllStrutton chl 10m night and first Argos measurement vs date
figure 
plot(firstArgosDate,firstArgosChl,'.')
hold on
plot(time_strut,chl_10m_night,'mx')
set(gca,'xlim',[datenum('12 jun 2004') datenum('06 sep 2004')])
datetick('x', 'mmmyy','keeplimits')
xlabel('Date')
ylabel('Chlorophyll [mg/m3]')
title({'COASOSU and ARGOS Chlorophyll','Night Data Only'})
legend('Float 0005','Chl 10m from Strutton')
hold off

%%%%%%%%%%%%%%%%% All Strutton chl 10m night and first Argos measurement vs date
figure 
plot(firstArgosDate,firstArgosChl,'.')
hold on
plot(time_strut,chl_15m_night,'mx')
set(gca,'xlim',[datenum('12 jun 2004') datenum('06 sep 2004')])
datetick('x', 'mmmyy','keeplimits')
xlabel('Date')
ylabel('Chlorophyll [mg/m3]')
title({'COASOSU and ARGOS Chlorophyll','Night Data Only'})
legend('Float 0005','Chl 15m from Strutton')
hold off

%%%%%%%%%%%%%%%%% NEAREST Strutton and Argos chl correlations
figure
subplot(2,2,1)
plot(strut_chl_2m_night_AR,firstArgosChl,'.')
set(gca,'xlim',[0 12])
xlabel('Argos Chlorophyll (First Measurement of the Day)')
ylabel('COASOSU Chlorophyll 2m (Night Data)')
title({'COASOSU vs ARGOS Chlorophyll [mg/m3]'})
subplot(2,2,2)
plot(strut_chl_10m_night_AR,firstArgosChl,'.')
set(gca,'xlim',[0 12])
xlabel('Argos Chlorophyll (First Measurement of the Day)')
ylabel('COASOSU Chlorophyll 10m (Night Data)')
title({'COASOSU vs ARGOS Chlorophyll [mg/m3]'})
subplot(2,2,3)
plot(strut_chl_15m_night_AR,firstArgosChl,'.')
set(gca,'xlim',[0 12])
xlabel('Argos Chlorophyll (First Measurement of the Day)')
ylabel('COASOSU Chlorophyll 15m (Night Data)')
title({'COASOSU vs ARGOS Chlorophyll [mg/m3]'})

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% BACKSCATTER PLOTS
%%%%%%%%%%%%%%%%% Strutton bback2m vs date
figure
plot(date_pscompile,b_back_2m_pscompile,'.')
xlabel('Date')
ylabel('b_back_2m')
datetick('x','mmmyy')
title({'COASOSU Backscatter'})

%%%%%%%%%%%%%%%%% Strutton bback10m vs date
figure
plot(date_pscompile,b_back_10m_pscompile,'.')
xlabel('Date')
ylabel('b_back_10m')
datetick('x','mmmyy')
title({'COASOSU Backscatter'})

%%%%%%%%%%%%%%%%% Strutton bback15m vs date
figure
plot(date_pscompile,b_back_15m_pscompile,'.')
xlabel('Date')
ylabel('b_back_15m')
datetick('x','mmmyy')
title({'COASOSU Backscatter'})

%%%%%%%%%%%%%%%%% Strutton bback2m and all Argos vs date
figure 
plot(date_pscompile,b_back_2m_pscompile,'.')
hold on
plot(date,B_LSS_b,'mx')
set(gca,'xlim',[datenum('12 jun 2004') datenum('06 sep 2004')])
datetick('x', 'mmmyy','keeplimits')
xlabel('Date')
ylabel('Backscatter')
title({'Backscatter','COASOSU (bback2m) and Float 0005 B LSS b'})
legend('Strutton','Float 0005')
hold off

%%%%%%%%%%%%%%%%% Strutton bback10m and all Argos vs date
figure 
plot(date_pscompile,b_back_10m_pscompile,'.')
hold on
plot(date,B_LSS_b,'mx')
set(gca,'xlim',[datenum('12 jun 2004') datenum('06 sep 2004')])
datetick('x', 'mmmyy','keeplimits')
xlabel('Date')
ylabel('Backscatter')
title({'Backscatter','COASOSU (bback10m) and Float 0005 B LSS b'})
legend('Strutton','Float 0005')
hold off

%%%%%%%%%%%%%%%%% NEAREST Strutton and Argos backscatter correlations
figure
subplot(2,2,1)
plot(strut_back2m_AR,B_LSS_b([1:131,133:end]),'.')
set(gca,'ylim',[0 0.05])
title({'COASOSU bback2m and ARGOS B LSS b'})
subplot(2,2,2)
plot(strut_back10m_AR,B_LSS_b([1:131,133:end]),'.')
set(gca,'ylim',[0 0.05])
title({'COASOSU bback10m and ARGOS B LSS b'})
subplot(2,2,3)
plot(strut_back15m_AR,B_LSS_b([1:131,133:end]),'.')
set(gca,'ylim',[0 0.05])
title({'COASOSU bback15m and ARGOS B LSS b'})

%%%%%%%%%%%%%%%%% 24 HRS Strutton backscatter and all Argos data vs date
%% bback 2m
figure
hold on
plot(dat(:,9),B_LSS_b, 'b.')
plot(dat(:,9),strutbin_mean_bback_2m, 'r.')
datetick('x','mmmyy')
xlabel('Date')
ylabel('Backscatter')
title({'COASOSU & ARGOS Backscatter'})
legend('Float 0005 B LSS b','COASOSU bback2m')
hold off

%% bback 10m
figure
hold on
plot(dat(:,9),B_LSS_b, 'b.')
plot(dat(:,9),strutbin_mean_bback_10m, 'r.')
datetick('x','mmmyy')
xlabel('Date')
ylabel('Backscatter')
title({'COASOSU & ARGOS Backscatter'})
legend('Float 0005 B LSS b','COASOSU bback10m')
hold off

%% bback 15m
figure
hold on
plot(dat(:,9),B_LSS_b, 'b.')
plot(dat(:,9),strutbin_mean_bback_15m, 'r.')
datetick('x','mmmyy')
xlabel('Date')
ylabel('Backscatter')
title({'COASOSU & ARGOS Backscatter'})
legend('Float 0005 B LSS b','COASOSU bback15m')
hold off

%%%%%%%%%%%%%%%%% 24 HRS Strutton and Argos backscatter correlations
figure % all depths, all correlation plots (adjust 67 for new Argos data!)
subplot(2,2,1)
plot(strutbin_mean_bback_2m(1:67,:),B_LSS_b(1:67,:), 'r.')
xlabel('COASOSU bback2m')
ylabel('ARGOS B LSS B')
title({'Mean COASOSU Data Within 24 Hours of ARGOS B LSS b'})
subplot(2,2,2)
plot(strutbin_mean_bback_10m(1:67,:),B_LSS_b(1:67,:), 'r.')
xlabel('COASOSU bback10m')
ylabel('ARGOS B LSS B')
title({'Mean COASOSU Data Within 24 Hours of ARGOS B LSS b'})
subplot(2,2,3)
plot(strutbin_mean_bback_15m(1:67,:),B_LSS_b(1:67,:), 'r.')
xlabel('COASOSU bback15m')
ylabel('ARGOS B LSS B')
title({'Mean COASOSU Data Within 24 Hours of ARGOS B LSS b'})
subplot(2,2,4)
hold on
plot(strutbin_mean_bback_2m(1:67,:),B_LSS_b(1:67,:), 'r.')
plot(strutbin_mean_bback_10m(1:67,:),B_LSS_b(1:67,:), 'b.')
plot(strutbin_mean_bback_15m(1:67,:),B_LSS_b(1:67,:), 'g.')
xlabel('COASOSU')
ylabel('ARGOS B LSS B')
title({'Mean COASOSU Data Within 24 Hours of ARGOS B LSS b'})
legend('2m','10m','15m')
hold off

%%%%%%%%%%%%%%%%% NEAREST Strutton and Argos backscatter correlation coefficients
% Bb=B_LSS_b([1:131,133:end]);
% I=find(isfinite(strut_back2m_AR));
% corrcoef(strut_back2m_AR(I),Bb(I)) % 0.8852
% polyfit(strut_back2m_AR(I),Bb(I),1) % 4, 0.0047
% I=find(isfinite(strut_back10m_AR));
% corrcoef(strut_back10m_AR(I),Bb(I)) % 0.9007
% polyfit(strut_back10m_AR(I),Bb(I),1) % 3.4227, -0.0038
% I=find(isfinite(strut_back15m_AR));
% corrcoef(strut_back15m_AR(I),Bb(I)) % 0.8679
% polyfit(strut_back15m_AR(I),Bb(I),1) % 3.5248, 0.0057