#!/usr/bin/perl
#

  my($output_file, $term_options);

  # Examples of Boltzmann equation output
  #   ratio of N(2) / N(1) as function of temperature
  $output_file = "./ratio_n2_n1.ps";
  $term_options = "postscript color '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";

  #  revert to some oldish default colors 
  printf CMDFILE "set style line 1 lt rgb 'red' lw 3  \n"; 
  printf CMDFILE "set style line 2 lt rgb 'sea-green' lw 3  \n"; 
  printf CMDFILE "set style line 3 lt rgb 'blue' lw 3  \n"; 
  printf CMDFILE "set style line 4 lt rgb 'cyan' lw 3  \n"; 
  printf CMDFILE "set style line 5 lt rgb 'violet' lw 3  \n"; 
  printf CMDFILE "set style line 6 lt rgb 'orange' lw 3  \n"; 
  printf CMDFILE "set style line 7 lt rgb 'black' lw 3  \n"; 


  printf CMDFILE "set grid \n";
  printf CMDFILE "set key top left \n";
  printf CMDFILE "set ylabel 'Ratio of N(2)/N(1)' \n";
  printf CMDFILE "set xlabel 'Temperature (K)' \n";
  printf CMDFILE "set title 'Populations based on energy level only' \n";
  
  $cmd = "plot [0:150000][] 'saha_bigt.out' using (\$4):(\$5)  ";
  $cmd .= "   with lines  ls 1  t '' ";
  $cmd .= " , ";
  $cmd .= "   1.0 with lines ls 7  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`;
  }



  # Examples of Boltzmann equation output
  #   ratio of N(ionized) / N(total) as function of temperature
  $output_file = "./ratio_ionized_total.ps";
  $term_options = "postscript color '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";

  #  revert to some oldish default colors 
  printf CMDFILE "set style line 1 lt rgb 'red' lw 3  \n"; 
  printf CMDFILE "set style line 2 lt rgb 'sea-green' lw 3  \n"; 
  printf CMDFILE "set style line 3 lt rgb 'blue' lw 3  \n"; 
  printf CMDFILE "set style line 4 lt rgb 'cyan' lw 3  \n"; 
  printf CMDFILE "set style line 5 lt rgb 'violet' lw 3  \n"; 
  printf CMDFILE "set style line 6 lt rgb 'orange' lw 3  \n"; 
  printf CMDFILE "set style line 7 lt rgb 'black' lw 3  \n"; 


  printf CMDFILE "set grid \n";
  printf CMDFILE "set key top left \n";
  printf CMDFILE "set ylabel 'Ratio of ionized to total hydrogen' \n";
  printf CMDFILE "set xlabel 'Temperature (K)' \n";
#  printf CMDFILE "set title 'Populations based on energy level only' \n";
  
  $cmd = "plot [0:20000][0.0:1.1] 'saha.out' using (\$1):(\$3)  ";
  $cmd .= "   with lines  lc -1 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 -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }


  # Examples of Boltzmann equation output
  #   ratio of N(neutral) / N(total) as function of temperature
  $output_file = "./ratio_neutral_total.ps";
  $term_options = "postscript color '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";

  #  revert to some oldish default colors 
  printf CMDFILE "set style line 1 lt rgb 'red' lw 3  \n"; 
  printf CMDFILE "set style line 2 lt rgb 'sea-green' lw 3  \n"; 
  printf CMDFILE "set style line 3 lt rgb 'blue' lw 3  \n"; 
  printf CMDFILE "set style line 4 lt rgb 'cyan' lw 3  \n"; 
  printf CMDFILE "set style line 5 lt rgb 'violet' lw 3  \n"; 
  printf CMDFILE "set style line 6 lt rgb 'orange' lw 3  \n"; 
  printf CMDFILE "set style line 7 lt rgb 'black' lw 3  \n"; 


  printf CMDFILE "set grid \n";
  printf CMDFILE "set key top left \n";
  printf CMDFILE "set ylabel 'Ratio of neutral to total hydrogen' \n";
  printf CMDFILE "set xlabel 'Temperature (K)' \n";
#  printf CMDFILE "set title 'Populations based on energy level only' \n";
  
  $cmd = "plot [0:20000][0.0:1.1] 'saha.out' using (\$1):(1.0-\$3)  ";
  $cmd .= "   with lines  ls 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 -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }




  # Examples of Boltzmann equation output
  #   three ratios: 
  #        ionized to total
  #        n=2 to n=1
  #        n=2 to total

  $output_file = "./ratio_three.ps";
  $term_options = "postscript color '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";

  #  revert to some oldish default colors 
  printf CMDFILE "set style line 1 lt rgb 'red' lw 3  \n"; 
  printf CMDFILE "set style line 2 lt rgb 'sea-green' lw 3  \n"; 
  printf CMDFILE "set style line 3 lt rgb 'blue' lw 3  \n"; 
  printf CMDFILE "set style line 4 lt rgb 'cyan' lw 3  \n"; 
  printf CMDFILE "set style line 5 lt rgb 'violet' lw 3  \n"; 
  printf CMDFILE "set style line 6 lt rgb 'orange' lw 3  \n"; 
  printf CMDFILE "set style line 7 lt rgb 'black' lw 3  \n"; 


  printf CMDFILE "set grid \n";
  printf CMDFILE "set key top right \n";
  printf CMDFILE "set ylabel 'Fraction' \n";
  printf CMDFILE "set xlabel 'Temperature (K)' \n";
