#!/usr/bin/perl
#
#
# Make a series of graphs comparing the spectral energy distribution
#   of a hot star and a cool star, assuming both are perfect blackbodies.
#
# MWR 2/1/2023

  
  my($output_file, $term_options);


# This is a semi-log plot, which shows only the hot star, basically 
  $output_file = "bolo_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 2  \n"; 
  printf CMDFILE "set style line 2 lt rgb 'sea-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 logscale x \n";
  printf CMDFILE "set ylabel 'Joules per sq.m. per sec per meter' \n";
  printf CMDFILE "set xlabel 'Wavelength (nm) ' \n";
  printf CMDFILE "set title 'Planck function for two temperatures '\n";
  
  $cmd = "plot [1:1e7][0:3e16] 'bblam_23000.dat' using (\$2*1e9):3 ";
  $cmd = $cmd . "   with lines ls 1   t 'T=23000 K' ";
  $cmd = $cmd . " , ";
  $cmd = $cmd . "             'bblam_5800.dat' using (\$2*1e9):3 ";
  $cmd = $cmd . "   with lines  ls 3  t 'T=5800 K' ";


  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;
    $giffile = $psfile;
    $giffile =~ s/.ps/.png/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }


# This is a log-log plot, which shows both stars clearly
  $output_file = "bolo_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 2  \n"; 
  printf CMDFILE "set style line 2 lt rgb 'sea-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 logscale x \n";
  printf CMDFILE "set ylabel 'Log(Joules per sq.m. per sec per meter)' \n";
  printf CMDFILE "set xlabel 'Wavelength (nm) ' \n";
  printf CMDFILE "set title 'Planck function for two temperatures '\n";
  
  $cmd = "plot [1:1e7][0:18] 'bblam_23000.dat' using (\$2*1e9):4 ";
  $cmd = $cmd . "   with lines ls 1   t 'T=23000 K' ";
  $cmd = $cmd . " , ";
  $cmd = $cmd . "             'bblam_5800.dat' using (\$2*1e9):4 ";
  $cmd = $cmd . "   with lines  ls 3  t 'T=5800 K' ";


  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;
    $giffile = $psfile;
    $giffile =~ s/.ps/.png/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }




# This is a log-log plot, with addition of V passband
  $output_file = "bolo_c.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 2  \n"; 
  printf CMDFILE "set style line 2 lt rgb 'sea-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 logscale x \n";
  printf CMDFILE "set ylabel 'Log(Joules per sq.m. per sec per meter)' \n";
  printf CMDFILE "set xlabel 'Wavelength (nm) ' \n";
  printf CMDFILE "set title 'Planck function for two temperatures '\n";
  
  $cmd = "plot [1:1e7][0:18] 'bblam_23000.dat' using (\$2*1e9):4 ";
  $cmd = $cmd . "   with lines ls 1   t 'T=23000 K' ";
  $cmd = $cmd . " , ";
  $cmd = $cmd . "             'bblam_5800.dat' using (\$2*1e9):4 ";
  $cmd = $cmd . "   with lines  ls 3  t 'T=5800 K' ";
  $cmd = $cmd . " , ";
  $cmd = $cmd . "             'bess_v.pass' using (\$1*0.1):(\$2*16) ";
  $cmd = $cmd . "   with lines  ls -1 lw 3 t '' ";


  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;
    $giffile = $psfile;
    $giffile =~ s/.ps/.png/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }

# This is a log-log plot, with addition of rectangular region to indicate V-band
  $output_file = "bolo_c2.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 2  \n"; 
  printf CMDFILE "set style line 2 lt rgb 'sea-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' dt 2 lw 2  \n"; 

  printf CMDFILE "set grid \n";
  printf CMDFILE "set key top right \n";
  printf CMDFILE "set logscale x \n";
  printf CMDFILE "set ylabel 'Log(Joules per sq.m. per sec per meter)' \n";
  printf CMDFILE "set xlabel 'Wavelength (nm) ' \n";
  printf CMDFILE "set title 'Planck function for two temperatures '\n";

  printf CMDFILE "set title 'Planck function for two temperatures '\n";
  
  $cmd = "plot [1:1e7][0:18] 'bblam_23000.dat' using (\$2*1e9):4 ";
  $cmd = $cmd . "   with lines ls 1   t 'T=23000 K' ";
  $cmd = $cmd . " , ";
  $cmd = $cmd . "             'bblam_5800.dat' using (\$2*1e9):4 ";
  $cmd = $cmd . "   with lines  ls 3  t 'T=5800 K' ";
  $cmd = $cmd . " , ";
  $cmd = $cmd . "             'square_bess.pass' using (\$1):(\$2*19) ";
  $cmd = $cmd . "   with lines  ls 6  t '' ";


  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;
    $giffile = $psfile;
    $giffile =~ s/.ps/.png/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }




# This is a log-log plot, with addition of V passband
#    zoomed in to show only wavelengths around V
  $output_file = "bolo_d.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 2  \n"; 
  printf CMDFILE "set style line 2 lt rgb 'sea-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' dt 2 lw 2  \n"; 

  printf CMDFILE "set grid \n";
  printf CMDFILE "set key top right \n";
  printf CMDFILE "set ylabel 'Log(Joules per sq.m. per sec per meter)' \n";
  printf CMDFILE "set xlabel 'Wavelength (nm) ' \n";
  printf CMDFILE "set title 'Planck function for two temperatures '\n";
  
  $cmd = "plot [400:800][0:18] 'bblam_23000.dat' using (\$2*1e9):4 ";
  $cmd = $cmd . "   with lines ls 1   t 'T=23000 K' ";
  $cmd = $cmd . " , ";
  $cmd = $cmd . "             'bblam_5800.dat' using (\$2*1e9):4 ";
  $cmd = $cmd . "   with lines  ls 3  t 'T=5800 K' ";
  $cmd = $cmd . " , ";
  $cmd = $cmd . "             'square_bess.pass' using (\$1):(\$2*19) ";
  $cmd = $cmd . "   with lines  ls 6 t '' ";


  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;
    $giffile = $psfile;
    $giffile =~ s/.ps/.png/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }






exit 0;
