%% dilate cloud pixels in lab sea data

cd C:\PROJECTS\ARGO_NATL\modis_data\current_labsea_modis_data
load chlor_a_modis_2004_2007.mat

se=strel('disk',1,0);  %radius=1 km disk stucturing element 

for ii=1:length(chlor_a(:,2))
    ii
    im=chlor_a(:,ii);
    im1=reshape(im,51,51);
    nim=zeros(51,51);           % new binary cloud mask array
    [i1,j1]=find(im1==-1);      % find cloud or flagged data (-999 is missing)

    for kk=1:length(i1)
        nim(i1(kk),j1(kk))=1;      % fill clouds as 1 to make a binary cloudmask
    end
    % dilate binary cloud mask
    nim2=imdilate(nim,se);          % dilate
    im2=im1;
    [i2,j2]=find(nim2==1);     % find new dilated cloud mask indices
    for kk=1:length(i2)
        im2(i2(kk),j2(kk))=-1;      % new cloud mask
    end
chlor_a1(:,ii)=im2(:);          %new chlor_a timeseries
%figure(1);imagesc(im1,[-1 5]);
%figure(2);imagesc(im2,[-1 5]); 
%drawnow;
end

%% do nLw_412
load nLw_412_2004_2007.mat

se=strel('disk',1,0);  %radius=1 km disk element 

for ii=1:length(nLw_412(1,:))
    ii
    im=nLw_412(:,ii);
    im1=reshape(im,51,51);
    nim=zeros(51,51);
    [i1,j1]=find(im1==0);      % find cloud or flagged data (-0.999 is missing)

    for kk=1:length(i1)
        nim(i1(kk),j1(kk))=1;      % fill clouds as 1 to make binary cloudmask
    end
    % dilate binary cloud mask
    nim2=imdilate(nim,se);
    im2=im1;
    [i2,j2]=find(nim2==1);     % find cloud mask indices
    for kk=1:length(i2)
        im2(i2(kk),j2(kk))=0;      % new cloud mask
    end
nLw_412a(:,ii)=im2(:);          %new chlor_a timeseries
%figure(1);imagesc(im1,[-1 5]);
%figure(2);imagesc(im2,[-1 5]); 
%drawnow;
end
save nLw_412_dilate_2004_2007.mat nLw_412a

%% do nLw_443
load nLw_443_2004_2007.mat

se=strel('disk',1,0);  %radius=1 km disk element 

for ii=1:length(nLw_443(1,:))
    ii
    im=nLw_443(:,ii);
    im1=reshape(im,51,51);
    nim=zeros(51,51);
    [i1,j1]=find(im1==0);      % find cloud or flagged data (-0.999 is missing)

    for kk=1:length(i1)
        nim(i1(kk),j1(kk))=1;      % fill clouds as 1 to make binary cloudmask
    end
    % dilate binary cloud mask
    nim2=imdilate(nim,se);
    im2=im1;
    [i2,j2]=find(nim2==1);     % find cloud mask indices
    for kk=1:length(i2)
        im2(i2(kk),j2(kk))=0;      % new cloud mask
    end
nLw_443a(:,ii)=im2(:);          %new chlor_a timeseries
%figure(1);imagesc(im1,[-1 5]);
%figure(2);imagesc(im2,[-1 5]); 
%drawnow;
end
save nLw_443_dilate_2004_2007.mat nLw_443a

%% do nLw_488
load nLw_488_2004_2007.mat

se=strel('disk',1,0);  %radius=1 km disk element 

for ii=1:length(nLw_488(1,:))
    ii
    im=nLw_488(:,ii);
    im1=reshape(im,51,51);
    nim=zeros(51,51);
    [i1,j1]=find(im1==0);      % find cloud or flagged data (-0.999 is missing)

    for kk=1:length(i1)
        nim(i1(kk),j1(kk))=1;      % fill clouds as 1 to make binary cloudmask
    end
    % dilate binary cloud mask
    nim2=imdilate(nim,se);
    im2=im1;
    [i2,j2]=find(nim2==1);     % find cloud mask indices
    for kk=1:length(i2)
        im2(i2(kk),j2(kk))=0;      % new cloud mask
    end
nLw_488a(:,ii)=im2(:);          %new chlor_a timeseries
%figure(1);imagesc(im1,[-1 5]);
%figure(2);imagesc(im2,[-1 5]); 
%drawnow;
end
save nLw_488_dilate_2004_2007.mat nLw_488a

%% do nLw_531
load nLw_531_2004_2007.mat

se=strel('disk',1,0);  %radius=1 km disk element 

for ii=1:length(nLw_531(1,:))
    ii
    im=nLw_531(:,ii);
    im1=reshape(im,51,51);
    nim=zeros(51,51);
    [i1,j1]=find(im1==0);      % find cloud or flagged data (-0.999 is missing)

    for kk=1:length(i1)
        nim(i1(kk),j1(kk))=1;      % fill clouds as 1 to make binary cloudmask
    end
    % dilate binary cloud mask
    nim2=imdilate(nim,se);
    im2=im1;
    [i2,j2]=find(nim2==1);     % find cloud mask indices
    for kk=1:length(i2)
        im2(i2(kk),j2(kk))=0;      % new cloud mask
    end
nLw_531a(:,ii)=im2(:);          %new chlor_a timeseries
%figure(1);imagesc(im1,[-1 5]);
%figure(2);imagesc(im2,[-1 5]); 
%drawnow;
end
save nLw_531_dilate_2004_2007.mat nLw_531a

%% do nLw_551
load nLw_551_2004_2007.mat

se=strel('disk',1,0);  %radius=1 km disk element 

for ii=1:length(nLw_551(1,:))
    ii
    im=nLw_551(:,ii);
    im1=reshape(im,51,51);
    nim=zeros(51,51);
    [i1,j1]=find(im1==0);      % find cloud or flagged data (-0.999 is missing)

    for kk=1:length(i1)
        nim(i1(kk),j1(kk))=1;      % fill clouds as 1 to make binary cloudmask
    end
    % dilate binary cloud mask
    nim2=imdilate(nim,se);
    im2=im1;
    [i2,j2]=find(nim2==1);     % find cloud mask indices
    for kk=1:length(i2)
        im2(i2(kk),j2(kk))=0;      % new cloud mask
    end
nLw_551a(:,ii)=im2(:);          %new chlor_a timeseries
%figure(1);imagesc(im1,[-1 5]);
%figure(2);imagesc(im2,[-1 5]); 
%drawnow;
end
save nLw_551_dilate_2004_2007.mat nLw_551a

%% do nLw_667
load nLw_667_2004_2007.mat

se=strel('disk',1,0);  %radius=1 km disk element 

for ii=1:length(nLw_667(1,:))
    ii
    im=nLw_667(:,ii);
    im1=reshape(im,51,51);
    nim=zeros(51,51);
    [i1,j1]=find(im1==0);      % find cloud or flagged data (-0.999 is missing)

    for kk=1:length(i1)
        nim(i1(kk),j1(kk))=1;      % fill clouds as 1 to make binary cloudmask
    end
    % dilate binary cloud mask
    nim2=imdilate(nim,se);
    im2=im1;
    [i2,j2]=find(nim2==1);     % find cloud mask indices
    for kk=1:length(i2)
        im2(i2(kk),j2(kk))=0;      % new cloud mask
    end
nLw_667a(:,ii)=im2(:);          %new chlor_a timeseries
%figure(1);imagesc(im1,[-1 5]);
%figure(2);imagesc(im2,[-1 5]); 
%drawnow;
end
save nLw_667_dilate_2004_2007.mat nLw_667a

