#!/usr/bin/perl
#
# Modify the uncertainties assigned to each measurement of RA and Dec
#   for GX And.  The goal is to reduce the _spread_ between the weights
#   given to different measurements.  We do the following, to each 
#   of the RA and Dec uncertainties in turn.
#
#     - if the uncertainty is < $min_sigma mas, change it to $min_sigma mas
#     - if the uncertainty is > $max_sigma80 mas, change it to $max_sigma mas
#     - otherwise, leave it unchanged
#
# Usage:  perl fudge_uncertainties.pl   infile
#
# MWR 8/8/2021

$debug = 0;

# change any uncertainty less than this to this value (mas)
$min_sigma = 40;
# change any uncertainty greater than this to this value (mas)
$max_sigma = 80;

if ($#ARGV != 0) {
  printf "usage: perl fudge_uncertainties  infile \n";
  exit(1);
}
$infile = $ARGV[0];
if ($debug > 0) {
  printf "infile is %s \n", $infile;
}


#########################
#  read in all the data from the original file
$num_pos = 0;
open(INFILE, "$infile") || die("can't open file $infile");
while (<INFILE>) {
  $line = $_;
  chomp($line);
  @words = split(/\s+/, $line);
  if ($#words != 4) {
    printf " invalid line follows, quitting \n";
    printf " %s\n", $line;
    exit(1);
  }

  $jd_array[$num_pos] = $words[0];
  $ra_pos_array[$num_pos] = $words[1];
  $ra_uncert_array[$num_pos] = $words[2];
  $dec_pos_array[$num_pos] = $words[3];
  $dec_uncert_array[$num_pos] = $words[4];

  $num_pos++;
}
close(INFILE);
if ($debug > 0) {
  printf " read %5d lines from %s \n", $num_pos, $infile;
}


###################################
#   Now check the values, modify as needed, print to stdout
for ($i = 0; $i < $num_pos; $i++) {

  if ($ra_uncert_array[$i] < $min_sigma) {
    $ra_uncert_array[$i] = $min_sigma;
  }
  if ($ra_uncert_array[$i] > $max_sigma) {
    $ra_uncert_array[$i] = $max_sigma;
  }
  if ($dec_uncert_array[$i] < $min_sigma) {
    $dec_uncert_array[$i] = $min_sigma;
  }
  if ($dec_uncert_array[$i] > $max_sigma) {
    $dec_uncert_array[$i] = $max_sigma;
  }

  printf "%12.5f  %8.1f  %6.1f  %8.1f  %6.1f \n",
      $jd_array[$i], $ra_pos_array[$i], $ra_uncert_array[$i],
          $dec_pos_array[$i], $dec_uncert_array[$i];

}



exit 0;



