% generates plots of modis and Float 0005 bb data (time and correlations)
% saves apexModis_bb for posting bb plots with chl (see plot_argosModisSeawifs_new)

close all
clear all

load 'C:\Documents and Settings\Lisa Taylor\My Documents\Data\0005\APEX\float0005.mat' % float0005 contains:
profileNum=float0005(:,1); % profile number
date=float0005(:,2); % date
lat=float0005(:,3); % latitue (degrees)
lon=float0005(:,4); % longitude (degrees)
P=float0005(:,5); % depth (dB)
theta=float0005(:,6); % potential temperature (degrees C)
S=float0005(:,7); % salinity (psu)
sigma=float0005(:,8); % sigma
B_LSS_b_corr=float0005(:,9); % bb (calibrated)
Chl_b_corr=float0005(:,10); % chlorophyll (calibrated) 
O2sat=float0005(:,11); % oxygen (% saturation) 
O2=float0005(:,12); % oxygen (um/kg) 
POC_b=float0005(:,13);
C_phy=float0005(:,14);
profile=float0005(:,15); % profile (in days from start)

% get time beginning June 17, 2004
tt=datenum(2004,6,17)+float0005(:,15);
t=168+float0005(:,15);

% get bb data between 1 and 9m
I=find(float0005(:,5)>1 & float0005(:,5)<9);
floatDate=float0005(I,2); % 216x1
floatT=float0005(I,6); % 216x1
floatS=float0005(I,7); % 216x1
floatBb=float0005(I,9); % 216x1
tt=tt(I); % 216x1

load 'C:\Documents and Settings\Lisa Taylor\My Documents\Data\0005\MODIS\MODIS_spectra_7_5km_new.mat';
% MODIS_spectra_7_5km_new.mat the following variables averaged over one day and located within 7.5km of float:
  % wl
  % mean_chl
  % median_chl
  % std_chl
  % date_modis
  % spectra_median
  % spectra_mean
  % spectra_std

Lwn=spectra_median;
F0=[172.718,187.688,194.973,185.820,186.594];
mm=length(Lwn(:,1));
Rrs=1.38*Lwn(:,1:5)./(ones(mm,1)*F0);
II=find(isfinite(Rrs(:,1).*Rrs(:,2).*Rrs(:,3).*Rrs(:,4).*Rrs(:,5)));
Rrs=Rrs(II,:);
time_MODIS=date_modis(II); % 265x1
wavelength=[412 443 488 531 551];

%user inputs:
%Temperature and salinity of water under consideration [degree C] (to compute water IOPs)
Temp=interp1(tt,floatT,time_MODIS,'linear');
Salt=interp1(tt,floatS,time_MODIS,'linear');

for i=1:length(II)
    %compute IOPs of seawater
    [a_w,bb_w]=IOP_of_sw(wavelength(1:5),Temp(i),Salt(i));
    %a_w=[0.02027,0.0111,0.01677,0.04496,0.05492];%,0.4920];
    %bb_w=[0.003242,0.002454,0.001622,0.001132,0.000989];%,0.0004262];
    rrs=Rrs./(0.52+1.7*Rrs);
    A=GSM01_invert(rrs(i,1:5),a_w,bb_w);
    chl(i)=A(1);
    a_dg(i)=A(2);
    b_bp(i)=A(3);
end

%%%%%%%%%%%%%%%%% CORRELATIONS
float_sat_median_bb=interp1(time_MODIS, b_bp, floatDate, 'linear'); % size: 216

I=finite(float_sat_median_bb); % size: 203
A1_sat=float_sat_median_bb(I);
B1_float=floatBb(I);

I2=find(A1_sat<0.06); % remove one outlier size: 202
A2=corrcoef(A1_sat(I2), B1_float(I2)); % .9017

%%%%%%%%%%%%%%%%% PLOTS
h=figure('Units','inches','Resize','off','position',[0 0 7.25 5.25],'paperposition',[0.5 3 7.25 5.25]);
subplot(1,2,2);
scatter(A1_sat(I2), B1_float(I2),'k.');
hold on;
plot([0 0.015],[0 0.015],'k','LineWidth',1)
hold off;
set(gca,'FontName','Times New Roman');
set(gca,'FontSize',10);
set(gca,'LineWidth',0.75);
set(gca,'position',[0.653 0.145 0.301 0.602]);
xlim([0 0.015]);
ylim([0 0.015]);
box on;
xlabel('b_{bp}(440), MODIS');
ylabel('b_{bp}(440), APEX');

figure(h);
subplot(1,2,1);
plot(floatDate, floatBb, 'ko');
hold on;
plot(time_MODIS,b_bp,'k*');
plot([732910 732910],[-0.002 0.014],'k','LineWidth',2)
plot([732985 732985],[-0.002 0.014],'k','LineWidth',2)
hold off
set(gca,'FontName','Times New Roman');
set(gca,'FontSize',10);
set(gca,'LineWidth',0.75);
set(gca,'position',[0.07 0.145 0.51 0.602]);
set(gca,'XTick',[732115 732480 732850 733215]);
datetick(gca,'x','mmmyy','keepticks');
axis([min(floatDate-1) max(floatDate) -0.001 0.015]);
ylabel('b_{bp}(440)');
legend('Float 0005','MODIS');
% title({'Median MODIS and Float Bb','Satellite Data Within 7.5km of Float'});

apexModis_A1_sat=A1_sat(I2);
apexModis_B1_float=B1_float(I2);

save apexModis_bb floatDate floatBb apexModis_A1_sat apexModis_B1_float time_MODIS b_bp
