#!/usr/bin/perl
#
#
# Insert copies of a galaxy into an image
#     Do several times, modifying the galaxy so that it appears
#     to be at larger and larger distances
#
# Note that we fudge the multiplicative factors (slightly) so that the 
#     central pixel value is nearly constant.  Probably round-off
#     due to integer pixel values.
#
# MWR 3/10/2011

$debug = 1;

# create the field image to use as background
$cmd = "pstar sky=1000 photon nrow=500 ncol=500 field=10,6000,100,3.0";
$ret = exec_cmd($cmd);



# create a little image which contains just the galaxy
#    and has a background of zero
$gal = "ngc4865.fts";
$cmd = "sky $gal bin=3";
$ret = exec_cmd($cmd);
$orig = "gal.fts";
$box_nrow = 144;
$box_ncol = 96;
$cmd = "box 2 cr=124 cc=116 nr=$box_nrow nc=$box_ncol";
$ret = exec_cmd($cmd);
$cmd = "cp $gal $orig"; 
$ret = exec_cmd($cmd);
$cmd = "window $orig box=2";
$ret = exec_cmd($cmd);
$cmd = "sub $orig const=sky";
$ret = exec_cmd($cmd);


# this is the background field
$field = "pstar.fts";

# insert the galaxy's little image into the field at this location
$ins_row = 290;
$ins_col = 315;


# Insert the star with its original appearance
$new = "gal_1.fts";
$cmd = "cp $field $new";
$ret = exec_cmd($cmd);
$ins_col_1 = $ins_col-24;
$cmd = "implant $new $orig,$ins_row,$ins_col_1 overlay ";
$ret = exec_cmd($cmd);


# Insert a copy of the galaxy as if it were 2 times farther away
$temp = "temp.fts";
$cmd = "cp $orig $temp";
$ret = exec_cmd($cmd);
# this should be divided by 4, but divide by a bit less
$cmd = "div $temp const=40 mul=12 ";
$ret = exec_cmd($cmd);
$cmd = "bin $temp bin=2 ";
$ret = exec_cmd($cmd);
$new = "gal_2.fts";
$cmd = "cp $field $new";
$ret = exec_cmd($cmd);
$ins_row_2 = $ins_row + $box_nrow/4;
$ins_col_2 = $ins_row + $box_ncol/4;
$cmd = "implant $new $temp,$ins_row_2,$ins_col_2 overlay ";
$ret = exec_cmd($cmd);



# Insert a copy of the galaxy as if it were 4 times farther away
$temp = "temp.fts";
$cmd = "cp $orig $temp";
$ret = exec_cmd($cmd);
# this should be divided by 16, but divide by a bit less
$cmd = "div $temp const=160 mul=14 ";
$ret = exec_cmd($cmd);
$cmd = "bin $temp bin=4 ";
$ret = exec_cmd($cmd);
$new = "gal_3.fts";
$cmd = "cp $field $new";
$ret = exec_cmd($cmd);
$ins_row_3 = $ins_row + 3*$box_nrow/8;
$ins_col_3 = $ins_row + 3*$box_ncol/8;
$cmd = "implant $new $temp,$ins_row_3,$ins_col_3 overlay ";
$ret = exec_cmd($cmd);


# Insert a copy of the galaxy as if it were 8 times farther away
$temp = "temp.fts";
$cmd = "cp $orig $temp";
$ret = exec_cmd($cmd);
# this should be divided by 64, but divide by a bit less
$cmd = "div $temp const=640 mul=28 ";
$ret = exec_cmd($cmd);
$cmd = "bin $temp bin=8 ";
$ret = exec_cmd($cmd);
$new = "gal_4.fts";
$cmd = "cp $field $new";
$ret = exec_cmd($cmd);
$ins_row_4 = $ins_row + 7*$box_nrow/16;
$ins_col_4 = $ins_row + 7*$box_ncol/16;
$cmd = "implant $new $temp,$ins_row_4,$ins_col_4 overlay ";
$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);
}


