#!/usr/bin/perl
#
# Modify the GI Del and HO Del datafiles from Henden to create a 
#    single big catalog with list of stars with known magnitudes.
#    Calculate the magnitude values from Henden's V and color values.
#
# MWR 10/31/2009

$debug = 0;

$v_mag_col = 7;
$bv_color_col = 8;
$ub_color_col = 9;
$vr_color_col = 10;
$ri_color_col = 11;

# print a header to help us keep track of the magnitudes
printf "#%4s %12.5s %12.5s  %6.3s %6.3s %6.3s %6.3s \n",
        "ID", "RA", "Dec", "B", "V", "R", "I";

$num_star = 0;
@file_list = ("gidel.dat", "hodel.dat");
foreach $file (@file_list) {
  
  if ($debug > 0) {
    printf "about to open file $file \n";
  }
  open(INFILE, "$file") || die("can't open file $file");
  while (<INFILE>) {
    $line = $_;
    if ($line =~ /^#/) {
      next;
    }
    $line = " " . $line;
    chomp($line);
    @words = split(/\s+/, $line);
    $ra = $words[2];
    $dec = $words[4];
    $vmag = $words[$v_mag_col];
    if ($vmag > 30) {
      if ($debug > 0) {
        printf " skipping because vmag %6.3f \n", $vmag;
      }
      next;
    }
  
    $bv_color = $words[$bv_color_col];
    if ($bv_color > 30) {
      if ($debug > 0) {
        printf " skipping because bv_color %6.3f \n", $bv_color;
      }
      next;
    }

    $vr_color = $words[$vr_color_col];
if (0 == 1) {
    if ($vr_color > 30) {
      if ($debug > 0) {
        printf " skipping because vr_color %6.3f \n", $vr_color;
      }
      next;
    }
}
    $ri_color = $words[$ri_color_col];
if (0 == 1) {
    if ($ri_color > 30) {
      if ($debug > 0) {
        printf " skipping because ri_color %6.3f \n", $ri_color;
      }
      next;
    }
}
  
    # okay, we can compute magnitudes in B, R, I
    $bmag = $vmag + $bv_color;
    if ($bmag > 30) {
      if ($debug > 0) {
        printf " skipping because bmag %6.3f \n", $bmag;
      }
      next;
    }
    if ($vr_color > 30) {
      $rmag = 99.0;
    }
    else {
      $rmag = $vmag - $vr_color;
    }
if (0 == 1) {
    if ($rmag > 30) {
      if ($debug > 0) {
        printf " skipping because rmag %6.3f \n", $rmag;
      }
      next;
    }
}
    if (($rmag > 30) || ($ri_color > 30)) {
      $imag = 99.0;
    }
    else {
      $imag = $rmag - $ri_color;
    }
if (0 == 1) {
    if ($imag > 30) {
      if ($debug > 0) {
        printf " skipping because imag %6.3f \n", $imag;
      }
      next;
    }
}
    
    # if we get this far, it's a good star.   Print out line line with info
    printf "%5d %12.5f %12.5f  %6.3f %6.3f %6.3f %6.3f \n",
          $num_star, $ra, $dec, $bmag, $vmag, $rmag, $imag;
  
    $num_star++;

  }
  close(INFILE);
}

exit 0;





##############################################################################
# PROCEDURE: exec_cmd
#
# DESCRIPTION: Execute the given shell command line.  If the $debug flag
#              is set, we print to stdout the command line, and
#              also print out the result string.
#
# RETURNS:
#              the result of the command
#
#
sub exec_cmd {

  my($cmd, $ret);

  $cmd = $_[0];

  if ($debug > 0) {
    printf "cmd is ..$cmd.. \n";
  }
  $ret = `$cmd`;
  if ($debug > 0) {
    printf "ret is ..$ret.. \n";
  }

  return($ret);
}


