#!/usr/bin/perl
#
# Make plots showing the properties of the ensemble photometric solution
#

$title = "'Field of 1107 Lictoria, V-band diff mag, Mar 28 2002 UT'";

# make the Postscript and PNG graphs of ensemble magsig vs. mag
sig_plot("magsig.ps", "postscript color");
sig_plot("magsig.png", "png color");

# make Postscript and PNG graphs of image adjustment vs. JD
em_plot("emplot.ps", "postscript color");
em_plot("emplot.png", "png color");

# make Postscript and PNG graphs of uncert in adjustment vs. JD
sigmaem_plot("sigmaemplot.ps", "postscript color");
sigmaem_plot("sigmaemplot.png", "png color");

exit 0;



############################################################################
# Make a plot showing "sigma" (stdev from mean mag) vs "mag" in solution.
#
# create a file with GNUPLOT commands, then execute GNUPLOT to read
#   from that file.
#
# usage:  sig_plot   outfile_file_name  set_term_options
#
sub sig_plot {
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = $_[0];
  $term_options = $_[1];

  $cmdfile = "gnuplot.in";

  # determine the limits of the graph
  $minmag = -0.5;
  $maxmag = 6.5;
  $minsig = -0.02;
  $maxsig = 0.40;
  
  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 nokey \n";
  printf CMDFILE "set xlabel 'differential magnitude' \n";
  printf CMDFILE "set ylabel 'stdev from mean mag' \n";
  printf CMDFILE "set title $title\n";
  
  # ($2-4.237) added for special case of Aug 22 UT data
  $cmd = "plot [$minmag:$maxmag][$minsig:$maxsig] 'solvepht.sig' using 2:3 pt 6 ps 0.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;
  
}
  
############################################################################
# Make a plot showing "em" (image adjustment) vs Juliand Date
#
# create a file with GNUPLOT commands, then execute GNUPLOT to read
#   from that file.
#
# usage:  em_plot   outfile_file_name  set_term_options
#
sub em_plot {
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = $_[0];
  $term_options = $_[1];

  $cmdfile = "gnuplot.in";

  # determine the limits of the graph
  $solveimg_file = "solvepht.img";
  ($minjd, $maxjd) = get_minmax($solveimg_file, 6);
  if ($debug > 0) {
    printf "em_plot: min JD %12.7f   max JD %12.7f \n", $minjd, $maxjd;
  }
  ($minem, $maxem) = get_minmax($solveimg_file, 7);
  if ($debug > 0) {
    printf "em_plot: min em %7.3f    max em %7.3f \n", $minem, $maxem;
  }
  # place absolute limits on the values of the adjustments, to avoid
  #   very distant outliers from throwing off the entire scale of the plot
if (0 == 1) {
  if ($minem < -10) {
    $minem = -10;
  }
  if ($maxem > 10) {
    $maxem = 10;
  }
}

  $minx = $minjd - ($maxjd - $minjd)*0.05;
  $maxx = $maxjd + ($maxjd - $minjd)*0.05;
  $miny = $minem - ($maxem - $minem)*0.05;
  $maxy = $maxem + ($maxem - $minem)*0.05;
  # now subtract 12000 from JD so plot looks nicer
  $minx -= 12000.0;
  $maxx -= 12000.0;
  
  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 nokey \n";
  printf CMDFILE "set xlabel 'Julian Date - 2,252,000' \n";
  printf CMDFILE "set ylabel 'image adjustment to solution' \n";
  printf CMDFILE "set title $title\n";
  
  $cmd = "plot [$minx:$maxx][$miny:$maxy] 'solvepht.img' ";
  $cmd = $cmd . " using (\$7-12000):8 pt 6 ps 0.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;
  
}
  
############################################################################
# Make a plot showing "sigmaem" (uncert in image adjustment) vs Julian Date
#
# create a file with GNUPLOT commands, then execute GNUPLOT to read
#   from that file.
#
# usage:  sigmaem_plot   outfile_file_name  set_term_options
#
sub sigmaem_plot {
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = $_[0];
  $term_options = $_[1];

  $cmdfile = "gnuplot.in";

  # determine the limits of the graph
  $solveimg_file = "solvepht.img";
  ($minjd, $maxjd) = get_minmax($solveimg_file, 6);
  if ($debug > 0) {
    printf "em_plot: min JD %12.7f   max JD %12.7f \n", $minjd, $maxjd;
  }
  ($minem, $maxem) = get_minmax($solveimg_file, 8);
  if ($debug > 0) {
    printf "em_plot: min sigmaem %7.3f    max sigmaem %7.3f \n", $minem, $maxem;
  }
  # place absolute limits on the values of the adjustments, to avoid
  #   very distant outliers from throwing off the entire scale of the plot
if (0 == 1) {
  if ($minem < -10) {
    $minem = -10;
  }
  if ($maxem > 10) {
    $maxem = 10;
  }
}

  $minx = $minjd - ($maxjd - $minjd)*0.05;
  $maxx = $maxjd + ($maxjd - $minjd)*0.05;
  $miny = $minem - ($maxem - $minem)*0.05;
  $maxy = $maxem + ($maxem - $minem)*0.05;
  # now subtract 12000 from JD so plot looks nicer
  $minx -= 12000.0;
  $maxx -= 12000.0;
  
  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 nokey \n";
  printf CMDFILE "set xlabel 'Julian Date - 2,252,000' \n";
  printf CMDFILE "set ylabel 'uncert in image adjustment to solution' \n";
  printf CMDFILE "set title $title\n";
  
  $cmd = "plot [$minx:$maxx][$miny:$maxy] 'solvepht.img' ";
  $cmd = $cmd . " using (\$7-12000):9 pt 6 ps 0.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;
  
}
  


#########################################################################
# PROCEDURE: get_minmax
#
# DESCRIPTION: open the given file, read numbers from the given 
#              column, find the min and max values, return them
#              in a two-element list
#
# RETURNS:
#      a list (min, max)
#
sub get_minmax {
  
  my($filename, 
     $column,
	  $value,
	  $sum,
	  $num,
	  $mean,
	  $i);

  $filename = $_[0];
  $column = $_[1];

  $num = 0;
  open(INFILE, $filename) || die ("get_mean: can't open file $filename");
  while (<INFILE>) {
    @words = split(/\s+/);
    $value = $words[$column];
	 if ($debug > 1) {
	   printf "  get_mean: next value is ..%s..\n", $value;
	 }
    if ($num == 0) {
	    $min = $value;
		 $max = $value;
	 } else {
	    if ($value < $min) {
		   $min = $value;
		 }
		 if ($value > $max) {
		   $max = $value;
		 }
    }
	 $num++;
  }
  close(INFILE);

  return($min, $max);
}
