#!/usr/bin/perl
#

$JDBASE = 2455000.0;


###################################################################
# Make a light curve showing relative mag for stars on all nights
#

  $output_file = "rel_mag_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";

  #  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 2  \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 \n";
  printf CMDFILE "set ylabel 'Magnitude relative to star A' \n";
  printf CMDFILE "set xlabel 'Julian Date - $JDBASE ' \n";


#  printf CMDFILE "set title 'V404 Cyg field, $filter $aper-pix diff mag, 12-inch, $date UT'\n";
  printf CMDFILE "set title 'Light curve, magnitude relative to star A '\n";
  
  $cmd = "plot [780:950][0.3:-3.5] 'peakpix.dat' using (\$2):(0.0-2.5*(log( (\$3/\$3) )/log(10.0) ) ) ";
  $cmd .= "      ls 1 pt 3 ps 0.5 t 'A' ";
  $cmd .= " , ";
  $cmd .= "         'peakpix.dat' using (\$2):(0.0-2.5*(log( (\$4/\$3) )/log(10.0)) ) ";
  $cmd .= "      ls 2 pt 4 ps 0.5 t 'B' ";
  $cmd .= " , ";
  $cmd .= "         'peakpix.dat' using (\$2):(0.0-2.5*(log( (\$5/\$3) )/log(10.0)) ) ";
  $cmd .= "      ls 3 pt 6 ps 1.0 t 'C' ";
  $cmd .= " , ";
  $cmd .= "         'peakpix.dat' using (\$2):(0.0-2.5*(log( (\$6/\$3) )/log(10.0)) ) ";
  $cmd .= "      ls 4 pt 6 ps 0.5 t 'D' ";

  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;




###################################################################
# Make a light curve showing phased light curve for candidate
#   period = 1.92 days
#

  $output_file = "phase_a.ps";
  $term_options = "postscript color enhanced";

  $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 \n";
  printf CMDFILE "set ylabel 'Intensity' \n";
  printf CMDFILE "set xlabel 'Julian Date - $JDBASE ' \n";

  $P = 1.92;

  printf CMDFILE "set title 'Light curve, phased with period $P days' \n";
  
  $cmd = "plot [-0.1:1.1][9.8:8.5] 'all_calib_mag.dat' using ";
  $cmd .= "   ((\$1/$P)-int(\$1/$P)):3 ";
  $cmd .= "      ls 1 pt 3 ps 0.3 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 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }



###################################################################
# Make a light curve showing phased light curve for candidate
#   period = 0.64 days
#

  $output_file = "phase_b.ps";
  $term_options = "postscript color enhanced";

  $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 \n";
  printf CMDFILE "set ylabel 'Intensity' \n";
  printf CMDFILE "set xlabel 'Julian Date - $JDBASE ' \n";

  $P = 0.64;

  printf CMDFILE "set title 'Light curve, phased with period $P days' \n";
  
  $cmd = "plot [-0.1:1.1][9.8:8.5] 'all_calib_mag.dat' using ";
  $cmd .= "   ((\$1/$P)-int(\$1/$P)):3 ";
  $cmd .= "      ls 2 pt 3 ps 0.3 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 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }



###################################################################
# Make a light curve showing phased light curve for candidate
#   period = 0.38 days
#

  $output_file = "phase_c.ps";
  $term_options = "postscript color enhanced";

  $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 \n";
  printf CMDFILE "set ylabel 'Intensity' \n";
  printf CMDFILE "set xlabel 'Julian Date - $JDBASE ' \n";

  $P = 0.38;

  printf CMDFILE "set title 'Light curve, phased with period $P days' \n";
  
  $cmd = "plot [-0.1:1.1][9.8:8.5] 'all_calib_mag.dat' using ";
  $cmd .= "   ((\$1/$P)-int(\$1/$P)):3 ";
  $cmd .= "      ls 3 pt 3 ps 0.3 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 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }


###################################################################
# Make a light curve showing phased light curve for candidate
#   period = 0.629 days
#

  $output_file = "phase_d.ps";
  $term_options = "postscript color enhanced";

  $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 \n";
  printf CMDFILE "set ylabel 'Intensity' \n";
  printf CMDFILE "set xlabel 'Julian Date - $JDBASE ' \n";

  $P = 0.629;

  printf CMDFILE "set title 'Light curve, phased with period $P days' \n";
  
  $cmd = "plot [-0.1:1.1][9.8:8.5] 'all_calib_mag.dat' using ";
  $cmd .= "   ((\$1/$P)-int(\$1/$P)):3 ";
  $cmd .= "      ls 0 pt 3 ps 0.3 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 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }




exit 0;
