#!/usr/bin/perl
#

  
  ###############################################################
  #  Make a plot showing Schechter lum func vs. LG data
  #
  # get the arguments
  $output_file = "lg_lumfunc.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 x \n";
  printf CMDFILE "set logscale y \n";
  #printf CMDFILE "set xtics (2000,4000,6000) \n";
  #printf CMDFILE "set ytics (0.4,0.8,1.0,4.0) \n";
  printf CMDFILE "set xlabel 'Absolute magnitude (g-band)' \n";
  printf CMDFILE "set ylabel 'Number of galaxies ' \n";
  printf CMDFILE "set title 'Luminosity function of the Local Group ' \n";


  
# data is taken from
#    http://adsabs.harvard.edu/abs/2009MNRAS.399.1106M
#    Montaro-Dorta and Prado, MNRAS 399, 1106 (2009) 
  $cmd = "plot [-4:-28][1e-4:1e-2] './compute_lf.out' index 0 using 1:3 ";
  $cmd .= "  with lines lt 2 lc 1 lw 3 t ''  ";
  $cmd .= " , ";
  $cmd .= "    './compute_lf.out' index 1 using 1:3 ";
  $cmd .= "  with lines lt 1 lc 1 lw 3 t 'SDSS luminosity function'  ";
  $cmd .= " , ";
  $cmd .= " 'irwin_hist_2.dat' using (\$1):(\$2*3e-4) ";
  $cmd .= "  with points lc 3 pt 5 lw 2 t 'Local Group'  ";

  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 motion of MW and Andromeda in toy model
  #
  # get the arguments
  $output_file = "andromeda_motion.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 at 8.5,1750 \n";
  printf CMDFILE "set grid \n";
  #printf CMDFILE "set logscale x \n";
  #printf CMDFILE "set logscale y \n";
  #printf CMDFILE "set xtics (2000,4000,6000) \n";
  #printf CMDFILE "set ytics (0.4,0.8,1.0,4.0) \n";
  printf CMDFILE "set xlabel 'Time in the past (Gyr)' \n";
  printf CMDFILE "set ylabel 'Distance (kpc) ' \n";
  printf CMDFILE "set title 'Motion of M31 and Milky Way in a toy model' \n";


  
# data is taken from
#    http://adsabs.harvard.edu/abs/2009MNRAS.399.1106M
#    Montaro-Dorta and Prado, MNRAS 399, 1106 (2009) 
  $cmd = "plot [0:20][-900:1800] './nbody/nbody.a.out' using (\$1/3.15e16):(\$4/3.08e19) ";
  $cmd .= "  with points ps 1 pt 1 t 'total 2 x 10^{12} solar'  ";
  $cmd .= " , ";
  $cmd .= "  './nbody/nbody.b.out' using (\$1/3.15e16):(\$4/3.08e19) ";
  $cmd .= "  with points ps 1 pt 2 t 'total 4 x 10^{12} solar'  ";
  $cmd .= " , ";
  $cmd .= "  './nbody/nbody.d.out' using (\$1/3.15e16):(\$4/3.08e19) ";
  $cmd .= "  with points ps 1 pt 3 t 'total 5 x 10^{12} solar'  ";
  $cmd .= " , ";
  $cmd .= "  './nbody/nbody.e.out' using (\$1/3.15e16):(\$4/3.08e19) ";
  $cmd .= "  with points ps 1 pt 3 t 'total 6 x 10^{12} solar'  ";




  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 Keplerian rotation curve in solar system
  #     this version runs to positive and negative distances
  #
  # get the arguments
  $output_file = "kepler_curve_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 right \n";
  #printf CMDFILE "set logscale x \n";
  #printf CMDFILE "set logscale y \n";
  #printf CMDFILE "set xtics (2000,4000,6000) \n";
  #printf CMDFILE "set ytics (0.4,0.8,1.0,4.0) \n";
  printf CMDFILE "set xlabel 'Distance from Sun (AU)' \n";
  printf CMDFILE "set ylabel 'Orbital velocity (km/s) ' \n";
  printf CMDFILE "set title 'Keplerian motion in the solar system' \n";
  
  $cmd = "plot [-50:50][]  (x/sqrt(x*x))*29.75/((x*x)**0.25) ";
  $cmd .= "  with lines 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;
  
  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 integrated light as function of radius
  #     in NGC 4374, and compare to linear growth
  #
  # get the arguments
  $output_file = "integrated_light_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 reverse Left \n";
  #printf CMDFILE "set logscale x \n";
  #printf CMDFILE "set logscale y \n";
  #printf CMDFILE "set xtics (2000,4000,6000) \n";
  #printf CMDFILE "set ytics (0.4,0.8,1.0,4.0) \n";
  printf CMDFILE "set xlabel 'Distance from center of galaxy (pixels)' \n";
  printf CMDFILE "set ylabel 'Integrated light (counts)' \n";
  printf CMDFILE "set title 'Profile of spiral galaxy NGC 4374 ' \n";
  
  $cmd = "plot [1:130][]  './integrate_light.out' using 1:3 ";
  $cmd .= "  with points lc 1 ps 1.5 lw 2 t 'measured light'  ";
  $cmd .= " , ";
  $cmd .= " x*80000 with lines lc 3 t 'integrated light = K r' ";
  

  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;
