#!/usr/bin/perl
#

  my($output_file, $term_options);

  # get the arguments
  $output_file = "consen_a.ps";
  $term_options = "postscript enhanced 'Helvectica,18' size 10,10 ";

  $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 multiplot layout 3,1  \n";

  printf CMDFILE "set grid \n";
  printf CMDFILE "set key top right \n";
  printf CMDFILE "set xlabel 'Time (sec)' \n";
  printf CMDFILE "set title 'Motion of a mass oscillating vertically on a spring'\n";
  
  printf CMDFILE "set ylabel 'Position (m)' \n";
  $cmd = "plot [][] 'make_dat.out' using (\$1):(\$2)  ";
  $cmd .= "            with linespoints lt 1 lc 1 pt 3 lw 2 t '' ";
  $cmd .= " , 0 lt -1 t '' ";
  printf CMDFILE "$cmd \n";

  printf CMDFILE "unset title \n";
  printf CMDFILE "set ylabel 'Velocity (m/s)' \n";
  $cmd = "plot [][] 'make_dat.out' using (\$1):(\$3)  ";
  $cmd .= "            with linespoints lt 1 lc 2 pt 4 lw 4 t '' ";
  $cmd .= " , 0 lt -1 t '' ";
  printf CMDFILE "$cmd \n";

  printf CMDFILE "unset title \n";
  printf CMDFILE "set ylabel 'Acceleration (m/s*s)' \n";
  $cmd = "plot [][] 'make_dat.out' using (\$1):(\$4)  ";
  $cmd .= "            with linespoints lt 1 lc 3 pt 6 lw 1 t '' ";
  $cmd .= " , 0 lt -1 t '' ";

  printf CMDFILE "$cmd \n";
  printf CMDFILE "unset multiplot \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, just the position as a function of time

  $output_file = "consen_xonly.ps";
  $term_options = "postscript enhanced 'Helvectica,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 (sec)' \n";
  printf CMDFILE "set title 'Motion of a mass oscillating vertically on a spring'\n";
  
  printf CMDFILE "set ylabel 'Position (m)' \n";
  $cmd = "plot [][] 'make_dat.out' using (\$1):(\$2)  ";
  $cmd .= "            with linespoints lt 1 lc 1 pt 3 lw 2 t '' ";
  $cmd .= " , 0 lt -1 t '' ";
  printf CMDFILE "$cmd \n";

  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, position and velocity 
  $output_file = "consen_xandv.ps";
  $term_options = "postscript enhanced 'Helvectica,18' size 10,8 ";

  $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 multiplot layout 2,1  \n";

  printf CMDFILE "set grid \n";
  printf CMDFILE "set key top right \n";
  printf CMDFILE "set xlabel 'Time (sec)' \n";
  printf CMDFILE "set title 'Motion of a mass oscillating vertically on a spring'\n";
  
  printf CMDFILE "set ylabel 'Position (m)' \n";
  $cmd = "plot [][] 'make_dat.out' using (\$1):(\$2)  ";
  $cmd .= "            with linespoints lt 1 lc 1 pt 3 lw 2 t '' ";
  $cmd .= " , 0 lt -1 t '' ";
  printf CMDFILE "$cmd \n";

  printf CMDFILE "unset title \n";
  printf CMDFILE "set ylabel 'Velocity (m/s)' \n";
  $cmd = "plot [][] 'make_dat.out' using (\$1):(\$3)  ";
  $cmd .= "            with linespoints lt 1 lc 2 pt 4 lw 4 t '' ";
  $cmd .= " , 0 lt -1 t '' ";
  printf CMDFILE "$cmd \n";


  printf CMDFILE "unset multiplot \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;
