#!/usr/local/bin/perl
#
# Master program to build up statistics of P(k) from spatially varying
# A(x).
# Makes source distributions, computes spectra and thus P(k),
# saves results, iterates.
#
#

$power     = "./fluxpk_src";
$source    = "/f/mwhite/work/lyasim/jfluct/sources";
$boxside   = 25.0;
$nout      = 5;
$niter     = 50;

  @zz     = (8.50, 6.00, 5.50, 5.00, 4.00);
  for ($iout=0; $iout<$nout; $iout++) {
    @taueff[$iout] = 0.0028*exp(3.46*log(1+@zz[$iout]));
  }
  @r0     = (100., 11.6, 23.9, 49.1, 179.);
  @n0     = (100., 0.50, 5.10, 49.0, 3190);

  $iout = 2;

  # Extract the positions of the most massive 15 sources.
  $cmd = sprintf "grep -v \\\# pm_%d.prop | sort -k 2 -n -r",$iout;
  $cmd = $cmd." | head -15 | awk \'{printf \"\%.4f \%.4f \%.4f\\n\"";
  $cmd = $cmd.",\$5,\$6,\$7}\' > source.tmp\n";
  print "$cmd\n"; $output=`$cmd`;
  if ($output) {print "$output";}

  for ($iter=0; $iter<$niter; $iter++) {

      # Produce a list of sources, and luminosities.
      $cmd = sprintf "%s %.2f %.2f %.2f source.tmp > source_%03d.lst",
        $source,@r0[$iout],@n0[$iout],$boxside,$iter;
      print "$cmd\n"; $output=`$cmd`;
      if ($output) {print "$output";}

      # Compute the power spectrum from this source list.
      $cmd = sprintf "%s del_%d.dat vel_%d.dat source_%03d.lst %.2f %.2f %.2f ",
        $power,$iout,$iout,$iter,@zz[$iout],@taueff[$iout],@r0[$iout];
      $cmd = $cmd.sprintf "> fluxpk_%d_%03d_src.dat\n",$iout,$iter;
      print "$cmd\n"; $output=`$cmd`;
      if ($output) {print "$output";}

      # Save the A(x) files.
      $cmd = sprintf "mv ax.bin fluxpk_%d_%03d_src.ax.dat\n",$iout,$iter;
      print "$cmd\n"; $output=`$cmd`;
      if ($output) {print "$output";}
  }
#

  `chmod og+r \*`;
