#!/usr/bin/perl
#

$JDBASE = 2453000;


  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "curves_instr_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 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 bot right \n";
  printf CMDFILE "set ylabel 'Instrumental clear magnitude' \n";
  printf CMDFILE "set xlabel 'Julian Date - 2,457,000' \n";


  printf CMDFILE "set title 'IY UMa field, instrumental mag, Apr 11, 2006 UT'\n";

  $datafile = "./apr11_instr_mags.dat";
  
  $cmd = "plot [837.548:837.61][15.5:12.6] '$datafile' using (\$1-$JDBASE):(\$2)   ls 1 pt 1 t 'A' ";
  $cmd = $cmd . ", '$datafile' using (\$1-$JDBASE):(\$3) ls 3 pt 2  t 'B'";
  $cmd = $cmd . ", '$datafile' using (\$1-$JDBASE):(\$4) ls 4 pt 2  t 'C'";
  $cmd = $cmd . ", '$datafile' using (\$1-$JDBASE):(\$5) ls 2 pt 2  t 'IY'";

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




  # this version has differential magnitudes
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "curves_diff_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 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 bot right \n";
  printf CMDFILE "set ylabel 'Differential clear magnitude' \n";
  printf CMDFILE "set xlabel 'Julian Date - 2,457,000' \n";


  printf CMDFILE "set title 'IY UMa field, differential mag, Apr 11, 2006 UT'\n";

  $datafile = "./apr11_instr_mags.dat";
  
  $cmd = "plot [837.548:837.61][1.3:-1.5] '$datafile' using (\$1-$JDBASE):(\$2-\$3)   ls 1 pt 1 t 'A-B' ";
  $cmd = $cmd . ", '$datafile' using (\$1-$JDBASE):(\$3-\$3) ls 3 pt 2  t 'B-B'";
  $cmd = $cmd . ", '$datafile' using (\$1-$JDBASE):(\$4-\$3) ls 4 pt 2  t 'C-B'";
  $cmd = $cmd . ", '$datafile' using (\$1-$JDBASE):(\$5-\$3) ls 2 pt 2  t 'IY-B'";

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




  # this version has magnitudes roughly calibrated to V-band,
  #    using an offset of 13.45 mag between differential mags
  #    (relative to star B) and V-band mags.  In other words,
  #    setting V = 13.45 for star B.
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "curves_calib_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 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 bot right \n";
  printf CMDFILE "set ylabel 'Clear magnitude calibrated to V-band scale ' \n";
  printf CMDFILE "set xlabel 'Julian Date - 2,457,000' \n";


  printf CMDFILE "set title 'IY UMa field, pseudo-V mag, Apr 11, 2006 UT'\n";

  $datafile = "./apr11_instr_mags.dat";
  $calib_mag = 13.45;
  
  $cmd = "plot [837.548:837.61][1.3+$calib_mag:-1.5+$calib_mag] '$datafile' using (\$1-$JDBASE):((\$2-\$3)+$calib_mag)  ls 1 pt 1 t 'A' ";
  $cmd = $cmd . ", '$datafile' using (\$1-$JDBASE):((\$3-\$3)+$calib_mag)ls 3 pt 2  t 'B'";
  $cmd = $cmd . ", '$datafile' using (\$1-$JDBASE):((\$4-\$3)+$calib_mag)ls 4 pt 2  t 'C'";
  $cmd = $cmd . ", '$datafile' using (\$1-$JDBASE):((\$5-\$3)+$calib_mag)ls 2 pt 2  t 'IY'";

  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;
