#!/usr/bin/perl
#
# Make radial profile plots for several real stars
#

  $cmdfile = "./makeplot.gnu";


  ##########
  #  This is the SDSS star only
  
  $outfile = "./compare_prof_a.ps";

  open (CMDFILE, ">$cmdfile") || die("can't open $cmdfile for writing");
  printf CMDFILE "set output '$outfile' \n";
  printf CMDFILE "set term postscript enhanced color 'Helvetica,18' \n";
  printf CMDFILE "set key top right \n";
  printf CMDFILE "set logscale y \n";
  printf CMDFILE "set xlabel 'Distance from star (arcsec) ' \n";
  printf CMDFILE "set ylabel 'Intensity (arbitrary scale)  ' \n";
  printf CMDFILE "set title 'SDSS r-band stellar profile  ' \n";
  
  $cmd = "plot [1:500][1:3e3] ";
  $cmd .="     'sdss_r.dat'  every 100 using (\$1*0.4):(\$2*1.0)   ";
  $cmd .= "        with points pt 3 ps 0.2 lc 1 lw 3 t '' ";
  $cmd .= "  , ";
  $cmd .= "    0.8e5*(x**(-1.3)) lw 3 lt 1 lc -1 t 'r^{-1.3}' ";
#  $cmd .= "    './ic3_prof.dat' every 100 using (\$1*0.04):(\$2*1.0) ";
#  $cmd .= "        with points pt 6 ps 0.1 lc 3 lw 3 t 'HST' ";

  printf CMDFILE "$cmd \n";
  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;

  # create a PNG copy of any Postscript file
    $png_file = $outfile;
    $png_file =~ s/.ps/.png/;
    $cmd = "convert -flatten -background white -rotate 90 $outfile $png_file ";
    printf "cmd is ..$cmd.. \n";
    $retval = `$cmd`;





  ##########
  #  This is the HST star only
  
  $outfile = "./compare_prof_b.ps";

  open (CMDFILE, ">$cmdfile") || die("can't open $cmdfile for writing");
  printf CMDFILE "set output '$outfile' \n";
  printf CMDFILE "set term postscript enhanced color 'Helvetica,18' \n";
  printf CMDFILE "set key top right \n";
  printf CMDFILE "set logscale y \n";
  printf CMDFILE "set xlabel 'Distance from star (arcsec) ' \n";
  printf CMDFILE "set ylabel 'Intensity (arbitrary scale)  ' \n";
  printf CMDFILE "set title 'HST I-band stellar profile  ' \n";
  printf CMDFILE "set samples 2000 \n";
  
  $cmd = "plot [0:15][1:1e5] ";
  $cmd .="     'ic3_prof.dat'  every 100 using (\$1*0.04):(\$2*1.0)   ";
  $cmd .= "        with points pt 3 ps 0.2 lc 3 lw 3 t '' ";
  $cmd .= "  , ";
  $cmd .= "    0.8e5*(x**(-1.3)) lw 3 lt 1 lc -1 t 'r^{-1.3}' ";
  $cmd .= "  , ";
  $cmd .= "    0.1e5*exp(-x/1.0) lw 3 lt 1 lc 4 t 'e^{-x}' ";
  $cmd .= "  , ";
  #$cmd .= "    0.7e5*exp(-(x*x)/(2*1*1)) lw 8 lt 3 lc 2 t 'gaussian' ";
  $cmd .= "    0.7e8*exp(-(x*x)/(2*0.031*0.031)) lw 8 lt 3 lc 2 t 'gaussian' ";
#  $cmd .= "    './ic3_prof.dat' every 100 using (\$1*0.04):(\$2*1.0) ";
#  $cmd .= "        with points pt 6 ps 0.1 lc 3 lw 3 t 'HST' ";

  printf CMDFILE "$cmd \n";
  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;

  # create a PNG copy of any Postscript file
    $png_file = $outfile;
    $png_file =~ s/.ps/.png/;
    $cmd = "convert -flatten -background white -rotate 90 $outfile $png_file ";
    printf "cmd is ..$cmd.. \n";
    $retval = `$cmd`;




  ##########
  #  Comparison of blackbody emission, wide-band
  
  $outfile = "./compare_bb_a.ps";

  open (CMDFILE, ">$cmdfile") || die("can't open $cmdfile for writing");
  printf CMDFILE "set output '$outfile' \n";
  printf CMDFILE "set term postscript enhanced color 'Helvetica,18' \n";
  printf CMDFILE "set key top right \n";
  printf CMDFILE "set logscale x \n";
  printf CMDFILE "set logscale y \n";
  printf CMDFILE "set xlabel 'Wavelength (microns)' \n";
  printf CMDFILE "set ylabel 'Flambda (Watts / (sq. meter)*m )  ' \n";
  # printf CMDFILE "set title '' \n";
  printf CMDFILE "set object 1 rect from 2.02,1e-18 to 2.28,1e6 behind fc lt -1 fs  solid 0.05 lw 0  \n";
  
  $cmd = "plot [0.1:1000][1e-18:1e6] ";
  $cmd .="     'bb_lam.5800.out'  every 100 using (\$2*1e6):7   ";
  $cmd .= "        with lines  lc 1 lw 3 t 'T = 5800 K' ";
  $cmd .= " , ";
  $cmd .="     'bb_lam.1500.out'  every 100 using (\$2*1e6):7   ";
  $cmd .= "        with lines  lc 2 lw 3 t 'T = 1500 K' ";
  $cmd .= " , ";
  $cmd .="     'bb_lam.150.out'  every 100 using (\$2*1e6):7   ";
  $cmd .= "        with lines  lc 3 lw 3 t 'T = 150 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;

  # create a PNG copy of any Postscript file
    $png_file = $outfile;
    $png_file =~ s/.ps/.png/;
    $cmd = "convert -flatten -background white -rotate 90 $outfile $png_file ";
    printf "cmd is ..$cmd.. \n";
    $retval = `$cmd`;



