% generates plots of MODIS and Float 0005 data

close all
clear all

% run gen_apex_data to get apex data (saves apex_data.mat)
% run float0005_data to consolidate apex data from gen_apex_data and apply
  % strutton calibrations to chl data (none to bb) (saves float0005)

load 'C:\Documents and Settings\Lisa Taylor\My Documents\Data\MODIS\modis_bb'
% modis_bb contains:
 % bb_10km_551 (1816x1) bb data at 10km around float
 % bbp_10km_440 (1816x1) bbp data at 10km around float
 % datenumdate (1816x1)

modis_date = datenumdate(:,1);
modis_10km_bb_551 = bb_10km_551(:,1);
modis_10km_bbp_440 = bbp_10km_440(:,1);

load 'C:\Documents and Settings\Lisa Taylor\My Documents\Data\Apex\0005\float0005'
% float0005 contains:
 
profile_apex=float0005(:,1);
date_apex=float0005(:,2); % 8879x1
lat_apex=float0005(:,3);
lon_apex=float0005(:,4);
pressure_apex=float0005(:,5);
theta_apex=float0005(:,6);
sal_apex=float0005(:,7);
sigma_apex=float0005(:,8);
B_LSS_b_apex=float0005(:,9);
chl_b_apex=float0005(:,10);
oxygenSat_apex=float0005(:,11);
oxygen_apex=float0005(:,12);
% T_apex=float0005(:,13); % added to 0005_data.m and then removed

%%%%%%%%%%%%%%%%%%%%%%%%%%%%% PROGRAMS  
%%%%%%%%%% set apex depth range (surface values)
% apex depths from 5 to 11m
% I10=find(float0005(:,5)<11.1 & float0005(:,5)>4.9); % apex depths from 5 to 11m
I10=find(float0005(:,5)<4.9); % apex depths from depth smaller than 5
I5=find(float0005(:,5)>3 & float0005(:,5)<10); % apex depths from depth smaller than 10 but larger than 3.

apex0m(:,1)=date_apex(I10);
apex0m(:,2)=pressure_apex(I10);
apex0m(:,3)=theta_apex(I10);
apex0m(:,4)=sal_apex(I10);
apex0m(:,5)=oxygen_apex(I10);
apex0m(:,6)=chl_b_apex(I10);
apex0m(:,7)=B_LSS_b_apex(I10);
% apex0m(:,8)=T_apex(I10);

apex05m(:,1)=date_apex(I5);
apex05m(:,2)=pressure_apex(I5);
apex05m(:,3)=theta_apex(I5);
apex05m(:,4)=sal_apex(I5);
apex05m(:,5)=oxygen_apex(I5);
apex05m(:,6)=chl_b_apex(I5);
apex05m(:,7)=B_LSS_b_apex(I5);
% apex05m(:,8)=T_apex(I5);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%% PLOTS
%%%%%%%%%%%%%%%%% median MODIS bb at surface vs date

figure
% subplot(2,2,1)
% plot(modis_date,modis_2km_bb,'.')
% datetick('x','mmmyy')
% % set(gca,'ylim',[-2 18])
% ylabel('bb')
% xlabel('Time')
% title({'Median MODIS bb Within 10km of Float'})

% subplot(2,2,2)
plot(modis_date,modis_10km_bb_551,'.')
datetick('x','mmmyy')
% set(gca,'ylim',[-2 18])
ylabel('bb')
xlabel('Time')
title({'Mean MODIS bb Within 10km of Float'})

% subplot(2,2,3)
% plot(modis_date,modis_20km_bb,'.')
% datetick('x','mmmyy')
% % set(gca,'ylim',[-2 18])
% ylabel('bb')
% xlabel('Time')
% title({'Mean MODIS bb Within 20km of Float'})

% subplot(2,2,4)
% plot(modis_date,modis_50km_bb,'.')
% datetick('x','mmmyy')
% % set(gca,'ylim',[-2 18])
% ylabel('bb')
% xlabel('Time')
% title({'Mean MODIS bb Within 50km of Float'})

%%%%%%%%%%%%%%%%% Median MODIS and apex bb vs date at surface
figure
% % subplot(2,2,1)
% plot(apex0m(:,1), apex0m(:,7), 'b.')
% hold on
% plot(modis_date,modis_2km_bb,'rx')
% datetick('x','mmmyy')
% % set(gca,'ylim',[-2 18])
% ylabel('bb')
% % xlabel('Time')
% title({'Median MODIS and Apex bb (<4.9m)','MODIS Data Within 2km of Float'})
% legend('Float 0005','MODIS')
% hold off

