#!/usr/bin/perl
#

$filter = "C";
if ($filter eq "C") {
  $filter_name = "clear";
}
else {
  $filter_name = $filter;
}
$filter_lc = lc($filter);
$mag_offset =  0.000;
$aper = 6;
$suffix = sprintf "dat%d", $aper;

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

# make a PNG graph
#make_graph("hs2331_" . $aper . "_field_jul23.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 nokey \n";
  printf CMDFILE "set xlabel 'Julian Date - 2,454,000' \n";
  printf CMDFILE "set title 'HS2331+3905 field, $filter_name %d-pix diff mag, Sep 25 2007 UT'\n", $aper;
  
  $cmd = "plot [368.50:368.60][3.5:-0.4] 'starA.$suffix' using (\$5-2454000):(\$7-$mag_offset)   lt 1 pt 1 lw 4";
  $cmd = $cmd . ", 'starHS.$suffix' using (\$5-2454000):(\$7-$mag_offset) lt 2 pt 2 lw 4";
  $cmd = $cmd . ", 'starB.$suffix' using (\$5-2454000):(\$7-$mag_offset) lt 3 pt 3  lw 4";
  $cmd = $cmd . ", 'starC.$suffix' using (\$5-2454000):(\$7-$mag_offset) lt 4 pt 4  lw 4";
  $cmd = $cmd . ", 'starD.$suffix' using (\$5-2454000):(\$7-$mag_offset) lt 5 pt 5  lw 4";
  # $cmd = $cmd . ", 'starE.$suffix' using (\$5-2454000):(\$7-$mag_offset) lt 10 pt 7  lw 4";
  $cmd = $cmd . ", 'starF.$suffix' using (\$5-2454000):(\$7-$mag_offset) lt 8 pt 8  lw 4";
  $cmd = $cmd . ", 'starG.$suffix' using (\$5-2454000):(\$7-$mag_offset) lt -1 pt 9  lw 4";
  $cmd = $cmd . ", 'starJ.$suffix' using (\$5-2454000):(\$7-$mag_offset) lt 3 pt 10  lw 4";
  #$cmd = $cmd . ", 'starX.$suffix' using (\$5-2454000):(\$7-$mag_offset) lt 5 pt 6 lw 2 ";
  #$cmd = $cmd . ", 'starY.$suffix' using (\$5-2454000):(\$7-$mag_offset) lt 10 pt 10 ps  0.5 lw 4 ";
#  $cmd = $cmd . ", 'starZ.$suffix' using (\$5-2454000):(\$7-$mag_offset) lt 11 pt 11 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/.gif/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }

}
