#!/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("sdss_all_" . $aper . "_jul15ut.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 (plus arb shift)' \n";
  printf CMDFILE "set ylabel 'unfiltered magnitude (set to match V)' \n";
  printf CMDFILE "set title 'SDSS150241 measurements at RIT Obs '\n", $aper;
  
  $cmd = "plot [22.55:22.78][16.7:13.5] '/z2/ritobs/jul09_2009/sdss_pff/sdss1502_jul10ut.dat' using (\$2-$JDBASE):(\$4-($mag_offset+0.00)) lc 1 pt 4 ps 0.5 lw 3 t 'UT Jul 10 ' ";
  $cmd = $cmd . ", ";
  $cmd = $cmd . " '/z2/ritobs/jul12_2009/work/pff/sdss1520_jul13ut.dat' ";
  $cmd = $cmd . "     using ((\$2-3.004)-$JDBASE):(\$4-($mag_offset-0.00)) ";
  $cmd = $cmd . "     with points  lc 2 pt 4 ps 0.5 lw 3 t 'UT Jul 13' ";
  $cmd = $cmd . ", ";
  $cmd = $cmd . " '/z2/ritobs/jul13_2009/work/pff/sdss1502_jul14ut.dat' ";
  $cmd = $cmd . "     using ((\$2-4.004)-$JDBASE):(\$4-($mag_offset-0.00)) ";
  $cmd = $cmd . "     with points  lc 3 pt 4 ps 0.5 lw 3 t 'UT Jul 14' ";
  $cmd = $cmd . ", ";
  $cmd = $cmd . " '/z2/ritobs/jul14_2009/work/pff/sdss1502_jul15ut.dat' ";
  $cmd = $cmd . "     using ((\$2-5.006)-$JDBASE):(\$4-($mag_offset-0.00)) ";
  $cmd = $cmd . "     with lines  lc -1  lw 2 t 'UT Jul 15' ";
  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`;
  }


  
}
  
  
