#!/usr/bin/perl
#
# Generate a Perl script which will run 
#   Gnuplot repeatedly to make a set of 
#   GIF images which can be animated to make
#   a movie illustrating reverberation.
#
# MWR 3/28/2013

$debug = 1;


# first, the ray going straight from source to observer
$cmdfile = "draw_reverb.gnu";
$term_options = "postscript enhanced font 'Helvectica,18' size 10,12 ";
$base = "reverb_direct";
$a_time = 30;
$extra_images = 25;

$minx = -250;
$maxx = 250;
$miny = -50;
$maxy = 450;


for ($i = 0; $i <= $a_time; $i++) {
  $index = $i;
  $output_file = sprintf "%s_%02d.ps", $base, $i;

  open(CMDFILE, ">$cmdfile") || die("can't open file $cmdfile");
  printf CMDFILE "set term $term_options \n";
  printf CMDFILE "set output '$output_file' \n";
  printf CMDFILE "set size 1.0,1.0 \n";
  printf CMDFILE "set origin 0.0,0.0 \n";
  printf CMDFILE "set multiplot  \n";

  # upper panel has picture of the events, bird's-eye-view
  printf CMDFILE "set size 1.0,0.6666 \n";
  printf CMDFILE "set origin 0.0,0.3333 \n";
  printf CMDFILE "set size square \n";
  printf CMDFILE "set grid \n";
  printf CMDFILE "set nokey \n";
  printf CMDFILE "set xlabel '' \n";
  printf CMDFILE "set ylabel '' \n";

  $cmd  = "plot [$minx:$maxx][$miny:$maxy] './calc_reverb_observer.out' ";
  $cmd .= "   using 2:3 with points ps 3 pt 6 lw 2  t '' ";
  $cmd .= " , ";
  $cmd .= "                                './calc_reverb_source.out' ";
  $cmd .= "   using 2:3 with points ps 3 pt 7 lw 2 lc -1 t '' ";
  $cmd .= " , ";
  $cmd .= "        './calc_reverb_a.out' index $index ";
  $cmd .= "   using 2:3 with points ps 0.8 pt 3 lw 2 lc 3 t '' ";
  printf CMDFILE "$cmd \n";

  
  # lower panel has the light curve
  printf CMDFILE "set size 1.0,0.3333 \n";
  printf CMDFILE "set origin 0.0,0.0 \n";
  printf CMDFILE "set size nosquare \n";
  $cmd =  " plot [0:($a_time+$extra_images)*10][-0.2:1.5] ";
  $cmd .= " './calc_reverb_a_curve.out' index $index ";
  $cmd .= "   using 1:2 with linespoints ps 1.5 pt 5 lw 2 lc -1 t '' ";
  printf CMDFILE "$cmd \n";


  printf CMDFILE "quit \n";
  close(CMDFILE) ;

  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;

  if ($term_options =~ /postscript/) {
    $psfile = $output_file;
    $giffile = $psfile;
    $giffile =~ s/.ps/.png/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }

}
# and now we add extra images, which show no light (the light
#   from brief outburst has passed the observer)
for ($j = 0; $j < $extra_images; $j++) {
  $index = $i + $j;
  $output_file = sprintf "%s_%02d.ps", $base, $i + $j;

  open(CMDFILE, ">$cmdfile") || die("can't open file $cmdfile");
  printf CMDFILE "set term $term_options \n";
  printf CMDFILE "set output '$output_file' \n";
  printf CMDFILE "set size 1.0,1.0 \n";
  printf CMDFILE "set origin 0.0,0.0 \n";
  printf CMDFILE "set multiplot  \n";

  # upper panel has picture of the events, bird's-eye-view
  printf CMDFILE "set size 1.0,0.6666 \n";
  printf CMDFILE "set origin 0.0,0.3333 \n";
  printf CMDFILE "set size square \n";
  printf CMDFILE "set grid \n";
  printf CMDFILE "set nokey \n";
  printf CMDFILE "set xlabel '' \n";
  printf CMDFILE "set ylabel '' \n";

  $cmd  = "plot [$minx:$maxx][$miny:$maxy] './calc_reverb_observer.out' ";
  $cmd .= "   using 2:3 with points ps 3 pt 6 lw 2  t '' ";
  $cmd .= " , ";
  $cmd .= "                                './calc_reverb_source.out' ";
  $cmd .= "   using 2:3 with points ps 3 pt 7 lw 2 lc -1 t '' ";
  printf CMDFILE "$cmd \n";


  # lower panel has the light curve
  printf CMDFILE "set size 1.0,0.3333 \n";
  printf CMDFILE "set origin 0.0,0.0 \n";
  printf CMDFILE "set size nosquare \n";
  $cmd =  " plot [0:($a_time+$extra_images)*10][-0.2:1.5] ";
  $cmd .= " './calc_reverb_a_curve.out' index $index ";
  $cmd .= "   using 1:2 with linespoints ps 1.5 pt 5 lw 2 lc -1 t '' ";
  printf CMDFILE "$cmd \n";



  printf CMDFILE "quit \n";
  close(CMDFILE) ;

  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;

  if ($term_options =~ /postscript/) {
    $psfile = $output_file;
    $giffile = $psfile;
    $giffile =~ s/.ps/.png/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }

}






