#!/usr/bin/perl
#
# Make plot(s) involving an isolated gaussian
#
# MWR 3/31/2019

$mypi = 3.14159;
$datadir = "./datafiles";

$num_snapshots = 123;

$A = 1.0;
$sigma = 1.0;
$xmin = -10.0;
$xmax = 10.0;
$dx = 0.1;


for ($x0 = 0.0; $x0 <= 12.0; $x0 += 1.0) {

  # step one: create a datafile
  $datafile = sprintf "%s/exp_%05.2f.dat", $datadir, $x0;
  open(DATAFILE, ">$datafile") || die("can't open file $datafile");

  for ($x = $xmin; $x <= $xmax; $x += $dx) {
    $arg = ($x - $x0)*($x - $x0)/(2.0*$sigma*$sigma);
    $y = $A*exp(0.0 - $arg);
    printf DATAFILE "%lf %lf \n", $x, $y;
  }
  close(DATAFILE);



  # step two: plot the datafile

  my($output_file, $term_options);

  $output_file = sprintf "exp_%05.2f.ps", $x0;
  $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 [-0.1:0+($A*1.1)]  \n";
  printf CMDFILE "set xlabel 'X position (m)  '  \n";
  printf CMDFILE "set ylabel 'Y position (m) ' \n";
  #  printf CMDFILE "set title '' \n"; 

  printf CMDFILE "plot '$datafile' using 1:2 with lines ls 1  t '' \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;

