% C:\Documents and Settings\Lisa Taylor\My Documents\Data\APEX\0015\Modis
% generates MODIS Lnw spectra for 7.5km around the float's position:

close all
clear all

%load 'C:\Documents and Settings\Emmanuel\Desktop\APEX\0015\MODIS\modis_nLw'
load data/nLw_412_0015.mat
load data/nLw_443_0015.mat
load data/nLw_488_0015.mat
load data/nLw_531_0015.mat
load data/nLw_551_0015.mat
load data/nLw_667_0015.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 data/chlor_a_modis_2004_2006.mat
load data/chlor_a_0015.mat

load data/dt_im_0015.mat
% fix the time stamp to Matlab's
t_offset=1721059;
datenum_modis=(dt_im-t_offset)'; % now 1105x1

% get mean and median MODIS values within specified distances of float
[uu vv]=size(nLw_412);
% 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_412);

% replace values less than 0 with NAN
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(chlor_a(i,:)<=0);chl_modis(i,I)=NaN;
end

% get mean, median, standard deviation
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(chlor_a(I10km,i),1);
   median_chl(i)=nanmedian(chlor_a(I10km,i),1);
   std_chl(i)=nanstd(chlor_a(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 wl mean_chl median_chl std_chl datenum_modis spectra_median spectra_mean spectra_std



