#!/usr/bin/perl
#

  $output_file = "car_tree_position.ps";
  $term_options = "postscript color 'Helvetica' 18 ";

  $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 bot left \n";
  printf CMDFILE "set xlabel 'Time (seconds) ' \n";
  printf CMDFILE "set ylabel 'Position (meters)' \n";
  printf CMDFILE "set title 'Motion of a car down the street' \n";
  
  $cmd = "plot [-1:6][20:60] 'car_tree.dat' using 1:2 ";
  $cmd .= "  with points ps 2 pt 5 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/.ps/.png/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }


# now let's add a linear fit


  $output_file = "car_tree_velocity.ps";
  $term_options = "postscript color 'Helvetica' 18 ";

  $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 bot left \n";
  printf CMDFILE "set xlabel 'Time (seconds) ' \n";
  printf CMDFILE "set ylabel 'Position (meters)' \n";
  printf CMDFILE "set title 'Motion of a car down the street' \n";
  
  $cmd = "plot [-1:6][20:60] 'car_tree.dat' using 1:2 ";
  $cmd .= "  with points ps 2 pt 5 t '' ";
  $cmd .= " , ";
  $cmd .= "  30 + 5*x with lines lw 3 lc -1 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/.ps/.png/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }


# this will be a plot of velocity versus time ... pretty dull


  $output_file = "car_tree_velocity_2.ps";
  $term_options = "postscript color 'Helvetica' 18 ";

  $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 bot left \n";
  printf CMDFILE "set xlabel 'Time (seconds) ' \n";
  printf CMDFILE "set ylabel 'Velocity (meters per second)' \n";
  printf CMDFILE "set title 'Motion of a car down the street' \n";
  
  $cmd = "plot [-1:6][-1:8] 'car_tree_vel.dat' using 1:2 ";
  $cmd .= "  with linespoints ps 2 pt 5 lw 3 lc -1  t '' ";
  $cmd .= "  ,  ";
  $cmd .= "  0 with lines lw 1 lt -1  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/.ps/.png/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }


# this will be a plot of velocity versus time ... but this
#    time we show integral as area under curve


  $output_file = "car_tree_velocity_3.ps";
  $term_options = "postscript color 'Helvetica' 18 ";

  $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 bot left \n";
  printf CMDFILE "set xlabel 'Time (seconds) ' \n";
  printf CMDFILE "set ylabel 'Velocity (meters per second)' \n";
  printf CMDFILE "set title 'Motion of a car down the street' \n";
  printf CMDFILE "set object 1 rect from 1,0 to 3,5 fc rgb 'gold' \n ";
  
  $cmd = "plot [-1:6][-1:8] 'car_tree_vel.dat' using 1:2 ";
  $cmd .= "  with linespoints ps 2 pt 5 lw 3 lc -1  t '' ";
  $cmd .= "  ,  ";
  $cmd .= "  0 with lines lw 1 lt -1  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/.ps/.png/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }


# plot of toy car on ramp: position vs. time

  $output_file = "toy_car_position.ps";
  $term_options = "postscript color 'Helvetica' 18 ";

  $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 bot left \n";
  printf CMDFILE "set xlabel 'Time (seconds) ' \n";
  printf CMDFILE "set ylabel 'Position (meters)' \n";
  printf CMDFILE "set title 'Motion of a toy car down a ramp ' \n";
  
  $cmd = "plot [-1:5][-1:6] 'toy_car_lots.dat' using 1:2 ";
  $cmd .= "  with lines lt -1 t '' "; 
  $cmd .= " ,  ";
  $cmd .= " 'toy_car.dat' using 1:2 ";
  $cmd .= "  with points ps 2 pt 5 lc 1 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/.ps/.png/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }




# plot of toy car on ramp: velocity vs. time

  $output_file = "toy_car_velocity.ps";
  $term_options = "postscript color 'Helvetica' 18 ";

  $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 bot left \n";
  printf CMDFILE "set xlabel 'Time (seconds) ' \n";
  printf CMDFILE "set ylabel 'Velocity (meters per second)' \n";
  printf CMDFILE "set title 'Motion of a toy car down a ramp ' \n";
  
  $cmd = "plot [0:6][-1:4] 'toy_car.dat' using 1:3 ";
  $cmd .= "  with points ps 2 pt 5 lc 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/.ps/.png/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }



# plot of position of balls dropped by Rapunzel and the prince

  $output_file = "rapunzel_pos.ps";
  $term_options = "postscript color 'Helvetica' 18 ";

  $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 xlabel 'Time (seconds) ' \n";
  printf CMDFILE "set ylabel 'Position (meters)' \n";
  printf CMDFILE "set title 'Balls dropped by Rapunzel and the Prince' \n";
  
  $cmd = "plot [0:5][-5:120] 'rapunzel.dat' using 1:2 ";
  $cmd .= "  with points ps 0.8 pt 5 lc 3  t 'Prince' "; 
  $cmd .= " , ";
  $cmd .= " 'rapunzel.dat' using 1:3 ";
  $cmd .= "  with points ps 0.8 pt 5 lc 2  t 'Rapunzel' "; 
  
  

  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/.ps/.png/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }






exit 0;
