#!/usr/bin/perl
#

$JDBASE = 2455000;

$filter = "clear";
$filter_lc = lc($filter);
$mag_offset = 0.000;
$aper = "";
$suffix = sprintf "dat%s", $aper;


# make the Postscript graph
make_graph("stein" . $aper . "_jul07ut.ps", "postscript color");

# make a PNG graph
#make_graph("mvlyr_" . $aper . "_jul23ut.png", "png color");

exit 0;



############################################################################
# create a file with GNUPLOT commands, then execute GNUPLOT to read
#   from that file.
#
# usage:  make_graph   outfile_file_name  set_term_options
#
sub make_graph {
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = $_[0];
  $term_options = $_[1];

  $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 left \n";
  printf CMDFILE "set xlabel 'Julian Date - $JDBASE' \n";
  printf CMDFILE "set title 'SDSSJ162520.3+120309 $filter 4-pix diff mag, 14-inch, Jul 07 2010 UT'\n", $aper;
  
  #$cmd = "plot [384.58:384.76][0.5:-0.15] 'starA.$suffix' using (\$5-$JDBASE):(\$7-($mag_offset+1.05)):8 with errorbars   lt 1 pt 4 ps 0.8 lw 3 t 'shifted star A' ";
  $cmd = "plot [384.58:384.90][0.35:-0.05] 'starC.$suffix' using (\$5-$JDBASE):(\$7-($mag_offset+9.00)):8 with errorbars   lt 4 pt 4 ps 0.8 lw 3 t '' ";
  $cmd = $cmd . ", ";
  $cmd = $cmd . " 'starSDSS.$suffix' using (\$5-$JDBASE):(\$7-($mag_offset+0.05)) lt 2 pt 4 ps 0.5 lw 3 t 'RIT' ";
  $cmd = $cmd . ", ";
  $cmd = $cmd . " 'starB.$suffix' using (\$5-$JDBASE):(\$7-($mag_offset+9.35)):8 with errorbars  pt 3  ps 0.5 lw 4 t '' ";
   $cmd = $cmd . ", 'stein.dat' using (\$1-0):(\$2-($mag_offset+2.15)) with points pt 2 ps 0.5 lw 2 t 'Stein (arbitrary offset)'";
  #$cmd = $cmd .  ",  2.346-2.2 lt -1 t '' ";
  #$cmd = $cmd .  ",  2.225-2.15 lt -1 t '' ";
  #$cmd = $cmd .  ",  0.000 lt -1 t '' ";
  # $cmd = $cmd . ", 'starC.$suffix' using (\$5-$JDBASE):(\$7+0.0-$mag_offset):8 with errorbars lt 4 pt 4 ps 0.8 lw 4";
  # $cmd = $cmd . ", 'starD.$suffix' using (\$5-$JDBASE):(\$7-0.0-$mag_offset):8 with errorbars lt 5 pt 5 ps 0.8 lw 4";
  # $cmd = $cmd . ", 'starE.$suffix' using (\$5-$JDBASE):(\$7+0.0-$mag_offset):8 with errorbars lt -1 pt 7 ps 0.8 lw 4";
  # $cmd = $cmd . ", 'starF.$suffix' using (\$5-$JDBASE):(\$7-$mag_offset):8 with errorbars lt 8 pt 8 ps 0.8 lw 4";
  # $cmd = $cmd . ", 'starG.$suffix' using (\$5-2454000):(\$7-$mag_offset):8 with errorbars lt 9 pt 9 ps 0.8 lw 4";
#  $cmd = $cmd . ", 'starX.$suffix' using (\$5-2454000):(\$7-$mag_offset):8 with errorbars lt 10 pt 10 ps 0.8 lw 4";
#  $cmd = $cmd . ", 'starY.$suffix' using (\$5-2454000):(\$7-$mag_offset):8 with errorbars lt 11 pt 11 ps 0.8 lw 4";
  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`;
  }


  
}
  
  