% subplot(2,2,2)
plot(apex0m(:,1), apex0m(:,7), 'b.')
hold on
plot(modis_date,modis_10km_bb_551,'rx')
datetick('x','mmmyy')
% set(gca,'ylim',[-2 18])
ylabel('bb')
% xlabel('Time')
title({'Median MODIS and Apex bb (<4.9m)','MODIS Data Within 10km of Float'})
legend('Float 0005','MODIS')

% % subplot(2,2,3)
% plot(apex0m(:,1), apex0m(:,7), 'b.')
% hold on
% plot(modis_date,modis_20km_bb,'rx')
% datetick('x','mmmyy')
% % set(gca,'ylim',[-2 18])
% ylabel('bb')
% % xlabel('Time')
% title({'Median MODIS and Apex bb (<4.9m)','MODIS Data Within 20km of Float'})
 
% % subplot(2,2,4)
% plot(apex0m(:,1), apex0m(:,7), 'b.')
% hold on
% plot(modis_date,modis_50km_bb,'rx')
% datetick('x','mmmyy')
% % set(gca,'ylim',[-2 18])
% ylabel('bb')
% % xlabel('Time')
% title({'Median MODIS and Apex bb (<4.9m)','MODIS Data Within 50km of Float'})

%%%%%%%%%%%%%%%%% Median MODIS and apex bb vs date at 3-10m depth
% figure
% % subplot(2,2,1)
% plot(apex05m(:,1), apex05m(:,7), 'b.')
% hold on
% plot(modis_date,modis_2km_bb,'rx')
% datetick('x','mmmyy')
% % set(gca,'ylim',[-2 18])
% ylabel('bb')
% % xlabel('Time')
% title({'Median MODIS and Apex bb (3-10m)','MODIS Data Within 2km of Float'})
% legend('Float 0005','MODIS')
% hold off

% subplot(2,2,2)
figure
plot(apex05m(:,1), apex05m(:,7), 'b.')
hold on
plot(modis_date,modis_10km_bb_551,'rx')
datetick('x','mmmyy')
% set(gca,'ylim',[-2 18])
ylabel('bb_551')
% xlabel('Time')
title({'Median MODIS and Apex bb (3-10m)','MODIS Data Within 10km of Float'})
legend('Float 0005','MODIS')

figure
plot(apex05m(:,1), apex05m(:,7), 'b.')
hold on
plot(modis_date,modis_10km_bbp_440,'rx')
datetick('x','mmmyy')
% set(gca,'ylim',[-2 18])
ylabel('bbp_440')
% xlabel('Time')
title({'Median MODIS and Apex bb (3-10m)','MODIS Data Within 10km of Float'})
legend('Float 0005','MODIS')

% % subplot(2,2,3)
% plot(apex05m(:,1), apex05m(:,7), 'b.')
% hold on
% plot(modis_date,modis_20km_bb,'rx')
% datetick('x','mmmyy')
% % set(gca,'ylim',[-2 18])
% ylabel('bb')
% % xlabel('Time')
% title({'Median MODIS and Apex bb (3-10m)','MODIS Data Within 20km of Float'})
 
% % subplot(2,2,4)
% plot(apex05m(:,1), apex05m(:,7), 'b.')
% hold on
% plot(modis_date,modis_50km_bb,'rx')
% datetick('x','mmmyy')
% % set(gca,'ylim',[-2 18])
% ylabel('bb')
% % xlabel('Time')
% title({'Median MODIS and Apex bb (3-10m)','MODIS Data Within 50km of Float'})

%%%%%%%%%%%%%%%%% CORRELATIONS
%%%%%% median MODIS chl vs Apex bb at surface (apex0m)
%%2km
% I2kk=find(modis_2km_bb>0); % size: 69
% float_sat_median_bb_2k=interp1(modis_date(I2kk), modis_2km_bb(I2kk), apex0m(:,1), 'linear');
% I=finite(float_sat_median_bb_2k);
% bb=apex0m(:,7);
% A1=corrcoef(float_sat_median_bb_2k(I), bb(I));
% corrMedianLinear=A1(2,1);

%%10km
I10kk=find(modis_10km_bb_551>0); % size: 224
float_sat_median_bb_10k=interp1(modis_date(I10kk), modis_10km_bb_551(I10kk), apex0m(:,1), 'linear');
I=finite(float_sat_median_bb_10k);
bb=apex0m(:,7);
A1=corrcoef(float_sat_median_bb_10k(I), bb(I));
corrMedianLinear=A1(2,1);
%corrMedianLinear=[corrMedianLinear; A1(2,1)];

%%20km
% I20kk=find(modis_20km_bb>0); % size: 144
% float_sat_median_bb_20k=interp1(modis_date(I20kk), modis_20km_bb(I20kk), apex0m(:,1), 'linear');
% I=finite(float_sat_median_bb_20k);
% bb=apex0m(:,7);
% A1=corrcoef(float_sat_median_bb_20k(I), bb(I));
% corrMedianLinear=[corrMedianLinear; A1(2,1)];

