function   [x,y,var,indxs,long,lat]=datachk(x,y,var,ninc,filter_type,supt,conversion)
%% datachk.m perform several operations on the original data
%% 1. check raw data to remove overlapping data points	
%% 2. data subsampling using averaged data over support ninc
%% 3. coordinate conversion 
%%
%% Input Varibles:
%% 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)
%%       conversion = 1  (long, lat) to  km
%%
%%  Output Varibles:
%%        (x,y)= coordinate of the processed data
%%         var = processed data value
%%       indxs = indices of the removed nearly collocated data points 
%%   (long,lat)= longitude and latitude if the coordinates are long. and lat.

%%  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.

r=sqrt(diff(x).^2+diff(y).^2);
rmin=0.01*mean(r);
indxs=find(r < rmin);
x(indxs)=[];
y(indxs)=[];
var(indxs)=[];

if ninc  > 1 | supt > 1
  [x,y,var]=datareduction(x,y,var,ninc,filter_type,supt);
end

%% 
if conversion == 1
   long=x(:);lat=y(:);			% in degree
   deg2km=111;		% conversion factor from Longitude/Latitude to km
   scl=cos(lat*pi/180);			% scale factor to latitude
   x=long*deg2km.*scl;   	% in degree of latitude
   y=lat*deg2km;
else
   lat=[];long=[];
end