#!/usr/bin/perl
#
# Make plots showing trajectories of pitches from several different angles
# 
# MWR 8/6/2008
  

  #
  #    90 MPH fastball in vacuum, and 90 MPH fastball with drag only
  #       3rd base view
  #
  # get the arguments
  $output_file = "./traj_b_2.ps";
  $term_options = "postscript color enhanced ";

  $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 bot left \n";
  printf CMDFILE "set xlabel 'Distance from pitcher (feet) ' \n";
  printf CMDFILE "set ylabel 'Height above ground (feet)' \n";
  printf CMDFILE "set title 'Third base view, circles every 0.10 seconds' \n";
  
  $cmd = "plot [60:-3][1.5:7] ";
#  $cmd = "plot [][] ";
  $cmd .= " './traj.A.dat' index 0 ";
  $cmd .= "    using (0.0-(\$5/0.3048)):(\$6/0.3048)  ";
  $cmd .= "    t '90 MPH fastball in a vacuum' ";
  $cmd .= "    with points ps 0.5 pt 3 lw 3 ";
  $cmd .= " , ";
  $cmd .= " './traj.A.dat' index 1 ";
  $cmd .= "    using (0.0-(\$5/0.3048)):(\$6/0.3048)  ";
  $cmd .= "    t '' ";
  $cmd .= "    with points ps 1.5 pt 6 lw 1 lt 1 ";
  $cmd .= " , ";
  $cmd .= " './traj.B.dat' index 0 ";
  $cmd .= "    using (0.0-(\$5/0.3048)):(\$6/0.3048)  ";
  $cmd .= "    t '90 MPH fastball with drag Cd=0.44' ";
  $cmd .= "    with points ps 0.5 pt 3 lw 3 lt -1 ";
  $cmd .= " , ";
  $cmd .= " './traj.B.dat' index 1 ";
  $cmd .= "    using (0.0-(\$5/0.3048)):(\$6/0.3048)  ";
  $cmd .= "    t '' ";
  $cmd .= "    with points ps 1.5 pt 6 lw 1 lt -1 ";
  #$cmd .= " , ";
  #$cmd .= " './traj.B.dat2' index 0 ";
  #$cmd .= "    using (0.0-(\$5/0.3048)):(\$6/0.3048)  ";
  #$cmd .= "    t '90 MPH fastball with drag Cd=0.35' ";
  #$cmd .= "    with points ps 0.5 pt 3 lw 3 lt 2  ";
  #$cmd .= " , ";
  #$cmd .= " './traj.B.dat2' index 1 ";
  #$cmd .= "    using (0.0-(\$5/0.3048)):(\$6/0.3048)  ";
  #$cmd .= "    t '' ";
  #$cmd .= "    with points ps 1.5 pt 6 lw 1 lt 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/.eps/.gif/;
    $giffile =~ s/.ps/.gif/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }




exit 0;
