#!/usr/bin/perl
#
# Make plots for the MACHO RR0 Lyr paper
  
  my($output_file, $term_options);


##########################################################################
# Make a histogram showing histogram of V-band magnitudes of sample
#   This is the mono EPS version
#
# MWR 12/18/2010
#

  # get the arguments

  $output_file = "insect_graph.eps";
  $term_options = "postscript eps 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 key top left \n";
  #printf CMDFILE "set arrow 1 from 3.3,22.588 to 3.6,22.588 lt 1 \n";
  #printf CMDFILE "set label 1 'upper limit due to matching' at 0.15,20 font \"Helvetica,18\" \n";
  printf CMDFILE "set xlabel 'Time (seconds)'  \n";
  printf CMDFILE "set ylabel 'Position (cm)' \n";
  printf CMDFILE "set title 'Motion of an insect crawling on the floor'\n";
  
  $cmd = "plot [-5:25][-5:15] ";
  $cmd .= " 'insect.dat' ";
  $cmd .= "    using (\$1):(\$2) with points pt 3 ps 2.5 lw 2 t ''   ";
  $cmd .= " , ";
  $cmd .= "  -0.02+0.598*x with lines lw 2 t 'linear fit' ";


  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;
    $pngfile = $psfile;
    $pngfile =~ s/.eps/.png/;
    $pngfile =~ s/.ps/.png/;
    $cmd = "convert -rotate 0 -geometry 720x504 $psfile $pngfile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }


exit 0;
