function   [x,y,var]=datareduction(x,y,var,ninc,filter_type,supt)
%% data reduction or subsampling
%% filter_type = 1  : box car filter or mean filter
%%             = 2  : median filter, non linear
%%             = 3  : simple sub-sampling by a factor of ninc
%%        (x,y)= coordinate of the data
%%         var = data value
%%        inic = increment, i.e. data reduction factor
%% filter_type = 1: mean filter (box-car)
%%             = 2: median filter
%%             = 3: simple sub-sampling by a factor of ninc
%%        supt = filter support (filter length)

%%  Kriging Software Package  version 1.0 beta,   October 29, 1998
%%  Copyright (c) 1998, property of Dezhang Chu and Woods Hole Oceanographic
%%  Institution.  All Rights Reserved.

if ninc > 1 | supt > 1
 n=length(x);
 if filter_type <= 2
   k=1;
   if rem(ninc,2) ~= 0  & ninc ~= 1 % odd ninc
      ninc=ninc+1;
   end   
   if rem(supt,2) ~= 0   % odd ninc
      supt=supt-1;
   end   
   for i=1:ninc:n
      if i <= supt/2
         indx=1:i+supt/2;
      elseif i > n-supt
         indx=i-supt/2:n;
      else
         indx=i-supt/2:i+supt/2;
      end
      switch filter_type 
        case 1 % box car (mean)
           xnew(k)=mean_nan(x(indx));		
           ynew(k)=mean_nan(y(indx));
           var_new(k)=mean_nan(var(indx));
        case 2	% median filter to avoid outliers
          xnew(k)=median_nan(x(indx));		
          ynew(k)=median_nan(y(indx));
          var_new(k)=median_nan(var(indx));
      end
      k=k+1;
   end
   x=xnew(:);
   y=ynew(:);
   var=var_new(:);
 else
   x=x(1:ninc:n);
   y=y(1:ninc:n);
   var=var(1:ninc:n);
 end
end
