% generates plots of seawifs, modis, and Float 0005 data
% correlates data

close all
clear all

% run gen_msg_data to get apex msg (saves apex_msg.mat)
% run plot_argos_data to consolidate apex msg data and apply
  % strutton calibrations (saves float0005Surface)
  
load float0015Surface
% float0015Surface contains:
  % float0015SurfaceChl: [date, chl_corr]

% get first reading of the day
I=find(diff(float0015SurfaceChl(:,1))>4);
time_surf=[float0015SurfaceChl(1,1);float0015SurfaceChl(I+1,1)];
chl_surf=[float0015SurfaceChl(1,2);float0015SurfaceChl(I+1,2)];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% MODIS data
% get modis data
load 'C:\Documents and Settings\Lisa Taylor\My Documents\Data\APEX\0015\MODIS\modis_chl'
% modis_chl contains:
  % modis_chl_ts (1105x2601) - chl data only from 08/20/05 - 04/09/07 THIS IS A 51X51 GRID
  % modisChl0015 [date, chl] (1105x2602)
  % distgrid (1105x2601) grid of distance around float
modis_date=modisChl0015(:,1);

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

%%%7.5km of float
I7km_modis=find(distgrid(1,:)<7.5); 
for i=1:uu
   dist_7km_A_modis(i)=nanmean(modis_chl_ts(i,I7km_modis),2);
   dist_7km_B_modis(i)=nanmedian(modis_chl_ts(i,I7km_modis),2);
end

dist_7km_mean_modis=dist_7km_A_modis';
dist_7km_median_modis=dist_7km_B_modis';

% remove zero's from plots
I7k_modis=find(dist_7km_mean_modis>0); % size:
I7kk_modis=find(dist_7km_median_modis>0); % size:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% SeaWIFS 5.1 data
load 'C:\Documents and Settings\Lisa Taylor\My Documents\Data\APEX\0015\Seawifs\seawifs_chl'
% seawifs_chl contains was generated from gen_seawifs_data.m:
  % seawifsChl [date, chl] (1033x170)
  % seawifs_chl_ts (10336x169) just chlorophyll THIS IS A 13X13 GRID
  % distgrid_seawifs (1033x169) grid of distance around float
  % clat (1x1033)
  % clon (1x1033)
seawifs_date=seawifsChl(:,1); % (1033x1)
seawifs_chl=seawifsChl(:,2);

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

%%%7km of float
I7km_seawifs=find(distgrid_seawifs(1,:)<7.5); 
for i=1:uu
   dist_7km_A_seawifs(i)=nanmean(seawifs_chl_ts(i,I7km_seawifs),2);
   dist_7km_B_seawifs(i)=nanmedian(seawifs_chl_ts(i,I7km_seawifs),2);
end
dist_7km_mean_seawifs=dist_7km_A_seawifs';
dist_7km_median_seawifs=dist_7km_B_seawifs';

% remove zero's from plots
I7k_seawifs=find(dist_7km_mean_seawifs>0); % size: 259
I7kk_seawifs=find(dist_7km_median_seawifs>0); % size: 259

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% PLOTS
figure(1);
plot(time_surf, chl_surf, 'b.');
hold on;
plot(modis_date(I7kk_modis),dist_7km_median_modis(I7kk_modis),'g*');
plot(seawifs_date(I7kk_seawifs),dist_7km_median_seawifs(I7kk_seawifs),'r*');
hold off;
set(gca,'FontName','Times New Roman');
set(gca,'FontSize',16);
set(gca,'XTick',[732464 732829 733194 733560]);
datetick(gca,'x','mmmyy','keepticks');
ylabel('Chlorophyll [mg ^{-3}]');
title({'Median SeaWIFS, MODIS, and Float Surface Chlorophyll','Satellite Data Within 7.5km of Float'});
legend('Float 0005','MODIS','SeaWIFS');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% MODIS/FLOAT CORRELATIONS
I10kk=find(dist_7km_median_modis>0); % size: 193

%%%%%% median MODIS chl vs Apex chl at surface
float_sat_median_chl_7k=interp1(modis_date(I10kk), dist_7km_median_modis(I10kk), time_surf(:,1), 'linear');
I=finite(float_sat_median_chl_7k);
chl=chl_surf(:,1);
A1=corrcoef(float_sat_median_chl_7k(I), chl(I));
corrMedianLinear=A1(2,1);

figure(2)
% scatter(log10(chl(I)),log10(float_sat_median_chl_7k(I)),5,str2num(datestr(time_surf(I,1),'mm')),'filled'),colorbar
scatter(chl(I),float_sat_median_chl_7k(I),5,str2num(datestr(time_surf(I,1),'mm')),'filled'),colorbar
hold on
% plot([-1 1], [-1,1],'--')
title({'Median MODIS and Float Surface Chlorophyll','MODIS Data Within 7.5km of Float'})
hold off

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% SEAWIFS/FLOAT CORRELATIONS
I10kk_seawifs=find(dist_7km_median_seawifs>0); % size: 

%%%%%% median Seawifs chl vs Apex chl at surface
float_sat_median_chl_7k_seawifs=double(interp1(seawifs_date(I10kk_seawifs), dist_7km_median_seawifs(I10kk_seawifs), time_surf(:,1), 'linear'));
II=finite(double(float_sat_median_chl_7k_seawifs));
chl=chl_surf(:,1);
A2=corrcoef(float_sat_median_chl_7k_seawifs(II), chl(II));
corrMedianLinear=A2(2,1);

figure(3)
% scatter(log10(chl(I)),log10(float_sat_median_chl_7k(I)),5,str2num(datestr(time_surf(I,1),'mm')),'filled'),colorbar
scatter(chl(I),float_sat_median_chl_7k_seawifs(I),5,str2num(datestr(time_surf(I,1),'mm')),'filled'),colorbar
hold on
% plot([-1 1], [-1,1],'--')
title({'Median SeaWIFS and Float Surface Chlorophyll','SeaWIFS Data Within 7.5km of Float'})
hold off
