#!/usr/bin/perl
#
# Remove items from .pht files which lie more than a given 
#   radial distance from some central point.
#   This allows us to remove data which may be strongly affected
#   by the vignetting in a wide field.
#
# Copy the original .pht file to the sub-dir "orig_pht", 
#   but modify the .pht file in the currect directory so it
#   doesn't contain the measurements in the outer regions.
#
# MWR 4/13/2016

$debug = 0;

# place copies of all .pht files into this subdir
$orig_subdir = "./orig_pht";
$center_row = 656;
$center_col = 1012;
# max allowed radial distance (pixels)
$max_rad = 500;

$tempfile = "./radial_cut.tmp";


# step 1: create subdir if needed
if (-d $orig_subdir) {
  # it does exist -- no need to do more
  if ($debug > 0) {
    printf " subdir $orig_subdir already exists \n";
  }
} 
else {
  # it does NOT yet exist -- so create it
  if ($debug > 0) {
    printf " subdir $orig_subdir does not exist, need to create \n";
  }
  mkdir($orig_subdir);
}


# now, go through all .pht files
$count = 0;
@pht_files = glob("*.pht");
foreach $pht_file (@pht_files) {

  # place a copy in the subdir, if it isn't there yet
  $copy = sprintf "$orig_subdir/$pht_file";

  if (-f $copy) {
    # already there -- do not overwrite
    if ($debug > 0) {
      printf "  no need to overwrite $copy \n";
    }
  }
  else {
    # need to write a copy into subdir
    if ($debug > 0) {
      printf "  we will place a copy into $copy \n";
    }
    $cmd = "cp $pht_file $orig_subdir";
    $ret = exec_cmd($cmd);
  }


  # now, go through this pht file and remove items which are too
  #   far from the center region
  open(PHT_FILE, "$pht_file") || die("can't open file $pht_file");
  open(TEMPFILE, ">$tempfile") || die("can't open file $tempfile");
  while (<PHT_FILE>) {
    $line = $_;
    if ($line =~ /^#/) {
      next;
    }
    @words = split(/\s+/, " " . $line);
    $row = $words[2];
    $col = $words[3];
    $dr = $row - $center_row;
    $dc = $col - $center_col;
    $dist = sqrt($dr*$dr + $dc*$dc);
    if ($dist > $max_rad) {
      # don't write this to the output file
      if ($debug > 0) {
        printf "  %8.2f %8.2f   yields rad %6.0f  don't write \n",
             $row, $col, $dist;
      }
    }
    else {
      # DO write this to the output file
      if ($debug > 0) {
        printf "  %8.2f %8.2f   yields rad %6.0f  DO  write \n",
             $row, $col, $dist;
      }
      printf TEMPFILE "%s", $line;
    }

  }
  close(TEMPFILE);
  close(PHT_FILE);

  # now, move the temp file to the original file
  $cmd = "cp $tempfile $pht_file";
  $ret = exec_cmd($cmd);

  $count++;

  # if ($count >= 2)  {
  #   exit 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);
}