%%50km
% I50kk=find(modis_50km_bb>0); % size: 163
% float_sat_median_bb_50k=interp1(modis_date(I50kk), modis_50km_bb(I50kk), apex0m(:,1), 'linear');
% I=finite(float_sat_median_bb_50k);
% bb=apex0m(:,7);
% A1=corrcoef(float_sat_median_bb_50k(I), bb(I));
% corrMedianLinear=[corrMedianLinear; A1(2,1)];

figure
% subplot(2,2,1)
% scatter(log10(bb(I)),log10(float_sat_median_bb_2k(I)),5,str2num(datestr(apex0m(I,1),'mm')),'filled'),colorbar
% hold on
% plot([-1 1], [-1,1],'--')
% title({'Median MODIS and Apex bb (surface data)','MODIS Data Within 2km of Float'})
% hold off
% subplot(2,2,2)
scatter(log10(bb(I)),log10(float_sat_median_bb_10k(I)),5,str2num(datestr(apex0m(I,1),'mm')),'filled'),colorbar
hold on
% plot([-1 1], [-1,1],'--')
title({'Median MODIS and Apex bb (surface data)','MODIS Data Within 10km of Float'})
hold off
% subplot(2,2,3)
% scatter(log10(bb(I)),log10(float_sat_median_bb_20k(I)),5,str2num(datestr(apex0m(I,1),'mm')),'filled'),colorbar
% hold on
% plot([-1 1], [-1,1],'--')
% title({'Median MODIS and Apex bb (surface data)','MODIS Data Within 20km of Float'})
% hold off
% subplot(2,2,4)
% scatter(log10(bb(I)),log10(float_sat_median_bb_50k(I)),5,str2num(datestr(apex0m(I,1),'mm')),'filled'),colorbar
% hold on
% plot([-1 1], [-1,1],'--')
% title({'Median MODIS and Apex bb (surface data)','MODIS Data Within 50km of Float'})
% hold off

%%%%%% median MODIS chl vs Apex chl at 3-10m
%%2km
% float_sat_median_bb_2k_05m=interp1(modis_date(I2kk), modis_2km_bb(I2kk), apex05m(:,1), 'linear');
% I=finite(float_sat_median_bb_2k_05m);
% bb=apex05m(:,7);
% A1_05m=corrcoef(float_sat_median_bb_2k_05m(I), bb(I));
% corrMedianLinear_05m=A1_05m(2,1);

%%10km
float_sat_median_bb_10k_05m=interp1(modis_date(I10kk), modis_10km_bb_551(I10kk), apex05m(:,1), 'linear');
I=finite(float_sat_median_bb_10k_05m);
bb=apex05m(:,7);
A1_05m=corrcoef(float_sat_median_bb_10k_05m(I), bb(I));
corrMedianLinear_05m=A1_05m(2,1);
% corrMedianLinear_05m=[corrMedianLinear_05m; A1_05m(2,1)];

%%20km
% float_sat_median_bb_20k_05m=interp1(modis_date(I20kk), modis_20km_bb(I20kk), apex05m(:,1), 'linear');
% I=finite(float_sat_median_bb_20k_05m);
% bb=apex05m(:,7);
% A1_05m=corrcoef(float_sat_median_bb_20k_05m(I), bb(I));
% corrMedianLinear_05m=[corrMedianLinear_05m; A1_05m(2,1)];

%%50km
% float_sat_median_bb_50k_05m=interp1(modis_date(I50kk), modis_50km_bb(I50kk), apex05m(:,1), 'linear');
% I=finite(float_sat_median_bb_50k_05m);
% bb=apex05m(:,7);
% A1_05m=corrcoef(float_sat_median_bb_50k_05m(I), bb(I));
% corrMedianLinear_05m=[corrMedianLinear_05m; A1_05m(2,1)];

figure
scatter(log10(bb(I)),log10(float_sat_median_bb_10k(I)),5,str2num(datestr(apex05m(I,1),'mm')),'filled'),colorbar
title({'Median MODIS and Apex bb (3-10m)','MODIS Data Within 10km of Float'})

%%%%%%%%%%%%%%%%% plot chl vs bb

scatter(apex05m(:,6),apex05m(:,7))
ylabel('Chlorophyll')
xlabel('bb')
title({'Apex chl vs bb (3-10m)'})

%%%%%%%%%%%%%%%%% CORRELATION COEFFICIENTS
corrcoef((isfinite(float_sat_median_bb_10k_05m)),apex05m(:,7)) 

