#!/usr/bin/perl
#
# Make diagnostic plots showing the variation in several
#   parameters (sky brightness, FWHM, etc.) as a function
#   of JD during the night.
#
# Place the plots into .png files
#
# Assumes 
#         a. that this is running in a "pff" directory
#         b. there is an "addheader.log" file in the .. dir
#         c. there are .coo and .pht files in .. dir
#         d. there are .pht files with headers in this dir
#
# MWR 7/31/2001

$debug = 1;

$night = "Sep 25 2005 UT";
$minjd = 3000000.0;
$maxjd = 0;
$JDBASE = 2454000.0;

# read info from the "addheader.log" file into arrays
#   and set the globals "minjd" and "maxjd"
$logfile = "../addheader.log";
$numfiles = 0;
open(LOGFILE, "$logfile") || die("can't open file $logfile");
while (<LOGFILE>) {
  @words = split(/\s+/);
  $image[$numfiles] = $words[1];
  $object[$numfiles] = $words[2];
  $filter[$numfiles] = $words[3];
  $exptime[$numfiles] = $words[4];
  $date[$numfiles] = $words[5];
  $ut[$numfiles] = $words[6];

  # calculate the JD for this image
  $cmd = "~/bait/jd ut=$ut[$numfiles] date=$date[$numfiles]";
  if ($debug > 0) {
    printf "cmd is ..$cmd..\n";
  }
  $jd[$numfiles] = `$cmd`;
  chomp($jd[$numfiles]);
  $jd[$numfiles] -= $JDBASE;

  # this checks for invalid dates and ignores them
  if ($jd[$numfiles] == 0.0 - $JDBASE) {
    next;
  }
  printf "file %5d  name %20s  has JD %12.5f \n", $numfiles,
              $image[$numfiles], $jd[$numfiles];

  if ($jd[$numfiles] < $minjd) {
    $minjd = $jd[$numfiles];
  }
  if ($jd[$numfiles] > $maxjd) {
    $maxjd = $jd[$numfiles];
  }

  $numfiles++;
}
close(LOGFILE);

if ($debug > 0) {
  printf "minjd %12.5f maxjd %12.5f \n", $minjd, $maxjd;
}



# make a plot of number of objects vs. JD
# 
# step 1: make a data file with columns of values to plot
$cmdfile = "gnuplot.in";
$datafile = "gnuplot.dat";
open (DATAFILE, ">$datafile") || die("can't open file $datafile for output");
@coos = glob("../*.coo");
foreach $coo (@coos) {
  # strip the leading ../ from the name
  $name = $coo;
  $name =~ s/\.\.\///;
  $name =~ s/\.coo//;
  $name =~ s/\_fit/\.fit/;
  printf "next coo is %s\n", $name;
  $index = -1;
  for ($i = 0; $i < $numfiles; $i++) {
    if ($name eq $image[$i]) {
      $index = $i;
	   last;
	 }
  }
  if ($index == -1) {
    printf "error: can't find image with name $name for coo ..%s..\n", $coo;
	 next;
  }

  # count the number of lines in the file
  $nobj = 0;
  open(COOFILE, "$coo") || die ("can't open file $coo");
  while (<COOFILE>) {
    $nobj++;
  }
  close(COOFILE);
  printf DATAFILE "%12.5f %5d \n", $jd[$index], $nobj;
}
close(DATAFILE);


# step 2: create the GNUPLOT command file, and execute it
$output_file = "numobj.ps";
$term_options = "postscript color";
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 - $JDBASE' \n";
printf CMDFILE "set ylabel 'Number of objects in frame' \n";
printf CMDFILE "set title '$night' \n";

$cmd = "plot [$minjd:$maxjd][0:50] '$datafile' using 1:2 pt 6 ps 1.6";
printf CMDFILE "$cmd \n";
printf CMDFILE "set output \n";
printf CMDFILE "quit \n";
close(CMDFILE);

$retval = `gnuplot < $cmdfile`;
printf "retval for gnuplot of nobj is ..$retval.. \n";

    $psfile = $output_file;
    $giffile = $psfile;
    $giffile =~ s/.ps/.gif/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;



