#!/usr/bin/perl
# 
# Shift and add a few images of the comet to make a nice picture
#   (I hope)
#
# MWR 11/24/2018

$debug = 1;

$pos_file = "./comet_pos.txt";
$num_image = 0;
open(POS_FILE, "$pos_file") || die("can't open file $pos_file");
while (<POS_FILE>) {
  $line = $_;
  @words = split(/\s+/, $line);
  $id = $words[0];
  $row = $words[1];
  $col = $words[2];

  $img = sprintf "../comet46p_v-%04d.fit", $id;
  if (!-f $img) {
    printf "error, can't find image $img.  Quitting \n";
    exit(1);
  }

  $shift_img = sprintf "shift_%04d.fit", $id;

  if ($num_image == 0) {
    $zero_row = $row;
    $zero_col = $col;
    $cmd = "cp $img $shift_img";
    $ret = exec_cmd($cmd);
    $num_image++;
    next;
  }

  # shift to make the first image and then add
  $cmd = "cp $img $shift_img";
  $ret = exec_cmd($cmd);
  $drow = $zero_row - $row;
  $dcol = $zero_col - $col;
  $cmd = "imshift $shift_img dr=$drow dc=$dcol ";
  $ret = exec_cmd($cmd);

  $num_image++;
}
  

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


