#!/usr/bin/perl
#
# Compute Johnson-Cousins R, I mags for some stars
#   given UCAC4 B, V, r, i
#   use Jester et al. (2005) conversion factors
#
# MWR 6/9/2016

$infile = "refstar_mags.dat";

open(INFILE, "$infile") || die("can't open file $infile");
while (<INFILE>) {
  $line = $_;
  if ($line =~ /^#/) {
    next;
  }
 
  @words = split(/\s+/, " " . $line);
  $id = $words[1];
  $bmag = $words[2];
  $bsig = $words[3];
  $vmag = $words[4];
  $vsig = $words[5];
  $sdss_rmag = $words[6];
  $sdss_rsig = $words[7];
  $sdss_imag = $words[8];
  $sdss_isig = $words[9];

  $sdss_ri = $sdss_rmag - $sdss_imag;

  $jc_rmag = $vmag - 1.09*($sdss_ri) - 0.22;
  $jc_rsig = sqrt(  $vsig*$vsig + $sdss_rsig*$sdss_rsig + $sdss_isig*$sdss_isig + 0.03*0.03);

  $jc_imag = $jc_rmag - 1.00*($sdss_ri) - 0.21;
  $jc_isig = sqrt(  $jc_rsig*$jc_rsig + $sdss_rsig*$sdss_rsig + $sdss_isig*$sdss_isig + 0.01*0.01);

  printf "%-5s  %6.3f %5.3f  %6.3f %5.3f  %6.3f %5.3f  %6.3f %5.3f  %6.3f %5.3f  %6.3f %5.3f \n",
      $id, $bmag, $bsig, $vmag, $vsig, $jc_rmag, $jc_rsig, $jc_imag, $jc_isig,
      $sdss_rmag, $sdss_rsig, $sdss_imag, $sdss_isig;


}
close(INFILE);



exit 0;
