#!/usr/bin/perl
#

$match_dir = "~/junk/match/match-0.16";

$debug = 1;

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

  $template_pht = "./sn_template.pht";
  
  # read in data from file with list of ID numbers and letters
  $letter_file = "./sn_template.pht";
  $num_ref = 0;
  open(LETTER, "$letter_file") || die("can't open file $letter_file");
  while (<LETTER>) {
    $line = $_;
    if ($line =~ /^#/) {
      next;
    }
    @words = split(/\s+/, $line);
    $id = $words[1];
    $letter = $words[2];
    $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 = "./aavso_only_hand.dat";
  $num_comp = 0;
  open(COMP, "$comp_file") || die("can't open file $comp_file");
  while (<COMP>) {
    $line = $_;
    if ($line =~ /^#/) {
      next;
    }
    $line =  " " . $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++;
  }
  if ($debug > 0) {
    for ($i = 0; $i < $num_comp; $i++) {
      printf " read comp %4d %s  %6.3f %6.3f %6.3f %6.3f \n",
            $i, $comp_letter_array[$i],
            $comp_bmag_array[$i],
            $comp_vmag_array[$i],
            $comp_rmag_array[$i],
            $comp_imag_array[$i];
    }
  }
  
  
  # read the ID, mag, and uncert-in-mag values for all objects
  #   from the $sigfile
  $num_sig = 0;
  open(SIGFILE, "$sigfile") || die("can't open file $sigfile");
  while (<SIGFILE>) {
    $line = $_;
    if ($line =~ /^#/) {
      next;
    }
    @words = split(/\s+/, " " . $line);
    $sigfile_id_array[$num_sig] = $words[1];
    $sigfile_mag_array[$num_sig] = $words[2];
    $sigfile_magerr_array[$num_sig] = $words[3];
    $num_sig++;
  }
  close(SIGFILE);
  if ($debug > 0) {
    printf " here come the sigfile values for %s \n", $sigfile;
    for ($i = 0; $i < $num_sig; $i++) {
      printf "  %6d  %6.3f  %6.3f \n", 
          $sigfile_id_array[$i],
          $sigfile_mag_array[$i],
          $sigfile_magerr_array[$i];
    }
  }
  

  # match the .pht file to the template
#  $cmd = "~/old/tass/match/match ";
  $cmd = "$match_dir/match ";
#  $cmd .= " $phtfile 1 2 5 ";
  $cmd .= " $sigfile 5 6 1 ";
  $cmd .= " $template_pht 2 3 6 ";
  $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_id = $words[1];
    $instr_mag = $words[4];
    $id = $words[5];
    
    # look for the uncert in instr mag
    $found = 0;
    for ($i = 0; $i < $num_sig; $i++) {
      if ($sigfile_id_array[$i] == $instr_id) {
        if ($debug > 0) {
          printf " found sigfile_id_array [ %2d ] = %d \n",
                  $i, $instr_id;
        }
        $found = 1;
        if ($instr_mag != $sigfile_mag_array[$i]) {
          printf " error: instr_mag %6.3f != sigfile_mag_array %6.3f \n",
               $instr_mag, $sigfile_mag_array[$i];
          exit(1);
        }
        $instr_magerr = $sigfile_magerr_array[$i];
        last;
      }
    }
    if ($found == 0) {
      printf " unable to find magsig for %4d, line follows \n", $id;
      printf " %s", $line;
      exit(1);
    }
  


    $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 %2s \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 %2s = %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 " %2s %3d  %7.3f %6.3f   %6.3f %6.3f %6.3f %6.3f \n",
        $letter, $id, $instr_mag, $instr_magerr,
        $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);
}


