#!/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 ".dat";




############################################################################
#  Graph showing flux for D,F,P,Q as function of image index
#
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "flux_all.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";
  printf CMDFILE "set grid \n";
  printf CMDFILE "set key top right \n";
  printf CMDFILE "set ylabel 'Counts inside 5-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 [-5:85][] 'flux_d.dat' using (\$1):(\$4)   lt 1 pt 1 lw 4 t 'D' ";
  $cmd = $cmd . ", 'flux_f.dat' using (\$1):(\$4) lt 3 pt 3  lw 4 t 'F'";
  $cmd = $cmd . ", 'flux_p.dat' using (\$1):(\$4) lc 4 pt 5  ps 0.5 lw 4 t 'P'";
  $cmd = $cmd . ", 'flux_q.dat' using (\$1):(\$4) 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`;
  }



############################################################################
#  Graph showing instr mag for D,F,P,Q as function of image index
#
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "instr_mag_all.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";
  printf CMDFILE "set grid \n";
  printf CMDFILE "set key top right \n";
  printf CMDFILE "set ylabel 'Instrumental mag in 5-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 [-5:85][18.2:14.0] 'mag_d.dat.diff' using (\$1):(\$2)   lt 1 pt 1 lw 4 t 'D' ";
  $cmd = $cmd . ", 'mag_f.dat.diff' using (\$1):(\$2) lt 3 pt 3  lw 4 t 'F'";
  $cmd = $cmd . ", 'mag_p.dat.diff' using (\$1):(\$2) lc 4 pt 5  ps 0.5 lw 4 t 'P'";
  $cmd = $cmd . ", 'mag_q.dat.diff' using (\$1):(\$2) 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`;
  }



############################################################################
#  Graph showing differential mag for D,F,P,Q as function of image index
#
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "diff_mag_all.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";
  printf CMDFILE "set grid \n";
  printf CMDFILE "set key top right \n";
  printf CMDFILE "set ylabel '(mag - mag_of_D) in 5-pix 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:85][4.0:-0.2] 'mag_d.dat.diff' using (\$1):(\$4)   lt 1 pt 1 lw 4 t 'D' ";
  $cmd = $cmd . ", 'mag_f.dat.diff' using (\$1):(\$4) lt 3 pt 3  lw 4 t 'F'";
  $cmd = $cmd . ", 'mag_p.dat.diff' using (\$1):(\$4) lc 4 pt 5  ps 0.5 lw 4 t 'P'";
  $cmd = $cmd . ", 'mag_q.dat.diff' using (\$1):(\$4) 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`;
  }






exit 0;

