#!/usr/bin/perl
#
# Make plot(s) involving the blackbody emission function
#
# MWR 8/13/2020



##################################################################
# plot intensity as a function of wavelength for 1000 K

  my($output_file, $term_options);

  $datafile = "./bb_lam_1000.dat";
  $output_file = "./bb_lam_a.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 [-7:-3]  \n";
  printf CMDFILE "set yrange [-2:10]  \n";
  printf CMDFILE "set xlabel 'Log_{10} (Wavelength [m])  '  \n";
  printf CMDFILE "set ylabel 'Log_{10} (Intensity per unit wavelength)' \n";
  #  printf CMDFILE "set title '' \n"; 

  printf CMDFILE "plot '$datafile' using 1:4 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`;
  }



##################################################################
# plot intensity as a function of wavelength for 1000 K
#    and add a line showing slope of -4

  my($output_file, $term_options);

  $datafile = "./bb_lam_1000.dat";
  $output_file = "./bb_lam_b.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 style line 6 lt rgb 'blue' lw 3 dt 3  \n";

  printf CMDFILE "set grid \n";
  printf CMDFILE "set key top right \n";
  printf CMDFILE "set xrange [-7:-3]  \n";
  printf CMDFILE "set yrange [-2:10]  \n";
  printf CMDFILE "set xlabel 'Log_{10} (Wavelength [m])  '  \n";
  printf CMDFILE "set ylabel 'Log_{10} (Intensity per unit wavelength)' \n";
  #  printf CMDFILE "set title '' \n"; 

  printf CMDFILE "plot '$datafile' using 1:4 with lines ls 1  t '' ";
  printf CMDFILE " , ";
  printf CMDFILE "  -10-4*x with lines ls 6  t 'slope -4' ";
  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`;
  }


##################################################################
# plot intensity as a function of frequency for 1000 K

  my($output_file, $term_options);

  $datafile = "./bb_fnu_1000.dat";
  $output_file = "./bb_nu_a.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 [11.47:15.47]  \n";
  printf CMDFILE "set yrange [-21:-9]  \n";
  printf CMDFILE "set xlabel 'Log_{10} (Frequency [Hz])  '  \n";
  printf CMDFILE "set ylabel 'Log_{10} (Intensity per unit frequency)' \n";
  #  printf CMDFILE "set title '' \n"; 

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



##################################################################
# plot intensity as a function of frequency for 1000 K
#    add a dotted line showing slope -2

  my($output_file, $term_options);

  $datafile = "./bb_fnu_1000.dat";
  $output_file = "./bb_nu_b.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 style line 6 lt rgb 'black' lw 3 dt 3  \n";

  printf CMDFILE "set grid \n";
  printf CMDFILE "set key top left \n";
  printf CMDFILE "set xrange [11.47:15.47]  \n";
  printf CMDFILE "set yrange [-21:-9]  \n";
  printf CMDFILE "set xlabel 'Log_{10} (Frequency [Hz])  '  \n";
  printf CMDFILE "set ylabel 'Log_{10} (Intensity per unit frequency)' \n";
  #  printf CMDFILE "set title '' \n"; 

  printf CMDFILE "plot '$datafile' using 1:4 with lines ls 2  t '' ";
  printf CMDFILE " , ";
  printf CMDFILE "  -38+2*x with lines ls 6  t 'slope +2' ";
  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`;
  }



##################################################################
# plot intensity as a function of wavelength for 
#    several different temperatures

  my($output_file, $term_options);

  $output_file = "./bb_temp_a.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 [-9:-3]  \n";
  printf CMDFILE "set yrange [-2:20]  \n";
  printf CMDFILE "set xlabel 'Log_{10} (Wavelength [m])  '  \n";
  printf CMDFILE "set ylabel 'Log_{10} (Intensity per unit wavelength)' \n";
  #  printf CMDFILE "set title '' \n"; 

  printf CMDFILE "plot './bb_lam_100000.dat' using 1:4 with lines ls 1  t 'T = 100000 K' ";
  printf CMDFILE " , ";
  printf CMDFILE "     './bb_lam_10000.dat' using 1:4 with lines ls 2  t 'T = 10000 K' ";
  printf CMDFILE " , ";
  printf CMDFILE "     './bb_lam_1000.dat' using 1:4 with lines ls 3  t 'T = 1000 K' ";
  printf CMDFILE " , ";
  printf CMDFILE "     './bb_lam_100.dat' using 1:4 with lines ls 4  t 'T = 100 K' ";
  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;

