#!/usr/bin/perl
#

# create simple datafile with waves in phase and out of phase
$data_file = "wave_phase.dat";
open(DATAFILE, ">$data_file") || die("can't open file $data_file");
$start_x = -30.0;
$end_x =  10.0;
$d_x = 0.05;
for ($x = $start_x; $x <= $end_x; $x += $d_x) {
 
  $y1 = sin($x);
  $y2 = $y1 + 0.5;
  $y3 = sin($x - (2.0*3.14159)/5.0);
  $y4 = $y3 + 0.5;

  printf DATAFILE " %10.4f %6.3f %6.3f %6.3f %6.3f \n",
              $x, $y1, $y2, $y3, $y4;

}
close(DATAFILE);


# Make plots showing two waves in phase.
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "waves_in_phase.ps";
  $term_options = "postscript color enhanced ";

  $cmdfile = "gnuplot.in";
  
  open (CMDFILE, ">$cmdfile") || die("can't open $cmdfile for writing");
  printf CMDFILE "set output '$output_file' \n";
  printf CMDFILE "set term $term_options \n";
  printf CMDFILE "set grid \n";
  printf CMDFILE "set key top right \n";
  printf CMDFILE "set size square \n";
  printf CMDFILE "set xlabel '' \n";
  printf CMDFILE "set ylabel '' \n";
  printf CMDFILE "set title 'Two waves moving in phase '\n";
  
  $cmd = "plot [-5:12][-1.5:2.0] ";
  $cmd .= " '$data_file' using 1:2 with linespoints lt 1 lw 5 t ''  ";
  $cmd .= " , ";
  $cmd .= " '$data_file' using 1:3 with linespoints ps 1 pt 3 lw 3 t ''  ";
 

  printf CMDFILE "$cmd \n";
  printf CMDFILE "set output \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/.eps/.gif/;
    $giffile =~ s/.ps/.gif/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }


# Make plots showing two waves out of phase.
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "waves_out_phase.ps";
  $term_options = "postscript color enhanced ";

  $cmdfile = "gnuplot.in";
  
  open (CMDFILE, ">$cmdfile") || die("can't open $cmdfile for writing");
  printf CMDFILE "set output '$output_file' \n";
  printf CMDFILE "set term $term_options \n";
  printf CMDFILE "set grid \n";
  printf CMDFILE "set key top right \n";
  printf CMDFILE "set size square \n";
  printf CMDFILE "set xlabel '' \n";
  printf CMDFILE "set ylabel '' \n";
  printf CMDFILE "set title 'Two waves moving out of phase '\n";
  
  $cmd = "plot [-5:12][-1.5:2.0] ";
  $cmd .= " '$data_file' using 1:2 with linespoints lt 1 lw 5 t ''  ";
  $cmd .= " , ";
  $cmd .= " '$data_file' using 1:5 with linespoints ps 1 pt 3 lw 3 t ''  ";
 

  printf CMDFILE "$cmd \n";
  printf CMDFILE "set output \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/.eps/.gif/;
    $giffile =~ s/.ps/.gif/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }




exit 0;

