#!/usr/bin/perl
#
# Make plots showing data from the "swinging ring" experiment.
#
# MWR 1/26/2021

$mypi = 3.14159;


  my($output_file, $term_options);


##########################################################################
# Linear plot of period versus diameter of ring
#
  $datafile = "./swing_ring_data.dat";
  $output_file = "./ring_per_diam_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 'Ring diameter (cm)'  \n";
  printf CMDFILE "set ylabel 'Period of ring (seconds) ' \n";
  printf CMDFILE "set title 'Swinging ring experiment' \n"; 

  printf CMDFILE "plot [][] '$datafile' ";
  printf CMDFILE "    using 1:2 with points ls 1 ps 0.8 pt 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 period versus diameter of ring
#    add a best-fit line
#
  $datafile = "./swing_ring_data.dat";
  $output_file = "./ring_per_diam_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 left \n";
  printf CMDFILE "set xrange [$xmin:$xmax]  \n";
  printf CMDFILE "set yrange [$ymin:$ymax]  \n";
  printf CMDFILE "set xlabel 'Ring diameter (cm)'  \n";
  printf CMDFILE "set ylabel 'Period of ring (seconds) ' \n";
  printf CMDFILE "set title 'Swinging ring experiment' \n"; 

  printf CMDFILE "plot [][] '$datafile' ";
  printf CMDFILE "    using 1:2 with points ls 1 ps 0.8 pt 6  t ''  ";
  printf CMDFILE " , ";
  printf CMDFILE " 0.529+0.01719*x with lines ls 3 t 'linear 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`;
  }



##########################################################################
# Log-log plot of period versus diameter of ring
#    add a best-fit line
#
  $datafile = "./swing_ring_data.dat";
  $output_file = "./ring_per_diam_d.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 logscale x \n";
  printf CMDFILE "set logscale y \n";
  printf CMDFILE "set xtics (6,8,10,20,40,60) \n";
  printf CMDFILE "set ytics (0.30,0.50,0.80,1.0,1.2,1.5) \n";
  printf CMDFILE "set xlabel 'Ring diameter (cm)'  \n";
  printf CMDFILE "set ylabel 'Period of ring (seconds) ' \n";
  printf CMDFILE "set title 'Swinging ring experiment' \n"; 

  printf CMDFILE "plot [6:60][0.45:1.60] '$datafile' ";
  printf CMDFILE "    using 1:2 with points ls 1 ps 0.8 pt 6  t ''  ";
  printf CMDFILE " , ";
#  printf CMDFILE "3.920+0.1905*log(x) with lines ls 3 t 'slope 0.5' ";
  printf CMDFILE " 0.2*sqrt(x) with lines ls 3 t 'slope 0.5' ";
  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`;
  }



##########################################################################
# log-log plot of period versus diameter of ring
#    plot log(period) vs log(diam)
#
  $datafile = "./swing_ring_data.dat";
  $output_file = "./ring_per_diam_e.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 'Log [Ring diameter (cm)]'  \n";
  printf CMDFILE "set ylabel 'Log [Period of ring (seconds)] ' \n";
  printf CMDFILE "set title 'Swinging ring experiment' \n"; 

  printf CMDFILE "plot [1.6:4.0][-0.75:0.40] '$datafile' ";
  printf CMDFILE "    using (log(\$1)):(log(\$2)) with points ls 1 ps 0.8 pt 6  t ''  ";
  printf CMDFILE " , ";
  printf CMDFILE " -1.608+0.5*x with lines ls 3 t '-1.60 + 0.5*log(diam)' ";
  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`;
  }


##########################################################################
# log-log plot of period versus diameter of ring
#    plot log(period) vs log(diam)
#    showing uncertainties in fitted line
#
  $datafile = "./swing_ring_data.dat";
  $output_file = "./ring_per_diam_f.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 'Log [Ring diameter (cm)]'  \n";
  printf CMDFILE "set ylabel 'Log [Period of ring (seconds)] ' \n";
  printf CMDFILE "set title 'Swinging ring experiment' \n"; 

  printf CMDFILE "plot [1.6:4.0][-0.75:0.40] '$datafile' ";
  printf CMDFILE "    using (log(\$1)):(log(\$2)) with points ls 1 ps 0.8 pt 6  t ''  ";
  printf CMDFILE " , ";
  printf CMDFILE " -1.600+0.5*x with lines ls 3 t '-1.60 + 0.5*log(diam)' ";
  printf CMDFILE " , ";
  printf CMDFILE " -1.400+0.44*x with lines ls 2 t '-1.40 + 0.44*log(diam)' ";
  printf CMDFILE " , ";
  printf CMDFILE " -1.800+0.56*x with lines lc -1 lw 2 t '-1.80 + 0.56*log(diam)' ";
  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;

