#!/usr/bin/perl
#

$debug = 1;

foreach $filt ("b", "v", "r", "i") {
  $sigfile = sprintf "solve_ngc7790_4_%s.sig", $filt;
  $outfile = sprintf "./do_photom_ngc7790_%s.out", $filt;
  open(OUTFILE, ">$outfile") || die("can't open file $outfile");

  $template_pht = "./ngc7790_template.pht";
  
  # read in data from file with list of ID numbers and letters
  $letter_file = "./ngc7790_template.pht";
  $num_ref = 0;
  open(LETTER, "$letter_file") || die("can't open file $letter_file");
  while (<LETTER>) {
    $line = $_;
    @words = split(/\s+/, $line);
    $id = $words[1];
    $letter = $words[1];
    $id_array[$num_ref] = $id;
    $letter_array[$num_ref] = $letter;
    $num_ref++;
  }
  close(LETTER);
  
  # read in all data from the file with calibrated mags of all comp stars
#  $comp_file = "./ngc7790_hand.dat";
  $comp_file = "./ngc7790_stetson_hand.dat";
  $num_comp = 0;
  open(COMP, "$comp_file") || die("can't open file $comp_file");
  while (<COMP>) {
    $line =  " " . $_;
    @words = split(/\s+/, $line);
    $letter = $words[2];
    $bmag = $words[5];
    $vmag = $words[7];
    $rmag = $words[9];
    $imag = $words[11];
    
    $comp_letter_array[$num_comp] = $letter;
    $comp_bmag_array[$num_comp] = $bmag;
    $comp_vmag_array[$num_comp] = $vmag;
    $comp_rmag_array[$num_comp] = $rmag;
    $comp_imag_array[$num_comp] = $imag;
  
    $num_comp++;
  }
  
  
  
  # match the .pht file to the template
#  $cmd = "~/old/tass/match/match ";
  $cmd = "~/junk/match-0.16/match ";
#  $cmd .= " $phtfile 1 2 5 ";
  $cmd .= " $sigfile 5 6 1 ";
  $cmd .= " $template_pht 1 2 5 ";
  $cmd .= " id1=0 id2=0 ";
  $cmd .= " matchrad=5 ";
  $cmd .= " min_scale=0.98 max_scale=1.02 ";
  $cmd .= " rotangle=0.0 rottol=1.0 ";
  $cmd .= " halt_sigma=1 min_req_pairs=5 ";
  $ret = exec_cmd($cmd);
  
  
  # check to see if the match succeeded
  if ($ret !~ /^TRANS/) {
    # no, it failed
    printf " matched failed for $sigfile \n";
    exit(1);
  }
  
  
  # now print out one line per matched star,
  #    which provides the measured instrumental magnitude
  #    and then the B, V, R, I magnitudes, in that order
  $cmd = "paste matched.mtA matched.mtB > paste.out ";
  $ret = exec_cmd($cmd);
  open(PASTE, "paste.out");
  while (<PASTE>) {
    $line = $_;
    @words = split(/\s+/, $line);
    $instr_mag = $words[4];
    $id = $words[5];
    
    $found = 0;
    for ($i = 0; $i < $num_ref; $i++) {
      if ($id == $id_array[$i]) {
        $found = 1;
        $letter = $letter_array[$i];
        if ($debug > 0) {
          printf " found ID %3d  letter %3d \n", $id, $letter;
        }
        last;
      }
    }
    if ($found == 0) {
      printf " unable to find ID for %4d, line follows \n", $id;
      printf " %s", $line;
      exit(1);
    }
  
    $found = 0;
    for ($i = 0; $i < $num_comp; $i++) {
      if ($comp_letter_array[$i] eq $letter) {
        $found = 1;
        if ($debug > 0) {
          printf " found comp letter %3d = %3d \n", $letter, $i;
        }
        last;
      } 
    }
    if ($found == 0) {
      if ($debug > 0) {
        printf " can't find comp letter for $letter -- skipping \n";
      }
      next;
    }
  
    # print one line with ID, instr mag, B, V, R, I
    printf OUTFILE " %3d %3d  %7.3f  %6.3f %6.3f %6.3f %6.3f \n",
        $letter, $id, $instr_mag, 
        $comp_bmag_array[$i], 
        $comp_vmag_array[$i], 
        $comp_rmag_array[$i], 
        $comp_imag_array[$i];
  
  }
  

  close(OUTFILE);


}




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);
}


