#!/usr/bin/perl
#


  $term_options = "postscript enhanced color 'Helvetica,18' ";
  $cmdfile = "gnuplot.in";
  $data_file = "./star_calc.out";


  ###########################################################
  # plot the signal for a star alone
  $output_file = "sn_a.ps";
  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 style line 1 lw 3 \n";
  printf CMDFILE "set grid \n";
  printf CMDFILE "set key top left \n";
  printf CMDFILE "set xlabel 'exposure time (seconds)' \n";
  printf CMDFILE "set ylabel 'signal or noise (electrons)' \n";
  printf CMDFILE "set title 'A star alone' \n";
  
 # GIF  1=red 2=green 3=blue 4=magenta 5=cyan 6=yellow 7=black 8=orange-pink 9=grey -1=black
 #      A     var     B      C         D                E          F             G
  $cmd = "plot [0:100][] '$data_file' using 2:5 t 'Signal from star' lt 1 lw 3 pt 1 ";
  #  $cmd .= " , ";
  # $cmd .= " '$data_file' using 2:(\$8*5) t 'Noise from star (x5) ' lt 2 lw 3 pt 2";
  # $cmd .= " , ";
  # $cmd .= " '$data_file' using 2:((\$5/\$8)*10) t 'S/N ratio (x10) ' lt 3 lw 3 pt 3";

  printf CMDFILE "$cmd \n";
  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;

  # create a GIF copy of any Postscript file
  if ($term_options =~ "postscript") {
    # printf "yes, term_options ..%s.. has postscript \n", $term_options;
    $gif_file = $output_file;
    $gif_file =~ s/.ps/.png/;
    $cmd = "convert -rotate 90 $output_file $gif_file ";
    printf "cmd is ..$cmd.. \n";
    $retval = `$cmd`;
  }




  ###########################################################
  # plot the noise for a star alone
  $output_file = "sn_b.ps";
  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 xlabel 'exposure time (seconds)' \n";
  printf CMDFILE "set ylabel 'signal or noise (electrons)' \n";
  printf CMDFILE "set title 'A star alone' \n";
  
 # GIF  1=red 2=green 3=blue 4=magenta 5=cyan 6=yellow 7=black 8=orange-pink 9=grey -1=black
 #      A     var     B      C         D                E          F             G
  $cmd = "plot [0:100][0:100] '$data_file' using 2:8 t 'Noise from star' lt 2 lw 3 pt 2 ";

  printf CMDFILE "$cmd \n";
  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;

  # create a GIF copy of any Postscript file
  if ($term_options =~ "postscript") {
    # printf "yes, term_options ..%s.. has postscript \n", $term_options;
    $gif_file = $output_file;
    $gif_file =~ s/.ps/.png/;
    $cmd = "convert -rotate 90 $output_file $gif_file ";
    printf "cmd is ..$cmd.. \n";
    $retval = `$cmd`;
  }





  ###########################################################
  # plot the S/N for a star alone
  $output_file = "sn_c.ps";
  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 xlabel 'exposure time (seconds)' \n";
  printf CMDFILE "set ylabel 'signal or noise (electrons)' \n";
  printf CMDFILE "set title 'A star alone' \n";
  
 # GIF  1=red 2=green 3=blue 4=magenta 5=cyan 6=yellow 7=black 8=orange-pink 9=grey -1=black
 #      A     var     B      C         D                E          F             G
  $cmd = "plot [0:100][0:100] '$data_file' using 2:8 t 'S/N from star alone' lt 3 lw 3 pt 3 ";

  printf CMDFILE "$cmd \n";
  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;

  # create a GIF copy of any Postscript file
  if ($term_options =~ "postscript") {
    # printf "yes, term_options ..%s.. has postscript \n", $term_options;
    $gif_file = $output_file;
    $gif_file =~ s/.ps/.png/;
    $cmd = "convert -rotate 90 $output_file $gif_file ";
    printf "cmd is ..$cmd.. \n";
    $retval = `$cmd`;
  }




  ###########################################################
  # plot the signal for a star, plus thermal noise
  $output_file = "sn_d.ps";
  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 xlabel 'exposure time (seconds)' \n";
  printf CMDFILE "set ylabel 'signal or noise (electrons)' \n";
  printf CMDFILE "set title 'A star and thermal noise' \n";
  
 # GIF  1=red 2=green 3=blue 4=magenta 5=cyan 6=yellow 7=black 8=orange-pink 9=grey -1=black
 #      A     var     B      C         D                E          F             G
  $cmd = "plot [0:100][] '$data_file' using 2:5 t 'Signal from star' lt 1 lw 3 pt 1 ";
    $cmd .= " , ";
  $cmd .= " '$data_file' using 2:(\$11) t 'Thermal electrons' lt 4 lw 3 pt 4";
  # $cmd .= " , ";
  # $cmd .= " '$data_file' using 2:((\$5/\$8)*10) t 'S/N ratio (x10) ' lt 3 lw 3 pt 3";

  printf CMDFILE "$cmd \n";
  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;

  # create a GIF copy of any Postscript file
  if ($term_options =~ "postscript") {
    # printf "yes, term_options ..%s.. has postscript \n", $term_options;
    $gif_file = $output_file;
    $gif_file =~ s/.ps/.png/;
    $cmd = "convert -rotate 90 $output_file $gif_file ";
    printf "cmd is ..$cmd.. \n";
    $retval = `$cmd`;
  }




  ###########################################################
  # plot the noise for a star plus thermal noise 
  $output_file = "sn_e.ps";
  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 xlabel 'exposure time (seconds)' \n";
  printf CMDFILE "set ylabel 'signal or noise (electrons)' \n";
  printf CMDFILE "set title 'A star and thermal noise' \n";
  
 # GIF  1=red 2=green 3=blue 4=magenta 5=cyan 6=yellow 7=black 8=orange-pink 9=grey -1=black
 #      A     var     B      C         D                E          F             G
  $cmd = "plot [0:100][0:60] '$data_file' using 2:7 t 'Noise from star' lt 2 lw 3 pt 2 ";
  $cmd .= " , ";
  $cmd .= " '$data_file' using 2:12 t 'Thermal noise' lt 5 lw 3 pt 5 ";


  printf CMDFILE "$cmd \n";
  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;

  # create a GIF copy of any Postscript file
  if ($term_options =~ "postscript") {
    # printf "yes, term_options ..%s.. has postscript \n", $term_options;
    $gif_file = $output_file;
    $gif_file =~ s/.ps/.png/;
    $cmd = "convert -rotate 90 $output_file $gif_file ";
    printf "cmd is ..$cmd.. \n";
    $retval = `$cmd`;
  }





  ###########################################################
  # plot the S/N for a star plus thermal noise
  $output_file = "sn_f.ps";
  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 xlabel 'exposure time (seconds)' \n";
  printf CMDFILE "set ylabel 'signal or noise (electrons)' \n";
  printf CMDFILE "set title 'A star plus thermal noise' \n";
  
 # GIF  1=red 2=green 3=blue 4=magenta 5=cyan 6=yellow 7=black 8=orange-pink 9=grey -1=black
 #      A     var     B      C         D                E          F             G
  $cmd = "plot [0:100][0:60] '$data_file' using 2:8 t 'S/N for star alone' lt 3 lw 3 pt 3 ";
  $cmd .=  " , ";
  $cmd .=  " '$data_file' using 2:((\$5)/(sqrt(\$5+\$11))) t 'S/N for star and thermal' lt 7 lw 3 pt 7" ;

  printf CMDFILE "$cmd \n";
  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;

  # create a GIF copy of any Postscript file
  if ($term_options =~ "postscript") {
    # printf "yes, term_options ..%s.. has postscript \n", $term_options;
    $gif_file = $output_file;
    $gif_file =~ s/.ps/.png/;
    $cmd = "convert -rotate 90 $output_file $gif_file ";
    printf "cmd is ..$cmd.. \n";
    $retval = `$cmd`;
  }




  ###########################################################
  # plot the signal for a star, plus thermal noise and readout noise
  $output_file = "sn_g.ps";
  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 xlabel 'exposure time (seconds)' \n";
  printf CMDFILE "set ylabel 'signal or noise (electrons)' \n";
  printf CMDFILE "set title 'A star, thermal and readout noise' \n";
  
 # GIF  1=red 2=green 3=blue 4=magenta 5=cyan 6=yellow 7=black 8=orange-pink 9=grey -1=black
 #      A     var     B      C         D                E          F             G
  $cmd = "plot [0:100][] '$data_file' using 2:5 t 'Signal from star' lt 1 lw 3 pt 1 ";
  $cmd .= " , ";
  $cmd .= " '$data_file' using 2:(\$11) t 'Thermal electrons' lt 4 lw 3 pt 4";
  # $cmd .= " , ";
  # $cmd .= " '$data_file' using 2:((\$5/\$8)*10) t 'S/N ratio (x10) ' lt 3 lw 3 pt 3";

  printf CMDFILE "$cmd \n";
  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;

  # create a GIF copy of any Postscript file
  if ($term_options =~ "postscript") {
    # printf "yes, term_options ..%s.. has postscript \n", $term_options;
    $gif_file = $output_file;
    $gif_file =~ s/.ps/.png/;
    $cmd = "convert -rotate 90 $output_file $gif_file ";
    printf "cmd is ..$cmd.. \n";
    $retval = `$cmd`;
  }




  ###########################################################
  # plot the noise for a star plus thermal and readout noise 
  $output_file = "sn_h.ps";
  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 xlabel 'exposure time (seconds)' \n";
  printf CMDFILE "set ylabel 'signal or noise (electrons)' \n";
  printf CMDFILE "set title 'A star, thermal and readout noise' \n";
  
 # GIF  1=red 2=green 3=blue 4=magenta 5=cyan 6=yellow 7=black 8=orange-pink 9=grey -1=black
 #      A     var     B      C         D                E          F             G
  $cmd = "plot [0:100][0:60] '$data_file' using 2:7 t 'Noise from star' lt 2 lw 3 pt 2 ";
  $cmd .= " , ";
  $cmd .= " '$data_file' using 2:12 t 'Thermal noise' lt 5 lw 3 pt 5 ";
  $cmd .= " , ";
  $cmd .= " '$data_file' using 2:14 t 'Readout noise' lt 8 lw 3 pt 8 ";


  printf CMDFILE "$cmd \n";
  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;

  # create a GIF copy of any Postscript file
  if ($term_options =~ "postscript") {
    # printf "yes, term_options ..%s.. has postscript \n", $term_options;
    $gif_file = $output_file;
    $gif_file =~ s/.ps/.png/;
    $cmd = "convert -rotate 90 $output_file $gif_file ";
    printf "cmd is ..$cmd.. \n";
    $retval = `$cmd`;
  }





  ###########################################################
  # plot the S/N for a star plus thermal and readout noise
  $output_file = "sn_i.ps";
  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 xlabel 'exposure time (seconds)' \n";
  printf CMDFILE "set ylabel 'signal or noise (electrons)' \n";
  printf CMDFILE "set title 'A star plus thermal and readout noise' \n";
  
 # GIF  1=red 2=green 3=blue 4=magenta 5=cyan 6=yellow 7=black 8=orange-pink 9=grey -1=black
 #      A     var     B      C         D                E          F             G
  $cmd = "plot [0:100][0:60] '$data_file' using 2:8 t 'S/N for star alone' lt 3 lw 3 pt 3 ";
  $cmd .=  " , ";
  $cmd .=  " '$data_file' using 2:((\$5)/(sqrt(\$5+\$11))) t 'S/N for star and thermal' lt 7 lw 3 pt 7" ;
  $cmd .=  " , ";
  $cmd .=  " '$data_file' using 2:17 t 'S/N for star, thermal, readout' lt 9 lw 3 pt 9" ;

  printf CMDFILE "$cmd \n";
  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;

  # create a GIF copy of any Postscript file
  if ($term_options =~ "postscript") {
    # printf "yes, term_options ..%s.. has postscript \n", $term_options;
    $gif_file = $output_file;
    $gif_file =~ s/.ps/.png/;
    $cmd = "convert -rotate 90 $output_file $gif_file ";
    printf "cmd is ..$cmd.. \n";
    $retval = `$cmd`;
  }


  ###########################################################
  # closeup plot the S/N for a star plus thermal and readout noise
  #    at very short exposure times
  $output_file = "sn_i2.ps";
  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 xlabel 'exposure time (seconds)' \n";
  printf CMDFILE "set ylabel 'signal or noise (electrons)' \n";
  printf CMDFILE "set title 'A star plus thermal and readout noise' \n";
  
 # GIF  1=red 2=green 3=blue 4=magenta 5=cyan 6=yellow 7=black 8=orange-pink 9=grey -1=black
 #      A     var     B      C         D                E          F             G
  $cmd = "plot [0:10][0:20] '$data_file' using 2:8 t 'S/N for star alone' with linespoints lt 3 lw 3 pt 3 ";
  $cmd .=  " , ";
  $cmd .=  " '$data_file' using 2:((\$5)/(sqrt(\$5+\$11))) t 'S/N for star and thermal' with linespoints lt 7 lw 3 pt 7" ;
  $cmd .=  " , ";
  $cmd .=  " '$data_file' using 2:17 t 'S/N for star, thermal, readout' with linespoints lt 9 lw 3 pt 9" ;

  printf CMDFILE "$cmd \n";
  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;

  # create a GIF copy of any Postscript file
  if ($term_options =~ "postscript") {
    # printf "yes, term_options ..%s.. has postscript \n", $term_options;
    $gif_file = $output_file;
    $gif_file =~ s/.ps/.png/;
    $cmd = "convert -rotate 90 $output_file $gif_file ";
    printf "cmd is ..$cmd.. \n";
    $retval = `$cmd`;
  }







exit 0;
  
