#!/usr/bin/perl
#

$JDBASE = 2459600;


###################################################################
# Make a light curve showing intensity for each star
#

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


#  printf CMDFILE "set title 'V404 Cyg field, $filter $aper-pix diff mag, 12-inch, $date UT'\n";
  printf CMDFILE "set title 'Light curve in intensity units '\n";
  
  $cmd = "plot [][0:1300] 'intensity_a.dat' using (\$1-$JDBASE):(\$2) ";
  $cmd .= "      ls 1 pt 3 ps 0.3 t 'A' ";
  $cmd .= " , ";
  $cmd .= "         'intensity_a.dat' using (\$1-$JDBASE):(\$3) ";
  $cmd .= "      ls 2 pt 4 ps 0.3 t 'B' ";
  $cmd .= " , ";
  $cmd .= "         'intensity_a.dat' using (\$1-$JDBASE):(\$4) ";
  $cmd .= "      ls 3 pt 6 ps 0.4 t 'C' ";

  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 magnitude for each star
#

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


  printf CMDFILE "set title 'Light curve in magnitude units '\n";
  
  $cmd = "plot [][15:12] 'mag_a.dat' using (\$1-$JDBASE):(\$2) ";
  $cmd .= "      ls 1 pt 3 ps 0.3 t 'A' ";
  $cmd .= " , ";
  $cmd .= "         'mag_a.dat' using (\$1-$JDBASE):(\$3) ";
  $cmd .= "      ls 2 pt 4 ps 0.3 t 'B' ";
  $cmd .= " , ";
  $cmd .= "         'mag_a.dat' using (\$1-$JDBASE):(\$4) ";
  $cmd .= "      ls 3 pt 6 ps 0.4 t 'C' ";

  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;
