#!/usr/bin/perl
#
# Make plots showing relationship between body length and mass
#    for a set of mammals
#
# Data taken from http://esapubs.org/archive/ecol/E090/184/#data
#    and slightly edited.
#
# MWR 1/24/2021

$mypi = 3.14159;



  my($output_file, $term_options);


##########################################################################
# Linear plot of mass vs. length
#
  $datafile = "./mammal_length_mass.dat";
  $output_file = "./mammal_linear.ps";
  $term_options = "postscript color enhanced '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";

  # revert to some oldish default colors
  printf CMDFILE "set style line 1 lt rgb 'red' lw 3  \n";
  printf CMDFILE "set style line 2 lt rgb 'green' lw 3  \n";
  printf CMDFILE "set style line 3 lt rgb 'blue' lw 3  \n";
  printf CMDFILE "set style line 4 lt rgb 'cyan' lw 3  \n";
  printf CMDFILE "set style line 5 lt rgb 'violet' lw 3  \n";


  printf CMDFILE "set grid \n";
  printf CMDFILE "set key top right \n";
  printf CMDFILE "set xrange [$xmin:$xmax]  \n";
  printf CMDFILE "set yrange [$ymin:$ymax]  \n";
  printf CMDFILE "set xlabel 'Body length (mm)  '  \n";
  printf CMDFILE "set ylabel 'Body mass (gram) ' \n";
  printf CMDFILE "set title 'Properties of Mammals ' \n"; 

  printf CMDFILE "plot [0:1e3][0:1e4] '$datafile' ";
  printf CMDFILE "    using 2:1 with points ls 1 ps 0.8 pt 3  t ''  ";
  printf CMDFILE "\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`;
  }


##########################################################################
# Linear plot of mass vs. (length)^3
#
  $datafile = "./mammal_length_mass.dat";
  $output_file = "./mammal_length3.ps";
  $term_options = "postscript color enhanced '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";

  # revert to some oldish default colors
  printf CMDFILE "set style line 1 lt rgb 'red' lw 3  \n";
  printf CMDFILE "set style line 2 lt rgb 'green' lw 3  \n";
  printf CMDFILE "set style line 3 lt rgb 'blue' lw 3  \n";
  printf CMDFILE "set style line 4 lt rgb 'cyan' lw 3  \n";
  printf CMDFILE "set style line 5 lt rgb 'violet' lw 3  \n";


  printf CMDFILE "set grid \n";
  printf CMDFILE "set key top right \n";
  printf CMDFILE "set xrange [$xmin:$xmax]  \n";
  printf CMDFILE "set yrange [$ymin:$ymax]  \n";
  printf CMDFILE "set xlabel 'Cube of [body length (mm)]  '  \n";
  printf CMDFILE "set ylabel 'Body mass (gram) ' \n";
  printf CMDFILE "set title 'Properties of Mammals ' \n"; 

  printf CMDFILE "plot [0:5e7][0:2e3] '$datafile' ";
  printf CMDFILE "    using (\$2*\$2*\$2):1 with points ls 1 ps 0.8 pt 3  t ''  ";
  printf CMDFILE "\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`;
  }


##########################################################################
# Logarithmic plot of mass vs. length
#
  $datafile = "./mammal_length_mass.dat";
  $output_file = "./mammal_log.ps";
  $term_options = "postscript color enhanced '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";

  # revert to some oldish default colors
  printf CMDFILE "set style line 1 lt rgb 'red' lw 3  \n";
  printf CMDFILE "set style line 2 lt rgb 'green' lw 3  \n";
  printf CMDFILE "set style line 3 lt rgb 'blue' lw 3  \n";
  printf CMDFILE "set style line 4 lt rgb 'cyan' lw 3  \n";
  printf CMDFILE "set style line 5 lt rgb 'violet' lw 3  \n";


  printf CMDFILE "set grid \n";
  printf CMDFILE "set key top right \n";
  printf CMDFILE "set xrange [$xmin:$xmax]  \n";
  printf CMDFILE "set yrange [$ymin:$ymax]  \n";
  printf CMDFILE "set xlabel 'Body length (mm)  '  \n";
  printf CMDFILE "set ylabel 'Body mass (gram) ' \n";
  printf CMDFILE "set title 'Properties of Mammals ' \n"; 
  printf CMDFILE "set logscale x \n";
  printf CMDFILE "set logscale y \n";

  printf CMDFILE "plot [10:1e4][1:1e6] '$datafile' ";
  printf CMDFILE "    using 2:1 with points ls 1 ps 0.8 pt 3  t ''  ";
  printf CMDFILE "\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`;
  }


##########################################################################
# Logarithmic plot of mass vs. length
#    with a line added for mass goes like length^3
#
  $datafile = "./mammal_length_mass.dat";
  $output_file = "./mammal_log_line.ps";
  $term_options = "postscript color enhanced '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";

  # revert to some oldish default colors
  printf CMDFILE "set style line 1 lt rgb 'red' lw 3  \n";
  printf CMDFILE "set style line 2 lt rgb 'green' lw 3  \n";
  printf CMDFILE "set style line 3 lt rgb 'blue' lw 3  \n";
  printf CMDFILE "set style line 4 lt rgb 'cyan' lw 3  \n";
  printf CMDFILE "set style line 5 lt rgb 'violet' lw 3  \n";


  printf CMDFILE "set grid \n";
  printf CMDFILE "set key top left reverse \n";
  printf CMDFILE "set xrange [$xmin:$xmax]  \n";
  printf CMDFILE "set yrange [$ymin:$ymax]  \n";
  printf CMDFILE "set xlabel 'Body length (mm)  '  \n";
  printf CMDFILE "set ylabel 'Body mass (gram) ' \n";
  printf CMDFILE "set title 'Properties of Mammals ' \n"; 
  printf CMDFILE "set logscale x \n";
  printf CMDFILE "set logscale y \n";

  printf CMDFILE "plot [10:1e4][1:1e6] '$datafile' ";
  printf CMDFILE "    using 2:1 with points ls 1 ps 0.8 pt 3  t ''  ";
  printf CMDFILE " , ";
  printf CMDFILE " (0.3e-4)*(x**3.0) ls 3 t 'slope = 3'  ";
  printf CMDFILE "\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;

