anchor.diff:  use random, only: iseed, timeseed
   
   use anneal_post, only: anneal_log_file, &
  +                       lu_log_file, &
                          burnin_iterations, &
                          best_output_file, &
                          plocked_output_file, &
  
positive.code: endif
  
  ! Open the log file
  open(unit=21,file=anneal_log_file,status='old')
  
  
  !----
  ! Read annealing log file
  !----
  ! Set search variables before reading
  nit = 0
  nflt = 0
  npoles = 0
  log_format = ''
  
  ! Parse log file header
  continueReadingHeader = .true.
  do while (continueReadingHeader)
  
      read(21,'(A)',end=4001,iostat=ios) input_line
  
      ! All header lines start with a "#" in the first character and have the format:
      !     # label=value
      ! Where "label" describes a parameter, and "value" is a number or string for the parameter
  
      if (index(input_line,'#').ne.1) then
          ! Not a header line; finished parsing header
          continueReadingHeader = .false.
          backspace(21)
  
      else
  
          ! Found header line; parse parameter and value
          if (index(input_line,'niterations').ne.0) then
              i = index(input_line,'=')
              input_line(1:i) = ''
              read(input_line,*) nit
          elseif (index(input_line,'nfaults').ne.0) then
              i = index(input_line,'=')
              input_line(1:i) = ''
              read(input_line,*) nflt
          elseif (index(input_line,'npoles').ne.0) then
              i = index(input_line,'=')
              input_line(1:i) = ''
              read(input_line,*) npoles
          elseif (index(input_line,'format').ne.0) then
              i = index(input_line,'=')
              input_line(1:i) = ''
              read(input_line,*) log_format
          else
              write(stderr,*) 'anneal_log: read header line:',trim(input_line)
              call usage('But could not parse a parameter')
          endif
      endif
  enddo
  
  4001 if (ios.ne.0) then
      call usage('anneal_post: reached end of file in header section')
  endif
  if (nit.le.burnin_iterations) then
      call usage('anneal_post: number of burn-in iterations is greater than total iterations')
  endif
  
  write(stdout,*) 'anneal_post: finished parsing header'
  write(stdout,*) 'niterations=',nit
  write(stdout,*) 'nfaults=',nflt
  write(stdout,*) 'npoles=',npoles
  write(stdout,*) 'log_format= ',log_format
  
  
  ! Set up arrays
  
negative.code:     i = index(input_line,trim(ch))
      input_line(1:i+len(trim(ch))) = ''
  enddo
  
  return
  end subroutine
  
  !--------------------------------------------------------------------------------------------------!
  
  subroutine read_header(nit,nflt,npoles,log_format)
  
  use io, only: stderr, stdout
  
  use anneal_post, only: lu_log_file, &
                         burnin_iterations
  
  implicit none
  
  ! Arguments
  integer :: nit, nflt, npoles
  character(len=*) ::  log_format
  
  ! Local variables
  logical :: continueReadingHeader
  integer :: i, ios
  character(len=512) :: input_line
  
  
  ! Set search variables before reading
  nit = 0
  nflt = 0
  npoles = 0
  log_format = ''
  
  
  ! Initialize to continue reading header
  continueReadingHeader = .true.
  
  
  do while (continueReadingHeader)
  
      read(lu_log_file,'(A)',end=4001,iostat=ios) input_line
  
      ! All header lines start with a "#" in the first character and have the format:
      !     # label1=value1 label2=value2 ...
      ! Where "label" describes a parameter, and "value" is a number or string for the parameter
  
      if (index(input_line,'#').ne.1) then
          ! Not a header line; finished parsing header
          continueReadingHeader = .false.
          backspace(lu_log_file)
  
      else
  
          ! Found header line; parse parameter and value
          if (index(input_line,'niterations').ne.0) then
              i = index(input_line,'=')
              input_line(1:i) = ''
              read(input_line,*) nit
  
          elseif (index(input_line,'nfaults').ne.0) then
              i = index(input_line,'=')
              input_line(1:i) = ''
              read(input_line,*) nflt
  
          elseif (index(input_line,'npoles').ne.0) then
              i = index(input_line,'=')
              input_line(1:i) = ''
              read(input_line,*) npoles
  
          elseif (index(input_line,'format').ne.0) then
              i = index(input_line,'=')
              input_line(1:i) = ''
              read(input_line,*) log_format
  
          else
              write(stderr,*) 'anneal_log: read header line:',trim(input_line)
              call usage('But could not parse a parameter')
          endif
  
      endif
  
  enddo
  
  4001 if (ios.ne.0) then
      call usage('anneal_post: reached end of file in header section')
  endif
  if (nit.le.burnin_iterations) then
      call usage('anneal_post: number of burn-in iterations is greater than total iterations')
  endif
  
  if (nit.eq.0) then
      call usage('anneal_post: read zero iterations in log file')
  endif
  if (nflt.eq.0) then
      call usage('anneal_post: read zero faults in log file')
  endif
  
  write(stdout,*) 'anneal_post: finished parsing header'
  write(stdout,*) 'niterations=',nit
  write(stdout,*) 'nfaults=',nflt
  write(stdout,*) 'npoles=',npoles
  write(stdout,*) 'log_format= ',log_format
  
  return
  end subroutine
  