function [ fit, z ] = restart_read ( chain_num, gen_num, par_num, ...
  restart_read_filename )

%*****************************************************************************80
%
%% restart_read() reads parameter sample data from a restart file.
%
%  Discussion:
%
%    Only a single generation (presumably the last one) was written to the file.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    22 January 2018
%
%  Author:
%
%    Original FORTRAN90 version by Guannan Zhang.
%    This version by John Burkardt.
%
%  Input:
%
%    integer CHAIN_NUM, the total number of chains.
%    3 <= CHAIN_NUM.
%
%    integer GEN_NUM, the total number of generations.
%    2 <= GEN_NUM.
%
%    integer PAR_NUM, the total number of parameters.
%    1 <= PAR_NUM.
%
%    string RESTART_READ_FILENAME, the name of the restart file.
%
%  Output:
%
%    real FIT(CHAIN_NUM,GEN_NUM), the likelihood of each sample.
%
%    real Z(PAR_NUM,CHAIN_NUM,GEN_NUM), the Markov chain sample data.
%
  restart_unit = fopen ( restart_read_filename );

  if ( restart_unit < 0 ) 
    fprintf ( 1, '\n' );
    fprintf ( 1, 'RESTART_READ - Error!\n' );
    fprintf ( 1, '  Could not open "%s"\n', restart_read_filename );
    error ( 'RESTART_READ - Error!' );
  end

  fit = zeros ( chain_num, gen_num );
  z = zeros ( par_num, chain_num, gen_num );

  line = fgets ( restart_unit );

  j = 0;

  while ( j < chain_num )

    line = fgets ( restart_unit );

    if ( line == -1 )
      fprintf ( 1, '\n' );
      fprintf ( 1, 'RESTART_READ - Error!\n' );
      fprintf ( 1, '  Early termination of data from "%s"\n', restart_read_filename );
      error ( 'RESTART_READ - Error!' );
      break;
    end

    [ x, count ] = sscanf ( line, ' ' );

    if ( count == par_num + 2 )
      j = j + 1;
      k = x(1);
      fit(j,1) = x(2);
      z(1:par_num,j,1) = x(3:par_num + 2);
    end

    end

  end

  fclose ( restart_unit );

  return
end
