function cavity_flow_movie ( scale, normalized )

%*****************************************************************************80
%
%% cavity_flow_movie() creates an animation from flow data.
%
%  Discussion:
%
%    This MATLAB script file reads the Cavity flow data:
%
%      geometry (XY values at nodes, assumed to be in 'xy.txt') 
%      flow (UV values at nodes, in a sequence of files starting with 'up001.txt')
%
%    and plots the velocity vectors (U,V)(X,Y), saving each plot and 
%    making an animation.
%
%    The file plots either the velocity vector field, or the velocity
%    direction field, depending on the value of the internal logical
%    parameter "normalized".
%
%    The MATLAB routine quiver internally scales the vectors, but this
%    can be adjusted by using a value of SCALE that is not 1.
%
%    For the unnormalized case, the velocity field is computed to a fixed
%    scale determined by finding the largest velocity vector over time.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    05 July 2003
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    real SCALE, the scale factor for the arrows.
%
%    logical NORMALIZED, is TRUE if all arrows should be the same size.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'cavity_flow_movie:\n' );
  fprintf ( 1, '  MATLAB/Octave version %s\', version ( ) );
  fprintf ( 1, '  Create avi move from cavity flow data.\n' );
%
%  Count the number of data files available.
%
  nframes = 0;
  upfile = 'up000.txt';

  while ( true )
    upfile = filename_inc ( upfile );
    if ( ~ exist ( upfile ) )
      break;
    end
    nframes = nframes + 1;
  end

  fprintf ( 1, '\n' );
  fprintf ( 1, '  %d data files available.\n', nframes );
%
%  Get the XY coordinates of the nodes.
%
  xy = load ( 'xy.txt' );

  x = xy(:,1);
  y = xy(:,2);
%
%  Thin the data.
%
  thin_factor = 2;
  thin_dex = thin_index ( x, y, thin_factor );
  thin_num = length ( thin_dex );
  x = x(thin_dex);
  y = y(thin_dex);
%
%  For unnormalized plots, you need to make sure that a fixed scale is preserved
%  from step to step.  The only way I can see to do this requires that I 
%  determine the maximum velocity over all time, and then append one extract 
%  node and velocity to the data structure.
%
  if ( ~ normalized )

    upfile = 'up000.txt';
    vnorm_max = 0.0;

    for i = 1 : nframes
      upfile = filename_inc ( upfile );
%     fprintf ( 1, 'Checking %s\n', upfile );
      uv = load ( upfile );
      u = uv(thin_dex,1);
      v = uv(thin_dex,2);
      norm = sqrt ( u.^2 + v.^2 );
      vnorm_max = max ( vnorm_max, max ( norm ) );
    end

    fprintf ( 1, '\n' );
    fprintf ( 1, '  Maximum visible velocity magnitude is %f\n', vnorm_max );

  end
%
%  Set the coordinates of the boundary lines, and an extra set of 
%  lines that will be invisible, but which make some space for the 
%  file name to be displayed within the plot area.
%
  bx1 = [ 0.00, 0.00, 1.00, 1.00, 0.99, 0.99, 0.01, 0.01, 0.00 ];
  by1 = [ 1.00, 0.00, 0.00, 1.00, 1.00, 0.01, 0.01, 1.00, 1.00 ];

  bx2 = [ -0.10,  1.10, 1.10, -0.10, -0.10 ];
  by2 = [ -0.10, -0.10, 1.10,  1.10, -0.10 ];
%
%  Set the name of the 0th (nonexistent) velocity file.
%  All the velocity files with have this format, with the
%  numeric part of the name incremented to get the next one.
%  In particular, the first velocity file is called UP001.TXT.
%
  upfile = 'up000.txt';
%
%  Initialize the movie.
%
  colormap ( 'jet' );

  moviename = 'cavity_flow_movie.avi';
  V = VideoWriter ( moviename, 'Motion JPEG AVI' );
  V.FrameRate = 30;
  open ( V );

  figure ( 1 );
  for i = 1 : nframes

    upfile = filename_inc ( upfile );
%   fprintf ( 1, '  Processing data file %s\n', upfile );
    uv = load ( upfile );
    u = uv(thin_dex,1);
    v = uv(thin_dex,2);

    if ( normalized )
      norm = sqrt ( u.^2 + v.^2 );
      nonzero = find ( norm ~= 0.0 );
      u(nonzero) = u(nonzero) ./ norm(nonzero);
      v(nonzero) = v(nonzero) ./ norm(nonzero);
      vnorm_max = 1.0;
    end

    x(thin_num+1) = 0.00;
    y(thin_num+1) = 1.05;
    u(thin_num+1) = vnorm_max;
    v(thin_num+1) = 0.0;
%
%  Make next plot.
%
    clf ( );
    quiver ( x, y, u, v, scale );
%
%  Draw the boundary lines, and the invisible bounding line.
%
    line ( bx1, by1, 'color', 'r' );
    line ( bx2, by2, 'color', 'w' );
    axis ( 'equal' );
    if ( normalized )
      title ( 'Cavity Direction Field' );
    else
      title ( 'Cavity Flow Field' );
    end
    text ( 0.420, 1.03, upfile );

    frame = getframe ( );
    writeVideo ( V, frame );

  end

  close ( V );
  fprintf ( 1, '  Movie saved as "%s"\n', moviename );
%
%  Terminate.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'cavity_flow_movie:\n' );
  fprintf ( 1, '  Normal end of execution.\n' );

  return
end

