#!/usr/bin/perl
#

  
  ###############################################################
  #  Make a plot showing distance modulus to LMC over time
  #
  # get the arguments
  $output_file = "lmc_estimate_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 'Year' \n";
  printf CMDFILE "set ylabel 'Distance modulus to LMC ' \n";
  printf CMDFILE "set title 'Estimates of the distance to the LMC' \n";


  
  $cmd = "plot [1960:2015][18.0:19.1] './lmc_dist.dat' ";
  $cmd .= " using (\$1+rand(0)*0.9):2:3 ";
  $cmd .= "  with yerrorbars pt 3 ps 1.5 lt 1 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 distance modulus to LMC over time
  #     Label the different methods
  #
  # get the arguments
  $output_file = "lmc_estimate_b.ps";
  $term_options = "postscript enhanced color 'Helvetica,18' size 15,8 ";
  $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 outside 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 'Year' \n";
  printf CMDFILE "set ylabel 'Distance modulus to LMC ' \n";
  printf CMDFILE "set title 'Estimates of the distance to the LMC' \n";


  
  $cmd = "plot [1965:2015][18.0:19.1] './lmc_dist_method.dat' index 0 ";
  $cmd .= " using (\$1+rand(0)*0.9):2:3 ";
  $cmd .= "  with yerrorbars pt 3 ps 1.5 lt 1 lc 1 lw 2 ";
  $cmd .= "  t 'average of several'  ";
  $cmd .= " ,  ";
  $cmd .= "   './lmc_dist_method.dat' index 1 ";
  $cmd .= " using (\$1+rand(0)*0.9):2:3 ";
  $cmd .= "  with yerrorbars pt 2 ps 1.0 lt 1 lc 2 lw 3 ";
  $cmd .= "  t 'Cepheid PL'  ";
  $cmd .= " ,  ";
  $cmd .= "   './lmc_dist_method.dat' index 2 ";
  $cmd .= " using (\$1+rand(0)*0.9):2:3 ";
  $cmd .= "  with yerrorbars pt 4 ps 1.0 lt 2 lc 2 lw 2 ";
  $cmd .= "  t 'Cepheid SB'  ";
  $cmd .= " ,  ";
  $cmd .= "   './lmc_dist_method.dat' index 3 ";
  $cmd .= " using (\$1+rand(0)*0.9):2:3 ";
  $cmd .= "  with yerrorbars pt 4 ps 1.0 lt 1 lc 3 lw 2 ";
  $cmd .= "  t 'RR Lyr'  ";
  $cmd .= " ,  ";
  $cmd .= "   './lmc_dist_method.dat' index 4 ";
  $cmd .= " using (\$1+rand(0)*0.9):2:3 ";
  $cmd .= "  with yerrorbars pt 2 ps 1.0 lt 1 lc 4 lw 2 ";
  $cmd .= "  t 'eclipsing binary'  ";
  $cmd .= " ,  ";
  $cmd .= "   './lmc_dist_method.dat' index 5 ";
  $cmd .= " using (\$1+rand(0)*0.9):2:3 ";
  $cmd .= "  with yerrorbars pt 2 ps 1.0 lt 1 lc 5 lw 3 ";
  $cmd .= "  t 'main sequence'  ";
  $cmd .= " ,  ";
  $cmd .= "   './lmc_dist_method.dat' index 6 ";
  $cmd .= " using (\$1+rand(0)*0.9):2:3 ";
  $cmd .= "  with yerrorbars pt 4 ps 1.0 lt 2 lc 5 lw 3 ";
  $cmd .= "  t 'spectro parallax'  ";
  $cmd .= " ,  ";
  $cmd .= "   './lmc_dist_method.dat' index 7 ";
  $cmd .= " using (\$1+rand(0)*0.9):2:3 ";
  $cmd .= "  with yerrorbars pt 3 ps 1.0 lt 1 lc 7 lw 2 ";
  $cmd .= "  t 'red clump'  ";
  $cmd .= " ,  ";
  $cmd .= "   './lmc_dist_method.dat' index 8 ";
  $cmd .= " using (\$1+rand(0)*0.9):2:3 ";
  $cmd .= "  with yerrorbars pt 6 ps 1.0 lt 1 lc 8 lw 2 ";
  $cmd .= "  t 'other'  ";









  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 distance modulus to LMC over time
  #     Label the different methods
  #     Closeup on results since 1995
  #
  # get the arguments
  $output_file = "lmc_estimate_c.ps";
  $term_options = "postscript enhanced color 'Helvetica,18' size 15,8 ";
  $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 outside 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 'Year' \n";
  printf CMDFILE "set ylabel 'Distance modulus to LMC ' \n";
  printf CMDFILE "set title 'Estimates of the distance to the LMC' \n";


  
  $cmd = "plot [1997:2013][18.0:19.0] './lmc_dist_method.dat' index 0 ";
  $cmd .= " using (\$1+rand(0)*0.9):2:3 ";
  $cmd .= "  with yerrorbars pt 3 ps 1.5 lt 1 lc 1 lw 2 ";
  $cmd .= "  t 'average of several'  ";
  $cmd .= " ,  ";
  $cmd .= "   './lmc_dist_method.dat' index 1 ";
  $cmd .= " using (\$1+rand(0)*0.9):2:3 ";
  $cmd .= "  with yerrorbars pt 2 ps 1.0 lt 1 lc 2 lw 3 ";
  $cmd .= "  t 'Cepheid PL'  ";
  $cmd .= " ,  ";
  $cmd .= "   './lmc_dist_method.dat' index 2 ";
  $cmd .= " using (\$1+rand(0)*0.9):2:3 ";
  $cmd .= "  with yerrorbars pt 4 ps 1.0 lt 2 lc 2 lw 2 ";
  $cmd .= "  t 'Cepheid SB'  ";
  $cmd .= " ,  ";
  $cmd .= "   './lmc_dist_method.dat' index 3 ";
  $cmd .= " using (\$1+rand(0)*0.9):2:3 ";
  $cmd .= "  with yerrorbars pt 4 ps 1.0 lt 1 lc 3 lw 2 ";
  $cmd .= "  t 'RR Lyr'  ";
  $cmd .= " ,  ";
  $cmd .= "   './lmc_dist_method.dat' index 4 ";
  $cmd .= " using (\$1+rand(0)*0.9):2:3 ";
  $cmd .= "  with yerrorbars pt 2 ps 1.0 lt 1 lc 4 lw 2 ";
  $cmd .= "  t 'eclipsing binary'  ";
  $cmd .= " ,  ";
  $cmd .= "   './lmc_dist_method.dat' index 5 ";
  $cmd .= " using (\$1+rand(0)*0.9):2:3 ";
  $cmd .= "  with yerrorbars pt 2 ps 1.0 lt 1 lc 5 lw 3 ";
  $cmd .= "  t 'main sequence'  ";
  $cmd .= " ,  ";
  $cmd .= "   './lmc_dist_method.dat' index 6 ";
  $cmd .= " using (\$1+rand(0)*0.9):2:3 ";
  $cmd .= "  with yerrorbars pt 4 ps 1.0 lt 2 lc 5 lw 3 ";
  $cmd .= "  t 'spectro parallax'  ";
  $cmd .= " ,  ";
  $cmd .= "   './lmc_dist_method.dat' index 7 ";
  $cmd .= " using (\$1+rand(0)*0.9):2:3 ";
  $cmd .= "  with yerrorbars pt 3 ps 1.0 lt 1 lc 7 lw 2 ";
  $cmd .= "  t 'red clump'  ";
  $cmd .= " ,  ";
  $cmd .= "   './lmc_dist_method.dat' index 8 ";
  $cmd .= " using (\$1+rand(0)*0.9):2:3 ";
  $cmd .= "  with yerrorbars pt 6 ps 1.0 lt 1 lc 8 lw 2 ";
  $cmd .= "  t 'other'  ";









  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 I vs. (V-I) Hertzsprung Russell
  #     diagram for subset of Hipparcos stars
  #
  # get the arguments
  $output_file = "hipp_hr_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 '(V-I)' \n";
  printf CMDFILE "set ylabel 'Absolute magnitude M_I' \n";
  printf CMDFILE "set title 'Stars from Hipparcos with parallax > 18 mas, uncert < 3 mas' \n";

  
  $cmd = "plot [-0.5:3][10:-2] './hip_sample.dat' ";
  $cmd .= " using (\$22):((\$14-\$22)+5-5*log10(1.0/(0.001*\$17))) ";
  $cmd .= "   with points ps 0.2 pt 3 lw 3 lc -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;
  
  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 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;
