# This TCL script calculates the median of a set of FITS images.
#   It can be used to create a "master dark" frame, or a "master flat" frame
#    
# MWR 2/27/2000
#
# Added "xvista_dir" to all calls to XVista programs
#   --  MWR 2/9/2001

if { [catch { set debug } ] == 1 } {
  set debug 2
}


###########################################################################
# create a median image from the set of input images.
#
#      imagedir         directory in which image files are located
#      filelist         use these as input files
#      outfile          name of output median image
#      scale_arg        if   0, don't scale images before combining
#                       if > 0, use the value as a box number for 
#                                 calc'ing mean value for scaling
#                                 (i.e. if scale_arg is 5, use box 5
#                                  to scale images before combining)
#
# RETURNS
#    0           if all goes well
#    1           if there's a problem
#    
proc make_median { imagedir filelist outfile { scale_arg 0 } } {
  global debug

  if { $debug > 0 } {
    puts "entering make_median"
  }
  if { $debug > 1 } {
    puts "   imagedir $imagedir filelist $filelist outfile $outfile scale_arg $scale_arg"
  }

  # the XVista programs live in this directory
  set xvista_dir [get_directory "xvista_dir"]
  if { $xvista_dir == {} } {
    puts stderr "make_median: get_directory fails for xvista_dir"
    return 1
  }

  # first, we calculate mean values for each image
  #    and, at the same time, build up a command line for "median"
  set count 0
  set cmd "exec $xvista_dir/median "
  foreach file $filelist {
    set fullname $imagedir/$file
    if { [file exist $fullname] != 1 } {
      puts stderr "make_median: can't find file $fullname"
    }
    
    if { $scale_arg > 0 } {
      set box $scale_arg
      if { $debug > 1 } {
        puts "  make_median: calcing mean of $fullname in box $box"
      }
      set mean_val [calc_mean_in_box $fullname $box]
      if { $debug > 1 } {
        puts "  make_median: mean of $fullname is $mean_val"
      }
    }

    lappend cmd $fullname
    if { $debug > 1 } {
      puts "cmd is now $cmd"
    }
    incr count
  }
 

  # now, we calculate the median frame
  lappend cmd iqm outfile=$outfile 
  if { $scale_arg == 0 } {
    lappend cmd nomean
  }
  if { $debug > 1 } {
    puts "make_median: cmd is $cmd"
  }

  if { $debug > 0 } { 
    lappend cmd verbose
    eval $cmd
  } else {
    eval $cmd
  }


  # put some comments in the median frame
  set datestr [exec date]
  exec $xvista_dir/comment $outfile "created by make_median.tcl on $datestr"
  if { $scale_arg == 0 } {
    exec $xvista_dir/comment $outfile "a median of $count unscaled images "
  } else {
    exec $xvista_dir/comment $outfile "a median of $count scaled images "
  }


  return 0
}



#############################################################################
# Given a FITS file, and a box number (which has already been defined
#    to contain one of the edge columns), run the "mn" program to
#    calculate the mean value inside the box.
#    
# RETURNS
#    the mean value
#  
proc calc_mean_in_box { imagefile box } {

  # the XVista programs live in this directory
  set xvista_dir [get_directory "xvista_dir"]
  if { $xvista_dir == {} } {
    puts stderr "calc_mean_in_box: get_directory fails for xvista_dir"
    return 1
  }

  set vals [exec $xvista_dir/mn $imagefile box=$box]
  set ll [lindex $vals 0]
  set mean [lindex [split $ll =] 1]

  return $mean
}

