clear
clc

% sign int 

for Jday=284:365

    Jday_str=num2str(Jday,'%03.f')
	 
    
    cd(['/ships19/hercules/mino/Satellite_Out/HIM08/2019/level2/',Jday_str,'/v3/'])
    files=dir('*.level2.nc');
	 
    for k=1:length(files)
	 
	 [Jday k]
    filename=files(k).name;
    
	 cloud_mask_org=single(ncread(filename,'cloud_mask'));
    cloud_mask=single(ncread(filename,'cloud_mask'));
	 
	 [row, col]=size(cloud_mask);
	 
    cloud_mask(cloud_mask<0)=NaN;
    cloud_mask(cloud_mask>0)=1;
	 
    Cld_distance_03= int8(ordfilt2(cloud_mask, 9, true(3)));
	 Cld_distance_04= int8(ordfilt2(cloud_mask, 16, true(4)));
    Cld_distance_05= int8(ordfilt2(cloud_mask, 25, true(5)));
	 
	 Cld_distance_03(cloud_mask_org<0)=-1;
    Cld_distance_04(cloud_mask_org<0)=-1;
    Cld_distance_05(cloud_mask_org<0)=-1;
    
	 
	 nccreate(filename,'Cld_distance_03',...
          'Dimensions',{'r',row,'c',col},...
          'Datatype','int8',...
			 'Format','netcdf4');
   ncwrite(filename,'Cld_distance_03',Cld_distance_03)
	
	nccreate(filename,'Cld_distance_04',...
          'Dimensions',{'r',row,'c',col},...
          'Datatype','int8',...
			 'Format','netcdf4');
   ncwrite(filename,'Cld_distance_04',Cld_distance_04)


   nccreate(filename,'Cld_distance_05',...
          'Dimensions',{'r',row,'c',col},...
          'Datatype','int8',...
			 'Format','netcdf4');
   ncwrite(filename,'Cld_distance_05',Cld_distance_05)
	
    
    end
    
end



finfo = ncinfo(filename);
disp(finfo)
finfo.Variables.Name


