% generates plots of MODIS and Float 0005 data

close all
clear all

load 'C:\Documents and Settings\Lisa Taylor\My Documents\Data\MODIS\modis_chl'
% modis_chl contains:
  % modis_chl_ts (2042x2601) - chl data only from 06/13/04 - 01/01/07
  % modisChl [date, chl] (2042x2602)
  % distgrid (2042x2601) grid of distance around float
modis_date=modisChl(:,1);

% 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 (saves float0005)
  
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)<7); % apex depths from depth smaller than 7 but larger than 3.
apex10m(:,1)=date_apex(I10);
apex10m(:,2)=pressure_apex(I10);
apex10m(:,3)=theta_apex(I10);
apex10m(:,4)=sal_apex(I10);
apex10m(:,5)=oxygen_apex(I10);
apex10m(:,6)=chl_b_apex(I10);
apex10m(:,7)=B_LSS_b_apex(I10);
% apex10m(:,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);

%%% get mean and median MODIS values within specified distances of float by way of Emmanuel's method
[uu vv]=size(modis_chl_ts);

%%%2km of float
I2km=find(distgrid(1,:)<2); 
for i=1:uu
   dist_2km_A(i)=nanmean(modis_chl_ts(i,I2km),2);
   dist_2km_B(i)=nanmedian(modis_chl_ts(i,I2km),2);
end
dist_2km_mean=dist_2km_A';
dist_2km_median=dist_2km_B';

%%%10km of float
I10km=find(distgrid(1,:)<10); 
for i=1:uu
   dist_10km_A(i)=nanmean(modis_chl_ts(i,I10km),2);
   dist_10km_B(i)=nanmedian(modis_chl_ts(i,I10km),2);
end
dist_10km_mean=dist_10km_A';
dist_10km_median=dist_10km_B';

%%%20km of float
I20km=find(distgrid(1,:)<20); 
for i=1:uu
   dist_20km_A(i)=(nanmean(modis_chl_ts(i,I20km),2))';
   dist_20km_B(i)=(nanmedian(modis_chl_ts(i,I20km),2))';
end
dist_20km_mean=dist_20km_A';
dist_20km_median=dist_20km_B';

%%%50km of float
I50km=find(distgrid(1,:)<50);
for i=1:uu
   dist_50km_A(i)=nanmean(modis_chl_ts(i,I50km),2);
   dist_50km_B(i)=nanmedian(modis_chl_ts(i,I50km),2);
end
dist_50km_mean=dist_50km_A';
dist_50km_median=dist_50km_B';


%%%%%%%%%%%%%%%%%%%%%%%%%%%%% PLOTS
%%%%%%%%%%%%%%%%% mean and median MODIS chl vs date by way of Emmanuel's method
% remove zero's from plots
I2k=find(dist_2km_mean>0); % size: 69
I10k=find(dist_10km_mean>0); % size: 107
I20k=find(dist_20km_mean>0); % size: 144
I50k=find(dist_50km_mean>0); % size: 163

I2kk=find(dist_2km_median>0); % size: 69
I10kk=find(dist_10km_median>0); % size: 107
I20kk=find(dist_20km_median>0); % size: 144
I50kk=find(dist_50km_median>0); % size: 163

% subplot(2,2,1)
% plot(modis_date(I2k),dist_2km_mean(I2k),'.')
% datetick('x','mmmyy')
% set(gca,'ylim',[-2 18])
% ylabel('Chlorophyll')
% xlabel('Time')
% title({'Mean MODIS Chlorophyll Within 2km of Float'})
% 
% subplot(2,2,2)
% plot(modis_date(I10k),dist_10km_mean(I10k),'.')
% datetick('x','mmmyy')
% set(gca,'ylim',[-2 18])
% ylabel('Chlorophyll')
% xlabel('Time')
% title({'Mean MODIS Chlorophyll Within 10km of Float'})
% 
% subplot(2,2,3)
% plot(modis_date(I20k),dist_20km_mean(I20k),'.')
% datetick('x','mmmyy')
% set(gca,'ylim',[-2 18])
% ylabel('Chlorophyll')
% xlabel('Time')
% title({'Mean MODIS Chlorophyll Within 20km of Float'})
% 
% subplot(2,2,4)
% plot(modis_date(I50k),dist_50km_mean(I50k),'.')
% datetick('x','mmmyy')
% set(gca,'ylim',[-2 18])
% ylabel('Chlorophyll')
% xlabel('Time')
% title({'Mean MODIS Chlorophyll Within 50km of Float'})

