function avg_con2(files, x, y, varargin)
%  1.  NAME
%
%  avg_con2 - Average data from different contour files
%
%  2.  SYNOPSIS
%
%  [data, grid] = avg_con2(files, x, y, out, method )
%
%  3.  DESCRIPTION
%
%  This is a user interface to function avg_ascii2(3m) for averaging data
%  from contour files.
%
%  4.  PARAMETERS
%
%     files
%        Cell array of the vector file names to be averaged together.
%
%     x  Column number to the x-axes for common regridding.
%
%     y  Column number to the y-axes for common regridding.
%
%     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 < 3) | (nargin > 5),
	error([ 'Wrong number of arguments' ]);
end
if nargin > 1,
	out = varargin{1};
end
if nargin > 2,
	method = varargin{2};
end

%--- Average data:
[data, grid] = avg_ascii2(files, x, y, [], 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;