#  printf CMDFILE "set title 'Populations based on energy level only' \n";
  
  $cmd = "plot [0:30000][0.0:1.2] 'saha.out' using (\$1):(\$3)  ";
  $cmd .= "   with lines  ls 1  t 'ionized to total' ";
  $cmd .= " , ";
  $cmd .= "                 'saha.out' using (\$1):(\$5*1e0)  ";
  $cmd .= "   with lines  ls 2  t 'n=2 to n=1' ";
  $cmd .= " , ";
  $cmd .= "                 'saha.out' using (\$1):(\$6*2e5)  ";
  $cmd .= "   with lines  ls 3  t 'n=2 to total, scaled by 2E5 ' ";

  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`;
  }



  # Examples of Boltzmann equation output
  #   three ratios: 
  #        ionized to total
  #        n=2 to n=1
  #        n=2 to total
  #   this is on logarithmic scale

  $output_file = "./ratio_three_log.ps";
  $term_options = "postscript color '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";

  #  revert to some oldish default colors 
  printf CMDFILE "set style line 1 lt rgb 'red' lw 3  \n"; 
  printf CMDFILE "set style line 2 lt rgb 'sea-green' lw 3  \n"; 
  printf CMDFILE "set style line 3 lt rgb 'blue' lw 3  \n"; 
  printf CMDFILE "set style line 4 lt rgb 'cyan' lw 3  \n"; 
  printf CMDFILE "set style line 5 lt rgb 'violet' lw 3  \n"; 
  printf CMDFILE "set style line 6 lt rgb 'orange' lw 3  \n"; 
  printf CMDFILE "set style line 7 lt rgb 'black' lw 3  \n"; 


  printf CMDFILE "set grid \n";
  printf CMDFILE "set logscale y \n";
  printf CMDFILE "set key bot right \n";
  printf CMDFILE "set ylabel 'Fraction' \n";
  printf CMDFILE "set xlabel 'Temperature (K)' \n";
#  printf CMDFILE "set title 'Populations based on energy level only' \n";
  
  $cmd = "plot [0:30000][1e-14:2] 'saha.out' using (\$1):(\$3)  ";
  $cmd .= "   with lines  ls 1  t 'ionized to total' ";
  $cmd .= " , ";
  $cmd .= "                 'saha.out' using (\$1):(\$5*1e0)  ";
  $cmd .= "   with lines  ls 2  t 'n=2 to n=1' ";
  $cmd .= " , ";
  $cmd .= "                 'saha.out' using (\$1):(\$6*1e0)  ";
  $cmd .= "   with lines  ls 3  t 'n=2 to total' ";

  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`;
  }



  # Examples of Boltzmann equation output
  #   three ratios: 
  #        neutral to total
  #        n=2 to n=1
  #        n=2 to total
  #   this is on logarithmic scale

  $output_file = "./ratio_three_n_log.ps";
  $term_options = "postscript color '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";

  #  revert to some oldish default colors 
  printf CMDFILE "set style line 1 lt rgb 'red' lw 3  \n"; 
  printf CMDFILE "set style line 2 lt rgb 'sea-green' lw 3  \n"; 
  printf CMDFILE "set style line 3 lt rgb 'blue' lw 3  \n"; 
  printf CMDFILE "set style line 4 lt rgb 'cyan' lw 3  \n"; 
  printf CMDFILE "set style line 5 lt rgb 'violet' lw 3  \n"; 
  printf CMDFILE "set style line 6 lt rgb 'orange' lw 3  \n"; 
  printf CMDFILE "set style line 7 lt rgb 'black' lw 3  \n"; 


  printf CMDFILE "set grid \n";
  printf CMDFILE "set logscale y \n";
  printf CMDFILE "set key bot right \n";
  printf CMDFILE "set ylabel 'Fraction' \n";
  printf CMDFILE "set xlabel 'Temperature (K)' \n";
#  printf CMDFILE "set title 'Populations based on energy level only' \n";
  
  $cmd = "plot [0:30000][1e-3:1.2] 'saha.out' using (\$1):(1.0-\$3)  ";
  $cmd .= "   with lines  ls 2  t 'neutral to total' ";
  $cmd .= " , ";
  $cmd .= "                 'saha.out' using (\$1):(\$5*1e2)  ";
  $cmd .= "   with lines  ls 1  t 'n=2 to n=1 (x 100)' ";
  $cmd .= " , ";
  $cmd .= "                 'saha.out' using (\$1):(\$6*1e5)  ";
  $cmd .= "   with lines  ls 3  t 'n=2 to total (x 100,000)' ";

  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;
