#!/usr/bin/perl
#
# Create set of simulated nod-and-chop images 
#   Inputs are:   source.fit      
#                 tel_left.fit and tel_right.fit
#                 sky_a.fit   sky_b.fit   sky_c.fit
#                 empty.fit 
#
# MWR 10/6/2017

$debug = 1;


$do_noisy = 0;

if ($do_noisy == 1) {

		  ######################################################
		  #  This set results in a "noisy" image of the source,
		  #     because sky and telescope are not IDENTICAL
		  #     in all positions.


		  # create a fainter version of the source
		  $cmd = "cp source.fit source_faint.fit";
		  $ret = exec_cmd($cmd);
		  $cmd = "div source_faint.fit const=10 ";
		  $ret = exec_cmd($cmd);

		  # nod A, chop A: source is present
		  $output = "./noda_chopa.fit";
		  $cmd = "cp source_faint.fit $output ";
		  $ret = exec_cmd($cmd);
		  $cmd = "add $output sky_b.fit ";
		  $ret = exec_cmd($cmd);
		  $cmd = "add $output tel_right.fit ";
		  $ret = exec_cmd($cmd);

		  # nod A, chop B: off the source
		  $output = "./noda_chopb.fit";
		  $cmd = "cp zero.fit $output ";
		  $ret = exec_cmd($cmd);
		  $cmd = "add $output sky_a.fit ";
		  $ret = exec_cmd($cmd);
		  $cmd = "add $output tel_left.fit ";
		  $ret = exec_cmd($cmd);

		  # nod B, chop B: on the source
		  $output = "./nodb_chopb.fit";
		  $cmd = "cp source_faint.fit $output ";
		  $ret = exec_cmd($cmd);
		  $cmd = "add $output sky_b.fit ";
		  $ret = exec_cmd($cmd);
		  $cmd = "add $output tel_left.fit ";
		  $ret = exec_cmd($cmd);

		  # nod B, chop A: off the source
		  $output = "./nodb_chopa.fit";
		  $cmd = "cp zero.fit $output ";
		  $ret = exec_cmd($cmd);
		  $cmd = "add $output sky_c.fit ";
		  $ret = exec_cmd($cmd);
		  $cmd = "add $output tel_right.fit ";
		  $ret = exec_cmd($cmd);


		  # now create a couple of differences between chops at same nod
		  #   Diff1 = Nod_A_Chop_A - Nod_A_Chop_B
		  $cmd = "cp noda_chopa.fit diff1.fit";
		  $ret = exec_cmd($cmd);
		  $cmd = "sub diff1.fit noda_chopb.fit";
		  $ret = exec_cmd($cmd);

		  #   Diff2 = Nod_B_Chop_A - Nod_B_Chop_B 
		  $cmd = "cp nodb_chopa.fit diff2.fit";
		  $ret = exec_cmd($cmd);
		  $cmd = "sub diff2.fit nodb_chopb.fit";
		  $ret = exec_cmd($cmd);

		  # and finally, we put it all together by subtracting diff2 from diff1
		  $cmd = "cp diff1.fit result.fit ";
		  $ret = exec_cmd($cmd);
		  $cmd = "sub result.fit diff2.fit ";
		  $ret = exec_cmd($cmd);

}




if ($do_noisy == 0) {

		  ######################################################
		  #  This set results in a "perfect" image of the source,
		  #     because sky and telescope are in fact IDENTICAL
		  #     in all positions.


		  # create a fainter version of the source
		  $cmd = "cp source.fit source_faint.fit";
		  $ret = exec_cmd($cmd);
		  $cmd = "div source_faint.fit const=10 ";
		  $ret = exec_cmd($cmd);

		  # nod A, chop A: source is present
		  $output = "./noda_chopa.fit";
		  $cmd = "cp source_faint.fit $output ";
		  $ret = exec_cmd($cmd);
		  $cmd = "add $output sky_b.fit ";
		  $ret = exec_cmd($cmd);
		  $cmd = "add $output tel_right.fit ";
		  $ret = exec_cmd($cmd);

		  # nod A, chop B: off the source
		  $output = "./noda_chopb.fit";
		  $cmd = "cp zero.fit $output ";
		  $ret = exec_cmd($cmd);
		  $cmd = "add $output sky_b.fit ";
		  $ret = exec_cmd($cmd);
		  $cmd = "add $output tel_left.fit ";
		  $ret = exec_cmd($cmd);

		  # nod B, chop B: on the source
		  $output = "./nodb_chopb.fit";
		  $cmd = "cp source_faint.fit $output ";
		  $ret = exec_cmd($cmd);
		  $cmd = "add $output sky_b.fit ";
		  $ret = exec_cmd($cmd);
		  $cmd = "add $output tel_left.fit ";
		  $ret = exec_cmd($cmd);

		  # nod B, chop A: off the source
		  $output = "./nodb_chopa.fit";
		  $cmd = "cp zero.fit $output ";
		  $ret = exec_cmd($cmd);
		  $cmd = "add $output sky_b.fit ";
		  $ret = exec_cmd($cmd);
		  $cmd = "add $output tel_right.fit ";
		  $ret = exec_cmd($cmd);


		  # now create a couple of differences between chops at same nod
		  #   Diff1 = Nod_A_Chop_A - Nod_A_Chop_B
		  $cmd = "cp noda_chopa.fit diff1.fit";
		  $ret = exec_cmd($cmd);
		  $cmd = "sub diff1.fit noda_chopb.fit";
		  $ret = exec_cmd($cmd);

		  #   Diff2 = Nod_B_Chop_A - Nod_B_Chop_B 
		  $cmd = "cp nodb_chopa.fit diff2.fit";
		  $ret = exec_cmd($cmd);
		  $cmd = "sub diff2.fit nodb_chopb.fit";
		  $ret = exec_cmd($cmd);

		  # and finally, we put it all together by subtracting diff2 from diff1
		  $cmd = "cp diff1.fit result.fit ";
		  $ret = exec_cmd($cmd);
		  $cmd = "sub result.fit diff2.fit ";
		  $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);
}


