#!/usr/bin/perl
#

  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "sunmoon_a.ps";
  $term_options = "postscript color enhanced 'Helvectica,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 left \n";
  printf CMDFILE "set ylabel 'Angular diameter (arcminutes)'  \n";
  printf CMDFILE "set xlabel 'Time from present (millions of years)' \n";
  printf CMDFILE "set title 'The future of solar eclipses '\n";
  
  $cmd = "plot [0:800][25:35]  ";
  $cmd .= "   './calc_sunmoon_sizes.out' using (\$1/1e6):(2*\$4) ";
  $cmd .= "       with lines lt 1 lc rgb \"red\" lw 2 t 'Sun' ";
  $cmd .= " , ";
  $cmd .= "   './calc_sunmoon_sizes.out' using (\$1/1e6):(2*\$5) ";
  $cmd .= "       with points ps 0.3 lc rgb \"red\" lw 2  t '' ";
  $cmd .= " , ";
  $cmd .= "   './calc_sunmoon_sizes.out' using (\$1/1e6):(2*\$8) ";
  $cmd .= "       with lines lt 1 lc rgb \"blue\" lw 2 t 'Moon' ";
  $cmd .= " , ";
  $cmd .= "   './calc_sunmoon_sizes.out' using (\$1/1e6):(2*\$9) ";
  $cmd .= "       with points ps 0.3 lc rgb \"blue\" lw 2  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 -flatten -background white -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }


  # using larger value for eccentricity of Earth's orbit
  $output_file = "sunmoon_b.ps";
  $term_options = "postscript color enhanced 'Helvectica,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 left \n";
  printf CMDFILE "set ylabel 'Angular diameter (arcminutes)'  \n";
  printf CMDFILE "set xlabel 'Time from present (millions of years)' \n";
  printf CMDFILE "set title 'The future of solar eclipses (using e=0.058 for Earth) '\n";
  
  $cmd = "plot [0:1000][25:38]  ";
  $cmd .= "   './calc_sunmoon_sizes.out.bige' using (\$1/1e6):(2*\$4) ";
  $cmd .= "       with lines lt 1 lc rgb \"red\" lw 2 t 'Sun' ";
  $cmd .= " , ";
  $cmd .= "   './calc_sunmoon_sizes.out.bige' using (\$1/1e6):(2*\$5) ";
  $cmd .= "       with points ps 0.3 lc rgb \"red\" lw 2  t '' ";
  $cmd .= " , ";
  $cmd .= "   './calc_sunmoon_sizes.out.bige' using (\$1/1e6):(2*\$8) ";
  $cmd .= "       with lines lt 1 lc rgb \"blue\" lw 2 t 'Moon' ";
  $cmd .= " , ";
  $cmd .= "   './calc_sunmoon_sizes.out.bige' using (\$1/1e6):(2*\$9) ";
  $cmd .= "       with points ps 0.3 lc rgb \"blue\" lw 2  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 -flatten -background white -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }

exit 0;
