#!/usr/bin/perl
#
# Make plot(s) we can use to illustrate superluminal motion
#
# MWR 10/9/2008

$debug = 1;

$DEGTORAD = (3.14159/180.0);

# create datafiles
$min_angle_deg = 0;
$max_angle_deg = 89;
$d_angle_deg = 2;

# slow speed
$v = 0.1;
$slow_speed_file = "./slow_speed_file.dat";
open(OUTFILE, ">$slow_speed_file") || die("can't open file $slow_speed_file for output");
for ($angle_deg = $min_angle_deg; $angle_deg < $max_angle_deg; $angle_deg += $d_angle_deg) {
  $angle_rad = $angle_deg*$DEGTORAD;
  $sine_term = $v * sin($angle_rad);
  $cosine_term = (1.0 - $v*cos($angle_rad));
  if ($cosine_term > 0) {
    $sine_over_cosine = $sine_term / $cosine_term;
  }
  else {
    $sine_over_cosine = 1.0e6;
  }
  printf OUTFILE " %8.3f  %8.3f %8.3f %8.3f \n", 
         $angle_deg, $sine_term, $cosine_term, $sine_over_cosine;
}
close(OUTFILE);

# intermediate speed
$v = 0.5;
$slow_speed_file = "./intermed_speed_file.dat";
open(OUTFILE, ">$slow_speed_file") || die("can't open file $slow_speed_file for output");
for ($angle_deg = $min_angle_deg; $angle_deg < $max_angle_deg; $angle_deg += $d_angle_deg) {
  $angle_rad = $angle_deg*$DEGTORAD;
  $sine_term = $v * sin($angle_rad);
  $cosine_term = (1.0 - $v*cos($angle_rad));
  if ($cosine_term > 0) {
    $sine_over_cosine = $sine_term / $cosine_term;
  }
  else {
    $sine_over_cosine = 1.0e6;
  }
  printf OUTFILE " %8.3f  %8.3f %8.3f %8.3f \n", 
         $angle_deg, $sine_term, $cosine_term, $sine_over_cosine;
}
close(OUTFILE);

# high speed
$v = 0.9;
$slow_speed_file = "./high_speed_file.dat";
open(OUTFILE, ">$slow_speed_file") || die("can't open file $slow_speed_file for output");
for ($angle_deg = $min_angle_deg; $angle_deg < $max_angle_deg; $angle_deg += $d_angle_deg) {
  $angle_rad = $angle_deg*$DEGTORAD;
  $sine_term = $v * sin($angle_rad);
  $cosine_term = (1.0 - $v*cos($angle_rad));
  if ($cosine_term > 0) {
    $sine_over_cosine = $sine_term / $cosine_term;
  }
  else {
    $sine_over_cosine = 1.0e6;
  }
  printf OUTFILE " %8.3f  %8.3f %8.3f %8.3f \n", 
         $angle_deg, $sine_term, $cosine_term, $sine_over_cosine;
}
close(OUTFILE);


# very high speed
$v = 0.99;
$slow_speed_file = "./veryhigh_speed_file.dat";
open(OUTFILE, ">$slow_speed_file") || die("can't open file $slow_speed_file for output");
for ($angle_deg = $min_angle_deg; $angle_deg < $max_angle_deg; $angle_deg += $d_angle_deg) {
  $angle_rad = $angle_deg*$DEGTORAD;
  $sine_term = $v * sin($angle_rad);
  $cosine_term = (1.0 - $v*cos($angle_rad));
  if ($cosine_term > 0) {
    $sine_over_cosine = $sine_term / $cosine_term;
  }
  else {
    $sine_over_cosine = 1.0e6;
  }
  printf OUTFILE " %8.3f  %8.3f %8.3f %8.3f \n", 
         $angle_deg, $sine_term, $cosine_term, $sine_over_cosine;
}
close(OUTFILE);




$min_y = -0.05;
$max_y = 1.10;


#######
# Plot showing quantities for slow speed
#
  $title = "'Behavior of trig functions at small angles for slow jet v=0.1c '";
  $output_file = "./make_plot_a.ps";
  $term_options = "postscript color 'Helvetica' 18 ";

  $cmdfile = "./make_plot_a.gnu";


  # determine the limits of the graph
  #    note that the y-axis is logarithmic
  $minx = $min_angle_deg;
  $maxx = 40;
  $miny = $min_y;
  $maxy = $max_y;
  
  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 xlabel 'Angle theta (degrees) ' \n";
  printf CMDFILE "set ylabel '' \n";
  printf CMDFILE "set title $title\n";
  
  $cmd = "plot [$minx:$maxx][$miny:$maxy] './slow_speed_file.dat' ";
  $cmd .= "      using (\$1):(\$2) with points pt 3 ps 1.5 lw 2  ";
  $cmd .= "      t 'v sin(theta)' ";
  $cmd .= " , ";
  $cmd .= "   './slow_speed_file.dat' ";
  $cmd .= "      using (\$1):(\$3) with points pt 5 ps 1.5 lw 2  ";
  $cmd .= "      t '1 - v cos(theta)' ";
  printf CMDFILE "$cmd \n";
  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;

  # create a GIF copy of any Postscript file
  if ($term_options =~ "postscript") {
    # printf "yes, term_options ..%s.. has postscript \n", $term_options;
    $gif_file = $output_file;
    $gif_file =~ s/.ps$/.gif/;
    $cmd = "convert -rotate 90 $output_file $gif_file ";
    printf "cmd is ..$cmd.. \n";
    $retval = `$cmd`;
  }


#######
# Plot showing quantities for intermediate speed
#
  $title = "'Behavior of trig functions at small angles for intermediate jet v=0.5c '";
  $output_file = "./make_plot_b.ps";
  $term_options = "postscript color 'Helvetica' 18 ";

  $cmdfile = "./make_plot_b.gnu";


  # determine the limits of the graph
  #    note that the y-axis is logarithmic
  $minx = $min_angle_deg;
  $maxx = 40;
  $miny = $min_y;
  $maxy = $max_y;
  
  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 xlabel 'Angle theta (degrees) ' \n";
  printf CMDFILE "set ylabel '' \n";
  printf CMDFILE "set title $title\n";
  
  $cmd = "plot [$minx:$maxx][$miny:$maxy] './intermed_speed_file.dat' ";
  $cmd .= "      using (\$1):(\$2) with points pt 3 ps 1.5 lw 2  ";
  $cmd .= "      t 'v sin(theta)' ";
  $cmd .= " , ";
  $cmd .= "   './intermed_speed_file.dat' ";
  $cmd .= "      using (\$1):(\$3) with points pt 5 ps 1.5 lw 2  ";
  $cmd .= "      t '1 - v cos(theta)' ";
  printf CMDFILE "$cmd \n";
  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;

  # create a GIF copy of any Postscript file
  if ($term_options =~ "postscript") {
    # printf "yes, term_options ..%s.. has postscript \n", $term_options;
    $gif_file = $output_file;
    $gif_file =~ s/.ps$/.gif/;
    $cmd = "convert -rotate 90 $output_file $gif_file ";
    printf "cmd is ..$cmd.. \n";
    $retval = `$cmd`;
  }


#######
# Plot showing quantities for high speed
#
  $title = "'Behavior of trig functions at small angles for fast jet v=0.9c '";
  $output_file = "./make_plot_c.ps";
  $term_options = "postscript color 'Helvetica' 18 ";

  $cmdfile = "./make_plot_c.gnu";


  # determine the limits of the graph
  #    note that the y-axis is logarithmic
  $minx = $min_angle_deg;
  $maxx = 40;
  $miny = $min_y;
  $maxy = $max_y;
  
  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 xlabel 'Angle theta (degrees) ' \n";
  printf CMDFILE "set ylabel '' \n";
  printf CMDFILE "set title $title\n";
  
  $cmd = "plot [$minx:$maxx][$miny:$maxy] './high_speed_file.dat' ";
  $cmd .= "      using (\$1):(\$2) with points pt 3 ps 1.5 lw 2  ";
  $cmd .= "      t 'v sin(theta)' ";
  $cmd .= " , ";
  $cmd .= "   './high_speed_file.dat' ";
  $cmd .= "      using (\$1):(\$3) with points pt 5 ps 1.5 lw 2  ";
  $cmd .= "      t '1 - v cos(theta)' ";
  printf CMDFILE "$cmd \n";
  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;

  # create a GIF copy of any Postscript file
  if ($term_options =~ "postscript") {
    # printf "yes, term_options ..%s.. has postscript \n", $term_options;
    $gif_file = $output_file;
    $gif_file =~ s/.ps$/.gif/;
    $cmd = "convert -rotate 90 $output_file $gif_file ";
    printf "cmd is ..$cmd.. \n";
    $retval = `$cmd`;
  }



