% loads float0005_dilate and MODIS_spectra_7_5km_dilate and calculates average
   % of monthly readings, and then compares float and satellite data to check
   % for biases due to the larger volume of satellite data.

close all
clear all

load 'C:\Documents and Settings\Lisa Taylor\My Documents\Data\0005\APEX\float0005_dilate.mat'

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

% get average bb concentration per month
[yy,month,dd] = datevec(floatDate); % converts date string to date vector
mmyy = [yy month floatBb]; % finds each year (once) in dataset and stores in array uyear
uyear = unique(yy); % finds each year (once) in dataset and stores in array uyear
BB_APEX = []; % initializes array BB_APEX
for ky = 1:length(uyear) % for each year in dataset
    umonth = unique(month(yy==uyear(ky))); % finds each month (once) in each year
    for km = 1:length(umonth) % for each month in each year in each dataset
        midx = yy==uyear(ky) & month==umonth(km); % where year AND month exist (create logical array)
        Bb_midx = floatBb(midx); % create array Bb_b_midx of bb data at midx = true
        N=length(find(midx==1)); % get number of float points in the month
        thisrow = [uyear(ky) umonth(km) nan(1,2)]; % make array thisrow of yr, mo, NaNs for each floatBb
        thisrow(3) = nanmedian(Bb_midx); % add median bb concentration for month to thisrow column 3
        thisrow(4) = nanmean(Bb_midx);  % add mean bb concentration for month
        thisrow(5) = N; % add number of points to column 5
        thisrow (6) = std(Bb_midx);
        BB_APEX = [BB_APEX; thisrow]; % append new row to big array BB_APEX
    end
end

% do the same with modis data
load 'C:\Documents and Settings\Lisa Taylor\My Documents\Data\0005\MODIS\MODIS_spectra_7_5km_dilate.mat';
% MODIS_spectra_7_5km_dilate.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

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
sat_bb=A1_sat(I2); % interp_chl
float_bb=B1_float(I2); % chl_surf
sat_date=time_MODIS(I2); % modis_date time_interp
float_date=floatDate(I2); % time_surf

% scatter(sat_bb,float_bb, '.')

% get average bb concentration per month    
[yy,month,dd] = datevec(sat_date); %converts date string to date vector
mmyy = [yy month sat_bb]; %finds each year (once) in dataset and stores in array uyear
uyear = unique(yy); %finds each year (once) in dataset and stores in array uyear
BB_APEX_C = []; %initializes array BB_APEX_C
for ky = 1:length(uyear) %for each year in dataset
    umonth = unique(month(yy==uyear(ky))); %finds each month (once) in each year
    for km = 1:length(umonth) %for each month in each year in each dataset
        midx = yy==uyear(ky) & month==umonth(km); %where year AND month exist (create logical array)
        Bb_b_midx = sat_bb(midx); %create array chl_b_midx of chl data at midx = true
        N=length(find(midx==1)); % get number of satellite points in the month
        thisrow = [uyear(ky) umonth(km) nan(1,2)]; %make array thisrow of yr, mo, NaNs for each chl_surf
        thisrow(3) = nanmedian(Bb_b_midx); 
        thisrow(4) = nanmean(Bb_b_midx); 
        thisrow(5) = N; % add number of points to column 5
        BB_APEX_C = [BB_APEX_C; thisrow]; %append new row to big array CHL_APEX (see plot.osd.m for BB_APEX contents)
    end
end

A=datenum(BB_APEX(:,1),BB_APEX(:,2),15); % 
C=datenum(BB_APEX_C(:,1),BB_APEX_C(:,2),15); % 

figure(1);
subplot(2,1,1)
[AX,H1,H2] = plotyy(A,BB_APEX(:,6)./BB_APEX(:,3),C,BB_APEX_C(:,5),'plot');
set(gca,'FontName','Times New Roman');
set(gca,'FontSize',12);
set(get(AX(1),'Ylabel'),'String',{'Standard Deviation/Median' 'of Float b_b_p(440)'});
% set(get(AX(2),'Ylabel'),'String','Number of Matchups per Month');
set(AX(1),'xlim',[min(float_date-1) max(float_date)]);
set(AX(2),'xlim',[min(float_date-1) max(float_date)]);
set(AX(1),'XTick',[732115 732480 732850 733215]);
set(AX(2),'XTick',[]);
datetick(AX(1),'x', 'mmmyy','keeplimits','keepticks');
% set(0,'DefaultAxesColorOrder',[0 0 0]);
set(H1,'LineStyle','-');
set(H2,'LineStyle','-');
set(H1,'Marker','.');
set(H2,'Marker','*');
set(H1,'LineWidth',1);
set(H2,'LineWidth',1);
% title({'Standard Deviation in Float',' and MODIS Coverage vs Time'});

