#!/usr/bin/perl
#

  
  my($output_file, $term_options);

  # Gaia vs. Hip parallax values -- wide view
  #
  $output_file = "gaia_hip_delta_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 'Difference in parallax (Gaia - Hipparcos) (mas) '  \n";
  printf CMDFILE "set xlabel 'Gaia parallax (millarcsec) ' \n";
  printf CMDFILE "set title 'Comparison of Gaia and Hipparcos parallax measurements '\n";
  
  $cmd = "plot [0:50][-10:10]  ";
  $cmd .= "   './gaia_hip_comp.dat' using (\$4):(\$4-\$9) ";
  $cmd .= "      ps 0.1 lc rgbcolor \"red\"  t '' ";
  $cmd .= " , ";
  $cmd .= "    0 with lines lw 3 lc -1 t '' ";
  $cmd .= " , ";
  $cmd .= "   './gaia_hip_delta.out' using (0.5*(\$1+\$2)):(\$4) ";
  $cmd .= "      with linespoints ps 2 pt 6 lw 3 lc rgbcolor \"blue\"  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 -flatten -background white -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }


  # Gaia vs. Hip parallax values -- zoom in
  #
  $output_file = "gaia_hip_delta_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 "unset key \n";
  printf CMDFILE "set ylabel 'Difference in parallax (Gaia - Hipparcos) (mas) '  \n";
  printf CMDFILE "set xlabel 'Gaia parallax (millarcsec) ' \n";
  printf CMDFILE "set title 'Comparison of Gaia and Hipparcos parallax measurements '\n";
  
  $cmd = "plot [0:20][-2:2]  ";
  $cmd .= "   './gaia_hip_comp.dat' using (\$4):(\$4-\$9) ";
  $cmd .= "      ps 0.1 lc rgbcolor \"red\"  t '' ";
  $cmd .= " , ";
  $cmd .= "    0 with lines lw 3 lc -1 t '' ";
  $cmd .= " , ";
  $cmd .= "   './gaia_hip_delta.out' using (0.5*(\$1+\$2)):(\$4) ";
  $cmd .= "      with linespoints ps 2 pt 6 lw 3 lc rgbcolor \"blue\"  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 -flatten -background white -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }



  # Gaia vs. Hip parallax values -- zoom in more
  #
  $output_file = "gaia_hip_delta_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 "unset key \n";
  printf CMDFILE "set ylabel 'Difference in parallax (Gaia - Hipparcos) (mas) '  \n";
  printf CMDFILE "set xlabel 'Gaia parallax (millarcsec) ' \n";
  printf CMDFILE "set title 'Comparison of Gaia and Hipparcos parallax measurements '\n";
  
  $cmd = "plot [0:20][-0.8:0.8]  ";
  $cmd .= "   './gaia_hip_comp.dat' using (\$4):(\$4-\$9) ";
  $cmd .= "      ps 0.1 lc rgbcolor \"red\"  t '' ";
  $cmd .= " , ";
  $cmd .= "    0 with lines lw 3 lc -1 t '' ";
  $cmd .= " , ";
  $cmd .= "   './gaia_hip_delta.out' using (0.5*(\$1+\$2)):(\$4) ";
  $cmd .= "      with linespoints ps 2 pt 6 lw 3 lc rgbcolor \"blue\"  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 -flatten -background white -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }


  # Gaia vs. Hip DISTANCE values -- wide view
  #
  $output_file = "gaia_hip_delta_dist_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 'Difference in distance (Gaia - Hipparcos) (pc) '  \n";
  printf CMDFILE "set xlabel 'Gaia parallax (millarcsec) ' \n";
  printf CMDFILE "set title 'Comparison of Gaia and Hipparcos distance measurements '\n";
  
  $cmd = "plot [0:50][-500:500]  ";
  $cmd .= "   './gaia_hip_comp.dat' using (\$4):((1000.0/\$4)-(1000.0/\$9)) ";
  $cmd .= "      ps 0.1 lc rgbcolor \"red\"  t '' ";
  $cmd .= " , ";
  $cmd .= "    0 with lines lw 3 lc -1 t '' ";
  $cmd .= " , ";
  $cmd .= "   './gaia_hip_delta_dist.out' using (0.5*(\$1+\$2)):(\$4) ";
  $cmd .= "      with linespoints ps 2 pt 6 lw 3 lc rgbcolor \"blue\"  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 -flatten -background white -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }


  # Gaia vs. Hip DISTANCE values -- zoom in view
  #
  $output_file = "gaia_hip_delta_dist_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 "unset key \n";
  printf CMDFILE "set ylabel 'Difference in distance (Gaia - Hipparcos) (pc) '  \n";
  printf CMDFILE "set xlabel 'Gaia parallax (millarcsec) ' \n";
  printf CMDFILE "set title 'Comparison of Gaia and Hipparcos distance measurements '\n";
  
  $cmd = "plot [0:20][-200:200]  ";
  $cmd .= "   './gaia_hip_comp.dat' using (\$4):((1000.0/\$4)-(1000.0/\$9)) ";
  $cmd .= "      ps 0.1 lc rgbcolor \"red\"  t '' ";
  $cmd .= " , ";
  $cmd .= "    0 with lines lw 3 lc -1 t '' ";
  $cmd .= " , ";
  $cmd .= "   './gaia_hip_delta_dist.out' using (0.5*(\$1+\$2)):(\$4) ";
  $cmd .= "      with linespoints ps 2 pt 6 lw 3 lc rgbcolor \"blue\"  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 -flatten -background white -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }



  # Gaia vs. Hip DISTANCE values -- plotted with Gaia dist on X-axis
  #
  $output_file = "gaia_hip_delta_dist_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 "unset key \n";
  printf CMDFILE "set ylabel 'Difference in distance (Gaia - Hipparcos) (pc) '  \n";
  printf CMDFILE "set xlabel 'Gaia distance (pc) ' \n";
  printf CMDFILE "set title 'Comparison of Gaia and Hipparcos distance measurements '\n";
  
  $cmd = "plot [0:1000][-300:300]  ";
  $cmd .= "   './gaia_hip_comp.dat' using ((1000.0/\$4)):((1000.0/\$4)-(1000.0/\$9)) ";
  $cmd .= "      ps 0.1 lc rgbcolor \"red\"  t '' ";
  $cmd .= " , ";
  $cmd .= "    0 with lines lw 3 lc -1 t '' ";
  $cmd .= " , ";
  $cmd .= "   './gaia_hip_delta_dist.out' using ((1000.0/(0.5*(\$1+\$2)))):(\$4) ";
  $cmd .= "      with linespoints ps 2 pt 6 lw 3 lc rgbcolor \"blue\"  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 -flatten -background white -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }




  # Gaia vs. Hip DISTANCE values -- plotted with Gaia dist on X-axis
  #    zoomed in view
  #
  $output_file = "gaia_hip_delta_dist_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 "unset key \n";
  printf CMDFILE "set ylabel 'Difference in distance (Gaia - Hipparcos) (pc) '  \n";
  printf CMDFILE "set xlabel 'Gaia distance (pc) ' \n";
  printf CMDFILE "set title 'Comparison of Gaia and Hipparcos distance measurements '\n";
  
  $cmd = "plot [0:500][-50:50]  ";
  $cmd .= "   './gaia_hip_comp.dat' using ((1000.0/\$4)):((1000.0/\$4)-(1000.0/\$9)) ";
  $cmd .= "      ps 0.1 lc rgbcolor \"red\"  t '' ";
  $cmd .= " , ";
  $cmd .= "    0 with lines lw 3 lc -1 t '' ";
  $cmd .= " , ";
  $cmd .= "   './gaia_hip_delta_dist.out' using ((1000.0/(0.5*(\$1+\$2)))):(\$4) ";
  $cmd .= "      with linespoints ps 2 pt 6 lw 3 lc rgbcolor \"blue\"  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 -flatten -background white -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }














exit 0;
