#!/usr/bin/perl
#

$JDBASE = 2457000.0;

$date = "2015 Jun 22";
$target = "V404 Cyg";
$filter = "I";
$filter_lc = lc($filter);
$mag_offset = 0.0;
$aper = "5";
$suffix = sprintf ".dat";



############################################################################
#  Graph showing instr mag for A,B,C,V404 as function of image index
#     for a subset of just 10 images
#
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "instr_mag_a.ps";
  $term_options = "postscript color '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";

  #  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 'sea-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 top right outside \n";
  printf CMDFILE "set ylabel 'Instrumental mag in 7-pix aperture ' \n";
  printf CMDFILE "set xlabel 'Image index number ' \n";
  printf CMDFILE "set title '$target field, $filter $aper-pix sky-sub flux, 12-inch, $date UT'\n";
  
  $cmd = "plot [399:411][16.0:13.0] 'instr_mag.dat' using (\$1):(\$2)   ls 1 t 'A' ";
  $cmd .= " , ";
  $cmd .= "   'instr_mag.dat' using (\$1):(\$3)   ls 3 t 'B' ";
  $cmd .= " , ";
  $cmd .= "   'instr_mag.dat' using (\$1):(\$4)   ls 4 t 'C' ";
  $cmd .= " , ";
  $cmd .= "   'instr_mag.dat' using (\$1):(\$5)   ls 2 t 'V404' ";

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



############################################################################
#  Graph showing instr mag for A,B,C,V404 as function of image index
#     for all 70 images
#
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "instr_mag_b.ps";
  $term_options = "postscript color '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";

  #  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 'sea-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 top right outside \n";
  printf CMDFILE "set ylabel 'Instrumental mag in 7-pix aperture ' \n";
  printf CMDFILE "set xlabel 'Image index number ' \n";
  printf CMDFILE "set title '$target field, $filter $aper-pix sky-sub flux, 12-inch, $date UT'\n";
  
  $cmd = "plot [380:470][16.0:13.0] 'mag_all.dat' using (\$1+390):(\$7)   ls 1 t 'A' ";
  $cmd .= " , ";
  $cmd .= "   'mag_all.dat' using (\$1+390):(\$14)   ls 3 t 'B' ";
  $cmd .= " , ";
  $cmd .= "   'mag_all.dat' using (\$1+390):(\$21)   ls 4 t 'C' ";
  $cmd .= " , ";
  $cmd .= "   'mag_all.dat' using (\$1+390):(\$28)   ls 2 t 'V404' ";

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



############################################################################
#  Graph showing differential mag for A,B,C,V404 as function of image index
#     for all 70 images
#
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "diff_mag_a.ps";
  $term_options = "postscript color '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";

  #  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 'sea-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 top right outside \n";
  printf CMDFILE "set ylabel 'Instrumental mag in 7-pix aperture ' \n";
  printf CMDFILE "set xlabel 'Image index number ' \n";
  printf CMDFILE "set title '$target field, $filter $aper-pix sky-sub flux, 12-inch, $date UT'\n";
  
  $cmd = "plot [380:470][2.8:-0.2] 'mag_all.dat' using (\$1+390):(\$7-\$7)   ls 1 t 'A' ";
  $cmd .= " , ";
  $cmd .= "   'mag_all.dat' using (\$1+390):(\$14-\$7)   ls 3 t 'B' ";
  $cmd .= " , ";
  $cmd .= "   'mag_all.dat' using (\$1+390):(\$21-\$7)   ls 4 t 'C' ";
  $cmd .= " , ";
  $cmd .= "   'mag_all.dat' using (\$1+390):(\$28-\$7)   ls 2 t 'V404' ";

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


############################################################################
#  Graph showing calibrated mag for A,B,C,V404 as function of image index
#     for all 70 images
#
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "cal_mag_a.ps";
  $term_options = "postscript color '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";

  #  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 'sea-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 top right outside \n";
  printf CMDFILE "set ylabel 'Approximate I-band mag in 7-pix aperture ' \n";
  printf CMDFILE "set xlabel 'Image index number ' \n";
  printf CMDFILE "set title '$target field, $filter $aper-pix sky-sub flux, 12-inch, $date UT'\n";

  $delta = 3.12;
  
  $cmd = "plot [380:470][13.0:10.0] 'mag_all.dat' using (\$1+390):(\$7-$delta)   ls 1 t 'A' ";
  $cmd .= " , ";
  $cmd .= "   'mag_all.dat' using (\$1+390):(\$14-$delta)   ls 3 t 'B' ";
  $cmd .= " , ";
  $cmd .= "   'mag_all.dat' using (\$1+390):(\$21-$delta)   ls 4 t 'C' ";
  $cmd .= " , ";
  $cmd .= "   'mag_all.dat' using (\$1+390):(\$28-$delta)   ls 2 t 'V404' ";

  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;

