function [newz]=cleangrid(x,y,z,data);

%Check if they are gridpointd to be eliminated 
figure
pcolor(x,y,z)
shading flat
hold on
scatter(data(:,1),data(:,2),4,'yo')
disp('Focus in the desired area of the figure and then press Enter')
zoom on
pause

disp('Indicate if they are data to be eliminated, Yes(n=1) / No(n=0)')
n=input('n=');

if n==0
    newz=z;
else
    rep=1;
    j=1;
    while rep == 1
        xc=[]; yc=[];
        %newz=z;
        if j==2;
            z=newz;
            figure(j)
        elseif j>=3
            z=newz;
            figure(j)
            %  pcolor(x,y,z)
            %  shading flat
            %  hold on
            %  scatter(data(:,1),data(:,2),4,'ko')
            disp('Focus in the desired area of the figure and then press Enter')
            zoom on
            %  pause
        end
        switch n
            case 1;
                disp('Introduce the corners of the polygon that includes the data to be eliminated');
                %[xc,yc] = ginput();
                hold on
                i=0;
                but = 1;
             
             disp('Select polygon to exclude in bathy')
             disp('Left mouse button picks points.')
             disp('Right mouse button picks last point.')
             while but == 1
                 i = i+1;
                 [xc(i),yc(i),but] = ginput(1);
                 plot(xc,yc,'k-o');
             end
             xc = [xc' ; xc(1)]; yc = [yc' ; yc(1)];
             in = inpolygon(x,y,xc,yc);
             scatter(x(in),y(in),6,'+k')
             j=j+1;
         case 0
             in=[];
     end

     z(in)=NaN;
     newz =z;
     figure(j)
     pcolor(x,y,newz)
     shading flat
     hold on
     scatter(data(:,1),data(:,2),4,'yo')
     
     
     zoom on
     disp('Focus in the desired area of the figure and then press Enter')
     pause
     
     disp('Do you want to eliminate more grid points ?, Yes(rep=1) / No(rep=0)')
     rep=input('rep=');
   
 end

end
 

close all
  