#!/usr/bin/perl
#


  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "k_graph.ps";
  $term_options = "postscript color ";

  $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 xlabel 'length stretched (cm)' \n";
  printf CMDFILE "set ylabel 'Force on bottom of cord (N)' \n";
  printf CMDFILE "set title 'Elastic cord experiment, analyzed for spring const k'\n", $aper;
  
  $cmd = "plot [0:50][0:8] 'elastic.dat' using (\$3):(\$1*0.001*9.8)  ";
  $cmd .= "    ps 2 pt 4 lw 3 t '' ";
  $cmd .= " , ";
  $cmd .= "  2.57167+0.101014*x lw 1 lt -1 t 'linear fit to 10-50 cm' ";

  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 plot quantities which yield Young's modulus as slope
  $output_file = "y_graph.ps";
  $term_options = "postscript color ";

  $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 xlabel 'fractional length stretched' \n";
  printf CMDFILE "set ylabel 'Force on cord (N) / area of cord (m^2) ' \n";
  printf CMDFILE "set title 'Elastic cord experiment, analyzed for Young_s modulus '\n", $aper;
  
  $cmd = "plot [0:0.8][0:7.5E5] 'elastic.dat' using (\$3/60.5):((\$1*0.001*9.8)/9.87E-6)  ";
  $cmd .= "    ps 2 pt 4 lw 3 t '' ";
  $cmd .= " , ";
  $cmd .= "  260554+619186*x lw 1 lt -1 t 'linear fit to 0.2-0.8' ";

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

