#!/usr/bin/perl
#
# Make plots showing relationship between time and distance rolled
#    for a cart going down a track.
#
# MWR 1/29/2021

$mypi = 3.14159;



  my($output_file, $term_options);


##########################################################################
# Linear plot of distance vs. time
#
  $datafile = "./cart_example.dat";
  $output_file = "./cart_plot_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";

  # 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 grid \n";
  printf CMDFILE "set key top right \n";
  printf CMDFILE "set xrange [$xmin:$xmax]  \n";
  printf CMDFILE "set yrange [$ymin:$ymax]  \n";
  printf CMDFILE "set xlabel 'Time since released (seconds)  '  \n";
  printf CMDFILE "set ylabel 'Distance rolled (cm)  ' \n";
  printf CMDFILE "set title 'A cart rolling down a track ' \n"; 

  printf CMDFILE "plot [0:4.0][0:350] '$datafile' ";
  printf CMDFILE "    using 1:2:3 with yerrorbars ls 1 pt 6 ps 0.6   t ''  ";
  printf CMDFILE "\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`;
  }


##########################################################################
# Linear plot of distance vs. time-squared
#
  $datafile = "./cart_example.dat";
  $output_file = "./cart_plot_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";

  # 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 grid \n";
  printf CMDFILE "set key top right \n";
  printf CMDFILE "set xrange [$xmin:$xmax]  \n";
  printf CMDFILE "set yrange [$ymin:$ymax]  \n";
  printf CMDFILE "set xlabel 'Square of [time since released (seconds)] '  \n";
  printf CMDFILE "set ylabel 'Distance rolled (cm)  ' \n";
  printf CMDFILE "set title 'A cart rolling down a track ' \n"; 

  printf CMDFILE "plot [0:14.0][0:350] '$datafile' ";
  printf CMDFILE "    using (\$1*\$1):2:3 with yerrorbars ls 1 pt 6 ps 0.6   t ''  ";
  printf CMDFILE "\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`;
  }


##########################################################################
# Linear plot of distance vs. time-squared
#     with a fitted line
#
  $datafile = "./cart_example.dat";
  $output_file = "./cart_plot_c.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";

  # 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 grid \n";
  printf CMDFILE "set key top left \n";
  printf CMDFILE "set xrange [$xmin:$xmax]  \n";
  printf CMDFILE "set yrange [$ymin:$ymax]  \n";
  printf CMDFILE "set xlabel 'Square of [time since released (seconds)] '  \n";
  printf CMDFILE "set ylabel 'Distance rolled (cm)  ' \n";
  printf CMDFILE "set title 'A cart rolling down a track ' \n"; 

  printf CMDFILE "plot [0:14.0][0:350] '$datafile' ";
  printf CMDFILE "    using (\$1*\$1):2:3 with yerrorbars ls 1 pt 6 ps 0.6   t ''  ";
  printf CMDFILE " , " ;
  printf CMDFILE " -0.5599+25.67*(x) lc -1 lw 3 t 'best fit'  " ;
  printf CMDFILE "\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`;
  }



##########################################################################
# Linear plot of distance vs. time-squared
#     with a fitted line, and max and min lines as well
#
  $datafile = "./cart_example.dat";
  $output_file = "./cart_plot_e.ps";
  $term_options = "postscript color enhanced 'Helvetica,18' size 12,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 grid \n";
  printf CMDFILE "set key top left \n";
  printf CMDFILE "set xrange [$xmin:$xmax]  \n";
  printf CMDFILE "set yrange [$ymin:$ymax]  \n";
  printf CMDFILE "set xlabel 'Square of [time since released (seconds)] '  \n";
  printf CMDFILE "set ylabel 'Distance rolled (cm)  ' \n";
  printf CMDFILE "set title 'A cart rolling down a track ' \n"; 

  printf CMDFILE "plot [0:14.0][0:350] '$datafile' ";
  printf CMDFILE "    using (\$1*\$1):2:3 with yerrorbars ls 1 pt 6 ps 0.6   t ''  ";
  printf CMDFILE " , " ;
  printf CMDFILE " -0.5599+25.67*(x) lc -1 lw 3 t 'best fit'  " ;
  printf CMDFILE " , " ;
  printf CMDFILE " -0.5599+26.15*(x) ls 2  t 'max slope'  " ;
  printf CMDFILE " , " ;
  printf CMDFILE " -0.5599+25.28*(x) ls 3  t 'min slope'  " ;
  printf CMDFILE "\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;