if (1 == 1) {

  ##########
  #  Comparison of blackbody emission, zoom into near-IR 
  
  $outfile = "./compare_bb_b.ps";

  open (CMDFILE, ">$cmdfile") || die("can't open $cmdfile for writing");
  printf CMDFILE "set output '$outfile' \n";
  printf CMDFILE "set term postscript enhanced color 'Helvetica,18' \n";
  printf CMDFILE "set key top right \n";
  printf CMDFILE "set logscale x \n";
  printf CMDFILE "set logscale y \n";
  printf CMDFILE "set xlabel 'Wavelength (microns)' \n";
  printf CMDFILE "set ylabel 'Flambda (Watts / (sq. meter)*m )  ' \n";
  # printf CMDFILE "set title '' \n";
  printf CMDFILE "set object 1 rect from 2.02,1e-18 to 2.28,1e6 behind fc lt -1 fs  solid 0.05 lw 0  \n";
  printf CMDFILE "set object 2 rect from 10.0,1e-18 to 11.5,4e4 behind fc lt 1 fs  solid 0.15 lw 0  \n";
  printf CMDFILE "set object 3 rect from 0.5,1e-18 to 0.6,1e6 behind fc lt 3 fs  solid 0.10 lw 0  \n";

  $cmd = "plot [0.1:50][10:3e5] ";
  $cmd .="     'bb_lam.5800.out'  every 100 using (\$2*1e6):7   ";
  $cmd .= "        with lines  lc 1 lw 3 t 'T = 5800 K' ";
  $cmd .= " , ";
  $cmd .="     'bb_lam.2000.out'  every 100 using (\$2*1e6):7   ";
  $cmd .= "        with lines  lc 3 lw 3 t 'T = 2000 K' ";
  $cmd .= " , ";
  $cmd .="     'bb_lam.1500.out'  every 100 using (\$2*1e6):7   ";
  $cmd .= "        with lines  lc 2 lw 3 t 'T = 1500 K' ";
  $cmd .= " , ";
  $cmd .="     'bb_lam.1000.out'  every 100 using (\$2*1e6):7   ";
  $cmd .= "        with lines  lt 1 lc 4 lw 3 t 'T = 1000 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;


  # create a PNG copy of any Postscript file
    $png_file = $outfile;
    $png_file =~ s/.ps/.png/;
    $cmd = "convert -flatten -background white -rotate 90 $outfile $png_file ";
    printf "cmd is ..$cmd.. \n";
    $retval = `$cmd`;


}



