% generates correlation plots of seawifs and Float 0005 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)];

% get seawifs data
load 'C:\Documents and Settings\Lisa Taylor\My Documents\Data\APEX\0015\Seawifs\seawifs_chl'
% seawifs_chl contains:
  % seawifsChl [date, chl] (1033x170)
  % seawifs_chl_ts (1033x169) just chlorophyll
  % distgrid_seawifs (1033x169) grid of distance around float
  % clat (1x1033)
  % clon (1x1033)
seawifs_date=seawifsChl(:,1); % (1033x1)
seawifs_chl=seawifsChl(:,2); % (1033x1)

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

for j=1:24
    dd=0.5+(j-1);
    I=find(distgrid_seawifs(1,:)*4<dd);
    for i=1:uu
        dist_A(i,j)=nanmean(double(seawifs_chl_ts(i,I)),2);
        dist_B(i,j)=nanmedian(double(seawifs_chl_ts(i,I)),2);
    end
end

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

% satellite at time of float
% for j=1:24
%     j % shows loop progress
%     Ikk=find(finite(dist_B(:,j))); 
%     number(j)=length(Ikk);
%     float_sat_median_chl=double(interp1(seawifs_date(Ikk), dist_B(Ikk,j), time_surf(:),'linear'));
%     I=finite(double(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:23.5],corrMedianLinear)
% subplot(1,2,2)
% plot([0.5:1:23.5],number)

% float at time of satellite
for j=1:24
    j % shows loop progress
    Ikk=find(finite(dist_B(:,j))); 
    float_sat_median_chl=double(interp1(time_surf,chl_surf,seawifs_date(Ikk),'linear')); % float at time of MODIS
    B=dist_B(Ikk,j);
    I=finite(float_sat_median_chl);
    numberLinearSeawifs(j)=length(I);
    A1=corrcoef(B(I), float_sat_median_chl(I));
    corrMedianLinearSeawifs(j)=A1(2,1);
    float_sat_median_chl=double(interp1(time_surf,chl_surf,seawifs_date(Ikk),'nearest')); % float at time of MODIS
    B=dist_B(Ikk,j);
    I=finite(float_sat_median_chl);
    numberNearestSeawifs(j)=length(I);
    A2=corrcoef(B(I), float_sat_median_chl(I));
    corrMedianNearestSeawifs(j)=A2(2,1);
end

figure
subplot(1,2,1)
plot([0.5:1:23.5],corrMedianLinearSeawifs,'b',[0.5:1:23.5],corrMedianNearestSeawifs,'k')
subplot(1,2,2)
plot([0.5:1:23.5],numberLinearSeawifs,'b',[0.5:1:23.5],numberNearestSeawifs,'k')

% clear unecessary variables
clear A1 A2 B I Ikk i j uu vv ans;
clear chl_surf clat clon dd dist_A dist_B distgrid_seawifs;
clear float0005Argos float0005SurfaceChl float_sat_median_chl;
clear seawifsChl seawifs_chl seawifs_chl_ts seawifs_date time_surf;

save corrSeawifs -v6
