function claw_pix ( q_file_name )

%% CLAW_PIX creates PNG files from a sequence of CLAWPACK 1D datasets.
%
%  Discussion:
%
%    This program can be used to turn a sequence of 1D CLAWPACK solution
%    files into a sequence of PNG images, suitable for an animation.
%
%    Although this program can handle a single input file, its most useful
%    application is in processing many files, with 'consecutive' names, such
%    as "fort.q0000", "fort.q0001", "fort.q0002" and so on.  The user only
%    has to tell the program to process the first file, and it will 
%    continue to process files until it reaches the end of the sequence 
%    of names.
%
%    The input files are presumed to be the sequence of "fort.qXXXX" files
%    or "Q files" created by a run of CLAWPACK on a 1-dimensional problem.
%
%    We are assuming that the equation is scalar.
%    We are assuming that the data is computed at equally spaced points.
%
%    For the scalar case, each Q file is assumed to have the following format:
%
%      grid number (an integer, and we ignore it)
%      AMR_level   (an integer, and we ignore it)
%      mx          (the number of points at which data is computed.)
%      xlow        (the left corner of cell 1)
%      dx          (the width of each cell.)
%                  (a blank line)
%      Q1          (the value of Q at cell center 1.)
%      ...
%      Qmx         (the value of Q at cell center MX).
%
%    For the vector case, the Q file has the same form, except that each line
%    of Q data contains the value of 2 or more functions at the cell center.
%    For the case where the solution has two components, this might be:
%
%      Q1,1 Q1,2        (the values of Q at cell center 1.)
%      ...
%      Qmx,1 Qmx,2      (the values of Q at cell center MX).
%
%    The program first reads every data file, in order to find the
%    maximum and minimum Q values for each component.  In this way, 
%    each frame of the sequence is shot on the same scale, and so an 
%    animation is meaningful.  
%
%    (As far as I can tell, this is NOT true of the graphics routines
%    that come with CLAWPACK.  In particular, each frame is scaled
%    independently.  Accidentally, you might not notice this, since 
%    the axes are rounded up to whole numbers, so small changes in
%    the data range will not cause the scale of successive pictures
%    to change.  But if the data decays by 10 or 15 percent, for 
%    instance, the scale of the picture will suddenly jump.  An 
%    animation of a sequence of such frames, using improperly scaled
%    data, is misleading, confusing, and obviously and needlessly wrong.)
%
%    Then, each file is opened and read, and a plot of the data is
%    displayed, and stored into a PNG file.
%
%    For vector problems, all components are displayed on the same plot,
%    with the same line color.  If I thought about it, I could figure
%    out how to cycle through the colors properly.
%
%    It is easy to turn the PNG files into an animation, using
%    QuickTime Pro, for instance.
%
%  Usage:
%
%    claw_pix ( u_file_name )
%
%    Note that a literal filename must be enclosed in single quotes.
%    Thus, a typical invocation might be
%
%      claw_pix ( 'fort.q0000' )
%
%    and since the parentheses are optional, you can also say
%
%      claw_pix 'fort.q0000'
%
%    If you simply say 
%
%      claw_pix
%
%    the program will give you a chance to enter the filename interactively.
%
%  Modified:
%
%    13 April 2006
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, string Q_FILE_NAME, the name of the (first) Q input file.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'CLAW_PIX:\n' );
  fprintf ( 1, '  Display 1D data from CLAWPACK.\n' );
%
%  If there is no command line argument, we need to ask for it.
%
  if ( nargin < 1 )

    fprintf ( 1, '\n' );
    fprintf ( 1, 'CLAW_PIX:\n' );
    q_file_name = input ( '  Enter the name of the (first) Q data file:' );

  end

  if ( ~file_exist ( q_file_name ) )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'CLAW_PIX - Fatal error!\n' );
    fprintf ( 1, '  The file "%s" does not exist.\n', q_file_name );
    return
  end
%
%  Save the name of the first input file, and set the name of the first PNG file.
%
  q_root = q_file_name;
%
%  The PNG file names will be the same as the Q file names, but with ".png"
%  appended.
%
  png_root = q_root;
  png_length = length ( png_root );
  png_root(png_length+1:png_length+4) = '.png';
  
  fprintf ( 1, '\n' );
  fprintf ( 1, '  First PNG file will be %s\n', png_root );
%
%  Read the data in the first file.
%
  q_unit = fopen ( q_file_name );

  if ( q_unit < 0 )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'CLAW_PIX - Fatal error!\n' );
    fprintf ( 1, '  Could not open "%s".\n', q_file_name );
  end
  
  line = fgets ( q_unit );
  grid_number = sscanf ( line, '%d' );
  line = fgets ( q_unit );
  AMR_level = sscanf ( line, '%d' );
  line = fgets ( q_unit );
  mx = sscanf ( line, '%d' );
  line = fgets ( q_unit );
  xlow = sscanf ( line, '%f' );
  line = fgets ( q_unit );
  dx = sscanf ( line, '%f' );
  line = fgets ( q_unit );
  line = fgets ( q_unit );
  dim_num = s_word_count ( line )

  fprintf ( 1, '\n' );
  if ( dim_num == 1 )
    fprintf ( 1, '  This is a SCALAR problem.\n' );
  else
    fprintf ( 1, ...
      '  This is a VECTOR problem with %d solution components.\n', ...
      dim_num );
  end

  fclose ( q_unit );
