#!/usr/bin/perl
#
#
# Plot quantities measured from an actual mass on a spring,
#   recorded with Logger Pro
$infile = "./cons_short.csv";


##########################################
#  plot the position and velocity as a function of time 
#      in separate panels
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "cons_pos_vel_a.ps";
  $term_options = "postscript color enhanced 'Helvetica,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";

  # revert to some oldish default colors
  printf CMDFILE "set style line 1 lt rgb 'red' lw 3  \n";
  printf CMDFILE "set style line 2 lt rgb 'green' lw 3  \n";
  printf CMDFILE "set style line 3 lt rgb 'blue' lw 3  \n";
  printf CMDFILE "set style line 4 lt rgb 'cyan' lw 3  \n";
  printf CMDFILE "set style line 5 lt rgb 'violet' lw 3  \n";


  printf CMDFILE "set multiplot layout 2,1 \n";

  # first, pos vs. time

  printf CMDFILE "set grid \n";
  printf CMDFILE "set key bot left \n";
#  printf CMDFILE "set size square \n";
  printf CMDFILE "set ylabel 'Position (m)'  \n";
  printf CMDFILE "set xlabel 'Time (s)' \n";
  $cmd = "plot [][] 'cons_short.dat' using 1:3 ";
  $cmd .= "        with lines ls 1 t 'pos' ";
  printf CMDFILE "$cmd \n";

  # next panel is vel vs. time

  printf CMDFILE "set grid \n";
  printf CMDFILE "set key bot left \n";
#  printf CMDFILE "set size square \n";
  printf CMDFILE "set ylabel 'Velocity (m/s)'  \n";
  printf CMDFILE "set xlabel 'Time (s)' \n";
  $cmd = "plot [][] 'cons_short.dat' using 1:4 ";
  $cmd .= "        with lines ls 2 t 'vel' ";
  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`;
  }




##########################################
#  plot the position and velocity as a function of time 
#      in the same panel
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "cons_pos_vel_b.ps";
  $term_options = "postscript color enhanced 'Helvetica,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";

  # revert to some oldish default colors
  printf CMDFILE "set style line 1 lt rgb 'red' lw 3  \n";
  printf CMDFILE "set style line 2 lt rgb 'green' lw 3  \n";
  printf CMDFILE "set style line 3 lt rgb 'blue' lw 3  \n";
  printf CMDFILE "set style line 4 lt rgb 'cyan' lw 3  \n";
  printf CMDFILE "set style line 5 lt rgb 'violet' lw 3  \n";


#  printf CMDFILE "set multiplot layout 2,1 \n";

  # first, pos vs. time

  printf CMDFILE "set grid \n";
  printf CMDFILE "set key bot left \n";
  printf CMDFILE "set ylabel 'Position (m) or Velocity*0.25 (m/s)'  \n";
  printf CMDFILE "set xlabel 'Time (s)' \n";
  $cmd = "plot [][] 'cons_short.dat' using 1:3 ";
  $cmd .= "        with lines ls 1 t 'pos' ";
  $cmd .= " , ";
  $cmd .= "   'cons_short.dat' using 1:(\$4*0.25) ";
  $cmd .= "        with lines ls 2 t 'vel' ";
  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 the position and accleration as a function of time 
#      in separate panels
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "cons_pos_acc_a.ps";
  $term_options = "postscript color enhanced 'Helvetica,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";

  # revert to some oldish default colors
  printf CMDFILE "set style line 1 lt rgb 'red' lw 3  \n";
  printf CMDFILE "set style line 2 lt rgb 'green' lw 3  \n";
  printf CMDFILE "set style line 3 lt rgb 'blue' lw 3  \n";
  printf CMDFILE "set style line 4 lt rgb 'cyan' lw 3  \n";
  printf CMDFILE "set style line 5 lt rgb 'violet' lw 3  \n";


  printf CMDFILE "set multiplot layout 2,1 \n";

  # first, pos vs. time

  printf CMDFILE "set grid \n";
  printf CMDFILE "set key bot left \n";
#  printf CMDFILE "set size square \n";
  printf CMDFILE "set ylabel 'Position (m)'  \n";
  printf CMDFILE "set xlabel 'Time (s)' \n";
  $cmd = "plot [][] 'cons_short.dat' using 1:3 ";
  $cmd .= "        with lines ls 1 t 'pos' ";
  printf CMDFILE "$cmd \n";

  # next panel is accel vs. time

  printf CMDFILE "set grid \n";
  printf CMDFILE "set key bot left \n";
#  printf CMDFILE "set size square \n";
  printf CMDFILE "set ylabel 'Accleration*0.05 (m/s^2) '  \n";
  printf CMDFILE "set xlabel 'Time (s)' \n";
  $cmd = "plot [][] 'cons_short.dat' using 1:(\$5*0.05) ";
  $cmd .= "        with lines ls 3 t 'accel' ";
  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`;
  }




##########################################
#  plot the position and acceleration as a function of time 
#      in the same panel
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "cons_pos_acc_b.ps";
  $term_options = "postscript color enhanced 'Helvetica,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";

  # revert to some oldish default colors
  printf CMDFILE "set style line 1 lt rgb 'red' lw 3  \n";
  printf CMDFILE "set style line 2 lt rgb 'green' lw 3  \n";
  printf CMDFILE "set style line 3 lt rgb 'blue' lw 3  \n";
  printf CMDFILE "set style line 4 lt rgb 'cyan' lw 3  \n";
  printf CMDFILE "set style line 5 lt rgb 'violet' lw 3  \n";


#  printf CMDFILE "set multiplot layout 2,1 \n";

  # first, pos vs. time

  printf CMDFILE "set grid \n";
  printf CMDFILE "set key bot left \n";
  printf CMDFILE "set ylabel 'Position (m) or Acceleration*0.05 (m/s^2)'  \n";
  printf CMDFILE "set xlabel 'Time (s)' \n";
  $cmd = "plot [][] 'cons_short.dat' using 1:3 ";
  $cmd .= "        with lines ls 1 t 'pos' ";
  $cmd .= " , ";
  $cmd .= "   'cons_short.dat' using 1:(\$5*0.050) ";
  $cmd .= "        with lines ls 3 t 'accel' ";
  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;


