#!/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

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


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

@files1 = glob("iyuma-*.fit");
foreach $img (@files1) {
  printf "next image is $img \n";

   # pick out the filter for this image -- strip out white space from result
   $filt = `picksym file=$img filter`;
   $filt =~ s/\s+//;
   printf "image $img has filter $filt\n";
   $flat = sprintf("flat%s.fts", lc($filt));

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

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

}


exit 0;
  
