%  1.  NAME
%
%  contotbl - Translate a contour file in table
%
%  2.  USAGE
%
%  contotbl
%
%  3.  DESCRIPTION
%
%  This Matlab script reads an ascii contour data file, keeping only
%  specified x, y, z1, z2, ... data. These are interpolated onto a
%  specified y-grid, which is common to each profile. The results are
%  written in a new ascii data file, in table .
%
%  Columns in the contour data file do not need to be ordered as
%  described below (i.e. x, y, z values may be located in different
%  columns, and x column does not need to contain the least varying
%  values). Reordering of data is carried by the script.
%
%  See below for a description of the invloved file formats.
%
%  4.  PARAMETERS
%
%  The following parameters must be edited:
%
%     contour_file
%        Name of input file containing contour data.
%
%     table_file
%        Name of output file for table data.
%
%     x  Number of column for x variable in contour file.
%
%     y  Number of column for y variable in contour file.
%
%     z  Vector of column numbers for z variables in contour file.
%
%     yi Common y-grid points for table .
%
%     fmt
%        Format for writting values in table file.
%
%     sep
%        Separator between columns in output file.
%
%  5.  CONTOUR FORMAT
%
%  The input contour  is equivalent of as the one generated by the
%  contour product of adcpsect: the first and second columns contain x
%  and y data, while remaining columns contain z data for the different
%  listed variables. Each line defines a grid point. This  has the
%  advantage that grid points do not need to be on a regularly spaced
%  grid. Hence, a typical contour file will look like:
%  ______________________________________________________________________
%        x(1)   y(1,1)      z1(1,1)      z2(1,1)    ... zQ(1,1)
%        x(1)   y(1,2)      z1(1,2)      z2(1,2)    ... zQ(1,2)
%        ...
%        x(1)   y(1,M(1))   z1(1,M(1))   z2(1,M(1)) ... zQ(1,M(1))
%        x(2)   y(2,1)      z1(2,1)      z2(2,1)    ... zQ(2,1)
%        ...
%        x(N)   y(N,M(N))   z1(N,M(N))   z2(N,M(N)) ... zQ(N,M(N))
%  ______________________________________________________________________
%
%  where N is the number of profiles and M(N) the number of cell depths
%  within each profile.
%
%  6.  TABLE FORMAT
%
%  In table , grid points must be regularly spaced. The first columns
%  contains all different y values. All other data on the line are z
%  values corresponding to the different x locations (normally sorted in
%  ascending order). If several z variables were selected from the input
%  contour file, these will appear in the same order for each x location.
%  The corresponding table file from the previous example will look as
%  follows:
%
%  ______________________________________________________________________
%        y(1)   z1(1,1)  z2(1,1) ... zQ(1,1) z1(2,1) ... zQ(N,1)
%        ...
%        y(M)   z1(1,M)  z2(1,M) ... zQ(1,M) z1(2,1) ... zQ(N,M)
%  ______________________________________________________________________
%
%  where index (i,j) corresponds to z value at position i and depth j.
%  This assumes that depths values within profiles are identical. In
%  order to be complete, this description must specifiy the xlocation
%  separately or at the beginning of the file.
%
%  7.  AUTHORS
%
%  Pierre Jaccard, Geophysical Institute, University of Bergen, 1999
%

%------------------------------------------------------------------
% NAME
%
%   contotbl - Translate a contour file in table format
%
% DESCRIPTION
%
%   This is an equivalent to the Perl script con2tbl, but works within Matlab.
%   It allows more operations to be performed on the data before writting them
%   into table format, such as interpolation of profiles on common grid points. 
%
%   This script reads an ascii contour data file, keeping only specified x, y,
%   z1, z2, ... data. These are interpolated onto a specified y-grid, which is
%   common to each profile. The results are written in a new ascii data file, in
%   table format.
%
%   Columns in the contour data file do not need to be ordered as described
%   below (i.e. x, y, z values may be located in different columns, and x column
%   does not need to contain the least varying values). Reordering of data is
%   carried by the script. 
%
% CONTOUR FORMAT
%
%   Ascii data files in contour format have successive profiles concatenated,
%   yielding the following structure:
%
%      x(1)   y(1,1)      z1(1,1)      z2(1,1)    ... zQ(1,1)
%      x(1)   y(1,2)      z1(1,2)      z2(1,2)    ... zQ(1,2)
%      ...
%      x(1)   y(1,M(1))   z1(1,M(1))   z2(1,M(1)) ... zQ(1,M(1))
%      x(2)   y(2,1)      z1(2,1)      z2(2,1)    ... zQ(2,1)
%      ...
%      x(N)   y(N,M(N))   z1(N,M(N))   z2(N,M(N)) ... zQ(N,M(N))
%
%   where N is the number of profiles and M(N) the number of cell depths within
%   each profile.
%
% TABLE FORMAT
%
%   Ascii data files in table format have the following structure:
%
%      y(1)   z1(1,1)  z2(1,1) ... zQ(1,1) z1(2,1) ... zQ(N,1)
%      ...
%      y(M)   z1(1,M)  z2(1,M) ... zQ(1,M) z1(2,1) ... zQ(N,M)
%
%   where index (i,j) corresponds to z value at position i and depth j. This
%   assumes that depths values within profiles are identical.
%
% PARAMETERS
%
%   The following parameters must be edited:
%
%   contour_file
%      Name of file containing contour data.
%
%   table_file
%      Name of file for table data.
%
%   x
%      Number of column for x variable in contour file.
%
%   y
%      Number of column for y variable in contour file.
%
%   z
%      Vector of column numbers for z variables in contour file.
%
%   yi
%      Common y-grid points for table format.
%
%   fmt
%      Format for writting values in table file.
%
%   sep
%      Separator between columns.
%
%------------------------------------------------------------------

% Edit the following parameters:
%------------------------------------------------------------------

contour_file = 'vgeo.con';
table_file   = 'vgeo.tbl';
x            = 1;
y            = 4;
z            = 5;
yi           = 2:286;
fmt          = '%12.6f';
sep          = ' ';

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

%--- Update format:
fmt = [ fmt sep ];
%--- Read data:
data = read_asc(contour_file);

%--- Create xyz matrices for each z variable:
ii = 1:length(z);
for i=ii,
	iz = z(i);
	[xx{i}, yy{i}, zz{i}] = contoxyz(data(:,[x,y,iz]), 3, yi);
end

%--- Write the table file:
ix = 1:length(xx{1});
iy = 1:length(yy{1});
fid = fopen(table_file, 'w');
fprintf(fid,'%%');
fprintf(fid, fmt, xx{1});
fprintf(fid, '\n');
for k=iy,
	y = yy{1}(k);
	fprintf(fid, fmt, y);
	for j=ix,
		for i=ii,
			z = zz{i}(k,j);
			fprintf(fid, fmt, z);
		end
	end
	fprintf(fid, '\n');
end
fclose(fid);







