#!/usr/bin/perl
#

$JDBASE = 2455000;

$filter = "clear";
$filter_lc = lc($filter);
$mag_offset =  0.0;
$aper = "5";
$suffix = sprintf "dat%s", $aper;

# make the Postscript graph
make_graph("drift_aug27.ps", "postscript color");

# make a PNG graph
#make_graph("hs2331_" . $aper . "_field_jul23.png", "png color");

exit 0;



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

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

  $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 bot right \n";
  printf CMDFILE "set xlabel 'image index number (approx 3 minutes each)' \n";
  printf CMDFILE "set ylabel 'motion in pixels (approx 1.8 arcsec each)' \n";
  printf CMDFILE "set title 'Drift of stars in MB82897 field, 14-inch, Aug 28 2010 UT'\n", $aper;
  
  $cmd = "plot [-2:30][-15:15] './do_multi.out' using (\$6):(0.0-\$10) ";
  $cmd .= "      lt 1 pt 1 lw 4  t 'rows (Dec)' ";
  $cmd = $cmd . ", './do_multi.out' using  (\$6):(0.0-\$13) ";
  $cmd .= "      lt 2 pt 2 lw 4  t 'cols (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`;
  }

}
