#!/usr/bin/perl
#

$JDBASE = 2455000;


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

  # get the arguments
  $output_file = "empty_graph.ps";
  $term_options = "postscript color enhanced 'Helvtica,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 \n";
  printf CMDFILE "set xlabel 'Image number' \n";
  printf CMDFILE "set ylabel 'Instrumental magnitude ' \n";
  printf CMDFILE "set title 'Raw aperture magnitudes of stars near IY UMa '\n";
  
  $cmd = "plot [-5:50][-9:-14] 'empty.dat' using (\$1):(\$2) 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`;
  }



######################################################################
#  A graph showing only the variable star
  
  # get the arguments
  $output_file = "iy_only.ps";
  $term_options = "postscript color enhanced 'Helvtica,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 \n";
  printf CMDFILE "set xlabel 'Image number' \n";
  printf CMDFILE "set ylabel 'Instrumental magnitude ' \n";
  printf CMDFILE "set title 'Raw aperture magnitudes of stars near IY UMa '\n";
  
  $cmd = "plot [-5:50][-9:-14] 'hw/hw.dat' using (\$1):(\$4) ";
  $cmd .= "   with points pt 1 lw 4 t 'IY' ";



  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`;
  }


######################################################################
#  A graph showing the variable star and star A
  
  # get the arguments
  $output_file = "iy_a.ps";
  $term_options = "postscript color enhanced 'Helvtica,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 \n";
  printf CMDFILE "set xlabel 'Image number' \n";
  printf CMDFILE "set ylabel 'Instrumental magnitude ' \n";
  printf CMDFILE "set title 'Raw aperture magnitudes of stars near IY UMa '\n";
  
  $cmd = "plot [-5:50][-9:-14] 'hw/hw.dat' using (\$1):(\$4) ";
  $cmd .= "   with points pt 1 lw 4 t 'IY' ";
  $cmd .= " , ";
  $cmd .= "      'hw/hw.dat' using (\$1):(\$2) ";
  $cmd .= "   with points pt 3 lw 4 t 'A' ";



  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`;
  }



######################################################################
#  A graph showing the variable star and star A and star B
  
  # get the arguments
  $output_file = "iy_a_b.ps";
  $term_options = "postscript color enhanced 'Helvtica,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 \n";
  printf CMDFILE "set xlabel 'Image number' \n";
  printf CMDFILE "set ylabel 'Instrumental magnitude ' \n";
  printf CMDFILE "set title 'Raw aperture magnitudes of stars near IY UMa '\n";
  
  $cmd = "plot [-5:50][-9:-14] 'hw/hw.dat' using (\$1):(\$4) ";
  $cmd .= "   with points pt 1 lw 4 t 'IY' ";
  $cmd .= " , ";
  $cmd .= "      'hw/hw.dat' using (\$1):(\$2) ";
  $cmd .= "   with points pt 3 lw 4 t 'A' ";
  $cmd .= " , ";
  $cmd .= "      'hw/hw.dat' using (\$1):(\$3) ";
  $cmd .= "   with points pt 5 lw 4 t 'B' ";



  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`;
  }



######################################################################
#  A graph showing differential magnitude of star A relative to star A
  
  # get the arguments
  $output_file = "diff_a.ps";
  $term_options = "postscript color enhanced 'Helvtica,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 \n";
  printf CMDFILE "set xlabel 'Image number' \n";
  printf CMDFILE "set ylabel 'Magnitude relative to A' \n";
  printf CMDFILE "set title 'Differential magnitude of stars relative to A '\n";
  
  $cmd = "plot [-5:50][4:-1] 'hw/hw_diff.dat' using (\$1):(\$2-400) ";
  $cmd .= "   with points pt 1 lw 4 t '' ";
  $cmd .= " , ";
  $cmd .= "      'hw/hw_diff.dat' using (\$1):(\$2) ";
  $cmd .= "   with points pt 3 lw 4 t 'A' ";
  $cmd .= " , ";
  $cmd .= "      'hw/hw_diff.dat' using (\$1):(\$3-400) ";
  $cmd .= "   with points pt 5 lw 4 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`;
  }


######################################################################
#  A graph showing diff mag of star A, B relative to star A
  
  # get the arguments
  $output_file = "diff_a_b.ps";
  $term_options = "postscript color enhanced 'Helvtica,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 \n";
  printf CMDFILE "set xlabel 'Image number' \n";
  printf CMDFILE "set ylabel 'Magnitude relative to A' \n";
  printf CMDFILE "set title 'Differential magnitude of stars relative to A '\n";
  
  $cmd = "plot [-5:50][4:-1] 'hw/hw_diff.dat' using (\$1):(\$2-400) ";
  $cmd .= "   with points pt 1 lw 4 t '' ";
  $cmd .= " , ";
  $cmd .= "      'hw/hw_diff.dat' using (\$1):(\$2) ";
  $cmd .= "   with points pt 3 lw 4 t 'A' ";
  $cmd .= " , ";
  $cmd .= "      'hw/hw_diff.dat' using (\$1):(\$3) ";
  $cmd .= "   with points pt 5 lw 4 t 'B' ";



  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`;
  }



######################################################################
#  A graph showing diff mag of star A, B, IY relative to star A
  
  # get the arguments
  $output_file = "diff_a_b_iy.ps";
  $term_options = "postscript color enhanced 'Helvtica,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 \n";
  printf CMDFILE "set xlabel 'Image number' \n";
  printf CMDFILE "set ylabel 'Magnitude relative to A' \n";
  printf CMDFILE "set title 'Differential magnitude of stars relative to A '\n";
  
  $cmd = "plot [-5:50][4:-1] 'hw/hw_diff.dat' using (\$1):(\$4) ";
  $cmd .= "   with points pt 1 lw 4 t 'IY' ";
  $cmd .= " , ";
  $cmd .= "      'hw/hw_diff.dat' using (\$1):(\$2) ";
  $cmd .= "   with points pt 3 lw 4 t 'A' ";
  $cmd .= " , ";
  $cmd .= "      'hw/hw_diff.dat' using (\$1):(\$3) ";
  $cmd .= "   with points pt 5 lw 4 t 'B' ";



  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`;
  }



######################################################################
#  Closeup of star B alone.
  
  # get the arguments
  $output_file = "diff_b_closeup.ps";
  $term_options = "postscript color enhanced 'Helvtica,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 \n";
  printf CMDFILE "set xlabel 'Image number' \n";
  printf CMDFILE "set ylabel 'Magnitude relative to A' \n";
  printf CMDFILE "set title 'Differential magnitude of star B relative to A '\n";
  
  $cmd = "plot [-5:50][1.4:0.9] 'hw/hw_diff.dat' using (\$1):(\$4-300) ";
  $cmd .= "   with points pt 1 lw 4 t '' ";
  $cmd .= " , ";
  $cmd .= "      'hw/hw_diff.dat' using (\$1):(\$2-300) ";
  $cmd .= "   with points pt 3 lw 4 t '' ";
  $cmd .= " , ";
  $cmd .= "      'hw/hw_diff.dat' using (\$1):(\$3) ";
  $cmd .= "   with points pt 5 lw 4 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;

