% C:\Documents and Settings\Lisa Taylor\My Documents\Data\MODIS
% generates MODIS Lnw spectra for 7.5km around the float's position
% data have been ringed

close all
clear all

%load 'C:\Documents and Settings\Emmanuel\Desktop\APEX\MODIS\modis_nLw'
load 'C:\Documents and Settings\Lisa Taylor\My Documents\Data\0005\MODIS\data\original downloads\08-23-07\nLw_412_dilate_2004_2007.mat'
load 'C:\Documents and Settings\Lisa Taylor\My Documents\Data\0005\MODIS\data\original downloads\08-23-07\nLw_443_dilate_2004_2007.mat'
load 'C:\Documents and Settings\Lisa Taylor\My Documents\Data\0005\MODIS\data\original downloads\08-23-07\nLw_488_dilate_2004_2007.mat'
load 'C:\Documents and Settings\Lisa Taylor\My Documents\Data\0005\MODIS\data\original downloads\08-23-07\nLw_531_dilate_2004_2007.mat'
load 'C:\Documents and Settings\Lisa Taylor\My Documents\Data\0005\MODIS\data\original downloads\08-23-07\nLw_551_dilate_2004_2007.mat'
load 'C:\Documents and Settings\Lisa Taylor\My Documents\Data\0005\MODIS\data\original downloads\08-23-07\nLw_667_dilate_2004_2007.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. 

load 'C:\Documents and Settings\Lisa Taylor\My Documents\Data\0005\MODIS\data\original downloads\08-23-07\chlor_a_modis_dilate_2004_2007.mat'

% fix the time stamp to Matlab's
t_offset=1721059;
date_modis=dt_modis-t_offset; % record 1,1 should be 12-Jun-2004 14:45:00

% get mean and median MODIS values within specified distances of float
[uu vv]=size(nLw_412a);
% I2km=find(distgrid(1,:)<2);
I10km=find(distgrid(:)<=7.5); 
% I20km=find(distgrid(1,:)<20); 
% I50km=find(distgrid(1,:)<50);
  
% %%%%%%%% 7.5km of float by wavelength

[uu vv]=size(nLw_412a);

% replace values less than 0 with NAN
for i=1:uu
    I=find(nLw_412a(i,:)<=0);nLw_412a(i,I)=NaN;
    I=find(nLw_443a(i,:)<=0);nLw_443a(i,I)=NaN;
    I=find(nLw_488a(i,:)<=0);nLw_488a(i,I)=NaN;
    I=find(nLw_531a(i,:)<=0);nLw_531a(i,I)=NaN;
    I=find(nLw_551a(i,:)<=0);nLw_551a(i,I)=NaN;
    I=find(nLw_667a(i,:)<=0);nLw_667a(i,I)=NaN;
    I=find(chlor_a1(i,:)<=0);chl_modis(i,I)=NaN;
end

% get mean, median, standard deviation
for i=1:vv
   dist_10km_412_A(i)=nanmean(nLw_412a(I10km,i),1);
   dist_10km_412_B(i)=nanmedian(nLw_412a(I10km,i),1);
   dist_10km_412_C(i)=nanstd(nLw_412a(I10km,i),0,1);
   dist_10km_443_A(i)=nanmean(nLw_443a(I10km,i),1);
   dist_10km_443_B(i)=nanmedian(nLw_443a(I10km,i),1);
   dist_10km_443_C(i)=nanstd(nLw_443a(I10km,i),1);
   dist_10km_488_A(i)=nanmean(nLw_488a(I10km,i),1);
   dist_10km_488_B(i)=nanmedian(nLw_488a(I10km,i),1);
   dist_10km_488_C(i)=nanstd(nLw_488a(I10km,i),1);
   dist_10km_531_A(i)=nanmean(nLw_531a(I10km,i),1);
   dist_10km_531_B(i)=nanmedian(nLw_531a(I10km,i),1);
   dist_10km_531_C(i)=nanstd(nLw_531a(I10km,i),1);
   dist_10km_551_A(i)=nanmean(nLw_551a(I10km,i),1);
   dist_10km_551_B(i)=nanmedian(nLw_551a(I10km,i),1);
   dist_10km_551_C(i)=nanstd(nLw_551a(I10km,i),1);
   dist_10km_667_A(i)=nanmean(nLw_667a(I10km,i),1);
   dist_10km_667_B(i)=nanmedian(nLw_667a(I10km,i),1);
   dist_10km_667_C(i)=nanstd(nLw_667a(I10km,i),1);
   mean_chl(i)=nanmean(chlor_a1(I10km,i),1);
   median_chl(i)=nanmedian(chlor_a1(I10km,i),1);
   std_chl(i)=nanstd(chlor_a1(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'];

wl=[412 443 488 531 551 667];

% save file MODIS_spectra_7_5km with fields below:
save MODIS_spectra_7_5km_dilate wl mean_chl median_chl std_chl date_modis spectra_median spectra_mean spectra_std