if (1 == 1) {

  ##########
  #  Adding a planet's signal to a star's noisy signal -- linear scale
  #      Can you find the _two_ planets?
  
  $outfile = "./add_planet_a.ps";

  open (CMDFILE, ">$cmdfile") || die("can't open $cmdfile for writing");
  printf CMDFILE "set output '$outfile' \n";
  printf CMDFILE "set term postscript enhanced color 'Helvetica,18' \n";
  printf CMDFILE "set key top right \n";
#  printf CMDFILE "set logscale x \n";
#  printf CMDFILE "set logscale y \n";
  printf CMDFILE "set xlabel 'Distance from star (pixels)' \n";
  printf CMDFILE "set ylabel 'Number of photons ' \n";
  # printf CMDFILE "set title '' \n";

  $cmd = "plot [-25:25][1:40000] ";
  $cmd .="     'star_prof_plus_planets.out'  using 1:2   ";
  $cmd .= "        with points pt 6 ps 0.2  lc 1 lw 3 t '' ";
#  $cmd .= " , ";
#  $cmd .="     'copy_prof.near.out'  using 1:2   ";
#  $cmd .= "        with points pt 6 ps 0.2  lc 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;


  # create a PNG copy of any Postscript file
    $png_file = $outfile;
    $png_file =~ s/.ps/.png/;
    $cmd = "convert -flatten -background white -rotate 90 $outfile $png_file ";
    printf "cmd is ..$cmd.. \n";
    $retval = `$cmd`;


  ##########
  #  Adding a planet's signal to a star's noisy signal -- LOG scale
  #      Can you find the _two_ planets?
  #      (One should be obvious)
  
  $outfile = "./add_planet_b.ps";

  open (CMDFILE, ">$cmdfile") || die("can't open $cmdfile for writing");
  printf CMDFILE "set output '$outfile' \n";
  printf CMDFILE "set term postscript enhanced color 'Helvetica,18' \n";
  printf CMDFILE "set key top right \n";
#  printf CMDFILE "set logscale x \n";
  printf CMDFILE "set logscale y \n";
  printf CMDFILE "set xlabel 'Distance from star (pixels)' \n";
  printf CMDFILE "set ylabel 'Number of photons ' \n";
  # printf CMDFILE "set title '' \n";

  $cmd = "plot [-25:25][1:40000] ";
  $cmd .="     'star_prof_plus_planets.out'  using 1:2   ";
  $cmd .= "        with points pt 6 ps 0.2  lc 1 lw 3 t '' ";
#  $cmd .= " , ";
#  $cmd .="     'copy_prof.near.out'  using 1:2   ";
#  $cmd .= "        with points pt 6 ps 0.2  lc 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;


  # create a PNG copy of any Postscript file
    $png_file = $outfile;
    $png_file =~ s/.ps/.png/;
    $cmd = "convert -flatten -background white -rotate 90 $outfile $png_file ";
    printf "cmd is ..$cmd.. \n";
    $retval = `$cmd`;


  ##########
  #  Adding a planet's signal to a star's noisy signal -- LOG scale
  #      Can you find the _two_ planets?
  #      This time, we try to zoom in on the close planet
  
  $outfile = "./add_planet_c.ps";

  open (CMDFILE, ">$cmdfile") || die("can't open $cmdfile for writing");
  printf CMDFILE "set output '$outfile' \n";
  printf CMDFILE "set term postscript enhanced color 'Helvetica,18' \n";
  printf CMDFILE "set key top right \n";
  printf CMDFILE "set grid \n";
  printf CMDFILE "set logscale y \n";
  printf CMDFILE "set xlabel 'Distance from star (pixels)' \n";
  printf CMDFILE "set ylabel 'Number of photons ' \n";
  # printf CMDFILE "set title '' \n";

  $cmd = "plot [1:6][1000:20000] ";
  $cmd .="     'star_prof.dat'  using 1:2   ";
  $cmd .= "        with points pt 6 ps 0.2  lc 1 lw 3 t '' ";
  $cmd .= " , ";
  $cmd .="     'copy_prof.near.out'  using 1:2   ";
  $cmd .= "        with points pt 6 ps 1.0  lc 3 lw 1 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;


  # create a PNG copy of any Postscript file
    $png_file = $outfile;
    $png_file =~ s/.ps/.png/;
    $cmd = "convert -flatten -background white -rotate 90 $outfile $png_file ";
    printf "cmd is ..$cmd.. \n";
    $retval = `$cmd`;



  ##########
  #  Adding a planet's signal to a star's noisy signal -- LOG scale
  #      Can you find the _two_ planets?
  #      This time, we try to zoom in on the close planet
  
  $outfile = "./add_planet_d.ps";

  open (CMDFILE, ">$cmdfile") || die("can't open $cmdfile for writing");
  printf CMDFILE "set output '$outfile' \n";
  printf CMDFILE "set term postscript enhanced color 'Helvetica,18' \n";
  printf CMDFILE "set key top right \n";
  printf CMDFILE "set grid \n";
  printf CMDFILE "set logscale y \n";
  printf CMDFILE "set xlabel 'Distance from star (pixels)' \n";
  printf CMDFILE "set ylabel 'Number of photons ' \n";
  # printf CMDFILE "set title '' \n";

  $cmd = "plot [-6:6][1000:20000] ";
  $cmd .="     'star_prof.dat'  using 1:2   ";
  $cmd .= "        with points pt 6 ps 0.2  lc 1 lw 3 t '' ";
  $cmd .= " , ";
  $cmd .="     'copy_prof.near.out'  using 1:2   ";
  $cmd .= "        with points pt 6 ps 1.0  lc 3 lw 1 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;


  # create a PNG copy of any Postscript file
    $png_file = $outfile;
    $png_file =~ s/.ps/.png/;
    $cmd = "convert -flatten -background white -rotate 90 $outfile $png_file ";
    printf "cmd is ..$cmd.. \n";
    $retval = `$cmd`;



}



exit 0;





##############################################################################
# PROCEDURE: exec_cmd
#
# DESCRIPTION: Execute the given shell command line.  If the $debug flag
#              is set, we print to stdout the command line, and
#              also print out the result string.
#
# RETURNS:
#              the result of the command
#
#
sub exec_cmd {

  my($cmd, $ret);

  $cmd = $_[0];

  if ($debug > 0) {
    printf "cmd is ..$cmd.. \n";
  }
  $ret = `$cmd`;
  if ($debug > 0) {
    printf "ret is ..$ret.. \n";
  }

  return($ret);
}



