#!/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.
#

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

  # get the arguments
  $output_file = "./trackplot.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 'image index number (120-sec exposures)' \n";
  printf CMDFILE "set ylabel 'image motion (arcsec) ' \n";
  printf CMDFILE "set title 'SDSS1538 field over 1.4 hours, UT May 12 2013 '\n";
  
  $cmd = "plot [-5:40][-20:8] ";
  $cmd .=  " 'do_multi.out' using 0:($offset+\$6*$plate_scale) with linespoints "; 
  $cmd .=  "    t 'Dec (offset by $offset arcsec)' ";
  $cmd .=  "  , ";
  $cmd .=  " 'do_multi.out' using 0:(\$9*$plate_scale) with lines 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;