# now, the ray going straight from source to circle
$cmdfile = "draw_reverb.gnu";
$term_options = "postscript enhanced font 'Helvectica,18' size 10,12";
$base = "reverb_bounce";


$minx = -250;
$maxx = 250;
$miny = -50;
$maxy = 450;

for ($i = 0; $i < 10; $i++) {
  $index = $i;
  $output_file = sprintf "%s_%02d.ps", $base, $i;

  open(CMDFILE, ">$cmdfile") || die("can't open file $cmdfile");
  printf CMDFILE "set term $term_options \n";
  printf CMDFILE "set output '$output_file' \n";
  printf CMDFILE "set size 1.0,1.0 \n";
  printf CMDFILE "set origin 0.0,0.0 \n";
  printf CMDFILE "set multiplot  \n";

  # upper panel has picture of the events, bird's-eye-view
  printf CMDFILE "set size 1.0,0.6666 \n";
  printf CMDFILE "set origin 0.0,0.3333 \n";
  printf CMDFILE "set size square \n";
  printf CMDFILE "set grid \n";
  printf CMDFILE "set nokey \n";
  printf CMDFILE "set xlabel '' \n";
  printf CMDFILE "set ylabel '' \n";

  $cmd  = "plot [$minx:$maxx][$miny:$maxy] './calc_reverb_observer.out' ";
  $cmd .= "   using 2:3 with points ps 3 pt 6 lw 2  t '' ";
  $cmd .= " , ";
  $cmd .= "                                './calc_reverb_source.out' ";
  $cmd .= "   using 2:3 with points ps 3 pt 7 lw 2 lc -1 t '' ";
  $cmd .= " , ";
  $cmd .= "                                './calc_reverb_circle.out' ";
  # if the light has not yet reached the ring, draw ring with light color
  if ($i < 9) {
    $cmd .= "   using 1:2 with points ps 0.3 pt 3 lw 1 lc 1 t '' ";
  }
  else {
    # but if the light HAS reached the ring, draw ring with heavy color
    $cmd .= "   using 1:2 with points ps 0.5 pt 3 lw 6 lc 1 t '' ";
  }
  $cmd .= " , ";
  $cmd .= "        './calc_reverb_b.out' index $index ";
  $cmd .= "   using 2:3 with points ps 0.8 pt 3 lw 2 lc 3 t '' ";
  printf CMDFILE "$cmd \n";


  # lower panel has the light curve
  printf CMDFILE "set size 1.0,0.3333 \n";
  printf CMDFILE "set origin 0.0,0.0 \n";
  printf CMDFILE "set size nosquare \n";
  $cmd =  " plot [0:($a_time+$extra_images)*10][-0.2:1.5] ";
  $cmd .= " './calc_reverb_a_curve.out' index $index ";
  $cmd .= "   using 1:2 with linespoints ps 1.5 pt 5 lw 2 lc -1 t '' ";
  printf CMDFILE "$cmd \n";






  printf CMDFILE "quit \n";
  close(CMDFILE) ;

  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;

  if ($term_options =~ /postscript/) {
    $psfile = $output_file;
    $giffile = $psfile;
    $giffile =~ s/.ps/.png/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }

}


# now, the ray going straight from circle to observer
$cmdfile = "draw_reverb.gnu";
$term_options = "postscript enhanced font 'Helvectica,18' size 10,12 ";
$base = "reverb_bounce";


$minx = -250;
$maxx = 250;
$miny = -50;
$maxy = 450;

