#!/usr/bin/perl
# 
# apply a flatfield frame to set of images
#
# Modified so it doesn't look in FITS header for keyword FILTER,
#   since ST9 doesn't put this in if filterwheel is not attached.
#   MWR 6/21/2004
#
# Modified back so that this DOES look in FITS header for the FILTER.
#   If necessary, create a special version with a different name
#   (not "flatten.pl") if we work on FITS files without FILTER
#   in the header in the future.
#   MWR 6/15/2016

# if set to 0, will not actually do work, just print commands
$do_it = 1;

$debug = 1;

# XVista dir
$xvista_dir = "/home/richmond/xvista";
# WCSTOOLS dir
$wcs_dir = "/w/wiyn/wcstools-3.9.2/bin/";
# bait dir for picksym/putsym
$bait_dir = "/w/spiff2012/home/richmond/bait";

# determine the mean value of each flatfield 
#@filters = ("B", "V");
#@filters = ("clear");
@filters = ("I");
#@filters = ("B", "V", "R", "I");
foreach $filter (@filters) {
  $lc_filter = lc($filter);
  $flat = sprintf("flat%s.fts", $lc_filter);
  #$flat = sprintf("domeflat%s.fts", $lc_filter);
  printf "next flat is $flat\n";
  $cmd = "$xvista_dir/mn $flat";
  $ret = exec_cmd($cmd);
}

#@files1 = glob("NSV13514*.fit");
@files1 = glob("v404cyg*.fit");
#@files1 = glob("ngc4125*.fit");
#foreach $img (@files1) {
foreach $img (@files1, @files2, @files3) {
  printf "next image is $img \n";

   # pick out the filter for this image -- strip out white space from result
   #$filt = `$bait_dir/picksym file=$img filter`;
   $cmd = "$wcs_dir/gethead $img filter";
   $filt = exec_cmd($cmd);
   chomp($filt);
   $filt =~ s/\s+//;
   printf "image $img has filter $filt\n";
   if ($filt eq "empty") {
     printf " replacing filter 'empty' with 'clear' \n";
     $filt = "clear";
   }

   # just for V404 Cyg images
   $filt = "I";

   $flat = sprintf("flat%s.fts", lc($filt));
   #$flat = sprintf("domeflat%s.fts", lc($filt));
   #$flat = sprintf("flat%s_shift.fts", lc($filt));


  # double check that the file and corresponding flatfield image do exist
  if (-e $img) {
    # image exists, we'll do stuff to it in a moment
  } else {
    printf "file $img doesn't exist, skip it\n";
    next;
  }
  if (-e $flat) {
    # image exists, we'll do stuff to it in a moment
  } else {
    printf "flatfield file $flat doesn't exist, skip it\n";
    next;
  }


  $cmd = "$xvista_dir/div $img $flat flat";
  printf "cmd is $cmd\n";
  if ($do_it == 1) {
    $result = exec_cmd($cmd);
  }
  printf "result of div is ..$result..\n";

  $datestr = `date`;
  $comment = "divided by flat $flat on $datestr";
  $cmd = "$xvista_dir/comment $img $comment";
  printf "cmd is $cmd\n";
  if ($do_it == 1) {
    $result  = exec_cmd($cmd);
  }
  printf "result of comment is ..$result..\n";

}


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


