#!/usr/bin/perl
#

  
  my($output_file, $term_options);

if (0 == 1) {

  ##############################################################
  # Make a stacked histogram showing discoveries by year and method
  $infile = "./year_type_hist.dat";
  $output_file = "year_method_a.ps";
  $term_options = "postscript color '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 nogrid \n";
  printf CMDFILE "set xtics rotate out \n";
  printf CMDFILE "set style data histogram \n";
  printf CMDFILE "set style fill solid border \n";
#  printf CMDFILE "set key invert reverse Left outside \n";
  printf CMDFILE "set key invert reverse Left top left \n";
  printf CMDFILE "set key autotitle columnheader \n";
  printf CMDFILE "set style histogram rowstacked \n";
  printf CMDFILE "set boxwidth 0.6 relative  \n";
  printf CMDFILE "set ylabel 'Exoplanets discovered per year'  \n";

  printf CMDFILE "plot for [COL=2:11] '$infile' ";
  printf CMDFILE "  using COL:xticlabels(1) title columnheader \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 -background white -flatten $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }


  ##############################################################
  # Make a graph with y-axis cutoff to show detail
  $infile = "./year_type_hist.dat";
  $output_file = "year_method_b.ps";
  $term_options = "postscript color '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 nogrid \n";
  printf CMDFILE "set xtics rotate out \n";
  printf CMDFILE "set style data histogram \n";
  printf CMDFILE "set style histogram rowstacked \n";
  printf CMDFILE "set style fill solid noborder \n";
#  printf CMDFILE "set style fill solid border lc rgb 'white' \n";
  # change yellow to a better color
  #printf CMDFILE "set style line 6 lt 1 lc rgb 'blue' \n";
#  printf CMDFILE "set key invert reverse Left outside \n";
  printf CMDFILE "set key invert reverse Left top left \n";
  printf CMDFILE "set key autotitle columnheader \n";
  printf CMDFILE "set boxwidth 0.6 relative  \n";
  printf CMDFILE "set ylabel 'Exoplanets discovered per year'  \n";
  printf CMDFILE "set yrange [0:60] \n";

  printf CMDFILE "plot for [COL=2:11] '$infile' ";
  printf CMDFILE "  using COL:xticlabels(1) title columnheader \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 -background white -flatten $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }


}


if (0 == 1) {


  ##############################################################
  # number of "planets" in the solar system over time
  $infile = "./planet_counting.dat";
  $output_file = "planet_counting_a.ps";
  $term_options = "postscript color '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 xtics rotate out \n";
  printf CMDFILE "set ylabel 'Year'  \n";
  printf CMDFILE "set ylabel 'Number of planets'  \n";

  printf CMDFILE "plot [1500:2020][0:25]  '$infile' using 1:2 ";
  printf CMDFILE "  with steps lt 1 lc 1 lw 3  t '' ";
  printf CMDFILE "  \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 -background white -flatten $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }


}



  ##############################################################
  # timescale for bodies to "clear the neighborhood"
  $infile = "./calc_lambda.out";
  $output_file = "clear_time_a.ps";
  $term_options = "postscript color '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 xtics rotate out \n";
  printf CMDFILE "set logscale x \n ";
  printf CMDFILE "set logscale y \n ";
  printf CMDFILE "set ylabel 'Time to eject one body (years)'  \n";
  printf CMDFILE "set xlabel 'Orbital radius (AU)'  \n";

  printf CMDFILE "set label 1 'Merc' at 0.2,1.0e7 \n";
  printf CMDFILE "set label 2 'Ven' at 0.45,5.0e3 \n";
  printf CMDFILE "set label 3 'Earth' at 1.1,5.0e3 \n";
  printf CMDFILE "set label 4 'Mars' at 1.2,2.0e7 \n";
  printf CMDFILE "set label 5 'Ceres' at 1.17,3.0e12 \n";
  printf CMDFILE "set label 6 'Jup' at 3.0,1.0e1 \n";
  printf CMDFILE "set label 7 'Sat' at 11.0,6.0e1 \n";
  printf CMDFILE "set label 8 'Ura' at 11.0,4.0e3 \n";
  printf CMDFILE "set label 9 'Nep' at 35.0,5.0e4 \n";
  printf CMDFILE "set label 10 'Plu' at 25.0,3.0e11 \n";
  printf CMDFILE "set label 11 'Eris' at 80.0,4.0e12 \n";


  printf CMDFILE "plot [0.1:500][1e0:1e14]  '$infile' using (\$4**(2.0/3.0)):8 ";
  printf CMDFILE "  with points pt 3 ps 1.5 lt 1 lc 1 lw 3  t '' ";
  printf CMDFILE " , ";
  printf CMDFILE "    13.7e9 with lines lt 2 lc -1 lw 4 t '' ";
  printf CMDFILE "  \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 -background white -flatten $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }






exit 0;






##############################################################################
# PROCEDURE: exec_cmd
#
# DESCRIPTION: Execute the given shell command line.  If the $debug flag
#              is set, we print to stdout the command line, and
#              also print out the result string.
#
# RETURNS:
#              the result of the command
#
#
sub exec_cmd {

  my($cmd, $ret);

  $cmd = $_[0];

  if ($debug > 0) {
    printf "cmd is ..$cmd.. \n";
  }
  $ret = `$cmd`;
  if ($debug > 0) {
    printf "ret is ..$ret.. \n";
  }

  return($ret);
}


