function [new, varargout] = regrid1(data, col, varargin)
%  1.  NAME
%
%  regrid1 - Regrid columnar data along a new grid
%
%  2.  SYNOPSIS
%
%  [new, grid] = regrid1(data, col, grid, method )
%
%  3.  DESCRIPTION
%
%  This Matlab function regrids separately each columns of an array on a
%  new common grid.  One of these column is the regrid axis and is
%  specified by column number col. If grid is not specified, a new grid
%  is automatically generated with these values, using function
%  new_grid(3m). Data are interpolated on the new grid using Matlab
%  function interp1.
%
%  Input values along column col may contain bad values, and do not need
%  to be monotonic. However, on output, the lines are sorted into
%  increasing order, and all lines with bad data in column col are
%  removed.  Duplicate data values on the same grid point are averaged
%  together.
%
%  Bad data are not sent to interp1, so that gaps with bad data along the
%  grid column should be correctly filled with interpolated values.
%
%  4.  PARAMETERS
%
%     data
%        Array of data to be regridded. Each column contains data for the
%        same variable given at different grid points.
%
%     col
%        Column number to use for regridding.
%
%     grid optional
%        Vector of grid points on which data should be regridded (default
%        is to generate a regular grid starting and ending at the same
%        points defined in the given column and with the same number of
%        points).
%
%     method optional
%        Regrid method to use (default is linear). See Matlab function
%        interp1 for more information.
%
%  5.  RETURNS
%
%     new
%        The array of regridded data,with the same structure as the input
%        data.
%
%     grid optional
%        The new grid.
%
%  6.  AUTHORS
%
%  Pierre Jaccard, Geophysical Institute, University of Bergen, 1999
%

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

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

%--- Check that regrid column is defined:
if (col > size(data,2)) | (col < 1),
	error([ 'Grid column must be whithin available columns' ]);
end

%--- Initializations:
nc = size(data,2);
vv = find((1:nc) ~= col);

%--- Get the grid and remove lines with bad grid data:
[ grd, data ] = ascii_grd(data, col);
grd = grd{1};
ng = length(grd);
nl = size(data,1);

%--- Rebuild the data array so that it has the same length as the grid:
data = sortrows(data, col);
% Build mask of duplicate grid lines:
ii = 2:nl;
ii = (data(ii,col) == data(ii-1, col));
ii = cat(1, 0, ii );
% Build mask of first duplicate grid lines:
i0 = 1:(nl-1);
i0 = (~ii(i0) & ii(i0+1));
i0 = find(i0);
% Build index of last duplicate grid lines:
i1 = 1:(nl-1);
i1 = (ii(i1) & ~(ii(i1+1)));
i1 = cat(1, i1, ii(nl));
i1 = find(i1);
% This is a control:
if length(i0) ~= length(i1),
	error([ 'Cannot find duplicate ranges on specified grid' ]);
end
% Loop through the duplicate ranges:
for i=1:length(i0),
	ii = i0(i):i1(i);
	% Average duplicate grid points:
	for j=vv,
		data(i0(i),j) = mean_nan(data(ii,j));
	end
	% Flag bad rest of positions in this range:
	ii = (i0(i)+1):i1(i);
	data(ii,col) = NaN;
end
% Filter out bad pad positions:
igood = find(~isnan(data(:,col)));
if isempty(igood),
	error([ 'No good data' ]);
end
data = data(igood, :);

%--- Eventually, build a new regular grid:
if isempty(grid),
	grid = new_grid(min(grd), max(grd), ng);
end

%--- Grid must be a column vector:
if size(grid,1) < size(grid,2),
	grid = grid';
end
ng = length(grid);
%--- Regrid data:
new = NaN * ones(ng, nc);
new(:,col) = grid;
% Loop through the variables:
for v=vv,
	igood = find(~isnan(data(:,v)));
	if ~isempty(igood),
		new(:,v) = interp1(data(igood,col), data(igood,v), grid, method);
	end
end

%--- Final exit:
if nargout > 0,
	varargout{1} = grid;
end	

return;




