#!/usr/bin/perl
#
# Make a series of images which feature a random star field.
#    Pick one star in the field and shift it horizontally 
#    by some amount.  Create a series of images by shifting
#    it by larger and larger amounts.
#
# MWR 4/1/2012

use POSIX;

$debug = 1;

$outbase = "./img_a";
$starlist = "./img_mag.dat";
$num_images = 10;
# starting shift in pixel units
$start_shift = 0.01;
# ending shift in pixel units
$end_shift = 10.0;
# logarithmic interval for shifts
$d_log_shift = (log10($end_shift) - log10($start_shift)) / $num_images;
$start_log_shift = log10($start_shift);
if ($debug > 0) {
  printf " start_log_shift %6.3f   d_log_shift is %6.3f \n", 
       $start_log_shift, $d_log_shift;
}

# first, create the field and save the list of star positions 
$cmd = "pstar ";
$cmd .= " sky=100 photon ";
$cmd .= " nrow=500 ncol=500 ";
$cmd .= " outfile=$outbase ";
$cmd .= " starlist=$starlist ";
$cmd .= " fwhm=3.0 ";
$ret = exec_cmd($cmd);

# the star we're going to move is at (188, 379) = id #13.
#   so, we loop over a series of larger and larger shifts,
#   placing images successively into images with names
#        img_a_1.fts
#        img_a_2.fts
#    etc.
$star_id = 13;

for ($i = 0; $i < $num_images; $i++) {
  $log_shift = $start_log_shift + $i*$d_log_shift;
  $shift = pow(10.0, $log_shift);
  if ($debug > 0) {
    printf " i %5d  log_shift %6.3f  shift %6.3f \n", 
        $i, $log_shift, $shift;
  }

  # create a temp copy of the original star list,
  #    but modify the column position of item $star_id
  open(INFILE, "$starlist") || die("can't open file $starlist");
  $outfile = sprintf "%s_%d.dat", $outbase, $i;
  open(OUTFILE, ">$outfile") || die("can't open file $outfile");
  while (<INFILE>) {
    $line = $_;
    chomp($line);
    @words = split(/\s+/, " " . $line);
    if ($words[1] != $star_id) {
      printf OUTFILE "%s\n", $line;
    }
    else {
      $id = $words[1];
      $row = $words[2];
      $col = $words[3];
      $mag = $words[4];

      $col -= $shift;
      printf OUTFILE " %6d %9.2f %9.2f  %6.3f \n", $id, $row, $col, $mag;
    }
  }
  close(INFILE);
  close(OUTFILE);

  # and now generate an image from this modified list
  # first, create the field and save the list of star positions 
  $outimg = sprintf "%s_%d.fts", $outbase, $i;
  if ($debug > 0) {
    printf " using list %s to create image %s \n", $outfile, $outimg;
  }
  $cmd = "pstar ";
  $cmd .= " sky=100 photon ";
  $cmd .= " nrow=500 ncol=500 ";
  $cmd .= " outfile=$outimg ";
  $cmd .= " starlist=$outfile ";
  $cmd .= " fwhm=3.0 ";
  $ret = exec_cmd($cmd);

}





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


