#!/usr/bin/perl
# 
# Examine the statistics of exoplanets, focusing on the parallax 
#    values of their host stars
#
# MWR 9/21/2016

$debug = 0;

$infile = "./planets_parallax.csv";

# require a measurement at least this many times uncert to be "good"
$minsig = 5.0;
# pick objects which are 
#    year_flag = 0  -->  any year
#    year_flag = 1  -->  year < $critical_year
#    year_flag = 2  -->  year >= $critical_year
$critical_year = 2010;
$year_flag = 2;

$num_tot = 0;
$num_no_parallax = 0;
$num_poor_value = 0;
$num_good_value = 0;


open(INFILE, "$infile") || die("can't open file $infile");
while (<INFILE>) {
  $line = $_;
  if ($line =~ /^#/) {
    next;
  }
  @words = split(/,/, $line);

  $year = $words[6];
  $parallax = $words[7];
  $parallax_posuncert = $words[8];
  $parallax_neguncert = $words[9];
  if ($debug > 0) {
    printf "  year %4d  pi %6.3f  pos %6.3f  neg %6.3f \n",
        $year, $parallax, $parallax_posuncert, $parallax_neguncert;
  }

  if ($year_flag == 1) {
    if ($year > $critical_year) {
      next;
    }
  }
  if ($year_flag == 2) {
    if ($year <= $critical_year) {
      next;
    }
  }

  if ($parallax eq "") {
    $num_no_parallax++;
  } else {
    if (($parallax - abs($minsig*$parallax_neguncert)) > 0) {
      $num_good_parallax++;
    } else {
      if ($debug > 0) {
        printf " parallax %6.3f  neguncert %6.3f  poor \n",
               $parallax, $parallax_neguncert;
      }
      $num_poor_parallax++;
    }
  }
  $num_tot++;

}
close(INFILE);

printf " num_tot            = %6d \n", $num_tot;
printf " num_no_parallax    = %6d    %6.2f \n",
             $num_no_parallax, ($num_no_parallax/$num_tot);
printf " num_poor_parallax  = %6d    %6.2f \n",
             $num_poor_parallax, ($num_poor_parallax/$num_tot);
printf " num_good_parallax  = %6d    %6.2f \n",
             $num_good_parallax, ($num_good_parallax/$num_tot);


exit 0;






    
