function  [out] = sergeiRemoveOutliers (T,inarr,t,ws,crit)
% This function scans a two-column array along the dimension dim and removes the rows
% that contain numbers outside of t*sigma range from the running mean
% The first 4 dimensions in the input array are: time, rmean,npoints in window, rstd
% The input array T has 2 dimensions: time, temperature
dinarr=max(size(inarr));
dT=max(size(T));
jdT=T(:,1);
jdinarr=inarr(:,1);
k=1;
for j=1:dT
    i=mod(jdT(j),365)+1;
            
        % CONDITION FOR RETAINING THE POINT
        if crit==0
 
       % --- Keep points within t-sigma of the mean -------- 
          if T(j,2)<inarr(i,2)+t*inarr(i,4)    % comment this line if high-T is ok
            if T(j,2)>inarr(i,2)-t*inarr(i,4)
            arr(k,:)=T(j,:);
            k=k+1;
            end
          end
       % -------------------------------------------
       
        end
        
        if crit==1
       
       % --- Keep points that pass Chauvenet's criterion ----
       
            dist = abs(T(j,2)-inarr(i,2));
            nexp = inarr(i,3)*(1-normcdf(dist,0,inarr(i,4)));
           if nexp>=1/2
               arr(k,:)=T(j,:);
               k=k+1;
            end
       %  --------------------------------------------
       
    end
end   
out = arr;