#!/usr/bin/perl
#
#

$mypi = 3.14159;

######################################################
#  plot potential energy as a function of position
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "calc_spring_pe_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 'sea-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 size square \n";
#  printf CMDFILE "set xzeroaxis  lw 3.5 \n";
#  printf CMDFILE "set yzeroaxis  lw 3.5 \n";
  printf CMDFILE "set xlabel 'Position (m)  '  \n";
  printf CMDFILE "set ylabel 'Potential energy (J) ' \n";
#  printf CMDFILE "set title 'Motion of 2-block, 3-spring system: m=2, k=30, 60, 90 ' \n";

  $cmd = "plot [0:10][] 'calc_spring_pe.out' using 2:6 ";
  $cmd .= "        with lines ls 1 lw 4 t '' ";
#  $cmd .= " , ";
#  $cmd .= "                         'calc_challenge.out' using 2:6 ";
#  $cmd .= "        with lines ls 3 lw 3 t 'right block' ";

  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 potential energy, with a "slope" at one point
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "calc_spring_pe_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 'sea-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 object 1 circle at 5,3150 size 0.1 \n";
#  printf CMDFILE "set xzeroaxis  lw 3.5 \n";
#  printf CMDFILE "set yzeroaxis  lw 3.5 \n";
  printf CMDFILE "set xlabel 'Position (m)  '  \n";
  printf CMDFILE "set ylabel 'Potential Energy (J) ' \n";
#  printf CMDFILE "set title 'Motion of 2-block, 3-spring system: m=2, k=30, 60, 90 ' \n";

  $cmd = "plot [0:10][0:14000] 'calc_spring_pe.out' using 2:6 ";
  $cmd .= "        with lines ls 1 lw 4 t '' ";
#  $cmd .= " , ";
#  $cmd .= "      'calc_spring_pe_subset.out' using 2:(0.0-2.0*\$4) ";
#  $cmd .= "        with lines ls 3 lw 3 t '' ";
  $cmd .= " , ";
  $cmd .= "       (3150.0-(5*1260)) + 1260.0*x with lines ls 3 lw 3 t '' ";
  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;
