function avg_vec2(files, col, varargin)
%  1.  NAME
%
%  avg_vec2 - Average data from different vector files
%
%  2.  SYNOPSIS
%
%  [data, grid] = avg_vec2(files, dir, out, method )
%
%  3.  DESCRIPTION
%
%  This is a user interface to function avg_ascii1(3m) for averaging data
%  from vector files.
%
%  4.  PARAMETERS
%
%     files
%        Cell array of the vector file names to be averaged together.
%
%     col
%        Column  to use for regridding data. For vector files this is
%        either 1 or 2.
%
%     out optional
%        Name for output file containing averaged vector data. Set it to
%        the empty string for not writting data to disk.
%
%     method optional
%        Method to use for regridding data from the different files
%        (default is linear).
%
%  5.  RETURNS
%
%     data optional
%        Array of averaged data.
%
%     grid optional
%        Grid points for averaged data.
%
%  6.  AUTHORS
%
%  Pierre Jaccard, Geophysical Institute, University of Bergen, 1999
%

%--- Default:
out = '';
method = 'linear';

%--- Arguments:
if (nargin < 1) | (nargin > 4),
	error([ 'Wrong number of arguments' ]);
end
if nargin > 2,
	out = varargin{1};
end
if nargin > 3,
	method = varargin{2};
end
	
%--- Average data:
[data, grid] = avg_ascii1(files, col, [], method);

if nargout > 0,
	varargout{1} = data;
end
if nargout > 1,
	varargout{2} = grid;
end

%--- Print data to file:

if length(out) > 0,
	fmt = ' %15.8f';
	fid = fopen(out, 'w');
	for n=1:size(data,1),
		line = sprintf(fmt, data(n,:));
		line = strrep(line, ' NaN', '1E38');
		fprintf(fid, '%s\n', line);
	end
	fclose(fid);
end
	
return;


