#!/usr/bin/perl
#

$JDBASE = 2457000.0;

$date = "2015 Jun 22";
$target = "_v404cyg";
$filter = "I";
$filter_lc = lc($filter);
$mag_offset = 0.0;
$aper = "5";
$suffix = sprintf "_%s.dat%d", $filter_lc, $aper;

# this has magnitude vs. time
make_graph($target . "_". $aper . "" . $filter . "_hw_jun22.ps", "postscript color 'Helvetica,18' ");

# this has flux vs. time
make_graph_2($target . "_". $aper . "" . $filter . "_hw2_jun22.ps", "postscript color 'Helvetica,18' ");


# this has flux vs. time for entire run
make_graph_3($target . "_". $aper . "" . $filter . "_hw3_jun22.ps", "postscript color 'Helvetica,18' ");



# 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 top right \n";
  printf CMDFILE "set ylabel 'Approximate $filter magnitude' \n";
  printf CMDFILE "set xlabel 'Julian Date - $JDBASE' \n";
  printf CMDFILE "set title '$target field, $filter $aper-pix diff mag, 12-inch, $date UT'\n";
  
  $cmd = "plot [195.7255:195.7292][4.0:0.5] 'starD$suffix' using (\$5-$JDBASE):(\$7-$mag_offset)   lt 1 pt 1 ps 1.5 lw 4 t 'D' ";
  $cmd = $cmd . ", 'star_v404cyg$suffix' using (\$5-$JDBASE):(\$7-$mag_offset) lt 2 pt 2 ps 1.5 lw 4 t 'V404'";
  $cmd = $cmd . ", 'starF$suffix' using (\$5-$JDBASE):(\$7-$mag_offset) lt 3 pt 3 ps 1.5  lw 4 t 'F'";
#  $cmd = $cmd . ", 'starP$suffix' using (\$5-$JDBASE):(\$7-$mag_offset) lc 4 pt 5  ps 0.5 lw 4 t 'P'";
#  $cmd = $cmd . ", 'starQ$suffix' using (\$5-$JDBASE):(\$7-$mag_offset) lc 5 pt 4 ps 0.3  lw 4 t 'Q'";

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

}



############################################################################
# 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_2 {
  
  my($output_file, $term_options);

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

  $cmdfile = "gnuplot.in";
  
  $mag_offset = 25.0;

  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 ylabel 'Counts in aperture ' \n";
  printf CMDFILE "set xlabel 'Image index number ' \n";
  printf CMDFILE "set title '$target field, $filter $aper-pix diff mag, 12-inch, $date UT'\n";
  
  $cmd = "plot [-2:15][0:35000] 'starD_multi.dat' using (\$2):(10.0**(-0.4*(\$6-$mag_offset)))   lt 1 pt 1 ps 1.5 lw 4 t 'D' ";
  $cmd = $cmd . ", 'starV404_multi.dat' using (\$2):(10.0**(-0.4*(\$6-$mag_offset))) lt 2 pt 2 ps 1.5 lw 4 t 'V404'";
  $cmd = $cmd . ", 'starF_multi.dat' using (\$2):(10**(-0.4*(\$6-$mag_offset))) lt 3 pt 3 ps 1.5  lw 4 t 'F'";

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

}


############################################################################
# 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_3 {
  
  my($output_file, $term_options);

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

  $cmdfile = "gnuplot.in";
  
  $mag_offset = 25.0;

  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 ylabel 'Counts in aperture ' \n";
  printf CMDFILE "set xlabel 'Image index number ' \n";
  printf CMDFILE "set title '$target field, $filter $aper-pix diff mag, 12-inch, $date UT'\n";
  
  $cmd = "plot [-5:75][0:35000] 'starD_multi.dat' using (\$2):(10.0**(-0.4*(\$6-$mag_offset)))   lt 1 pt 1 ps 1.5 lw 4 t 'D' ";
  $cmd = $cmd . ", 'starV404_multi.dat' using (\$2):(10.0**(-0.4*(\$6-$mag_offset))) lt 2 pt 2 ps 1.5 lw 4 t 'V404'";
  $cmd = $cmd . ", 'starF_multi.dat' using (\$2):(10**(-0.4*(\$6-$mag_offset))) lt 3 pt 3 ps 1.5  lw 4 t 'F'";

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

}