%%%%%%%%%%%%%%%%% Mean MODIS and apex chl vs date
figure
subplot(2,2,1)
plot(apex10m(:,1), apex10m(:,6), 'b.')
hold on
plot(modis_date(I2k),dist_2km_mean(I2k),'rx')
datetick('x','mmmyy')
% set(gca,'ylim',[-2 18])
ylabel('Chlorophyll')
% xlabel('Time')
title({'Mean MODIS and Apex Chlorophyll','MODIS Data Within 2km of Float'})
legend('Float 0005','MODIS')
hold off

subplot(2,2,2)
plot(apex10m(:,1), apex10m(:,6), 'b.')
hold on
plot(modis_date(I10k),dist_10km_mean(I10k),'rx')
datetick('x','mmmyy')
% set(gca,'ylim',[-2 18])
ylabel('Chlorophyll')
% xlabel('Time')
title({'Mean MODIS and Apex Chlorophyll','MODIS Data Within 10km of Float'})

subplot(2,2,3)
plot(apex10m(:,1), apex10m(:,6), 'b.')
hold on
plot(modis_date(I20k),dist_20km_mean(I20k),'rx')
datetick('x','mmmyy')
% set(gca,'ylim',[-2 18])
ylabel('Chlorophyll')
% xlabel('Time')
title({'Mean MODIS and Apex Chlorophyll','MODIS Data Within 20km of Float'})

subplot(2,2,4)
plot(apex10m(:,1), apex10m(:,6), 'b.')
hold on
plot(modis_date(I50k),dist_50km_mean(I50k),'rx')
datetick('x','mmmyy')
% set(gca,'ylim',[-2 18])
ylabel('Chlorophyll')
% xlabel('Time')
title({'Mean MODIS and Apex Chlorophyll','MODIS Data Within 50km of Float'})

%%%%%%%%%%%%%%%%% Median MODIS and apex chl vs date
figure
subplot(2,2,1)
plot(apex10m(:,1), apex10m(:,6), 'b.')
hold on
plot(modis_date(I2kk),dist_2km_median(I2kk),'rx')
datetick('x','mmmyy')
% set(gca,'ylim',[-2 18])
ylabel('Chlorophyll')
% xlabel('Time')
title({'Median MODIS and Apex Chlorophyll','MODIS Data Within 2km of Float'})
legend('Float 0005','MODIS')
hold off

subplot(2,2,2)
plot(apex10m(:,1), apex10m(:,6), 'b.')
hold on
plot(modis_date(I10kk),dist_10km_median(I10kk),'rx')
datetick('x','mmmyy')
% set(gca,'ylim',[-2 18])
ylabel('Chlorophyll')
% xlabel('Time')
title({'Median MODIS and Apex Chlorophyll','MODIS Data Within 10km of Float'})

subplot(2,2,3)
plot(apex10m(:,1), apex10m(:,6), 'b.')
hold on
plot(modis_date(I20kk),dist_20km_median(I20kk),'rx')
datetick('x','mmmyy')
% set(gca,'ylim',[-2 18])
ylabel('Chlorophyll')
% xlabel('Time')
title({'Median MODIS and Apex Chlorophyll','MODIS Data Within 20km of Float'})

subplot(2,2,4)
plot(apex10m(:,1), apex10m(:,6), 'b.')
hold on
plot(modis_date(I50kk),dist_50km_median(I50kk),'rx')
datetick('x','mmmyy')
% set(gca,'ylim',[-2 18])
ylabel('Chlorophyll')
% xlabel('Time')
title({'Median MODIS and Apex Chlorophyll','MODIS Data Within 50km of Float'})

