#!/usr/bin/perl
#

$JDBASE = 2456000;

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


# make the Postscript graph
make_graph("uzboo_all_" . "_aug29ut.ps", "postscript color 'Helvetica,18' ");

# make a PNG graph
#make_graph("mvlyr_" . $aper . "_jul23ut.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 left \n";
  printf CMDFILE "set nokey \n";
  printf CMDFILE "set xlabel 'Julian Date - $JDBASE ' \n";
  printf CMDFILE "set ylabel 'magnitude (V or clear, set to match V)' \n";
  printf CMDFILE "set title 'UZ Boo measurements at RIT Obs '\n", $aper;
  
  $cmd = "plot [][18.3:11.5] '~/z1/ritobs/jul26_2013/work/pff/uzboo_jul27ut.dat' using (\$2-$JDBASE):(\$4-($mag_offset+0.00)) lc 1 pt 4 ps 0.5 lw 3 t 'UT Jul 27 ' ";
  $cmd = $cmd . ", ";
  $cmd = $cmd . " '~/z1/ritobs/aug04_2013/work/pff/uzboo_aug05ut.dat' ";
  $cmd = $cmd . "     using ((\$2-0)-$JDBASE):(\$4-($mag_offset-0.00)) ";
  $cmd = $cmd . "     with points  lc 2 pt 4 ps 0.5 lw 3 t 'UT Aug 05' ";
  $cmd = $cmd . ", ";
  $cmd = $cmd . " '~/z1/ritobs/aug09_2013/work/pff/uzboo_aug10ut.dat' ";
  $cmd = $cmd . "     using ((\$2-0)-$JDBASE):(\$4-($mag_offset-0.00)) ";
  $cmd = $cmd . "     with points  lc 3 pt 4 ps 1.5 lw 3 t 'UT Aug 10' ";
  $cmd = $cmd . ", ";
  $cmd = $cmd . " '~/z1/ritobs/aug10_2013/work/pff/uzboo_aug11ut.dat' ";
  $cmd = $cmd . "     using ((\$2-0)-$JDBASE):(\$4-($mag_offset-0.00)) ";
  $cmd = $cmd . "     with lines  lc -1  lt -1 lw 3 t 'UT Aug 11' ";
  $cmd = $cmd . ", ";
  $cmd = $cmd . " '~/z1/ritobs/aug14_2013/work/pff/uzboo_aug15ut.dat' ";
  $cmd = $cmd . "     using ((\$2-0)-$JDBASE):(\$4-($mag_offset-0.00)) ";
  $cmd = $cmd . "     with points  lc 1  pt 4 ps 0.5 lw 4 t 'UT Aug 15' ";
  $cmd = $cmd . ", ";
  $cmd = $cmd . " '~/z1/ritobs/aug15_2013/work/pff/uzboo_aug16ut.dat' ";
  $cmd = $cmd . "     using ((\$2-0)-$JDBASE):(\$4-($mag_offset-0.00)) ";
  $cmd = $cmd . "     with points  lc 2  pt 4 ps 0.5 lw 4 t 'UT Aug 16' ";
  $cmd = $cmd . ", ";
  $cmd = $cmd . " '~/z1/ritobs/aug16_2013/work/pff/uzboo_aug17ut.dat' ";
  $cmd = $cmd . "     using ((\$2-0)-$JDBASE):(\$4-($mag_offset-0.00)) ";
  $cmd = $cmd . "     with points  lc 3  pt 4 ps 0.3 lw 2 t 'UT Aug 17' ";
  $cmd = $cmd . ", ";
  $cmd = $cmd . " '~/z1/ritobs/aug18_2013/work/pff/uzboo_aug19ut.dat' ";
  $cmd = $cmd . "     using ((\$2-0)-$JDBASE):(\$4-($mag_offset-0.00)) ";
  $cmd = $cmd . "     with points  lc -1  pt 4 ps 0.3 lw 2 t 'UT Aug 19' ";
  $cmd = $cmd . ", ";
  $cmd = $cmd . " '~/z1/ritobs/aug19_2013/work/pff/uzboo_aug20ut.dat' ";
  $cmd = $cmd . "     using ((\$2-0)-$JDBASE):(\$4-($mag_offset-0.00)) ";
  $cmd = $cmd . "     with points  lc 1  pt 4 ps 0.5 lw 4 t 'UT Aug 20' ";
  $cmd = $cmd . ", ";
  $cmd = $cmd . " '~/z1/ritobs/aug20_2013/work/pff/uzboo_aug21ut.dat' ";
  $cmd = $cmd . "     using ((\$2-0)-$JDBASE):(\$4-($mag_offset-0.00)) ";
  $cmd = $cmd . "     with points  lc 2  pt 4 ps 0.5 lw 4 t 'UT Aug 21' ";
  $cmd = $cmd . ", ";
  $cmd = $cmd . " '~/z1/ritobs/aug23_2013/work/pff/uzboo_aug24ut.dat' ";
  $cmd = $cmd . "     using ((\$2-0)-$JDBASE):(\$4-($mag_offset-0.00)) ";
  $cmd = $cmd . "     with points  lc 3  pt 4 ps 0.5 lw 4 t 'UT Aug 24' ";
  $cmd = $cmd . ", ";
  $cmd = $cmd . " '~/z1/ritobs/aug24_2013/work/pff/uzboo_aug25ut.dat' ";
  $cmd = $cmd . "     using ((\$2-0)-$JDBASE):(\$4-($mag_offset-0.00)) ";
  $cmd = $cmd . "     with points  lc -1  pt 4 ps 0.3 lw 2 t 'UT Aug 25' ";
  $cmd = $cmd . ", ";
  $cmd = $cmd . " '~/z1/ritobs/aug28_2013/work/pff/uzboo_aug29ut.dat' ";
  $cmd = $cmd . "     using ((\$2-0)-$JDBASE):(\$4-($mag_offset-0.00)) ";
  $cmd = $cmd . "     with points  lc 1  pt 4 ps 0.5 lw 4 t 'UT Aug 29' ";
  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`;
  }


  
}
  
  

