#!/usr/bin/perl
#

############################################################################
# make a plot showing motion of field in RA and Dec over
#   the course of a long set of exposures.  Use the "do_multi.pl"
#   output as the input.
#

$JDBASE = 2458000.0;

$filter = "V";
# plate scale, arcsec per pixel
$plate_scale = 1.32;
# offset between RA and Dec symbols, for clarity
$offset = -10.0;
  
  my($output_file, $term_options);

  # a graph showing all the data
  $output_file = "./trackplot_a.ps";
  $term_options = "postscript color enhanced '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 right \n";
  printf CMDFILE "set xlabel 'Julian Date - $JDBASE'  \n";
  printf CMDFILE "set ylabel 'star motion (arcsec) ' \n";
  printf CMDFILE "set title 'GSC 05140-03255, UT Sep 19 2019 '\n";

  
  $cmd = "plot [][] ";
#  $cmd = "plot [][-100:100] ";
  $cmd .=  " 'prepare_track_data.out' using (\$3-$JDBASE):($offset+\$5*$plate_scale) with linespoints "; 
  $cmd .=  "   pt 6 ps 1.5  t 'Dec (offset by $offset arcsec)' ";
  $cmd .=  "  , ";
  $cmd .=  " 'prepare_track_data.out' using (\$3-$JDBASE):(\$7*$plate_scale) with linespoints ";
  $cmd .=  "     pt 3 ps 0.5 lw 2 "; 
  $cmd .=  "    lc 3 t 'RA' ";

  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;

  # now a graph zooming in on one section
  $output_file = "./trackplot_b.ps";
  $term_options = "postscript color enhanced '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 reverse top Left left \n";
  printf CMDFILE "set xlabel 'Julian Date - $JDBASE'  \n";
  printf CMDFILE "set ylabel 'star motion (arcsec) ' \n";
  printf CMDFILE "set title 'PNV20 field over 2.6 hours, UT Sep 27 2017 '\n";
  
  printf CMDFILE "set object 1 rect from 23.56588,-400 to 23.57093,200 fc lt 1 fs solid 0.10 lw 0 \n";
  printf CMDFILE "set object 2 rect from 23.57226,-400 to 23.57780,200 fc lt 2 fs solid 0.10 lw 0 \n";
  printf CMDFILE "set object 3 rect from 23.57894,-400 to 23.58851,200 fc lt 3 fs solid 0.10 lw 0 \n";
  printf CMDFILE "set object 4 rect from 23.58957,-400 to 23.59662,200 fc lt 4 fs solid 0.10 lw 0 \n";
  printf CMDFILE "set object 5 rect from 23.61528,-400 to 23.62767,200 fc lt 5 fs solid 0.10 lw 0 \n";

  $cmd = "plot [][-20:30] ";
  $cmd .=  " 'prepare_track_data.out' using (\$3-$JDBASE):($offset+\$5*$plate_scale) with linespoints "; 
  $cmd .=  "   pt 6 ps 1.5  t 'Dec (offset by $offset arcsec)' ";
  $cmd .=  "  , ";
  $cmd .=  " 'prepare_track_data.out' using (\$3-$JDBASE):(\$7*$plate_scale) with linespoints ";
  $cmd .=  "     pt 3 ps 0.5 lw 2 "; 
  $cmd .=  "    lc 3 t 'RA' ";

  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;



  # now a graph showing FWHM and roundness over time
  $output_file = "./trackplot_c.ps";
  $term_options = "postscript color enhanced '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 reverse top right \n";
  printf CMDFILE "set xlabel 'Julian Date - $JDBASE'  \n";
  printf CMDFILE "set ylabel 'FWHM (pixels) or roundness ' \n";
  printf CMDFILE "set title 'PNV20 field over 2.6 hours, UT Sep 27 2017 '\n";

  printf CMDFILE "set object 1 rect from 23.56588,-400 to 23.57093,200 fc lt 1 fs solid 0.10 lw 0 \n";
  printf CMDFILE "set object 2 rect from 23.57226,-400 to 23.57780,200 fc lt 2 fs solid 0.10 lw 0 \n";
  printf CMDFILE "set object 3 rect from 23.57894,-400 to 23.58851,200 fc lt 3 fs solid 0.10 lw 0 \n";
  printf CMDFILE "set object 4 rect from 23.58957,-400 to 23.59662,200 fc lt 4 fs solid 0.10 lw 0 \n";
  printf CMDFILE "set object 5 rect from 23.61528,-400 to 23.62767,200 fc lt 5 fs solid 0.10 lw 0 \n";

  
  $cmd = "plot [][-2:8] ";
  $cmd .=  " 'check_coo.out' using (\$3-$JDBASE):(\$6) with points "; 
  $cmd .=  "   pt 6 ps 1.5 lw 2 t 'FWHM' ";
  $cmd .=  "  , ";
  $cmd .=  " 'check_coo.out' using (\$3-$JDBASE):(\$7) with points ";
  $cmd .=  "     pt 3 ps 0.5 lw 2 "; 
  $cmd .=  "    lc 3 t 'roundness' ";
  $cmd .=  " , ";
  $cmd .=  "    0  with lines lt 1 lc 3 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`;
  }



exit 0;




####################################################################
# PROCEDURE: find_median
#
# DESCRIPTION: Given the number of elements in an array,
#              and a pointer to the array,
#              compute the median value.
#
# USAGE:     find_median   num   array
#
#            where
#                   num              is number of elements in array
#                   array            is pointer to head of array
#
#
# RETURNS:     median value          if all goes well
#              -1                    if an error occurs
#
sub find_median {

  my $num = $_[0];
  my @array = $_[1];

  # check for bad input
  if ($num <= 1) {
    printf "find_median: first arg is only %5d, too small \n", $num;
    printf "find_median: returning -1 now \n";
    return(-1);
  }

  # sort the elements numerically, then pick the middle (or middle-1 if even)
  #   entry as the median value
    my @sorted_array = sort { $a <=> $b } @array;
    my $index = floor($num/2);
    if ($debug > 0) {
      printf "  num %5d  index %5d  \n", $num, $index;
    }
    my $result = $sorted_array[$index];

  return($result);
}
