#!/usr/bin/perl
#


############################################################################
# create a file with GNUPLOT commands, then execute GNUPLOT to read
#   from that file.
#
# usage:  make_graph   outfile_file_name  set_term_options
#
  

  # get the arguments
  $output_file = "corvette_torque.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 nokey \n";
  printf CMDFILE "set xlabel 'engine angular velocity (RPM) ' \n";
  printf CMDFILE "set ylabel 'engine torque (Newton-meters) ' \n";
  printf CMDFILE "set title '1997 Corvette LS1 '\n", $aper;
  
  $cmd = "plot [0:6500][300:500] 'graph_si.dat' using (\$1):(\$2)  ";
  $cmd .= "  lt 1 pt 3 ps 2 lw 4";

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


  # Corvette power
  $output_file = "corvette_power.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 nokey \n";
  printf CMDFILE "set xlabel 'engine angular velocity (RPM) ' \n";
  printf CMDFILE "set ylabel 'engine power (kW)) ' \n";
  printf CMDFILE "set title '1997 Corvette LS1 '\n", $aper;
  
  $cmd = "plot [0:6500][] 'graph_power.dat' using (\$1):(\$3/1000)  ";
  $cmd .= "  lt 1 pt 3 ps 2 lw 4";

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



  # Tesla torque vs. RPM
  $output_file = "tesla_torque.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 xlabel 'engine angular velocity (RPM) ' \n";
  printf CMDFILE "set ylabel 'engine torque (Newton-meters) ' \n";
  printf CMDFILE "set title 'Tesla Roadster vs. Corvette '\n", $aper;
  
  $cmd = "plot [0:6500][300:500] 'electric_engine.dat' using (\$1):(\$2)  ";
  $cmd .= "  lt 1 lc 3 pt 5 ps 2 lw 4  t 'Tesla' ";
  $cmd .= " , ",
  $cmd .= "                       'graph_si.dat' using (\$1):(\$2)  ";
  $cmd .= "  lt 1 lc 1 pt 3 ps 2 lw 2  t 'Corvette' ";


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



  # Tesla vs. Corvette power
  $output_file = "tesla_power.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 xlabel 'engine angular velocity (RPM) ' \n";
  printf CMDFILE "set ylabel 'engine power (kW)) ' \n";
  printf CMDFILE "set title 'Tesla vs. Corvette  '\n", $aper;
  
  $cmd = "plot [0:6500][] 'electric_engine.dat' using (\$1):(\$3/1000)  ";
  $cmd .= "  lt 1 lc 3 pt 5 ps 2 lw 4 t 'Tesla' ";
  $cmd .= " , ";
  $cmd .= "                'graph_power.dat' using (\$1):(\$3/1000)  ";
  $cmd .= "  lt 1 lc 1 pt 3 ps 2 lw 2 t 'Corvette' ";

  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;
