%  1.  NAME
%
%  gcon2con - Create a contour file from a GCON data file
%
%  2.  USAGE
%
%  gcon2con
%
%  3.  DESCRIPTION
%
%  Reads a generalized contour file, usually obtained from xtract(1), and
%  create a contour file with four columns. By specifiying a file with a
%  list of ship heading, you can rotate the vectors so that the positive
%  x-axis is starbord to the ship track, and positive y-axis is along
%  track.  For an explanation of generalized contour files, see
%  con2con(1).
%
%  4.  PARAMETERS
%
%  The following parameters must be edited:
%
%     gcon_file
%        Name of generalized contour file.
%
%     con_file
%        Name for output contour file.
%
%     nav_file
%        Name of a file with time and heading columns for rotation of
%        contour velocities. Set it to the empty string to avoid this.
%
%     t_nav
%        Number of the column in navigation file containing time
%        information.
%
%     h_nav
%        Number of the column in navigation file containing heading
%        information.
%
%     t_gcon
%        Number of column in gcon_file containing time information. This
%        is used to synchronize the generalized contour data with
%        navigation information.
%
%     i_gcon
%        Vector of indexes to columns in gcon_file to print in the output
%        file. To generate a contour output,this will be a list of column
%        numbers like [ x y u v ].
%
%     scale
%        Multiply velocities by this factor.
%
%  5.  AUTHORS
%
%  Pierre Jaccard, Geophysical Institute, University of Bergen, 1999
%

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

gcon_file = 'adcp_fs_abs_detided.rgd';
con_file  = 'adcp_dfs.con';

nav_file  = 'btnav.asc';

i_gcon = [ 3, 4, 5, 6 ];

t_gcon = 1;
h_nav  = 9;
t_nav  = 1;

scale = 100.0;

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

%--- Read_data:
gcon = tonan(read_asc(gcon_file));

%--- Indexes:
uv = i_gcon([3,4]);

%--- Mask with good positions and currents only:
ibad = isnan(gcon);
ibad = sum(ibad,2);
igood = find(~ibad);
gcon=gcon(igood,:);

%--- Get all different times:
ii=2:size(gcon,1);
ii = find(gcon(ii,t_gcon) ~= gcon(ii-1,t_gcon)) + 1;
tt = [ 1 ii ];


%--- Check for rotation along the ship's course:
if isstr(nav_file) & (length(nav_file) > 0),
   %--- Read navigation:
   nav = tonan(read_asc(nav_file));
   %--- Select only time and heading:
   nav = nav(:, [t_nav, h_nav]);
   %--- Mask with good values only:
   ibad = isnan(nav);
   ibad = sum(ibad,2);
   igood = find(~ibad);
   nav = nav(igood,:);
   %--- Get heading for data file times:
   hh = interp1(nav(:,1), nav(:,2), tt);
   %--- Update velocities:          
   ii=1:size(gcon,1);
   for i=ii,
      h = hh(i) * pi/180.0;
      rot = [ cos(h), -sin(h); sin(h), cos(h) ];
      gcon(i,uv) = ( rot*gcon(i,uv)' )';
   end
end

%--- scale velocities:
ii = 1:size(gcon,1);
for i=ii,
   gcon(i,uv) = gcon(i,uv) .* scale;
end

%--- Write the results:
fid = fopen(con_file, 'w');
ii = 1:size(gcon,1);
for i=ii,
   fprintf(fid, '%15.8f', gcon(i, i_gcon));
   fprintf(fid, '\n');
end
fclose(fid);
