#!/usr/bin/perl
#



############################################################################
# Create a light curve showing V404 Cyg over the course of the night,
#   using magnitudes and JD.
#

  # get the arguments
  $output_file = "v404cyg_jun23_a.ps";
  $term_options = "postscript color enhanced 'Helvetica,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 size 1.0,1.0 \n";
  printf CMDFILE "set key top right \n";
  printf CMDFILE "set ylabel 'Differential unfiltered magnitude' \n";
  printf CMDFILE "set xlabel 'Universal Time on Jun 23, 2015' \n";
#  printf CMDFILE "set format x '' \n";
  printf CMDFILE "set xtics ( '5:00:00' 197.70833 , '6:00:00' 197.75, '7:00:00' 197.7917, '8:00:00' 197.8333) font 'Helvetica,18' \n";
  printf CMDFILE "set title '' \n";
#  printf CMDFILE "set label 1 '5:00' at 197.70833,0.20 center font 'Helvetica,28' \n";
#  printf CMDFILE "set label 2 '6:00' at 197.75000,0.20 center font 'Helvetica,28' \n";
#  printf CMDFILE "set label 3 '7:00' at 197.79167,0.20 center font 'Helvetica,28' \n";
#  printf CMDFILE "set label 4 '8:00' at 197.83333,0.20 center font 'Helvetica,28' \n";
#  printf CMDFILE "set label 5 '9:00' at 197.87500,0.25 center font 'Helvetica,28' \n";

  $mag_offset = 0;
  $JDBASE = 2457000.0;
  
  $cmd = "plot [197.69:197.87][4.0:0.0] 'star_v404cyg_v.dat4' ";
  $cmd .= "   using  (\$5-$JDBASE):(\$7-$mag_offset) ";
  $cmd .= "   lc 1 pt 7 ps 1.0 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 -flatten -background white $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }


exit 0;
