# 
# Display thumbnails of the images from a given run.  This gives
#   the viewer a quick peek at all the data at once.
#

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

source param.tcl

#############################################################################
# Display thumbnails of all the "object" images on the screen at once.
#
# Args:
#       param_file            contains parameters controlling which images
#                                      we display, sub-section of image, etc.
#
# RETURNS
#   0               if all goes well
#   1               if there's a problem
#
proc thumbnail { param_file } {
  global debug
 
  if { $debug > 0 } {
    puts "entering thumbnail"
  }

  # the XVista program which displays an image lives here
  set xvista_dir [get_directory "xvista_dir"]
  if { $xvista_dir == {} } {
    puts stderr "thumbnail: get_directory fails for xvista_dir"
    return 1
  }

  # get information from the param file
  if { [file exists $param_file] != 1 } {
    puts "thumbnail: can't open param file $param_file"
    return 1
  }
  set input_dir [get_param $param_file "input_dir"]
  set output_dir [get_param $param_file "output_dir"]
  set zoom      [get_param $param_file "zoom"]
  set xlimit    [get_param $param_file "xlimit"]
  set ylimit    [get_param $param_file "ylimit"]
  set sleeptime [get_param $param_file "sleeptime"]
  set zero_sigma [get_param $param_file "zero_sigma"]
  set span_sigma [get_param $param_file "span_sigma"]

  # create the box we'll use to display image subsections
  set box_num [get_param $param_file "box_num"]
  set box_cr  [get_param $param_file "box_cr"]
  set box_cc  [get_param $param_file "box_cc"]
  set box_nr  [get_param $param_file "box_nr"]
  set box_nc  [get_param $param_file "box_nc"]
  exec $xvista_dir/box $box_num cr=$box_cr cc=$box_cc nr=$box_nr nc=$box_nc

  # initialize the XVista communications with the display
  exec $xvista_dir/propinit

  # calculate the size each thumbnail will appear on the screen
  set xo [expr $box_nc*$zoom]
  set yo [expr $box_nr*$zoom]

  # we loop over filters ...
  if { [get_list_info filter_list "filter"] != 0 } {
    puts stderr "thumbnail: get_info_list fails on 'filter'"
    return 1
  }
  foreach filter $filter_list {

    if { $debug > 1 } {
      puts "displaying images in filter $filter "
    }

    # create a list of files to display
    set type "object"
    set param_list [list [list "type" $type] \
                         [list "filter" $filter] ]
    set display_list [select_input_list $param_list]
  
    # figure out a reasonable zero and span value -- look at middle image
    set middle_index [llength $display_list]
    set middle_index [expr int($middle_index/2)]
    set test_image [lindex $display_list $middle_index]
    set cmdstr "exec $xvista_dir/sky $input_dir/$test_image bin=5"
    if { $debug > 1 } {
      puts "thumbnail: about to $cmdstr"
    }
    eval $cmdstr
    set sky [exec $xvista_dir/let sky]
    set skysig [exec $xvista_dir/let skysig]
    set zero [expr $sky-$zero_sigma*$skysig]
    set span [expr $span_sigma*$skysig]
    if { $debug > 1 } {
      puts "thumbnail: using file $test_image to estimate zero/span "
      puts [format "thumbnail: sky %7.0f skysig %4.0f  zero %7.0f span %4.0f" \
                              $sky $skysig $zero $span]
    }
  
    # display each one
    set xpos 0
    set ypos 0
    foreach img $display_list {
  
      set cmdstr "exec $xvista_dir/tv $input_dir/$img box=$box_num xo=$xpos yo=$ypos z=$zero l=$span zoom=$zoom &"
      puts "$cmdstr"
      eval $cmdstr 
      exec sleep $sleeptime
      set xpos [expr $xpos+$xo]
      if { $xpos > $xlimit } {
        set xpos 0
        set ypos [expr $ypos+$yo]
      }
  
    }

    # wait for the user to hit return, then kill all tv procs and move on
    puts "hit return to kill all 'tv' windows and continue ... "
    set fid [open "/dev/tty" "r"]
    set a [gets $fid]
    close $fid
    exec killall -1 tv

  }
  
  return 0
}