%%%%%%%%%%%%%%%%% CORRELATIONS
%%%%%% mean MODIS chl vs Apex chl
%%2km
float_sat_mean_chl_2k=interp1(modis_date(I2k), dist_2km_mean(I2k), apex10m(:,1), 'linear');
I=finite(float_sat_mean_chl_2k);
chl=apex10m(:,6);
A2=corrcoef(float_sat_mean_chl_2k(I), chl(I));
corrMeanLinear=A2(2,1);

%%10km
float_sat_mean_chl_10k=interp1(modis_date(I10k), dist_10km_mean(I10k), apex10m(:,1), 'linear');
I=finite(float_sat_mean_chl_10k);
chl=apex10m(:,6);
A2=corrcoef(float_sat_mean_chl_10k(I), chl(I));
corrMeanLinear=[corrMeanLinear; A2(2,1)];

%%20km
float_sat_mean_chl_20k=interp1(modis_date(I20k), dist_20km_mean(I20k), apex10m(:,1), 'linear');
I=finite(float_sat_mean_chl_20k);
chl=apex10m(:,6);
A2=corrcoef(float_sat_mean_chl_20k(I), chl(I));
corrMeanLinear=[corrMeanLinear; A2(2,1)];

%%50km
float_sat_mean_chl_50k=interp1(modis_date(I50k), dist_50km_mean(I50k), apex10m(:,1), 'linear');
I=finite(float_sat_mean_chl_50k);
chl=apex10m(:,6);
A2=corrcoef(float_sat_mean_chl_50k(I), chl(I));
corrMeanLinear=[corrMeanLinear; A2(2,1)];

figure
subplot(2,2,1)
scatter(log10(chl(I)),log10(float_sat_mean_chl_2k(I)),5,str2num(datestr(apex10m(I,1),'mm')),'filled'),colorbar
hold on
plot([-1 1], [-1,1],'--')
title({'Mean MODIS and Apex Chlorophyll','MODIS Data Within 2km of Float'})
hold off
subplot(2,2,2)
scatter(log10(chl(I)),log10(float_sat_mean_chl_10k(I)),5,str2num(datestr(apex10m(I,1),'mm')),'filled'),colorbar
hold on
plot([-1 1], [-1,1],'--')
title({'Mean MODIS and Apex Chlorophyll','MODIS Data Within 10km of Float'})
hold off
subplot(2,2,3)
scatter(log10(chl(I)),log10(float_sat_mean_chl_20k(I)),5,str2num(datestr(apex10m(I,1),'mm')),'filled'),colorbar
hold on
plot([-1 1], [-1,1],'--')
title({'Mean MODIS and Apex Chlorophyll','MODIS Data Within 20km of Float'})
hold off
subplot(2,2,4)
scatter(log10(chl(I)),log10(float_sat_mean_chl_50k(I)),5,str2num(datestr(apex10m(I,1),'mm')),'filled'),colorbar
hold on
plot([-1 1], [-1,1],'--')
title({'Mean MODIS and Apex Chlorophyll','MODIS Data Within 50km of Float'})
hold off

%%%%%% median MODIS chl vs Apex chl at surface
%%2km
float_sat_median_chl_2k=interp1(modis_date(I2kk), dist_2km_median(I2kk), apex10m(:,1), 'linear');
I=finite(float_sat_median_chl_2k);
chl=apex10m(:,6);
A1=corrcoef(float_sat_median_chl_2k(I), chl(I));
corrMedianLinear=A1(2,1);

%%10km
float_sat_median_chl_10k=interp1(modis_date(I10kk), dist_10km_median(I10kk), apex10m(:,1), 'linear');
I=finite(float_sat_median_chl_10k);
chl=apex10m(:,6);
A1=corrcoef(float_sat_median_chl_10k(I), chl(I));
corrMedianLinear=[corrMedianLinear; A1(2,1)];

%%20km
float_sat_median_chl_20k=interp1(modis_date(I20kk), dist_20km_median(I20kk), apex10m(:,1), 'linear');
I=finite(float_sat_median_chl_20k);
chl=apex10m(:,6);
A1=corrcoef(float_sat_median_chl_20k(I), chl(I));
corrMedianLinear=[corrMedianLinear; A1(2,1)];

