#!/usr/bin/perl
#

$JDBASE = 2456830;
$filter = "clear";
$aper = "5";
$date = "June 22, 2014";



############################################################################
# make a graph showing intensity as function of time
#
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "intensity_plot.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";

  #  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 outside top right \n";
  printf CMDFILE "set ylabel 'Intensity ' \n";
  printf CMDFILE "set xlabel 'Julian Date - $JDBASE' \n";
  printf CMDFILE "set xtics 0.001 \n";


  printf CMDFILE "set title 'ASAS14cl , $filter $aper-pix diff mag, 12-inch, $date UT'\n";
  
  $infile = "./grab_cols.out";
  
  $cmd = "plot [0.708:0.715][]  ";
  $cmd = $cmd . " '$infile'  using (\$6-$JDBASE):(\$8) ls 1 pt 2  t 'ASAS'";
  $cmd = $cmd . ", '$infile' using (\$6-$JDBASE):(\$11) ls 3 pt 3  t 'C2'";
  $cmd = $cmd . ", '$infile' using (\$6-$JDBASE):(\$14) ls 2 pt 4  t 'C3'";
  $cmd = $cmd . ", '$infile' using (\$6-$JDBASE):(\$17) ls 5 pt 5  t 'C4'";

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



############################################################################
# make a graph showing relative magnitude as function of time
#
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "relmag_plot.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";

  #  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 outside top right \n";
  printf CMDFILE "set ylabel 'Magnitude relative to C2 ' \n";
  printf CMDFILE "set xlabel 'Julian Date - $JDBASE' \n";
  printf CMDFILE "set xtics 0.001 \n";


  printf CMDFILE "set title 'ASAS14cl , $filter $aper-pix diff mag, 12-inch, $date UT'\n";
  
  $infile = "./grab_cols.out";
  
  $cmd = "plot [0.708:0.715][0.8:-0.1]  ";
  $cmd = $cmd . " '$infile'  using (\$6-$JDBASE):(0.0-2.5*log(\$8/\$11)/log(10.0)) ls 1 pt 2  t 'ASAS'";
  $cmd = $cmd . ", '$infile' using (\$6-$JDBASE):(0.0-2.5*log(\$11/\$11)/log(10.0)) ls 3 pt 3  t 'C2'";
  $cmd = $cmd . ", '$infile' using (\$6-$JDBASE):(0.0-2.5*log(\$14/\$11)/log(10.0)) ls 2 pt 4  t 'C3'";
  $cmd = $cmd . ", '$infile' using (\$6-$JDBASE):(0.0-2.5*log(\$17/\$11)/log(10.0)) ls 5 pt 5  t 'C4'";

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



############################################################################
# make a graph showing V-band-ish magnitude as function of time
#
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "calibmag_plot.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";

  #  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 outside top right \n";
  printf CMDFILE "set ylabel 'Magnitude on V-band scale ' \n";
  printf CMDFILE "set xlabel 'Julian Date - $JDBASE' \n";
  printf CMDFILE "set xtics 0.001 \n";


  printf CMDFILE "set title 'ASAS14cl , $filter $aper-pix diff mag, 12-inch, $date UT'\n";
  
  $infile = "./grab_cols.out";
  
  $cmd = "plot [0.708:0.715][11.863+0.8:11.863-0.1]  ";
  $cmd = $cmd . " '$infile'  using (\$6-$JDBASE):(11.863-2.5*log(\$8/\$11)/log(10.0)) ls 1 pt 2  t 'ASAS'";
  $cmd = $cmd . ", '$infile' using (\$6-$JDBASE):(11.863-2.5*log(\$11/\$11)/log(10.0)) ls 3 pt 3  t 'C2'";
  $cmd = $cmd . ", '$infile' using (\$6-$JDBASE):(11.863-2.5*log(\$14/\$11)/log(10.0)) ls 2 pt 4  t 'C3'";
  $cmd = $cmd . ", '$infile' using (\$6-$JDBASE):(11.863-2.5*log(\$17/\$11)/log(10.0)) ls 5 pt 5  t 'C4'";

  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;
