% generates MODIS Lnw spectra for 10km around the float's position:
close all
clear all
%load 'C:\Documents and Settings\Emmanuel\Desktop\APEX\MODIS\modis_nLw'
load nLw_412_2004_2006.mat
load nLw_443_2004_2006.mat
load nLw_488_2004_2006.mat
load nLw_531_2004_2006.mat
load nLw_551_2004_2006.mat
load nLw_667_2004_2006.mat

% distgrid (51*51=2601) grid of distance around float:
k=0;
for i=1:51
    for j=1:51
        k=k+1;
        distgrid(k)=sqrt((i-26)^2+(j-26)^2); %grid of distance around float
    end
end

% modis_nLw's have dimensions of 2601xnumber of scenes. 
% dt_modis and chl_modis: associated time and chlorophyll
load chlor_a_modis_2004_2006.mat

%%% get mean and median MODIS values within specified distances of float

[uu vv]=size(nLw_412);
% I2km=find(distgrid(1,:)<2);
I10km=find(distgrid(:)<=10); 
% I20km=find(distgrid(1,:)<20); 
% I50km=find(distgrid(1,:)<50);
  
% %%%%%%%% 10km of float by wavelength

[uu vv]=size(nLw_412);
for i=1:uu
    I=find(nLw_412(i,:)<=0);nLw_412(i,I)=NaN;
    I=find(nLw_443(i,:)<=0);nLw_443(i,I)=NaN;
    I=find(nLw_488(i,:)<=0);nLw_488(i,I)=NaN;
    I=find(nLw_531(i,:)<=0);nLw_531(i,I)=NaN;
    I=find(nLw_551(i,:)<=0);nLw_551(i,I)=NaN;
    I=find(nLw_667(i,:)<=0);nLw_667(i,I)=NaN;
    I=find(chl_modis(i,:)<=0);chl_modis(i,I)=NaN;
end

for i=1:vv
   dist_10km_412_A(i)=nanmean(nLw_412(I10km,i),1);
   dist_10km_412_B(i)=nanmedian(nLw_412(I10km,i),1);
   dist_10km_412_C(i)=nanstd(nLw_412(I10km,i),0,1);
   dist_10km_443_A(i)=nanmean(nLw_443(I10km,i),1);
   dist_10km_443_B(i)=nanmedian(nLw_443(I10km,i),1);
   dist_10km_443_C(i)=nanstd(nLw_443(I10km,i),1);
   dist_10km_488_A(i)=nanmean(nLw_488(I10km,i),1);
   dist_10km_488_B(i)=nanmedian(nLw_488(I10km,i),1);
   dist_10km_488_C(i)=nanstd(nLw_488(I10km,i),1);
   dist_10km_531_A(i)=nanmean(nLw_531(I10km,i),1);
   dist_10km_531_B(i)=nanmedian(nLw_531(I10km,i),1);
   dist_10km_531_C(i)=nanstd(nLw_531(I10km,i),1);
   dist_10km_551_A(i)=nanmean(nLw_551(I10km,i),1);
   dist_10km_551_B(i)=nanmedian(nLw_551(I10km,i),1);
   dist_10km_551_C(i)=nanstd(nLw_551(I10km,i),1);
   dist_10km_667_A(i)=nanmean(nLw_667(I10km,i),1);
   dist_10km_667_B(i)=nanmedian(nLw_667(I10km,i),1);
   dist_10km_667_C(i)=nanstd(nLw_667(I10km,i),1);
   mean_chl(i)=nanmean(chl_modis(I10km,i),1);
   median_chl(i)=nanmedian(chl_modis(I10km,i),1);
   std_chl(i)=nanstd(chl_modis(I10km,i),1);   
end
spectra_median=[dist_10km_412_B',dist_10km_443_B',dist_10km_488_B',dist_10km_531_B',dist_10km_551_B',dist_10km_667_B'];
spectra_mean=[dist_10km_412_A',dist_10km_443_A',dist_10km_488_A',dist_10km_531_A',dist_10km_551_A',dist_10km_667_A'];
spectra_std=[dist_10km_412_C',dist_10km_443_C',dist_10km_488_C',dist_10km_531_C',dist_10km_551_C',dist_10km_667_C'];

%fix the time stamp to Matlab's
load offset_time
datenum_modis=dt_modis-offset;
wl=[412 443 488 531 551 667];
save MODIS_spectra wl mean_chl median_chl std_chl datenum_modis spectra_median spectra_mean spectra_std



