#!/usr/bin/perl
#
# Compute absolute mags and colors for a set of ordinary stars
#    with good parallax values,
#    using a selection from Gaia and UCAC4 catalogs
#
# MWR 5/15/2017

use POSIX;
use Class::Struct;

$debug = 0;


struct ( Gaia => {  ucac4_name => '$', 
                    vartype => '$', 
                    parallax => '$', 
                    parallax_err => '$', 
                    dist => '$',
                    mean_g_mag => '$' } );
my @gaia_array = "";

struct ( Ucac4 => { ucac4_name => '$', 
                    vartype => '$', 
                    bmag => '$', 
                    vmag => '$', 
                    imag => '$' } );
my @ucac4_array = "";



# First, read in all stars from Gaia with good parallaxes
$num_gaia = 0;
$gaia_file = "gaia_A_goodp.csv";
open (INFILE, "$gaia_file") || die("can't open file $gaia_file");

while (<INFILE>) {
  $line = $_;
  if ($line =~ /^#/) {
    next;
  }

  @words = split(/,/, $line);
  $ucac4_name = $words[5];
  
  # reformat UCAC4 name to get rid of leading UCAC4-
  $new_name = $ucac4_name;
  $new_name =~ s/UCAC4-//;
  chomp($new_name);

  if ($debug > 0) {
    printf " orig %20s  new_name %20s \n", $ucac4_name, $new_name;
  }

  $gaia_parallax = $words[2];
  $gaia_parallax_err = $words[3];
  $gaia_g_mean_mag = $words[4];


  # check to see if the parallax is reliable
  if (($gaia_parallax > 0) && ($gaia_parallax_err > 0) &&
        ($gaia_parallax >= 4.0*$gaia_parallax_err)) {

    $dist = 1000.0 / $gaia_parallax;


    if ($debug > 0) {
      printf " %12s  , %8s  ,  %8.2f , %8.2f , %10.2f , %6.3f \n",
          $new_name, $vartype, $gaia_parallax, $gaia_parallax_err,
          $dist, $gaia_g_mean_mag;
    }

    $gaia_array[$num_gaia] = Gaia->new();
    $gaia_array[$num_gaia]->ucac4_name($new_name);
    $gaia_array[$num_gaia]->vartype($vartype);
    $gaia_array[$num_gaia]->parallax($gaia_parallax);
    $gaia_array[$num_gaia]->parallax_err($gaia_parallax_err);
    $gaia_array[$num_gaia]->dist($dist);
    $gaia_array[$num_gaia]->mean_g_mag($gaia_g_mean_mag);

    $num_gaia++;

  }

}
if ($debug > 0) {
  printf "num_gaia %5d \n", $num_gaia;
}



# now, we read in all the stars from UCAC4
$num_ucac4 = 0;
$ucac4_file = "ucac4_A_goodp.dat";
open (INFILE, "$ucac4_file") || die("can't open file $ucac4_file");

while (<INFILE>) {
  $line = $_;
  if ($line =~ /^#/) {
    next;
  }

  @words = split(/\s+/, $line);
  $ucac4_name = $words[6];

  $new_name = $ucac4_name;

  if ($debug > 0) {
    printf " orig %20s  new_name %20s \n", $ucac4_name, $new_name;
  }

  $ucac4_bmag = substr($line, 110, 6);
  $ucac4_vmag = substr($line, 117, 6);
  $ucac4_imag = substr($line, 131, 6);


  # check to see if the (B-V) mag  is reliable
  if (  ($ucac4_bmag > 0) && ($ucac4_bmag < 20) &&
        ($ucac4_vmag > 0) && ($ucac4_vmag < 20) ) {

    if ($debug > 0) {
      printf " %12s  ,  %6.3f , %6.3f , %6.3f \n",
          $new_name,  $ucac4_bmag, $ucac4_bmag, $ucac4_imag;
    }

    $ucac4_array[$num_ucac4] = Ucac4->new();
    $ucac4_array[$num_ucac4]->ucac4_name($new_name);
    $ucac4_array[$num_ucac4]->vartype($vartype);
    $ucac4_array[$num_ucac4]->bmag($ucac4_bmag);
    $ucac4_array[$num_ucac4]->vmag($ucac4_vmag);
    $ucac4_array[$num_ucac4]->imag($ucac4_imag);

    $num_ucac4++;

  }

}
if ($debug > 0) {
  printf "num_ucac4 %5d \n", $num_ucac4;
}



#
# and now, we walk through the array of Gaia stars, looking for a 
#    match in the array of UCAC4 star


for ($i_gaia = 0; $i_gaia < $num_gaia; $i_gaia++) {
#for ($i_gaia = 0; $i_gaia < 100; $i_gaia++) {

  $this_name = $gaia_array[$i_gaia]->ucac4_name;
  $found = 0;

  if ($debug > 0) {
    printf " searching for Gaia name %s \n", $this_name;
  }

  for ($i_ucac4 = 0; $i_ucac4 < $num_ucac4; $i_ucac4++) {

    if ($debug > 1) {
      printf "    UCAC4 name is  %s \n", $ucac4_array[$i_ucac4]->ucac4_name;
    }

    if ($ucac4_array[$i_ucac4]->ucac4_name eq $this_name) {

      # compute absolute mag in gaia band, and in V-band
      $dist_mod = 5.0*log10($gaia_array[$i_gaia]->dist) - 5.0;
      $abs_g = $gaia_array[$i_gaia]->mean_g_mag - $dist_mod;
      $abs_v = $ucac4_array[$i_ucac4]->vmag - $dist_mod;

      $bv_color = $ucac4_array[$i_ucac4]->bmag - $ucac4_array[$i_ucac4]->vmag;
      $vi_color = $ucac4_array[$i_ucac4]->vmag - $ucac4_array[$i_ucac4]->imag;

      printf "%12s %8s %6.3f %8.2f  %6.2f  %6.3f %6.3f   %6.3f \n",
            $this_name, $gaia_array[$i_gaia]->vartype,
            $gaia_array[$i_gaia]->mean_g_mag,
            $gaia_array[$i_gaia]->dist,
            $dist_mod,
            $abs_g, $abs_v, $bv_color;
 
     $found = 1;
     last;

    }

  }

  if ($debug > 0) {
    if ($found == 0) {
      printf " %12s  not found \n", $this_name;
    }
    else {
      printf " %12s  WAS found \n", $this_name;
    }
  }


}


exit 0;
