#!/usr/bin/perl
#

  
  my($output_file, $term_options);

  # FK4 stars only
  $output_file = "parallax_fk4_a.ps";
  $term_options = "postscript color enhanced '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 ylabel 'Number of measurements '  \n";
  printf CMDFILE "set xlabel 'Parallax (millarcsec) ' \n";
  printf CMDFILE "set title 'Measurements listed in FK4 (1963) '\n";
  
  $cmd = "plot [-50:1000][0:10]  ";
  $cmd .= "   './fk4_cat_hist.dat' using (\$1+25):2 with impulse lw 8 ";

  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 -flatten -background white -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }


  # YBS stars only
  $output_file = "parallax_ybs_a.ps";
  $term_options = "postscript color enhanced '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 ylabel 'Number of measurements '  \n";
  printf CMDFILE "set xlabel 'Parallax (millarcsec) ' \n";
  printf CMDFILE "set title 'Measurements listed in Yale Bright Star Catalog (1964-1991) '\n";
  
  $cmd = "plot [-10:200][]  ";
  $cmd .= "   './ybs_cat_hist.dat' using (\$1+(5/2)):2 with impulse lw 8 ";

  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 -flatten -background white -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }


  # Hipparcos stars only (first 10000 of them)
  $output_file = "parallax_hip_a.ps";
  $term_options = "postscript color enhanced '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 ylabel 'Number of measurements '  \n";
  printf CMDFILE "set xlabel 'Parallax (millarcsec) ' \n";
  printf CMDFILE "set title 'Measurements listed in Hipparcos (1997)'\n";
  
  $cmd = "plot [-10:200][]  ";
  $cmd .= "   './hip_partial_hist.dat' using (\$1+(1/2)):2 with impulse lw 8 ";

  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 -flatten -background white -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }


  # Gaia stars only (first 21618 of them)
  $output_file = "parallax_gaia_a.ps";
  $term_options = "postscript color enhanced '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 ylabel 'Number of measurements '  \n";
  printf CMDFILE "set xlabel 'Parallax (millarcsec) ' \n";
  printf CMDFILE "set title 'Measurements listed in Gaia DR1 (2016)'\n";
  
  $cmd = "plot [-10:200][]  ";
  $cmd .= "   './gaia_50sqdeg_cat_hist.dat' using (\$1+(0.1/2)):2 with impulse lw 8 ";

  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 -flatten -background white -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }


  # YBS and Hipparcos only stars 
  $output_file = "parallax_comp_a1.ps";
  $term_options = "postscript color enhanced '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 \n";
  printf CMDFILE "set ylabel 'Number of measurements '  \n";
  printf CMDFILE "set xlabel 'Parallax (millarcsec) ' \n";
  printf CMDFILE "set title 'Advances in parallax (binsize 1 mas)'\n";
  
  $cmd = "plot [-5:50][]  ";
  $cmd .= "   './ybs_cat_hist_bin1.dat' using (\$1+(1/2)):(\$2*100) with points pt 6 ps 2 lw 3 ";
  $cmd .= "      t 'YBS (scaled x 100)' ";
  $cmd .= " , ";
  $cmd .= "   './hip_partial_hist.dat' using (\$1+(1/2)):(\$2*11.8) with linespoints pt 3 lw 3 ";
  $cmd .= "      t 'Hipparcos' ";


  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 -flatten -background white -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }



  # YBS and Hipparcos and Gaia stars 
  $output_file = "parallax_comp_a.ps";
  $term_options = "postscript color enhanced '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 \n";
  printf CMDFILE "set ylabel 'Number of measurements '  \n";
  printf CMDFILE "set xlabel 'Parallax (millarcsec) ' \n";
  printf CMDFILE "set title 'Advances in parallax '\n";
  
  $cmd = "plot [-5:50][]  ";
  $cmd .= "   './ybs_cat_hist_bin1.dat' using (\$1+(5/2)):(\$2*30000) with points pt 6 ps 2 lw 3 ";
  $cmd .= "      t 'YBS (scaled x 30000)' ";
  $cmd .= " , ";
  $cmd .= "   './hip_partial_hist.dat' using (\$1+(1/2)):(\$2*11.8*300) with lines lw 3 ";
  $cmd .= "      t 'Hipparcos (scaled x 300)' ";
  $cmd .= " , ";
  $cmd .= "   './gaia_50sqdeg_cat_hist.dat' using (\$1+(0.1/2)):(\$2*825) with lines lc 4 lw 3 ";
  $cmd .= "      t 'Gaia' ";


  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 -flatten -background white -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }

  # YBS and Hipparcos and Gaia stars (logarithmic scale)
  $output_file = "parallax_comp_b.ps";
  $term_options = "postscript color enhanced '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 \n";
  printf CMDFILE "set logscale y \n";
  printf CMDFILE "set ylabel 'Number of  measurements '  \n";
  printf CMDFILE "set xlabel 'Parallax (millarcsec) ' \n";
  printf CMDFILE "set title 'Advances in parallax '\n";
  
  $cmd = "plot [-2:30][]  ";
  $cmd .= "   './ybs_cat_hist_bin1.dat' using (\$1+(1/2)):(\$2*1) with points pt 6 ps 2 lw 3 ";
  $cmd .= "      t 'YBS' ";
  $cmd .= " , ";
  $cmd .= "   './hip_partial_hist.dat' using (\$1+(1/2)):(\$2*11.8) with linespoints pt 3 lw 2 ";
  $cmd .= "      t 'Hipparcos' ";
  $cmd .= " , ";
  $cmd .= "   './gaia_50sqdeg_cat_hist.dat' using (\$1+(1/2)):(\$2*825) with linespoints pt 6 lw 4 ";
  $cmd .= "      t 'Gaia' ";

  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 -flatten -background white -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }


  # Hipparcos and Gaia stars (logarithmic scale, 0.1 mas bins)
  $output_file = "parallax_comp_c.ps";
  $term_options = "postscript color enhanced '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 \n";
  printf CMDFILE "set logscale y \n";
  printf CMDFILE "set ylabel 'Number of  measurements '  \n";
  printf CMDFILE "set xlabel 'Parallax (millarcsec) ' \n";
  printf CMDFILE "set title 'Advances in parallax (0.1 mas bins)'\n";
  
  $cmd = "plot [-1:10][]  ";
  $cmd .= "   './ybs_cat_hist_bin1.dat' using (20+\$1+(1/2)):(\$2*1) with points pt 6 ps 2 lw 3 ";
  $cmd .= "      t '' ";
  $cmd .= " , ";
  $cmd .= "   './hip_partial_hist_bin01.dat' using (\$1+(1/2)):(\$2*11.8) with linespoints pt 3 lw 2 ";
  $cmd .= "      t 'Hipparcos' ";
  $cmd .= " , ";
  $cmd .= "   './gaia_50sqdeg_cat_hist_bin01.dat' using (\$1+(1/2)):(\$2*825) with linespoints pt 6 lw 4 ";
  $cmd .= "      t 'Gaia' ";

  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 -flatten -background white -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }



  # Distribution of real distances, based on parallax simulation
  #    small errors only
  $output_file = "dist_comp_a.ps";
  $term_options = "postscript color enhanced '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 \n";
  printf CMDFILE "set nologscale y \n";
  printf CMDFILE "set ylabel 'Number of stars '  \n";
  printf CMDFILE "set xlabel 'True distance (pc) ' \n";
  printf CMDFILE "set title 'Simulation of stars with parallax = 10 mas +/- '\n";
  
  $cmd = "plot [80:130][]  ";
  $cmd .= "   './calc_bias_10_05.hist' using (\$1+0.25):(\$2) with boxes lc rgbcolor \"blue\" lw 2 ";
  $cmd .= "      t 'uncert 5%' ";
  $cmd .= " , ";
  $cmd .= "   './calc_bias_10_02.hist' using (\$1+0):(\$2) with impulse lc rgbcolor \"red\" lw 7 ";
  $cmd .= "      t 'uncert 2%' ";

  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 -flatten -background white -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }



  # Distribution of real distances, based on parallax simulation
  #    small and moderate errors 
  $output_file = "dist_comp_b.ps";
  $term_options = "postscript color enhanced '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 \n";
  printf CMDFILE "set nologscale y \n";
  printf CMDFILE "set ylabel 'Number of stars '  \n";
  printf CMDFILE "set xlabel 'True distance (pc) ' \n";
  printf CMDFILE "set title 'Simulation of stars with parallax = 10 mas +/- '\n";
  
  $cmd = "plot [70:160][]  ";
  $cmd .= "   './calc_bias_10_1.hist' using (\$1+0):(\$2) with points pt 6 ";
  $cmd .= "      lc rgbcolor \"dark-green\" lw 4 ";
  $cmd .= "      t 'uncert 10%' ";
  $cmd .= " , ";
  $cmd .= "   './calc_bias_10_05.hist' using (\$1+0.25):(\$2) with boxes lc rgbcolor \"blue\" lw 2 ";
  $cmd .= "      t 'uncert 5%' ";
  $cmd .= " , ";
  $cmd .= "   './calc_bias_10_02.hist' using (\$1+0):(\$2) with impulse lc rgbcolor \"red\" lw 7 ";
  $cmd .= "      t 'uncert 2%' ";

  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 -flatten -background white -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }


  # Distribution of real distances, based on parallax simulation
  #    small and large errors 
  $output_file = "dist_comp_c.ps";
  $term_options = "postscript color enhanced '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 \n";
  printf CMDFILE "set nologscale y \n";
  printf CMDFILE "set ylabel 'Number of stars '  \n";
  printf CMDFILE "set xlabel 'True distance (pc) ' \n";
  printf CMDFILE "set title 'Simulation of stars with parallax = 10 mas +/- '\n";
  
  $cmd = "plot [60:250][]  ";
  $cmd .= "   './calc_bias_10_2.hist' using (\$1+0):(\$2) with lines ";
  $cmd .= "      lc rgbcolor \"dark-orange\" lw 3 ";
  $cmd .= "      t 'uncert 20%' ";
  $cmd .= " , ";
  $cmd .= "   './calc_bias_10_1.hist' using (\$1+0):(\$2) with points ";
  $cmd .= "      lc rgbcolor \"dark-green\" pt 6 lw 4 ";
  $cmd .= "      t 'uncert 10%' ";
  $cmd .= " , ";
  $cmd .= "   './calc_bias_10_05.hist' using (\$1+0.25):(\$2) with boxes ";
  $cmd .= "      lc rgbcolor \"blue\" lw 2 ";
  $cmd .= "      t 'uncert 5%' ";
  $cmd .= " , ";
  $cmd .= "   './calc_bias_10_02.hist' using (\$1+0):(\$2) with impulse ";
  $cmd .= "      lc rgbcolor \"red\" lw 7 ";
  $cmd .= "      t 'uncert 2%' ";

  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 -flatten -background white -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }



  # Distribution of real distances, based on parallax simulation
  #    small and silly errors 
  $output_file = "dist_comp_d.ps";
  $term_options = "postscript color enhanced '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 \n";
  printf CMDFILE "set nologscale y \n";
  printf CMDFILE "set ylabel 'Number of stars '  \n";
  printf CMDFILE "set xlabel 'True distance (pc) ' \n";
  printf CMDFILE "set title 'Simulation of stars with parallax = 10 mas +/- '\n";
  
  $cmd = "plot [20:1250][]  ";
  $cmd .= "   './calc_bias_10_3.hist' using (\$1+0):(\$2) with lines ";
  $cmd .= "      lc rgbcolor \"black\" lw 2 ";
  $cmd .= "      t 'uncert 30%' ";
  $cmd .= " , ";
  $cmd .= "   './calc_bias_10_2.hist' using (\$1+0):(\$2) with lines ";
  $cmd .= "      lc rgbcolor \"dark-orange\" lw 3 ";
  $cmd .= "      t 'uncert 20%' ";
  $cmd .= " , ";
  $cmd .= "   './calc_bias_10_1.hist' using (\$1+0):(\$2) with points ";
  $cmd .= "      lc rgbcolor \"dark-green\" pt 6 lw 4 ";
  $cmd .= "      t 'uncert 10%' ";
  $cmd .= " , ";
  $cmd .= "   './calc_bias_10_05.hist' using (\$1+0.25):(\$2) with boxes ";
  $cmd .= "      lc rgbcolor \"blue\" lw 2 ";
  $cmd .= "      t 'uncert 5%' ";
  $cmd .= " , ";
  $cmd .= "   './calc_bias_10_02.hist' using (\$1+0):(\$2) with impulse ";
  $cmd .= "      lc rgbcolor \"red\" lw 3 ";
  $cmd .= "      t 'uncert 2%' ";

  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 -flatten -background white -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }










exit 0;
