% generates correlation plots of modis and Float 0005 chl and number of point matchups by
  % satellite at time of float and
  % float at time of satellite
  
% loads corrSeawifs and generates same plots above with addition of seawifs correlations
  
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 float0015Surface)
  
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)];

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 06/13/04 - 01/01/07
  % 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

for j=1:25
    j
    dd=0.5+(j-1);
    I=find(distgrid(1,:)<dd);
    for i=1:uu
        dist_A(i,j)=nanmean(modis_chl_ts(i,I),2);
        dist_B(i,j)=nanmedian(modis_chl_ts(i,I),2);
    end
end

%%%%%%%%%%%%%%%%% CORRELATIONS
%%%%%% median Modis chl vs Apex chl at surface

% satellite at time of float
% for j=1:24
%     Ikk=find(finite(dist_B(:,j))); 
%     number(j)=length(Ikk);
%     float_sat_median_chl=interp1(modis_date(Ikk), dist_B(Ikk,j), time_surf(:),'linear');
%     I=finite(float_sat_median_chl);
%     A1=corrcoef(chl_surf(I), float_sat_median_chl(I));
%     corrMedianLinear(j)=A1(2,1);
% end
% 
% subplot(1,2,1)
% plot([0.5:1:24.5],corrMedianLinear)
% subplot(1,2,2)
% plot([0.5:1:24.5],number)

% float at time of satellite
for j=1:25
    j % shows loop progress
    Ikk=find(finite(dist_B(:,j))); 
    float_sat_median_chl=interp1(time_surf,chl_surf,modis_date(Ikk),'linear'); % float at time of MODIS
    B=dist_B(Ikk,j);
    I=finite(float_sat_median_chl);
    numberLinearModis(j)=length(I);
    A1=corrcoef(B(I), float_sat_median_chl(I));
    corrMedianLinearModis(j)=A1(2,1);
    float_sat_median_chl=interp1(time_surf,chl_surf,modis_date(Ikk),'nearest'); % float at time of MODIS
    B=dist_B(Ikk,j);
    I=finite(float_sat_median_chl);
    numberNearestModis(j)=length(I);
    A2=corrcoef(B(I), float_sat_median_chl(I));
    corrMedianNearestModis(j)=A2(2,1);
end

figure
subplot(1,2,1)
plot([0.5:1:24.5],corrMedianLinearModis,'b',[0.5:1:24.5],corrMedianNearestModis,'k')
subplot(1,2,2)
plot([0.5:1:24.5],numberLinearModis,'b',[0.5:1:24.5],numberNearestModis,'k')

% clear unecessary variables
clear A1 A2 B I Ikk i j uu vv ans;
clear chl_surf dd dist_A dist_B distgrid;
clear float0005Argos float0005SurfaceChl float_sat_median_chl;
clear modisChl modis_chl modis_chl_ts modis_date time_surf;

save corrModis -v6

% compare with Seawifs
load corrSeawifs
% corrSeawifs contains:
  % corrMedianLinearSeawifs
  % corrMedianNearestSeawifs
  % numberLinearSeawifs
  % numberNearestSeawifs
  
figure
subplot(1,2,1)
plot([0.5:1:24.5],corrMedianLinearModis,'b',[0.5:1:24.5],corrMedianNearestModis,'k')
hold on
plot([0.5:1:23.5],corrMedianLinearSeawifs,'r',[0.5:1:23.5],corrMedianNearestSeawifs,'g')
legend('Modis Linear','Modis Nearest','Seawifs Linear','Seawifs Nearest')
title({'Correlation of Median MODIS/Seawifs Data','with Float Surface Chlorophyll','Float at Time of Satellites'})
xlabel('Distance from Float (km)')
hold off
subplot(1,2,2)
plot([0.5:1:24.5],numberLinearModis,'b',[0.5:1:24.5],numberNearestModis,'k')  
hold on
plot([0.5:1:23.5],numberLinearSeawifs,'r',[0.5:1:23.5],numberNearestSeawifs,'g')
title({'Median MODIS and Seawifs Correlated with Float Surface Chlorophyll'})
hold off
legend('Modis Linear','Modis Nearest','Seawifs Linear','Seawifs Nearest')
title({'Correlation of Number of MatchUps for','Median MODIS/Seawifs Data with',' Float Surface Chlorophyll','Float at Time of Satellites'})

