#!/usr/bin/perl
#


$suffix = sprintf "dat";

############################################################################
# create a file with GNUPLOT commands, then execute GNUPLOT to read
#   from that file.
#
# usage:  make_graph   outfile_file_name  set_term_options
#
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "warm_dark.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 key top left \n";
  printf CMDFILE "set xlabel 'Exposure time (seconds)' \n";
  printf CMDFILE "set ylabel 'Mean level in a central region' \n";
  printf CMDFILE "set title 'Warm dark frames, Sep 24, 2003 '\n", $aper;
  
  $cmd = "plot [-5:35][-50:2000] 'darks.dat' index 0 lc 1 pt 1 ps 2 lw 4 ";
  $cmd .= "   t '' ";
  $cmd .= " , ";
  $cmd .= "   178+54.71*x t 'linear fit slope = 54.7 counts/sec' ";

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






  # this graph has cold CCD measurements
  # get the arguments
  $output_file = "cold_dark.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 key top left \n";
  printf CMDFILE "set xlabel 'Exposure time (seconds)' \n";
  printf CMDFILE "set ylabel 'Mean level in a central region' \n";
  printf CMDFILE "set title 'Cold dark frames, Sep 24, 2003 '\n", $aper;
  
  $cmd = "plot [-5:35][-5:200] 'darks.dat' index 1 lc 3 pt 1 ps 2 lw 4 ";
  $cmd .= "   t '' ";
  $cmd .= " , ";
  $cmd .= "   108+2.37*x t 'linear fit slope = 2.37 counts/sec' ";


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




  # Now a histogram of the counts in one image
  # get the arguments
  $output_file = "example_histogram.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 key top left \n";
  printf CMDFILE "set xlabel 'Counts in a pixel' \n";
  printf CMDFILE "set ylabel 'Number of pixels ' \n";
  printf CMDFILE "set title 'Histogram of a warm 1-second dark frame, Sep 24, 2003 '\n", $aper;
  
  $cmd = "plot [][] 'dark1-001d.his' index 0 using 1:2 lc 1 pt 1 ps 2 lw 4 ";
  $cmd .= "   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`;
  }


  # Now a histogram of the counts in four different dark exposures
  # get the arguments
  $output_file = "four_histograms.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 key top right \n";
  printf CMDFILE "set xlabel 'Counts in a pixel' \n";
  printf CMDFILE "set ylabel 'Number of pixels ' \n";
  printf CMDFILE "set title 'Histograms of warm dark frames, Sep 24, 2003 '\n", $aper;
  
  $cmd = "plot [0:2500][] 'dark1-001d.his' index 0 using 1:2 lc 1 pt 1 ps 1 lw 4 ";
  $cmd .= "   t '1 sec' ";
  $cmd .= " , ";
  $cmd .= " 'dark10-001d.his' index 0 using 1:2 lc 3 pt 1 ps 1 lw 4 t '10 sec' ";
  $cmd .= " , ";
  $cmd .= " 'dark20-001d.his' index 0 using 1:2 lc 4 pt 1 ps 1 lw 4 t '20 sec' ";
  $cmd .= " , ";
  $cmd .= " 'dark30-001d.his' index 0 using 1:2 lc -1 pt 1 ps 1 lw 4 t '30 sec' ";


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




  # Now a histogram of the counts in four different COLD dark exposures
  # get the arguments
  $output_file = "four_cold_histograms.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 key top right \n";
  printf CMDFILE "set xlabel 'Counts in a pixel' \n";
  printf CMDFILE "set ylabel 'Number of pixels ' \n";
  printf CMDFILE "set title 'Histograms of cold dark frames, Sep 24, 2003 '\n", $aper;
  
  $cmd = "plot [50:300][] 'dark1cold-001d.his' index 0 using 1:2 lc 1 pt 1 ps 1 lw 4 ";
  $cmd .= "   t '1 sec' ";
  $cmd .= " , ";
  $cmd .= " 'dark10cold-001d.his' index 0 using 1:2 lc 3 pt 1 ps 1 lw 4 t '10 sec' ";
  $cmd .= " , ";
  $cmd .= " 'dark20cold-001d.his' index 0 using 1:2 lc 4 pt 1 ps 1 lw 4 t '20 sec' ";
  $cmd .= " , ";
  $cmd .= " 'dark30cold-001d.his' index 0 using 1:2 lc -1 pt 1 ps 1 lw 4 t '30 sec' ";


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

