#!/usr/bin/perl
#
  
  ###############################################################
  #  Make a graph showing spectrum of spiral galaxy and
  #     transmission curve of Bessell V filter
  #
  # get the arguments
  $output_file = "kcor_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 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 'Wavelength (Angstroms)' \n";
  printf CMDFILE "set ylabel 'Flux (ergs/cm^2/s/Angstrom)' \n";
  printf CMDFILE "set title 'A spiral galaxy ' \n";


  
  $cmd = "plot [3750:8000][0:1.4] './gal_spectrum.txt' using 1:2 ";
  $cmd .= "  with lines  lc 1 lw 2 t 'galaxy'  ";
  $cmd .= " ,  ";
  $cmd .= " './bess_v.pass' using 1:2 with lines lc -1 lw 2 t 'V passband' ";

  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 graph showing spectrum of spiral galaxy  
  #     at z=0 and z=0.05, and 
  #     transmission curve of Bessell V filter
  #
  # get the arguments
  $output_file = "kcor_b.ps";
  $term_options = "postscript enhanced color 'Helvetica,18' size 11,9";
  $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 7000,0.60\n";
  #printf CMDFILE "set logscale x \n";
  #printf CMDFILE "set logscale y \n";
  printf CMDFILE "set multiplot layout 2,1 \n";
  printf CMDFILE "set xlabel 'Wavelength (Angstroms)' \n";
  printf CMDFILE "set ylabel 'Flux (ergs/cm^2/s/Angstrom)' \n";
  printf CMDFILE "set title 'A spiral galaxy and the Bessell V passband' \n";


  
  $cmd = "plot [4500:7000][0:1.2] './gal_spectrum.txt' using 1:2 ";
  $cmd .= "  with lines  lt 1 lc 1 lw 2 t 'galaxy at z=0.00'  ";
  $cmd .= " ,  ";
  #$cmd .= "                        './gal_spectrum.txt' using (\$1*1.05):2 ";
  #$cmd .= "  with lines  lt 1 lc 3 lw 2 t 'galaxy at z=0.05'  ";
  #$cmd .= " ,  ";
  $cmd .= " './bess_v.pass' using 1:2 with lines lc -1 lw 2 t '' ";
  printf CMDFILE "$cmd \n";

  printf CMDFILE "set title '' \n";
  $cmd = "plot [4500:7000][0:1.2] './gal_spectrum.txt' using (\$1*1.05):2 ";
  $cmd .= "  with lines  lt 1 lc 3 lw 2 t 'galaxy at z=0.05'  ";
  $cmd .= " ,  ";
  $cmd .= " './bess_v.pass' using 1:2 with lines lc -1 lw 2 t '' ";
  printf CMDFILE "$cmd \n";





  printf CMDFILE "unset multiplot \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 graph showing spectrum of spiral galaxy  
  #     at z=0 and z=0.05, and 
  #     transmission curve of Bessell I filter
  #
  # get the arguments
  $output_file = "kcor_i.ps";
  $term_options = "postscript enhanced color 'Helvetica,18' size 11,9";
  $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 8500,0.60\n";
  #printf CMDFILE "set logscale x \n";
  #printf CMDFILE "set logscale y \n";
  printf CMDFILE "set multiplot layout 2,1 \n";
  printf CMDFILE "set xlabel 'Wavelength (Angstroms)' \n";
  printf CMDFILE "set ylabel 'Flux (erg/cm^2/s/Angstrom)' \n";
  printf CMDFILE "set title 'A spiral galaxy and the Bessell I passband' \n";


  
  $cmd = "plot [6500:9500][0:1.2] './gal_spectrum.txt' using 1:2 ";
  $cmd .= "  with lines  lt 1 lc 1 lw 2 t 'galaxy at z=0.00'  ";
  $cmd .= " ,  ";
  #$cmd .= "                        './gal_spectrum.txt' using (\$1*1.05):2 ";
  #$cmd .= "  with lines  lt 1 lc 3 lw 2 t 'galaxy at z=0.05'  ";
  #$cmd .= " ,  ";
  $cmd .= " './bess_i.pass' using 1:2 with lines lc -1 lw 2 t '' ";
  printf CMDFILE "$cmd \n";

  printf CMDFILE "set title '' \n";
  $cmd = "plot [6500:9500][0:1.2] './gal_spectrum.txt' using (\$1*1.05):2 ";
  $cmd .= "  with lines  lt 1 lc 3 lw 2 t 'galaxy at z=0.05'  ";
  $cmd .= " ,  ";
  $cmd .= " './bess_i.pass' using 1:2 with lines lc -1 lw 2 t '' ";
  printf CMDFILE "$cmd \n";





  printf CMDFILE "unset multiplot \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 graph showing K correction for a spiral galaxy and V-band
  #
  # get the arguments
  $output_file = "kcor_delta_v.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 logscale x \n";
  #printf CMDFILE "set logscale y \n";
  printf CMDFILE "set xlabel 'Redshift z ' \n";
  printf CMDFILE "set ylabel 'Mag at z - Mag at rest' \n";
  printf CMDFILE "set title 'K corrections for a spiral galaxy and the Bessell V passband' \n";


  
  $cmd = "plot [-0.01:0.25][0.30:-0.05] './calc_kcor_v.out' using 2:4 ";
  $cmd .= "  with points  lt 1 lc 2 lw 4 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 graph showing K corrections for a spiral galaxy and 
  #     the Bessell V-band and I-band
  #
  # get the arguments
  $output_file = "kcor_delta_i_v.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 bot left \n";
  #printf CMDFILE "set logscale x \n";
  #printf CMDFILE "set logscale y \n";
  printf CMDFILE "set xlabel 'Redshift z ' \n";
  printf CMDFILE "set ylabel 'Mag at z - Mag at rest' \n";
  printf CMDFILE "set title 'K corrections for a spiral galaxy and Bessell passbands' \n";


  
  $cmd = "plot [-0.01:0.25][0.30:-0.05] './calc_kcor_v.out' using 2:4 ";
  $cmd .= "  with points  lt 1 lc 2 lw 4 t 'V-band'  ";
  $cmd .= " ,  ";
  $cmd .= "  './calc_kcor_i.out' using 2:4 ";
  $cmd .= "  with points  lt 1 lc 1 lw 2 t 'I-band'  ";
  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 graph showing spectrum of spiral galaxy  
  #     at z=0, through Bessell B filter,
  #     at z=0.3, through Bessell V  filter
  #
  # get the arguments
  $output_file = "kcor_swap.ps";
  $term_options = "postscript enhanced color 'Helvetica,18' size 11,9";
  $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 6500,1.15\n";
  #printf CMDFILE "set logscale x \n";
  #printf CMDFILE "set logscale y \n";
  printf CMDFILE "set multiplot layout 2,1 \n";
  printf CMDFILE "set xlabel 'Wavelength (Angstroms)' \n";
  printf CMDFILE "set ylabel 'Flux (ergs/cm^2/s/Angstrom)' \n";
  printf CMDFILE "set title 'Using different filters to minimize K corrections' \n";


  
  $cmd = "plot [3500:7000][0:1.2] './gal_spectrum_ang.txt' using 1:(\$2*8) ";
  $cmd .= "  with lines  lt 1 lc -1 lw 2 t 'galaxy at z=0.00'  ";
  $cmd .= " ,  ";
  #$cmd .= "                        './gal_spectrum.txt' using (\$1*1.05):2 ";
  #$cmd .= "  with lines  lt 1 lc 3 lw 2 t 'galaxy at z=0.05'  ";
  #$cmd .= " ,  ";
  $cmd .= " './bess_b.pass' using 1:2 with lines lc 3 lw 2 t 'B filter' ";
  printf CMDFILE "$cmd \n";

  printf CMDFILE "set title '' \n";

  $cmd = "plot [3500:7000][0:1.2] './gal_spectrum_ang_z03.txt' using (\$1):(\$2*8) ";
  $cmd .= "  with lines  lt 1 lc -1 lw 2 t 'galaxy at z=0.30'  ";
  $cmd .= " ,  ";
  $cmd .= " './bess_v.pass' using 1:2 with lines lc 2 lw 3 t 'V filter' ";
  printf CMDFILE "$cmd \n";





  printf CMDFILE "unset multiplot \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 Hubble diagram using fake perfect data
  #
  # get the arguments
  $output_file = "hubble_fake_1.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 (Mpc)' \n";
  printf CMDFILE "set ylabel 'Velocity (km/sec)' \n";
  printf CMDFILE "set title 'Hubble diagram, version A ' \n";

  
  $cmd = "plot [][] './hubble_fake_1.dat' using 2:1 ";
  $cmd .= "  every 1 with points  pt 4 ps 0.5 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 Hubble diagram using fake perfect data
  #     We swap the axes, so velocity on horizontal axis
  #
  # get the arguments
  $output_file = "hubble_fake_2.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 'Velocity (km/sec)' \n";
  printf CMDFILE "set ylabel 'Distance (Mpc)' \n";
  printf CMDFILE "set title 'Hubble diagram, version B (swap axes)' \n";

  
  $cmd = "plot [][] './hubble_fake_1.dat' using 1:2 ";
  $cmd .= "  every 1 with points  pt 4 ps 0.5 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 Hubble diagram using fake perfect data
  #     We swap the axes, so velocity on horizontal axis
  #     and now take logarithm of both axes
  #
  # get the arguments
  $output_file = "hubble_fake_3.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 'Velocity (km/sec)' \n";
  printf CMDFILE "set ylabel 'Distance (Mpc)' \n";
  printf CMDFILE "set title 'Hubble diagram, version C (take log) ' \n";

  
  $cmd = "plot [][] './hubble_fake_1.dat' using 1:2 ";
  $cmd .= "  every 1 with points  pt 4 ps 0.5 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 Hubble diagram using fake perfect data
  #     We swap the axes, so velocity on horizontal axis
  #     and now take logarithm of both axes, but on linear scale
  #
  # get the arguments
  $output_file = "hubble_fake_4.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 'Log_{10} Velocity (km/sec)' \n";
  printf CMDFILE "set ylabel 'Log_{10} Distance (Mpc)' \n";
  printf CMDFILE "set title 'Hubble diagram, version D (log on linear paper) ' \n";

  
  $cmd = "plot [][] './hubble_fake_1.dat' using (log10(\$1)):(log10(\$2)) ";
  $cmd .= "  every 1 with points  pt 4 ps 0.5 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 Hubble diagram using fake perfect data
  #     We swap the axes, so velocity on horizontal axis
  #     and now take logarithm of both axes, but on linear scale
  #     and add distance modulus, too
  #
  # get the arguments
  $output_file = "hubble_fake_5.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 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 'Log_{10} Velocity (km/sec)' \n";
  printf CMDFILE "set ylabel '' \n";
  printf CMDFILE "set title 'Hubble diagram, version E (distance modulus) ' \n";

  
  $cmd = "plot [0.8:3.7][] './hubble_fake_1.dat' using (log10(\$1)):(log10(\$2)) ";
  $cmd .= "  every 1 with points  pt 4 ps 0.5 lc 1 lw 3 t 'log_{10} dist (Mpc)'  ";
  $cmd .= " , ";
  $cmd .= "   './hubble_fake_1.dat' using (log10(\$1)):(log10(\$2*1.0e6)) ";
  $cmd .= "  every 1 with points  pt 5 ps 0.6 lc -1 lw 3 t 'log_{10} dist (pc) '  ";
  $cmd .= " , ";
  $cmd .= "   './hubble_fake_1.dat' using (log10(\$1)):(\$3) ";
  $cmd .= "  every 1 with points  pt 6 ps 0.5 lc 3 lw 3 t 'distance modulus'  ";


  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 Hubble diagram using fake perfect data
  #     This starts the relativistic series: 
  #          low redshift, dist mod vs. velocity
  #
  # get the arguments
  $output_file = "hubble_fake_6.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 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 'Velocity (km/sec) ' \n";
  printf CMDFILE "set ylabel 'Distance modulus (mag)' \n";
  printf CMDFILE "set title 'Hubble diagram, version F (it curves!)  ' \n";

  
  $cmd = "plot [0.0:3.0e4][] './hubble_fake_rel.dat' using (\$1):(\$3) ";
  $cmd .= "  every 1 with linespoints  pt 4 ps 0.9 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 Hubble diagram using fake perfect data
  #     This starts the relativistic series: 
  #          low redshift, dist mod vs. redshift
  #
  # get the arguments
  $output_file = "hubble_fake_7.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 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 'Redshift z ' \n";
  printf CMDFILE "set ylabel 'Distance modulus (mag)' \n";
  printf CMDFILE "set title 'Hubble diagram, version G (redshift)  ' \n";

  
  $cmd = "plot [0.0:0.11][] './hubble_fake_rel.dat' using (\$4):(\$3) ";
  $cmd .= "  every 1 with linespoints  pt 4 ps 0.9 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 Hubble diagram using fake perfect data
  #     This starts the relativistic series: 
  #          low redshift, dist mod vs. log(redshift)
  #
  # get the arguments
  $output_file = "hubble_fake_8.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 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 'Log_{10} z ' \n";
  printf CMDFILE "set ylabel 'Distance modulus (mag)' \n";
  printf CMDFILE "set title 'Hubble diagram, version H (log of redshift)  ' \n";

  
  $cmd = "plot [-2.0:-0.80][] './hubble_fake_rel.dat' using (log10(\$4)):(\$3) ";
  $cmd .= "  every 1 with linespoints  pt 4 ps 0.9 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`;
  }







exit 0;
  
  ###############################################################
  #  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;