%%50km
float_sat_median_chl_50k=interp1(modis_date(I50kk), dist_50km_median(I50kk), apex10m(:,1), 'linear');
I=finite(float_sat_median_chl_50k);
chl=apex10m(:,6);
A1=corrcoef(float_sat_median_chl_50k(I), chl(I));
corrMedianLinear=[corrMedianLinear; A1(2,1)];

figure
subplot(2,2,1)
scatter(log10(chl(I)),log10(float_sat_median_chl_2k(I)),5,str2num(datestr(apex10m(I,1),'mm')),'filled'),colorbar
hold on
plot([-1 1], [-1,1],'--')
title({'Median MODIS and Apex Chlorophyll','MODIS Data Within 2km of Float'})
hold off
subplot(2,2,2)
scatter(log10(chl(I)),log10(float_sat_median_chl_10k(I)),5,str2num(datestr(apex10m(I,1),'mm')),'filled'),colorbar
hold on
plot([-1 1], [-1,1],'--')
title({'Median MODIS and Apex Chlorophyll','MODIS Data Within 10km of Float'})
hold off
subplot(2,2,3)
scatter(log10(chl(I)),log10(float_sat_median_chl_20k(I)),5,str2num(datestr(apex10m(I,1),'mm')),'filled'),colorbar
hold on
plot([-1 1], [-1,1],'--')
title({'Median MODIS and Apex Chlorophyll','MODIS Data Within 20km of Float'})
hold off
subplot(2,2,4)
scatter(log10(chl(I)),log10(float_sat_median_chl_50k(I)),5,str2num(datestr(apex10m(I,1),'mm')),'filled'),colorbar
hold on
plot([-1 1], [-1,1],'--')
title({'Median MODIS and Apex Chlorophyll','MODIS Data Within 50km of Float'})
hold off

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

%%10km
float_sat_median_chl_10k_05m=interp1(modis_date(I10kk), dist_10km_median(I10kk), apex05m(:,1), 'linear');
I=finite(float_sat_median_chl_10k_05m);
chl=apex05m(:,6);
A1_05m=corrcoef(float_sat_median_chl_10k_05m(I), chl(I));
corrMedianLinear_05m=[corrMedianLinear_05m; A1_05m(2,1)];

%%20km
float_sat_median_chl_20k_05m=interp1(modis_date(I20kk), dist_20km_median(I20kk), apex05m(:,1), 'linear');
I=finite(float_sat_median_chl_20k_05m);
chl=apex05m(:,6);
A1_05m=corrcoef(float_sat_median_chl_20k_05m(I), chl(I));
corrMedianLinear_05m=[corrMedianLinear_05m; A1_05m(2,1)];

%%50km
float_sat_median_chl_50k_05m=interp1(modis_date(I50kk), dist_50km_median(I50kk), apex05m(:,1), 'linear');
I=finite(float_sat_median_chl_50k_05m);
chl=apex05m(:,6);
A1_05m=corrcoef(float_sat_median_chl_50k_05m(I), chl(I));
corrMedianLinear_05m=[corrMedianLinear_05m; A1_05m(2,1)];

%%%%%%%%%%%%%%% Diff plots
% plot(apex10m(:,1), apex10m(:,3), 'b.') % theta vs date at surface
% plot(apex05m(:,1), apex05m(:,4), 'b.') % salinity vs date at 3-7m
% plot(apex05m(:,1), apex05m(:,8), 'b.') % T vs date at 3-7m
% plot(apex10m(I,4), float_sat_median_chl_50k(I)-chl(I), 'b.') % diff chl vs sal at surface
% plot(apex05m(I,4), float_sat_median_chl_50k_05m(I)-chl(I), 'b.') % diff chl vs sal at 3-7m
% plot(apex05m(I,8), float_sat_median_chl_50k_05m(I)-chl(I), 'b.') % diff chl vs T at 3-7m
% plot(apex05m(I,3), float_sat_median_chl_50k_05m(I)-chl(I), 'b.') % diff chl vs theta at 3-7m

