#!/usr/bin/perl
#
# create a picture which looks like a classic double-slit
#    interference pattern,
# then make copies with the pattern shifted a bit
#
# MWR 9/4/2008

$debug = 1;

$outfile = "sim_pattern.fts";
$tempfile = "./make_diagram.tmp";
$background_file = "./background.fts";
$nrow = 80;
$ncol = 700;
$background_nrow = 800;
$background_ncol = $ncol;
$spacing = 100.0;
$sky = 100;
$peak = 1000.0;
$fwhm = 8.0;
$stretch_col_fwhm = 30.0;

# create the background picture
$cmd =  "pstar nrow=$background_nrow ncol=$background_ncol ";
$cmd .= "  sky=$sky photon ";
$cmd .= "  outfile=$background_file ";
exec_cmd($cmd);

# create the basic picture
$cmd =  "pstar nrow=$nrow ncol=$ncol ";
$cmd .= " sky=$sky photon ";
$star_row = sprintf "%.0f", $nrow/2;
for ($col = $spacing; $col < $ncol; $col += $spacing) {
  $str = sprintf " star=$peak,$star_row,$col,$fwhm ";
  $cmd .= $str;
}
$cmd .= " outfile=$tempfile";
exec_cmd($cmd);

# now we stretch it in the column direction
$cmd =  "smooth $tempfile ";
$cmd .= " fwc=$stretch_col_fwhm ";
$cmd .= " outfile=$outfile ";
exec_cmd($cmd);


if (0 == 1) {

# next, we add copies of the pattern to the background
#    we shift the copies to show what happens
#    as instrument rotates
$shift_counter = 0;
$drow = 0 - $spacing;
$nshift = 2;
$fraction = 0.1;
for ($i_shift = 0; $i_shift <= $nshift; $i_shift++) {
  $drow += $spacing;
  $dcol = $spacing*$fraction*$i_shift;
  $cmd =  " implant $background_file ";
  $cmd .=  "    $outfile,$drow,$dcol overlay ";
  exec_cmd($cmd);
}
# now shift back to starting position
for ( ; $i_shift >= 0; $i_shift--) {
  $drow += $spacing;
  $dcol = $spacing*$fraction*$i_shift;
  $cmd =  " implant $background_file ";
  $cmd .=  "    $outfile,$drow,$dcol overlay ";
  exec_cmd($cmd);
}


exit 0;

}



# next, we make copies which are shifted by various
#   amounts from the original
$shift_counter = 0;
$nshift = 5;
$fraction = 0.1;
for ($i_shift = 0; $i_shift <= $nshift; $i_shift++) {

  # do a positive shift ...
  $shift_file = sprintf "shift_%d.fts", $i_shift;
  $dcol = 0.0 - $spacing*$fraction*$i_shift;
  printf " will shift by %5.0f cols \n", $dcol;
  $nr = $nrow;
  $sr = 0;
  $sc = ($spacing/2.0)+$dcol;
  $nc = $ncol - $spacing;
  $cmd = "box 1 sr=$sr nr=$nr sc=$sc nc=$nc ";
  exec_cmd($cmd);
  $cmd = "cp $outfile $shift_file ";
  exec_cmd($cmd);
  $cmd = "window $shift_file box=1 ";
  exec_cmd($cmd);

  # and a negative shift of the same size

}



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


