#!/usr/bin/perl
#

  
  ###############################################################
  #  Make a plot showing spectrum of early-type galaxy M32
  #
  # get the arguments
  $output_file = "spec_m32.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 galaxy A \n";
  
  $cmd = "plot [3500:8000][] 'm32_spec.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 late-type galaxy M33
  #
  # get the arguments
  $output_file = "spec_m33.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 galaxy B \n";
  
  $cmd = "plot [3500:8000][] 'm33_spec.dat' using (\$1):(\$2) ";
  $cmd .= "  with lines lc 3 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`;
  }

exit(0);


  ###############################################################
  #  Make a plot showing logarithmic intensity as function of radius
  #    for two galaxies
  #
  # get the arguments
  $output_file = "prof_log.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 logscale y \n";
  printf CMDFILE "set xlabel 'Distance from center (arcsec) ' \n";
  printf CMDFILE "set ylabel 'Surface brightness (arbitrary scale) ' \n";
  printf CMDFILE "set title 'Radial profiles of two galaxies -- logarithmic surface brightness' \n";
  
  $cmd = "plot [0:250][] 'ngc4365_prof.dat' using (\$1):(\$2) ";
  $cmd .= "  with linespoints lc 1 pt 1 lw 2 t 'NGC4365'  ";
  $cmd .= " , ";
  $cmd .= " 'm101_prof_inten.dat' using (\$1):(\$4) index 1 ";
  $cmd .= "  with linespoints lc 3 pt 5 lw 2 t 'NGC5447'  ";
  $cmd .= " , ";
  #$cmd .= " 50*exp(-x*0.01) lc -1 lt 2 lw 2 t '{/Symbol m} = K e^{-r/R_0} ' ";
  $cmd .= " 50*exp(-x*0.01) lc -1 lt 2 lw 2 t '' ";
  $cmd .= " , ";
  #$cmd .= " 10*exp(-(x/10)*0.25) lc 5 lt 1 lw 2 t '{/Symbol m} = K e^{(-r/R_1)^{0.25}} ' ";
  $cmd .= " 10*exp(-(x/10)*0.25) lc 5 lt 1 lw 2 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`;
  }

  ###############################################################
  #  Make a plot showing logarithmic scales on both axes
  #    for two galaxies
  #
  # get the arguments
  $output_file = "prof_log_log.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 logscale y \n";
  printf CMDFILE "set logscale x \n";
  printf CMDFILE "set xlabel 'Distance from center (arcsec) ' \n";
  printf CMDFILE "set ylabel 'Surface brightness (arbitrary scale) ' \n";
  printf CMDFILE "set title 'Radial profiles of two galaxies -- logarithmic on both axes' \n";
  
  $cmd = "plot [1:250][] 'ngc4365_prof.dat' using (\$1):(\$2) ";
  $cmd .= "  with linespoints lc 1 pt 1 lw 2 t 'NGC4365'  ";
  $cmd .= " , ";
  $cmd .= " 'm101_prof_inten.dat' using (\$1):(\$4) index 1 ";
  $cmd .= "  with linespoints lc 3 pt 5 lw 2 t 'NGC5447'  ";
  $cmd .= " , ";
  #$cmd .= " 500*(x**-1.5) lc -1 lt 2 t '{/Symbol m} = C r^{-a} ' ";
  $cmd .= " 50*exp(-x*0.01) lc -1 lt 2 lw 2 t '' ";
  $cmd .= " , ";
  #$cmd .= " 10*exp(-(x/10)*0.25) lc 5 lt 1 lw 2 t '' ";
  $cmd .= " 10*exp(-(x/10)*0.25) lc 5 lt 1 lw 2 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;
