#!/usr/bin/perl
#
# Start with a data file with Gaia and Hip parallax values.
# compute difference delta = (Gaia DISTANCE - Hip DISTANCE)
# Sort stars by delta 
# Break up stars into bins by delta
# Compute median value of delta within each bin (if possible).
# Print table of results.
#
# MWR 5/23/2017

use POSIX;

$debug = 0;
# in case I need to cut-and-paste to get the tilde character
#$tt = "~";

$infile = "./gaia_hip_comp.dat";
$bin_width = 1.0;
$bin_start = 0.0;
$bin_end = 50.0;

# first, we read in all data from the file
$num_data = 0;
open(INFILE, "$infile") || die("can't open file $infile");
while (<INFILE>) {
  $line = $_;
  if ($line =~ /^#/) {
    next;
  }

  @words = split(/\s+/, " " . $line);
  $id_array[$num_data] = $words[1];
  $gaia_parallax = $words[4];
  $gaia_parallax_error = $words[5];
  $hip_parallax = $words[9];

  if ($gaia_parallax <= 0.0) {
    next;
  }
  if ($hip_parallax <= 0.0) {
    next;
  }

  $gaia_distance = 1000.0 / $gaia_parallax;
  $hip_distance = 1000.0 / $hip_parallax;


  $delta = $gaia_distance - $hip_distance;

  $gpara_array[$num_data] = $gaia_parallax;
  $delta_array[$num_data] = $delta;
  $sig_array[$num_data] = $gaia_parallax_error;
  $num_data++;
  
}
close(INFILE);
if ($debug > 0) {
  printf " read %d items from %s \n", $num_data, $infile;
  if ($debug > 0) {
    for ($i = 0; $i < $num_data; $i++) {
       printf " item %5d  ID %5d  gpara %10.2f delta %10.2f  sig %10.2f \n",
             $i, $id_array[$i], $gpara_array[$i], $delta_array[$i], $sig_array[$i];
    }
  }
}


for ($bin_top = $bin_start; $bin_top < $bin_end; $bin_top += $bin_width) {
  $bin_bot = $bin_top + $bin_width;

  @bin_array = "";

  $num_in_bin = 0;
  for ($i = 0; $i < $num_data; $i++) {
    $gpara = $gpara_array[$i];
    if (($gpara >= $bin_top) && ($gpara < $bin_bot)) {
      $bin_array[$num_in_bin] = $delta_array[$i];
      $num_in_bin++;
    }
  }

  if ($debug > 1) {
    printf "Bin top %6.1f bot %6.1f \n", $bin_top, $bin_bot;
    for ($i = 0; $i < $num_in_bin; $i++) {
      printf "  bin %5d  %10.2f \n", $i, $bin_array[$i];
    }
  }


  # now figure out the median value of all those in the "bin_array[]"
  if ($num_in_bin == 0) {
    $result = -1.0;
  }
  elsif ($num_in_bin == 1) {
    $result = $bin_array[0];
  }
  else {
    # sort the elements numerically, then pick the middle (or middle-1 if even)
    #   entry as the median value
    @sorted_array = sort { $a <=> $b } @bin_array;
    $index = floor($num_in_bin/2);
    if ($debug > 1) {
      printf "Bin top %6.1f bot %6.1f \n", $bin_top, $bin_bot;
      printf "  num_in_bin %d  index %d  \n", $num_in_bin, $index;
      for (my $j = 0; $j < $num_in_bin; $j++) {
        printf "      %10.2f \n", $sorted_array[$j];
      }
    }
    $result = $sorted_array[$index];
  }

  # print out one line for this bin
  printf " %6.2f %6.2f  %5d   %7.3f  \n", 
              $bin_top, $bin_bot, $num_in_bin, $result;
}









exit 0;