for ($i = 0; $i < 40; $i++) {
  $index = $i + 10;
  $output_file = sprintf "%s_%02d.ps", $base, $index;

  open(CMDFILE, ">$cmdfile") || die("can't open file $cmdfile");
  printf CMDFILE "set term $term_options \n";
  printf CMDFILE "set output '$output_file' \n";
  printf CMDFILE "set size 1.0,1.0 \n";
  printf CMDFILE "set origin 0.0,0.0 \n";
  printf CMDFILE "set multiplot  \n";

  # upper panel has picture of the events, bird's-eye-view
  printf CMDFILE "set size 1.0,0.6666 \n";
  printf CMDFILE "set origin 0.0,0.3333 \n";
  printf CMDFILE "set size square \n";
  printf CMDFILE "set grid \n";
  printf CMDFILE "set nokey \n";
  printf CMDFILE "set xlabel '' \n";
  printf CMDFILE "set ylabel '' \n";

  $cmd  = "plot [$minx:$maxx][$miny:$maxy] './calc_reverb_observer.out' ";
  $cmd .= "   using 2:3 with points ps 3 pt 6 lw 2  t '' ";
  $cmd .= " , ";
  $cmd .= "                                './calc_reverb_source.out' ";
  $cmd .= "   using 2:3 with points ps 3 pt 7 lw 2 lc -1 t '' ";
  $cmd .= " , ";
  $cmd .= "                                './calc_reverb_circle.out' ";
  $cmd .= "   using 1:2 with points ps 0.3 pt 3 lw 1 lc 1 t '' ";
  $cmd .= " , ";
  $cmd .= "        './calc_reverb_c.out' index $i ";
  $cmd .= "   using 2:3 with points ps 0.8 pt 3 lw 2 lc 1 t '' ";
  printf CMDFILE "$cmd \n";


  # lower panel has the light curve
  printf CMDFILE "set size 1.0,0.3333 \n";
  printf CMDFILE "set origin 0.0,0.0 \n";
  printf CMDFILE "set size nosquare \n";
  $cmd =  " plot [0:($a_time+$extra_images)*10][-0.2:1.5] ";
  $cmd .= " './calc_reverb_c_curve.out' index $index-10 ";
  $cmd .= "   using (\$1):2 with linespoints ps 1.5 pt 5 lw 2 lc -1 t '' ";
  printf CMDFILE "$cmd \n";






  printf CMDFILE "quit \n";
  close(CMDFILE) ;

  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;

  if ($term_options =~ /postscript/) {
    $psfile = $output_file;
    $giffile = $psfile;
    $giffile =~ s/.ps/.png/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }

}
# and finally, a few extra frames with no more light (the light from
#   outburst has passed observer)
for ($i = 0; $i < 5; $i++) {
  $index = $i + 50;
  $output_file = sprintf "%s_%02d.ps", $base, $index;

  open(CMDFILE, ">$cmdfile") || die("can't open file $cmdfile");
  printf CMDFILE "set term $term_options \n";
  printf CMDFILE "set output '$output_file' \n";
  printf CMDFILE "set size 1.0,1.0 \n";
  printf CMDFILE "set origin 0.0,0.0 \n";
  printf CMDFILE "set multiplot  \n";

  # upper panel has picture of the events, bird's-eye-view
  printf CMDFILE "set size 1.0,0.6666 \n";
  printf CMDFILE "set origin 0.0,0.3333 \n";
  printf CMDFILE "set size square \n";
  printf CMDFILE "set grid \n";
  printf CMDFILE "set nokey \n";
  printf CMDFILE "set xlabel '' \n";
  printf CMDFILE "set ylabel '' \n";

  $cmd  = "plot [$minx:$maxx][$miny:$maxy] './calc_reverb_observer.out' ";
  $cmd .= "   using 2:3 with points ps 3 pt 6 lw 2  t '' ";
  $cmd .= " , ";
  $cmd .= "                                './calc_reverb_source.out' ";
  $cmd .= "   using 2:3 with points ps 3 pt 7 lw 2 lc -1 t '' ";
#  $cmd .= " , ";
#  $cmd .= "                                './calc_reverb_circle.out' ";
#  $cmd .= "   using 1:2 with points ps 0.3 pt 3 lw 1 lc 1 t '' ";
  printf CMDFILE "$cmd \n";


  # lower panel has the light curve
  printf CMDFILE "set size 1.0,0.3333 \n";
  printf CMDFILE "set origin 0.0,0.0 \n";
  printf CMDFILE "set size nosquare \n";
  $cmd =  " plot [0:($a_time+$extra_images)*10][-0.2:1.5] ";
  $cmd .= " './calc_reverb_c_curve.out' index $index-10 ";
  $cmd .= "   using (\$1):2 with linespoints ps 1.5 pt 5 lw 2 lc -1 t '' ";
  printf CMDFILE "$cmd \n";






  printf CMDFILE "quit \n";
  close(CMDFILE) ;


  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;

  if ($term_options =~ /postscript/) {
    $psfile = $output_file;
    $giffile = $psfile;
    $giffile =~ s/.ps/.png/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }

}








exit 0;