%
%  Determine grid information.
%
  xhigh = xlow + ( 2 * mx - 1 ) * dx / 2;
  x(1:mx) = linspace ( xlow + dx / 2, xhigh, mx );
  
  x_min = xlow;
  x_max = xhigh + dx / 2;
%
%  We need to read all the U data once to get the range.
%
  q_max(1:dim_num) = -Inf;
  q_min(1:dim_num) =  Inf;

  q_file_name = q_root;

  while ( 1 )
      
    if ( ~file_exist ( q_file_name ) )
      break
    end

    q_unit = fopen ( q_file_name );

    if ( q_unit < 0 )
      fprintf ( 1, '\n' );
      fprintf ( 1, 'CLAW_PIX - Fatal error!\n' );
      fprintf ( 1, '  Could not open "%s".\n', q_file_name );
    end

    line = fgets ( q_unit );
    line = fgets ( q_unit );
    line = fgets ( q_unit );
    line = fgets ( q_unit );
    line = fgets ( q_unit );
    line = fgets ( q_unit );

    for i = 1 : mx
      line = fgets ( q_unit );
      q(i,1:dim_num) = sscanf ( line, '%f' );
    end

    fclose ( q_unit );

    for j = 1 : dim_num
      q_max(j) = max ( q_max(j), max ( q(1:mx,j) ) );
      q_min(j) = min ( q_min(j), min ( q(1:mx,j) ) );
    end

    q_file_name = file_name_inc ( q_file_name );

  end

  for j = 1 : dim_num
    if ( q_min(j) == q_max(j) )
      q_min(j) = q_max(j) - 0.5;
      q_max(j) = q_max(j) + 0.5;
    end
  end

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Min ( X ) = %f, Max ( X ) = %f\n', x_min, x_max );
  for j = 1 : dim_num
    fprintf ( 1, '  Max ( Q(%d) ) = %f, Max ( Q(%d) ) = %f\n', ...
      j, q_min(j), j, q_max(j) );
  end
%
%  Create the plots.
%
  q_file_name = q_root;
  png_file_name = png_root;

  frame_num = 0;
%
%  Loop over all files.
%
  while ( 1 )
      
    if ( ~file_exist ( q_file_name ) )
      break
    end

    q_unit = fopen ( q_file_name );

    if ( q_unit < 0 )
      fprintf ( 1, '\n' );
      fprintf ( 1, 'CLAW_PIX - Fatal error!\n' );
      fprintf ( 1, '  Could not open "%s".\n', q_file_name );
    end

    line = fgets ( q_unit );
    line = fgets ( q_unit );
    line = fgets ( q_unit );
    line = fgets ( q_unit );
    line = fgets ( q_unit );
    line = fgets ( q_unit );

    for i = 1 : mx
      line = fgets ( q_unit );
      q(i,1:dim_num) = sscanf ( line, '%f' );
    end

    fclose ( q_unit );

    for j = 1 : dim_num
      q(1:mx,j) = ( q(1:mx,j) - q_min(j) ) / ( q_max(j) - q_min(j) );
    end
    
    hold off
    
    for j = 1 : dim_num
      plot ( x, q(1:mx,j) );
      hold on
    end   

    axis ( [ x_min, x_max, 0.0, 1.0 ] );
%
%  Label the axes and the plot.
%
    xlabel ( 'X', 'FontName', 'Helvetica', 'FontWeight', 'bold', ...
      'FontSize', 16 );

    ylabel ( 'Y', 'FontName', 'Helvetica', 'FontWeight', 'bold', ...
      'FontSize', 16, 'Rotation', 0 );

    title ( q_file_name, 'FontName', 'Helvetica', 'FontWeight', ...
      'bold', 'FontSize', 16 );
%
%  Here's where we take a snapshot of the current image, and save it to 
%  a PNG file.  Doing this was surprisingly complicated, and the help
%  system was not very clear.  Surely there's a more straightforward way!
%
    F = getframe;
    [ XX, map ] = frame2im ( F );
    imwrite ( XX, png_file_name, 'PNG' );
    frame_num = frame_num + 1;

    hold off
%
%  Prepare for next iteration.
%
    q_file_name = file_name_inc ( q_file_name );

    if ( ~file_exist ( q_file_name ) )
      break
    end
    
    png_file_name = file_name_inc ( png_file_name );

  end

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Processed %d frames.\n', frame_num );
  fprintf ( 1, '\n' );
  fprintf ( 1, 'CLAW_PIX:\n' );
  fprintf ( 1, '  Normal end of execution.\n' );
