%  1.  NAME
%
%  regrid - Regrid one or two dimensional data
%
%  2.  USAGE
%
%  regrid
%
%  3.  DESCRIPTION
%
%  This Matlab script regrids columnar ascii data files. It can handle
%  one, two or mixed data files.
%
%  One dimensional data files contain the values of each variable in
%  different columns. Two dimensional data files have the xyz , where
%  several z values are allowed. Mixed dimensional files have the same
%  as two dimensional ones, however several x columns are allowed.
%
%  How the different columns are regridded is detected automatically by
%  setting unused parameters to the empty array (see below).
%
%  Input data do not nead to be sorted, may have bad data and duplicate
%  grid points are allowed.
%
%  The result is a columnar data file with the same  as the input file.
%  However, columns of non-regridded variables are removed. The lines are
%  sorted along increasing values of x_grid, and y_grid if any. Lines
%  with missing data are not removed but flagged as bad.
%
%  4.  PARAMETERS
%
%  The following parameters must be edited:
%
%     data_file
%        Name of input data file.
%
%     out_file
%        Name of output data file.
%
%     x_grid
%        Name of a file containing the new x-grid values in the first
%        column,or and array with [min max n] to specify a regular grid
%        with n points from min to max (inclusive). This parameter is
%        required.
%
%     y_grid
%        Name of a file containing the new y-grid values in the first
%        column,or and array with [min max n] to specify a regular grid
%        with n points from min to max (inclusive). If only one
%        dimensional data are to be regridded,set this parameter to the
%        empty array.
%
%     method
%        Interpolation method (default is linear). See Matlab help on
%        function interp1 or griddata for more information.
%
%     x_col
%        The column number for x-data in the input file.
%
%     y_col
%        The column number for y-data in the input file.  If only one
%        dimensionnal data are to be regridded,set this parameter to the
%        empty array.
%
%     cols_1d
%        A vector with all column numbers to one dimensionnal variables
%        to be regridded. If only two domensionnal data are to be
%        regridded,set it to the empty array.
%
%     cols_2d
%        A vector with all column numbers to two dimensionnal variables
%        to be regridded. If only one domensionnal data are to be
%        regridded,set it to the empty array.
%
%  5.  AUTHORS
%
%  Pierre Jaccard, Geophysical Institute, University of Bergen, 1999
%

% Edit following lines:
%------------------------------------------------------------------

%--- Input and output files:

data_file = 'adcp_fs_abs.gcon';
out_file  = 'adcp_fs_abs.rgd';

%data_file = 'adcp_bt_abs.gcon';
%out_file  = 'adcp_bt_abs.rgd';

%data_file = 'adcp_fs_abs_detided.gcon';
%out_file  = 'adcp_fs_abs_detided.rgd';

%data_file = 'adcp_bt_abs_detided.gcon';
%out_file  = 'adcp_bt_abs_detided.rgd';

%--- Define new grid:
x_grid = [ 57.305 58.025 49 ];
y_grid = [ 20 550 54 ];
method = [];

%--- Define columns in iput file:
x_col   = 3;
y_col   = 4;
cols_1d = [1,2];
cols_2d = [5,6];

%------------------------------------------------------------------
% No more editing!!!

if isempty(method),
  method = 'linear';
end

%--- Read input data:
data = tonan(read_asc(data_file));

%--- Build x grids
if ischar(x_grid),
  x_grid = tonan(read_ascii(x_grid));
elseif length(x_grid) > 1,
  x_grid = new_grid( x_grid(1), x_grid(2), x_grid(3));
else
  error([ 'A grid for x-axis is required' ]);
end

%--- Build y grids
if ischar(y_grid),
  y_grid = tonan(read_ascii(y_grid));
elseif length(y_grid) > 1,
  y_grid = new_grid( y_grid(1), y_grid(2), y_grid(3));
else
  y_grid = [];
end

%--- Regrid 1D data:
if isempty(cols_1d),
  disp([ '*No 1-D regridding*' ]);
  d1 = [];
else
  disp([ 'Regridding 1-D data' ]);
  [d1, g1] = regrid1(data(:, [x_col, cols_1d]), 1, x_grid, method);
end

%--- Regrid 2D data:
if isempty(y_grid) | isempty(cols_2d),
  disp([ '*No 2-D regridding*' ]);
  d2 = [];
else
  disp([ 'Regridding 2-D data' ]);
  [d2, g2] = regrid2(data(:, [x_col, y_col, cols_2d]), 1, 2, ...
      { x_grid ; y_grid }, method);
end

%--- Get the dimensions of the output mesh
nx = length(x_grid);
ny = 1;
if ~isempty(d2),
  ny = length(y_grid);
end
nv = length(x_col) + length(y_col) + length(cols_1d) + length(cols_2d);
d = NaN * ones(ny,nx,nv);

%--- Fill with x-axis data:
for y=1:ny,
  d(y,:,x_col) = x_grid;
end

%--- Fill with y-axis data:
if ~isempty(d2),
  for x=1:nx,
    d(:,x,y_col) = y_grid;
  end
end

%--- Fill with 1-D regridded data:
if ~isempty(d1) & ~isempty(cols_1d),
  for v=cols_1d,
    for y=1:ny,
      c = 1 + find(cols_1d == v);
      d(y,:,v) = d1(:,c);
    end
  end
end

%--- Fill with 2-D regridded data:
if ~isempty(d2) & ~isempty(cols_2d),
  for v=cols_2d,
    c = 2 + find(cols_2d == v);
    d(:,:,v) = d2(:,:,c);
  end
end

%--- Write the output:
fid = fopen(out_file, 'w');
for x=1:nx,
  for y=1:ny,
    for v=1:nv,
      if isnan(d(y,x,v)),
        fprintf(fid, '         1.0E38');
      else
        fprintf(fid, '%15.6f', d(y,x,v));
      end
    end
    fprintf(fid, '\n');
  end
end
fclose(fid);


