function varargout=avg_ascii1(files, varargin)
%  1.  NAME
%
%  avg_ascii1 - Average columns from different input files together
%
%  2.  SYNOPSIS
%
%  [new, grid] = avg_ascii1(files, col, grid, method )
%
%  3.  DESCRIPTION
%
%  This Matlab function calculates averages of data between different
%  ASCII columnar files. Each column specifies the values of a variable
%  at different grid points. If only files is specified, it is assumed
%  that each line of data correspond to the same grid point, and data
%  from the different files are avergaed together as is. This requires
%  the files to have the same number of lines.
%
%  If col is specified, a new grid, common to all datasets is calculated,
%  and all other columns are regridded on this new grid. This is carried
%  out by function regrid(3m).
%
%  If grid is specified, it is taken as the new grid.
%
%  The interpolation method for regridding can be specified by method.
%
%  Since regridding is done by regrid1(3m), bad data along the regrid
%  axis is allowed, and duplicate grid points oqn input will be averaged
%  together.
%
%  4.  PARAMETERS
%
%     files
%        Cell array of file names containing columnar ASCII data to be
%        averaged together. Instead of passing file names,you can also
%        pass a cell array of data already processed.
%
%     col optional
%        Column number to use for regridding data to a common grid before
%        averaging.
%
%     grid optional
%        Grid on which data should be averaged (default is to build a
%        default grid).  This parameters requires parameter col to be
%        defined.
%
%     method optional
%        Method to use for regridding data (default is linear). See
%        Matlab function interp1 for more information.
%
%  5.  RETURNS
%
%     new
%        Array with averaged data on the new grid.
%
%     grid optional
%        The new grid points.
%
%  6.  AUTHORS
%
%  Pierre Jaccard, Geophysical Institute, University of Bergen, 1999
%

%--- Default values:
col     = [];
method  = 'linear';
grid    = [];

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

%--- Initialization:
n_files = length(files);
if n_files < 2,
	error([ 'Too few files/data for averaging data' ]);
end
if ~isempty(col),
	col = col(1);
else
	col = -1;
end

if isstr(files{1}),
	%--- Read the data:
	for f=1:n_files,
		data{f} = tonan(read_asc(files{f}));	
	end
else
	data = files;
end

%--- Check if all files have the same number of columns:
nc = size(data{1}, 2);
for f=2:n_files,
	if nc ~= size(data{f},2),
		error([ 'Files ' int2str(1) ' and ' int2str(f) ...
					' have different column number' ]);
	end
end

%--- Check that regrid column is within the columns of data:
if col > 0,
	vv = find((1:nc) ~= col);
	if col > nc,
		error([ 'Regrid column outside range' ]);
	end
else
	vv = (1:nc);
end

%--- Regrid data:
if col > 0,
	
	%--- Build a new grid common to all datasets:
	if isempty(grid),
		
		%--- Get the most included range along regrid column:
		[ xmin, xmax, n ] = com_grd(data, col);
		%--- Build the new grid:
		grid = new_grid(xmin, xmax, n);
		
	end

	%--- Regrid data along specified grid:
	for f=1:n_files,
		new{f} = regrid1(data{f}, col, grid, method);
	end

else
	%--- If no regrid column is specified, no regridding is required. This
	%    assumes that data from the different files are given for the same common
	%    grid. 
	for f=1:n_files,
		new{f} = data{f};
	end
end

%--- Check that all datasets have the same number of lines:
nl = size(new{1},1);
for f=2:n_files,
	if size(new{f},1) ~= nl,
		error([ 'Files ' files{1} ' and ' files{f} ...
					' do not have the same number of lines' ]);
	end
end

%--- For averaging, build a 3-dimensionnal array:
clear data;
data(:,:,1) = new{1};
for f=2:n_files,
	data(:,:,f) = new{f};
end
clear new;

%--- Average data along dimension 3:
nl = size(data,1);
new = NaN * ones(nl,nc);
for n=1:nl,
	for c=1:nc,
		igood = find(~isnan(data(n,c,:)));
		if ~isempty(igood),
			new(n,c) = mean(data(n,c,igood));
		end
	end
end

%--- Return values:
if nargout > 0,
	varargout{1} = new;
end
if nargout > 1,
	varargout{2} = grid;
end

return;