#######
# Plot showing quantities for very high speed
#
  $title = "'Behavior of trig functions at small angles for very fast jet v=0.99c '";
  $output_file = "./make_plot_d.ps";
  $term_options = "postscript color 'Helvetica' 18 ";

  $cmdfile = "./make_plot_d.gnu";


  # determine the limits of the graph
  #    note that the y-axis is logarithmic
  $minx = $min_angle_deg;
  $maxx = 40;
  $miny = $min_y;
  $maxy = $max_y;
  
  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 xlabel 'Angle theta (degrees) ' \n";
  printf CMDFILE "set ylabel '' \n";
  printf CMDFILE "set title $title\n";
  
  $cmd = "plot [$minx:$maxx][$miny:$maxy] './veryhigh_speed_file.dat' ";
  $cmd .= "      using (\$1):(\$2) with points pt 3 ps 1.5 lw 2  ";
  $cmd .= "      t 'v sin(theta)' ";
  $cmd .= " , ";
  $cmd .= "   './veryhigh_speed_file.dat' ";
  $cmd .= "      using (\$1):(\$3) with points pt 5 ps 1.5 lw 2  ";
  $cmd .= "      t '1 - v cos(theta)' ";
  printf CMDFILE "$cmd \n";
  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;

  # create a GIF copy of any Postscript file
  if ($term_options =~ "postscript") {
    # printf "yes, term_options ..%s.. has postscript \n", $term_options;
    $gif_file = $output_file;
    $gif_file =~ s/.ps$/.gif/;
    $cmd = "convert -rotate 90 $output_file $gif_file ";
    printf "cmd is ..$cmd.. \n";
    $retval = `$cmd`;
  }




#######
# Plot showing the "apparent" speed for various jet speeds
#
  $title = "'Apparent transverse speed of a jet '";
  $output_file = "./make_plot_e.ps";
  $term_options = "postscript color 'Helvetica' 18 ";

  $cmdfile = "./make_plot_e.gnu";


  # determine the limits of the graph
  #    note that the y-axis is logarithmic
  $minx = $min_angle_deg;
  $maxx = 90;
  $miny = $min_y;
  $maxy = 2.5;
  
  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 xlabel 'Angle theta (degrees) ' \n";
  printf CMDFILE "set ylabel 'speed in terms of c' \n";
  printf CMDFILE "set title $title\n";
  
  $cmd = "plot [$minx:$maxx][$miny:$maxy] './slow_speed_file.dat' ";
  $cmd .= "      using (\$1):(\$4) with points pt 3 ps 1.5 lw 2  ";
  $cmd .= "      t 'v = 0.1c' ";
  $cmd .= " , ";
  $cmd .= "   './intermed_speed_file.dat' ";
  $cmd .= "      using (\$1):(\$4) with points pt 5 ps 1.5 lw 2  ";
  $cmd .= "      t 'v = 0.5c' ";
  $cmd .= " , ";
  $cmd .= "   './high_speed_file.dat' ";
  $cmd .= "      using (\$1):(\$4) with points pt 6 ps 1.5 lw 2  ";
  $cmd .= "      t 'v = 0.9c' ";
  printf CMDFILE "$cmd \n";
  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;

  # create a GIF copy of any Postscript file
  if ($term_options =~ "postscript") {
    # printf "yes, term_options ..%s.. has postscript \n", $term_options;
    $gif_file = $output_file;
    $gif_file =~ s/.ps$/.gif/;
    $cmd = "convert -rotate 90 $output_file $gif_file ";
    printf "cmd is ..$cmd.. \n";
    $retval = `$cmd`;
  }






exit 0;




##############################################################################
# PROCEDURE: exec_cmd
#
# DESCRIPTION: Execute the given shell command line.  If the $debug flag
#              is set, we print to stdout the command line, and
#              also print out the result string.
#
# RETURNS:
#              the result of the command
#
#
sub exec_cmd {

  my($cmd, $ret);

  $cmd = $_[0];

  if ($debug > 0) {
    printf "cmd is ..$cmd.. \n";
  }
  $ret = `$cmd`;
  if ($debug > 0) {
    printf "ret is ..$ret.. \n";
  }

  return($ret);
}


