#! /usr/bin/perl
#
# MWR 3/8/2011

use POSIX;

$debug = 1;


  $gnuplot_file = "./make_plot.gnu";
  $plot_file = sprintf "./flam_a.ps";

  open(CMDFILE, ">$gnuplot_file") || die("can't open plot_file $gnuplot_file");
  printf CMDFILE "set output '$plot_file' \n";
  #printf CMDFILE "set term postscript color size 15,10.5 font 'Helvetica,18' \n";
  printf CMDFILE "set term postscript color font 'Helvetica,14' \n";
  printf CMDFILE "set nogrid \n";
  printf CMDFILE "set nokey \n";
  printf CMDFILE "set logscale x \n";
  printf CMDFILE "set multiplot layout 2,1 \n";
  #printf CMDFILE "set label 1 'red' at -0.08,-0.25 font 'Helvetica,14' \n";
  #printf CMDFILE "set label 2 'blue+$blue_offset' at -0.08, 0.25+$blue_offset font 'Helvetica,14'\n";

  # first, we plot the spectrum in flam at top
  printf CMDFILE "set xlabel 'Wavelength (Angstroms)' \n";
  printf CMDFILE "set ylabel 'Flam (arb units) ' \n";
  printf CMDFILE "set xtics (2000,3000,6000,10000) \n";
  printf CMDFILE "set title 'Spectrum of an A0V star in Flam units' \n";
    $cmd = "plot [1200:30000][] 'uka0v_dat' using 1:2 with lines \n";
    printf CMDFILE "$cmd \n";

  # first, we plot the spectrum in fnu at bottom
  printf CMDFILE "set xlabel 'Frequency (Hz)' \n";
  printf CMDFILE "set ylabel 'Fnu (arb units) ' \n";
  printf CMDFILE "set xtics (2.0e15,1.2e15,6e14,1.5e14) \n";
  printf CMDFILE "set title 'Spectrum of an A0V star in Fnu units' \n";
    $cmd = "plot [2.5e15:1.0e14][] 'uka0v_fnu.dat' ";
    $cmd .= "  using 1:(\$2*1e22) with lines lc 3  \n";
    printf CMDFILE "$cmd \n";


  #printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE);


  # run gnuplot to create the postscript output file
  $retval = `gnuplot < $gnuplot_file`;
  if ($debug > 0) {
    printf "retval is ..%s..\n", $retval;
  }


  # make a copy of the Postscript in PNG format, too
  $pngfile = $plot_file;
  $pngfile =~ s/.ps/.png/;
  $cmd = "convert -rotate 90 $plot_file $pngfile";
  if ($debug > 0) {
    printf "cmd is ..$cmd.. \n";
  }
  $ret = `$cmd`;


#######################################################################33
# Now graph for blackbody comparison, T = 10,000 

  $gnuplot_file = "./make_plot.gnu";
  $plot_file = sprintf "./flam_b.ps";

  open(CMDFILE, ">$gnuplot_file") || die("can't open plot_file $gnuplot_file");
  printf CMDFILE "set output '$plot_file' \n";
  #printf CMDFILE "set term postscript color size 15,10.5 font 'Helvetica,18' \n";
  printf CMDFILE "set term postscript color font 'Helvetica,14' \n";
  printf CMDFILE "set nogrid \n";
  printf CMDFILE "set nokey \n";
  printf CMDFILE "set logscale x \n";
  printf CMDFILE "set multiplot layout 2,1 \n";
  #printf CMDFILE "set label 1 'red' at -0.08,-0.25 font 'Helvetica,14' \n";
  #printf CMDFILE "set label 2 'blue+$blue_offset' at -0.08, 0.25+$blue_offset font 'Helvetica,14'\n";

  # first, we plot the spectrum in flam at top
  printf CMDFILE "set xlabel 'Wavelength (Angstroms)' \n";
  printf CMDFILE "set ylabel 'Flam (arb units) ' \n";
  printf CMDFILE "set xtics (2000,3000,6000,10000) \n";
  printf CMDFILE "set title 'Spectrum of a blackbody of T = 10,000 K in Flam units' \n";
    $cmd = "plot [1200:30000][] './bb_lam.10000.out' using (\$2*1e10):(\$3*1E-14) with lines \n";
    printf CMDFILE "$cmd \n";

  # first, we plot the spectrum in fnu at bottom
  printf CMDFILE "set xlabel 'Frequency (Hz)' \n";
  printf CMDFILE "set ylabel 'Fnu (arb units) ' \n";
  printf CMDFILE "set xtics (2.0e15,1.2e15,6e14,1.5e14) \n";
  printf CMDFILE "set title 'Spectrum of an A0V star in Fnu units' \n";
    $cmd = "plot [2.5e15:1.0e14][] 'bb_lam.10000.out' ";
    $cmd .= "  using (\$1):(\$4*2e7) with lines lc 3  \n";
    printf CMDFILE "$cmd \n";


  #printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE);


  # run gnuplot to create the postscript output file
  $retval = `gnuplot < $gnuplot_file`;
  if ($debug > 0) {
    printf "retval is ..%s..\n", $retval;
  }


  # make a copy of the Postscript in PNG format, too
  $pngfile = $plot_file;
  $pngfile =~ s/.ps/.png/;
  $cmd = "convert -rotate 90 $plot_file $pngfile";
  if ($debug > 0) {
    printf "cmd is ..$cmd.. \n";
  }
  $ret = `$cmd`;


exit 0;
