#!/usr/bin/perl
#


############################################################################
# make a plot showing a single "orbit" computed by different
#   techniques with a very large timestep
#
#
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "./single_orbit.ps";
  $term_options = "postscript color";

  $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 size square \n";

  printf CMDFILE "set xlabel 'Distance in X direction (km) ' \n";
  printf CMDFILE "set ylabel 'Distance in Y direction (km) ' \n";
  printf CMDFILE "set title 'Motion of a satellite around the Earth with timestep 500 seconds' \n";
  
  $cmd = "plot [-2.5e4:1.5e4][-2.0e4:2.0e4] 'nbody.out.bad.e1' every 2::1 using (\$4/1000):(\$5/1000) ";
  $cmd .= "    lt 1 ps 1.5 pt 1 lw 3 t 'Euler 1' ";
   $cmd .= " , ";
   $cmd .= "  'nbody.out.bad.e1a' every 2::1 using (\$4/1000):(\$5/1000) ";
   $cmd .= "    lt 3 ps 1.5 pt 3 lw 3 t 'symplectic Euler 1' ";
   $cmd .= " , ";
   $cmd .= "  'nbody.out.bad.e2' every 2::1 using (\$4/1000):(\$5/1000) ";
   $cmd .= "    lt 4 ps 1.5 pt 4 lw 3 t 'Euler 2' ";
   $cmd .= " , ";
   $cmd .= "  'nbody.out.bad.rk4' every 2::1 using (\$4/1000):(\$5/1000) ";
   $cmd .= "    lt -1 ps 1.5 pt 6 lw 2 t 'Runge-Kutta 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`;
  }



############################################################################
# A second version of the the comparison of a single
#   orbit for different techniques with a very large timestep
#
#
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "./single_orbit_a.ps";
  $term_options = "postscript color size 12,12 '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 size square \n";
  printf CMDFILE "set label 1 at 7000,500 'Start' font 'Helvetica,18' \n";
  printf CMDFILE "set arrow 2 from  6600,500 to 6600,1500 lt -1 lw 2 \n";

  printf CMDFILE "set xlabel 'Distance in X direction (km) ' \n";
  printf CMDFILE "set ylabel 'Distance in Y direction (km) ' \n";
  printf CMDFILE "set title 'Motion of a satellite around the Earth with timestep 500 seconds' \n";
  
  $cmd = "plot [-1.0e4:1.0e4][-1.0e4:1.0e4] 'nbody.out.bad.e1' every 2::1 using (\$4/1000):(\$5/1000) ";
  $cmd .= "    lt 1 ps 1.5 pt 1 lw 5 t 'Euler 1' ";
   $cmd .= " , ";
   $cmd .= "  'nbody.out.bad.e1a' every 2::1 using (\$4/1000):(\$5/1000) ";
   $cmd .= "    lt 3 ps 1.5 pt 3 lw 3 t 'symplectic Euler 1' ";
   $cmd .= " , ";
   $cmd .= "  'nbody.out.bad.e2' every 2::1 using (\$4/1000):(\$5/1000) ";
   $cmd .= "    lt 4 ps 1.5 pt 4 lw 3 t 'Euler 2' ";
   $cmd .= " , ";
   $cmd .= "  'nbody.out.bad.rk4' every 2::1 using (\$4/1000):(\$5/1000) ";
   $cmd .= "    lt -1 ps 1.5 pt 6 lw 2 t 'Runge-Kutta 4' ";
   $cmd .= " , ";
   $cmd .= "  'circle.dat' using 2:3 " ;
   $cmd .= "    with lines lw 1 lt -1 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 -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }


