#!/usr/bin/perl
# 
# Make some nice plots showing sine curves adding out of phase
#   to illustrate diffraction gratings
#
# MWR 5/14/2007


$twopi = 6.2831853;

# graph of two waves meeting in phase
$data = "cosine_off_00.dat";
$title = "Light waves striking center of a distant screen";
$cmdfile = "two_in_phase.gnu";
$psfile = "two_in_phase.ps";
open(CMDFILE, ">$cmdfile") || die("can't open $cmdfile");
printf CMDFILE "set output '$psfile' \n";
printf CMDFILE "set term postscript color \n";
printf CMDFILE "set grid \n";
printf CMDFILE "set key top left \n";
printf CMDFILE "set xlabel 'Distance wave has travelled (wavelengths)' \n";
printf CMDFILE "set ylabel 'Amplitude of wave'  \n";
printf CMDFILE "set title '$title'  \n";
printf CMDFILE "plot [-2.0:2][-1.1:1.3] ";
printf CMDFILE " '$data' using (\$1/$twopi):(\$2) ";
printf CMDFILE "     with linespoints t 'wave from slit 0' pt 1 ps 1.3 lw 2  ";
printf CMDFILE " , ";
printf CMDFILE " 'cosine_off_00.dat' using (\$1/$twopi):(\$2) ";
printf CMDFILE "     with linespoints t 'wave from slit 1' pt 2 ps 0.3 lw 2  ";
printf CMDFILE " , ";
printf CMDFILE " 'center.dat' using (\$1):(\$2) t '' with lines lt -1  ";
printf CMDFILE " \n";

printf CMDFILE "quit \n";
close(CMDFILE);

$cmd = "gnuplot $cmdfile ";
printf "cmd is ..$cmd.. \n";
$ret = `$cmd`;
$giffile = $psfile;
$giffile =~ s/.ps/.gif/;
$cmd = "convert -rotate 90 $psfile $giffile";
printf "cmd is ..$cmd.. \n";
$ret = `$cmd`;



# graph of two waves meeting slightly out of phase
$data = "cosine_off_00.dat";
$title = "Light waves striking slightly off-center of a distant screen";
$cmdfile = "two_out_of_phase.gnu";
$psfile = "two_out_of_phase.ps";
open(CMDFILE, ">$cmdfile") || die("can't open $cmdfile");
printf CMDFILE "set output '$psfile' \n";
printf CMDFILE "set term postscript color \n";
printf CMDFILE "set grid \n";
printf CMDFILE "set key top left \n";
printf CMDFILE "set xlabel 'Distance wave has travelled (wavelengths)' \n";
printf CMDFILE "set ylabel 'Amplitude of wave'  \n";
printf CMDFILE "set title '$title'  \n";
printf CMDFILE "plot [-2.0:2][-1.1:1.3] ";
printf CMDFILE " '$data' using (\$1/$twopi):(\$2) ";
printf CMDFILE "     with linespoints t 'wave from slit 0' pt 1 ps 1.3 lw 2  ";
printf CMDFILE " , ";
printf CMDFILE " 'cosine_off_01.dat' using (\$1/$twopi):(\$2) ";
printf CMDFILE "     with linespoints t 'wave from slit 1' pt 2 ps 0.3 lw 2  ";
printf CMDFILE " , ";
printf CMDFILE " 'center.dat' using (\$1):(\$2) t '' with lines lt -1  ";
printf CMDFILE " \n";

printf CMDFILE "quit \n";
close(CMDFILE);

$cmd = "gnuplot $cmdfile ";
printf "cmd is ..$cmd.. \n";
$ret = `$cmd`;
$giffile = $psfile;
$giffile =~ s/.ps/.gif/;
$cmd = "convert -rotate 90 $psfile $giffile";
printf "cmd is ..$cmd.. \n";
$ret = `$cmd`;



exit 0;
