#!/usr/bin/perl
#

$JDBASE = 0;


############################################################################
#
# Graph of the "raw" instrumental magnitudes
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "raw_a.ps";
  $term_options = "postscript color enhanced 'Helvtica,18' ";

  #  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"; 


  $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 xlabel 'Image number' \n";
  printf CMDFILE "set ylabel 'Instrumental magnitude ' \n";
  printf CMDFILE "set title 'Raw aperture magnitudes of stars near IY UMa '\n", $aper;
  
  $cmd = "plot [-5:105][16:11] 'hw_2026.dat' using (\$1):(\$2) ";
  $cmd .= "            pt 3 ps 0.6 t 'A' ";
  $cmd .= " , ";
  $cmd .= "   'hw_2026.dat' using (\$1):(\$3) pt 4 ps 0.8 t 'B' ";
  $cmd .= " , ";
  $cmd .= "   'hw_2026.dat' using (\$1):(\$4) pt 6 ps 0.8 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`;
  }




############################################################################
#
# Graph of magnitudes relative to star "A"
  

  # get the arguments
  $output_file = "relative_a.ps";
  $term_options = "postscript color enhanced 'Helvtica,18' ";

  #  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"; 


  $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 xlabel 'Image number' \n";
  printf CMDFILE "set ylabel 'Instrumental magnitude ' \n";
  printf CMDFILE "set title 'Mag of stars near IY UMa (rel to star A) '\n", $aper;
  
  $cmd = "plot [-5:105][4:-1] 'hw_2026.dat' using (\$1):(\$2-\$2) ";
  $cmd .= "            pt 3 ps 0.6 t 'A' ";
  $cmd .= " , ";
  $cmd .= "   'hw_2026.dat' using (\$1):(\$3-\$2) pt 4 ps 0.8 t 'B' ";
  $cmd .= " , ";
  $cmd .= "   'hw_2026.dat' using (\$1):(\$4-\$2) pt 6 ps 0.8 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;

