function clean(core,ppath)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% clean.m
%
% goes through the processed images and check for "pancake amygdales" 
% (amygdales 1 voxel thick), and removes most of them (may need to be run
% several times
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%clear all,close all,clc
%tic
if ppath==1
  path3=['/media/cloud/Paleobarometry/Binaries/' core(1:end-2) '/'];
  files=dir(fullfile(path3, '*.tif'));
  fileext=files(1).name;
  disp(sprintf(['Current image format: ' fileext(end-3:end)]));
  disp(sprintf('Cleaning.. Please wait'));
else
  path3=['/media/cloud/Paleobarometry/Binaries/' core(1:end-2) '/even/'];
  files=dir(fullfile(path3, '*.tif'));	
  disp(sprintf('Cleaning again.. almost ready..'));
end 

for jj=1:1 %how many times over you wanna go over the core and clean
    for j=3:length(files)-2
	%files(j).name
	H=imread([path3 files(j-2).name]);
        I=imread([path3 files(j-1).name]);
        J=imread([path3 files(j).name]);
        K=imread([path3 files(j+1).name]);
	L=imread([path3 files(j+2).name]);
    %     I=imread('d:\Binaries\05017-5\05017-5-1_rec0616.tiff');
    %     J=imread('d:\Binaries\05017-5\05017-5-1_rec0617.tiff');
    %     K=imread('d:\Binaries\05017-5\05017-5-1_rec0618.tiff');
    %     subplot(131), imshow(I)
    %     subplot(132),imshow(J)
    %     subplot(133),imshow(K)
        J2=pancake(H,I,J,K,L);
        filename=files(j).name;
        imwrite(J2,[path3 filename(1:end-4) '.tif'],'tif');
	if j == round((length(files)-5)/2)
	   disp(sprintf('Chill, halfway there...'));
	end
        %imshow(J)
    %     keyboard
    %     
    %     imwrite(Ibin,[path3 filename(1:end-4) '.tiff'],'tiff');
    end
end
%toc
