#!/usr/bin/perl
# Make a nice graph showing supernova redshifts over the past 20 years
#
# MWR 10/27/2007

  
  $output_file = "./sn_mag_by_year.ps";
  $term_options = "postscript color";

  $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 bot left \n";
  printf CMDFILE "set xlabel 'Year' \n";
  printf CMDFILE "set ylabel 'Redshift' \n";
  printf CMDFILE "set title 'Recent supernovae with measured redshifts '\n", $aper;
  printf CMDFILE "set label 1 '14.5' at 2008,0.01 font 'Helvetica,18' \n";
  printf CMDFILE "set label 2 '18.0' at 2009.5,0.05 font 'Helvetica,18' \n";
  printf CMDFILE "set label 3 '19.5' at 2008.0,0.10 font 'Helvetica,18' \n";
  printf CMDFILE "set label 4 '20.9' at 2009.5,0.20 font 'Helvetica,18' \n";
  printf CMDFILE "set label 5 '21.7' at 2008.0,0.30 font 'Helvetica,18' \n";
  printf CMDFILE "set label 6 '22.3' at 2009.5,0.40 font 'Helvetica,18' \n";
  printf CMDFILE "set label 7 '23.1' at 2008.0,0.60 font 'Helvetica,18' \n";
  printf CMDFILE "set label 8 '23.6' at 2008.0,0.80 font 'Helvetica,18' \n";
  
  $cmd =   "plot [1993:2011][-0.1:1.5] 'make_sn_hist_table.out' ";
  $cmd .=  "      using (\$1-0.3+0.6*rand(0)):(\$2-0.005+0.01*rand(0)) ";
  $cmd .=  "    pt 6 ps 0.3 lw 2 t '' ";

  printf CMDFILE "$cmd \n";
  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;
  
    $psfile = $output_file;
    $giffile = $psfile;
    $giffile =~ s/.ps/.gif/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;



  
#  Now make a version with logscaling on the y-axis
  $output_file = "./sn_mag_by_year_log.ps";
  $term_options = "postscript color";

  $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 bot left \n";
  printf CMDFILE "set xlabel 'Year' \n";
  printf CMDFILE "set ylabel 'Redshift' \n";
  printf CMDFILE "set logscale y \n";
  printf CMDFILE "set title 'Recent supernovae with measured redshifts '\n", $aper;
  printf CMDFILE "set label 1 '14.5' at 2008,0.01 font 'Helvetica,18' \n";
  printf CMDFILE "set label 2 '18.0' at 2008.0,0.05 font 'Helvetica,18' \n";
  printf CMDFILE "set label 3 '19.5' at 2008.0,0.10 font 'Helvetica,18' \n";
  printf CMDFILE "set label 4 '20.9' at 2008.0,0.20 font 'Helvetica,18' \n";
  printf CMDFILE "set label 5 '21.7' at 2008.0,0.30 font 'Helvetica,18' \n";
  printf CMDFILE "set label 6 '22.3' at 2008.0,0.40 font 'Helvetica,18' \n";
  printf CMDFILE "set label 7 '23.1' at 2008.0,0.60 font 'Helvetica,18' \n";
  printf CMDFILE "set label 8 '23.6' at 2008.0,0.80 font 'Helvetica,18' \n";
  
  $cmd =   "plot [1993:2011][0.005:1.5] 'make_sn_hist_table.out' ";
  $cmd .=  "      using (\$1-0.3+0.6*rand(0)):(\$2-0.005+0.01*rand(0)) ";
  $cmd .=  "    pt 6 ps 0.3 lw 2 t '' ";

  printf CMDFILE "$cmd \n";
  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;
  
    $psfile = $output_file;
    $giffile = $psfile;
    $giffile =~ s/.ps/.gif/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;


