#!/usr/bin/perl
#


  my($output_file, $term_options);

  # get the arguments
  $output_file = "pend_data_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 left \n";
  printf CMDFILE "set ylabel 'Square of angular frequency (rad/s)^2' \n";
  printf CMDFILE "set xlabel 'Reciprocal of length (1/meter) ' \n";
  printf CMDFILE "set title 'Classroom measurements of pendula '\n";
  
  $cmd = "plot [0:10.5][0:110] 'calc_pend_graph.out' using 2:4:3:5 ";
  $cmd .= "    with xyerrorbars ";
  $cmd .= "    lt 1 pt 3 lw 3 t '' ";
  $cmd .= " , ";
  $cmd .= " 9.8*x with lines lt 3 lw 2 t 'expected 9.8 m/s^2' ";
  $cmd .= " , ";
  $cmd .= " 9.69*x+0.045 with lines lt -1 t 'fit 9.69 m/s^2' ";



  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, a closeup of the first few points, to show errorbars clearly 

  # get the arguments
  $output_file = "pend_data_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 'Square of angular frequency (rad/s)^2' \n";
  printf CMDFILE "set xlabel 'Reciprocal of length (1/meter) ' \n";
  printf CMDFILE "set title 'Classroom measurements of pendula '\n";
  
  $cmd = "plot [0.9:2.6][8:28] 'calc_pend_graph.out' using 2:4:3:5 ";
  $cmd .= "    with xyerrorbars ";
  $cmd .= "    lt 1 pt 3 lw 3 t '' ";
  $cmd .= " , ";
  $cmd .= " 9.8*x with lines lt 3 lw 2 t 'expected 9.8 m/s^2' ";
  $cmd .= " , ";
  $cmd .= " 9.69*x+0.045 with lines lt -1 t 'fit 9.69 m/s^2' ";



  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`;
  }



  # this time, we show extreme acceptable values of slope

  # get the arguments
  $output_file = "pend_data_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";
  printf CMDFILE "set grid \n";
  printf CMDFILE "set key top left \n";
  printf CMDFILE "set ylabel 'Square of angular frequency (rad/s)^2' \n";
  printf CMDFILE "set xlabel 'Reciprocal of length (1/meter) ' \n";
  printf CMDFILE "set title 'Classroom measurements of pendula '\n";
  
  $cmd = "plot [0:10.5][0:110] 'calc_pend_graph.out' using 2:4:3:5 ";
  $cmd .= "    with xyerrorbars ";
  $cmd .= "    lt 1 pt 3 lw 3 t '' ";
  $cmd .= " , ";
  $cmd .= " 9.44*x+0.045 with lines lc 3 lt 1 t '9.54 m/s^2' ";
  $cmd .= " , ";
  $cmd .= " 9.94*x+0.045 with lines lc 4 lt 1 t '9.94 m/s^2' ";



  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;
