#!/usr/bin/perl
#

$JDBASE = 2458000.0;

$date = "2019 Sep 19";
$target = "_gsc";
$filter = "v";
$filter_lc = lc($filter);
$mag_offset = 0;
$mag_offset = 3.392 - 13.350;
# mag_offset = -10.707;
$aper = "4";
$suffix = sprintf "_%s.dat%d", $filter_lc, $aper;

# make the Postscript graph
make_graph($target . "_". $aper . "" . $filter . "_field_sep19.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";


  # revert to some oldish default colors
  printf CMDFILE "set style line 1 lt rgb 'red' lw 3  \n";
  printf CMDFILE "set style line 2 lt rgb 'green' lw 3  \n";
  printf CMDFILE "set style line 3 lt rgb 'blue' lw 3  \n";
  printf CMDFILE "set style line 4 lt rgb 'cyan' lw 3  \n";
  printf CMDFILE "set style line 5 lt rgb 'violet' lw 3  \n";


  printf CMDFILE "set grid \n";
  printf CMDFILE "set key outside 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 (bin 2x2) diff mag, 12-inch, $date UT'\n";
  
  $cmd = "plot [][16.0:13.0] 'starA$suffix' using (\$5-$JDBASE):(\$7-$mag_offset)   ls 1 pt 1 lw 4 t 'A' ";
#  $cmd = "plot [][16.0:15.0] 'starA$suffix' using (\$5-$JDBASE):(\$7-$mag_offset)   lt 1 pt 1 lw 4 t 'A' ";
  $cmd = $cmd . ", 'star_gsc$suffix' using (\$5-$JDBASE):(\$7-$mag_offset) with points ls 2 pt 2 ps 0.5 lw 4 t 'GSC'";
  $cmd = $cmd . ", 'starB$suffix' using (\$5-$JDBASE):(\$7-$mag_offset) ls 3 pt 3  lw 1 t 'B'";
  $cmd = $cmd . ", 'starC$suffix' using (\$5-$JDBASE):(\$7-$mag_offset) ls 4 pt 5  ps 0.5 lw 4 t 'C'";
  $cmd = $cmd . ", 'starD$suffix' using (\$5-$JDBASE):(\$7-$mag_offset) ls 5 pt 4 ps 0.3  lw 4 t 'D'";
  $cmd = $cmd . ", 'starE$suffix' using (\$5-$JDBASE):(\$7-$mag_offset) ls -1 pt 6 ps 0.3 lw 2 t 'E'";
#  $cmd = $cmd . ", 'starVEND$suffix' using (\$5-$JDBASE):(\$7-$mag_offset) lc 1 pt 3  ps 0.3 lw 2 t 'VEND'";
#  $cmd = $cmd . ", 'starX$suffix' using (\$5-$JDBASE):(\$7-$mag_offset) lc 1 pt 6  ps 0.3 lw 2 t 'X'";
#  $cmd = $cmd . ", 'starY$suffix' using (\$5-$JDBASE):(\$7-$mag_offset) lc 2 pt 2  ps 2.3 lw 2 t 'Y'";

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

}