figure(1);
subplot(2,1,2);
plot(A,BB_APEX(:,5),'k.-',C,BB_APEX_C(:,5),'*k-','LineWidth',1); % plots number of float and satellite points read per month
set(gca,'FontName','Times New Roman');
set(gca,'FontSize',12);
set(gca,'XTick',[732115 732480 732850 733215]);
datetick(gca,'x','mmmyy','keepticks');
xlim([min(float_date-1) max(float_date)]);
% xlabel('date');
ylabel({'Number of Matchup' 'per Month'});
legend('Float','Modis');

load 'C:/Documents and Settings/Lisa Taylor/My Documents/Data/0005/ARGOS/cloudChl_dilate.mat'

figure(2);
subplot(2,2,1) % bb
[AX,H1,H2] = plotyy(A,BB_APEX(:,6)./BB_APEX(:,3),C,BB_APEX_C(:,5),'plot');
set(gca,'FontName','Times New Roman');
set(gca,'FontSize',12);
set(get(AX(1),'Ylabel'),'String',{'Standard Deviation/Median' 'of Float b_b_p(440)'});
% set(get(AX(2),'Ylabel'),'String','Number of Matchups per Month');
set(AX(1),'xlim',[min(float_date-1) max(float_date)]);
set(AX(2),'xlim',[min(float_date-1) max(float_date)]);
set(AX(1),'XTick',[732115 732480 732850 733215]);
set(AX(2),'XTick',[]);
% set(0,'DefaultAxesColorOrder',[0 0 0]);
set(H1,'LineStyle','-');
set(H2,'LineStyle','-');
set(H1,'Marker','.');
set(H2,'Marker','*');
set(H1,'LineWidth',1);
set(H2,'LineWidth',1);
datetick(AX(1),'x', 'mmmyy','keeplimits','keepticks');
set(gca,'XTick',[]);

figure(2);
subplot(2,2,2); % chl
[AX,H1,H2] = plotyy(A_chl,std_chl./med_chl,C_chl,n_chl,'plot');
set(gca,'FontName','Times New Roman');
set(gca,'FontSize',12);
set(get(AX(1),'Ylabel'),'String',{'Standard Deviation/Median' 'of Float Chlorophyll'});
% set(get(AX(2),'Ylabel'),'String','Number of Matchups per Month');
set(AX(1),'xlim',[min(float_date-1) max(float_date)]);
set(AX(2),'xlim',[min(float_date-1) max(float_date)]);
set(AX(1),'XTick',[732115 732480 732850 733215]);
set(AX(2),'XTick',[]);
% set(0,'DefaultAxesColorOrder',[0 0 0]);
set(H1,'LineStyle','-');
set(H2,'LineStyle','-');
set(H1,'Marker','.');
set(H2,'Marker','*');
set(H1,'LineWidth',1);
set(H2,'LineWidth',1);
datetick(AX(1),'x', 'mmmyy','keeplimits','keepticks');
set(gca,'XTick',[]);

figure(2);
subplot(2,2,3);
plot(A,BB_APEX(:,5),'k.-',C,BB_APEX_C(:,5),'*k-','LineWidth',1); % plots number of float and satellite points read per month
set(gca,'FontName','Times New Roman');
set(gca,'FontSize',12);
set(gca,'XTick',[732115 732480 732850 733215]);
datetick(gca,'x','mmmyy','keepticks');
xlim([min(float_date-1) max(float_date)]);
% xlabel('date');
ylabel({'Number of Matchups' 'per Month'});
legend('Float','Modis');

figure(3);
subplot(2,1,1);
plot(A,BB_APEX(:,5),'k.-',C,BB_APEX_C(:,5),'*k-','LineWidth',1); % plots number of float and satellite points read per month
set(gca,'FontName','Times New Roman');
set(gca,'FontSize',12);
set(gca,'XTick',[732115 732480 732850 733215]);
datetick(gca,'x','mmmyy','keepticks');
xlim([min(float_date-1) max(float_date)]);
set(gca,'XTick',[]);
ylabel({'Number of Matchups' 'per Month'});
legend('Float','Modis');

figure(3);
subplot(2,1,2);
[AX,H1,H2] = plotyy(A_chl,std_chl./med_chl,A,BB_APEX(:,6)./BB_APEX(:,3))
set(gca,'FontName','Times New Roman');
set(gca,'FontSize',12);
set(get(AX(1),'Ylabel'),'String',{'Standard Deviation/Median' 'of Float Chlorophyll'});
set(get(AX(2),'Ylabel'),'String',{'Standard Deviation/Median' 'of Float b_b_p(440)'});
set(AX(1),'xlim',[732099 733194]);
set(AX(2),'xlim',[732099 733194]);
set(AX(1),'XTick',[732099 732480 732850 733194]);
set(AX(2),'XTick',[]);
datetick(AX(1),'x', 'mmmyy','keeplimits','keepticks');
set(H1,'LineStyle','-');
set(H2,'LineStyle','-');
set(H1,'Marker','.');
set(H2,'Marker','*');