# make a plot of FWHM vs. JD
# 
# step 1: make a data file with columns of values to plot
$cmdfile = "gnuplot.in";
$datafile = "gnuplot.dat";
open (DATAFILE, ">$datafile") || die("can't open file $datafile for output");
@coos = glob("../*.coo");
foreach $coo (@coos) {
  # strip the leading ../ from the name
  $name = $coo;
  $name =~ s/\.\.\///;
  $name =~ s/\.coo//;
  $name =~ s/\_fit/\.fit/;
  printf "next coo is %s\n", $name;
  $index = -1;
  for ($i = 0; $i < $numfiles; $i++) {
    if ($name eq $image[$i]) {
      $index = $i;
	   last;
	 }
  }
  if ($index == -1) {
    printf "error: can't find image for coo ..%s..\n", $coo;
	 next;
  }

  $fwhm = get_mean($coo, 5);
  printf DATAFILE "%12.5f %6.2f \n", $jd[$index], $fwhm;
}
close(DATAFILE);


# step 2: create the GNUPLOT command file, and execute it
$output_file = "fwhm.ps";
$term_options = "postscript color";
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 - $JDBASE' \n";
printf CMDFILE "set ylabel 'average FWHM (pix)' \n";
printf CMDFILE "set title '$night' \n";

$cmd = "plot [$minjd:$maxjd][0:6] '$datafile' using 1:2 pt 6 ps 1.6";
printf CMDFILE "$cmd \n";
printf CMDFILE "set output \n";
printf CMDFILE "quit \n";
close(CMDFILE);

$retval = `gnuplot < $cmdfile`;
printf "retval for gnuplot of nobj is ..$retval.. \n";

    $psfile = $output_file;
    $giffile = $psfile;
    $giffile =~ s/.ps/.gif/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;




# make a plot of sky value vs. JD
# 
# step 1: make a data file with columns of values to plot
$cmdfile = "gnuplot.in";
$datafile = "gnuplot.dat";
open (DATAFILE, ">$datafile") || die("can't open file $datafile for output");
@phts = glob("../*.pht");
foreach $pht (@phts) {
  # strip the leading ../ from the name
  $name = $pht;
  $name =~ s/\.\.\///;
  $name =~ s/\.pht//;
  $name =~ s/\_fit/\.fit/;
  printf "next pht is %s\n", $name;
  $index = -1;
  for ($i = 0; $i < $numfiles; $i++) {
    if ($name eq $image[$i]) {
      $index = $i;
	   last;
	 }
  }
  if ($index == -1) {
    printf "error: can't find image for pht ..%s..\n", $pht;
	 next;
  }

  $sky = get_mean($pht, 4);
  # we calculate the LOGARITHM base 10 of the sky value
  if ($sky <= 0) {
    $skylog = 0;
  } else {
    $skylog = log($sky)/log(10.0);
  }
  printf DATAFILE "%12.5f %7.2f %8.4f\n", $jd[$index], $sky, $skylog;
}
close(DATAFILE);


# step 2: create the GNUPLOT command file, and execute it
$output_file = "sky.ps";
$term_options = "postscript color";
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 - $JDBASE' \n";
printf CMDFILE "set ylabel 'log10 of background sky (DN)' \n";
printf CMDFILE "set title '$night' \n";

$cmd = "plot [$minjd:$maxjd][1.0:4.0] '$datafile' using 1:3 pt 6 ps 1.6";
printf CMDFILE "$cmd \n";
printf CMDFILE "set output \n";
printf CMDFILE "quit \n";
close(CMDFILE);

$retval = `gnuplot < $cmdfile`;
printf "retval for gnuplot of nobj is ..$retval.. \n";

    $psfile = $output_file;
    $giffile = $psfile;
    $giffile =~ s/.ps/.gif/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;






exit 0;




#########################################################################
# PROCEDURE: get_mean
#
# DESCRIPTION: open the given file, read numbers from the given 
#              column, calculate the mean value.  
#
# RETURNS:
#      the mean value
#
sub get_mean {
  
  my($filename, 
     $column,
	  $value,
	  $sum,
	  $num,
	  $mean,
	  $i);

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

  $num = 0;
  $sum = 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;
	 }
	 $sum += $value;
	 $num++;
  }
  close(INFILE);

  if ($num > 0) {
    $mean = $sum/$num;
  } else {
    $mean = 0.0;
  }

  return($mean);
}
