% plots modis and Float 0005 bb data (time and correlations)

% loads:
  % float0005 (generated by applyCalProfile.m)
  % MODIS_spectra_7_5km.mat (generated by gen_modis_spectra.m)

% saves apexModis_bb for posting bb plots with chl (see plot_msgModis)

% written by E. Boss and L. Taylor, UMaine, 2007
% email: emmanuel.boss@maine.edu

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); %latitude (degrees)
lon=float0005(:,4); %longitude (degrees)
P=float0005(:,5); %depth (m)
theta=float0005(:,6); %potential temperature (degrees C)
S=float0005(:,7); %salinity (psu)
sigma=float0005(:,8); %density-1000 (kg/m3)
B_LSS_b_corr=float0005(:,9); %bb (m-1, calibrated using MODIS data downloaded 01/11/08)
Chl_b_corr=float0005(:,10); %chlorophyll (mg/m3, calibrated using MODIS data downloaded 01/11/08) 
O2sat=float0005(:,11); %oxygen (% saturation) !oxygen sensor not working
O2=float0005(:,12); %oxygen (um/kg) !oxygen sensor not working
POC_b=float0005(:,13);
C_phy=float0005(:,14);
profile=float0005(:,15); % (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);
floatT=float0005(I,6);
floatS=float0005(I,7);
floatBb=float0005(I,9);
tt=tt(I);

load 'C:\Documents and Settings\Lisa Taylor\My Documents\Data\0005\MODIS\MODIS_spectra_7_5km.mat';
% MODIS_spectra_7_5km.mat contains the following variables within 7.5km of the 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');

I=finite(float_sat_median_bb);
A1_sat=float_sat_median_bb(I);
B1_float=floatBb(I);

I2=find(A1_sat<0.06);
A2=corrcoef(A1_sat(I2), B1_float(I2));

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