#!/usr/bin/perl
#
# Run "div2" on all .fit images
#
# MWR 6/21/2014

$debug = 1;

$div2_exe = "~/xvista/div2";
$wcs_dir = "/w/wiyn/wcstools-3.9.2/bin";
$tempfile = "./dodiv.tmp";


# use the same date for all comments -- should only take a few seconds,
#   anyway, so dates would be almost the same
$datestr = exec_cmd("date");
chomp($datestr);


@files = glob("*.fit");
#@files = glob("v627peg_a*.fit");
#@files = glob("dark90*.fit");
#@files2 = glob("dark30*.fit");
#@files = glob("domeflat*.fit");
#@files = (@files1, @files2);
#@files = glob("domeflat*.fit");
#@files = glob("maxi*.fit");
#@files = glob("domeflat_test*.fit");
#@files = glob("foo.fit");
#@files = glob("ngc4125*.fit");
foreach $file (@files) {
  if ($debug > 0) {
    printf "next is file $file \n";
  }
  
  $cmd = "$div2_exe $file outfile=$tempfile ";
  $ret = exec_cmd($cmd);
  $cmd = "mv $tempfile $file";
  $ret = exec_cmd($cmd);

  # and add a comment in the FITS header that we've divided it
  $cmd = "$wcs_dir/sethead $file comment='div2 divided by 2 on $datestr' ";
  $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);
}


