#!/usr/bin/perl
#

  
  ###############################################################
  #  Make a plot showing spectrum of a K3III star by itself
  #
  # get the arguments
  $output_file = "spec_k3_single.ps";
  $term_options = "postscript enhanced color '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";
  printf CMDFILE "set grid \n";
  printf CMDFILE "set key top right \n";
  printf CMDFILE "set xlabel 'Wavelength (Angstroms) ' \n";
  printf CMDFILE "set ylabel 'Intensity (arbitrary scale) ' \n";
  printf CMDFILE "set title 'Spectrum of a K3 III star \n";
  
  $cmd = "plot [5100:5300][] 'k3_spec2.dat' using (\$1):(\$2) ";
  $cmd .= "  with lines lc 1 lw 2 t ''  ";
#  $cmd .= " , ";
#  $cmd .= " 'm101_prof_inten.dat' using (\$1):(\$4) index 1 ";
#  $cmd .= "  with linespoints lc 3 pt 5 lw 2 t 'NGC5447'  ";

  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`;
  }


  
  ###############################################################
  #  Make a plot showing spectrum of a K3III star 
  #    plus a second K3 III star shifted by +100 km/s
  #
  # get the arguments
  $output_file = "spec_k3_a.ps";
  $term_options = "postscript enhanced color '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";
  printf CMDFILE "set grid \n";
  printf CMDFILE "set key top left \n";
  printf CMDFILE "set xlabel 'Wavelength (Angstroms) ' \n";
  printf CMDFILE "set ylabel 'Intensity (arbitrary scale) ' \n";
  printf CMDFILE "set title 'Spectrum of a K3 III star: at rest and shifted by v = +100 km/s \n";
  
  $cmd = "plot [5100:5300][] 'k3_spec2.dat' using (\$1):(\$2) ";
  $cmd .= "  with lines lc 1 lw 2 t 'at rest'  ";
  $cmd .= " , ";
  $cmd .= "    'k3_spec2_100.dat' using (\$1):(\$2*1.5) ";
  $cmd .= "  with lines lt 1 lc 3 lw 2 t 'v = +100 km/s'  ";

  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`;
  }


  
  ###############################################################
  #  Make a plot showing spectrum of a K3III star 
  #    plus a second K3 III star shifted by +100 km/s
  #    plus the sum of the two spectra
  #
  # get the arguments
  $output_file = "spec_k3_b.ps";
  $term_options = "postscript enhanced color '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";
  printf CMDFILE "set grid \n";
  printf CMDFILE "set key top left \n";
  printf CMDFILE "set xlabel 'Wavelength (Angstroms) ' \n";
  printf CMDFILE "set ylabel 'Intensity (arbitrary scale) ' \n";
  printf CMDFILE "set title 'Adding spectra of K3 III stars with different radial velocities \n";
  
  $cmd = "plot [5100:5300][] 'k3_spec2.dat' using (\$1):(\$2) ";
  $cmd .= "  with lines lc 1 lw 2 t 'at rest'  ";
  $cmd .= " , ";
  $cmd .= "    'k3_spec2_100.dat' using (\$1):(\$2*1.5) ";
  $cmd .= "  with lines lt 1 lc 3 lw 2 t 'v = +100 km/s'  ";
  $cmd .= " , ";
  $cmd .= "    'summed_2.dat' using (\$1):(\$2*2.0) ";
  $cmd .= "  with lines lt 1 lc -1 lw 2 t 'sum'  ";


  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`;
  }


  
  ###############################################################
  #  Make a plot showing spectrum of a K3III star 
  #    plus the sum of the two spectra -- right on top of original
  #
  # get the arguments
  $output_file = "spec_k3_c.ps";
  $term_options = "postscript enhanced color '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";
  printf CMDFILE "set grid \n";
  printf CMDFILE "set key top left \n";
  printf CMDFILE "set xlabel 'Wavelength (Angstroms) ' \n";
  printf CMDFILE "set ylabel 'Intensity (arbitrary scale) ' \n";
  printf CMDFILE "set title 'Adding spectra of K3 III stars with different radial velocities \n";
  
  $cmd = "plot [5100:5300][] 'k3_spec2.dat' using (\$1):(\$2) ";
  $cmd .= "  with lines lc 1 lw 2 t 'at rest'  ";
#  $cmd .= " , ";
#  $cmd .= "    'k3_spec2_100.dat' using (\$1):(\$2*1.5) ";
#  $cmd .= "  with lines lt 1 lc 3 lw 2 t 'v = +100 km/s'  ";
  $cmd .= " , ";
  $cmd .= "    'summed_2.dat' using (\$1):(\$2*1.0) ";
  $cmd .= "  with lines lt 1 lc -1 lw 2 t 'sum of v=0 and v=+100 km/s'  ";


  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`;
  }

  
  ###############################################################
  #  Make a plot showing spectrum of a K3III star 
  #    plus a second K3 III star shifted by +100 km/s
  #    plus a third K3 III star shifted by +200 km/s
  #    plus a fourth K3 III star shifted by -100 km/s
  #    plus a fifth K3 III star shifted by -200 km/s
  #
  # get the arguments
  $output_file = "spec_k3_d.ps";
  $term_options = "postscript enhanced color '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";
  printf CMDFILE "set grid \n";
  printf CMDFILE "unset key \n";
  printf CMDFILE "set xlabel 'Wavelength (Angstroms) ' \n";
  printf CMDFILE "set ylabel 'Intensity (arbitrary scale) ' \n";
  printf CMDFILE "set title 'Spectrum of a K3 III star: v = -200, -100, 0, +100, +200 km/s \n";
  
  $cmd = "plot [5100:5300][] 'k3_spec2_n200.dat' using (\$1):(\$2) ";
  $cmd .= "  with lines lt 1 lc 4 lw 2 t 'v = -200 km/s'  ";
  $cmd .= " , ";
  $cmd .= "    'k3_spec2_n100.dat' using (\$1):(\$2*1.2) ";
  $cmd .= "  with lines lt 1 lc 5 lw 2 t 'v = -100 km/s'  ";
  $cmd .= " , ";
  $cmd .= "    'k3_spec2.dat' using (\$1):(\$2*1.4) ";
  $cmd .= "  with lines lt 1 lc 1 lw 2 t 'v = 0 km/s'  ";
  $cmd .= " , ";
  $cmd .= "    'k3_spec2_100.dat' using (\$1):(\$2*1.6) ";
  $cmd .= "  with lines lt 1 lc 3 lw 2 t 'v = +100 km/s'  ";
  $cmd .= " , ";
  $cmd .= "    'k3_spec2_200.dat' using (\$1):(\$2*1.8) ";
  $cmd .= "  with lines lt 1 lc 2 lw 2 t 'v = +200 km/s'  ";

  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`;
  }

  
  ###############################################################
  #  Make a plot showing spectrum of a K3III star 
  #    plus the sum of the five spectra -- right on top of original
  #
  # get the arguments
  $output_file = "spec_k3_e.ps";
  $term_options = "postscript enhanced color '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";
  printf CMDFILE "set grid \n";
  printf CMDFILE "set key top left \n";
  printf CMDFILE "set xlabel 'Wavelength (Angstroms) ' \n";
  printf CMDFILE "set ylabel 'Intensity (arbitrary scale) ' \n";
  printf CMDFILE "set title 'Adding spectra of K3 III stars: v = -200, -100, 0, +100, +200 km/s \n";
  
  $cmd = "plot [5100:5300][] 'k3_spec2.dat' using (\$1):(\$2) ";
  $cmd .= "  with lines lc 1 lw 2 t 'v = 0 km/s'  ";
#  $cmd .= " , ";
#  $cmd .= "    'k3_spec2_100.dat' using (\$1):(\$2*1.5) ";
#  $cmd .= "  with lines lt 1 lc 3 lw 2 t 'v = +100 km/s'  ";
  $cmd .= " , ";
  $cmd .= "    'summed_all.dat' using (\$1):(\$2*1.0) ";
  $cmd .= "  with lines lt 1 lc -1 lw 2 t 'sum of all'  ";


  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;

