#!/usr/bin/perl
#


  my($output_file, $term_options);

  # get the arguments
  $output_file = "./drag_2015_expt_v.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 'terminal velocity (m/s)' \n";
  printf CMDFILE "set ylabel 'force of air resistance (N)' \n";
  printf CMDFILE "set title 'Falling coffee filters (Sep 23, 2015) '\n", $aper;
  
  $cmd = "plot  './drag_2015.x.dat' using (\$2):(\$1+rand(0)*0.001)  ";
  $cmd .=   "    lt 1 pt 3 lw 3";
#  $cmd .= " , ";
#  $cmd .= " -0.0241874+0.0259123*x with lines lt -1 ";

  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 as function of square of velocity
  $output_file = "./drag_2015_expt_vsq.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 'square of terminal velocity [(m/s)]^2' \n";
  printf CMDFILE "set ylabel 'force of air resistance (N)' \n";
  printf CMDFILE "set title 'Falling coffee filters (Sep 23, 2015) '\n", $aper;
  
  $cmd = "plot  './drag_2015.x.dat' using (\$2*\$2):(\$1+rand(0)*0.001)  ";
  $cmd .=   "    lt 1 pt 3 lw 3";
  #$cmd .= " , ";
  #$cmd .= " 0.00247954+0.00607765*x with lines lt -1 ";

  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 log of force vs. log of velocity
  $output_file = "./drag_2015_expt_log.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 bot right \n";
  printf CMDFILE "set xlabel 'ln (terminal velocity (m/s))' \n";
  printf CMDFILE "set ylabel 'ln (force of air resistance (N))' \n";
  printf CMDFILE "set title 'Falling coffee filters (Sep 23, 2015) '\n", $aper;
  
  $cmd = "plot  './drag_2015.x.log.dat' using 4:3 ";
  $cmd .=   "    lt 1 pt 3 lw 3 t '' ";
  $cmd .= " , ";
  $cmd .= " -2.11245+1.9008*x with lines lt -1 t 'slope 1.90' ";
  $cmd .= " , ";
  $cmd .= " -2.172+(1.9008+0.226)*x with lines lt 2 lc -1 t 'slope 2.13' ";
  $cmd .= " , ";
  $cmd .= " -2.042+(1.9008-0.226)*x with lines lt 2 lc -1 t 'slope 1.67' ";

  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);

  # Now plot force vs. (velocity)^(1.88)
  $output_file = "./drag_2013_expt_188.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 '(terminal velocity (m/s)) to the 1.88 power' \n";
  printf CMDFILE "set ylabel 'force of air resistance (N)' \n";
  printf CMDFILE "set title 'Falling coffee filters'\n", $aper;
  
  $cmd = "plot  './drag_2013_b.dat' using ((\$4)**1.88311):(\$3)  ";
  $cmd .=   "    lt 1 pt 3 lw 3";
  $cmd .= " , ";
  $cmd .= " 0.000774864+0.00709369*x with lines lt -1 ";

  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;
