#!/usr/bin/perl
#

############################################################################
# plot velocity vs. time for motion of a cart
#
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "sample_graph_a.ps";
  $term_options = "postscript color enhanced '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 ylabel 'position of cart (m) ' \n";
  printf CMDFILE "set xlabel 'time (s) ' \n";
  printf CMDFILE "set title 'Motion of a cart along the floor' \n";
  
  $cmd = "plot [0.0:3.5][0.0:2.0] 'sample_graph.dat' ";
  $cmd .= "   using 1:3:2:4 with xyerrorbars pt 5 ps 1 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/.ps/.png/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }



############################################################################
# plot velocity vs. time for motion of a cart, 
#    and include a best-fit line
#
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "sample_graph_b.ps";
  $term_options = "postscript color enhanced '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 left \n";
  printf CMDFILE "set ylabel 'position of cart (m) ' \n";
  printf CMDFILE "set xlabel 'time (s) ' \n";
  printf CMDFILE "set title 'Motion of a cart along the floor' \n";
  
  $cmd = "plot [0.0:3.5][0.0:2.0] 'sample_graph.dat' ";
  $cmd .= "   using 1:3:2:4 with xyerrorbars pt 5 ps 1 lw 3 t '' ";
  $cmd .= " , ";
  $cmd .= "   0.037+0.522*x with lines lt -1 lw 1 t 'best fit 0.52 m/s' ";
  $cmd .= " , ";
  $cmd .= "   0.097+0.474*x with lines lt 2 lc 3 lw 2 t 'least steep 0.47  m/s' ";
  $cmd .= " , ";
  $cmd .= "  -0.023+0.570*x with lines lt 2 lc 4 lw 2 t 'most  steep 0.57 m/s' ";

  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;
