%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                  ProcEDīs subroutine                    %
%               5 horizontal cells filter                 %
%               matrix of 2 or 3 dimensions               %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function [new_speed]=filter_5(speed,n_line,n_col,dim);

   	% speed - velocity matrix in
    % n_line - matrix lines 
    % n_col - matrix columns
    % dim - matrix dimension
    % new_speed - velocity matrix out
    
    for i=1:n_line
        
        for j=3:n_col-2
            speed_tmp=speed(i,j-2:j+2);
 
            ind0=isnan(speed_tmp);
            ind2=find(ind0==1);
            speed_tmp(ind2)=0;
            ncel_filter=length(speed_tmp)-length(ind2);
            if ncel_filter > 0
                filter=1/ncel_filter;
                new_speed(i,j)=filter*speed_tmp(1)+...
                                filter*speed_tmp(2)+...
                                filter*speed_tmp(3)+...
                                filter*speed_tmp(4)+...
                                filter*speed_tmp(5);
            end
            clear speed_temp; clear ind1; clear ind2; 
        end % j

        % calculates 1st column cell
        j=1;        
        speed_tmp=speed(i,j:j+2);
        ind0=isnan(speed_tmp);
        ind2=find(ind0==1);
        speed_tmp(ind2)=0;
        ncel_filter=length(speed_tmp)-length(ind2);
            if ncel_filter > 0
               filter=1/ncel_filter;

               new_speed(i,j)=filter*speed_tmp(1)+...
                               filter*speed_tmp(2)+...
                               filter*speed_tmp(3);
            end
        clear speed_temp; clear ind1; clear ind0; 
        
        % calculates 2nd column cell 
        j=2;        
        speed_tmp=speed(i,j-1:j+2);
            ind0=isnan(speed_tmp);
            ind2=find(ind0==1);
            speed_tmp(ind2)=0;
            ncel_filter=length(speed_tmp)-length(ind2);
        if ncel_filter > 0
           filter=1/ncel_filter;
       
           new_speed(i,j)=filter*speed_tmp(1)+...
                           filter*speed_tmp(2)+...
                           filter*speed_tmp(3)+...
                           filter*speed_tmp(4);
        end
        clear speed_temp; clear ind1; clear ind0; 
        
        % calculates penult column cell 
        j=n_col-1;        
        speed_tmp=speed(i,j-2:j+1);
            ind0=isnan(speed_tmp);
            ind2=find(ind0==1);
            speed_tmp(ind2)=0;
            ncel_filter=length(speed_tmp)-length(ind2);
        if ncel_filter > 0
           filter=1/ncel_filter;
       
           new_speed(i,j)=filter*speed_tmp(1)+...
                           filter*speed_tmp(2)+...
                           filter*speed_tmp(3)+...
                           filter*speed_tmp(4);
        end
        clear speed_temp; clear ind1; clear ind0; 
        
        % calculates last column cell 
        j=n_col;        
        speed_tmp=speed(i,j-2:j);
            ind0=isnan(speed_tmp);
            ind2=find(ind0==1);
            speed_tmp(ind2)=0;
            ncel_filter=length(speed_tmp)-length(ind2);
        if ncel_filter > 0
           filter=1/ncel_filter;
       
           new_speed(i,j)=filter*speed_tmp(1)+...
                           filter*speed_tmp(2)+...
                           filter*speed_tmp(3);
        end
        clear speed_temp; clear ind1; clear ind0; 
    end % i

    
        
