#!/usr/bin/perl
#

  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "wiggins_lc_a.ps";
  $term_options = "postscript color enhanced font 'Helvetica,18' ";

  $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 'time since burst detected (hours) ' \n";
  printf CMDFILE "set ylabel 'Unfiltered magnitude (r-band reference) ' \n";
  printf CMDFILE "set title 'Wiggins measurements of GRB 130427A '\n", $aper;
  
  $cmd = "plot [][16:13] './wiggins.dat' using 2:4:5 with yerrorbars pt 3 ps 1.5 lw 2 ";

  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`;
  }


  

  # Now a plot with logarithm of time on x-axis
  $output_file = "wiggins_lc_b.ps";
  $term_options = "postscript color enhanced font 'Helvetica,18' ";

  $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 logscale x \n";
  printf CMDFILE "set xlabel 'time since burst detected (hours) ' \n";
  printf CMDFILE "set ylabel 'Unfiltered magnitude (r-band reference) ' \n";
  printf CMDFILE "set title 'Wiggins measurements of GRB 130427A '\n", $aper;
  
  $cmd = "plot [0.2:5][16:13] './wiggins.dat' using 2:4:5 with yerrorbars pt 3 ps 1.5 lw 2 ";

  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`;
  }


  

  # Now a histogram of GRB redshits
  $output_file = "grb_redshift_hist.ps";
  $term_options = "postscript color enhanced font 'Helvetica,18' ";

  $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 arrow 1 from 0.34,0.0 to 0.34,1.0 \n";
  printf CMDFILE "set label 1 at 0.40,0.5 \"130427A\" \n";
  printf CMDFILE "set nokey \n";
  printf CMDFILE "set xlabel 'redshift z ' \n";
  printf CMDFILE "set ylabel 'Number of GRBs ' \n";
  printf CMDFILE "set title 'GRBs with redshifts from Nysewander et al. (2009) '\n", $aper;
  
  $cmd = "plot [-0.2:7][] './grb_cat_hist.dat' using 1:2 with steps lw 3 ";

  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;
