#!/usr/bin/perl
#
# Generate fake stars, overlay them into the real image, detect
#
# MWR 4/26/2011

$debug = 1;

srand(0);

$orig_file = "./sxdsr_16.fts";
$temp_file = "./fake_temp.fts";
$fake_file = "./fake.fts";
$list_file = "./fake_list.dat";
$coo_file = "./fake.coo";

$nrow = 2000;
$ncol = 2000;
$fwhm = 4.5;
$bright_mag = 13.0;
$start_mag = 13.0;
$end_mag = 20.0;
$d_mag = 0.25;
$num_fake = 100;
$zero = 0.0;

# step 1: make a copy of the original file
$cmd = "cp $orig_file $temp_file ";
$ret = exec_cmd($cmd);
$cmd = "chmod 644 $temp_file ";
$ret = exec_cmd($cmd);

# step 2: create a file with list of fake stars, range of mags and positions
for ($mag = $start_mag; $mag <= $end_mag; $mag += $d_mag) {

  open(LIST_FILE, ">$list_file") || die("can't open file $list_file");
  # first, we create a single fake star of mag $start_mag at center
  $row = $nrow/2;
  $col = $ncol/2;
  printf LIST_FILE " %6d %8.2f %8.2f  %6.3f  %5.2f %5.2f  %5.2f \n",
      $fake_counter, $row, $col, $bright_mag, $fwhm, $zero, $zero;

  $fake_counter = 0;
  for ($i = 0; $i < $num_fake; $i++) {
    $row_array[$fake_counter] = rand()*$nrow; 
    $col_array[$fake_counter] = rand()*$ncol; 
    printf LIST_FILE " %6d %8.2f %8.2f  %6.3f  %5.2f %5.2f  %5.2f \n",
         $fake_counter, $row_array[$fake_counter], $col_array[$fake_counter], 
         $mag, $fwhm, $zero, $zero;
    $fake_counter++;
  }
  close(LIST_FILE);
 
 
  # next, create an image with only the fake stars, background value zero,
  #    and no noise
  $cmd  = "pstar sky=0 rms=0 ";
  $cmd .= "  nrow=$nrow ncol=$ncol ";
  $cmd .= "  fwhm=$fwhm ";
  $cmd .= "  starlist=$list_file ";
  $cmd .= "  outfile=$fake_file ";
  $ret = exec_cmd($cmd);

#exit(0);


  # add the real image to the image with fake stars
  $cmd = "add $fake_file $temp_file ";
  $ret = exec_cmd($cmd);

exit(0);

  # now run "stars" on the fake+real image
  $cmd = "stars $fake_file ";
  $cmd .= " sky=-5 skysig=6 minsig=10 ";
  $cmd .= " outfile=$coo_file ";
  $ret = exec_cmd($cmd);


  # check to see if every fake input star appears in the output 
  $max_dr = 0.5;
  $max_dc = 0.5;
  $min_dr = 0.0 - $max_dr;
  $min_dc = 0.0 - $max_dc;
     # read in the list of all detected objects
     $num_in_fake_coo = 0;
     open(FAKE_COO, "$coo_file") || die("can't open file $coo_file");
     while (<FAKE_COO>) {
       $line = $_;
       @words = split(/\s+/, $line);
       $found_row_array[$num_in_fake_coo] = $words[2];
       $found_col_array[$num_in_fake_coo] = $words[3];
       $num_in_fake_coo++;
     }
     if ($debug > 0) {
       printf " num_in_fake_coo = %6d \n", $num_in_fake_coo;
     }
 
     # compare the input fake_row[] and fake_col[] arrays
     #    to the detected arrays
     $num_found = 0;
     for ($i = 0; $i < $fake_counter; $i++) {
       for ($j = 0; $j < $num_in_fake_coo; $j++) {
         $dr = $row_array[$i] - $found_row_array[$j];
         if (($dr > $min_dr) && ($dr < $max_dr)) {
           $dc = $col_array[$i] - $found_col_array[$j];
           if (($dc > $min_dc) && ($dc < $max_dc)) {
             # yes!  Found it
             $num_found++;
             if ($debug > 0) { 
               printf " %7.2f %7.2f  %7.2f %7.2f   %6.2f %6.2f  match %d \n",
                  $row_array[$i], $col_array[$i],
                  $found_row_array[$j], $found_col_array[$j],
                  $dr, $dc, $num_found;
             }
           }
         }
       }
     }
     if ($debug > 0) {
       printf " mag %6.2f  number found %5d out of %5d = %8.3f percent \n", 
        $mag, $num_found, $fake_counter, 
        ($num_found / $fake_counter)*100.0;
     }


  


}



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


